From 70aabdc67c495af452f13fadb42ae3a1cbb5e43b Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 4 May 2023 17:49:11 +0100 Subject: [PATCH] Fix segfault if SSH connection terminates very early. Introduced in the previous commit. The new ssh_ppl_final_output method shouldn't be called in any of the error cleanup functions if ssh->base_layer is NULL, which it can be if we haven't got far enough through the connection to set up any packet protocol layers at all. (For example, ECONNREFUSED would do it.) --- ssh/ssh.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/ssh/ssh.c b/ssh/ssh.c index 0af5668c..836661d6 100644 --- a/ssh/ssh.c +++ b/ssh/ssh.c @@ -474,7 +474,8 @@ void ssh_remote_error(Ssh *ssh, const char *fmt, ...) if (ssh->base_layer || !ssh->session_started) { GET_FORMATTED_MSG; - ssh_ppl_final_output(ssh->base_layer); + if (ssh->base_layer) + ssh_ppl_final_output(ssh->base_layer); /* Error messages sent by the remote don't count as clean exits */ ssh->exitcode = 128; @@ -494,7 +495,8 @@ void ssh_remote_eof(Ssh *ssh, const char *fmt, ...) if (ssh->base_layer || !ssh->session_started) { GET_FORMATTED_MSG; - ssh_ppl_final_output(ssh->base_layer); + if (ssh->base_layer) + ssh_ppl_final_output(ssh->base_layer); /* EOF from the remote, if we were expecting it, does count as * a clean exit */ @@ -519,7 +521,8 @@ void ssh_proto_error(Ssh *ssh, const char *fmt, ...) if (ssh->base_layer || !ssh->session_started) { GET_FORMATTED_MSG; - ssh_ppl_final_output(ssh->base_layer); + if (ssh->base_layer) + ssh_ppl_final_output(ssh->base_layer); ssh->exitcode = 128; @@ -538,7 +541,8 @@ void ssh_sw_abort(Ssh *ssh, const char *fmt, ...) if (ssh->base_layer || !ssh->session_started) { GET_FORMATTED_MSG; - ssh_ppl_final_output(ssh->base_layer); + if (ssh->base_layer) + ssh_ppl_final_output(ssh->base_layer); ssh->exitcode = 128; @@ -557,7 +561,8 @@ void ssh_user_close(Ssh *ssh, const char *fmt, ...) if (ssh->base_layer || !ssh->session_started) { GET_FORMATTED_MSG; - ssh_ppl_final_output(ssh->base_layer); + if (ssh->base_layer) + ssh_ppl_final_output(ssh->base_layer); /* Closing the connection due to user action, even if the * action is the user aborting during authentication prompts,