Repository navigation
sftp: fix client state teardown and results - #1371
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the SFTP state cleanup and error-reporting changes.
No open issues 🎉
What changed in this PR
This PR fixes SFTP client state cleanup and error reporting, including interrupted operations and failed automatic negotiation.
Changes:
- Preserve operation state when negotiation fails, and release resources held by interrupted LS and GET operations.
- Return appropriate errors for malformed STAT replies and failed RMDIR requests.
- Add regression tests, including a guard allocator to detect use-after-free writes and leaks.
| File | Description |
|---|---|
tests/unit.c |
Adds regression coverage for state teardown, resource cleanup, and error reporting. |
src/wolfsftp.c |
Corrects cleanup ownership and STAT/RMDIR error handling. |
Review effort: Balanced
09f84dc to
51e3950
Compare
|
@claude review |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1371
Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Lite
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Additional findings (outside the current diff — GitHub can't attach inline comments there):
-
🟣
src/wolfsftp.c— pre-existing: An interrupted PUT still leaks its local file when the session is freed. This PR fixes the same leak for GET only. The STATE_ID_PUT branch of wolfSSH_SFTP_ClearState frees putState without closing state->fl (or fileHandle on Windows). Fix: give WS_SFTP_PUT_STATE a localOpened flag. Set it after WFOPEN/WS_CreateFileA succeeds in STATE_PUT_OPEN_LOCAL and clear it in STATE_PUT_CLOSE_LOCAL. Have ClearState close the file when the flag is set, as the new GET branch at src/wolfsftp.c:881 does. Without the flag, a never-opened or already-closed handle would be closed.Why this was flagged
wolfSSH_SFTP_Put opens the local source file in STATE_PUT_OPEN_LOCAL (WFOPEN at src/wolfsftp.c:10554, or WS_CreateFileA on Windows). It can then return WS_FATAL_ERROR and keep putState for a retry in two places: when wolfSSH_SFTP_Open hits WS_WANT_READ/WS_WANT_WRITE in STATE_PUT_OPEN_REMOTE (around 10610), and when NoticeError fires after wolfSSH_SFTP_SendWritePacket in STATE_PUT_WRITE. If the application gives up and calls wolfSSH_free, wolfSSH_SFTP_free runs wolfSSH_SFTP_ClearState(ssh, STATE_ID_ALL) at src/wolfsftp.c:10824. Its STATE_ID_PUT branch (966-971) only zeroes and frees putState, so the FILE*/HANDLE leaks every time. That is exactly the GET leak this PR closes at 881-887. WS_SFTP_PUT_STATE has no localOpened field, so ClearState cannot tell whether the file is open. STATE_PUT_CLOSE_LOCAL (10742) is the only place that closes it, and it is never reached on this path. Behaviour is the same as on the base branch, so this is pre-existing. Long-running clients that retry or abandon PUTs on non-blocking sockets slowly run out of descriptors.
Verification: A non-blocking client gets WS_WANT_READ/WS_WANT_WRITE in wolfSSH_SFTP_Put after the local file is open, then frees the session instead of retrying. The STATE_ID_PUT branch of wolfSSH_SFTP_ClearState (src/wolfsftp.c:966-970) only runs WS_FORCEZERO and WFREE on putState and never closes state->fl, so the FILE*/HANDLE leaks. The base has the same leak by the same route, so merging makes nothing worse.
A failed automatic negotiation now frees only the VERSION receive state, leaving the calling operation's state for it to clean up. wolfSSH_SFTP_ClearState releases what LS, GET, and PUT hold across calls. - SFTP_STAT fails with WS_BUFFER_E on a malformed ATTRS reply - RMDIR returns WS_PERMISSIONS on a permission failure and sets WS_SFTP_STATUS_NOT_OK for other statuses - ClearState frees the LS name list and closes the local file of an interrupted GET or PUT - tests: unit cases for each, with a guard allocator catching the write after free and the LS leak Issue: F-17045, F-17049, F-17046, F-17048
51e3950 to
3c054c7
Compare
|
Fixed the PUT leak too. The PUT state now tracks whether its local file is open, and ClearState closes it. A new unit test covers an interrupted PUT followed by a session free. |
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1371
Scan targets checked: wolfssh-src, wolfssh-bugs
Coverage: 2 of 2 in-scope changed file(s) opened by the reviewer
Fenrir result: Approved ✅
No new issues found in the changed files.
Advisory only — this automated result does not count as a GitHub approval.
Review tier: Max
A failed automatic negotiation frees only the VERSION state, leaving the
caller's operation state for it to clean up, and ClearState releases
what LS and GET hold across calls.
Issue: F-17045, F-17046, F-17048, F-17049