port: resolve the full SFTP offset in the Harmony and Zephyr ports - #1172
port: resolve the full SFTP offset in the Harmony and Zephyr ports#1172yosuke-wolfssl wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes incorrect SFTP read/write behavior at offsets ≥ 4 GiB by ensuring the Harmony and Zephyr filesystem ports assemble and validate the full 64-bit (hi/lo) SFTP offset before seeking, preventing silent truncation to the low 32 bits.
Changes:
- Update Harmony and Zephyr
wPread()/wPwrite()implementations to resolve the split offset viawResolveOffset()before seeking. - Add a Zephyr-only unit test that verifies offsets at exactly 4 GiB are rejected when the port’s seek type cannot represent them.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/port.c |
Uses wResolveOffset() before seeking in Harmony and Zephyr ports to avoid dropping the high offset word. |
tests/unit.c |
Adds a Zephyr-gated unit test that exercises the “offset ceiling rejection” path at 4 GiB. |
Suppressed comments (1)
src/port.c:153
- Same issue as wPwrite(): Harmony seeks with a signed 32-bit offset, so validating against WOLFSSH_MAX_FILE_OFFSET (based on off_t) can allow offsets that will later be truncated by the (int32_t) cast. Guard with an explicit 32-bit maximum for SYS_FS_FileSeek.
if (wResolveOffset(shortOffset, WOLFSSH_MAX_FILE_OFFSET,
&offset) == 0) {
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
ab71720 to
cc7a304
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1172
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. Findings are non-blocking.
cc7a304 to
ed39288
Compare
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #1172
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. Findings are non-blocking.
- Assemble the split offset with wResolveOffset() in the Harmony wPread and wPwrite, and seek with the resolved value. - Assemble the split offset with wResolveOffset() in the Zephyr wPread and wPwrite, and seek with the resolved value. - Define WOLFSSH_MAX_FILE_OFFSET as 0x7FFFFFFF in the Harmony block, so the ceiling comes from SYS_FS_FileSeek's int32_t offset rather than from off_t. - Add test_PreadPwriteOffsetCeiling() covering ports whose seek type cannot reach 4 GiB, including a read back at an in-range offset. - Add test_ResolveOffset() covering offset assembly, both sides of the ceiling, and the NULL guards. Issue: F-8823
ed39288 to
aea6b5e
Compare
Problem
wPread()/wPwrite()receive the SFTP file offset as two 32-bit words, lowword first, and
SFTP_Read/SFTP_Writepropagate carry into the high word.The Harmony and Zephyr ports seeked with
shortOffset[0]alone, silentlymasking any offset at or past 4 GiB — an SFTP client reading past that boundary
gets data from the wrong part of the file, and a write corrupts it. PR #1166
fixed the two POSIX ports and added the shared helper; these are the next two
ports in that sequence.
Fix (
src/port.c,wolfssh/port.h)Both ports now assemble the offset with
wResolveOffset()before seeking:WOLFSSH_MAX_FILE_OFFSET, then casts toint32_tforSYS_FS_FileSeek. The existingWFSEEK_SUCCESScheck isunchanged.
z_fds_mutexand casts tooff_tforfs_seek, replacing theword32*cast ofshortOffset. The fd-range check,mutex discipline, and seek return check are unchanged.
The Harmony block also defines
WOLFSSH_MAX_FILE_OFFSETas0x7FFFFFFF, aheadof the
off_t-derived default, so the ceiling comes fromSYS_FS_FileSeek'sint32_tparameter rather than from a type this port never uses. It keeps the#ifndefguard, so the macro stays user-overridable like the rest ofport.h.An offset above the ceiling now returns
-1, surfacing asWOLFSSH_FTP_FAILUREfor that one request instead of truncating silently.Nucleus, the
fseekfallback, and FATFS are intentionally not in this PR —each also needs a sequential-position or unchecked-return fix beyond the helper.
Tests (
tests/unit.c)test_PreadPwriteHighOffset()needsmkstempand a 4 GiB sparse file, so itexcludes both ports. Two tests replace that coverage:
test_ResolveOffset()checks the shared helper directly — offsetassembly from both words, the accepted and rejected sides of the ceiling, and
the
NULLguards. It takesmaxOffsetas a parameter, so it needs nofilesystem and runs in the ordinary Linux and macOS jobs.
test_PreadPwriteOffsetCeiling()exercises the Zephyr port end to end onthe sample's 64 KiB RAM disk: write at offset 0, read back at offset 8 to
confirm the resolved offset reaches the seek, then confirm a read and a write
at exactly 4 GiB both fail.
Verification
-Werror: all 6 configs.3; forcing
fs_seekto offset 0 also fails, so the read back has teeth.test_ResolveOffset()kills three helper mutants:>changed to>=, thehigh word dropped, and the comparison replaced by
shortOffset[1] != 0.src/port.cwas compiled against Microchip's generatedsys_fs.hand driven by a host harness:0x100and0x7FFFFFFFreach theseek unchanged, 2 GiB and 4 GiB are rejected without seeking. Reverting the
fix makes the 4 GiB case seek to 0.
Closes f-8823 for these two ports.