[PATCH] ext4: bound dot and dotdot of dx_root by the dirent tail
From: Piyus Kedia
Date: Sun Dec 06 2026 - 07:59:38 EST
make_indexed_dir() moves out of block 0 everything between the end of
the '..' entry, which it takes at its fixed offset, and the start of
the dirent tail:
fde = &root->dotdot;
de = (struct ext4_dir_entry_2 *)((char *)fde +
ext4_rec_len_from_disk(fde->rec_len, blocksize));
len = ((char *) root) + (blocksize - csum_size) - (char *) de;
...
memcpy(data2, de, len);
memset(de, 0, len); /* wipe old data */
ext4_check_dx_root() only rejects a '..' that reaches the end of the
block. With metadata_csum the entries end csum_size (12) bytes
earlier, so a '..' that ends inside the tail passes, and len, an
unsigned int, wraps: '..'.rec_len 1004 in a 1 KiB block gives
0xfffffffc. The walk that runs just before, ext4_find_dest_de() from
add_dirent_to_buf(), checks each entry against the tail, but it steps
from '.' by '.'.rec_len, which ext4_check_dx_root() also bounds only by
the block end, and a '.' of rec_len 24 steps over '..'. A crafted
image with such a block 0, and a create whose name does not fit:
BUG: KASAN: slab-out-of-bounds in make_indexed_dir (fs/ext4/namei.c:2114)
Read of size 4294967292 at addr ffff888045ea03f8 by task syz-executor/9604
__asan_memcpy (mm/kasan/shadow.c:105)
make_indexed_dir (fs/ext4/namei.c:2114)
__ext4_add_entry (fs/ext4/namei.c:2283)
ext4_add_nondir (fs/ext4/namei.c:2320 fs/ext4/namei.c:2643)
ext4_create (fs/ext4/namei.c:2689)
KASAN skips the copy it reports, but reports only once, and the
memset() is then carried out: it zeroes memory from the directory
block onwards until the kernel crashes. Without KASAN the memcpy()
runs first, for the same length, into the new block's buffer.
The bound came with the "invalid rec_len for '..'" check in
make_indexed_dir(), before there was a dirent tail. The commit named
below made len stop at the tail and left the check at the block end;
commit 50ea741def58 ("ext4: check dot and dotdot of dx_root before
making dir indexed") moved the check into ext4_check_dx_root() as it
was.
Bound '.' and '..' by the start of the tail when the filesystem has
metadata_csum, as the walk does; len is then at least 1. This also
rejects a '..' that ends exactly at the tail (len 0), which otherwise
ends in the WARN_ON_ONCE(split == 0) in do_split(). A directory the
kernel wrote does not get to make_indexed_dir() with either: its '.' is
12 bytes long, so the walk checks '..', and a '..' reaching the tail
would have had room for the new name.
We found this with a syzkaller-based fuzzing harness driven by an AI tool.
Fixes: b0336e8d2108 ("ext4: calculate and verify checksums of directory leaf blocks")
Closes: https://lore.kernel.org/r/ext4-dx-root-dotdot.20261206@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
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 1 KiB-block filesystem
with metadata_csum, made fresh for every run, a one-block directory
with '.'.rec_len 24 and the '..'.rec_len below in its block 0, then a
create that does not fit:
before '..' 1004: KASAN read of 4294967292 bytes in
make_indexed_dir() (:2114) in 6 of 6 boots (the fuzzer's
program once); with KASAN's single report the memset()
then ran and the kernel panicked (4 of 4); '..' 1000:
WARNING in do_split() (1 of 1)
after no report and no warning in 3 boots, each running the
fuzzer's program, ours twice and '..' 1000: the create
fails with EUCLEAN and "Corrupt dir, invalid rec_len for
'..'"
control both rec_lens as written: both kernels index the
directory and create the file
Before v6.11 the check is in make_indexed_dir() itself (50ea741def58
moved it into ext4_check_dx_root()); stable trees without that commit
need the same bound there.
fs/ext4/namei.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f..5f96e87f49a2 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -2017,6 +2017,13 @@ static bool ext4_check_dx_root(struct inode *dir, struct dx_root *root)
unsigned int blocksize = dir->i_sb->s_blocksize;
char *blockend = (char *)root + dir->i_sb->s_blocksize;
+ /*
+ * make_indexed_dir() moves out what follows '..' up to the dirent
+ * tail, so '.' and '..' have to end before the tail starts.
+ */
+ if (ext4_has_feature_metadata_csum(dir->i_sb))
+ blockend -= sizeof(struct ext4_dir_entry_tail);
+
fde = &root->dot;
if (unlikely(fde->name_len != 1)) {
error_msg = "invalid name_len for '.'";
--
2.34.1