ext4_htree_fill_tree() returns '.' and '..' of an indexed directory from its root block, and finds '..' by stepping over '.' by its rec_len: de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; de = ext4_next_entry(de, dir->i_sb->s_blocksize); tmp_str.name = de->name; tmp_str.len = de->name_len; err = ext4_htree_store_dirent(dir_file, 2, 0, de, &tmp_str); dx_probe(), which reads the root just before, checks the dx_root_info and the index entries but not the two directory entries. Without metadata_csum nothing else does, and with it the root's checksum is found through '.'.rec_len, which may be 12 or the block size. The block size puts '..' just past the block: its name_len, whatever byte lies there, becomes the length ext4_htree_store_dirent() copies, from the memory after the block, into the name getdents64() returns: BUG: KASAN: slab-out-of-bounds in ext4_htree_store_dirent (fs/ext4/dir.c:509) Read of size 173 at addr ffff888019ff6008 by task syz-executor/11682 __asan_memcpy (mm/kasan/shadow.c:105) ext4_htree_store_dirent (fs/ext4/dir.c:509) ext4_htree_fill_tree (fs/ext4/namei.c:1060) ext4_dx_readdir (fs/ext4/dir.c:620) ext4_readdir (fs/ext4/dir.c:149) iterate_dir (fs/readdir.c:114) __do_sys_getdents64 (fs/readdir.c:404) KASAN reports this only when what follows the block is poisoned. When it is not, or once KASAN has stopped reporting, the bytes reach user space: on our upstream base '..' came back as up to 255 bytes holding kernel heap pointers and kernel text addresses, also when the directory was read by an unprivileged user. Check '.' and then '..' with ext4_check_dir_entry() before using '..', as htree_dirblock_to_tree() does for the leaf entries, and fail the readdir with -EFSCORRUPTED if either is bad. A '.' that passes ends at least 12 bytes before the end of the block, so '..' starts inside it, and a '..' that passes holds its name. The root the kernel and e2fsck write ('.' of 12 bytes, '..' to the end of the block) passes. We found this with a syzkaller-based fuzzing harness driven by an AI tool. Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3") Closes: https://lore.kernel.org/r/ext4-htree-dotdot.20261206@iiitd.ac.in/ Cc: stable@vger.kernel.org Signed-off-by: Kapil Vaswani Signed-off-by: Piyus Kedia --- Tested on ksmbd-for-next (d70570cf8003), whose fs/ext4 is identical to mainline master (9437ee8b6cf6) and to ext4/dev (aceadbccd428), with KASAN, the same config before and after. A 4 KiB-block filesystem made fresh for every run, an indexed directory of 200 files, the root's '.'.rec_len set to 0x1000, then getdents64(): before 29 readdirs in 6 boots (the fuzzer's program once): '..' never came back as '..' - kernel memory, zeros or EIO; KASAN reported in 4 of the 6 boots after 29 readdirs in 3 boots, as root, as uid 65534 and with metadata_csum: no report, EUCLEAN and "bad entry in directory: '.' directory cannot be the last in data block" control '.'.rec_len as written: 202 entries on both kernels fs/ext4/namei.c | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c index b2753daad49f..5ca84f600c9b 100644 --- a/fs/ext4/namei.c +++ b/fs/ext4/namei.c @@ -1053,8 +1053,24 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, count++; } if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { - de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; + struct buffer_head *bh = frames[0].bh; + + /* + * '..' is found by stepping over '.' by its rec_len, which + * dx_probe() does not look at: check both entries first. + */ + de = (struct ext4_dir_entry_2 *)bh->b_data; + if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, + bh->b_size, 0)) { + err = -EFSCORRUPTED; + goto errout; + } de = ext4_next_entry(de, dir->i_sb->s_blocksize); + if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, + bh->b_size, (char *)de - bh->b_data)) { + err = -EFSCORRUPTED; + goto errout; + } tmp_str.name = de->name; tmp_str.len = de->name_len; err = ext4_htree_store_dirent(dir_file, 2, 0, -- 2.34.1