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 889983ae4ef fs/inode: validate parameters and avoid premature 
allocation in dup3
889983ae4ef is described below

commit 889983ae4efd282224eec7ecfac80573fb7bd29b
Author: Swatantra Yadav <[email protected]>
AuthorDate: Mon Nov 16 18:27:49 2026 +0530

    fs/inode: validate parameters and avoid premature allocation in dup3
    
    POSIX IEEE Std 1003.1 and Linux specifications (dup3(2)) specify
    parameter validation and descriptor handling for dup3:
    
    1. Validate flags against O_CLOEXEC:
       Linux dup3(2) specifies that if flags contains any bit other than
       O_CLOEXEC, the call shall fail with -EINVAL. Previously, NuttX did
       not validate flags, silently accepting invalid bits.
    
    2. Avoid premature heap expansion on invalid oldfd (fd1):
       Previously, fdlist_dup3() called fdlist_extend() to allocate heap
       memory and expand the file descriptor table before verifying fd1.
       When invoked with an invalid oldfd (e.g., fd1 = -1 or an unopened
       descriptor), the task's file table was permanently expanded on
       the heap before returning -EBADF. Moving fdlist_get2() before
       fdlist_extend() rejects invalid fd1 immediately without heap
       allocations or side-effects. If fdlist_extend() fails later,
       file_put(filep1) safely releases the acquired file reference.
    
    3. Defensively validate list != NULL:
       Returns -EINVAL if the provided descriptor list is NULL, preventing
       CWE-476 NULL pointer dereferences in fdlist_limit(list).
    
    4. Query descriptor from list in fdlist_dup2():
       When fd1 == fd2, fdlist_dup2() previously called file_get(fd1, &filep)
       which checked this_task() rather than the provided list. Uses
       fdlist_get2(list, fd1, &filep, NULL) so the descriptor is verified
       in the intended file list (e.g., during task spawning).
    
    Assisted-by: Google DeepMind:antigravity
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 fs/inode/fs_files.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index 3bb6cab3c3c..dd6dc883dce 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -362,38 +362,47 @@ int fdlist_dup3(FAR struct fdlist *list, int fd1, int 
fd2, int flags)
   FAR struct fd *fdp1;
   int ret;
 
+  if (list == NULL)
+    {
+      return -EINVAL;
+    }
+
   if (fd1 == fd2)
     {
       return -EINVAL;
     }
 
+  if ((flags & ~O_CLOEXEC) != 0)
+    {
+      return -EINVAL;
+    }
+
 #ifdef CONFIG_FDCHECK
   fd2 = fdcheck_restore(fd2);
 #endif
 
-  /* Get the file descriptor list.  It should not be NULL in this context. */
-
   if (fd2 < 0 || fd2 >= fdlist_limit(list))
     {
       return -EBADF;
     }
 
+  ret = fdlist_get2(list, fd1, &filep1, &fdp1);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
   if (fd2 >= fdlist_count(list))
     {
       ret = fdlist_extend(list,
                           fd2 / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + 1);
       if (ret < 0)
         {
+          file_put(filep1);
           return ret;
         }
     }
 
-  ret = fdlist_get2(list, fd1, &filep1, &fdp1);
-  if (ret < 0)
-    {
-      return ret;
-    }
-
   fdlist_install(list, fd2, filep1, fdp1, flags, false);
   file_put(filep1);
 
@@ -992,7 +1001,7 @@ int fdlist_dup2(FAR struct fdlist *list, int fd1, int fd2)
       FAR struct file *filep;
       int ret;
 
-      ret = file_get(fd1, &filep);
+      ret = fdlist_get2(list, fd1, &filep, NULL);
       if (ret < 0)
         {
           return ret;

Reply via email to