Skip to content

fs/inode: validate parameters and avoid premature allocation in dup3 - #21884

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

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

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 and Linux specifications (dup3(2)) specify parameter validation, flag constraints, and descriptor handling for dup3():

  1. POSIX / Linux Flags Validation:
    Linux dup3(2) explicitly mandates:

    "EINVAL (dup3()) flags contain an invalid value. Or, oldfd was equal to newfd."
    The only valid flag for dup3() is O_CLOEXEC (or 0). Previously, NuttX did not validate flags, silently accepting and ignoring invalid flag bits. Validates (flags & ~O_CLOEXEC) != 0 returning -EINVAL.

  2. 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 validating fd1. If fd1 was invalid (e.g. fd1 = -1 or an unopened descriptor), the task's file descriptor table was permanently expanded on the heap before fdlist_get2() returned -EBADF. Moving fdlist_get2() before fdlist_extend() validates fd1 first, returning -EBADF with zero heap allocations and zero side-effects. If fdlist_extend() fails later, file_put(filep1) safely releases the acquired file reference.

  3. Defensive NULL List Validation:
    Defensively validates list != NULL returning -EINVAL, eliminating CWE-476 NULL pointer dereferences in fdlist_limit(list).

  4. Task-Specific Descriptor Resolution in fdlist_dup2():
    When fd1 == fd2, fdlist_dup2() previously called file_get(fd1, &filep) which queried this_task() rather than the provided list. Uses fdlist_get2(list, fd1, &filep, NULL) so the descriptor is looked up in the intended file list (e.g., during spawned task file actions in sched/task/task_spawnparms.c).


Before vs. After Behavior

Scenario Before After
dup3(fd1, fd2, 0x1234) (invalid flags) Silently ignored invalid bits, succeeded Fails immediately with ERROR, errno = EINVAL
dup3(-1, 50, 0) (invalid oldfd, large newfd) Pre-allocated heap rows expanding task fd table before returning -EBADF Returns -EBADF immediately; zero heap memory allocated
fdlist_dup3(NULL, ...) Dereferenced NULL->fl_limit (Kernel Panic / Crash) Safely returns -EINVAL
posix_spawn with dup2(5, 5) action Checked descriptor 5 in caller's task instead of spawned task Checks descriptor 5 in spawned task's descriptor table

Impact

  • POSIX / Linux Conformance: Rejects invalid flags with -EINVAL.
  • Memory & Resource Efficiency: Prevents failing dup3() invocations with invalid oldfd from permanently fragmenting and growing the process heap descriptor table.
  • Security & Robustness: Prevents NULL pointer dereferences and incorrect task descriptor queries during process spawning.

Testing

1. Build Verification

Cleanly built with arm-none-eabi-gcc 10.3.1 on stm32f103-minimum:nsh (tools/ci/testlist/arm-04.dat):

$ ./tools/configure.sh stm32f103-minimum:nsh
$ make -j8
...
CC:  inode/fs_files.c
...
LD: nuttx
CP: nuttx.bin

$ arm-none-eabi-size nuttx
   text	   data	    bss	    dec	    hex	filename
  53723	    468	   3056	  57247	   df9f	nuttx

2. Code Style Verification

Checked with nxstyle inside Docker:

$ ./tools/nxstyle fs/inode/fs_files.c
No errors, no warnings.

3. What Was Not Tested

  • Real hardware target (tested using cross-compilation for ARM Cortex-M3).
  • Protected and Kernel build modes.

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]>
@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 16, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text +10 B (+0.0%, 68,222 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +32 B (+0.0%, 131,376 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +32 B (+0.0%, 97,240 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +20 B (+0.0%, 87,436 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +12 B (+0.0%, 69,724 B / 131,072 B, total: 53% used)

qemu-armv8a

  • Code: .text.fdlist_dup2 +4 B, .text.fdlist_dup3 +36 B (+0.0%, 368,076 B)

qemu-intel64

  • Code: .text +35 B (+0.0%, 8,698,356 B)

rx65n-rsk2mb

  • ROM: .text +16 B (+0.0%, 93,020 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +96 B (+0.0%, 397,264 B)

stm32-nucleo-f103rb

  • flash: .text +12 B (+0.0%, 35,864 B / 131,072 B, total: 27% used)

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

@xiaoxiang781216
xiaoxiang781216 merged commit 889983a into apache:master Nov 17, 2026
52 of 53 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