From 49d6815cdf929f57937fe3d2adfc0b79e6f06ccc Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 11:18:34 -0800 Subject: [PATCH 1/3] Keep highwater callback results out of counts HighwaterCheck() hands the application callback's result back through wolfSSH_SendPacket() into wolfSSH_stream_read(), which returns it as a byte count. A positive result now comes back as WS_SUCCESS. - sftp: check buffer_set_size() in SendReadPacket's remainder loop, so a count over the request ends the read instead of overrunning out - tests: cover positive and negative callback results in ByteHighwater Issue: CID-662152 --- src/internal.c | 5 +++++ src/wolfsftp.c | 13 +++++++++++-- tests/unit.c | 29 ++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/internal.c b/src/internal.c index 71c540ee5..56bceea61 100644 --- a/src/internal.c +++ b/src/internal.c @@ -712,6 +712,11 @@ static INLINE int HighwaterCheck(WOLFSSH* ssh, byte side) if (fire && ssh->ctx->highwaterCb) ret = ssh->ctx->highwaterCb(side, ssh->highwaterCtx); + /* The result travels back through calls that return byte counts, such + * as wolfSSH_stream_read(), so a positive one would read as data. */ + if (ret > 0) + ret = WS_SUCCESS; + return ret; } diff --git a/src/wolfsftp.c b/src/wolfsftp.c index 062dce2c3..4a088127e 100644 --- a/src/wolfsftp.c +++ b/src/wolfsftp.c @@ -9066,9 +9066,18 @@ int wolfSSH_SFTP_SendReadPacket(WOLFSSH* ssh, byte* handle, word32 handleSz, break; } + /* A count over the request wraps to a growth, which + * set_size refuses. */ + if (wolfSSH_SFTP_buffer_set_size(&state->buffer, + wolfSSH_SFTP_buffer_size(&state->buffer) + - (word32)ret) != WS_SUCCESS) { + WLOG(WS_LOG_SFTP, "Read more data than requested"); + ssh->error = WS_BUFFER_E; + ret = WS_FATAL_ERROR; + state->state = STATE_SEND_READ_CLEANUP; + break; + } state->recvSz += ret; - wolfSSH_SFTP_buffer_set_size(&state->buffer, - wolfSSH_SFTP_buffer_size(&state->buffer) - ret); } while (wolfSSH_SFTP_buffer_size(&state->buffer) != 0); if (ret < 0) { diff --git a/tests/unit.c b/tests/unit.c index 7d3ec5911..d6a7db5e5 100644 --- a/tests/unit.c +++ b/tests/unit.c @@ -5316,18 +5316,22 @@ typedef struct HwTestCtx { WOLFSSH* ssh; int count; int keying; + int ret; byte lastSide; } HwTestCtx; static int HwTestCb(byte side, void* ctx) { HwTestCtx* hc = (HwTestCtx*)ctx; + int ret = WS_SUCCESS; + if (hc != NULL) { hc->count++; hc->lastSide = side; hc->keying = wolfSSH_RekeyPending(hc->ssh); + ret = hc->ret; } - return WS_SUCCESS; + return ret; } /* Exercise the wolfSSH_*MsgHighwater APIs and the per-key packet-count @@ -5533,6 +5537,29 @@ static int test_ByteHighwater(void) goto done; } + /* A positive callback result is not a status: it would reach callers + * that return byte counts, so it comes back as WS_SUCCESS. A negative + * one passes through. */ + if (wolfSSH_SetHighwater(ssh, 1024) != WS_SUCCESS) { + result = -829; + goto done; + } + hc.ret = 4096; + ssh->highwaterFlag = 0; + ssh->txCount = 1024; + if (wolfSSH_TestHighwaterCheck(ssh, WOLFSSH_HWSIDE_TRANSMIT) != WS_SUCCESS + || hc.count != 3) { + result = -830; + goto done; + } + hc.ret = WS_WANT_WRITE; + ssh->highwaterFlag = 0; + if (wolfSSH_TestHighwaterCheck(ssh, WOLFSSH_HWSIDE_TRANSMIT) + != WS_WANT_WRITE || hc.count != 4) { + result = -831; + goto done; + } + done: wolfSSH_free(ssh); wolfSSH_CTX_free(ctx); From a1f1326d076b0f67990ae7e29a07df516cb6623f Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 11:18:34 -0800 Subject: [PATCH 2/3] client: end readPeer on a failed rekey worker wolfSSH_stream_read() keeps answering WS_REKEYING while a key exchange runs, so a transport that dies mid-rekey is reported only by the wolfSSH_worker() call that drives it. readPeer() now ends the session on any worker result that is not a session status, instead of spinning. Issue: CID-662153 --- examples/client/client.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/examples/client/client.c b/examples/client/client.c index b7e35519d..22b3a0b04 100644 --- a/examples/client/client.c +++ b/examples/client/client.c @@ -673,8 +673,19 @@ static THREAD_RET readPeer(void* in) } #endif /* WOLFSSH_AGENT */ else if (ret == WS_REKEYING) { - wolfSSH_worker(args->ssh, NULL); - ret = 0; + /* Drive the key exchange. A read keeps answering + * WS_REKEYING while it runs, so a transport failure + * shows up only here; end the session on it. */ + ret = wolfSSH_worker(args->ssh, NULL); + if (ret == WS_FATAL_ERROR) { + ret = wolfSSH_get_error(args->ssh); + } + if (ret == WS_SUCCESS || ret == WS_WANT_READ + || ret == WS_WANT_WRITE || ret == WS_REKEYING + || ret == WS_CHAN_RXD || ret == WS_EXTDATA + || ret == WS_EOF || ret == WS_CHANNEL_CLOSED) { + ret = 0; + } } } else if (ret != WS_EOF) { From 6a68a2a48a92a4934597271c3d56c144de42f35e Mon Sep 17 00:00:00 2001 From: John Safranek Date: Thu, 19 Nov 2026 14:15:56 -0800 Subject: [PATCH 3/3] scp: drop dead stores in ReceiveScpMessage The receive loop looks for the newline in scpRecvMsgSz bytes and sets sz from the newline offset after the loop, so copying scpRecvMsgSz into sz after each read is never used. Issue: CID-662317 --- src/wolfscp.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wolfscp.c b/src/wolfscp.c index 49e2eb592..51747001d 100644 --- a/src/wolfscp.c +++ b/src/wolfscp.c @@ -1987,7 +1987,6 @@ int ReceiveScpMessage(WOLFSSH* ssh) if (sz <= 0) return sz; ssh->scpRecvMsgSz += sz; - sz = ssh->scpRecvMsgSz; continue; } @@ -2004,7 +2003,6 @@ int ReceiveScpMessage(WOLFSSH* ssh) return sz; } ssh->scpRecvMsgSz += sz; - sz = ssh->scpRecvMsgSz; break; case WS_EXTDATA: