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

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

commit 236ec9844f58f1a1135c81cb4ca7602726573e94
Author: hujun5 <[email protected]>
AuthorDate: Wed Dec 20 18:39:40 2023 +0800

    fs: dup3 should pass the fdcheck & fdsan
    
    Signed-off-by: hujun5 <[email protected]>
    Signed-off-by: ligd <[email protected]>
---
 fs/inode/fs_files.c | 31 ++++++++++++++++++++++++++++++-
 fs/vfs/fs_dup2.c    | 10 ++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c
index a574a6e79a..e61f432c52 100644
--- a/fs/inode/fs_files.c
+++ b/fs/inode/fs_files.c
@@ -209,6 +209,13 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int 
fd1, int fd2,
                             int flags)
 {
   FAR struct filelist *list;
+  FAR struct file *filep;
+#ifdef CONFIG_FDCHECK
+  uint8_t f_tag_fdcheck;
+#endif
+#ifdef CONFIG_FDSAN
+  uint64_t f_tag_fdsan;
+#endif
   int count;
   int ret;
 
@@ -241,14 +248,36 @@ static int nx_dup3_from_tcb(FAR struct tcb_s *tcb, int 
fd1, int fd2,
         }
     }
 
+  filep = files_fget(list, fd2);
+  if (filep == NULL)
+    {
+      return -EBADF;
+    }
+
+#ifdef CONFIG_FDSAN
+  f_tag_fdsan = filep->f_tag_fdsan;
+#endif
+
+#ifdef CONFIG_FDCHECK
+  f_tag_fdcheck = filep->f_tag_fdcheck;
+#endif
+
   /* Perform the dup3 operation */
 
-  ret = file_dup3(files_fget(list, fd1), files_fget(list, fd2), flags);
+  ret = file_dup3(files_fget(list, fd1), filep, flags);
   if (ret < 0)
     {
       return ret;
     }
 
+#ifdef CONFIG_FDSAN
+  filep->f_tag_fdsan = f_tag_fdsan;
+#endif
+
+#ifdef CONFIG_FDCHECK
+  filep->f_tag_fdcheck = f_tag_fdcheck;
+#endif
+
 #ifdef CONFIG_FDCHECK
   return fdcheck_protect(fd2);
 #else
diff --git a/fs/vfs/fs_dup2.c b/fs/vfs/fs_dup2.c
index c7baf3a28f..f5917456e7 100644
--- a/fs/vfs/fs_dup2.c
+++ b/fs/vfs/fs_dup2.c
@@ -164,6 +164,16 @@ int file_dup3(FAR struct file *filep1, FAR struct file 
*filep2, int flags)
         }
     }
 
+  /* Copy tag */
+
+#ifdef CONFIG_FDSAN
+  filep2->f_tag_fdsan = filep1->f_tag_fdsan;
+#endif
+
+#ifdef CONFIG_FDCHECK
+  filep2->f_tag_fdcheck = filep1->f_tag_fdcheck;
+#endif
+
   return OK;
 }
 

Reply via email to