mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>,
	linux-kernel@vger.kernel.org,
	Kapil Vaswani <kapil@presceralabs.ai>
Subject: [BUG] ext4: readdir of an htree directory copies '..' from past the root block to user space
Date: Sun,  6 Dec 2026 18:30:04 +0530	[thread overview]
Message-ID: <ext4-htree-dotdot.20261206@iiitd.ac.in> (raw)

Hi,

For an indexed directory, readdir returns '.' and '..' from the htree
root block, and ext4_htree_fill_tree() finds '..' by stepping over '.'
by its rec_len:

  fs/ext4/namei.c:1055 (ext4_htree_fill_tree)
	if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
		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);

ext4_htree_store_dirent() copies de->inode, de->file_type and
name_len bytes of de->name into the entry that getdents64() then
returns (fs/ext4/dir.c:506-509).  dx_probe(), which read the root just
before, checks the dx_root_info and the index entries but neither
directory entry, and nothing else on this path checks '.'.rec_len
(with metadata_csum the root's checksum is found through it; see
below).  A '.'.rec_len of the block size puts '..' just past the root
block, and its name_len, whatever byte lies there, becomes the length
of the copy.
The same two entries are checked before make_indexed_dir() uses them
(ext4_check_dx_root(), 50ea741def58 "ext4: check dot and dotdot of
dx_root before making dir indexed"), but not here.

Our fuzzer made a filesystem with 4 KiB blocks, dir_index, no journal
and no metadata_csum, a directory of 200 files (so it is indexed), set
'.'.rec_len in its htree root to 0x1000, mounted the image again and
called getdents64() on the directory:

  BUG: KASAN: slab-out-of-bounds in ext4_htree_store_dirent
                                    (fs/ext4/dir.c:509)
  Read of size 173 at addr ffff888019ff6008 by task syz-executor/11682
   __asan_memcpy (mm/kasan/shadow.c:105)
   ext4_htree_store_dirent (fs/ext4/dir.c:509)
   ext4_htree_fill_tree (fs/ext4/namei.c:1060)
   ext4_dx_readdir (fs/ext4/dir.c:620)
   ext4_readdir (fs/ext4/dir.c:149)
   iterate_dir (fs/readdir.c:114)
   __do_sys_getdents64 (fs/readdir.c:404)
  The buggy address is located 8 bytes inside of
   allocated 88-byte region [ffff888019ff6000, ffff888019ff6058)
  (kmalloc-96)

The root block is a page of the block device's page cache, so '..' is
read from the start of whatever page follows it in memory: here an
allocated kmalloc-96 object, whose byte 6 (173) became the length.

What comes back to user space, measured upstream (4 KiB blocks, the
root's '.'.rec_len 0x1000, getdents64() on the directory):

  - KASAN, when it reports, names the first poisoned access - in our
    runs the read of name_len (namei.c:1059) or the copy (dir.c:509) -
    after whatever object or page followed the block - a
    freed or allocated slab object (kmalloc-32, kmalloc-256,
    sgpool-16, names_cache) or a free page.  It skips a memcpy() it
    reports, so that name comes back as zeros, but the loads of
    name_len, inode and file_type are done anyway: with
    kasan_multi_shot, d_ino came back as 0x47915500 and 0x43e387e0,
    the first 4 bytes of a freed sgpool-16 and a freed names_cache
    object.

  - KASAN reports once per boot.  In a boot where it had already
    reported, ten fresh images read as root returned, as the name of
    '..': 255 bytes holding 5 to 14 kernel heap pointers (0xffff8880...)
    and up to 4 kernel text addresses (0xffffffff8.../9...) in 4 of
    10, 248 bytes of 0xf8 in 3 (a KASAN shadow page, it appears: 0xf8
    is what KASAN fills them with), one byte in 1, and getdents64()
    failed with EIO in 2 (name_len 0, rejected by verify_dirent_name()).
    For example, one name began

      10 00 00 00 7a 00 88 06 40 36 79 1f 80 88 ff ff  ...

    that is ffff88801f793640 at byte 8.  d_ino came back with what
    look like the low halves of such pointers (0x1f793750, 0x272863c0,
    ...).

  - Read as uid 65534 (no groups, CapEff 0) in the same boot, 2 of 5
    returned 255 and 173 bytes of kernel memory, with 3 and 5 heap
    pointers and 3 kernel text addresses each (ffffffff97841f20,
    ffffffff8808b380, ...); the other 3 failed with EIO.

  - Without KASAN nothing skips the copy; by the source every read
    returns the name_len bytes that follow the block, as in the runs
    above where KASAN no longer checked.

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 anyone who can read the directory triggers it with
    getdents64() (ls, find, ...): measured as uid 65534 with no groups
    and CapEff 0, on a directory of mode 0755 - KASAN reported the same
    reads with "UID: 65534" in them, and the kernel memory above came
    back to that user.

  - The image needs dir_index and an indexed directory whose root's
    '.'.rec_len is changed; nothing else.  With metadata_csum the root's
    dx checksum is located by '.'.rec_len (get_dx_countlimit()): 12,
    or the block size, in which case the count and limit are taken from
    byte 8 and the checksum from after them.  A root with '.'.rec_len
    4096 and a checksum written there passes (measured, 2 boots: no
    checksum error, the directory listed, and '..' read from past the
    block - 248 bytes of 0xf8 in one, the next page's own first entry
    in the other).  So metadata_csum does not stop it; it only limits
    '.'.rec_len to the block size, which is enough.

  - Kernel memory disclosed to user space: up to 255 bytes as the name
    of '..' (from 8 bytes past the end of the root block) and 4 as its
    d_ino (from the first 4 bytes past it), out of whatever page
    follows the block in memory, read again by every readdir from the
    start of the directory.  Measured: kernel heap pointers and kernel
    text addresses, as above (a text address gives away the KASLR
    offset).
    Without metadata_csum, '.'.rec_len can place '..' up to 64 KiB
    past the block, by the source; we ran 0x1000.

Seen on 7.4.0-rc5 (the report 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 and fs/ext4/dir.c are 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 (6 boots, 29 readdirs of a crafted root, the
    fuzzer's program once): in all 29, '..' came back as something
    other than '..' - zeros (a copy KASAN had reported), kernel
    memory, 0xf8 bytes, another page's entry - or getdents64() failed
    with EIO (an empty name).  KASAN reported in 4 of the 6 boots, the
    fuzzer's program's included; in the other 2 the memory after the
    block was not poisoned.
  with the patch (3 boots, one with kasan_multi_shot, 29 readdirs: the
    fuzzer's program, ours as root, as uid 65534 and with
    metadata_csum, and the fifteen-attempt loop): no KASAN report;
    getdents64() fails with EUCLEAN and the kernel logs
      EXT4-fs error (device loop0): ext4_htree_fill_tree:1063: inode
      #12: block 11: comm poc: bad entry in directory: '.' directory
      cannot be the last in data block - offset=0, inode=12,
      rec_len=4096, size=4096 fake=1
  control, the root as written ('.'.rec_len 12; with the patch also
    with metadata_csum): 202 entries, '..' as on disk, on both kernels
    (2 boots each).

A patch follows in reply to this mail.  It checks '.' and then '..'
with ext4_check_dir_entry() before ext4_htree_fill_tree() uses '..',
as htree_dirblock_to_tree() checks the entries of the leaf blocks, and
fails the readdir with -EFSCORRUPTED if either is bad.  A '.' that
passes ends at least 12 bytes before the end of the block, so '..'
starts inside it, and a '..' that passes holds its name.  The root the
kernel and e2fsck write ('.' of 12 bytes, '..' to the end of the
block) passes.

Other places we read that use the root's '.' and '..': dx_probe() reads
the dx_root_info at its fixed offset, not through '.'; the metadata_csum
lookup of the root's checksum (get_dx_countlimit()) accepts only
'.'.rec_len 12 or the block size and checks that what it then reads
fits the block; a lookup of ".." searches block 0 with
ext4_search_dir(), which bounds every entry by the block;
ext4_get_first_dir_block() and ext4_empty_dir() check both entries
with ext4_check_dir_entry(); make_indexed_dir() takes '..' at its fixed
offset after ext4_check_dx_root() (we report a gap in that check
separately: "ext4: make_indexed_dir() copies and wipes 4 GiB when '..'
ends inside the dirent tail").  ext4_htree_fill_tree() is the only one
that steps over '.' unchecked.

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.  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

             reply	other threads:[~2026-12-06 13:00 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 13:00 Piyus Kedia [this message]
2026-12-06 13:00 ` [PATCH] ext4: check dot and dotdot of the htree root before readdir uses them 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-htree-dotdot.20261206@iiitd.ac.in \
    --to=piyus@iiitd.ac.in \
    --cc=adilger.kernel@dilger.ca \
    --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®