Skip to content

fs/vfs: validate nfds bounds and timeout in select and poll - #21812

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

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

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

POSIX IEEE Std 1003.1 explicitly specifies input parameter validation for both select() and poll():

For select():

  • [EINVAL] The nfds argument is less than 0 or greater than {FD_SETSIZE}.
  • [EINVAL] An invalid timeout was specified (negative tv_sec or invalid tv_usec).

Previously:

  1. If nfds > FD_SETSIZE, FD_ISSET(fd, set) would index past the caller's fd_set structure (CWE-125 out-of-bounds read).
  2. If timeout->tv_sec < 0, the negative value was converted into negative milliseconds, which poll() interprets as an infinite wait (timeout == -1), causing the calling thread to hang indefinitely.
  3. Large timeout->tv_sec overflowed signed 32-bit integer arithmetic during millisecond conversion.
  4. When CONFIG_FDCHECK is enabled, file descriptors are encoded with an 8-bit tag ((fd << 8) | tag), which can exceed FD_SETSIZE before normalization.

This patch:

  • Validates nfds < 0 upfront and rejects invalid timeout fields (tv_sec < 0 || tv_usec < 0 || tv_usec >= USEC_PER_SEC) with EINVAL.
  • Clamps millisecond conversion to INT_MAX when tv_sec > (INT_MAX - 1000) / 1000 to prevent 32-bit signed integer overflow.
  • Decodes descriptors via fdcheck_restore() before validating nfds > FD_SETSIZE, supporting protected descriptors while preventing out-of-bounds bit array reads.

For poll():

  • [EINVAL] The nfds argument is greater than {OPEN_MAX}.
  • [EFAULT] The fds argument points outside the process's allocated address space.

Previously:

  1. poll() used DEBUGASSERT(nfds == 0 || fds != NULL). In release builds (CONFIG_DEBUG_ASSERTIONS=n), this was compiled out, causing kernel NULL pointer dereference (CWE-476).
  2. An unbounded nfds argument could overflow 32-bit integer multiplication in fs_heap_malloc(nfds * sizeof(struct pollfd_s)), resulting in heap buffer overflow (CWE-190/CWE-122).

This patch:

  • Enforces runtime checks returning -EINVAL if nfds > OPEN_MAX and -EFAULT if nfds > 0 && fds == NULL.

Impact

  • Is new feature added? No.
  • Is existing feature changed? Yes, select() and poll() now validate parameter bounds per POSIX.
  • Impact on user: None for valid applications. Invalid calls safely return error codes (EINVAL or EFAULT) instead of crashing or hanging.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: None.
  • Impact on security: Hardens against CWE-125, CWE-476, CWE-190, and CWE-122.

Testing

Before vs After

Scenario Before After
select(FD_SETSIZE + 10, &rfds, ...) Out-of-bounds read in FD_ISSET (CWE-125) Returns ERROR, errno = EINVAL
select(1, &rfds, NULL, NULL, &{ -1, 0 }) Hangs indefinitely (negative msec treated as infinite wait) Returns ERROR, errno = EINVAL
select(1, &rfds, NULL, NULL, &{ 0, 2000000 }) Accepts invalid microseconds Returns ERROR, errno = EINVAL
select(maxfd + 1, ...) with CONFIG_FDCHECK=y N/A fdcheck_restore() decodes tag before bounds check
poll(NULL, 5, 0) with CONFIG_DEBUG_ASSERTIONS=n NULL pointer dereference crash (CWE-476) Returns -EFAULT
poll(fds, (nfds_t)-1, 0) Integer multiplication overflow & heap corruption (CWE-190/CWE-122) Returns -EINVAL

Build & Verification Log

  • Host: Linux 6.6.137+ / Ubuntu 24.04 (Docker builder nuttx-builder)
  • Target: sim:nsh (x86_64)
$ /repos/nuttx/tools/nxstyle /repos/nuttx/fs/vfs/fs_select.c
$ /repos/nuttx/tools/nxstyle /repos/nuttx/fs/vfs/fs_poll.c
(0 errors, 0 warnings)

$ make -j8
CC:  vfs/fs_select.c
CC:  vfs/fs_poll.c
IN:  fs/libfs.a -> staging/libfs.a
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz

$ echo -e 'help\nexit' | ./nuttx
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

Tested: sim:nsh (flat build, x86_64).
Untested: kernel and protected builds, real hardware target boards.

POSIX IEEE Std 1003.1 explicitly specifies parameter validation for
both select() and poll():

For select():
- [EINVAL] The nfds argument is less than 0 or greater than {FD_SETSIZE}.
- [EINVAL] An invalid timeout was specified (negative tv_sec or invalid
  tv_usec).
Previously, passing nfds > FD_SETSIZE caused FD_ISSET() to index past
caller fd_set boundaries (CWE-125), and negative timeouts were silently
converted into infinite blocking waits. This change validates nfds
bounds and timeout upfront, clamps large seconds conversion to INT_MAX
to prevent 32-bit signed integer overflow, and correctly applies the
FD_SETSIZE upper bound after fdcheck_restore() when CONFIG_FDCHECK
is enabled.

For poll():
- [EINVAL] The nfds argument is greater than {OPEN_MAX}.
- [EFAULT] The fds argument points outside the process's allocated
  address space.
Previously, poll() used DEBUGASSERT(nfds == 0 || fds != NULL), which is
compiled out in release builds, leading to NULL pointer dereference
(CWE-476). Large nfds could also cause integer multiplication wraparound
in fs_heap_malloc(nfds * sizeof(struct pollfd_s)), resulting in heap
buffer overflow (CWE-190/CWE-122). This change enforces runtime bounds
against OPEN_MAX and validates non-NULL fds when nfds > 0.

Assisted-by: gemini-3.8-flash
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 12, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

arduino-mega2560

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

qemu-armv8a

  • Code: .text.poll +4 B (+0.0%, 366,340 B)

qemu-intel64

  • Code: .text -3 B (-0.0%, 8,699,084 B)
  • Data: .rodata -32 B (-0.0%, 126,873 B)

rx65n-rsk2mb

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

No memory changes detected for:


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

@xiaoxiang781216
xiaoxiang781216 merged commit 03db496 into apache:master Nov 13, 2026
50 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