All of lore.kernel.org
 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 08/13] ext4: rename ext4_dir_rec_len() and clarify dirdata usage
Date: Thu, 03 Dec 2026 21:05:10 +0000	[thread overview]
Message-ID: <sashiko-outbox-248664@kernel.org> (raw)
In-Reply-To: <20261203184938.422849-9-ablagodarenko@thelustrecollective.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: ext4_dir_entry_len() parses dirdata flags on
  non-dirdata filesystems
- [Medium] ext4: do_split() leaves unzeroed slack parsed as dirdata in
  add_dirent_to_buf()
- [Medium] ext4: dx_get_dx_info() omits 4-byte rec_len alignment check
- [Low] ext4: commit message omits API and behavioral changes in rename
  patch
- [Low] ext4: redundant ext4_dir_entry_len() walk in
  __ext4_check_dir_entry()
--

commit 2d5f99bd8296e9ea507c7610391e9c358e49b150
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>

> ext4: rename ext4_dir_rec_len() and clarify dirdata usage
>
> Rename ext4_dir_rec_len() to ext4_dirent_rec_len() to better
> reflect that it computes the record length for a directory
> entry based on the provided name length.
>
> Update the comment to clarify handling of dirdata-enabled
> directories and document the use of ext4_dir_entry_len()
> when dirdata is present.
>
> ext4_dir_entry_len() reads the dirdata of an entry, so the length of
> the entry that do_split() hands to add_dirent_to_buf() now depends on
> on-disk data.  add_dirent_to_buf() checks that this entry has room for
> the new one before anything is written, instead of overrunning the
> block when the directory is corrupted.
>
> Signed-off-by: Artem Blagodarenko <artem.blagodarenko@gmail.com>
> Assisted-by: Claude:claude-opus-5-5

[Severity: Low]
Should the commit message also describe the API and behavioral changes
bundled into this patch?  In addition to the rename and the check in
add_dirent_to_buf(), the diff widens ext4_dirent_rec_len()'s name_len
parameter from __u8 to unsigned int (avoiding 8-bit truncation when
name_len + dirdata >= 256), adds a dlen parameter to exported
ext4_find_dest_de(), and switches __ext4_check_dir_entry(),
dx_move_dirents(), dx_pack_dirents(), ext4_find_dest_de(), and
ext4_insert_dentry() from ext4_dir_rec_len() to ext4_dir_entry_len().

Also, the new comment in ext4_add_dirent_to_inline() says the dirdata
types are defined in the next patch, though commit "ext4: add dirdata
format definitions and access helpers" already defined them.

