From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796561962; b=kfXgv1KGlEIqF70QBVJh0qqFFTyvVgfvgLNHkDfXVQEihjwPs0Nz33WV6Oa+xLDSrD78 AVHpahnI583xFRq6TTVFWLrLQ1z5MzXi1y7fkE2nYO/lVBvcvIVpsqv4p0gOuYxRJZaII dt5Prv80kTCmidblddffaE2j2PFKv/IzgoBVLFbkN5akYfdj3Qidq3NVC3f69k6Msc7lC B0Vz1/eMwExTQil+nVPNuz+mOD+ZeITMr1zFpck5UxAg6IQQ0usEvanz1irJG7LU86fT3 DkozM7riCJsP1wkIItLajuwLMT8vlRExw3FAWnezINzrlazFDMQLB1Q7n6dVspMFNZw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796561962; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=aGiiNv7jaxCJePJ4bQpBqrOJW03vUaBcAHepN2uh3YU=; b=O/NBlgRgr+PcAVZZJsVqlJd5lLEz26x46pkEfs/MKXmRoDcT2p0KLfdU3t+HunPXwjL9 A1VV6PG4hU1VBwBz5ObohCPP7p//muSOxgX+JMEv9PreD5OX/yZGFeqYBwps5eXBbG3j6 q4mmtA3BK1QHy6/m78LWMr3TnG4OR55NtSAq4OnH4IFX5uSV7wEYzMqlb7Vo4qcAc6HIW iOxQZOz2erk8U2uVTm6j9OR1JBw0FUuw7v4XnpN1g6LKTBLFZcUx2tc4iBBT8hCzOZ0lp w7emAeV8QuK0Cb+XVcJC8yLYpNJt5ud0BnslyKaOvU9s+VESXTpwhXOoqjN6Iv2rDIw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=iiitd.ac.in; spf=pass smtp.mailfrom=iiitd.ac.in; dkim=pass header.d=iiitd.ac.in header.i=@iiitd.ac.in header.a=rsa-sha256 header.s=google header.b=fwpS3ou0; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8CF9606A33F for ; Sun, 6 Dec 2026 12:59:21 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org E8CF9606A33F Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iiitd.ac.in Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iiitd.ac.in Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=iiitd.ac.in header.i=@iiitd.ac.in header.a=rsa-sha256 header.s=google header.b=fwpS3ou0 Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-386ab9ab619so3970212eec.1 for ; Sun, 06 Dec 2026 04:59:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iiitd.ac.in; s=google; t=1796561961; x=1797166761; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=aGiiNv7jaxCJePJ4bQpBqrOJW03vUaBcAHepN2uh3YU=; b=fwpS3ou0aB24zLsJTAM2iPXAk1vmvKIvL43qNYKQ26OxfQ6+xsO6WYXkOqicR93Bws OPpIHiXdPLquhIISB6IlgbITmIGKDRBC1XysuWUe1Zhh+kg6qXVnHN0AJzmAbzo0fdEk 8sB05YXOaJYG/NC0xj07/q2wsjv2gjCbGRYWw= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796561961; x=1797166761; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=aGiiNv7jaxCJePJ4bQpBqrOJW03vUaBcAHepN2uh3YU=; b=Yy0rNUiWuvv6cnTt00ZxFpoQtlSeduxa4deEH04krNECvhgkMd2jgPERgT7ga8B2z8 ZWRKJPC3Svt+sjZPlBq1MPbswg1ZRtk2NQ5D6cRTvlIGT5+XloeOVCOgvcI4eXxDT69s mUnEYqof0JsgNW2zilo4G8zPosDjdGxjDevHrAMYL19UiYTFfmyigrbKbWtPOforfIH7 R6wnD9gCJyWAFbDWKSJHeCx46I1czjGQ7WEwxbYrktGB2z2zPNGxUXzI7XU6KiEGKQaJ t+61zHU3//SFhsC/K0NeUCzQx5AIve2d9JFwEV/sBsXroyRvR+6gh4UmcvKwoxueWDW1 cjAQ== X-Forwarded-Encrypted: i=1; AL+hErGgQu1TBigb30JFyvsej7cy8v6exAM8s84wQX/Bm1Gn6CGauK/Ef5aH2Snnj+r8T8/gWzbdX86guJq/kfM=@vger.kernel.org X-Gm-Message-State: AB4q+uGvOH3yMqWgzS6swuX0XZPNBzOZCvSo+KeLjx4bO9CvtT5xfRbH vTegyOlEtH3ncFMJHltZvaTn21YKYPqYjXudSu4h/uhzj/O5hz3tYS7GbVOgPtdIEjw= X-Gm-Gg: AZFdTZcByZECZboR+U4BcVTniBiimiRH3jZbKz1+lfr4behbfMFH/qcWNzf/xMoGOwf u1dQ4DuAZuLGJXxhIDhZcKrwGn5PERFp0u8W8GXnIvQlnSh9DKPxtaYzZMuFhyX2jwoxRs4qmML fRh6bZJO8NLup+vvR1VA/evVl1pcvTp/jSBArceg2jnBj6HWNY9JUZnPrmCyFBVhvk0xTiOfmh7 9kVZvuf3O24g+I2RYb4qmut+EQdBHW1msKBCRdhp1pk0GuRuObBxWqV/trNTaLQSbLIK6QzPk4q 7UJSZDXU1W1XcZuczGWJFog0jA/jiR8/B+Yj3fMUU7MY2JrrKY3YuJduPNg1gWu0fjL2W/WnAVB rJYE7vf1NoMdIXooG8AjLsfM+xlKe7cNLhDfbQqAmazNSOiALvGp8n917kuzj40Xa1w5ApnwIjD /0C8SnSouc89dxlRzroWoT/3NNHbYgDadOhT4yLki4pYe5QgfIItmnE43yB9KqpVS0sbCrtSqVZ 6PkPjkVH0tWzQ3zbS09nZs3oT/JvHO5RpY40eAtQ+OEF5rUSGZsqEknar1p6IGXuYCQl0MSZSj0 cdGsZ0rzNTztwHGidi7QaWD6f7c2zP6gQHTQP6hHNqpnWrilZxt16OO8UEzcKZdpoqzGkKPgIso fQVu55E+Bdnno9QaL4kFRtMLEF1n3zFUeTWirMUOP2OsH2OBcms+9oGWblz1J8viR X-Received: by 2002:a05:7301:da85:b0:387:3248:15d4 with SMTP id 5a478bee46e88-38751fc3beamr8354332eec.4.1796561960882; Sun, 06 Dec 2026 04:59:20 -0800 (PST) Received: from iiitd-S2600STB.iiitd.edu.in ([103.25.231.160]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e5e7dsm33739496eec.19.2026.12.06.04.59.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Dec 2026 04:59:20 -0800 (PST) From: Piyus Kedia To: "Theodore Ts'o" , linux-ext4@vger.kernel.org Cc: Andreas Dilger , Baokun Li , Jan Kara , Ojaswin Mujoo , "Ritesh Harjani (IBM)" , Zhang Yi , "Darrick J . Wong" , linux-kernel@vger.kernel.org, Kapil Vaswani 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 Message-Id: X-Mailer: git-send-email 2.34.1 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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