Repository navigation
fs/inode: validate parameters and avoid premature allocation in dup3 - #21884
Merged
xiaoxiang781216 merged 1 commit intoNov 17, 2026
Merged
Conversation
POSIX IEEE Std 1003.1 and Linux specifications (dup3(2)) specify parameter validation and descriptor handling for dup3: 1. Validate flags against O_CLOEXEC: Linux dup3(2) specifies that if flags contains any bit other than O_CLOEXEC, the call shall fail with -EINVAL. Previously, NuttX did not validate flags, silently accepting invalid bits. 2. Avoid premature heap expansion on invalid oldfd (fd1): Previously, fdlist_dup3() called fdlist_extend() to allocate heap memory and expand the file descriptor table before verifying fd1. When invoked with an invalid oldfd (e.g., fd1 = -1 or an unopened descriptor), the task's file table was permanently expanded on the heap before returning -EBADF. Moving fdlist_get2() before fdlist_extend() rejects invalid fd1 immediately without heap allocations or side-effects. If fdlist_extend() fails later, file_put(filep1) safely releases the acquired file reference. 3. Defensively validate list != NULL: Returns -EINVAL if the provided descriptor list is NULL, preventing CWE-476 NULL pointer dereferences in fdlist_limit(list). 4. Query descriptor from list in fdlist_dup2(): When fd1 == fd2, fdlist_dup2() previously called file_get(fd1, &filep) which checked this_task() rather than the provided list. Uses fdlist_get2(list, fd1, &filep, NULL) so the descriptor is verified in the intended file list (e.g., during task spawning). Assisted-by: Google DeepMind:antigravity Signed-off-by: Swatantra Yadav <[email protected]>
Swatantra-66
requested review from
Donny9,
pussuw and
xiaoxiang781216
as code owners
November 16, 2026 13:55
xiaoxiang781216
approved these changes
Nov 16, 2026
|
GUIDINGLI
approved these changes
Nov 17, 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 (
dup3(2)) specify parameter validation, flag constraints, and descriptor handling fordup3():POSIX / Linux Flags Validation:
Linux
dup3(2)explicitly mandates:Prevent Premature Heap Table Allocation on Invalid
oldfd:In
fdlist_dup3(),fdlist_extend()was invoked to expand the file descriptor table on the heap before validatingfd1. Iffd1was invalid (e.g.fd1 = -1or an unopened descriptor), the task's file descriptor table was permanently expanded on the heap beforefdlist_get2()returned-EBADF. Movingfdlist_get2()beforefdlist_extend()validatesfd1first, returning-EBADFwith zero heap allocations and zero side-effects. Iffdlist_extend()fails later,file_put(filep1)safely releases the acquired file reference.Defensive NULL List Validation:
Defensively validates
list != NULLreturning-EINVAL, eliminating CWE-476 NULL pointer dereferences infdlist_limit(list).Task-Specific Descriptor Resolution in
fdlist_dup2():When
fd1 == fd2,fdlist_dup2()previously calledfile_get(fd1, &filep)which queriedthis_task()rather than the providedlist. Usesfdlist_get2(list, fd1, &filep, NULL)so the descriptor is looked up in the intended file list (e.g., during spawned task file actions insched/task/task_spawnparms.c).Before vs. After Behavior
dup3(fd1, fd2, 0x1234)(invalid flags)ERROR,errno = EINVALdup3(-1, 50, 0)(invalid oldfd, large newfd)-EBADF-EBADFimmediately; zero heap memory allocatedfdlist_dup3(NULL, ...)NULL->fl_limit(Kernel Panic / Crash)-EINVALposix_spawnwithdup2(5, 5)action5in caller's task instead of spawned task5in spawned task's descriptor tableImpact
-EINVAL.dup3()invocations with invalidoldfdfrom permanently fragmenting and growing the process heap descriptor table.Testing
1. Build Verification
Cleanly built with
arm-none-eabi-gcc 10.3.1onstm32f103-minimum:nsh(tools/ci/testlist/arm-04.dat):2. Code Style Verification
Checked with
nxstyleinside Docker:3. What Was Not Tested