ntfs: validate index root entries_offset in ntfs_readdir()
Subsystems (2)
PATCH 1/1 ntfs: validate index root entries_offset in ntfs_readdir()
ntfs_readdir() computes the first index-root entry directly from the on-disk entries_offset and dereferences next->flags without any bounds check. At inode-read time only index_length is validated against the attribute value; entries_offset is left unchecked. A crafted directory with a valid index_length but an out-of-range entries_offset therefore causes an out-of-bounds read, reachable via getdents64() on a mounted image (a read-only mount is sufficient), and can feed a bogus pointer into ntfs_index_walk_down().
The sibling read paths already guard this: ntfs_lookup() bounds-checks each entry before dereferencing, and ntfs_index_block_inconsistent() rejects entries_offset < sizeof(index_header) for index blocks. Apply the equivalent check to the index-root path in ntfs_readdir().
Found by code inspection; not yet reproduced at runtime under KASAN.
--- fs/ntfs/dir.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
fs/ntfs/dir.c +15 -1
@@ -759,6 +759,7 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)u8 *name;struct index_root *ir;struct index_entry *next =NULL ; +u8 *index_end;struct ntfs_file_private *private =NULL ;int err =0 ;loff_t ie_pos =2 ;/* initialize it with dot and dotdot size */
@@ -893,9 +894,22 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)else ictx->vcn_size_bits =NTFS_BLOCK_SIZE_BITS ; -/* The first index entry. */ +/* +* entries_offset comes straight from the on-disk index root and is +* not otherwise validated on this path (only index_length is checked +* at inode-read time). Bound the first entry before dereferencing it, +* mirroring ntfs_lookup() and ntfs_index_block_inconsistent(). +*/ + index_end = (u8 *)&ir->index +le32_to_cpu (ir->index.index_length); next = (struct index_entry *)((u8 *)&ir->index +le32_to_cpu (ir->index.entries_offset)); +if (le32_to_cpu (ir->index.entries_offset) <sizeof (struct index_header ) || + (u8 *)next +sizeof (struct index_entry_header ) > index_end) { +ntfs_error (sb,"Corrupt index root entries_offset in inode %llu" , + ndir->mft_no); + err = -EIO ; + goto out; + }if (next->flags &INDEX_ENTRY_NODE ) { ictx->ia_ni =ntfs_ia_open (ictx, ictx->idx_ni);
Signature · 1 line
2.34.1
Hyunchul Lee
maintainerHi Hem,
2026년 6월 2일 (화) 오후 2:02, Hem Parekh <[email protected]>님이 작성:
Quoted 46 lines
ntfs_readdir()computes the first index-root entry directly from the on-diskentries_offsetand dereferencesnext->flagswithout any bounds check. At inode-read time onlyindex_lengthis validated against the attribute value;entries_offsetis left unchecked. A crafted directory with a validindex_lengthbut an out-of-rangeentries_offsettherefore causes an out-of-bounds read, reachable viagetdents64()on a mounted image (a read-only mount is sufficient), and can feed a bogus pointer intontfs_index_walk_down().The sibling read paths already guard this:
ntfs_lookup()bounds-checks each entry before dereferencing, andntfs_index_block_inconsistent()rejectsentries_offset<sizeof(index_header)for index blocks. Apply the equivalent check to the index-root path inntfs_readdir().Found by code inspection; not yet reproduced at runtime under KASAN.
Fixes 0a8ac0c1fa0b ("ntfs: update directory operations")Signed-off-by Hem Parekh <[email protected]>--- fs/ntfs/dir.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)fs/ntfs/dir.c +7
-1@@ -759,6 +759,7 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)u8 *name;struct index_root *ir;struct index_entry *next =NULL ; +u8 *index_end;struct ntfs_file_private *private =NULL ;int err =0 ;loff_t ie_pos =2 ;/* initialize it with dot and dotdot size */ @@ -893,9 +894,22 @@ static int ntfs_readdir(struct file *file, struct dir_context *actor)else ictx->vcn_size_bits =NTFS_BLOCK_SIZE_BITS ; -/* The first index entry. */ +/* +* entries_offset comes straight from the on-disk index root and is +* not otherwise validated on this path (only index_length is checked +* at inode-read time). Bound the first entry before dereferencing it, +* mirroring ntfs_lookup() and ntfs_index_block_inconsistent(). +*/
fs/ntfs/dir.c static int ntfs_readdir(struct file *file, struct dir_context *actor) · line 912
Quoted 10 lines
fs/ntfs/dir.c +8
-0+ index_end = (u8 *)&ir->index +le32_to_cpu (ir->index.index_length); next = (struct index_entry *)((u8 *)&ir->index +le32_to_cpu (ir->index.entries_offset)); +if (le32_to_cpu (ir->index.entries_offset) <sizeof (struct index_header ) || + (u8 *)next +sizeof (struct index_entry_header ) > index_end) { +ntfs_error (sb,"Corrupt index root entries_offset in inode %llu" , + ndir->mft_no); + err = -EIO ; + goto out; + }
There is a patch which validates the same thing.
https://lore.kernel.org/all/[email protected]/
Quoted 6 lines
fs/ntfs/dir.c +0
-1if (next->flags &INDEX_ENTRY_NODE ) { ictx->ia_ni =ntfs_ia_open (ictx, ictx->idx_ni); --2.34.1
Signature · 2 lines
Thanks, Hyunchul