Skip to content

fs/vfs: validate parameters in epoll_create, epoll_ctl, and epoll_wait - #21815

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

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

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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)
$ /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.

POSIX and Linux man-pages explicitly specify parameter validation for
the epoll family of system calls:

For 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, which caused
epoll_wait() / epoll_pwait() to loop indefinitely on 'goto retry;',
pegging the CPU at 100% and starving other tasks. Furthermore, if
evs == NULL, epoll_teardown() dereferenced NULL (CWE-476). This change
validates maxevents > 0 and evs != NULL upfront.

For epoll_ctl():
- [EINVAL] epfd is the same as fd (circular dependency).
- [EFAULT] The memory area pointed to by event is not accessible.
Previously, epoll_ctl() did not reject epfd == fd and dereferenced
ev->events without checking ev != NULL for EPOLL_CTL_ADD and
EPOLL_CTL_MOD (CWE-476). This change enforces epfd != fd and validates
ev != NULL for ADD/MOD operations.

For epoll_create() and epoll_create1():
- [EINVAL] size is not positive.
- [EINVAL] Invalid value specified in flags.
Previously, epoll_create() silently accepted non-positive sizes, and
large sizes could cause integer multiplication wraparound in
sizeof(epoll_head_t) + sizeof(epoll_node_t) * size, resulting in a heap
buffer overflow (CWE-190/CWE-122). epoll_create1() also accepted
arbitrary invalid flags. This change validates size bounds and flags,
while preserving normalization in epoll_do_create() for configurations
where CONFIG_FS_NEPOLL_DESCRIPTORS is zero.

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 →

No memory changes detected for:


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

@xiaoxiang781216
xiaoxiang781216 merged commit 9916800 into apache:master Nov 13, 2026
49 of 52 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