From: Aibing Chen <chenaibing@cmss.chinamobile.com>
To: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Cc: ntfs3@lists.linux.dev, linux-kernel@vger.kernel.org
Subject: [PATCH] ntfs3: avoid freeing the primary MFT record during attribute list removal
Date: Sat, 14 Nov 2026 23:33:19 +0800 [thread overview]
Message-ID: <20261114153319.658-1-chenaibing@cmss.chinamobile.com> (raw)
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;
}
next reply other threads:[~2026-11-14 15:33 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-14 15:33 Aibing Chen [this message]
2026-12-03 15:45 ` Shuai Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261114153319.658-1-chenaibing@cmss.chinamobile.com \
--to=chenaibing@cmss.chinamobile.com \
--cc=almaz.alexandrovich@paragon-software.com \
--cc=linux-kernel@vger.kernel.org \
--cc=ntfs3@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®