From 84f4cc8d41744f103d6fa7e87739b2be5a2d27b0 Mon Sep 17 00:00:00 2001 From: Xingyuan Mo Date: Thu, 5 Nov 2026 15:34:04 +0800 Subject: [PATCH 1/5] ntfs: fix out-of-bounds write race in NTFS_IOC_LIST_STREAMS NTFS_IOC_LIST_STREAMS walks the base inode's MFT attributes twice: a first pass sizes the output buffer, a second pass fills it, both times filtering out streams that ntfs_stream_unlinked() reports as unlinked. That filter is the culprit: it treats a stream inode that is no longer in the inode hash as live, which is precisely the state a stream being deleted ends up in. When a base file is deleted while one of its named streams is still open, ntfs_cleanup_deleted_inode() sets NInoStreamUnlinked on the stream inode and removes it from the inode hash after dropping the MFT record lock. Nothing serializes this against the ioctl's two passes, so if the flag and the unhash land between them, the first pass excludes the stream while the second pass includes it, and the ioctl writes an entry the allocation never accounted for. The per-entry bounds check cannot catch this divergence, because "offset > required - entry_size" underflows when entry_size exceeds required. With a 1-character stream sizing the buffer at 48 bytes and a 250-character stream surfacing only in the second pass, a 544-byte entry is written into the 48-byte kmalloc-64 allocation. Close the hole in two steps: - Reject base inodes that are being deleted before walking their attributes, like the other named stream entry points in this file already do. This also stops the listing from reporting streams of a deleted base file as live, which is reproducible even without the race. - Make the bounds check underflow-safe so any remaining divergence between the passes can only produce -EOVERFLOW, never an out-of-bounds write. KASAN report from the reproducer (timestamps stripped): BUG: KASAN: slab-out-of-bounds in ntfs_ioctl_list_streams+0x7a9/0xa00 Write of size 500 at addr ffff88800b1931a8 by task ntfs_ads_race/305 CPU: 2 UID: 0 PID: 305 Comm: ntfs_ads_race Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: dump_stack_lvl+0x73/0xb0 print_report+0xd1/0x610 kasan_report+0xff/0x130 kasan_check_range+0x10c/0x1c0 __asan_memcpy+0x3f/0x70 ntfs_ioctl_list_streams+0x7a9/0xa00 ntfs_ioctl+0x3b6/0x420 __x64_sys_ioctl+0xd1/0x120 x64_sys_call+0x2a5/0x2220 do_syscall_64+0x109/0x5e0 entry_SYSCALL_64_after_hwframe+0x77/0x7f Allocated by task 305: kasan_save_stack+0x3d/0x60 kasan_save_track+0x18/0x40 kasan_save_alloc_info+0x3b/0x50 __kasan_kmalloc+0x9c/0xa0 __kvmalloc_node_noprof+0x2dd/0x800 ntfs_ioctl_list_streams+0x57f/0xa00 ntfs_ioctl+0x3b6/0x420 __x64_sys_ioctl+0xd1/0x120 x64_sys_call+0x2a5/0x2220 do_syscall_64+0x109/0x5e0 entry_SYSCALL_64_after_hwframe+0x77/0x7f The buggy address belongs to the object at ffff88800b193180 which belongs to the cache kmalloc-64 of size 64 The buggy address is located 40 bytes inside of allocated 48-byte region [ffff88800b193180, ffff88800b1931b0) Fixes: 48996054b706 ("ntfs: add named stream ioctls support") Signed-off-by: Xingyuan Mo Reviewed-by: Baolin Liu Signed-off-by: Namjae Jeon --- fs/ntfs/named_stream.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/fs/ntfs/named_stream.c b/fs/ntfs/named_stream.c index 95a3c05f9821..70c589207713 100644 --- a/fs/ntfs/named_stream.c +++ b/fs/ntfs/named_stream.c @@ -1632,6 +1632,10 @@ int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg) ubuf = (void __user *)arg + sizeof(hdr); mutex_lock(&ni->mrec_lock); + if (NInoBeingDeleted(ni) || !inode->i_nlink) { + mutex_unlock(&ni->mrec_lock); + return -ENOENT; + } actx = ntfs_attr_get_search_ctx(ni, NULL); if (!actx) { mutex_unlock(&ni->mrec_lock); @@ -1731,7 +1735,7 @@ int ntfs_ioctl_list_streams(struct file *filp, unsigned long arg) } entry_size = ALIGN(name_offset + name_len, 8); - if (offset > required - entry_size) { + if (offset > required || entry_size > required - offset) { ret = -EOVERFLOW; kfree(sn); sn = NULL; From 5e7143476219eac7cab7bdd341db68dab1649737 Mon Sep 17 00:00:00 2001 From: Wahid Khan Date: Fri, 6 Nov 2026 16:57:52 +0530 Subject: [PATCH 2/5] ntfs: check the file name against the key length of an index entry ntfs_index_entry_inconsistent() checks that the file name of a $FILE_NAME key fits in the index entry. It does not check that the name fits in the key, which is what key_length describes. Code that works on the key alone relies on that. ntfs_readdir() copies key_length bytes of the key of the next entry to resume the enumeration, and later uses the copy to search the index. If file_name_length is larger than the key holds, the name comparison reads past the copy: BUG: KASAN: slab-out-of-bounds in ntfs_file_compare_values+0x2d1/0x4b0 Read of size 2 at addr ffff88800a4ef2ce by task init/1 ntfs_file_compare_values ntfs_collate ntfs_ie_lookup ntfs_index_lookup ntfs_readdir iterate_dir Allocated by task 1: kmemdup_noprof ntfs_readdir The buggy address is located 0 bytes to the right of allocated 78-byte region Two bytes of an image from mkntfs are enough: in an entry whose name has six characters (key_length 78), change file_name_length from 6 to 7, so the key claims 80 bytes. The entry itself is 96 bytes, so the existing check lets it pass. The next readdir of the directory reads the extra bytes. Check the name against key_length, and check that the key is large enough to hold file_name_length in the first place. Fixes: 0a8ac0c1fa0b ("ntfs: update directory operations") Cc: stable@vger.kernel.org # v7.1+ Signed-off-by: Wahid Khan Signed-off-by: Namjae Jeon --- fs/ntfs/index.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c index 322cb371dbb3..1abca298aed9 100644 --- a/fs/ntfs/index.c +++ b/fs/ntfs/index.c @@ -43,9 +43,11 @@ static int ntfs_index_entry_inconsistent(const struct ntfs_volume *vol, } else { if (collation_rule == COLLATION_FILE_NAME) { - if ((offsetof(struct index_entry, key.file_name.file_name) + - ie->key.file_name.file_name_length * sizeof(__le16)) > - le16_to_cpu(ie->length)) { + if (le16_to_cpu(ie->key_length) < + offsetof(struct file_name_attr, file_name) || + (offsetof(struct file_name_attr, file_name) + + ie->key.file_name.file_name_length * sizeof(__le16)) > + le16_to_cpu(ie->key_length)) { ntfs_error(vol->sb, "File name overflow from index entry in inode %lld\n", (long long)inum); From ce5cdda498a9479b32ba1c7d68b1d97f3c840354 Mon Sep 17 00:00:00 2001 From: Namjae Jeon Date: Fri, 6 Nov 2026 21:46:10 +0900 Subject: [PATCH 3/5] ntfs: silence kmalloc/kvfree mismatch warning The temporary runlist is allocated with kmalloc(). Free it with kfree() on merge failure to silence the Coccinelle warning. Fixes: b041ca562526 ("ntfs: update iomap and address space operations") Reported-by: kernel test robot Closes: https://lore.kernel.org/r/202611060717.0Na5FqQR-lkp@intel.com/ Reported-by: Julia Lawall Signed-off-by: Namjae Jeon --- fs/ntfs/iomap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fs/ntfs/iomap.c b/fs/ntfs/iomap.c index 6e51cce3aeba..0b7d7b861485 100644 --- a/fs/ntfs/iomap.c +++ b/fs/ntfs/iomap.c @@ -499,7 +499,7 @@ remap_rl: ntfs_error(vol->sb, "Failed to merge runlists"); up_write(&ni->runlist.lock); mutex_unlock(&mrec_ni->mrec_lock); - kvfree(rlc); + kfree(rlc); return PTR_ERR(rl); } From 0ea30dd6eeabc09235d5c2cdb98ca4e0d593963f Mon Sep 17 00:00:00 2001 From: Wahid Khan Date: Fri, 6 Nov 2026 18:15:51 +0530 Subject: [PATCH 4/5] ntfs: do not read an index block outside $INDEX_ALLOCATION A directory whose $INDEX_ROOT has a child node but whose $INDEX_ALLOCATION is missing or damaged gets an empty index inode from ntfs_read_locked_index_inode(): size 0 and not non-resident. ntfs_lookup_inode_by_name() still follows the child node and reads a folio from that inode while it holds the mft record lock of the directory. Since the resident iomap read started to take base_ni->mrec_lock, that read waits for a lock the caller already holds: INFO: task init:1 is blocked on a mutex likely owned by task init:1. __mutex_lock __ntfs_read_iomap_begin ntfs_read_iomap_next iomap_read_folio ntfs_read_folio ntfs_lookup_inode_by_name ntfs_lookup The task never returns, and neither does anything else that needs the directory's mft record. It takes one corrupted byte on an image from mkntfs: change the first byte of the type of the $INDEX_ALLOCATION attribute header of a directory with index blocks from 0xa0 to 0x00. The first lookup in that directory hangs. Check that the index inode is non-resident and that the vcn lies inside it before every index block read, and fail with -EIO otherwise. This also rejects a negative vcn, which was only checked on later descents. Fixes: d9e00c457d4a ("ntfs: serialize resident iomap reads with mrec_lock") Cc: stable@vger.kernel.org # v7.3+ Signed-off-by: Wahid Khan Signed-off-by: Namjae Jeon --- fs/ntfs/dir.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/fs/ntfs/dir.c b/fs/ntfs/dir.c index fce10baa2b93..9fb5bb3d3512 100644 --- a/fs/ntfs/dir.c +++ b/fs/ntfs/dir.c @@ -308,6 +308,20 @@ found_it: ia_mapping = ia_vi->i_mapping; descend_into_child_node: + /* + * The child node has to be inside $INDEX_ALLOCATION. A directory that + * lacks the attribute gets an empty index inode that is not + * non-resident, and reading a folio from it locks the mft record that + * is already locked here. + */ + if (!NInoNonResident(NTFS_I(ia_vi)) || vcn < 0 || + (u64)vcn >= (u64)i_size_read(ia_vi) >> dir_ni->itype.index.vcn_size_bits) { + ntfs_error(sb, + "Child node vcn %lld is outside $INDEX_ALLOCATION in directory inode 0x%llx.", + vcn, dir_ni->mft_no); + err = -EIO; + goto err_out; + } /* * Convert vcn to index into the index allocation attribute in units * of PAGE_SIZE and map the page cache page, reading it from From bada7bbb11aaa6feba8904cf38ff833453f5b5d9 Mon Sep 17 00:00:00 2001 From: Wahid Khan Date: Fri, 6 Nov 2026 18:15:52 +0530 Subject: [PATCH 5/5] ntfs: open the index allocation before reading or writing an index block ntfs_ib_write() and ntfs_ib_read() use icx->ia_ni without checking it. ntfs_ir_reparent() only opens the index allocation through ntfs_ia_add() when the root is a small index. For a root that is already marked as a large index it calls ntfs_ib_write() directly. The lookup does not open the index allocation either when it ends in the root. If the last entry of the root has no child node, ia_ni is still NULL when the root has to be moved into an index block, and the write dereferences it: Oops: general protection fault, KASAN: null-ptr-deref in range [0x0000000000000128-0x000000000000012f] RIP: 0010:ntfs_inode_attr_pwrite+0x52/0x1600 ntfs_ir_make_space ntfs_ie_add ntfs_index_add_filename __ntfs_link ntfs_link vfs_link A single byte of an image from mkntfs is enough. In a directory that has one empty index block, clear INDEX_ENTRY_NODE (flags 3 -> 2) of the last entry of the $INDEX_ROOT and link() a file into the directory. Commit 9365c4a1357e ("ntfs: fix NULL dereference on index allocation open failure") covers the case where ntfs_ia_open() fails in the lookup. Here it was never tried. Open the index allocation on demand in both helpers and fail with -EIO if that does not work. ntfs_ia_open() also succeeds for a directory that has no $INDEX_ALLOCATION attribute. The inode it returns is then an empty resident stub that does not describe an index allocation, so reject that case too. Fixes: 0a8ac0c1fa0b ("ntfs: update directory operations") Cc: stable@vger.kernel.org # v7.1+ Signed-off-by: Wahid Khan Signed-off-by: Namjae Jeon --- fs/ntfs/index.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/fs/ntfs/index.c b/fs/ntfs/index.c index 1abca298aed9..b4462f760e3b 100644 --- a/fs/ntfs/index.c +++ b/fs/ntfs/index.c @@ -101,12 +101,30 @@ static s64 ntfs_ib_pos_to_vcn(struct ntfs_index_context *icx, s64 pos) return pos >> icx->vcn_size_bits; } +/* Open the index allocation inode if no earlier step of the operation did. */ +static int ntfs_icx_ia_ensure(struct ntfs_index_context *icx) +{ + if (!icx->ia_ni) { + icx->ia_ni = ntfs_ia_open(icx, icx->idx_ni); + if (!icx->ia_ni) + return -EIO; + } + /* Without $INDEX_ALLOCATION the index inode is an empty resident stub. */ + if (!NInoNonResident(icx->ia_ni)) + return -EIO; + return 0; +} + static int ntfs_ib_write(struct ntfs_index_context *icx, struct index_block *ib) { s64 ret, vcn = le64_to_cpu(ib->index_block_vcn); ntfs_debug("vcn: %lld\n", vcn); + ret = ntfs_icx_ia_ensure(icx); + if (ret) + return ret; + ret = pre_write_mst_fixup((struct ntfs_record *)ib, icx->block_size); if (ret) return -EIO; @@ -807,6 +825,10 @@ static int ntfs_ib_read(struct ntfs_index_context *icx, s64 vcn, struct index_bl ntfs_debug("vcn: %lld\n", vcn); + ret = ntfs_icx_ia_ensure(icx); + if (ret) + return ret; + pos = ntfs_ib_vcn_to_pos(icx, vcn); ret = ntfs_inode_attr_pread(VFS_I(icx->ia_ni), pos, icx->block_size, (u8 *)dst);