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 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries
Date: Thu, 03 Dec 2026 23:01:59 +0000	[thread overview]
Message-ID: <sashiko-outbox-248770@kernel.org> (raw)
In-Reply-To: <20261203184938.422849-13-ablagodarenko@thelustrecollective.com>

Thank you for your contribution! Sashiko AI review found 11 potential
issues to consider:
- [Critical] ext4: rollback in ext4_dirdata_set_lufid() reuses stale
  buffer_head
- [Critical] ext4: casefold ignorable code points bypass dot/dotdot
  check in set_lufid
- [High] ext4: TOCTOU race on directory permissions and IS_IMMUTABLE in
  set_lufid
- [Medium] ext4: ext4_dirdata_set_lufid() drops non-LUFID/CFHASH dirdata
  records
- [Medium] ext4: ext4_dirdata_set_lufid() sets LUFID without filetype
  feature
- [Medium] ext4: ext4_dirdata_set_lufid() skips in-place update and
  over-reserves credits
- [Medium] ext4: EXT4_IOC_SET_LUFID UAPI lacks extensibility, alignment,
  and read/clear
- [Low] ext4: commit bundles unrelated fixes and cleanups absent from
  changelog
- [Low] ext4: ext4_dirdata_set_lufid() frees fscrypt buffer with raw
  kfree()
- [Low] ext4: ext4_dirdata_set_lufid() ignores ext4_mark_inode_dirty()
  error
- [Low] ext4: ext4_dirdata_set_lufid() repeats lookup and filename setup
  unnecessarily
--

commit 7fa00ca4c562c2ac8bca881f8348e7b8059dfb52
Author: Artem Blagodarenko <artem.blagodarenko@gmail.com>

ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries

This commit adds the EXT4_IOC_SET_LUFID ioctl along with
ext4_ioctl_set_lufid() and ext4_dirdata_set_lufid() to attach LUFID
dirdata to an existing directory entry by deleting and re-adding it.

