* [PATCH] ext4: validate dot entry before reading dotdot in htree readdir
@ 2026-12-04 6:36 Kyuwon Cho
2026-12-04 6:59 ` sashiko-bot
0 siblings, 1 reply; 2+ messages in thread
From: Kyuwon Cho @ 2026-12-04 6:36 UTC (permalink / raw)
To: Theodore Ts'o, Andreas Dilger
Cc: Jan Kara, Baokun Li, Ojaswin Mujoo, Ritesh Harjani, Zhang Yi,
linux-ext4, linux-kernel
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. This 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>
---
Report: https://lore.kernel.org/all/179485814813.2717208.11773019866458970526@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 | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
index b2753daad49f..6b3d82d220a5 100644
--- a/fs/ext4/namei.c
+++ b/fs/ext4/namei.c
@@ -1054,6 +1054,13 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
}
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;
+ }
de = ext4_next_entry(de, dir->i_sb->s_blocksize);
tmp_str.name = de->name;
tmp_str.len = de->name_len;
--
2.43.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ext4: validate dot entry before reading dotdot in htree readdir
2026-12-04 6:36 [PATCH] ext4: validate dot entry before reading dotdot in htree readdir Kyuwon Cho
@ 2026-12-04 6:59 ` sashiko-bot
0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-04 6:59 UTC (permalink / raw)
To: Kyuwon Cho; +Cc: linux-ext4, tytso
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] ext4: ext4_htree_fill_tree() inserts dot before validating
dot rec_len
--
commit 6f4ef7a8631413a1ffaaa8592a0cf12dd1285622
Author: Kyuwon Cho <kyuwoncho18@gmail.com>
ext4: validate dot entry before reading dotdot in htree readdir
This commit validates that the '.' directory entry's record length in an
htree root block matches the offset of '..' in struct dx_root before
advancing to '..' in ext4_htree_fill_tree().
> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index b2753daad49f4..6b3d82d220a5a 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -1054,6 +1054,13 @@ int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
> }
> 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;
> + }
> de = ext4_next_entry(de, dir->i_sb->s_blocksize);
> tmp_str.name = de->name;
> tmp_str.len = de->name_len;
[Severity: Medium]
Does this leave '.' stranded in info->root when readdir starts at offset 0?
When start_hash and start_minor_hash are both 0, the preceding block already
inserts '.' via ext4_htree_store_dirent() before this check fails with
-EFSCORRUPTED. In ext4_dx_readdir(), info->cookie was already updated and a
negative return jumps to finished without freeing info->root.
On the next getdents64() call on the same fd, ext4_dx_readdir() emits the
stranded '.' from info->root and retries ext4_htree_fill_tree(). Because
'.' was emitted, getdents64() returns a positive byte count instead of
-EFSCORRUPTED with f_pos still at 0, so repeated calls loop forever.
Checking de->rec_len before inserting '.' avoids mutating info->root before
rejecting the corrupt entry.
--
Sashiko AI review · https://sashiko.dev/#/patchset/179636470936.1110822.11479118660789833770@gmail.com?part=1
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-04 6:59 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 6:36 [PATCH] ext4: validate dot entry before reading dotdot in htree readdir Kyuwon Cho
2026-12-04 6:59 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox