Skip to contentSkip to conversation

ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit

PATCH In review Yun Zhou 2 participants 5 messages Jul 1 – Oct 16 NTFS3 filesystem

lore
Share & tools
lore

Yun Zhou

author
#0 lore raw

cover ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit

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(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 ntfs3: remove ntfs_iget5 call in ntfs_dir_emit to fix recursive deadlock

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 [email protected]
Fixes d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata walk")
---
 fs/ntfs3/dir.c | 16 ----------------
 1 file changed, 16 deletions(-)
fs/ntfs3/dir.c +0 -16
index 873d52233003..07de564a2c47 100644
@@ -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);
 }
 
Signature · 1 line
2.43.0
author
#2 lore raw

PATCH 2/2 ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks

In reply to 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.

---
 fs/ntfs3/dir.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
fs/ntfs3/dir.c +20 -2
index 07de564a2c47..65883409689a 100644
@@ -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:
Signature · 1 line
2.43.0
#3 lore raw

Re: [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks

Quoted 70 lines · 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.

---
  fs/ntfs3/dir.c | 22 ++++++++++++++++++++--
  1 file changed, 20 insertions(+), 2 deletions(-)
fs/ntfs3/dir.c +17 -1
index 07de564a2c47..65883409689a 100644
@@ -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'.
  		 */

fs/ntfs3/dir.c static int ntfs_readdir(struct file *file, struct dir_context *ctx) · line 556

Quoted 10 lines
fs/ntfs3/dir.c +3 -1
@@ -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

author
#4 lore raw
In reply to Yun Zhou

Friendly ping.

Quoted 28 lines · 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(-)