From: Piyus Kedia <piyus@iiitd.ac.in>
To: "Theodore Ts'o" <tytso@mit.edu>, linux-ext4@vger.kernel.org
Cc: Andreas Dilger <adilger.kernel@dilger.ca>,
Baokun Li <libaokun@linux.alibaba.com>, Jan Kara <jack@suse.cz>,
Ojaswin Mujoo <ojaswin@linux.ibm.com>,
"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
Zhang Yi <yi.zhang@huawei.com>,
"Darrick J . Wong" <djwong@kernel.org>,
linux-kernel@vger.kernel.org,
Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] ext4: make_indexed_dir() copies and wipes 4 GiB when '..' ends inside the dirent tail
Date: Sun, 6 Dec 2026 18:29:14 +0530 [thread overview]
Message-ID: <ext4-dx-root-dotdot.20261206@iiitd.ac.in> (raw)
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
next reply other threads:[~2026-12-06 12:59 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-06 12:59 Piyus Kedia [this message]
2026-12-06 12:59 ` [PATCH] ext4: bound dot and dotdot of dx_root by " Piyus Kedia
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ext4-dx-root-dotdot.20261206@iiitd.ac.in \
--to=piyus@iiitd.ac.in \
--cc=adilger.kernel@dilger.ca \
--cc=djwong@kernel.org \
--cc=jack@suse.cz \
--cc=kapil@presceralabs.ai \
--cc=libaokun@linux.alibaba.com \
--cc=linux-ext4@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=ojaswin@linux.ibm.com \
--cc=ritesh.list@gmail.com \
--cc=tytso@mit.edu \
--cc=yi.zhang@huawei.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®