Skip to content

fs/inode: validate parameters and minfd bounds in fdlist_dupfile - #21979

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-dupfile-bounds-validation
Nov 19, 2026
Merged

xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-dupfile-bounds-validation

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 (fcntl(2) F_DUPFD), Linux specifications, and file descriptor allocation rules specify parameter validation, bounds constraints, and descriptor decoding for fdlist_dupfile() and fdcheck_restore():

  1. Validate minfd >= 0 and minfd < limit in fdlist_dupfile():
    Previously, fdlist_dupfile() only checked minfd >= limit without verifying minfd >= 0. Passing a negative minfd (e.g., -1 or -9) bypassed the limit check because negative values are strictly < limit. This caused:

    • Negative array indexing: j = minfd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK = -1.
    • Out-of-bounds heap memory access: accessing &list->fl_fds[0][-1] or &list->fl_fds[-1][-1] (CWE-125 / CWE-129).
    • Heap corruption: if fdp->f_file == NULL, it wrote into out-of-bounds heap memory before the table (atomic_add(&filep->f_refs, 1) and fdp->f_file = filep) (CWE-787).
      Checking minfd < 0 upfront before and after restoration safely returns -EINVAL.
  2. Defensive NULL List and File Pointer Validation:
    Defensively validates list == NULL || filep == NULL returning -EINVAL. This eliminates:

    • CWE-476 NULL pointer dereference in fdlist_limit(list) when list == NULL.
    • CWE-476 NULL pointer dereference in atomic_add(&filep->f_refs, 1) when filep == NULL (previously only guarded by release-stripped DEBUGASSERT(filep)).
  3. Avoid False Descriptor Decoding in fdcheck_restore():
    Under CONFIG_FDCHECK, fdcheck_protect() never protects standard descriptors (fd <= 2), and assigned tags are never 0 (g_fdcheck_tag skips 0). Previously, fdcheck_restore() only checked (fd == 0 || val < 0). Consequently:

    • Bare integers $\ge 256$ (such as minfd = 256, 0x0100, where $\text{fd} = 1$ and $\text{tag} = 0$) were mistakenly interpreted as tagged descriptors for descriptor 1 with tag 0.
    • If descriptor 1 (stdout) was open, ioctl(1, FIOC_GETTAG_FDCHECK, &tag_store) succeeded with tag_store = 0, matching tag_expect = 0 and returning 1. This silently bypassed minfd >= limit checks on tasks with limit = 16.
    • If descriptor 1 was closed, ioctl(1) failed with -EBADF and invoked PANIC().
      Updating fdcheck_restore() to check (fd <= 2 || tag_expect == 0 || val < 0) ensures bare integers and untagged descriptors are returned directly without false mutation or panics.

Before vs. After Behavior

Scenario Before After
fdlist_dupfile(list, flags, -1, filep) Negative index calculation (j = -1); out-of-bounds heap read/write at fl_fds[0][-1] Fails immediately, returns -EINVAL
fdlist_dupfile(NULL, flags, 0, filep) Dereferenced NULL->fl_limit (Kernel Panic / Crash) Safely returns -EINVAL
fdlist_dupfile(list, flags, 0, NULL) In release builds, dereferenced NULL->f_refs (Crash) Safely returns -EINVAL
CONFIG_FDCHECK with minfd = 256, limit = 16, fd 1 open Decoded 256 as descriptor 1; bypassed limit check and allocated fd $\ge 1$ Returns bare 256; correctly rejected with -EINVAL ($256 \ge 16$)
CONFIG_FDCHECK with minfd = 256, fd 1 closed ioctl(1) failed with -EBADF and triggered kernel PANIC() Returns bare 256; correctly rejected with -EINVAL

Impact

  • Memory Safety & Robustness: Eliminates negative heap array subscripts (CWE-125 / CWE-129 / CWE-787) and NULL pointer dereferences (CWE-476).
  • POSIX / System Conformance: Correctly rejects negative or out-of-limit minfd values with -EINVAL.
  • Subsystem Reliability: Prevents false tagged descriptor decoding and spurious PANIC() calls under CONFIG_FDCHECK.

Testing

1. Build Verification

Cleanly built with arm-none-eabi-gcc 10.3.1 on stm32f103-minimum:nsh:

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

$ arm-none-eabi-size nuttx
   text    data     bss     dec     hex filename
  53427     344    3144   56915    de53 nuttx

2. Code Style & Commit Verification

Verified with nxstyle inside Docker:

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

$ ./tools/nxstyle libs/libc/misc/lib_fdcheck.c
No errors, no warnings.

Verified with checkpatch.sh:

$ ./tools/checkpatch.sh -r -m -g HEAD~1..HEAD
✔️ All checks pass.

3. What Was Not Tested

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

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

github-actions Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

  • flash: .text -20 B (-0.0%, 68,649 B / 262,144 B, total: 26% used)

esp32-devkitc

  • ROM: .flash.text +4 B (+0.0%, 132,936 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +4 B (+0.0%, 98,616 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text +16 B (+0.0%, 86,956 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +32 B (+0.0%, 68,464 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.fdlist_dupfile -24 B (-0.0%, 372,076 B)

qemu-intel64

  • Code: .text -18 B (-0.0%, 8,701,146 B)

s698pm-dkit

  • Code: .text +16 B (+0.0%, 406,528 B)

stm32-nucleo-f103rb

  • flash: .text -8 B (-0.0%, 35,480 B / 131,072 B, total: 27% used)

No memory changes detected for:


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

POSIX IEEE Std 1003.1 (fcntl(2) F_DUPFD) and file descriptor allocation
rules specify parameter validation and bounds handling for fdlist_dupfile:

1. Validate minfd >= 0 and minfd < limit:
   Previously, fdlist_dupfile() only checked minfd >= limit without
   verifying minfd >= 0. Passing a negative minfd (e.g., -1) bypassed
   the limit check, resulting in negative array indexing (minfd % 8 = -1)
   and out-of-bounds heap access (&list->fl_fds[0][-1]) or heap corruption
   (CWE-125/CWE-129/CWE-787). Checking minfd < 0 upfront safely returns
   -EINVAL.

2. Defensively validate list != NULL and filep != NULL:
   Returns -EINVAL if list or filep is NULL, eliminating potential
   CWE-476 NULL pointer dereferences in fdlist_limit(list) and
   atomic_add(&filep->f_refs, 1).

3. Avoid false descriptor decoding in fdcheck_restore():
   Standard streams (<= 2) are never protected by fdcheck_protect(),
   and assigned tags are never 0. In lib_fdcheck.c, fdcheck_restore()
   previously checked only (fd == 0 || val < 0), causing bare values
   >= 256 (such as minfd = 256 with tag 0) to be decoded as descriptor 1
   (matching open stdout with tag 0) or panicking if descriptor 1 was
   closed. Checking (fd <= 2 || tag_expect == 0 || val < 0) ensures bare
   descriptors are returned directly.

Assisted-by: Claude:claude-opus-4.6
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/vfs-dupfile-bounds-validation branch from 099273b to df9a8fa Compare November 19, 2026 07:31
@xiaoxiang781216
xiaoxiang781216 merged commit 629c4e5 into apache:master Nov 19, 2026
54 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