Repository navigation
fs/vfs: validate parameters and fix error codes in fcntl - #21856
Merged
acassis merged 1 commit intoNov 14, 2026
Merged
Conversation
Swatantra-66
requested review from
Donny9,
pussuw and
xiaoxiang781216
as code owners
November 13, 2026 20:17
|
xiaoxiang781216
approved these changes
Nov 13, 2026
xiaoxiang781216
approved these changes
Nov 14, 2026
Contributor
|
fixed by #21771. |
1. F_DUPFD and F_DUPFD_CLOEXEC:
- Validate that minfd >= 0, returning -EINVAL per POSIX
IEEE Std 1003.1. Prevents negative index calculation in
fdlist_dupfile() and resulting out-of-bounds array access (CWE-125).
2. F_SETFD:
- Replace set_errno(ENOSYS) with set_errno(EINVAL) when invalid
flags other than FD_CLOEXEC are passed, adhering strictly to
POSIX fcntl(2) specification.
3. file_vfcntl():
- Defensively guard against filep == NULL and filep->f_inode == NULL,
returning -EBADF to prevent CWE-476 NULL pointer dereferences.
4. F_GETLK, F_SETLK, and F_SETLKW:
- Validate that flock != NULL, returning -EFAULT to prevent
CWE-476 NULL pointer dereference in file_lock_normalize().
5. F_GETPATH:
- Validate that path != NULL, returning -EFAULT to prevent
driver-level null pointer dereference.
6. F_SETPIPE_SZ:
- Validate that size > 0, returning -EINVAL per Linux fcntl(2).
Prevents negative values from silently clamping to MAXSIZE.
Assisted-by: Google Gemini <gemini-3.8-flash>
Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
force-pushed
the
fix/vfs-fcntl-posix-validation
branch
from
November 14, 2026 07:26
e1abe81 to
d336e11
Compare
acassis
approved these changes
Nov 14, 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
This PR validates parameters, prevents out-of-bounds memory accesses, and corrects error codes in
fs/vfs/fs_fcntl.c:F_DUPFDandF_DUPFD_CLOEXEC:minfdValidation (CWE-125 Out-of-Bounds Access): Validates thatminfd >= 0, returning-EINVALper POSIX IEEE Std 1003.1. Previously, passing a negativeminfd(e.g.-1) resulted in negative row/column indexing (minfd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK,minfd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) infdlist_dupfile(), causing out-of-bounds array reads and kernel panics.F_SETFD:FD_CLOEXECare passed,fcntl()now setserrno = EINVALinstead ofENOSYS. Under POSIX.1-2024 / IEEE Std 1003.1fcntl(2), passing invalid flag bits to a command requires[EINVAL]. ReturningENOSYS("Function not implemented") broke POSIX compliance test suites.file_vfcntl():filep != NULL && filep->f_inode != NULLbefore accessingfilep->f_inode, returning-EBADFand preventing NULL pointer dereference panics when invoked on invalid or unallocated file structures.F_GETLK,F_SETLK, andF_SETLKW:flockPointer Validation: Validatesflock != NULL, returning-EFAULTper POSIX / Linuxfcntl(2). Previously, passingNULLwas passed down tofile_lock_normalize(), which unconditionally dereferencedflock->l_type, causing a kernel crash.F_GETPATH:path != NULL, returning-EFAULTto prevent NULL pointer dereferences in underlying filesystem drivers.F_SETPIPE_SZ:size > 0, returning-EINVALper Linuxfcntl(2)specification. Previously, passing a negative value converted to an unsigned integer and clamped toCONFIG_DEV_PIPE_MAXSIZE, silently succeeding instead of reporting an error.Comparison Table
fcntl(fd, F_DUPFD, -1)-1witherrno = EINVALfcntl(fd, F_SETFD, ~FD_CLOEXEC)-1witherrno = ENOSYS❌-1witherrno = EINVAL✅file_fcntl(NULL, cmd)-EBADFfcntl(fd, F_SETLK, NULL)flock->l_typeNULL dereference)-1witherrno = EFAULTfcntl(2)fcntl(fd, F_SETPIPE_SZ, -1)MAXSIZE-1witherrno = EINVALfcntl(2)fcntl(fd, F_GETPATH, NULL)strcpy(NULL, ...)in drivers)-1witherrno = EFAULTImpact
Testing
nuttx-builderLinux x86_64)sim:nsh1. Code Style Check (
nxstyle)$ ./tools/nxstyle fs/vfs/fs_fcntl.c # Exit code: 0 (0 errors, 0 warnings)2. Build Verification
3. Binary Size
4. Simulator Boot and Runtime Test