* [BUG] ext4: make_indexed_dir() copies and wipes 4 GiB when '..' ends inside the dirent tail
@ 2026-12-06 12:59 Piyus Kedia
2026-12-06 12:59 ` [PATCH] ext4: bound dot and dotdot of dx_root by " Piyus Kedia
0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 12:59 UTC (permalink / raw)
To: Theodore Ts'o, linux-ext4
Cc: Andreas Dilger, Baokun Li, Jan Kara, Ojaswin Mujoo,
Ritesh Harjani (IBM), Zhang Yi, Darrick J . Wong, linux-kernel,
Kapil Vaswani
Hi,
When a new name does not fit in a directory of one block,
__ext4_add_entry() calls make_indexed_dir(), which moves everything
after the '..' entry of block 0 to a new block and makes block 0 the
htree root. It takes '..' at its fixed offset and stops at the
metadata_csum dirent tail:
fs/ext4/namei.c:2100 (make_indexed_dir)
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 */
len is an unsigned int. The one check of '..' before this is in
ext4_check_dx_root(), added by 50ea741def58 ("ext4: check dot and
dotdot of dx_root before making dir indexed"):
fs/ext4/namei.c:2044 (ext4_check_dx_root)
rlen = ext4_rec_len_from_disk(fde->rec_len, blocksize);
if (unlikely((char *)fde + rlen >= blockend)) {
error_msg = "invalid rec_len for '..'";
and blockend is root + blocksize. That commit added the checks of the
two names and of '.', and kept this bound from the check it replaced
in make_indexed_dir(), which e6b8bc09ba20 ("ext4: Add sanity check to
make_indexed_dir") had added before there was a dirent tail.
b0336e8d2108 ("ext4: calculate and verify checksums of directory leaf
blocks") made len stop csum_size (12) bytes before the end of the
block and left the bound at the end. So with metadata_csum a '..'
that ends in the last 12 bytes of the block passes, and len wraps: on
a 1 KiB block, '..'.rec_len 1000 gives len 0, and 1001 to 1011 give
0xffffffff down to 0xfffffff5.
The walk that runs just before, ext4_find_dest_de() from
add_dirent_to_buf(), checks each entry it visits against the tail and
would reject such a '..'. But it goes from entry to entry by rec_len,
and ext4_check_dx_root() bounds '.'.rec_len only by the block end too,
so a '.' with rec_len 24 takes the walk from offset 0 to offset 24,
over the '..' at offset 12, which then only make_indexed_dir() reads.
Our fuzzer made a filesystem with 1 KiB blocks, metadata_csum and
dir_index (no journal), and a directory holding 16 files with
50-character names (one block, 28 bytes free); it set '.'.rec_len to
24 and '..'.rec_len to 1004 in the directory's block 0, recomputed the
block's checksum, mounted the image and created a file with a
255-character name in the directory, so len = 1012 - (12 + 1004):
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)
lookup_open (fs/namei.c:4575)
path_openat (fs/namei.c:4774 fs/namei.c:5013)
...
__x64_sys_openat (fs/open.c:1460)
The read starts at byte 1016 of the directory block, which in all our
runs and the fuzzer's was the first 1 KiB of a page of the block
device's page cache, and KASAN names it after the first poisoned byte
it meets, at the start of the next page: the fuzzer also got
"use-after-free", and we got "slab-out-of-bounds", "use-after-free"
and "slab-use-after-free" upstream.
What follows the read, measured upstream:
- KASAN skips a memcpy() or memset() whose range it reports, but it
reports only the first bad access of a boot; after that,
kasan_report() returns false and the access is let through. So
the memcpy() at :2114 is reported and not done, and the memset()
at :2115 runs. With kasan_multi_shot, KASAN reports the memset()
too, at the same address, and skips it as well:
BUG: KASAN: use-after-free in make_indexed_dir (fs/ext4/namei.c:2115)
Write of size 4294967292 at addr ffff888049f5b3f8 by task x/9444
and the create then fails with EUCLEAN (the loop after the copy
finds rec_len 0 in the new, zeroed block).
- In every boot with KASAN's default single report (4 of 4, one
of them on a single CPU), within 2 ms of the end of the report
other tasks faulted on zeroed memory - processes segfaulting at
address 0, NULL pointer dereferences in mod_memcg_lruvec_state(),
wq_worker_running(), __rb_erase_color(), update_se(),
__refill_objects_node() - and the kernel panicked within 0.5 s.
In one of them the task itself oopsed when a timer interrupt
preempted it inside the memset():
BUG: unable to handle page fault for address: ffffed10136dd802
psi_group_change (kernel/sched/psi.c:848)
...
asm_sysvec_apic_timer_interrupt (arch/x86/include/asm/idtentry.h:674)
RIP: 0010:memset_orig (arch/x86/lib/memset_64.S:69)
RDX: 00000000fffffffc RSI: 0000000000000000 RDI: ffff88801fc4cb78
RBP: ffff88801e3643f8 R08: 0000000000000001 R09: 0000000000000000
make_indexed_dir (fs/ext4/namei.c:2115)
__ext4_add_entry (fs/ext4/namei.c:2283)
RBP is the address of the KASAN report, RDI the memset()'s current
position: 26118016 bytes (25 MiB) zeroed by then. The fuzzer's
run on 7.4-rc5 got further: KFENCE reported "invalid write in
memset_orig" from make_indexed_dir (fs/ext4/namei.c:2115) at
ffff88807ec00000, 473312264 bytes (451 MiB) past where it started,
then on the next KFENCE pages as the memset() went on.
- With kasan.fault=panic the kernel panics in the first report.
- We have no kernel without KASAN here. By the source, there the
memcpy() runs first: 0xfffffffc bytes from byte 1016 of the
directory block, into the buffer of the newly appended block and
on, until a source or destination address faults.
Impact - who can trigger it:
- A crafted ext4 image has to be mounted. ext4 is not
FS_USERNS_MOUNT, so that needs CAP_SYS_ADMIN in the initial user
namespace: root, or a service that mounts on a user's behalf. We
have not tested whether desktop auto-mounting of removable media
mounts such an image.
- Then a name has to be added to the crafted directory: open(2) with
O_CREAT, mkdir, mknod, symlink, link or rename into it - anything
that goes through ext4_add_entry(). That needs no privilege beyond
write and search permission on the directory, whose owner and mode
the image sets. With the directory 0777 in the image, the create
done by uid 65534 (no groups, CapEff 0) gave the same KASAN report,
"UID: 65534" in it, in 3 of 3 boots; with it 0755, that create
failed with EACCES and nothing was reported.
- The image needs metadata_csum (without the tail the bound and len
agree), dir_index, a directory of one block, and the two rec_lens.
Block 0 has to pass its checksum, which the image's author
computes: crc32c over the block, seeded with the UUID, the inode
number and the generation, all in the image. We ran 1 KiB blocks;
by the source, other block sizes have the same 12-byte window.
- What is written: with KASAN, zeros from byte 1016 of the
directory block onwards, measured as above; without KASAN, by the
source, first a copy of the memory from there over the memory from
the new block's buffer onwards, then the zeros. The length is
fixed by the window, 4 GiB less 1 to 11 bytes, so it runs until
something faults or the kernel dies of what it overwrote; every
boot we ran without kasan.fault=panic or kasan_multi_shot ended in
a panic. We did not look at whether the damage can be steered.
Seen on 7.4.0-rc5 (two reports, the titles above) and REPRODUCED ON
UPSTREAM: on ksmbd-for-next (d70570cf8003, 2026-12-04), our upstream
base, whose fs/ext4 is identical to mainline master (9437ee8b6cf6) and
to the ext4 tree's dev branch (aceadbccd428, merged in 9437ee8b6cf6),
with KASAN. fs/ext4/namei.c is also unchanged in mainline
c3500c5ef648 (2026-12-04).
Same config and image recipe before and after, the patch the only
change, each run on a freshly made image:
without the patch
'..' 1004 KASAN read of 4294967292 bytes in make_indexed_dir()
(:2114) in 6 of 6 boots: the fuzzer's program
unchanged once, ours five times; the four boots with
KASAN's single report went on as described above
and panicked
'..' 1000 WARNING at fs/ext4/namei.c:1826 in do_split(), "bad
indexed directory? ... count=0 move=0", EUCLEAN
(1 of 1)
with the patch: no KASAN report and no warning in 3 boots (one with
kasan_multi_shot), each running the fuzzer's program once, ours
twice and '..' 1000 once: the create fails with EUCLEAN, the
directory is left as it was, and the kernel logs
EXT4-fs error (device loop0): ext4_check_dx_root:2060: inode #12:
comm poc: Corrupt dir, invalid rec_len for '..', running e2fsck
is recommended
The create as uid 65534 fails the same way (1 boot); with the
directory 0755 it fails with EACCES, as on the base.
control, both rec_lens as the kernel wrote them: on both kernels the
file is created, the directory becomes indexed and readdir lists
all 19 entries, with no ext4 message (1 boot on the base, 3 with
the patch). With only '.' left at 24 ('..' 12) both kernels also
create the file without a memory error, but cannot place the index
checksum ("dir seems corrupt? Run e2fsck -D.") and readdir then
fails with EIO.
A patch follows in reply to this mail. It bounds '.' and '..' in
ext4_check_dx_root() by the start of the dirent tail when the
filesystem has metadata_csum, the bound make_indexed_dir() and the
entry walk use, so len is at least 1. That also rejects a '..' that
ends exactly at the tail (len == 0), which otherwise ends in the
WARN_ON_ONCE(split == 0) in do_split(), as we measured on the base. A
directory the kernel wrote cannot reach 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.
Other places we read for the same pattern: the other lengths taken
against blocksize - csum_size in fs/ext4/namei.c (do_split(),
dx_make_map(), ext4_generic_delete_entry(), __ext4_add_entry(),
ext4_init_dot_dotdot()) come from entries ext4_check_dir_entry() has
checked or from the kernel's own arithmetic; dx_probe() reads the root
info at its fixed offset but takes no length from '..'; fs/ext4/dir.c
walks with ext4_check_dir_entry(); and an inline directory keeps '..'
as a 4-byte inode number, not an entry. ext4_htree_fill_tree(), which
finds the htree root's '..' for readdir by stepping over '.' by its
unchecked rec_len, is a separate bug that we report separately
("ext4: readdir of an htree directory copies '..' from past the root
block to user space").
This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code, and every access
above was reproduced, except where we say it is from the source. The
reproducer is one program that makes the image with mkfs.ext4 and edits
it, and we are happy to send it on request.
Kapil Vaswani
Piyus Kedia
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH] ext4: bound dot and dotdot of dx_root by the dirent tail
2026-12-06 12:59 [BUG] ext4: make_indexed_dir() copies and wipes 4 GiB when '..' ends inside the dirent tail Piyus Kedia
@ 2026-12-06 12:59 ` Piyus Kedia
0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-06 12:59 UTC (permalink / raw)
To: Theodore Ts'o, linux-ext4
Cc: Andreas Dilger, Baokun Li, Jan Kara, Ojaswin Mujoo,
Ritesh Harjani (IBM), Zhang Yi, Darrick J . Wong, linux-kernel,
Kapil Vaswani
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@iiitd.ac.in/
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
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
^ permalink raw reply related [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-06 12:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-06 12:59 [BUG] ext4: make_indexed_dir() copies and wipes 4 GiB when '..' ends inside the dirent tail Piyus Kedia
2026-12-06 12:59 ` [PATCH] ext4: bound dot and dotdot of dx_root by " Piyus Kedia
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox