Linux EXT4 FS development
 help / color / mirror / Atom feed
From: Kyuwon Cho <kyuwoncho18@gmail.com>
To: Theodore Ts'o <tytso@mit.edu>, Andreas Dilger <adilger.kernel@dilger.ca>
Cc: Jan Kara <jack@suse.cz>, Baokun Li <libaokun@linux.alibaba.com>,
	Ojaswin Mujoo <ojaswin@linux.ibm.com>,
	Ritesh Harjani <ritesh.list@gmail.com>,
	Zhang Yi <yi.zhang@huawei.com>,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v2] ext4: validate dot entry before reading dotdot in htree readdir
Date: Sat,  5 Dec 2026 17:21:34 +0900	[thread overview]
Message-ID: <179637501338.1425433.8835682775805888953@gmail.com> (raw)

ext4_htree_fill_tree() locates the dotdot entry by advancing over the
dot entry according to its on-disk record length. A corrupt record length
can make that advance land outside the dx root block before the code reads
the resulting entry.

Require the advance to land at the fixed dotdot position in struct
dx_root. Do this before storing either root entry because the readdir
rb-tree persists across calls, and an error must not leave a partial entry
queued there. The check only runs while the root entries are emitted by
readdir and adds no work to indexed lookups.

Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Signed-off-by: Kyuwon Cho <kyuwoncho18@gmail.com>
---
Changes in v2:
- Validate the '.' rec_len before inserting '.' into the rb-tree, so an
  error cannot leave partial state for a later readdir. This addresses the
  Sashiko AI review:
  https://lore.kernel.org/all/sashiko-outbox-249274@kernel.org/

v1: https://lore.kernel.org/all/179636470936.1110822.11479118660789833770@gmail.com/

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found this issue.

A reproducer is available on request.

 fs/ext4/namei.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f..95dd2d1a76c2 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1041,9 +1041,19 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
 	if (IS_ERR(frame))
 		return PTR_ERR(frame);
 
+	if (start_hash < 2 || (start_hash == 2 && start_minor_hash == 0)) {
+		de = (struct ext4_dir_entry_2 *)frames[0].bh->b_data;
+		if (unlikely(ext4_rec_len_from_disk(de->rec_len,
+						    dir->i_sb->s_blocksize) !=
+			     offsetof(struct dx_root, dotdot))) {
+			EXT4_ERROR_INODE(dir, "Corrupt dir, invalid rec_len for '.'");
+			err = -EFSCORRUPTED;
+			goto errout;
+		}
+	}
+
 	/* Add '.' and '..' from the htree header */
 	if (!start_hash && !start_minor_hash) {
-		de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data;
 		tmp_str.name = de->name;
 		tmp_str.len = de->name_len;
 		err = ext4_htree_store_dirent(dir_file, 0, 0,
@@ -1053,7 +1063,6 @@ 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;
 		de = ext4_next_entry(de, dir->i_sb->s_blocksize);
 		tmp_str.name = de->name;
 		tmp_str.len = de->name_len;

             reply	other threads:[~2026-12-05  8:21 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  8:21 Kyuwon Cho [this message]
2026-12-05  8:41 ` [PATCH v2] ext4: validate dot entry before reading dotdot in htree readdir sashiko-bot

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=179637501338.1425433.8835682775805888953@gmail.com \
    --to=kyuwoncho18@gmail.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=jack@suse.cz \
    --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