mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit
@ 2026-07-01  1:30 Yun Zhou
  2026-07-01  1:30 ` [PATCH 1/2] ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock Yun Zhou
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yun Zhou @ 2026-07-01  1:30 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, yun.zhou

Commit d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata
walk") extended ni_lock to cover the entire directory walk in
ntfs_readdir(), including dir_emit() calls that copy data to userspace.
This introduced multiple deadlocks reported by syzkaller:

1. Recursive ni_lock: ntfs_dir_emit() -> ntfs_iget5() -> mi_read()
   tries to acquire ni_lock on a child inode with the same lock class
   while the parent's ni_lock is already held.

2. Lock-order inversions: dir_emit() may trigger page faults that need
   mmap_lock or mapping->invalidate_lock, conflicting with paths that
   acquire those locks before ni_lock (ntfs_fallocate, mmap).

This series fixes both issues:
- Patch 1 removes the ntfs_iget5() call that caused recursive locking.
- Patch 2 releases ni_lock before calling dir_emit(), using the XFS
  approach of reading data into stable buffers under the lock, then
  emitting to userspace after releasing it.

Yun Zhou (2):
  ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive
    deadlock
  ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix
    deadlocks

 fs/ntfs3/dir.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/2] ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock
  2026-07-01  1:30 [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Yun Zhou
@ 2026-07-01  1:30 ` Yun Zhou
  2026-07-01  1:30 ` [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks Yun Zhou
  2026-07-19 12:29 ` [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Zhou, Yun
  2 siblings, 0 replies; 5+ messages in thread
From: Yun Zhou @ 2026-07-01  1:30 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, yun.zhou

ntfs_dir_emit() calls ntfs_iget5() to refine the directory entry type
by reading the child inode's MFT record.  However, ntfs_dir_emit() is
called from ntfs_readdir() under the parent directory's ni_lock, and
ntfs_iget5() -> mi_read() tries to acquire ni_lock on the child inode
with the same lock class, triggering a recursive locking deadlock.

Remove the ntfs_iget5() call.  The dt_type derived from the directory
entry's FILE_ATTRIBUTE_DIRECTORY flag is sufficient for readdir.  The
original code's own comment noted this approach was "not good idea".

Reported-by: syzbot+17f812893d5906837f33@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=17f812893d5906837f33
Fixes: d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata walk")
Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
---
 fs/ntfs3/dir.c | 16 ----------------
 1 file changed, 16 deletions(-)

diff --git a/fs/ntfs3/dir.c b/fs/ntfs3/dir.c
index 873d52233003..07de564a2c47 100644
--- a/fs/ntfs3/dir.c
+++ b/fs/ntfs3/dir.c
@@ -327,22 +327,6 @@ static inline bool ntfs_dir_emit(struct ntfs_sb_info *sbi,
 	 */
 	dt_type = (fname->dup.fa & FILE_ATTRIBUTE_DIRECTORY) ? DT_DIR : DT_REG;
 
-	/*
-	 * It is not reliable to detect the type of name using duplicated information
-	 * stored in parent directory.
-	 * The only correct way to get the type of name - read MFT record and find ATTR_STD.
-	 * The code below is not good idea.
-	 * It does additional locks/reads just to get the type of name.
-	 * Should we use additional mount option to enable branch below?
-	 */
-	if (fname->dup.extend_data && ino != ni->mi.rno) {
-		struct inode *inode = ntfs_iget5(sbi->sb, &e->ref, NULL);
-		if (!IS_ERR_OR_NULL(inode)) {
-			dt_type = fs_umode_to_dtype(inode->i_mode);
-			iput(inode);
-		}
-	}
-
 	return dir_emit(ctx, (s8 *)name, name_len, ino, dt_type);
 }
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks
  2026-07-01  1:30 [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Yun Zhou
  2026-07-01  1:30 ` [PATCH 1/2] ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock Yun Zhou
@ 2026-07-01  1:30 ` Yun Zhou
  2026-10-16 14:48   ` Konstantin Komarov
  2026-07-19 12:29 ` [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Zhou, Yun
  2 siblings, 1 reply; 5+ messages in thread
From: Yun Zhou @ 2026-07-01  1:30 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, yun.zhou

ntfs_readdir() holds ni_lock while calling ntfs_read_hdr(), which calls
dir_emit() to copy directory entries to userspace.  If the user buffer
triggers a page fault, the fault handler may need mmap_lock or
mapping->invalidate_lock, creating lock-order inversions with paths
that acquire those locks before ni_lock (e.g. ntfs_fallocate, mmap).

Fix by releasing ni_lock before calling ntfs_read_hdr():

- For non-root index nodes, indx_read_ra() returns data in a kmalloc'd
  buffer (node->index) that remains valid after ni_unlock.

- For the root index header, the data lives in the main MFT record
  whose lifetime is bound to the inode.  i_rwsem shared prevents
  concurrent directory modifications, so it is safe to access after
  ni_unlock.

Reported-by: syzbot+7736960b837908f3a81d@syzkaller.appspotmail.com
Reported-by: syzbot+262a71e9d2faf8747085@syzkaller.appspotmail.com
Reported-by: syzbot+cfc6e8106bec1c524f7d@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7736960b837908f3a81d
Closes: https://syzkaller.appspot.com/bug?extid=262a71e9d2faf8747085
Closes: https://syzkaller.appspot.com/bug?extid=cfc6e8106bec1c524f7d
Fixes: d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata walk")
Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
---
 fs/ntfs3/dir.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/dir.c b/fs/ntfs3/dir.c
index 07de564a2c47..65883409689a 100644
--- a/fs/ntfs3/dir.c
+++ b/fs/ntfs3/dir.c
@@ -491,12 +491,22 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
 	if (pos >= sbi->record_size) {
 		bit = (pos - sbi->record_size) >> index_bits;
 	} else {
+		/*
+		 * Release ni_lock before dir_emit (which may fault into
+		 * user pages).  root->ihdr lives in the main MFT record
+		 * whose lifetime is bound to the inode, and i_rwsem shared
+		 * prevents concurrent directory modifications.
+		 */
+		ni_unlock(ni);
+
 		/*
 		 * Add each name from root in 'ctx'.
 		 */
 		err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
 		if (err)
-			goto out_unlock;
+			goto out;
+
+		ni_lock(ni);
 		bit = 0;
 	}
 
@@ -525,6 +535,12 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
 		if (err)
 			goto out_unlock;
 
+		/*
+		 * node->index is a kmalloc'd buffer, safe after ni_unlock.
+		 * Release lock before dir_emit to avoid lock-order issues.
+		 */
+		ni_unlock(ni);
+
 		/*
 		 * Add each name from index in 'ctx'.
 		 */
@@ -532,7 +548,9 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
 				    ((u64)bit << index_bits) + sbi->record_size,
 				    pos, name, ctx);
 		if (err)
-			goto out_unlock;
+			goto out;
+
+		ni_lock(ni);
 	}
 
 out_unlock:
-- 
2.43.0


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit
  2026-07-01  1:30 [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Yun Zhou
  2026-07-01  1:30 ` [PATCH 1/2] ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock Yun Zhou
  2026-07-01  1:30 ` [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks Yun Zhou
@ 2026-07-19 12:29 ` Zhou, Yun
  2 siblings, 0 replies; 5+ messages in thread
From: Zhou, Yun @ 2026-07-19 12:29 UTC (permalink / raw)
  To: almaz.alexandrovich; +Cc: ntfs3, linux-kernel, yun.zhou

Friendly ping.

On 7/1/2026 9:30 AM, Yun Zhou wrote:
> Commit d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata
> walk") extended ni_lock to cover the entire directory walk in
> ntfs_readdir(), including dir_emit() calls that copy data to userspace.
> This introduced multiple deadlocks reported by syzkaller:
> 
> 1. Recursive ni_lock: ntfs_dir_emit() -> ntfs_iget5() -> mi_read()
>     tries to acquire ni_lock on a child inode with the same lock class
>     while the parent's ni_lock is already held.
> 
> 2. Lock-order inversions: dir_emit() may trigger page faults that need
>     mmap_lock or mapping->invalidate_lock, conflicting with paths that
>     acquire those locks before ni_lock (ntfs_fallocate, mmap).
> 
> This series fixes both issues:
> - Patch 1 removes the ntfs_iget5() call that caused recursive locking.
> - Patch 2 releases ni_lock before calling dir_emit(), using the XFS
>    approach of reading data into stable buffers under the lock, then
>    emitting to userspace after releasing it.
> 
> Yun Zhou (2):
>    ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive
>      deadlock
>    ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix
>      deadlocks
> 
>   fs/ntfs3/dir.c | 38 ++++++++++++++++++++------------------
>   1 file changed, 20 insertions(+), 18 deletions(-)
> 


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks
  2026-07-01  1:30 ` [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks Yun Zhou
@ 2026-10-16 14:48   ` Konstantin Komarov
  0 siblings, 0 replies; 5+ messages in thread
From: Konstantin Komarov @ 2026-10-16 14:48 UTC (permalink / raw)
  To: Yun Zhou; +Cc: ntfs3, linux-kernel

On 7/1/26 03:30, Yun Zhou wrote:

> ntfs_readdir() holds ni_lock while calling ntfs_read_hdr(), which calls
> dir_emit() to copy directory entries to userspace.  If the user buffer
> triggers a page fault, the fault handler may need mmap_lock or
> mapping->invalidate_lock, creating lock-order inversions with paths
> that acquire those locks before ni_lock (e.g. ntfs_fallocate, mmap).
>
> Fix by releasing ni_lock before calling ntfs_read_hdr():
>
> - For non-root index nodes, indx_read_ra() returns data in a kmalloc'd
>    buffer (node->index) that remains valid after ni_unlock.
>
> - For the root index header, the data lives in the main MFT record
>    whose lifetime is bound to the inode.  i_rwsem shared prevents
>    concurrent directory modifications, so it is safe to access after
>    ni_unlock.
>
> Reported-by: syzbot+7736960b837908f3a81d@syzkaller.appspotmail.com
> Reported-by: syzbot+262a71e9d2faf8747085@syzkaller.appspotmail.com
> Reported-by: syzbot+cfc6e8106bec1c524f7d@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=7736960b837908f3a81d
> Closes: https://syzkaller.appspot.com/bug?extid=262a71e9d2faf8747085
> Closes: https://syzkaller.appspot.com/bug?extid=cfc6e8106bec1c524f7d
> Fixes: d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata walk")
> Signed-off-by: Yun Zhou <yun.zhou@windriver.com>
> ---
>   fs/ntfs3/dir.c | 22 ++++++++++++++++++++--
>   1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/fs/ntfs3/dir.c b/fs/ntfs3/dir.c
> index 07de564a2c47..65883409689a 100644
> --- a/fs/ntfs3/dir.c
> +++ b/fs/ntfs3/dir.c
> @@ -491,12 +491,22 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
>   	if (pos >= sbi->record_size) {
>   		bit = (pos - sbi->record_size) >> index_bits;
>   	} else {
> +		/*
> +		 * Release ni_lock before dir_emit (which may fault into
> +		 * user pages).  root->ihdr lives in the main MFT record
> +		 * whose lifetime is bound to the inode, and i_rwsem shared
> +		 * prevents concurrent directory modifications.
> +		 */
> +		ni_unlock(ni);
> +
>   		/*
>   		 * Add each name from root in 'ctx'.
>   		 */
>   		err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
>   		if (err)
> -			goto out_unlock;
> +			goto out;
> +
> +		ni_lock(ni);
>   		bit = 0;
>   	}
>   
> @@ -525,6 +535,12 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
>   		if (err)
>   			goto out_unlock;
>   
> +		/*
> +		 * node->index is a kmalloc'd buffer, safe after ni_unlock.
> +		 * Release lock before dir_emit to avoid lock-order issues.
> +		 */
> +		ni_unlock(ni);
> +
>   		/*
>   		 * Add each name from index in 'ctx'.
>   		 */
> @@ -532,7 +548,9 @@ static int ntfs_readdir(struct file *file, struct dir_context *ctx)
>   				    ((u64)bit << index_bits) + sbi->record_size,
>   				    pos, name, ctx);
>   		if (err)
> -			goto out_unlock;
> +			goto out;
> +
> +		ni_lock(ni);
>   	}
>   
>   out_unlock:

Hello,

The patch was applied, thanks.

Regards,
Konstantin


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-10-16 14:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-07-01  1:30 [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Yun Zhou
2026-07-01  1:30 ` [PATCH 1/2] ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock Yun Zhou
2026-07-01  1:30 ` [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks Yun Zhou
2026-10-16 14:48   ` Konstantin Komarov
2026-07-19 12:29 ` [PATCH 0/2] ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit Zhou, Yun

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®