Skip to content

wolfsftp: apply the attributes SETSTAT and FSETSTAT acknowledge - #1197

Open
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f_11658
Open

wolfsftp: apply the attributes SETSTAT and FSETSTAT acknowledge#1197
yosuke-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
yosuke-wolfssl:fix/f_11658

Conversation

@yosuke-wolfssl

@yosuke-wolfssl yosuke-wolfssl commented Aug 25, 2026

Copy link
Copy Markdown
Contributor

Problem

SFTP_SetFileAttributes() and SFTP_SetFileAttributesHandle() left SIZE and
UIDGID as @TODO no-ops yet returned WS_SUCCESS, so RecvSetSTAT/RecvFSetSTAT
replied SSH_FX_OK for truncate and chown requests that never happened.
WSETTIME/WFSETTIME were (0) stubs on every port, so TIME was discarded too.

Closes f-11658.

Fix (src/wolfsftp.c)

Every requested attribute is applied or reported. A port with no wrapper yields
WS_UNIMPLEMENTED_E, answered as SSH_FX_OP_UNSUPPORTED; the 64-bit size is
bounds-checked by wResolveOffset() against WOLFSSH_MAX_FILE_OFFSET.

Flag POSIX Windows
SIZE truncate / ftruncate unsupported
UIDGID chown / fchown unsupported
PERM chmod / fchmod _wchmod via new WS_ChmodA()
TIME futimens / utimensat unsupported

Windows. SFTP paths arrive as /C:/..., which _chmod() rejects.
WS_ChmodA() strips the leading root via TrimFileName() as the other Windows
routines do, so WCHMOD works on Windows for the first time. It is left out
under _WIN32_WCE, which does not include <io.h>.

Client companion fix. wolfSSH_SFTP_CHMOD() echoed a whole STAT reply back,
so once the server honours SIZE a chmod asked it to truncate() the target —
which fails on a directory. It now sends only WOLFSSH_FILEATRB_PERM.

Tests

TestSftpSetStatAttributes() drives both handlers for size, timestamp and
ownership, asserting the on-disk result via WSTAT, with separate root and
non-root paths. tests/sftp.c adds an end-to-end chmod of a directory under a
fixed umask; tests/api.c's STATE_SET_ATR_SEND case sends PERM only.

Verification

  • Negative controls fail first: pre-fix the status is OK while the file is
    unchanged; without the client fix the directory chmod fails; stubbing
    WFCHOWN fails the give-away-chown case; removing WTRUNCATE fails the
    api.c case before the PERM narrowing.
  • regress.test passes as an ordinary user and under sudo. make check:
    10 pass, 1 skip, 2 fail — both scripts/{sftp,scp}.test, which pass on
    serial re-run (known parallel flake).
  • GCC preflight clean across 6 configs; ASan + UBSan clean. Windows MSVC builds
    with SFTP enabled, api-test/unit-test pass, and chmod 400/chmod 600
    over SFTP toggles the read-only attribute. OpenSSH interop: chmod on a
    directory and put -p both correct.

Not in this PR

  • No 32-bit build was run locally; the wResolveOffset() bound only bites where
    SIZEOF_OFF_T != 8.
  • SFTP_SetFileAttributes() stops at the first failing attribute rather than
    continuing best-effort, and the UNSUPPORTED mapping has no POSIX test since
    it is reachable only on ports lacking the wrappers.

@yosuke-wolfssl yosuke-wolfssl self-assigned this Aug 25, 2026
Copilot AI lite review requested due to automatic review settings August 25, 2026 02:07

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.

Pull request overview

Fixes SFTP SETSTAT/FSETSTAT so the server only acknowledges attributes it actually applies (size/uid-gid/timestamps), returning SSH_FX_OP_UNSUPPORTED when a port lacks the needed wrappers. Also adjusts the built-in SFTP client’s chmod behavior to avoid unintentionally requesting unrelated attribute changes.

Changes:

  • Implement SIZE, UIDGID, and TIME application in SFTP_SetFileAttributes() / SFTP_SetFileAttributesHandle(), with WS_UNIMPLEMENTED_E mapped to SSH_FX_OP_UNSUPPORTED.
  • Add portable wrappers in port.h for truncate/chown and real WSETTIME/WFSETTIME implementations when supported.
  • Add regression and end-to-end tests covering SETSTAT/FSETSTAT attribute application and directory chmod behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
wolfssh/port.h Adds WTRUNCATE/WFTRUNCATE, WCHOWN/WFCHOWN, and functional WSETTIME/WFSETTIME wrappers where supported.
src/wolfsftp.c Applies SETSTAT/FSETSTAT attributes (or reports unsupported), and fixes client-side chmod to send only PERM.
tests/regress.c Adds a regression test ensuring SETSTAT/FSETSTAT really change size/time/ownership on disk.
tests/sftp.c Adds an end-to-end chmod-directory test to guard the client fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/wolfsftp.c Outdated
Comment thread src/wolfsftp.c Outdated

@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 #1197

Scan targets checked: wolfssh-bugs, wolfssh-src

Findings: 2
2 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread src/wolfsftp.c Outdated
Comment thread tests/regress.c
Comment thread src/wolfsftp.c Outdated
Comment thread tests/regress.c

@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 #1197

Scan targets checked: wolfssh-bugs, wolfssh-src

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread tests/regress.c
Comment thread tests/regress.c

@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 #1197

Scan targets checked: wolfssh-bugs, wolfssh-src

Findings: 1
1 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Reported findings require changes before merge.

Comment thread tests/sftp.c
- SFTP_SetFileAttributes() and SFTP_SetFileAttributesHandle() carry
  out the size, ownership, permission and timestamp requests while ret
  is WS_SUCCESS, bound the size with wResolveOffset() against
  WOLFSSH_MAX_FILE_OFFSET, and set WS_UNIMPLEMENTED_E where the port
  defines no wrapper.
- wolfSSH_SFTP_RecvSetSTAT() and wolfSSH_SFTP_RecvFSetSTAT() answer
  WOLFSSH_FTP_UNSUPPORTED for WS_UNIMPLEMENTED_E; wolfSSH_SFTP_CHMOD()
  sets the attribute flags to WOLFSSH_FILEATRB_PERM before sending.
- port.h adds WTRUNCATE, WFTRUNCATE, WCHOWN and WFCHOWN for the POSIX
  port, and defines WSETTIME and WFSETTIME over the existing WUTIMES
  and WFUTIMES helpers in place of their (0) definitions.
- SFTP_SetMode() guards on _WIN32_WCE in place of USE_WINDOWS_API, and
  port.c adds WS_ChmodA(), which trims the SFTP leading root and calls
  _wchmod(); it and the WCHMOD mapping to it are left out under
  _WIN32_WCE, which keeps the _chmod mapping.
- tests/regress.c adds TestSftpSetStatAttributes(); tests/sftp.c adds
  a chmod of a directory and installs SFTP_TEST_UMASK in place of the
  sftpTestUmask static; the STATE_SET_ATR_SEND case in tests/api.c
  sets atr.flags to WOLFSSH_FILEATRB_PERM.

Issue: F-11658
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.

4 participants