Swatantra-66 opened a new pull request, #21855:
URL: https://github.com/apache/nuttx/pull/21855

   ## 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`)
   ```bash
   $ ./tools/nxstyle fs/vfs/fs_eventfd.c
   # Exit code: 0 (0 errors, 0 warnings)
   ```
   
   ### 2. Build Verification
   ```bash
   $ 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
      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
   ```text
   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
   ```


-- 
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]

Reply via email to