> diff --git a/fs/ext4/dir.c b/fs/ext4/dir.c
> index 43f4f2d451aa8..06d8d2eb52ea4 100644
> --- a/fs/ext4/dir.c
> +++ b/fs/ext4/dir.c
> @@ -89,16 +89,15 @@ int __ext4_check_dir_entry(const char *function, unsigned int line,
>  	bool fake = is_fake_dir_entry(de);
>  	bool has_csum = ext4_has_feature_metadata_csum(dir->i_sb);
>  
> -	if (unlikely(rlen < ext4_dir_rec_len(1, fake ? NULL : dir)))
> +	if (unlikely(rlen < ext4_dirent_rec_len(1, fake ? NULL : dir)))
>  		error_msg = "rec_len is smaller than minimal";
>  	else if (unlikely(rlen % 4 != 0))
>  		error_msg = "rec_len % 4 != 0";
> -	else if (unlikely(rlen < ext4_dir_rec_len(de->name_len,
> -							fake ? NULL : dir)))
> -		error_msg = "rec_len is too small for name_len";
>  	else if (unlikely(next_offset > size))
>  		error_msg = "directory entry overrun";
> -	else if (unlikely(next_offset > size - ext4_dir_rec_len(1,
> +	else if (unlikely(rlen < ext4_dir_entry_len(de, size, fake ? NULL : dir)))
> +		error_msg = "rec_len is too small for name_len";
> +	else if (unlikely(next_offset > size - ext4_dirent_rec_len(1,
>  						  has_csum ? NULL : dir) &&
>  			  next_offset != size))
>  		error_msg = "directory entry too close to block end";

[Severity: Low]
Does calling ext4_dir_entry_len() here validate anything beyond
ext4_dirent_rec_len(de->name_len, fake ? NULL : dir)?

By this point rlen % 4 == 0 and next_offset <= size are already verified.
Inside ext4_dirent_get_data_len(), any non-zero dirdata length satisfies
EXT4_BASE_DIR_LEN + de->name_len + dirdata <= rec_len (and out-of-bounds
records return 0), so rounding up to 4 bytes can never exceed the already
4-byte-aligned rlen.  Keeping ext4_dirent_rec_len() here avoids
re-decoding de->rec_len and running the READ_ONCE() dirdata walk on every
entry check (which ext4_find_dest_de() then repeats immediately).

> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index 32401150503eb..e34d91cd63ece 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h

[ ... ]

> @@ -4529,8 +4535,8 @@ static inline unsigned int ext4_dir_entry_len(struct ext4_dir_entry_2 *de,
>  	unsigned int rec_len = ext4_rec_len_from_disk(de->rec_len, blocksize);
>  	unsigned int dirdata = ext4_dirent_get_data_len(de, rec_len);
>  
> -	return max(ext4_dir_rec_len(de->name_len + dirdata, NULL),
> -		   ext4_dir_rec_len(de->name_len, dir));
> +	return max(ext4_dirent_rec_len(de->name_len + dirdata, NULL),
> +		   ext4_dirent_rec_len(de->name_len, dir));
>  }

[Severity: Medium]
Should ext4_dir_entry_len() skip ext4_dirent_get_data_len() when
EXT4_FEATURE_INCOMPAT_DIRDATA is not enabled?

On a non-dirdata filesystem, an entry with upper file_type bits set and a
padding byte after the name larger than its current rec_len gets
dirdata == 0 from ext4_dirent_get_data_len(), so it passes
__ext4_check_dir_entry() and dx_move_dirents() / dx_pack_dirents() copy
only its original length.  Once do_split() expands the last entry's
rec_len to the rest of the half-block, subsequent ext4_dir_entry_len()
calls see that byte as in-bounds and jump the entry length, annexing
uncopied or stale block bytes into the entry (or failing in
add_dirent_to_buf() with -EFSCORRUPTED).

Passing the superblock to ext4_dir_entry_len() and gating
ext4_dirent_get_data_len() on ext4_has_feature_dirdata(sb) would keep
non-dirdata filesystems from interpreting those bits.

[Severity: Medium]
Can dx_get_dx_info() (added in commit "ext4: refactor dx_root to support
variable dirent sizes") return an unaligned pointer into the htree root
block?

fs/ext4/namei.c:dx_get_dx_info() {
    ...
	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;
    ...
}

For blocksize < 65536, ext4_rec_len_from_disk() returns the raw 16-bit
value without masking the low two bits, and dx_get_dx_info() does not
check rlen % 4 != 0.  A '.' entry with an unaligned rec_len (such as 13
with name_len 1) passes ext4_dir_entry_len(de, blocksize, NULL) <= rlen,
shifting '..', dx_root_info, and the dx_entry array in dx_probe() to
unaligned offsets and causing unaligned 16-bit and 32-bit accesses on
non-packed structs.

> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index 78ee4936e6cc4..6050dbfd9f786 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c

[ ... ]

> @@ -1797,7 +1797,7 @@ static struct ext4_dir_entry_2 *dx_pack_dirents(struct inode *dir, char *base,
>  	while ((char*)de < base + blocksize) {
>  		next = ext4_next_entry(de, blocksize);
>  		if (de->inode && de->name_len) {
> -			rec_len = ext4_dir_rec_len(de->name_len, dir);
> +			rec_len = ext4_dir_entry_len(de, blocksize, dir);
>  			if (de > to)
>  				memmove(to, de, rec_len);
>  			to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize);

[ ... ]

> @@ -2035,9 +2036,29 @@ static int add_dirent_to_buf(handle_t *handle, struct ext4_filename *fname,
>  
>  	if (!de) {
>  		err = ext4_find_dest_de(dir, bh, bh->b_data,
> -					blocksize - csum_size, fname, &de);
> +					blocksize - csum_size, fname, &de, 0);
>  		if (err)
>  			return err;
> +	} else {
> +		/*
> +		 * do_split() hands over the last entry of a half.  Its length
> +		 * comes from on-disk dirdata, so check that the new entry fits
> +		 * before it is written.  do_split() has already packed this
> +		 * half and left it to us to dirty, so dirty it on failure too.
> +		 */
> +		unsigned int nlen = de->inode ?
> +			ext4_dir_entry_len(de, blocksize, dir) : 0;
> +		unsigned int rlen = ext4_rec_len_from_disk(de->rec_len,
> +							   blocksize);
> +
> +		if (nlen > rlen ||
> +		    rlen - nlen < ext4_dirent_rec_len(fname_len(fname), dir)) {
> +			EXT4_ERROR_INODE(dir,
> +				"no room for new entry in split block %llu",
> +				(unsigned long long)bh->b_blocknr);
> +			ext4_handle_dirty_dirblock(handle, dir, bh);
> +			return -EFSCORRUPTED;
> +		}
>  	}
>  	BUFFER_TRACE(bh, "get_write_access");
>  	err = ext4_journal_get_write_access(handle, dir->i_sb, bh,

[Severity: Medium]
Can add_dirent_to_buf() and ext4_insert_dentry() parse unzeroed stale
bytes left after dx_pack_dirents() as dirdata?

dx_pack_dirents() compacts entries in the first half without zeroing the
trailing slack space, and do_split() then extends the last entry's
rec_len to blocksize - csum_size.  If that entry has stray upper
file_type bits whose dirdata was previously ignored because its offset or
length exceeded the old rec_len, re-evaluating ext4_dir_entry_len() over
the expanded rec_len reads the unzeroed slack bytes as ddh_length.

That either makes add_dirent_to_buf() falsely report EXT4_ERROR_INODE and
return -EFSCORRUPTED, or causes ext4_insert_dentry() to split at the
bogus nlen and permanently attach stale block bytes to the entry as
phantom dirdata.

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

  reply	other threads:[~2026-12-03 21:05 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 [this message]
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-248664@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.