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

   ## Summary
   
   This PR validates parameters, prevents out-of-bounds memory accesses, and 
corrects error codes in `fs/vfs/fs_fcntl.c`:
   
   1. **`F_DUPFD` and `F_DUPFD_CLOEXEC`**:
      - **Negative `minfd` Validation (CWE-125 Out-of-Bounds Access)**: 
Validates that `minfd >= 0`, returning `-EINVAL` per POSIX IEEE Std 1003.1. 
Previously, passing a negative `minfd` (e.g. `-1`) resulted in negative 
row/column indexing (`minfd / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK`, `minfd % 
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK`) in `fdlist_dupfile()`, causing 
out-of-bounds array reads and kernel panics.
   
   2. **`F_SETFD`**:
      - **POSIX Error Code Conformance**: When invalid flags other than 
`FD_CLOEXEC` are passed, `fcntl()` now sets `errno = EINVAL` instead of 
`ENOSYS`. Under POSIX.1-2024 / IEEE Std 1003.1 `fcntl(2)`, passing invalid flag 
bits to a command requires `[EINVAL]`. Returning `ENOSYS` ("Function not 
implemented") broke POSIX compliance test suites.
   
   3. **`file_vfcntl()`**:
      - **Defensive Pointer Null-Check (CWE-476)**: Validates `filep != NULL && 
filep->f_inode != NULL` before accessing `filep->f_inode`, returning `-EBADF` 
and preventing NULL pointer dereference panics when invoked on invalid or 
unallocated file structures.
   
   4. **`F_GETLK`, `F_SETLK`, and `F_SETLKW`**:
      - **`flock` Pointer Validation**: Validates `flock != NULL`, returning 
`-EFAULT` per POSIX / Linux `fcntl(2)`. Previously, passing `NULL` was passed 
down to `file_lock_normalize()`, which unconditionally dereferenced 
`flock->l_type`, causing a kernel crash.
   
   5. **`F_GETPATH`**:
      - **Buffer Validation**: Validates that `path != NULL`, returning 
`-EFAULT` to prevent NULL pointer dereferences in underlying filesystem drivers.
   
   6. **`F_SETPIPE_SZ`**:
      - **Capacity Bounds**: Validates that `size > 0`, returning `-EINVAL` per 
Linux `fcntl(2)` specification. Previously, passing a negative value converted 
to an unsigned integer and clamped to `CONFIG_DEV_PIPE_MAXSIZE`, silently 
succeeding instead of reporting an error.
   
   ---
   
   ## Comparison Table
   
   | Issue / Command | Prior Behavior | Hardened POSIX Behavior | Specification 
|
   |---|---|---|---|
   | `fcntl(fd, F_DUPFD, -1)` | Out-of-bounds array access (CWE-125) / crash | 
Returns `-1` with `errno = EINVAL` | POSIX IEEE Std 1003.1 |
   | `fcntl(fd, F_SETFD, ~FD_CLOEXEC)` | Returns `-1` with `errno = ENOSYS` ❌ | 
Returns `-1` with `errno = EINVAL` ✅ | POSIX IEEE Std 1003.1 |
   | `file_fcntl(NULL, cmd)` | Kernel Panic (CWE-476 NULL dereference) | 
Returns `-EBADF` | POSIX IEEE Std 1003.1 |
   | `fcntl(fd, F_SETLK, NULL)` | Kernel Panic (`flock->l_type` NULL 
dereference) | Returns `-1` with `errno = EFAULT` | Linux / POSIX `fcntl(2)` |
   | `fcntl(fd, F_SETPIPE_SZ, -1)` | Silently resizes pipe to `MAXSIZE` | 
Returns `-1` with `errno = EINVAL` | Linux `fcntl(2)` |
   | `fcntl(fd, F_GETPATH, NULL)` | Kernel Panic (`strcpy(NULL, ...)` in 
drivers) | Returns `-1` with `errno = EFAULT` | POSIX / Linux |
   
   ---
   
   ## Impact
   
   - Is new feature added? No.
   - Impact on user: None. Corrects POSIX error codes and prevents 
out-of-bounds and NULL dereference panics.
   - Impact on build: None.
   - Impact on hardware: None.
   - Impact on documentation: None.
   - Impact on security: Hardens against CWE-125 (out-of-bounds access) and 
CWE-476 (NULL pointer dereference).
   
   ---
   
   ## Testing
   
   - **Host OS**: Windows 11 (Docker container `nuttx-builder` Linux x86_64)
   - **Target**: `sim:nsh`
   - **Compiler**: GCC / Ubuntu clang
   
   ### 1. Code Style Check (`nxstyle`)
   ```bash
   $ ./tools/nxstyle fs/vfs/fs_fcntl.c
   # Exit code: 0 (0 errors, 0 warnings)
   ```
   
   ### 2. Build Verification
   ```bash
   $ make -j$(nproc)
   ...
   CC:  vfs/fs_fcntl.c
   IN: fs/libfs.a -> staging/libfs.a
   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
      1757       0       0    1757     6dd /repos/nuttx/fs/fs_fcntl.o
    603833    5340   46752  655925   a0235 /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