Skip to contentSkip to conversation

ntfs: validate the full MST update sequence array

PATCH New DaeMyung Kang 1 participant 1 message May 8

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntfs: validate the full MST update sequence array

The NTFS update sequence array contains the update sequence number itself followed by one fixup entry for each protected sector. The current MST validation subtracts one from usa_count before checking the array bounds, so it only accounts for the fixup entries and not the leading update sequence number.

A malformed record can therefore pass validation with the last fixup entry outside the record buffer. post_read_mst_fixup() then reads past the supplied buffer, while pre_write_mst_fixup() can write past it. The USA also must end before the last word of the first 512-byte sector, otherwise applying the fixups can overlap the sector trailer that the array is supposed to protect.
This matches the documented NTFS MULTI_SECTOR_HEADER layout, where the update sequence array is required to end before the last USHORT in the first sector, and the sequence number stride is 512 bytes.

Validate the on-disk usa_count before converting it to a fixup count, check the full USA size, and share this validation between the MST helpers. Pass the record size to post_write_mst_fixup() as well so error paths do not restore data from a malformed update sequence array.

A small userspace ASAN reproducer with size=512, usa_ofs=510 and usa_count=2 triggers a heap-buffer-overflow with the old validation. With this change the same malformed record is rejected before the fixup array is dereferenced.

Fixes d3ad708fecaa ("ntfs: Initial commit")
---
 fs/ntfs/index.c |  3 ++-
 fs/ntfs/mst.c   | 68 ++++++++++++++++++++++++++++++++++++++---------------------------
 fs/ntfs/ntfs.h  |  2 +-
 3 files changed, 43 insertions(+), 30 deletions(-)
fs/ntfs/index.c +2 -1
index 413a1425d640..c2289550bc8f 100644
@@ -178,7 +178,8 @@ int ntfs_icx_ib_sync_write(struct ntfs_index_context *icx)
 		icx->ib = NULL;
 		icx->ib_dirty = false;
 	} else {
-		post_write_mst_fixup((struct ntfs_record *)icx->ib);
+		post_write_mst_fixup((struct ntfs_record *)icx->ib,
+				icx->block_size);
 		icx->sync_write = false;
 	}
 
fs/ntfs/mst.c +40 -28

124 lines not on this page · raw message

fs/ntfs/ntfs.h +1 -1
index e301c68c780b..445101c06ef9 100644
@@ -230,7 +230,7 @@ int ntfs_write_volume_label(struct ntfs_volume *vol, char *label);
 /* From fs/ntfs/mst.c */
 int post_read_mst_fixup(struct ntfs_record *b, const u32 size);
 int pre_write_mst_fixup(struct ntfs_record *b, const u32 size);
-void post_write_mst_fixup(struct ntfs_record *b);
+void post_write_mst_fixup(struct ntfs_record *b, const u32 size);
 
 /* From fs/ntfs/unistr.c */
 bool ntfs_are_names_equal(const __le16 *s1, size_t s1_len,