Swatantra-66 opened a new pull request, #21812:
URL: https://github.com/apache/nuttx/pull/21812
## 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)
```text
$ /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.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]