mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] ntfs3: avoid freeing the primary MFT record during attribute list removal
@ 2026-11-14 15:33 Aibing Chen
  2026-12-03 15:45 ` Shuai Liu
  0 siblings, 1 reply; 2+ messages in thread
From: Aibing Chen @ 2026-11-14 15:33 UTC (permalink / raw)
  To: Konstantin Komarov; +Cc: ntfs3, linux-kernel

ni_try_remove_attr_list() keeps a backup copy of the primary MFT record
and, when moving an attribute out of a subrecord fails, restores it with
swap(mrec, ni->mi.mrec) and frees the buffer that ni->mi.mrec used to
point to.

ntfs_readdir() now drops ni_lock before walking the root index header
(commit 7a5aa6c45072), assuming that the header lives in the main record
whose lifetime is bound to the inode.  Background writeback does not
take i_rwsem: ni_write_inode() only needs ni_trylock(), which succeeds as
soon as ntfs_readdir() has released the lock.  If the attribute list is
dirty, writeback calls ni_try_remove_attr_list(), and the error path
above frees the record that the unlocked reader is walking:

  BUG: KASAN: slab-use-after-free in ntfs_read_hdr
  Read of size 2 at ...
  Freed by task ...:
   kfree
   ni_try_remove_attr_list
   ni_write_inode
   write_inode

Restore the primary record contents in place with memcpy() so that the
allocation stays alive for concurrent readers, and free only the backup
copy.

Reported-by: syzbot+7f14bc1da1f4da6518e5@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=7f14bc1da1f4da6518e5
Fixes: 7a5aa6c45072 ("ntfs3: release ni_lock before dir_emit in ntfs_readdir to fix deadlocks")
Signed-off-by: Aibing Chen <chenaibing@cmss.chinamobile.com>
---
 fs/ntfs3/frecord.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/fs/ntfs3/frecord.c b/fs/ntfs3/frecord.c
index 16ac847de..50c5d301b 100644
--- a/fs/ntfs3/frecord.c
+++ b/fs/ntfs3/frecord.c
@@ -755,8 +755,13 @@ static int ni_try_remove_attr_list(struct ntfs_inode *ni)
 	kfree(mrec);
 	return 0;
 out:
-	/* Restore primary record. */
-	swap(mrec, ni->mi.mrec);
+	/*
+	 * Restore the primary record contents in place.  ntfs_readdir() walks
+	 * the root index header after releasing ni_lock (see commit 7a5aa6c45072),
+	 * so a concurrent reader may still hold pointers into this buffer; swapping
+	 * it out and freeing it would leave that reader with freed memory.
+	 */
+	memcpy(ni->mi.mrec, mrec, sbi->record_size);
 	kfree(mrec);
 	return 0;
 }



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

* Re: [PATCH] ntfs3: avoid freeing the primary MFT record during attribute list removal
  2026-11-14 15:33 [PATCH] ntfs3: avoid freeing the primary MFT record during attribute list removal Aibing Chen
@ 2026-12-03 15:45 ` Shuai Liu
  0 siblings, 0 replies; 2+ messages in thread
From: Shuai Liu @ 2026-12-03 15:45 UTC (permalink / raw)
  To: chenaibing
  Cc: almaz.alexandrovich, ntfs3, syzbot+7f14bc1da1f4da6518e5, linux-kernel

Hi Aibing,

I verified this patch against the syzbot report [1] on top of master
commit d06ce6f1eb5ee, using the syzbot kernel configuration
(CONFIG_KASAN=y with KASAN_INLINE, CONFIG_PROVE_LOCKING=y) and the
syzbot C reproducer running in QEMU.

Baseline (unpatched), reproduced twice:

  BUG: KASAN: slab-use-after-free in ntfs_read_hdr+0xa1f/0xb70
  Freed by task ...:
   kfree
   ni_write_inode            <- inlined ni_try_remove_attr_list()
   __writeback_single_inode
   write_inode_now
   ntfs_file_fsync

which matches the syzbot report: the free comes from the error path of
ni_try_remove_attr_list() during writeback while ntfs_readdir() walks
the root index header with ni_lock already dropped (7a5aa6c45072).

With this patch applied, two consecutive runs of the reproducer are
clean: the error path still executes (restore path reached), the
primary record buffer stays alive, and no KASAN report appears.

Thanks,
Shuai Liu

[1] https://syzkaller.appspot.com/bug?extid=7f14bc1da1f4da6518e5


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

end of thread, other threads:[~2026-12-03 15:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-14 15:33 [PATCH] ntfs3: avoid freeing the primary MFT record during attribute list removal Aibing Chen
2026-12-03 15:45 ` Shuai Liu

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®