> diff --git a/fs/ext4/ioctl.c b/fs/ext4/ioctl.c
> index 83630cd40712c..a6baba5968f47 100644
> --- a/fs/ext4/ioctl.c
> +++ b/fs/ext4/ioctl.c
> @@ -1563,6 +1563,90 @@ static int ext4_ioctl_set_tune_sb(struct file *filp,

[ ... ]

> +static long ext4_ioctl_set_lufid(struct file *filp, unsigned long arg)
> +{

[ ... ]

> +	/* This ioctl mutates directory entries; merely having the directory
> +	 * open (which only ever requires read access) is not enough.
> +	 * MAY_EXEC is required for entry lookup; MAY_WRITE for modification. */
> +	err = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC);
> +	if (err)
> +		return err;
> +
> +	/* Copy arguments from user space */
> +	if (copy_from_user(&lufid_args, (struct ext4_set_lufid __user *)arg,
> +			   sizeof(lufid_args)))
> +		return -EFAULT;

[Severity: High]
Can a concurrent chattr +i or chmod/chown race with inode_permission()
here and be bypassed?

inode_permission() checks directory write/exec permissions and
IS_IMMUTABLE(dir) without holding dir->i_rwsem, before the sleepable
copy_from_user() and mnt_want_write_file().  ext4_dirdata_set_lufid()
later acquires inode_lock_nested(dir, I_MUTEX_PARENT) and checks
IS_APPEND(dir), but never re-checks inode_permission() or
IS_IMMUTABLE(dir).

CPU0 (ioctl)                      CPU1 (chattr +i / chmod)
ext4_ioctl_set_lufid()
  inode_permission(dir, ...)
  copy_from_user()  [stalls]
                                  vfs_fileattr_set()
                                    inode_lock(dir)
                                    set S_IMMUTABLE on dir
                                    inode_unlock(dir)
  mnt_want_write_file()
  ext4_dirdata_set_lufid()
    inode_lock_nested(dir, ...)
    ext4_delete_entry()           <-- mutates immutable dir

A user with initial access can stall copy_from_user() (for example via
userfaultfd) while S_IMMUTABLE is set or permissions are revoked, then
resume and mutate the directory.  Moving inode_permission(idmap, dir,
MAY_WRITE | MAY_EXEC) into ext4_dirdata_set_lufid() under
inode_lock_nested(dir, I_MUTEX_PARENT) closes the window and ensures
the exported helper enforces directory permissions itself.

[ ... ]

> +	/* '.' and '..' are not ordinary entries -- they must stay the first
> +	 * two entries in the directory's first block, so they can't go
> +	 * through the general delete+re-add path this ioctl uses */
> +	if (!strcmp(lufid_args.esl_name, ".") || !strcmp(lufid_args.esl_name, ".."))
> +		return -EINVAL;

[Severity: Critical]
Can a casefolded name with Unicode default-ignorable code points bypass
this strcmp() check and match "." or ".."?

In a casefolded directory, utf8_casefold() strips default-ignorable code
points such as U+00AD (soft hyphen), so "." or ".." followed by U+00AD
passes strcmp() here but still matches "." or ".." in block 0 via
ext4_find_entry().  Matching "." hits EXT4_ERROR_INODE() on ino ==
dir->i_ino on a clean filesystem, triggering emergency read-only remount
or panic.

Matching ".." proceeds to ext4_delete_entry(), where
ext4_generic_delete_entry() merges ".." into "." and zeroes de->rec_len.
In an HTree directory where dotdot->rec_len spans blocksize - 12, that
wipes the entire dx_root block; in a linear directory, it relocates ".."
out of slot 2.  Checking whether the matched dirent de is "." or ".." in
ext4_dirdata_set_lufid() avoids both.

> diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
> index ae58ce5120e1e..f9250407cedbc 100644
> --- a/fs/ext4/namei.c
> +++ b/fs/ext4/namei.c
> @@ -1372,9 +1372,9 @@ bool ext4_dirent_hash_get(struct ext4_dir_entry_2 *de, struct inode *dir,
> -static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> -			     struct ext4_dirent_fid *dfid,
> -			     struct ext4_filename *fname)
> +static int ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> +			    struct ext4_dirent_fid *dfid,
> +			    struct ext4_filename *fname)

[ ... ]

