echoserver: close the agent socket on reset and handle a rekey - #1209
Merged
Conversation
The worker drops back to APP_STATE_LISTEN when an agent connection ends, but never closes the socket. The next accept() overwrites agentFd, so every agent connection after the first leaks the previous descriptor. The forward path has the same gap on its connection-reset arm, where the socket is closed but fwdFd keeps the closed number. A rekey was treated as a read failure and ended the session. It cannot just be skipped either: wolfSSH_worker() reports WS_REKEYING in place of WS_CHAN_RXD while keying, and nothing raises the data report again, so ignoring it strands whatever arrived in that call and the peer waits on an answer that never comes. This is the hazard the library already calls out for WS_EXTDATA, which is exempted from the same override. - close agentFd and clear it on both the read-zero and the ECONNRESET/ECONNABORTED arms - clear fwdFd on the forward reset arm, matching the read-zero arm - clear agentCtx.appFd and fwdCtx.appFd wherever the worker closes the socket, so the stored copy cannot outlive the descriptor - drain the channel on WS_REKEYING as well as WS_CHAN_RXD, and take an empty read as "nothing buffered" rather than a failure on that path. wolfSSH_ChannelIdRead() has no isKeying gate and the window credit it owes is parked until the rekey completes
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes correctness issues in the echoserver example’s multiplexing loop by preventing socket descriptor leaks when agent/forward connections reset/close, and by correctly draining buffered channel data during SSH rekeying so sessions don’t stall.
Changes:
- Close and invalidate
agentFdon read-0 and reset/abort paths; also clearthreadCtx->agentCtx.appFd. - Clear
fwdFdon the forward reset/abort path (matching the read-0 path) and clearthreadCtx->fwdCtx.appFdwherever the socket is closed. - Treat
WS_REKEYINGlikeWS_CHAN_RXDfor purposes of draining per-channel buffered data, and treat an empty drain during rekey as non-fatal.
Suppressed comments (2)
examples/echoserver/echoserver.c:1062
- Same issue as the shell-path:
cnt_r <= 0treats negativewolfSSH_ChannelIdRead()results as “nothing buffered” duringWS_REKEYING, which can hide real errors.
if (cnt_r <= 0) {
/* Nothing was buffered. Only an actual data
* report makes that a failure. */
if (rc == WS_REKEYING)
continue;
examples/echoserver/echoserver.c:1086
- Same issue as the other
WS_REKEYINGdrain paths:cnt_r <= 0will ignore negativewolfSSH_ChannelIdRead()return values during rekey, which can mask real failures.
if (cnt_r <= 0) {
/* Nothing was buffered. Only an actual data
* report makes that a failure. */
if (rc == WS_REKEYING)
continue;
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
wolfSSH_ChannelIdRead() returns a negative value for a real error, and the rekey arm treated that the same as a zero read. Restrict the continue to cnt_r == 0 so an error still ends the loop.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1209
Scan targets checked: wolfssh-bugs, wolfssh-src
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
philljj
approved these changes
Aug 28, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The worker drops back to
APP_STATE_LISTENwhen an agent connection ends, but never closes the socket. The nextaccept()overwritesagentFd, so every agent connection after the first leaks the previous descriptor. The forward path has the same gap on its connection-reset arm, where the socket is closed butfwdFdkeeps the closed number.A rekey was treated as a read failure and ended the session. It cannot just be skipped either:
wolfSSH_worker()reportsWS_REKEYINGin place ofWS_CHAN_RXDwhile keying, and nothing raises the data report again, so ignoring it strands whatever arrived in that call and the peer waits on an answer that never comes. This is the hazard the library already calls out forWS_EXTDATA, which is exempted from the same override.agentFdand clear it on both the read-zero and the ECONNRESET/ECONNABORTED armsfwdFdon the forward reset arm, matching the read-zero armagentCtx.appFdandfwdCtx.appFdwherever the worker closes the socket, so the stored copy cannot outlive the descriptorWS_REKEYINGas well asWS_CHAN_RXD, and take an empty read as "nothing buffered" rather than a failure on that path.wolfSSH_ChannelIdRead()has noisKeyinggate and the window credit it owes is parked until the rekey completesExample program only. The echoserver's send paths still treat
WS_REKEYINGas fatal; that needs a held-output buffer and is left for separate work.