mirror of https://lore.kernel.org/lkml/
 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
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-11-16 19:42 UTC | newest]

Thread overview: (only message) (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

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®