Skip to contentSkip to conversation

ntfs3: fix uninit memory after failed mi_read in mi_format_new

PATCH Applied Raphael Pinsonneault-Thibeault 2 participants 2 messages Oct 12, 2025 – Nov 20, 2025 NTFS3 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 ntfs3: fix uninit memory after failed mi_read in mi_format_new

Fix a KMSAN un-init bug found by syzkaller.

ntfs_get_bh() expects a buffer from sb_getblk(), that buffer may not be uptodate. We do not bring the buffer uptodate before setting it as uptodate. If the buffer were to not be uptodate, it could mean adding a buffer with un-init data to the mi record. Attempting to load that record will trigger KMSAN.

Avoid this by setting the buffer as uptodate, if it’s not already, by overwriting it.

Reported-by [email protected]
Fixes 4342306f0f0d5 ("fs/ntfs3: Add file operations and implementation")

---

v1:
https://lore.kernel.org/all/[email protected]/
Differences from v1:
In the previous version, I thought that mi_read() returning an error was a genuine reason to stop trying to format the record. That was wrong. If we find that mi_read() fails and that therefore we cannot reuse an old record, we should try to make a new one. It then follows that ntfs_get_bh() should provide an uptodate bh for the new record.

On testing:
I could not get xfstests-bld to work for ntfs3. Config nightmare. So I first tested with the syzkaller repro, then tested for regressions manually with a program that called all the syscall code paths touched by my change:
mkdir, fallocate (FALLOC_FL_COLLAPSE_RANGE), and
fallocate (FALLOC_FL_INSERT_RANGE).

 fs/ntfs3/fsntfs.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/ntfs3/fsntfs.c +8 -1
index 938d351ebac7..0c07502fdb0f 100644
@@ -1373,7 +1373,14 @@ int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
 				}
 				if (buffer_locked(bh))
 					__wait_on_buffer(bh);
-				set_buffer_uptodate(bh);
+
+				lock_buffer(bh);
+				if (!buffer_uptodate(bh))
+				{
+					memset(bh->b_data, 0, blocksize);
+					set_buffer_uptodate(bh);
+				}
+				unlock_buffer(bh);
 			} else {
 				bh = ntfs_bread(sb, block);
 				if (!bh) {
Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 47 lines · Raphael Pinsonneault-Thibeault

Fix a KMSAN un-init bug found by syzkaller.

ntfs_get_bh() expects a buffer from sb_getblk(), that buffer may not be uptodate. We do not bring the buffer uptodate before setting it as uptodate. If the buffer were to not be uptodate, it could mean adding a buffer with un-init data to the mi record. Attempting to load that record will trigger KMSAN.

Avoid this by setting the buffer as uptodate, if it’s not already, by overwriting it.

Reported-by [email protected]
Fixes 4342306f0f0d5 ("fs/ntfs3: Add file operations and implementation")

---

v1:
https://lore.kernel.org/all/[email protected]/
Differences from v1:
In the previous version, I thought that mi_read() returning an error was a genuine reason to stop trying to format the record. That was wrong. If we find that mi_read() fails and that therefore we cannot reuse an old record, we should try to make a new one. It then follows that ntfs_get_bh() should provide an uptodate bh for the new record.

On testing:
I could not get xfstests-bld to work for ntfs3. Config nightmare. So I first tested with the syzkaller repro, then tested for regressions manually with a program that called all the syscall code paths touched by my change:
mkdir, fallocate (FALLOC_FL_COLLAPSE_RANGE), and
fallocate (FALLOC_FL_INSERT_RANGE).

  fs/ntfs3/fsntfs.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
fs/ntfs3/fsntfs.c +1 -1
index 938d351ebac7..0c07502fdb0f 100644
@@ -1373,7 +1373,14 @@ int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo,
  				}
  				if (buffer_locked(bh))
  					__wait_on_buffer(bh);
-				set_buffer_uptodate(bh);
+

fs/ntfs3/fsntfs.c int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo, · line 1386

Quoted 10 lines
fs/ntfs3/fsntfs.c +7 -0
+				lock_buffer(bh);
+				if (!buffer_uptodate(bh))
+				{
+					memset(bh->b_data, 0, blocksize);
+					set_buffer_uptodate(bh);
+				}
+				unlock_buffer(bh);
  			} else {
  				bh = ntfs_bread(sb, block);
  				if (!bh) {

Applied to my tree, thanks for the patch.

Regards,
Konstantin