[PATCH] ntfs3: snapshot root index entries before unlocking

From: Haoli Zhang

Date: Tue Nov 10 2026 - 23:52:52 EST


The syzbot report shows a slab use-after-free while ntfs_readdir() emits
a directory entry. The root index header and entries are borrowed from an
MFT record. ntfs_readdir() drops ni_lock before walking them because
dir_emit() may fault while writing to userspace, but writeback can replace
and free the record under the same ni_lock.

The unlocked path also checks each entry's parent MFT reference with
mi_is_ref(), which reads the current MFT record sequence number. Snapshot
the directory MFT reference while holding ni_lock and pass that stable
value to both root and allocation-index traversal. Copy the used root
index header under the lock as well, then walk the copy after unlocking.
This keeps dir_emit() outside ni_lock while avoiding references to the
replaceable MFT record. Validate the copied range and use subtraction-based
bounds checks when walking index entries.

Fixes: 7a5aa6c45072 ("ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks")
Reported-by: syzbot+62769d828b0914efeb09@xxxxxxxxxxxxxxxxxxxxxxxxx
Closes: https://syzkaller.appspot.com/bug?extid=62769d828b0914efeb09
Co-developed-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Signed-off-by: Jinning Zhang <25031212117@xxxxxxxxxxxxxxxxx>
Co-developed-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Bin Yu <byu@xxxxxxxxxxxxx>
Signed-off-by: Haoli Zhang <26031212468@xxxxxxxxxxxxxxxxx>

---
fs/ntfs3/dir.c | 75 +++++++++++++++++++++--------------
1 file changed, 46 insertions(+), 29 deletions(-)

diff --git a/fs/ntfs3/dir.c b/fs/ntfs3/dir.c
--- a/fs/ntfs3/dir.c
+++ b/fs/ntfs3/dir.c
@@ -336,8 +336,9 @@
* returns false if 'ctx' if full
*/
static inline bool ntfs_dir_emit(struct ntfs_sb_info *sbi,
- struct ntfs_inode *ni, const struct NTFS_DE *e,
- u8 *name, struct dir_context *ctx)
+ const struct MFT_REF *dir_ref,
+ const struct NTFS_DE *e, u8 *name,
+ struct dir_context *ctx)
{
const struct ATTR_FILE_NAME *fname;
u64 ino;
@@ -349,7 +350,9 @@
if (fname->type == FILE_NAME_DOS)
return true;

- if (!mi_is_ref(&ni->mi, &fname->home))
+ if (fname->home.low != dir_ref->low ||
+ fname->home.high != dir_ref->high ||
+ fname->home.seq != dir_ref->seq)
return true;

ino = ino_get(&e->ref);
@@ -392,7 +395,7 @@
* 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) {
+ if (fname->dup.extend_data && ino != ino_get(dir_ref)) {
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);
@@ -410,7 +413,8 @@
* returns -EINVAL if directory is corrupted.
* returns +1 if 'ctx' is full.
*/
-static int ntfs_read_hdr(struct ntfs_sb_info *sbi, struct ntfs_inode *ni,
+static int ntfs_read_hdr(struct ntfs_sb_info *sbi,
+ const struct MFT_REF *dir_ref,
const struct INDEX_HDR *hdr, u64 vbo, u64 pos,
u8 *name, struct dir_context *ctx)
{
@@ -420,12 +424,12 @@
u32 off = le32_to_cpu(hdr->de_off);

for (;; off += e_size) {
- if (off + sizeof(struct NTFS_DE) > end)
+ if (off > end || end - off < sizeof(struct NTFS_DE))
return -EINVAL;

e = Add2Ptr(hdr, off);
e_size = le16_to_cpu(e->size);
- if (e_size < sizeof(struct NTFS_DE) || off + e_size > end)
+ if (e_size < sizeof(struct NTFS_DE) || e_size > end - off)
return -EINVAL;

if (de_is_last(e))
@@ -441,7 +445,7 @@
ctx->pos = vbo + off;

/* Submit the name to the filldir callback. */
- if (!ntfs_dir_emit(sbi, ni, e, name, ctx)) {
+ if (!ntfs_dir_emit(sbi, dir_ref, e, name, ctx)) {
/* ctx is full. */
return +1;
}
@@ -457,6 +461,9 @@
static int ntfs_readdir(struct file *file, struct dir_context *ctx)
{
const struct INDEX_ROOT *root;
+ struct INDEX_HDR *root_hdr = NULL;
+ struct MFT_REF dir_ref;
+ struct mft_inode *root_mi;
size_t bit;
int err = 0;
struct inode *dir = file_inode(file);
@@ -530,12 +537,15 @@
name = kmalloc(PATH_MAX, GFP_KERNEL);
if (!name)
return -ENOMEM;
+ if (pos < sbi->record_size) {
+ root_hdr = kmalloc(sbi->record_size, GFP_KERNEL);
+ if (!root_hdr) {
+ kfree(name);
+ return -ENOMEM;
+ }
+ }

- /*
- * Keep directory metadata stable for the whole walk. Loading subrecords
- * once is not enough if concurrent writeback can still compact ATTR_LIST
- * entries and free the record that ntfs_read_hdr() is currently walking.
- */
+ /* Serialize metadata lookup with writeback; snapshot root entries below. */
ni_lock(ni);

if (!ni->mi_loaded && ni->attr_list.size) {
@@ -550,7 +560,8 @@
ni->mi_loaded = true;
}

- root = indx_get_root(&ni->dir, ni, NULL, NULL);
+ mi_get_ref(&ni->mi, &dir_ref);
+ root = indx_get_root(&ni->dir, ni, NULL, &root_mi);
if (!root) {
err = -EINVAL;
goto out;
@@ -559,20 +570,25 @@
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);
+ size_t root_off = (u8 *)&root->ihdr - (u8 *)root_mi->mrec;
+ size_t root_size = max_t(size_t, sizeof(*root_hdr),
+ le32_to_cpu(root->ihdr.used));
+
+ /* Keep the root entries alive while dir_emit() may fault. */
+ if (root_off > sbi->record_size ||
+ root_size > sbi->record_size - root_off) {
+ err = -EINVAL;
+ goto out;
+ }

- /*
- * Add each name from root in 'ctx'.
- */
- err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx);
+ memcpy(root_hdr, &root->ihdr, root_size);
+
+ ni_unlock(ni);
+ err = ntfs_read_hdr(sbi, &dir_ref, root_hdr, 0, pos, name, ctx);
+ kfree(root_hdr);
+ root_hdr = NULL;
if (err)
- goto out;
+ goto out_free;

ni_lock(ni);
bit = 0;
@@ -612,18 +628,19 @@
/*
* Add each name from index in 'ctx'.
*/
- err = ntfs_read_hdr(sbi, ni, &node->index->ihdr,
+ err = ntfs_read_hdr(sbi, &dir_ref, &node->index->ihdr,
((u64)bit << index_bits) + sbi->record_size,
pos, name, ctx);
if (err)
- goto out;
+ goto out_free;

ni_lock(ni);
}

out:
ni_unlock(ni);
-
+out_free:
+ kfree(root_hdr);
kfree(name);
put_indx_node(node);