> @@ -1419,11 +1419,18 @@ static void ext4_dirdata_set(struct ext4_dir_entry_2 *de, struct inode *dir,
> -			/* Compatibility: store hash inline after filename */
> -			if (EXT4_BASE_DIR_LEN + data_offset +
> -			    sizeof(struct ext4_dir_entry_hash) > rec_len) {
> +			/* Compatibility: store hash inline after filename.
> +			 * EXT4_DIRENT_HASHES places the hash at the
> +			 * 4-byte-aligned offset (8 + name_len + 3) & ~3, which
> +			 * may be less than EXT4_BASE_DIR_LEN + data_offset when
> +			 * name_len is a multiple of 4 (gap byte counted twice).
> +			 * Use the same aligned offset for the bounds check. */
> +			unsigned int hash_off =
> +				(EXT4_BASE_DIR_LEN + de->name_len + 3) & ~3;
> +
> +			if (hash_off + sizeof(struct ext4_dir_entry_hash) > rec_len) {
>  				EXT4_ERROR_INODE(dir, "Can not insert dhash");
> -				return;
> +				return -EIO;
>  			}

[Severity: Low]
This isn't a bug, but the commit bundles several unrelated changes that
are not mentioned in the changelog: the legacy inline-hash bounds-check
fix and void-to-int conversion of ext4_dirdata_set() and
ext4_insert_dentry_data(), caching dot_rec_len in ext4_init_dirblock(),
simplifying kmalloc/memcpy in ext4_rename(), and comments in
ext4_dirent_rec_len() and ext4_init_new_dir_data().  Splitting these out
or folding them into the earlier patches in the series would make the
history easier to follow and bisect.

[ ... ]

> @@ -4685,6 +4718,280 @@ static int ext4_rename2(const struct mnt_idmap *idmap,

[ ... ]

> +int ext4_dirdata_set_lufid(const struct mnt_idmap *idmap, struct inode *dir,
> +			   const char *filename, int namelen,
> +			   struct ext4_dentry_param *edp)
> +{

[ ... ]

> +	if (!ext4_has_feature_dirdata(sb))
> +		return -EOPNOTSUPP;

[Severity: Medium]
Should this also require ext4_has_feature_filetype(sb)?

ext4_feature_set_ok() allows mounting a filesystem with dirdata enabled
and filetype disabled.  In that configuration, ext4_dirdata_set() still
sets EXT4_DIRENT_LUFID in de->file_type (the high byte of legacy 16-bit
name_len), while ext4_setent() skips all dirdata maintenance when
filetype is off, leaving stale LUFIDs attached to the wrong inode on
later rename overwrites or exchanges.

[ ... ]

> +	handle = ext4_journal_start(dir, EXT4_HT_DIR,
> +				    3 * EXT4_DATA_TRANS_BLOCKS(sb) +
> +				    2 * EXT4_INDEX_EXTRA_TRANS_BLOCKS);

[Severity: Medium]
Is it necessary to unconditionally reserve 105 journal credits (with
extents and quotas) and always delete and re-add the entry instead of
attempting an in-place LUFID update first?

ext4_setent() already updates LUFID in place via ext4_dirent_drop_lufid()
and ext4_dirent_add_lufid() when de->rec_len has room.  Always deleting
and calling ext4_add_fname() causes O(N * B) linear rescans from block 0
when tagging entries across B blocks in a non-indexed directory, triggers
avoidable HTree leaf splits on hash-collision chains even for same-size
LUFID updates, and consumes excessive JBD2 credit budget under concurrent
metadata workloads.

[ ... ]

> +	/*
> +	 * Look up the entry again inside the handle and check that it still
> +	 * refers to the same inode (an unlink and create could have run in
> +	 * between).
> +	 */
> +	bh = ext4_find_entry(dir, &d_name, &de, &inlined);

[Severity: Low]
Why is ext4_find_entry() repeated here after releasing bh from the first
lookup?

inode_lock_nested(dir, I_MUTEX_PARENT) is held continuously across both
lookups, so no concurrent unlink or create on dir can run in between;
keeping bh from the first lookup (as __ext4_unlink() and ext4_rename()
do) avoids repeating the directory search under an open JBD2 handle.
Similarly, ext4_fname_setup_filename() is called twice below on the same
add_name only to hold different dfid pointers, whereas a single
ext4_filename can swap fname.dfid to old_snap.fid on rollback.

[ ... ]

> +	/* Copy the old LUFID so a failed re-add can restore it */
> +	err = ext4_lufid_snapshot(de, sb->s_blocksize, &old_snap);
> +	if (err)
> +		goto out_brelse;

[Severity: Medium]
Does deleting and re-adding the entry here silently drop non-LUFID,
non-CFHASH dirdata records such as EXT4_DIRENT_INO64 or 0x80?

Only EXT4_DIRENT_LUFID is snapshotted, and ext4_dirdata_set() only writes
LUFID and CFHASH, so any other dirdata records present on de are lost on
both success and rollback.  Preserving trailing records (as ext4_setent()
does) or rejecting entries with unsupported dirdata flags with
-EOPNOTSUPP would avoid silent metadata loss.

[ ... ]

> +	err = ext4_add_fname(handle, dir, &new_fname, inode);
> +	if (err) {
> +		/*
> +		 * Put the old entry back into the space it was deleted from.
> +		 * A new lookup by name could pick another block of the same
> +		 * hash, which may be full.  An inline directory has only the
> +		 * one buffer, and the space is there.
> +		 */
> +		if (inlined)
> +			err2 = ext4_add_fname(handle, dir, &old_fname, inode);
> +		else
> +			err2 = add_dirent_to_buf(handle, &old_fname, dir,
> +						 inode, NULL, bh);

[Severity: Critical]
Can this rollback corrupt the directory by writing old_fname into a stale
bh after ext4_add_fname() has already transformed that block?

If dir is a 1-block linear directory with dir_index enabled and the
larger LUFID entry does not fit in block 0 (bh), ext4_add_fname() calls
make_indexed_dir() on bh.  make_indexed_dir() moves existing entries to
block 1, converts bh into the HTree dx_root block (setting
dotdot_de->rec_len to blocksize - 12 and EXT4_INODE_INDEX on dir), and
then calls do_split().  If do_split() fails (for example with -ENOSPC or
-EDQUOT), make_indexed_dir() returns an error with bh permanently
converted to dx_root:

- Without metadata_csum, add_dirent_to_buf() treats the dx_root_info and
  dx_entry[] area as slack space in dotdot_de, overwrites the HTree root
  with old_fname, and returns 0, detaching every entry moved to block 1.
- With metadata_csum, __ext4_check_dir_entry() rejects dotdot_de as
  overrunning blocksize - csum_size, firing EXT4_ERROR_INODE() and
  failing rollback so the directory entry is permanently lost and inode
  is orphaned.

The same staleness occurs in an already-indexed directory if
ext4_dx_add_entry() commits a leaf split in do_split() and then
add_dirent_to_buf() fails: rollback writes old_fname into the original bh
even when its hash now belongs in the new split block bh2, leaving the
entry unreachable by lookup while readdir still lists it.

[ ... ]

> +	inode_set_ctime_current(dir);
> +	inode_inc_iversion(dir);
> +	ext4_mark_inode_dirty(handle, dir);

[Severity: Low]
Should the return value of ext4_mark_inode_dirty() be checked, or can
these three lines be removed?

Every success path of ext4_add_fname() (add_dirent_to_buf() and
ext4_try_add_inline_entry()) already updates dir's ctime and iversion and
calls ext4_mark_inode_dirty(handle, dir) with error checking, so this
repeats the work and silently discards any error.

[ ... ]

> +	kfree(stored.name);

[Severity: Low]
Should the encrypted branch free stored with fscrypt_fname_free_buffer()
instead of raw kfree()?

When IS_CASEFOLDED(dir) && IS_ENCRYPTED(dir), stored is allocated via
fscrypt_fname_alloc_buffer(), so freeing it directly with kfree() relies
on fscrypt's internal allocation strategy.

> diff --git a/include/uapi/linux/ext4.h b/include/uapi/linux/ext4.h
> index 9c683991c32f9..5d5c8c3df2a43 100644
> --- a/include/uapi/linux/ext4.h
> +++ b/include/uapi/linux/ext4.h

[ ... ]

> @@ -92,6 +93,19 @@ struct move_extent {
>  	__u64 moved_len;	/* moved block length */
>  };
>  
> +/*
> + * Structure for EXT4_IOC_SET_LUFID
> + * Sets LUFID on a directory entry
> + * Called on parent directory with filename and LUFID data as arguments
> + */
> +struct ext4_set_lufid {
> +	__u8 esl_name_len;	/* length of filename, NOT including NUL terminator
> +				 * (valid range: 1-255, matching EXT4_NAME_LEN) */
> +	char  esl_name[255 + 1]; /* filename (NUL-terminated) */
> +	__u8 esl_data_len;	/* length of LUFID data (1-48) */
> +	char  esl_data[255]; /* LUFID data (raw bytes) */
> +};

[Severity: Medium]
Does struct ext4_set_lufid have several UAPI layout and extensibility
issues that will be difficult to fix once merged?

Unlike struct fsuuid and struct ext4_tune_sb_params in this header,
struct ext4_set_lufid has no flags or reserved fields and has an
unaligned 513-byte size.  esl_data uses signedness-dependent char[255]
instead of __u8 for raw bytes, yet esl_data_len is capped at 48 bytes
while the 207 unused tail bytes (and unused esl_name bytes) are never
checked for zero, preventing future reuse.

In addition, esl_data_len == 0 is rejected with -EINVAL so callers cannot
clear an existing LUFID, arbitrary lengths from 1 to 48 bytes are
accepted without validating 16-byte struct ext4_fid alignment, and there
is no UAPI to read LUFID data back from userspace.

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

  reply	other threads:[~2026-12-03 23:02 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 [this message]
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-248770@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.