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 fab614ba83d fs/vfs: fix pseudofs readdir use-after-free on SMP
fab614ba83d is described below

commit fab614ba83d65048a438e823dcdbb853acd54d1f
Author: dengwenqi <[email protected]>
AuthorDate: Fri Nov 13 15:46:02 2026 +0800

    fs/vfs: fix pseudofs readdir use-after-free on SMP
    
    open_pseudodir() read inode->i_child and addref'd it in two separate
    unlocked reads, and seek_pseudodir() read fd_root->i_child before
    acquiring the inode tree lock.  On SMP a concurrent inode_insert()/
    inode_unlink() can change i_child between the reads, leaving the
    directory cursor (pdir->next) pointing at an inode that was never
    addref'd; a concurrent inode_remove() then frees it, and the next
    readdir() dereferences the freed inode.  KASAN reports the 0x55555555
    freed-memory poison at read_pseudodir()'s atomic_add(&pdir->next->
    i_crefs, 1) (DFAR=0x55555561).
    
    Take the first child inode and its reference under inode_lock() in
    open_pseudodir(), and read the seek starting inode under the lock in
    seek_pseudodir(), so the cursor always holds a valid reference.
    
    Signed-off-by: dengwenqi <[email protected]>
    Signed-off-by: jingfei <[email protected]>
---
 fs/vfs/fs_dir.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/fs/vfs/fs_dir.c b/fs/vfs/fs_dir.c
index 72bd42fd28f..c21eb72428f 100644
--- a/fs/vfs/fs_dir.c
+++ b/fs/vfs/fs_dir.c
@@ -166,8 +166,13 @@ static int open_pseudodir(FAR struct inode *inode,
 
   *dir              = &pdir->dir;
   pdir->dir.fd_root = inode;          /* Save the inode where we start */
+
+  /* A concurrent unlink may free i_child between the read and the addref */
+
+  inode_lock();
   pdir->next        = inode->i_child; /* This next node for readdir */
-  inode_addref(inode->i_child);
+  inode_addref(pdir->next);
+  inode_unlock();
   return 0;
 }
 
@@ -188,6 +193,8 @@ static off_t seek_pseudodir(FAR struct file *filep, off_t 
offset)
    * "rewind" to the root dir.
    */
 
+  inode_lock();
+
   if (offset < filep->f_pos)
     {
       pos  = 0;
@@ -205,8 +212,6 @@ static off_t seek_pseudodir(FAR struct file *filep, off_t 
offset)
    * be a very unpredictable operation.
    */
 
-  inode_lock();
-
   for (; curr != NULL && pos != offset; pos++, curr = curr->i_peer);
 
   /* Now get the inode to visit next time that readdir() is called */

Reply via email to