Skip to content

drivers/pipes: validate parameters and flags in pipe2 and file_pipe - #21938

Merged
xiaoxiang781216 merged 1 commit into
apache:masterfrom
Swatantra-66:fix/pipe2-posix-validation
Nov 17, 2026
Merged

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

Conversation

@Swatantra-66

Copy link
Copy Markdown
Contributor

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

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

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

POSIX IEEE Std 1003.1 and Linux specifications (pipe2(2)) specify
parameter and flags validation for pipe2:

1. Validate pipefd pointer in pipe2():
   Linux pipe2(2) specifies that passing an invalid pipefd shall fail
   with EFAULT. Previously, pipe2() dereferenced fd[1] directly
   without checking fd != NULL, resulting in an immediate CWE-476
   NULL pointer dereference kernel panic.

2. Validate flags against O_CLOEXEC and O_NONBLOCK:
   Linux pipe2(2) specifies that flags containing any bits other than
   O_CLOEXEC and O_NONBLOCK shall fail with EINVAL. Previously, NuttX
   did not validate flags, silently accepting invalid bits.

3. Avoid premature driver registration on invalid arguments:
   Previously, pipe2() called pipe_register() to register /dev/pipe/N
   and allocate pipe structures before validating input arguments.
   Validating fd and flags upfront avoids premature device
   registration and memory allocation on invalid parameters.

4. Defensively validate pointers and flags in file_pipe():
   Validates filep != NULL && filep[0] != NULL && filep[1] != NULL
   and flags against ~(O_CLOEXEC | O_NONBLOCK), returning -EINVAL
   on invalid input.

Assisted-by: Google DeepMind:antigravity
Signed-off-by: Swatantra Yadav <[email protected]>
@github-actions github-actions Bot added Area: Drivers Drivers issues Size: S The size of the change in this PR is small labels Nov 17, 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 b8bfb0a into apache:master Nov 17, 2026
51 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Drivers Drivers 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