Skip to content

sftp: fix client state teardown and results - #1371

Merged
philljj merged 1 commit into
wolfSSL:masterfrom
ejohnstown:sftp-client-state
Nov 16, 2026
Merged

philljj merged 1 commit into
wolfSSL:masterfrom
ejohnstown:sftp-client-state

Conversation

@ejohnstown

Copy link
Copy Markdown
Contributor

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.

  • STAT fails with WS_BUFFER_E on a malformed ATTRS reply
  • RMDIR reports WS_PERMISSIONS and WS_SFTP_STATUS_NOT_OK correctly
  • unit tests, with a guard allocator for the use after free and leak

Issue: F-17045, F-17046, F-17048, F-17049

Copilot AI balanced review requested due to automatic review settings November 3, 2026 16:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@ejohnstown

Copy link
Copy Markdown
Contributor Author

@claude review

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
@ejohnstown

Copy link
Copy Markdown
Contributor Author

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 wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@philljj philljj self-assigned this Nov 15, 2026
@philljj
philljj merged commit c4f6f89 into wolfSSL:master Nov 16, 2026
222 checks passed
@ejohnstown
ejohnstown deleted the sftp-client-state branch November 16, 2026 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants