Skip to content

fs/vfs: validate parameters and preserve 64-bit counts in sendfile - #22128

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-sendfile-posix-validation
Nov 21, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-sendfile-posix-validation

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 and Linux specifications (sendfile(2)) parameter validation, bounds checking, and pointer safety hardening across sendfile:

  1. Validate self-transfer (outfd == infd / outfile == infile):

    • Linux sendfile(2) explicitly specifies: [EINVAL] out_fd and in_fd refer to the same file descriptor.
    • Previously, invoking sendfile(fd, fd, ...) caused copyfile() to read a chunk from fd into the temporary buffer (advancing offset) and write it right back to fd (advancing offset again), leading to file data corruption or infinite transfer loops.
    • Now validates outfd == infd in sendfile() and outfile == infile in file_sendfile(), returning -EINVAL.
  2. Validate negative offset (offset != NULL && *offset < 0):

    • Linux sendfile(2) specifies: [EINVAL] offset is not NULL but the value it points to is negative.
    • Rejects negative offsets upfront with -EINVAL.
  3. Validate file descriptor access modes:

    • Linux sendfile(2) specifies: [EBADF] The input file was not opened for reading or the output file was not opened for writing.
    • Validates that infile is opened for reading ((infile->f_oflags & O_ACCMODE) != O_WRONLY) and outfile is opened for writing ((outfile->f_oflags & O_ACCMODE) != O_RDONLY), returning -EBADF.
  4. Defensive NULL pointer checks:

    • Defensively validates outfile == NULL || infile == NULL returning -EBADF.
    • Verifies infile->f_inode != NULL before evaluating !INODE_IS_NOSEEK(infile->f_inode), eliminating CWE-476 NULL pointer dereference kernel panics on descriptors without inodes (such as sockets).
  5. Clamp count > SSIZE_MAX:

    • Clamps count to SSIZE_MAX to prevent signed overflow on the return value.
  6. Preserve 64-bit return width on LP64 architectures:

    • Widens result variable ret from int to ssize_t in sendfile() and file_sendfile() (psock_sendfile fast path), preventing 32-bit signed integer truncation and erroneous negative returns on large file transfers (> 2 GiB) on 64-bit architectures (ARM64, x86_64, RISC-V 64).

Behavior Comparison

Condition Before (master) After (this PR) Standard Conformance
outfd == infd Reads/writes same file in a loop (corrupts data) Returns -1, errno = EINVAL Linux sendfile(2) [EINVAL]
offset != NULL && *offset < 0 Silent seek or undefined behavior Returns -1, errno = EINVAL Linux sendfile(2) [EINVAL]
outfd opened O_RDONLY Fails inside write loop or corrupts Returns -1, errno = EBADF Linux sendfile(2) [EBADF]
infd opened O_WRONLY Fails inside read loop or corrupts Returns -1, errno = EBADF Linux sendfile(2) [EBADF]
infile->f_inode == NULL Kernel panic (CWE-476 NULL dereference) Safely falls back or returns error CWE-476 Mitigation
count > 2 GiB on LP64 Truncated to 32-bit int, returns -1 Preserved in 64-bit ssize_t LP64 correctness

Impact

  • Is new feature added?: No
  • Is existing feature changed?: Yes (Parameter validation & safety conformance in sendfile)
  • Impact on user: Applications receive POSIX/Linux standard error codes (EINVAL, EBADF) when passing invalid arguments instead of corrupted files or kernel panics.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: Updated function comments in include/sys/sendfile.h and include/nuttx/fs/fs.h.

Testing

Build Verification

  • Target: sim:nsh (x86_64)
  • Built with gcc (Ubuntu 11.4.0) inside Docker:
    LD: nuttx
    Pac SIM with dynamic libs..
    SIM elf with dynamic libs archive in nuttx.tgz
    

Runtime Test Execution Log

Executed on sim:nsh simulator:

nsh> hello
=== sendfile POSIX parameter validation test ===
[TEST 1] sendfile(same_fd, same_fd): ret=-1, errno=22 (Invalid argument) -> PASS
[TEST 2] sendfile(outfd, infd, offset=-5): ret=-1, errno=22 (Invalid argument) -> PASS
[TEST 3] sendfile(bad_outfd, infd): ret=-1, errno=9 (Bad file descriptor) -> PASS
[TEST 4] sendfile(outfd, bad_infd): ret=-1, errno=9 (Bad file descriptor) -> PASS
[TEST 5] sendfile(ro_outfd, infd): ret=-1, errno=9 (Bad file descriptor) -> PASS
[TEST 6] sendfile(outfd, wo_infd): ret=-1, errno=9 (Bad file descriptor) -> PASS
[TEST 7] sendfile(outfd, infd, count=0): ret=0, errno=0 -> PASS
[TEST 8] sendfile(outfd, infd, offset=4, count=6): ret=6, new_offset=10 -> PASS
[TEST 9] Transferred content verify: '456789' (expected '456789') -> PASS
=== sendfile tests complete ===
nsh> poweroff

Code Style Verification

Checked with tools/nxstyle:

tools/nxstyle fs/vfs/fs_sendfile.c
tools/nxstyle include/sys/sendfile.h
tools/nxstyle include/nuttx/fs/fs.h
# 0 errors, 0 warnings across all files

POSIX IEEE Std 1003.1 and Linux specifications (sendfile(2)) parameter
validation, bounds checking, and pointer safety hardening across sendfile:

1. Validate self-transfer (outfd == infd) in sendfile() and file_sendfile():
   Linux sendfile(2) explicitly specifies: [EINVAL] out_fd and in_fd
   refer to the same file descriptor. Previously, invoking sendfile with
   outfd == infd caused copyfile() to read from the descriptor into the
   I/O buffer (advancing offset) and write back to the same descriptor
   (advancing offset again), resulting in file data corruption or infinite
   loops. Returns -EINVAL upfront.

2. Validate negative offset (*offset < 0) in file_sendfile():
   Linux sendfile(2) specifies: [EINVAL] offset is not NULL but the
   value it points to is negative. Rejects negative offsets upfront with
   -EINVAL.

3. Validate file descriptor access modes:
   Linux sendfile(2) specifies: [EBADF] The input file was not opened
   for reading or the output file was not opened for writing. Validates
   that infile is readable ((infile->f_oflags & O_ACCMODE) != O_WRONLY)
   and outfile is writable ((outfile->f_oflags & O_ACCMODE) != O_RDONLY),
   returning -EBADF.

4. Defensive NULL checks in file_sendfile():
   Defensively validates outfile == NULL || infile == NULL returning
   -EBADF, and verifies infile->f_inode != NULL before evaluating
   !INODE_IS_NOSEEK(infile->f_inode). Eliminates CWE-476 NULL pointer
   dereference kernel panics when descriptors lack inodes (e.g. sockets).

5. Clamp count > SSIZE_MAX:
   Clamps count to SSIZE_MAX to prevent signed return value overflow.

6. Preserve 64-bit return width on LP64 architectures:
   Widens result variable ret from int to ssize_t in sendfile() and
   file_sendfile() (psock_sendfile fast path), preventing 32-bit signed
   integer truncation and erroneous negative returns on large file
   transfers (> 2 GiB) on 64-bit architectures (ARM64, x86_64, RISC-V 64).

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: File System File System issues Size: S The size of the change in this PR is small labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

mps2-an500-citest-protected

  • flash: .text +72 B (+0.1%, 116,264 B / 4,194,304 B, total: 3% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@xiaoxiang781216
xiaoxiang781216 merged commit cb626f7 into apache:master Nov 21, 2026
58 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: File System File System issues Size: S The size of the change in this PR is small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants