Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Artem Blagodarenko" <artem.blagodarenko@gmail.com>,
	linux-ext4@vger.kernel.org
Cc: tytso@mit.edu
Subject: Re: [PATCH v15 13/13] ext4: add KUnit tests for dirdata
Date: Thu, 03 Dec 2026 23:15:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-248783@kernel.org> (raw)
In-Reply-To: <20261203184938.422849-14-ablagodarenko@thelustrecollective.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Critical] ext4: ext4_dirdata_set_lufid() rollback corrupts htree root
  or orphans inode
- [Medium] ext4: TOCTOU race on dir permission/immutability in
  ext4_ioctl_set_lufid()
- [Medium] ext4: unaligned rec_len accepted in dx_get_dx_info() and
  get_dx_countlimit()
- [Low] ext4: inverted e2fsck vs kernel comment in
  test_dirdata_dx_node_limit()
- [Low] ext4: inaccurate rename-test comment above dirdata_iget_stub()
- [Low] ext4: oversized dot entry test misses first guard in
  dx_get_dx_info()
- [Low] ext4: leak of dir_info on assertion failure in
  test_dirdata_htree_with_lufids()
--

commit 09dacab8b09110015062b9b89088f346310d76d3
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>

ext4: add KUnit tests for dirdata

This commit adds the ext4_dirdata_test KUnit suite to fs/ext4/namei-test.c
and exports the needed dirdata and htree test helpers from fs/ext4/namei.c.

> diff --git a/fs/ext4/namei-test.c b/fs/ext4/namei-test.c
> index b45f5ad3b7af6..ba1bb52d24425 100644
> --- a/fs/ext4/namei-test.c
> +++ b/fs/ext4/namei-test.c

[ ... ]

> +	/* So is a rec_len of '.' that leaves no room for '..' */
> +	dirdata_make_de(b, dir->i_ino, ".", EXT4_FT_DIR, false, 0, false,
> +			0, 0, TEST_BLK_SIZE - 8);
> +	KUNIT_EXPECT_TRUE(test, IS_ERR(dx_get_dx_info_test(dir, b)));

[Severity: Low]
Does TEST_BLK_SIZE - 8 actually exercise the first-entry overflow guard in
dx_get_dx_info()?

dx_get_dx_info() checks rlen + EXT4_BASE_DIR_LEN > blocksize, and with
EXT4_BASE_DIR_LEN == 8 a rec_len of TEST_BLK_SIZE - 8 passes that check.
This subcase only returns an error because the trailing 8 bytes of b happen
to be zeroed from an earlier call; using TEST_BLK_SIZE - 4 would hit the
intended guard directly.

[ ... ]

> +		dir_info = kzalloc_obj(*dir_info);
> +		KUNIT_ASSERT_NOT_NULL(test, dir_info);
> +		dir_file->private_data = dir_info;
> +		cnt = ext4_htree_fill_tree(dir_file, curr_hash, 0, &next_hash);
> +		KUNIT_ASSERT_GE(test, cnt, 0);
> +		total += cnt;
> +		ext4_htree_free_dir_info(dir_info);

[Severity: Low]
Can this leak dir_info if ext4_htree_fill_tree() fails?

KUNIT_ASSERT_GE() aborts the test before ext4_htree_free_dir_info() runs,
leaking both dir_info and any struct fname rb-tree nodes inserted before
the error.  Calling ext4_htree_free_dir_info(dir_info) before the assertion
avoids the leak.

[ ... ]

> +/* The rename tests look the target up through ext4_iget() */
> +static struct inode *dirdata_iget_stub(struct super_block *sb,

[Severity: Low]
This isn't a bug, but test_dirdata_rename_keeps_lufid() never activates
dirdata_iget_stub(); the only caller is test_dirdata_set_lufid().

[ ... ]

> +/*
> + * Dirdata 9: an htree index node of a casefolded and encrypted directory
> + * may have the limit that the kernel computes or the one that e2fsck
> + * computes, which reserves room for the hash in the fake dirent.
> + */

[Severity: Low]
Is this comment inverted relative to fs/ext4/namei.h?  dx_node_limit() and
dx_node_limit_ok() document that e2fsck (and the current kernel) do not
reserve room for the hash in the fake dirent, while older kernels did.

> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index f9250407cedbc..b8a5bd8036036 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c

[ ... ]

> @@ -4991,6 +4993,7 @@ int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
>  
>  	return err;
>  }
> +EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_dirdata_set_lufid);

[Severity: Critical]
Can the error rollback in ext4_dirdata_set_lufid() (introduced in commit
"ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries")
corrupt an htree root block or orphan the target inode?

On a one-block linear directory, ext4_dirdata_set_lufid() deletes the entry
from block 0 (bh) and calls ext4_add_fname().  If the larger LUFID entry
does not fit, make_indexed_dir() sets EXT4_INODE_INDEX, moves existing
entries to block 1, and rewrites bh into an htree root block before calling
do_split().  If do_split() then fails on ext4_append() with -ENOSPC or
-EDQUOT, make_indexed_dir() leaves dir indexed and bh formatted as an htree
root.

