This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new b8bfb0aac69 drivers/pipes: validate parameters and flags in pipe2 and 
file_pipe
b8bfb0aac69 is described below

commit b8bfb0aac69e82db8667654e8e6c16a250f6df90
Author: Swatantra Yadav <[email protected]>
AuthorDate: Tue Nov 17 14:59:00 2026 +0530

    drivers/pipes: validate parameters and flags in pipe2 and file_pipe
    
    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]>
---
 drivers/pipes/pipe.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/drivers/pipes/pipe.c b/drivers/pipes/pipe.c
index dc074f8b26f..8b2d664f60e 100644
--- a/drivers/pipes/pipe.c
+++ b/drivers/pipes/pipe.c
@@ -190,6 +190,18 @@ int file_pipe(FAR struct file *filep[2], size_t bufsize, 
int flags)
   int nonblock = !!(flags & O_NONBLOCK);
   int ret;
 
+  /* Check parameters */
+
+  if (filep == NULL || filep[0] == NULL || filep[1] == NULL)
+    {
+      return -EINVAL;
+    }
+
+  if ((flags & ~(O_CLOEXEC | O_NONBLOCK)) != 0)
+    {
+      return -EINVAL;
+    }
+
   /* Register a new pipe device */
 
   ret = pipe_register(bufsize, flags, devname, sizeof(devname));
@@ -263,6 +275,20 @@ int pipe2(int fd[2], int flags)
   int nonblock = !!(flags & O_NONBLOCK);
   int ret;
 
+  /* Check parameters */
+
+  if (fd == NULL)
+    {
+      set_errno(EFAULT);
+      return ERROR;
+    }
+
+  if ((flags & ~(O_CLOEXEC | O_NONBLOCK)) != 0)
+    {
+      set_errno(EINVAL);
+      return ERROR;
+    }
+
   /* Register a new pipe device */
 
   ret = pipe_register(CONFIG_DEV_PIPE_SIZE, flags, devname, sizeof(devname));

Reply via email to