Skip to contentSkip to conversation

ntfs3: fix uninit memory after failed mi_read in mi_format_new

PATCH Superseded Raphael Pinsonneault-Thibeault 2 participants 3 messages Sep 25, 2025 – Sep 28, 2025 NTFS3 filesystem

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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

attr_insert_range() called from ntfs_fallocate() has 2 different code paths that trigger mi_read() (which calls ntfs_read_bh). If the first mi_read() -> ntfs_read_bh() fails with an IO error, it leaves an uninitialized buffer in the buffer cache.
The second mi_read() -> ntfs_read_bh() then uses that buffer, where we get KMSAN warning "uninit-value in ntfs_read_bh".

The fix is to check if mi_read failed in mi_format_new.

Reported-by [email protected]
Fixes 4342306f0f0d5 ("fs/ntfs3: Add file operations and implementation")
---
 fs/ntfs3/record.c | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)
fs/ntfs3/record.c +12 -7
index 714c7ecedca8..98d2e5517077 100644
@@ -431,13 +431,18 @@ int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno,
 		seq = rno;
 	} else if (rno >= sbi->mft.used) {
 		;
-	} else if (mi_read(mi, is_mft)) {
-		;
-	} else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
-		/* Record is reused. Update its sequence number. */
-		seq = le16_to_cpu(rec->seq) + 1;
-		if (!seq)
-			seq = 1;
+	} else {
+		err = mi_read(mi, is_mft);
+		if (err) {
+			return err;
+		}
+
+		if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
+			/* Record is reused. Update its sequence number. */
+			seq = le16_to_cpu(rec->seq) + 1;
+			if (!seq)
+				seq = 1;
+		}
 	}
 
 	memcpy(rec, sbi->new_rec, sbi->record_size);
Signature · 1 line
2.43.0
#1 lore raw
Quoted 9 lines · Raphael Pinsonneault-Thibeault

attr_insert_range() called from ntfs_fallocate() has 2 different code paths that trigger mi_read() (which calls ntfs_read_bh). If the first mi_read() -> ntfs_read_bh() fails with an IO error, it leaves an uninitialized buffer in the buffer cache.
The second mi_read() -> ntfs_read_bh() then uses that buffer, where we get KMSAN warning "uninit-value in ntfs_read_bh".

The fix is to check if mi_read failed in mi_format_new.

Did you do any testing for this patch?

David Hunter

#2 lore raw
In reply to Raphael Pinsonneault-Thibeault
Quoted 9 lines · Raphael Pinsonneault-Thibeault
fs/ntfs3/record.c +2 -7
-	} else if (mi_read(mi, is_mft)) {
-		;
-	} else if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
-		/* Record is reused. Update its sequence number. */
-		seq = le16_to_cpu(rec->seq) + 1;
-		if (!seq)
-			seq = 1;
+	} else {
+		err = mi_read(mi, is_mft);

fs/ntfs3/record.c int mi_format_new(struct mft_inode *mi, struct ntfs_sb_info *sbi, CLST rno, · line 445

Quoted 10 lines
fs/ntfs3/record.c +10 -0
+		if (err) {
+			return err;
+		}
+
+		if (rec->rhdr.sign == NTFS_FILE_SIGNATURE) {
+			/* Record is reused. Update its sequence number. */
+			seq = le16_to_cpu(rec->seq) + 1;
+			if (!seq)
+				seq = 1;
+		}

How was this tested?