Rollback then calls add_dirent_to_buf(handle, &old_fname, dir, inode, NULL,
bh) on that same bh.  Without metadata_csum, ext4_find_dest_de() treats the
block-spanning '..' rec_len as free space and overwrites dx_root_info and
dx_entry[] with old_fname, silently corrupting the htree root on disk.  With
metadata_csum, ext4_check_dir_entry() rejects the full-block '..' entry with
-EFSCORRUPTED, triggering EXT4_ERROR_INODE() and leaving inode orphaned.

In an already-indexed directory, if ext4_dx_add_entry() splits the leaf
block before failing, writing old_fname back into the stale bh can also
place the entry in the wrong htree hash leaf.

[Severity: Medium]
Can a racing EXT4_IOC_SET_LUFID ioctl bypass a concurrent chmod or chattr +i
on the parent directory?

ext4_ioctl_set_lufid() (from commit "ext4: Add EXT4_IOC_SET_LUFID ioctl for
setting LUFID on directory entries") calls inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) without holding dir->i_rwsem, and then sleeps in
copy_from_user(), mnt_want_write_file(), and dquot_initialize() before
ext4_dirdata_set_lufid() takes inode_lock_nested(dir, I_MUTEX_PARENT).

CPU0                              CPU1
ext4_ioctl_set_lufid()
  inode_permission(MAY_WRITE...)
  copy_from_user()  (sleeps)
                                  vfs_fileattr_set()  (chattr +i)
                                    inode_lock(dir)
                                    dir->i_flags |= S_IMMUTABLE
                                    inode_unlock(dir)
  ext4_dirdata_set_lufid()
    inode_lock_nested(dir, ...)
    ... (no MAY_WRITE/IMMUTABLE
         re-check)
    ext4_delete_entry()  <-- mutates immutable dir

Under dir->i_rwsem, ext4_dirdata_set_lufid() checks IS_APPEND(dir) but
relies on the earlier unlocked inode_permission() call for MAY_WRITE,
MAY_EXEC, and IS_IMMUTABLE(dir).  Re-checking inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) after acquiring inode_lock_nested(dir, I_MUTEX_PARENT)
would close this window.

[ ... ]

> @@ -5132,6 +5135,12 @@ bool ext4_check_dx_root_test(struct inode *dir, struct dx_root *root)
>  }
>  EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_check_dx_root_test);
>  
> +struct dx_root_info *dx_get_dx_info_test(struct inode *dir, void *de_buf)
> +{
> +	return dx_get_dx_info(dir, de_buf);
> +}
> +EXPORT_SYMBOL_FOR_EXT4_TEST(dx_get_dx_info_test);

[Severity: Medium]
Can dx_get_dx_info() and get_dx_countlimit() (updated in commit "ext4:
refactor dx_root to support variable dirent sizes") return unaligned htree
pointers on a corrupted root block?

On filesystems with blocksize < 65536, ext4_rec_len_from_disk() returns the
raw 16-bit rec_len without enforcing 4-byte alignment, and neither
dx_get_dx_info() nor get_dx_countlimit() checks (rlen & 3) != 0 on '.' (or
on '..' in dx_get_dx_info()).  A crafted '.' rec_len such as 13 passes the
bounds checks and places the '..' entry, dx_root_info, dx_countlimit,
dx_entry[], and dx_tail at unaligned offsets even though none of those
structs are __packed, triggering UBSAN alignment faults or traps on
strict-alignment architectures.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203184938.422849-1-ablagodarenko@thelustrecollective.com?part=13

  reply	other threads:[~2026-12-03 23:15 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 18:49 [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 01/13] ext4: validate count against limit in ext4_dx_csum_verify/_set Artem Blagodarenko
2026-12-03 19:03   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 02/13] ext4: use the e2fsck limit for htree index nodes Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 03/13] ext4: replace ext4_dir_entry with ext4_dir_entry_2 Artem Blagodarenko
2026-12-03 19:33   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 04/13] ext4: add ext4_dir_entry_is_tail() Artem Blagodarenko
2026-12-03 18:49 ` [PATCH v15 05/13] ext4: add dirdata format definitions and access helpers Artem Blagodarenko
2026-12-03 19:59   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 06/13] ext4: refactor dx_root to support variable dirent sizes Artem Blagodarenko
2026-12-03 20:21   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 07/13] ext4: size the htree root from where its dx_entry array starts Artem Blagodarenko
2026-12-03 20:37   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage Artem Blagodarenko
2026-12-03 21:05   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 09/13] ext4: dirdata feature Artem Blagodarenko
2026-12-03 21:37   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Artem Blagodarenko
2026-12-03 22:02   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 11/13] ext4: add dirdata set/get helpers Artem Blagodarenko
2026-12-03 22:30   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Artem Blagodarenko
2026-12-03 23:01   ` sashiko-bot
2026-12-03 18:49 ` [PATCH v15 13/13] ext4: add KUnit tests for dirdata Artem Blagodarenko
2026-12-03 23:15   ` sashiko-bot [this message]
2026-12-03 23:15 ` [PATCH v15 00/13] Data in direntry (dirdata) feature, with KUnit tests 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=sashiko-outbox-248783@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=artem.blagodarenko@gmail.com \
    --cc=linux-ext4@vger.kernel.org \
    --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