Swatantra-66 opened a new pull request, #21815:
URL: https://github.com/apache/nuttx/pull/21815
## Summary
POSIX and Linux specifications (`epoll(7)`) explicitly define parameter
validation for the `epoll` family of system calls:
### 1. `epoll_wait()` and `epoll_pwait()`
- `[EINVAL]` `maxevents` is less than or equal to zero.
- `[EFAULT]` The memory area pointed to by `events` is not accessible.
**Previously:**
- If `maxevents <= 0`, `epoll_teardown()` returned 0, causing `epoll_wait()`
and `epoll_pwait()` to enter an infinite busy-wait loop (`goto retry;`),
pegging the CPU at 100% and starving other tasks.
- If `maxevents > 0` but `evs == NULL`, `epoll_teardown()` dereferenced NULL
when storing results (CWE-476).
**Fix:**
- Validates `maxevents > 0` upfront returning `ERROR` with `errno = EINVAL`.
- Validates `evs != NULL` upfront returning `ERROR` with `errno = EFAULT`.
### 2. `epoll_ctl()`
- `[EINVAL]` `fd` is the same as `epfd`.
- `[EFAULT]` The memory area pointed to by `event` is not accessible.
**Previously:**
- `epoll_ctl()` did not check for `epfd == fd`, allowing circular epoll
self-monitoring dependencies.
- For `EPOLL_CTL_ADD` and `EPOLL_CTL_MOD`, `ev->events` and `ev->data` were
dereferenced without checking `ev != NULL`, causing kernel panics on NULL
pointer dereference (CWE-476).
**Fix:**
- Rejects `epfd == fd` with `EINVAL`.
- Validates `ev != NULL` for `EPOLL_CTL_ADD` and `EPOLL_CTL_MOD` returning
`EFAULT`.
### 3. `epoll_create()` and `epoll_create1()`
- `[EINVAL]` `size` is not positive.
- `[EINVAL]` Invalid value specified in `flags`.
**Previously:**
- `epoll_create()` silently accepted non-positive sizes (`size <= 0 ? 1 :
size`).
- Unbounded `size` could overflow 32-bit integer multiplication in
`sizeof(epoll_head_t) + sizeof(epoll_node_t) * size`, causing a small heap
allocation followed by an out-of-bounds heap initialization loop
(CWE-190/CWE-122).
- `epoll_create1()` accepted arbitrary invalid flags without checking
against `EPOLL_CLOEXEC`.
**Fix:**
- In public `epoll_create()`, rejects `size <= 0` or oversized values
exceeding `(SIZE_MAX - sizeof(epoll_head_t)) / sizeof(epoll_node_t)` with
`EINVAL`.
- In `epoll_do_create()`, preserves normalization for configurations where
`CONFIG_FS_NEPOLL_DESCRIPTORS=0` is used by `epoll_create1()`.
- In `epoll_create1()`, validates `(flags & ~EPOLL_CLOEXEC) == 0`.
- In `epoll_head_from_fd()`, defensively guards against `(*filep)->f_inode
== NULL`.
---
## Impact
- **Is new feature added?** No.
- **Is existing feature changed?** Yes, `epoll` system calls now validate
parameter bounds per POSIX/Linux.
- **Impact on user:** None for valid applications. Invalid calls safely
return standard error codes (`EINVAL` or `EFAULT`) instead of hanging or
crashing.
- **Impact on build:** None.
- **Impact on hardware:** None.
- **Impact on documentation:** None.
- **Impact on security:** Hardens against CWE-476, CWE-190, CWE-122, and
infinite loop denial-of-service.
---
## Testing
### Before vs After
| Scenario | Before | After |
| :--- | :--- | :--- |
| `epoll_wait(epfd, evs, 0, 100)` | Infinite busy-loop (`goto retry;`), 100%
CPU hang | Returns `ERROR`, `errno = EINVAL` |
| `epoll_wait(epfd, NULL, 5, 0)` | Kernel NULL pointer dereference (CWE-476)
| Returns `ERROR`, `errno = EFAULT` |
| `epoll_ctl(epfd, EPOLL_CTL_ADD, epfd, &ev)` | Allows circular
self-monitoring | Returns `ERROR`, `errno = EINVAL` |
| `epoll_ctl(epfd, EPOLL_CTL_ADD, fd, NULL)` | Kernel NULL pointer
dereference (CWE-476) | Returns `ERROR`, `errno = EFAULT` |
| `epoll_create(0)` / `epoll_create(-1)` | Silently accepted, normalized to
1 | Returns `ERROR`, `errno = EINVAL` |
| `epoll_create(SIZE_MAX / 2)` | Integer overflow & heap memory corruption
(CWE-190/CWE-122) | Returns `ERROR`, `errno = EINVAL` |
| `epoll_create1(0xdeadbeef)` | Silently accepted | Returns `ERROR`, `errno
= EINVAL` |
| `epoll_create1(0)` with `CONFIG_FS_NEPOLL_DESCRIPTORS=0` | Normalizes to 1
| Preserved, normalizes to 1 |
### 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_epoll.c
(0 errors, 0 warnings)
$ cd /repos/nuttx/fs && make TOPDIR=/repos/nuttx fs_epoll.o
CC: vfs/fs_epoll.c
$ make -j8
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]