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 06/13] ext4: refactor dx_root to support variable dirent sizes
Date: Thu, 03 Dec 2026 20:21:17 +0000	[thread overview]
Message-ID: <sashiko-outbox-248620@kernel.org> (raw)
In-Reply-To: <20261203184938.422849-7-ablagodarenko@thelustrecollective.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: dx_get_dx_info() misses 4-byte alignment check on
  rec_len
- [Medium] ext4: dx_get_dx_info() offset shifts after make_indexed_dir()
  updates dotdot
- [Medium] ext4: ext4_dx_add_entry() mutates root before
  dx_get_dx_info()
- [Low] ext4: unnecessary dir parameter and dx_get_dx_info() call in
  dx_release()
- [Low] ext4: dx_probe() regresses ext4_warning_inode() to
  ext4_warning() with %d
--

commit 8c7de95a1909ec95132a5dca0d865a06d32ff26e
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>

ext4: refactor dx_root to support variable dirent sizes

This commit splits struct dx_root_info out of struct dx_root and adds
dx_get_dx_info() and ext4_dir_entry_len() so htree root blocks can
locate dx_root_info when '.' and '..' have variable sizes.

> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 8fc39c91b6ef8..edbe033b623e6 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -443,6 +443,46 @@ static inline int ext4_handle_dirty_dx_node(handle_t *handle,

[ ... ]

> +static struct dx_root_info *dx_get_dx_info(struct inode *dir, void *de_buf)
> +{
> +	unsigned int blocksize = dir->i_sb->s_blocksize;
> +	struct ext4_dir_entry_2 *de = de_buf;
> +	unsigned int off, rlen, need;
> +
> +	rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
> +	if (rlen < EXT4_BASE_DIR_LEN || rlen + EXT4_BASE_DIR_LEN > blocksize ||
> +	    ext4_dir_entry_len(de, blocksize, NULL) > rlen)
> +		return ERR_PTR(-EFSCORRUPTED);
> +	off = rlen;
> +
> +	de = (struct ext4_dir_entry_2 *)((char *)de_buf + off);
> +	rlen = ext4_rec_len_from_disk(de->rec_len, blocksize);
> +	if (rlen < EXT4_BASE_DIR_LEN || off + rlen > blocksize ||
> +	    ext4_dir_entry_len(de, blocksize, NULL) > rlen)
> +		return ERR_PTR(-EFSCORRUPTED);
> +	off += ext4_dir_entry_len(de, blocksize, NULL);

[Severity: Medium]
Can an unaligned '.' rec_len cause dx_get_dx_info() to return a
misaligned struct dx_root_info pointer?

dx_probe() passes the raw root block directly to dx_get_dx_info()
without running __ext4_check_dir_entry().  When blocksize is below
65536 and '.' has an unaligned rec_len (such as 13), rlen passes the
checks here, leaving '..', dx_root_info, and the dx_entry array
unaligned and causing alignment faults on strict-alignment
architectures.

Checking (rlen & EXT4_DIR_ROUND) would reject unaligned entries.  Also,
ext4_dir_entry_len(de, blocksize, NULL) is called twice on '..' and
could be cached in a local variable.

[ ... ]

> @@ -640,23 +680,31 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
>  	if (IS_ERR(frame->bh))
>  		return (struct dx_frame *) frame->bh;
>  
> -	root = (struct dx_root *) frame->bh->b_data;
> -	if (root->info.hash_version != DX_HASH_TEA &&
> -	    root->info.hash_version != DX_HASH_HALF_MD4 &&
> -	    root->info.hash_version != DX_HASH_LEGACY &&
> -	    root->info.hash_version != DX_HASH_SIPHASH) {
> -		ext4_warning_inode(dir, "Unrecognised inode hash code %u",
> -				   root->info.hash_version);
> +	info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frame->bh->b_data);
> +	if (IS_ERR(info))
> +		goto fail;
> +	if (info->info_length != sizeof(struct dx_root_info)) {
> +		ext4_warning_inode(dir, "corrupted htree: info_length %u",
> +				   info->info_length);
> +		goto fail;
> +	}
> +	if (info->hash_version != DX_HASH_TEA &&
> +	    info->hash_version != DX_HASH_HALF_MD4 &&
> +	    info->hash_version != DX_HASH_LEGACY &&
> +	    info->hash_version != DX_HASH_SIPHASH) {
> +		ext4_warning(dir->i_sb,
> +			"Unrecognised inode hash code %d for directory #%llu",
> +			info->hash_version, dir->i_ino);
>  		goto fail;
>  	}

[Severity: Low]
Was switching ext4_warning_inode() back to ext4_warning() with %d
intentional?  This drops current->comm from the log message, formats
unsigned u8 hash_version with %d instead of %u, and is inconsistent
with the surrounding ext4_warning_inode() calls in dx_probe().

[ ... ]

> @@ -783,7 +829,7 @@ dx_probe(struct ext4_filename *fname, struct inode *dir,
>  	return ret_err;
>  }
>  
> -static void dx_release(struct dx_frame *frames)
> +static void dx_release(struct inode *dir, struct dx_frame *frames)
>  {
>  	struct dx_root_info *info;
>  	int i;
> @@ -792,7 +838,18 @@ static void dx_release(struct dx_frame *frames)
>  	if (frames[0].bh == NULL)
>  		return;
>  
> -	info = &((struct dx_root *)frames[0].bh->b_data)->info;
> +	info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)frames[0].bh->b_data);
> +	if (IS_ERR(info)) {
> +		/* Cannot determine indirect_levels; release all frame buffers
> +		 * that may have been loaded to avoid leaking them. */
> +		for (i = 0; i < EXT4_HTREE_LEVEL; i++) {
> +			if (!frames[i].bh)
> +				break;
> +			brelse(frames[i].bh);
> +			frames[i].bh = NULL;
> +		}
> +		return;
> +	}

[Severity: Low]
Does dx_release() need the dir parameter or dx_get_dx_info() at all?

Every caller allocates frames[EXT4_HTREE_LEVEL] and zero-initializes it
via dx_probe() or make_indexed_dir().  Using the bounded loop over
EXT4_HTREE_LEVEL unconditionally avoids re-parsing the root block on
every htree release and avoids adding the dir argument to all callers.

[ ... ]

> @@ -2096,17 +2165,22 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
>  		return retval;
>  	}
>  
> -	root = (struct dx_root *) bh->b_data;
> -	if (!ext4_check_dx_root(dir, root)) {
> +	dot_de = (struct ext4_dir_entry_2 *)bh->b_data;
> +	if (!ext4_check_dx_root(dir, dot_de, &dotdot_de, &de)) {
>  		brelse(bh);
>  		return -EFSCORRUPTED;
>  	}
>  
> +	/* Find room for the hashing info before the directory is changed */
> +	dx_info = dx_get_dx_info(dir, dot_de);
> +	if (IS_ERR(dx_info)) {
> +		EXT4_ERROR_INODE(dir, "no room for htree root info");
> +		brelse(bh);
> +		return PTR_ERR(dx_info);
> +	}

[ ... ]

> @@ -2137,24 +2211,26 @@ static int make_indexed_dir(handle_t *handle, struct ext4_filename *fname,
>  		ext4_initialize_dirent_tail(bh2, blocksize);
>  
>  	/* Initialize the root; the dot dirents already exist */
> -	de = (struct ext4_dir_entry_2 *) (&root->dotdot);
> -	de->rec_len = ext4_rec_len_to_disk(
> -			blocksize - ext4_dir_rec_len(2, NULL), blocksize);
> -	memset (&root->info, 0, sizeof(root->info));
> -	root->info.info_length = sizeof(root->info);
> +	dotdot_de->rec_len =
> +		ext4_rec_len_to_disk(blocksize - le16_to_cpu(dot_de->rec_len),
> +				     blocksize);

[Severity: Medium]
Can expanding dotdot_de->rec_len here shift the offset that later
dx_get_dx_info() calls compute, corrupting the newly indexed directory?

ext4_dir_entry_len() calls ext4_dirent_get_data_len() even without the
dirdata feature.  If '..' has an upper bit set in file_type and a byte
>= 2 at offset 11, ext4_dirent_get_data_len() returns 0 while rec_len
is 12, so make_indexed_dir() writes dx_root_info at offset 24.

After dotdot_de->rec_len is expanded to cover the rest of the block,
ext4_dirent_get_data_len() accepts that same byte and later
dx_get_dx_info() calls resolve dx_root_info inside the dx_entry array.
Guarding dirdata parsing on the dirdata feature would avoid this.

[ ... ]

> @@ -2472,11 +2548,17 @@ static int ext4_dx_add_entry(handle_t *handle, struct ext4_filename *fname,
>  			/* Set up root */
>  			dx_set_count(entries, 1);
>  			dx_set_block(entries + 0, newblock);
> -			dxroot = (struct dx_root *)frames[0].bh->b_data;
> -			dxroot->info.indirect_levels += 1;
> +			info = dx_get_dx_info(dir, (struct ext4_dir_entry_2 *)
> +					      frames[0].bh->b_data);
> +			if (IS_ERR(info)) {
> +				err = PTR_ERR(info);
> +				brelse(bh2);
> +				goto journal_error;
> +			}
> +			info->indirect_levels += 1;

[Severity: Medium]
Can mutating entries in frames[0].bh before calling dx_get_dx_info()
leave the root block partially updated or shift info into entries[]?

Because '..' spans the rest of the root block and ext4_dir_entry_len()
parses dirdata unconditionally, stray upper bits in '..' file_type can
make ext4_dirent_get_data_len() read into entries[0].block.  Writing
newblock can change a byte that was zero during dx_probe() to non-zero.

dx_get_dx_info() then either returns -EFSCORRUPTED after frames[0].bh
was modified and dir->i_size grown by ext4_append(), or returns a
shifted pointer so info->indirect_levels += 1 corrupts an index entry.

Calling dx_get_dx_info() before ext4_append() and before mutating
entries, as make_indexed_dir() does, would avoid both.

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

  reply	other threads:[~2026-12-03 20:21 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 [this message]
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
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-248620@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