Skip to content

fs/vfs: validate parameters and buffer alignment in eventfd - #21855

Merged
acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-eventfd-posix-validation
Nov 14, 2026
Merged

acassis merged 1 commit into
apache:masterfrom
Swatantra-66:fix/vfs-eventfd-posix-validation

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

Summary

This PR validates parameters and fixes error handling, buffer alignment, and counter overflow bounds in fs/vfs/fs_eventfd.c:

  1. eventfd_do_read():

    • POSIX Error Code Separation: Separates buffer nullity and buffer size checks. buffer == NULL returns -EFAULT per POSIX read(2) ([EFAULT] buf is outside your accessible address space). A buffer size less than sizeof(eventfd_t) returns -EINVAL per Linux eventfd(2).
    • Buffer Alignment Hardening: Replaces direct pointer cast *(FAR eventfd_t *)buffer and direct 64-bit store with a local eventfd_t value; written via memcpy(buffer, &value, sizeof(eventfd_t));. Because eventfd_t is a 64-bit unsigned integer, writing directly to an unaligned user pointer (char *buffer) causes unaligned memory access hardware traps on strict architectures (e.g. ARM Cortex-M0/M4) and violates C strict aliasing.
  2. eventfd_do_write():

    • POSIX Error Code Separation: Separates buffer nullity and size checks. Returns -EFAULT when buffer == NULL per POSIX write(2), and -EINVAL when len < sizeof(eventfd_t) per Linux eventfd(2).
    • Single-Load Alignment Hardening: Replaces raw pointer dereferences *(FAR eventfd_t *)buffer with a single local load via memcpy(&value, buffer, sizeof(eventfd_t));, eliminating unaligned memory access traps and eliminating repeated buffer dereferencing throughout the function and its wait loop.
    • Maximum Counter Ceiling Enforcement: Enforces the Linux eventfd(2) specification ceiling where the maximum counter value is 0xfffffffffffffffe ((eventfd_t)-2). Previously, NuttX only checked new_counter < dev->counter, which allowed the counter to reach 0xffffffffffffffff ((eventfd_t)-1). Because (eventfd_t)-1 is reserved and stops triggering POLLOUT in eventfd_do_poll(), checking new_counter < dev->counter || new_counter == (eventfd_t)-1 ensures the counter is bounded correctly.

Comparison Table

Issue / System Call Prior Behavior Hardened POSIX Behavior Specification
read(efd, NULL, len) Returns -1 with errno = EINVAL Returns -1 with errno = EFAULT POSIX read(2)
read(efd, buf, < 8) Returns -1 with errno = EINVAL Returns -1 with errno = EINVAL Linux eventfd(2)
read(efd, unaligned_buf, 8) Potential unaligned access trap on 64-bit store Safe copy via memcpy C standard & Architecture ABI
write(efd, NULL, len) Returns -1 with errno = EINVAL Returns -1 with errno = EFAULT POSIX write(2)
write(efd, unaligned_buf, 8) Repeated unaligned 64-bit reads in loop Single safe load via memcpy C standard & Architecture ABI
write(efd, ...) overflowing to (eventfd_t)-1 Allowed counter to become (eventfd_t)-1 Blocks or fails with EAGAIN Linux eventfd(2)

Impact

  • Is new feature added? No.
  • Impact on user: None. Corrects POSIX error codes, eliminates unaligned memory access traps, and bounds eventfd counter overflow.
  • Impact on build: None.
  • Impact on hardware: None.
  • Impact on documentation: None.
  • Impact on security: Hardens against unaligned memory access traps and counter overflow boundary issues.

Testing

  • Host OS: Windows 11 (Docker container nuttx-builder Linux x86_64)
  • Target: sim:nsh with CONFIG_EVENT_FD=y
  • Compiler: GCC / Ubuntu clang

1. Code Style Check (nxstyle)

$ ./tools/nxstyle fs/vfs/fs_eventfd.c
# Exit code: 0 (0 errors, 0 warnings)

2. Build Verification

$ make -j$(nproc)
...
CC:  fs/vfs/fs_eventfd.c
AR:  staging/libfs.a fs_eventfd.o
LD:  nuttx
Pac SIM with dynamic libs..
SIM elf with dynamic libs archive in nuttx.tgz
# Exit code: 0

3. Binary Size

   text    data     bss     dec     hex filename
   2089      72       0    2161     871 /repos/nuttx/fs/fs_eventfd.o
 604057    5340   46752  656149   a0315 /repos/nuttx/nuttx

4. Simulator Boot and Runtime Test

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

@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 13, 2026
@github-actions

github-actions Bot commented Nov 13, 2026 •

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

Copy link
Copy Markdown
Contributor

fixed by #21771.

1. eventfd_do_read():
   - Separate buffer nullity and size validation: return -EFAULT when
     buffer == NULL per POSIX read(2), and -EINVAL when
     len < sizeof(eventfd_t) per Linux eventfd(2) specification.
   - Eliminate unaligned memory access: replace direct pointer casting
     and 64-bit store on (FAR eventfd_t *)buffer with a local variable
     and memcpy(), preventing alignment traps on strict architectures.

2. eventfd_do_write():
   - Separate buffer nullity and size validation: return -EFAULT when
     buffer == NULL per POSIX write(2), and -EINVAL when
     len < sizeof(eventfd_t) per Linux eventfd(2) specification.
   - Read input counter once into a local variable via memcpy() to
     prevent unaligned 64-bit loads and eliminate repeated buffer
     dereferencing.
   - Bound counter overflow: enforce Linux eventfd maximum counter
     ceiling (new_counter < dev->counter || new_counter == (eventfd_t)-1)
     to prevent counter from reaching reserved (eventfd_t)-1.

Assisted-by: Google Gemini <gemini-3.8-flash>
Signed-off-by: Swatantra Yadav <[email protected]>
@Swatantra-66
Swatantra-66 force-pushed the fix/vfs-eventfd-posix-validation branch from 37f0ee8 to 8bb8609 Compare November 14, 2026 07:26
@acassis
acassis merged commit f926b18 into apache:master Nov 14, 2026
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