From ab3622e6729f823a019013fd7ad11658cdb23179 Mon Sep 17 00:00:00 2001 From: JacobBarthelmeh Date: Mon, 17 Feb 2025 10:07:39 -0700 Subject: [PATCH] fix for FD_SET call on pipes and handling of channel EOF --- apps/wolfsshd/wolfsshd.c | 53 ++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/apps/wolfsshd/wolfsshd.c b/apps/wolfsshd/wolfsshd.c index 1d4fb898..4794f3e6 100644 --- a/apps/wolfsshd/wolfsshd.c +++ b/apps/wolfsshd/wolfsshd.c @@ -816,7 +816,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, BOOL ret; word32 shellChannelId = 0; #ifndef EXAMPLE_BUFFER_SZ -#define EXAMPLE_BUFFER_SZ 4096 + /* default to try and read max packet size */ + #define EXAMPLE_BUFFER_SZ 32768 #endif byte shellBuffer[EXAMPLE_BUFFER_SZ]; int cnt_r, cnt_w; @@ -1166,7 +1167,8 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, pid_t childPid; #ifndef EXAMPLE_BUFFER_SZ - #define EXAMPLE_BUFFER_SZ 4096 + /* default to try and read max packet size */ + #define EXAMPLE_BUFFER_SZ 32768 #endif #ifndef MAX_IDLE_COUNT #define MAX_IDLE_COUNT 2 @@ -1431,23 +1433,23 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, FD_SET(sshFd, &writeFds); } - /* select on stdout/stderr pipes with forced commands */ - if (forcedCmd) { - FD_SET(stdoutPipe[0], &readFds); - if (stdoutPipe[0] > maxFd) - maxFd = stdoutPipe[0]; - - FD_SET(stderrPipe[0], &readFds); - if (stderrPipe[0] > maxFd) - maxFd = stderrPipe[0]; - } - else { - FD_SET(childFd, &readFds); - if (childFd > maxFd) - maxFd = childFd; - } - if (wolfSSH_stream_peek(ssh, tmp, 1) <= 0) { + /* select on stdout/stderr pipes with forced commands */ + if (forcedCmd) { + FD_SET(stdoutPipe[0], &readFds); + if (stdoutPipe[0] > maxFd) + maxFd = stdoutPipe[0]; + + FD_SET(stderrPipe[0], &readFds); + if (stderrPipe[0] > maxFd) + maxFd = stderrPipe[0]; + } + else { + FD_SET(childFd, &readFds); + if (childFd > maxFd) + maxFd = childFd; + } + rc = select((int)maxFd + 1, &readFds, &writeFds, NULL, NULL); if (rc == -1) break; @@ -1503,6 +1505,21 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh, break; } } + + /* did the channel just receive an EOF? */ + if (cnt_r == 0) { + int eof; + WOLFSSH_CHANNEL* current; + + current = wolfSSH_ChannelFind(ssh, lastChannel, + WS_CHANNEL_ID_SELF); + eof = wolfSSH_ChannelGetEof(current); + if (eof) { + /* SSH is done, kill off child process */ + kill(childPid, SIGKILL); + break; + } + } } /* if the window was previously full, try resending the data */