Skip to content

fs/vfs: validate parameters and fix error codes in fcntl - #21856

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-fcntl-posix-validation
Nov 14, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-fcntl-posix-validation

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

This PR validates parameters, prevents out-of-bounds memory accesses, and corrects error codes in fs/vfs/fs_fcntl.c:

  1. F_DUPFD and F_DUPFD_CLOEXEC:

    • Negative minfd Validation (CWE-125 Out-of-Bounds Access): Validates that minfd >= 0, returning -EINVAL per POSIX IEEE Std 1003.1. Previously, passing a negative minfd (e.g. -1) resulted in negative row/column indexing (minfd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK, minfd % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) in fdlist_dupfile(), causing out-of-bounds array reads and kernel panics.
  2. F_SETFD:

    • POSIX Error Code Conformance: When invalid flags other than FD_CLOEXEC are passed, fcntl() now sets errno = EINVAL instead of ENOSYS. Under POSIX.1-2024 / IEEE Std 1003.1 fcntl(2), passing invalid flag bits to a command requires [EINVAL]. Returning ENOSYS ("Function not implemented") broke POSIX compliance test suites.
  3. file_vfcntl():

    • Defensive Pointer Null-Check (CWE-476): Validates filep != NULL && filep->f_inode != NULL before accessing filep->f_inode, returning -EBADF and preventing NULL pointer dereference panics when invoked on invalid or unallocated file structures.
  4. F_GETLK, F_SETLK, and F_SETLKW:

    • flock Pointer Validation: Validates flock != NULL, returning -EFAULT per POSIX / Linux fcntl(2). Previously, passing NULL was passed down to file_lock_normalize(), which unconditionally dereferenced flock->l_type, causing a kernel crash.
  5. F_GETPATH:

    • Buffer Validation: Validates that path != NULL, returning -EFAULT to prevent NULL pointer dereferences in underlying filesystem drivers.
  6. F_SETPIPE_SZ:

    • Capacity Bounds: Validates that size > 0, returning -EINVAL per Linux fcntl(2) specification. Previously, passing a negative value converted to an unsigned integer and clamped to CONFIG_DEV_PIPE_MAXSIZE, silently succeeding instead of reporting an error.

Comparison Table

Issue / Command Prior Behavior Hardened POSIX Behavior Specification
fcntl(fd, F_DUPFD, -1) Out-of-bounds array access (CWE-125) / crash Returns -1 with errno = EINVAL POSIX IEEE Std 1003.1
fcntl(fd, F_SETFD, ~FD_CLOEXEC) Returns -1 with errno = ENOSYS ❌ Returns -1 with errno = EINVAL ✅ POSIX IEEE Std 1003.1
file_fcntl(NULL, cmd) Kernel Panic (CWE-476 NULL dereference) Returns -EBADF POSIX IEEE Std 1003.1
fcntl(fd, F_SETLK, NULL) Kernel Panic (flock->l_type NULL dereference) Returns -1 with errno = EFAULT Linux / POSIX fcntl(2)
fcntl(fd, F_SETPIPE_SZ, -1) Silently resizes pipe to MAXSIZE Returns -1 with errno = EINVAL Linux fcntl(2)
fcntl(fd, F_GETPATH, NULL) Kernel Panic (strcpy(NULL, ...) in drivers) Returns -1 with errno = EFAULT POSIX / Linux

Impact

  • Is new feature added? No.
  • Impact on user: None. Corrects POSIX error codes and prevents out-of-bounds and NULL dereference panics.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: None.
  • Impact on security: Hardens against CWE-125 (out-of-bounds access) and CWE-476 (NULL pointer dereference).

Testing

  • Host OS: Windows 11 (Docker container nuttx-builder Linux x86_64)
  • Target: sim:nsh
  • Compiler: GCC / Ubuntu clang

1. Code Style Check (nxstyle)

$ ./tools/nxstyle fs/vfs/fs_fcntl.c
# Exit code: 0 (0 errors, 0 warnings)

2. Build Verification

$ make -j$(nproc)
...
CC:  vfs/fs_fcntl.c
IN: fs/libfs.a -> staging/libfs.a
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz
# Exit code: 0

3. Binary Size

   text    data     bss     dec     hex filename
   1757       0       0    1757     6dd /repos/nuttx/fs/fs_fcntl.o
 603833    5340   46752  655925   a0235 /repos/nuttx/nuttx

4. Simulator Boot and Runtime Test

nsh> help
help usage:  help [-v] [<cmd>]

    .            break        exec         mkdir        rm           true         
    [            cat          exit         mkfifo       rmdir        truncate     
    ?            cd           expr         mkrd         rmmod        uname        
    addroute     cp           false        mount        set          umount       
    alias        cmp          help         mv           kill         unset        
    unalias      dirname      hexdump      nslookup     sleep        uptime       
    arp          date         insmod       poweroff     usleep       watch        
    base64dec    delroute     losetup      quit         source       xd           
    base64enc    dmesg        ls           printf       test         
    basename     echo         du           pwd          time         

Builtin Apps:
    dd      init    nsh     sh      
nsh> exit

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

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


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

@xiaoxiang781216

Copy link
Copy Markdown
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
Swatantra-66 force-pushed the fix/vfs-fcntl-posix-validation branch from e1abe81 to d336e11 Compare November 14, 2026 07:26
@acassis
acassis merged commit 0ead233 into apache:master Nov 14, 2026
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