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

   ## Summary
   
   POSIX IEEE Std 1003.1 and Linux specifications (`pipe2(2)`) specify 
parameter and flags validation for `pipe2()`:
   
   1. **POSIX / Linux `pipefd` Validation**:
      Linux `pipe2(2)` explicitly mandates:
      > *"EFAULT pipefd is not valid."*
      Previously, `pipe2()` directly dereferenced `fd[1] = open(...)` without 
checking `fd != NULL`, resulting in an immediate **CWE-476 NULL pointer 
dereference** kernel crash when `pipe(NULL)` or `pipe2(NULL, ...)` was called. 
Validates `fd == NULL` returning `ERROR` with `errno = EFAULT`.
   
   2. **POSIX / Linux Flags Validation**:
      Linux `pipe2(2)` explicitly mandates:
      > *"EINVAL (pipe2()) Invalid value in flags."*
      The only valid flags for `pipe2()` are `O_CLOEXEC` and `O_NONBLOCK` (or 
`0`). Previously, NuttX did not validate `flags`, silently accepting and 
ignoring invalid flag bits. Validates `(flags & ~(O_CLOEXEC | O_NONBLOCK)) != 
0` returning `ERROR` with `errno = EINVAL` (or `-EINVAL` in `file_pipe()`).
   
   3. **Avoid Premature Driver Registration & Resource Allocation**:
      Previously, `pipe2()` called `pipe_register(CONFIG_DEV_PIPE_SIZE, ...)` 
to allocate minor numbers, allocate heap memory via `pipecommon_allocdev()`, 
and register `/dev/pipe/N` in the VFS *before* checking parameters. Moving 
validation upfront ensures invalid system calls fail immediately without 
allocating heap memory or registering dangling `/dev/pipe/N` drivers.
   
   4. **Defensive Pointer Validation in `file_pipe()`**:
      Defensively validates `filep != NULL && filep[0] != NULL && filep[1] != 
NULL` returning `-EINVAL` before dereferencing file pointers, and rejects 
invalid flag bits.
   
   ---
   
   ## Before vs. After Behavior
   
   | Scenario | Before | After |
   | :--- | :--- | :--- |
   | `pipe(NULL)` or `pipe2(NULL, 0)` | Dereferenced `fd[1]` directly (**Kernel 
Panic / Crash**) | Fails immediately with `ERROR`, `errno = EFAULT` |
   | `pipe2(fd, 0x1234)` (invalid flags) | Silently accepted arbitrary flags, 
succeeded | Fails immediately with `ERROR`, `errno = EINVAL` |
   | Invalid invocation (e.g. `pipe2(NULL, ...)` or bad flags) | Pre-allocated 
minor numbers & heap device `/dev/pipe/N` before crashing | Fails immediately; 
**zero heap memory allocated, zero drivers registered** |
   | `file_pipe(NULL, ...)` or invalid flags | Dereferenced uninitialized file 
pointers | Safely returns `-EINVAL` |
   
   ---
   
   ## Impact
   
   * **Security & Robustness**: Eliminates CWE-476 NULL pointer dereference 
panics when `pipe(NULL)` or `pipe2(NULL, ...)` is called.
   * **POSIX / Linux Conformance**: Strictly enforces `EFAULT` on invalid 
pointers and `EINVAL` on invalid flag bits per Linux `pipe2(2)`.
   * **Resource Protection**: Prevents failed `pipe2()` invocations from 
allocating heap device memory or leaking `/dev/pipe/N` driver entries in the 
VFS.
   * **Compatibility**: Standard-compliant applications using `0`, `O_CLOEXEC`, 
or `O_NONBLOCK` continue to work without any changes.
   
   ---
   
   ## Testing
   
   ### 1. Build Verification
   Cleanly built with `arm-none-eabi-gcc 10.3.1` on `sam4s-xplained-pro:nsh` 
(`CONFIG_PIPES=y`, `CONFIG_DEV_PIPE_SIZE > 0`):
   ```text
   $ ./tools/configure.sh sam4s-xplained-pro:nsh
   $ make -j4
   ...
   CC:  drivers/pipes/pipe.c
   CC:  drivers/pipes/pipe_common.c
   CC:  unistd/lib_pipe.c
   LD:  nuttx
   CP:  nuttx.hex
   CP:  nuttx.bin
   
   $ arm-none-eabi-size nuttx
      text    data     bss     dec     hex filename
    140728    1756    8144  150628   24c64 nuttx
   ```
   
   ### 2. Code Style Verification
   Checked with `nxstyle` inside Docker:
   ```text
   $ ./tools/nxstyle drivers/pipes/pipe.c
   No errors, no warnings.
   ```
   
   ### 3. What Was Not Tested
   * Real hardware target (tested using cross-compilation for ARM Cortex-M4).
   * Protected and Kernel build modes.


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