Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Piyus Kedia" <piyus@iiitd.ac.in>, linux-ext4@vger.kernel.org
Cc: tytso@mit.edu
Subject: Re: [PATCH] ext4: check dot and dotdot of the htree root before readdir uses them
Date: Sun, 06 Dec 2026 13:14:02 +0000	[thread overview]
Message-ID: <sashiko-outbox-252439@kernel.org> (raw)
In-Reply-To: <20261206130012.1327058-1-piyus@iiitd.ac.in>

Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [High] ext4: OOB read in ext4_htree_fill_tree() when entry 0 is not
  dot
- [Medium] ext4: ext4_htree_fill_tree() stores unvalidated dot entry
  before checking it
--

commit 8909302584683de8683d0cfae7c7f0e4a46d181d
Author: Piyus Kedia <piyus@iiitd.ac.in>

ext4: check dot and dotdot of the htree root before readdir uses them

This commit validates the '.' and '..' entries in an indexed directory's
root block with ext4_check_dir_entry() in ext4_htree_fill_tree() before
storing '..' for readdir.

> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index b2753daad49f4..5ca84f600c9b0 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -1053,8 +1053,24 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
>  		count++;
>  	}
>  	if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) {
> -		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
> +		struct buffer_head *bh = frames[0].bh;
> +
> +		/*
> +		 * '..' is found by stepping over '.' by its rec_len, which
> +		 * dx_probe() does not look at: check both entries first.
> +		 */
> +		de = (struct ext4_dir_entry_2 *)bh->b_data;
> +		if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
> +					 bh->b_size, 0)) {
> +			err = -EFSCORRUPTED;
> +			goto errout;
> +		}

[Severity: Medium]
Can this leave an unvalidated '.' entry stranded in dir_file's rbtree when
ext4_check_dir_entry() fails?

When start_hash and start_minor_hash are both zero, the preceding block has
already inserted entry 0 into info->root via ext4_htree_store_dirent().  If
either check here then returns -EFSCORRUPTED, ext4_dx_readdir() exits
without calling free_rb_tree_fname(&info->root) and leaves next_hash at 0.

On a subsequent getdents64() on the same fd, ext4_dx_readdir() picks up
that node via rb_first(&info->root), emits the unvalidated entry, and loops
at f_pos == 0 because getdents64() returns the positive byte count instead
of -EFSCORRUPTED once an entry has been copied out.

Validating '.' and '..' before the if (!start_hash && !start_minor_hash)
block would prevent storing an unvalidated entry.

>  		de = ext4_next_entry(de, dir->i_sb->s_blocksize);
> +		if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data,
> +					 bh->b_size, (char *)de - bh->b_data)) {
> +			err = -EFSCORRUPTED;
> +			goto errout;
> +		}

[Severity: High]
Can the second ext4_check_dir_entry() still read past the end of bh->b_data
if entry 0 is not named '.'?

__ext4_check_dir_entry() only rejects next_offset == size when the entry is
named '.':

fs/ext4/dir.c:__ext4_check_dir_entry() {
	...
	else if (unlikely(next_offset == size && de->name_len == 1 &&
			  de->name[0] == '.'))
		error_msg = "'.' directory cannot be the last in data block";
	else
		return 0;
}

If entry 0 has rec_len == blocksize and any other name (e.g. name_len == 0),
it passes the first check and get_dx_countlimit() even with metadata_csum.
ext4_next_entry() then sets de to bh->b_data + bh->b_size, so the second
ext4_check_dir_entry() reads past the buffer via de->rec_len and de->inode.

Checking (char *)de >= bh->b_data + bh->b_size (or verifying that entry 0
is '.') before the second ext4_check_dir_entry() call would close this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261206130012.1327058-1-piyus@iiitd.ac.in?part=1

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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-06 13:00 [BUG] ext4: readdir of an htree directory copies '..' from past the root block to user space Piyus Kedia
2026-12-06 13:00 ` [PATCH] ext4: check dot and dotdot of the htree root before readdir uses them Piyus Kedia
2026-12-06 13:14   ` sashiko-bot [this message]

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=sashiko-outbox-252439@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=linux-ext4@vger.kernel.org \
    --cc=piyus@iiitd.ac.in \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tytso@mit.edu \
    /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