Skip to contentSkip to conversation

ntfs3: fix out-of-bounds read in ntfs_read_mft()

PATCH In review Weiming Shi 2 participants 2 messages Jun 25 – Sep 16 NTFS3 filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ntfs3: fix out-of-bounds read in ntfs_read_mft()

ntfs_read_mft() walks the attributes of an MFT record and, for each FILE_NAME attribute, compares the resident on-disk name with the name being looked up. The only size check before the compare is rsize >= SIZEOF_ATTRIBUTE_FILENAME, which covers the fixed header up to and including name_len, but not the variable-length name that follows it.

name_len comes straight from disk and is not validated against the resident data_size. A crafted image can set name_len large while keeping data_size at the header size, so ntfs_cmp_names_cpu() reads name_len le16 characters past the end of the MFT record buffer:

  BUG: KASAN: slab-out-of-bounds in ntfs_cmp_names_cpu (fs/ntfs3/upcase.c:84)
  Read of size 2 at addr ffff888031879c00 by task a.out
   ntfs_cmp_names_cpu (fs/ntfs3/upcase.c:84)
   ntfs_iget5 (fs/ntfs3/inode.c:209)
   dir_search_u (fs/ntfs3/dir.c:265)
   ntfs_lookup (fs/ntfs3/namei.c:85)
   __lookup_slow (fs/namei.c:1915)
   lookup_slow (fs/namei.c:1932)
   path_lookupat (fs/namei.c:2278)
   vfs_statx (fs/stat.c)
   vfs_fstatat (fs/stat.c)
   __do_sys_newfstatat (fs/stat.c)

Allocated by task:
mi_init (fs/ntfs3/record.c)
ntfs_iget5 (fs/ntfs3/inode.c)
dir_search_u (fs/ntfs3/dir.c)
ntfs_lookup (fs/ntfs3/namei.c)

Check that the name fits in the resident data before using it.

Fixes 82cae269cfa9 ("fs/ntfs3: Add initialization of super block")
Assisted-by Claude:claude-opus-4-8
---
 fs/ntfs3/inode.c | 5 +++++
 1 file changed, 5 insertions(+)
fs/ntfs3/inode.c +5 -0
index c43101cc064d..c41c7231bb87 100644
@@ -201,6 +201,11 @@ static struct inode *ntfs_read_mft(struct inode *inode,
 
 		names += 1;
 		fname = Add2Ptr(attr, roff);
+
+		/* Make sure the full name fits in the resident data. */
+		if (rsize < fname_full_size(fname))
+			goto out;
+
 		if (fname->type == FILE_NAME_DOS)
 			goto next_attr;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 46 lines · Weiming Shi

ntfs_read_mft() walks the attributes of an MFT record and, for each FILE_NAME attribute, compares the resident on-disk name with the name being looked up. The only size check before the compare is rsize >= SIZEOF_ATTRIBUTE_FILENAME, which covers the fixed header up to and including name_len, but not the variable-length name that follows it.

name_len comes straight from disk and is not validated against the resident data_size. A crafted image can set name_len large while keeping data_size at the header size, so ntfs_cmp_names_cpu() reads name_len le16 characters past the end of the MFT record buffer:

   BUG: KASAN: slab-out-of-bounds in ntfs_cmp_names_cpu (fs/ntfs3/upcase.c:84)
   Read of size 2 at addr ffff888031879c00 by task a.out
    ntfs_cmp_names_cpu (fs/ntfs3/upcase.c:84)
    ntfs_iget5 (fs/ntfs3/inode.c:209)
    dir_search_u (fs/ntfs3/dir.c:265)
    ntfs_lookup (fs/ntfs3/namei.c:85)
    __lookup_slow (fs/namei.c:1915)
    lookup_slow (fs/namei.c:1932)
    path_lookupat (fs/namei.c:2278)
    vfs_statx (fs/stat.c)
    vfs_fstatat (fs/stat.c)
    __do_sys_newfstatat (fs/stat.c)
   Allocated by task:
    mi_init (fs/ntfs3/record.c)
    ntfs_iget5 (fs/ntfs3/inode.c)
    dir_search_u (fs/ntfs3/dir.c)
    ntfs_lookup (fs/ntfs3/namei.c)

Check that the name fits in the resident data before using it.

Fixes 82cae269cfa9 ("fs/ntfs3: Add initialization of super block")
Assisted-by Claude:claude-opus-4-8
---
  fs/ntfs3/inode.c | 5 +++++
  1 file changed, 5 insertions(+)
fs/ntfs3/inode.c +0 -0
index c43101cc064d..c41c7231bb87 100644
@@ -201,6 +201,11 @@ static struct inode *ntfs_read_mft(struct inode *inode,
 

fs/ntfs3/inode.c static struct inode *ntfs_read_mft(struct inode *inode, · line 211

Quoted 10 lines
fs/ntfs3/inode.c +5 -0
  		names += 1;
  		fname = Add2Ptr(attr, roff);
+
+		/* Make sure the full name fits in the resident data. */
+		if (rsize < fname_full_size(fname))
+			goto out;
+
  		if (fname->type == FILE_NAME_DOS)
  			goto next_attr;
 

Hello,

Sorry for the delay.
Your patch was applied, thank you.

Regards,
Konstantin