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) { 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/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: 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);