diff --git a/ncat/ncat_exec_win.c b/ncat/ncat_exec_win.c index cf392cb9e..d927f2814 100644 --- a/ncat/ncat_exec_win.c +++ b/ncat/ncat_exec_win.c @@ -434,6 +434,12 @@ static DWORD WINAPI subprocess_thread_func(void *data) n = ncat_recv(&info->fdn, buffer, sizeof(buffer), &pending); if (n <= 0) { + /* return value can be 0 without meaning EOF in some cases such as SSL + * renegotiations that require read/write socket operations but do not + * have any application data. */ + if(n == 0 && fdn->lasterr == 0) { + continue; /* Check pending */ + } goto loop_end; } n_r = n; diff --git a/ncat/ncat_listen.c b/ncat/ncat_listen.c index dc1959fbf..6c78da336 100644 --- a/ncat/ncat_listen.c +++ b/ncat/ncat_listen.c @@ -674,7 +674,7 @@ int read_socket(int recv_fd) /* return value can be 0 without meaning EOF in some cases such as SSL * renegotiations that require read/write socket operations but do not * have any application data. */ - if(n == 0 && fdn->lasterr != 0) { + if(n == 0 && fdn->lasterr == 0) { continue; /* Check pending */ } close_fd(fdn, n == 0); @@ -742,7 +742,7 @@ static void read_and_broadcast(int recv_fd) /* return value can be 0 without meaning EOF in some cases such as SSL * renegotiations that require read/write socket operations but do not * have any application data. */ - if(n == 0 && fdn->lasterr != 0) { + if(n == 0 && fdn->lasterr == 0) { continue; /* Check pending */ } close_fd(fdn, n == 0); diff --git a/ncat/ncat_posix.c b/ncat/ncat_posix.c index 62db3c5bd..a76648b5a 100644 --- a/ncat/ncat_posix.c +++ b/ncat/ncat_posix.c @@ -220,9 +220,18 @@ void netexec(struct fdinfo *info, char *cmdexec) do { n_r = ncat_recv(info, buf, sizeof(buf), &pending); - if (n_r <= 0) + if (n_r <= 0) { + /* return value can be 0 without meaning EOF in some cases such as SSL + * renegotiations that require read/write socket operations but do not + * have any application data. */ + if(n_r == 0 && info->lasterr == 0) { + continue; /* Check pending */ + } goto loop_end; - write_loop(child_stdin[1], buf, n_r); + } + r = write_loop(child_stdin[1], buf, n_r); + if (r != n_r) + goto loop_end; } while (pending); } if (checked_fd_isset(child_stdout[0], &fds)) { @@ -235,9 +244,11 @@ void netexec(struct fdinfo *info, char *cmdexec) if (fix_line_endings((char *) buf, &n_r, &crlf, &crlf_state)) wbuf = crlf; } - ncat_send(info, wbuf, n_r); + r = ncat_send(info, wbuf, n_r); if (crlf != NULL) free(crlf); + if (r <= 0) + goto loop_end; } } loop_end: