Repository navigation
fs/vfs: validate parameters and preserve 64-bit counts in sendfile - #22128
Merged
xiaoxiang781216 merged 1 commit intoNov 21, 2026
Merged
xiaoxiang781216 merged 1 commit into
xiaoxiang781216 merged 1 commit into
Conversation
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]>
Swatantra-66
requested review from
Donny9,
jerpelea,
pkarashchenko,
pussuw and
xiaoxiang781216
as code owners
November 20, 2026 13:00
xiaoxiang781216
approved these changes
Nov 20, 2026
GUIDINGLI
approved these changes
Nov 20, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
POSIX IEEE Std 1003.1 and Linux specifications (
sendfile(2)) parameter validation, bounds checking, and pointer safety hardening acrosssendfile:Validate self-transfer (
outfd == infd/outfile == infile):sendfile(2)explicitly specifies:[EINVAL] out_fd and in_fd refer to the same file descriptor.sendfile(fd, fd, ...)causedcopyfile()to read a chunk fromfdinto the temporary buffer (advancing offset) and write it right back tofd(advancing offset again), leading to file data corruption or infinite transfer loops.outfd == infdinsendfile()andoutfile == infileinfile_sendfile(), returning-EINVAL.Validate negative offset (
offset != NULL && *offset < 0):sendfile(2)specifies:[EINVAL] offset is not NULL but the value it points to is negative.-EINVAL.Validate file descriptor access modes:
sendfile(2)specifies:[EBADF] The input file was not opened for reading or the output file was not opened for writing.infileis opened for reading ((infile->f_oflags & O_ACCMODE) != O_WRONLY) andoutfileis opened for writing ((outfile->f_oflags & O_ACCMODE) != O_RDONLY), returning-EBADF.Defensive NULL pointer checks:
outfile == NULL || infile == NULLreturning-EBADF.infile->f_inode != NULLbefore evaluating!INODE_IS_NOSEEK(infile->f_inode), eliminating CWE-476 NULL pointer dereference kernel panics on descriptors without inodes (such as sockets).Clamp
count > SSIZE_MAX:counttoSSIZE_MAXto prevent signed overflow on the return value.Preserve 64-bit return width on LP64 architectures:
retfrominttossize_tinsendfile()andfile_sendfile()(psock_sendfilefast 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
outfd == infd-1,errno = EINVALsendfile(2)[EINVAL]offset != NULL && *offset < 0-1,errno = EINVALsendfile(2)[EINVAL]outfdopenedO_RDONLY-1,errno = EBADFsendfile(2)[EBADF]infdopenedO_WRONLY-1,errno = EBADFsendfile(2)[EBADF]infile->f_inode == NULLcount > 2 GiBon LP64int, returns-1ssize_tImpact
sendfile)EINVAL,EBADF) when passing invalid arguments instead of corrupted files or kernel panics.include/sys/sendfile.handinclude/nuttx/fs/fs.h.Testing
Build Verification
sim:nsh(x86_64)gcc (Ubuntu 11.4.0)inside Docker:Runtime Test Execution Log
Executed on
sim:nshsimulator:Code Style Verification
Checked with
tools/nxstyle: