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 629c4e54d1f fs/inode: validate parameters and minfd bounds in 
fdlist_dupfile
629c4e54d1f is described below

commit 629c4e54d1fe2a8833dc61c4e37ce09d92062aac
Author: Swatantra Yadav <[email protected]>
AuthorDate: Wed Nov 18 10:25:16 2026 +0530

    fs/inode: validate parameters and minfd bounds in fdlist_dupfile
    
    POSIX IEEE Std 1003.1 (fcntl(2) F_DUPFD) and file descriptor allocation
    rules specify parameter validation and bounds handling for fdlist_dupfile:
    
    1. Validate minfd >= 0 and minfd < limit:
       Previously, fdlist_dupfile() only checked minfd >= limit without
       verifying minfd >= 0. Passing a negative minfd (e.g., -1) bypassed
       the limit check, resulting in negative array indexing (minfd % 8 = -1)
       and out-of-bounds heap access (&list->fl_fds[0][-1]) or heap corruption
       (CWE-125/CWE-129/CWE-787). Checking minfd < 0 upfront safely returns
       -EINVAL.
    
    2. Defensively validate list != NULL and filep != NULL:
       Returns -EINVAL if list or filep is NULL, eliminating potential
       CWE-476 NULL pointer dereferences in fdlist_limit(list) and
       atomic_add(&filep->f_refs, 1).
    
    3. Avoid false descriptor decoding in fdcheck_restore():
       Standard streams (<= 2) are never protected by fdcheck_protect(),
       and assigned tags are never 0. In lib_fdcheck.c, fdcheck_restore()
       previously checked only (fd == 0 || val < 0), causing bare values
       >= 256 (such as minfd = 256 with tag 0) to be decoded as descriptor 1
       (matching open stdout with tag 0) or panicking if descriptor 1 was
       closed. Checking (fd <= 2 || tag_expect == 0 || val < 0) ensures bare
       descriptors are returned directly.
    
    Assisted-by: Claude:claude-opus-4.6
    Signed-off-by: Swatantra Yadav <[email protected]>
---
 fs/inode/fs_files.c          |  7 +++++--
 libs/libc/misc/lib_fdcheck.c | 11 +++++++----
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index a02e0ddcbe6..617c1cd6aa3 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -600,14 +600,17 @@ int fdlist_dupfile(FAR struct fdlist *list, int oflags, 
int minfd,
   int i;
   int j;
 
-  DEBUGASSERT(filep);
+  if (list == NULL || filep == NULL || minfd < 0)
+    {
+      return -EINVAL;
+    }
 
 #ifdef CONFIG_FDCHECK
   minfd = fdcheck_restore(minfd);
 #endif
 
   limit = fdlist_limit(list);
-  if (minfd >= limit)
+  if (minfd < 0 || minfd >= limit)
     {
       return -EINVAL;
     }
diff --git a/libs/libc/misc/lib_fdcheck.c b/libs/libc/misc/lib_fdcheck.c
index 88e56e564a5..e9d9b9fac64 100644
--- a/libs/libc/misc/lib_fdcheck.c
+++ b/libs/libc/misc/lib_fdcheck.c
@@ -62,14 +62,19 @@ static uint8_t    g_fdcheck_tag = 0;
 
 int fdcheck_restore(int val)
 {
+  uint8_t tag_expect;
   uint8_t tag_store;
   int ret;
   int fd;
 
-  /* If val is a bare fd(0~255), we should return it directly  */
+  /* If val is a bare fd (<= 2 or untagged) or negative, return it directly.
+   * Standard streams (0..2) are never protected, and assigned tags are
+   * never 0.
+   */
 
   fd = (val >> FD_SHIFT) & FD_MASK;
-  if (fd == 0 || val < 0)
+  tag_expect = (val >> TAG_SHIFT) & TAG_MASK;
+  if (fd <= 2 || tag_expect == 0 || val < 0)
     {
       return val;
     }
@@ -77,8 +82,6 @@ int fdcheck_restore(int val)
   ret = ioctl(fd, FIOC_GETTAG_FDCHECK, &tag_store);
   if (ret >= 0)
     {
-      uint8_t tag_expect = (val >> TAG_SHIFT) & TAG_MASK;
-
       if (tag_expect != tag_store)
         {
           ferr("tag_expect 0x%x tag_store 0x%x\n",

Reply via email to