Repository navigation
fs/vfs: validate nfds bounds and timeout in select and poll - #21812
Merged
xiaoxiang781216 merged 1 commit intoNov 13, 2026
Merged
xiaoxiang781216 merged 1 commit into
xiaoxiang781216 merged 1 commit into
Conversation
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]>
Swatantra-66
requested review from
Donny9,
jerpelea and
xiaoxiang781216
as code owners
November 12, 2026 17:18
|
xiaoxiang781216
approved these changes
Nov 13, 2026
GUIDINGLI
approved these changes
Nov 13, 2026
4 of 5 tasks
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 explicitly specifies input parameter validation for both
select()andpoll():For
select():[EINVAL]Thenfdsargument is less than 0 or greater than{FD_SETSIZE}.[EINVAL]An invalid timeout was specified (negativetv_secor invalidtv_usec).Previously:
nfds > FD_SETSIZE,FD_ISSET(fd, set)would index past the caller'sfd_setstructure (CWE-125 out-of-bounds read).timeout->tv_sec < 0, the negative value was converted into negative milliseconds, whichpoll()interprets as an infinite wait (timeout == -1), causing the calling thread to hang indefinitely.timeout->tv_secoverflowed signed 32-bit integer arithmetic during millisecond conversion.CONFIG_FDCHECKis enabled, file descriptors are encoded with an 8-bit tag ((fd << 8) | tag), which can exceedFD_SETSIZEbefore normalization.This patch:
nfds < 0upfront and rejects invalidtimeoutfields (tv_sec < 0 || tv_usec < 0 || tv_usec >= USEC_PER_SEC) withEINVAL.INT_MAXwhentv_sec > (INT_MAX - 1000) / 1000to prevent 32-bit signed integer overflow.fdcheck_restore()before validatingnfds > FD_SETSIZE, supporting protected descriptors while preventing out-of-bounds bit array reads.For
poll():[EINVAL]Thenfdsargument is greater than{OPEN_MAX}.[EFAULT]Thefdsargument points outside the process's allocated address space.Previously:
poll()usedDEBUGASSERT(nfds == 0 || fds != NULL). In release builds (CONFIG_DEBUG_ASSERTIONS=n), this was compiled out, causing kernel NULL pointer dereference (CWE-476).nfdsargument could overflow 32-bit integer multiplication infs_heap_malloc(nfds * sizeof(struct pollfd_s)), resulting in heap buffer overflow (CWE-190/CWE-122).This patch:
-EINVALifnfds > OPEN_MAXand-EFAULTifnfds > 0 && fds == NULL.Impact
select()andpoll()now validate parameter bounds per POSIX.EINVALorEFAULT) instead of crashing or hanging.Testing
Before vs After
select(FD_SETSIZE + 10, &rfds, ...)FD_ISSET(CWE-125)ERROR,errno = EINVALselect(1, &rfds, NULL, NULL, &{ -1, 0 })ERROR,errno = EINVALselect(1, &rfds, NULL, NULL, &{ 0, 2000000 })ERROR,errno = EINVALselect(maxfd + 1, ...)withCONFIG_FDCHECK=yfdcheck_restore()decodes tag before bounds checkpoll(NULL, 5, 0)withCONFIG_DEBUG_ASSERTIONS=n-EFAULTpoll(fds, (nfds_t)-1, 0)-EINVALBuild & Verification Log
nuttx-builder)sim:nsh(x86_64)Tested:
sim:nsh(flat build, x86_64).Untested: kernel and protected builds, real hardware target boards.