ntfs3: fix deadlocks in ntfs_readdir caused by ni_lock held across dir_emit
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
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".
--- fs/ntfs3/dir.c | 16 ---------------- 1 file changed, 16 deletions(-)
fs/ntfs3/dir.c +0 -16
@@ -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
PATCH 2/2 ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks
In reply to Yun Zhountfs_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
@@ -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
Konstantin Komarov
maintainerRe: [PATCH 2/2] ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks
Quoted 70 lines · Yun Zhou
ntfs_readdir()holdsni_lockwhile callingntfs_read_hdr(), which callsdir_emit()to copy directory entries to userspace. If the user buffer triggers a page fault, the fault handler may needmmap_lockormapping->invalidate_lock, creating lock-order inversions with paths that acquire those locks beforeni_lock(e.g.ntfs_fallocate, mmap).Fix by releasing
ni_lockbefore callingntfs_read_hdr():- For non-root index nodes,
indx_read_ra()returns data in a kmalloc'd buffer (node->index) that remains valid afterni_unlock.- For the root index header, the data lives in the main MFT record whose lifetime is bound to the inode.
i_rwsemshared prevents concurrent directory modifications, so it is safe to access afterni_unlock.Reported-by [email protected]Reported-by [email protected]Reported-by [email protected]Fixes d62cf685d12e ("fs/ntfs3: hold ni_lock across readdir metadata walk")Signed-off-by Yun Zhou <[email protected]>--- fs/ntfs3/dir.c | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-)fs/ntfs3/dir.c +17
-1@@ -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
Friendly ping.
Quoted 28 lines · Yun Zhou
Commit
d62cf685d12e("fs/ntfs3: holdni_lockacross readdir metadata walk") extendedni_lockto cover the entire directory walk inntfs_readdir(), includingdir_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 acquireni_lockon a child inode with the same lock class while the parent'sni_lockis already held.2. Lock-order inversions:
dir_emit()may trigger page faults that needmmap_lockormapping->invalidate_lock, conflicting with paths that acquire those locks beforeni_lock(ntfs_fallocate, mmap).This series fixes both issues:
- Patch 1 removes thentfs_iget5()call that caused recursive locking.
- Patch 2 releasesni_lockbefore callingdir_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: removentfs_iget5call inntfs_dir_emitto fix recursive
deadlock
ntfs3: releaseni_lockbeforedir_emitinntfs_readdirto fix
deadlocksfs/ntfs3/dir.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-)