From a1732e5f6a42494fb2851acb053d4cc4e79dcba2 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 2 Nov 2026 14:56:32 -0700 Subject: [PATCH 1/3] echoserver: classify scp exec with the library wsExecStartCb() now takes an exec request as a transfer only when wolfSSH_ChannelCommandIsScp() does, the same test wolfSSH_accept() diverts on. A command such as "scpbackup" is served as a session. - tests: test_wolfSSH_SCP_ExecNotScp echoes data over a "scpbackup" exec channel Issue: F-14206 --- examples/echoserver/echoserver.c | 8 ++-- tests/api.c | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index e1b1cfb28..f75d1d0a9 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -1137,15 +1137,14 @@ static int wsExecStartCb(WOLFSSH_CHANNEL* channel, void* vCtx) int rej = 1; if (vCtx && channel) { - const char* cmd = wolfSSH_ChannelGetSessionCommand(channel); - if (SessionInUse((thread_ctx_t*)vCtx)) { return 1; } #ifdef WOLFSSH_SCP - /* The prefix ChannelCommandIsScp() matches, so both modes agree. */ - if (cmd != NULL && WSTRNCMP(cmd, "scp", 3) == 0) { + /* The test wolfSSH_accept() diverts on, so a command granted here + * as a transfer is served as one. */ + if (wolfSSH_ChannelCommandIsScp(channel) == 1) { /* An scp command the transfer cannot be run for is refused * rather than served as a session. */ if (TransferChannel((thread_ctx_t*)vCtx, channel)) { @@ -1158,7 +1157,6 @@ static int wsExecStartCb(WOLFSSH_CHANNEL* channel, void* vCtx) { rej = wsShellStartCb(channel, vCtx); } - (void)cmd; } return rej; diff --git a/tests/api.c b/tests/api.c index 58bfcb333..47c519baf 100644 --- a/tests/api.c +++ b/tests/api.c @@ -9446,8 +9446,83 @@ static void test_wolfSSH_SCP_SourceOpenFail(void) { ; } static void test_wolfSSH_SCP_SourceSingleFifo(void) { ; } #endif /* !USE_WINDOWS_API */ +/* An exec command that only starts with "scp" is not a transfer: the + * echoserver serves it as a session and echoes the data back. */ +static void test_wolfSSH_SCP_ExecNotScp(void) +{ + func_args ser; + tcp_ready ready; + int argsCount; + int ret; + WS_SOCKET_T clientFd; +#ifdef USE_WINDOWS_API + DWORD rcvTimeout = 20000; +#else + struct timeval rcvTimeout; +#endif + const char* args[6]; + WOLFSSH_CTX* ctx = NULL; + WOLFSSH* ssh = NULL; + byte msg[] = "scpbackup echo"; + byte got[sizeof(msg)]; + word32 gotSz = 0; + THREAD_TYPE serThread; + + WMEMSET(&ser, 0, sizeof(func_args)); + argsCount = 0; + args[argsCount++] = "."; + args[argsCount++] = "-1"; + args[argsCount++] = "-f"; + args[argsCount++] = "-p"; + args[argsCount++] = "0"; + ser.argv = (char**)args; + ser.argc = argsCount; + ser.signal = &ready; + InitTcpReady(ser.signal); + ThreadStart(echoserver_test, (void*)&ser, &serThread); + WaitTcpReady(&ready); + + scp_client_connect(&ctx, &ssh, ready.port, "scpbackup"); + AssertNotNull(ctx); + AssertNotNull(ssh); + + clientFd = wolfSSH_get_fd(ssh); +#ifdef USE_WINDOWS_API + (void)setsockopt(clientFd, SOL_SOCKET, SO_RCVTIMEO, + (const char*)&rcvTimeout, sizeof(rcvTimeout)); +#else + rcvTimeout.tv_sec = 20; + rcvTimeout.tv_usec = 0; + (void)setsockopt(clientFd, SOL_SOCKET, SO_RCVTIMEO, + &rcvTimeout, sizeof(rcvTimeout)); +#endif + + ret = wolfSSH_stream_send(ssh, msg, (word32)sizeof(msg)); + AssertIntEQ(ret, (int)sizeof(msg)); + while (gotSz < (word32)sizeof(msg)) { + ret = wolfSSH_stream_read(ssh, got + gotSz, + (word32)sizeof(got) - gotSz); + if (ret <= 0) + break; + gotSz += (word32)ret; + } + AssertIntEQ(gotSz, (word32)sizeof(msg)); + AssertIntEQ(XMEMCMP(got, msg, sizeof(msg)), 0); + + ret = wolfSSH_shutdown(ssh); + (void)ret; + + clientFd = wolfSSH_get_fd(ssh); + WCLOSESOCKET(clientFd); + wolfSSH_free(ssh); + wolfSSH_CTX_free(ctx); + ThreadJoin(serThread); + FreeTcpReady(&ready); +} + #else /* WOLFSSH_SCP && !NO_WOLFSSH_CLIENT && !SINGLE_THREADED && * !NO_FILESYSTEM && !WOLFSSH_SCP_USER_CALLBACKS && !WOLFSSH_ZEPHYR */ +static void test_wolfSSH_SCP_ExecNotScp(void) { ; } static void test_wolfSSH_SCP_ReKey(void) { ; } static void test_wolfSSH_SCP_SourceWarning(void) { ; } static void test_wolfSSH_SCP_SourceAbort(void) { ; } @@ -11141,6 +11216,7 @@ int wolfSSH_ApiTest(int argc, char** argv) test_wolfSSH_SCP_SourceSkipMidFile(); test_wolfSSH_SCP_SourceOpenFail(); test_wolfSSH_SCP_SourceSingleFifo(); + test_wolfSSH_SCP_ExecNotScp(); /* SFTP tests */ test_wolfSSH_SFTP_SendReadPacket(); From 0fd59cf3a2353fddd2d40ad25de5a6e7876f938c Mon Sep 17 00:00:00 2001 From: John Safranek Date: Mon, 2 Nov 2026 14:56:32 -0700 Subject: [PATCH 2/3] echoserver: run the shell as the logged-in user When the echoserver runs as root, the forkpty child hands the pty slave to the authenticated user, mode 0620, so the shell can reopen its terminal, then takes that user's groups, gid and uid and closes every descriptor above stderr before it execs the shell. It exits without a shell if any step fails or root can be regained. - configure: check for closefrom(), with a close() loop as fallback - a server that is not root starts the shell as before Issue: F-8857 --- configure.ac | 9 +++++++ examples/echoserver/echoserver.c | 41 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/configure.ac b/configure.ac index 42cf4b943..23e5bebdb 100644 --- a/configure.ac +++ b/configure.ac @@ -172,6 +172,15 @@ AC_LINK_IFELSE( AC_MSG_RESULT([yes])], [AC_MSG_RESULT([no])]) +# closefrom(): glibc 2.34+, BSDs, Solaris; not macOS. +AC_MSG_CHECKING([for closefrom]) +AC_LINK_IFELSE( + [AC_LANG_PROGRAM([[#include ]], + [[closefrom(3);]])], + [AC_DEFINE([HAVE_CLOSEFROM], [1], [closefrom() available and declared]) + AC_MSG_RESULT([yes])], + [AC_MSG_RESULT([no])]) + AC_MSG_CHECKING([for utimensat]) AC_LINK_IFELSE( [AC_LANG_PROGRAM([[#include diff --git a/examples/echoserver/echoserver.c b/examples/echoserver/echoserver.c index f75d1d0a9..3a7a7eea2 100644 --- a/examples/echoserver/echoserver.c +++ b/examples/echoserver/echoserver.c @@ -85,8 +85,11 @@ #include #endif #ifndef USE_WINDOWS_API + #include #include + #include #include + #include #endif #include #if defined(__QNX__) || defined(__QNXNTO__) @@ -1020,6 +1023,27 @@ static int wsShellStartCb(WOLFSSH_CHANNEL* channel, void* ctx) system("env"); #endif + /* Started as root, the shell runs as the user who logged in, + * or not at all. Otherwise the process has no root to give + * up. */ + if (geteuid() == 0) { + /* Give the user its terminal, so it can reopen it by + * name. */ + if (fchown(STDIN_FILENO, p_passwd->pw_uid, (gid_t)-1) != 0 + || fchmod(STDIN_FILENO, 0620) != 0) { + _exit(EXIT_FAILURE); + } + if (initgroups(p_passwd->pw_name, p_passwd->pw_gid) != 0 + || setgid(p_passwd->pw_gid) != 0 + || setuid(p_passwd->pw_uid) != 0) { + _exit(EXIT_FAILURE); + } + if (p_passwd->pw_uid != 0 + && (setuid(0) == 0 || seteuid(0) == 0)) { + _exit(EXIT_FAILURE); + } + } + setenv("HOME", p_passwd->pw_dir, 1); setenv("LOGNAME", p_passwd->pw_name, 1); rc = chdir(p_passwd->pw_dir); @@ -1029,6 +1053,23 @@ static int wsShellStartCb(WOLFSSH_CHANNEL* channel, void* ctx) _exit(EXIT_FAILURE); } + /* keep only the pty on 0-2; don't leak the server's sockets */ + #ifdef HAVE_CLOSEFROM + closefrom(STDERR_FILENO + 1); + #else + { + long fd; + long maxFd = sysconf(_SC_OPEN_MAX); + + if (maxFd < 0) { + maxFd = 1024; + } + for (fd = STDERR_FILENO + 1; fd < maxFd; fd++) { + (void)close((int)fd); + } + } + #endif + execv("/bin/sh", (char **)args); _exit(EXIT_FAILURE); } From 42852b069e223f0b8baf076d5141abfd1a7b2342 Mon Sep 17 00:00:00 2001 From: John Safranek Date: Wed, 18 Nov 2026 10:07:02 -0800 Subject: [PATCH 3/3] scp: fix the send callback zero-return comments A send callback may return 0 once in a row at any point while file data remains, not only on the metadata call; a return that sends data resets the stall detector. The WS_CallbackScpSend doc and the comment at the stall check in DoScpSource now say so. Issue: F-13331 --- src/wolfscp.c | 11 ++++++----- wolfssh/wolfscp.h | 14 +++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index 2a0273be4..49e2eb592 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -960,11 +960,12 @@ int DoScpSource(WOLFSSH* ssh) continue; } else if (ssh->scpFileOffset < ssh->scpFileSz) { - /* A send callback may hand back 0 bytes once, on the call - * that only fills in metadata. Twice running with file - * data still outstanding means it cannot make progress, - * and looping back to SCP_TRANSFER would spin here with no - * socket I/O at all, so fail instead. */ + /* A send callback may hand back 0 bytes once in a row, + * after metadata or after data; sending data resets the + * count. Twice running with file data still outstanding + * means it cannot make progress, and looping back to + * SCP_TRANSFER would spin here with no socket I/O at all, + * so fail instead. */ if (ssh->scpNoProgress) { WLOG(WS_LOG_ERROR, scpError, "send callback made no progress", WS_SCP_ABORT); diff --git a/wolfssh/wolfscp.h b/wolfssh/wolfscp.h index 38284fff7..b08caab47 100644 --- a/wolfssh/wolfscp.h +++ b/wolfssh/wolfscp.h @@ -130,13 +130,13 @@ typedef int (*WS_CallbackScpRecv)(WOLFSSH* ssh, int state, * "fileNameSz" is the capacity of the "fileName" buffer, not the length of any * name already in it. * - * Returning 0 is only valid on the call that fills in metadata (file name, - * mode, times, totalFileSz) and has no data ready yet; the library then sends - * the file header and calls back with WOLFSSH_SCP_CONTINUE_FILE_TRANSFER. A - * second 0 in a row while "fileOffset" is still short of "totalFileSz" is - * treated as a stalled callback and aborts the transfer, since there is no - * "no data right now" status in this API. Callbacks that can block should do - * so rather than returning 0. + * Returning 0 while "fileOffset" is still short of "totalFileSz" makes the + * library call back once with WOLFSSH_SCP_CONTINUE_FILE_TRANSFER; on the call + * that fills in metadata (file name, mode, times, totalFileSz) it sends the + * file header first. A second 0 in a row aborts the transfer as stalled, and + * any return that hands back data resets that count. There is no "no data + * right now" status in this API, so callbacks that can block should do so + * rather than returning 0. * * Returning WS_SCP_CONTINUE in place of an entry skips it: the message set * with wolfSSH_SetScpErrorMsg() goes to the peer as a warning record. A