From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796338920; b=kaWeP/pDrRhJY0ySL0o9TfQAUdLCON1lzL6yion2LXzdmiWLPHozuu1XZACPCkL0V4cs hOzKdsJBRWJnvBAeyOiHhxGu2emMYDf/Wge1MjreE5H5X2iGAdFBnF9p/NU+JPhU3k8eC yjrxu/362T4oxFgQa5SOCZRByPE8OE3aFmSjU2ws4fkTCs/WigpTJi+oICXRibjlxOjFz BJoOU6Y6w47xF5iZKE9xD4SyVYH92B4AmEHYoSZ8o2ITJC6VASg2c35pw2NCKoSpQ9Rql 3/KNvs74UngWa75xK29HdgbZ6nj76qxfj4sOhbkh8OfjaloZWmgSMoCvPYsi+UtYGaQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796338920; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=R6BzFY0zfG90kukDpetJhyf3MIZ/73FXDO+ER38duuI=; b=HxnTg7NsUCr0b3DiE2y2Pn7Z6Ksw8l19DgC5v0wiATRxKf0LXBGoH4UjbTZDR2FtmxlC BZ/AxIocxQytgwBaQO31WqRisumGBc2XtahGZ1pinZGl6bH+Tuebmzi6nt3w/NkmJoG+F mD1FKZvOgowSX4pCGkz2x7Seo/Z2XN3U35BGteO5VpOLX43tEXreXVX9opPs9NqbLV1PT aUq4AE8cZWXWR5RmNEVGkYkywtoq0ALP2GtRZP9nixgaXX7VDOlYWXYTiSKzkQmG/ANl0 tQiR8mu/fSDC8KlWjQ2+PWGeYqO0f7MbjTtAnU+mq2/8cVAmm2r0atKu0GRN7t0URFA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jSmnvpZU; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7F5CA606A322 for ; Thu, 3 Dec 2026 23:02:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=jSmnvpZU Received: by smtp.kernel.org (Postfix) with ESMTPSA id CED521F000C2; Thu, 3 Dec 2026 23:01:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796338920; bh=R6BzFY0zfG90kukDpetJhyf3MIZ/73FXDO+ER38duuI=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=jSmnvpZUqsisGT0EHkw1arsljV/+aJdh38oCJu6wcN2OheQxwpZ5SQkY5OFZiFS4j hd84IaCct+Wb1IWeYNnGsKSeMee7kTC3Vrbz5ki4cm5vsmURiKqKYyGv5rp0aSWvLH afR4JU2p6s9r5GmLI7X8xyKK8OEkTwwjDTvsYkVlqS/2dudKOLjuWfhLh/jCq+FMOI QU+vOrczhs6fh36aFnSWIRZudTNkS+rdu7YhN1rt8RiGnfRp/FXs5bwGQfMsWA938M 1ObJyQ9/26S0jMpGGOjBFufeE/2FVlDAcwwROPXBZmExeqxOluZRVvKcLJLD2E3eGC iByZGrZNaKJww== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v15 12/13] ext4: Add EXT4_IOC_SET_LUFID ioctl for setting LUFID on directory entries Reply-To: sashiko-reviews@lists.linux.dev To: "Artem Blagodarenko" , linux-ext4@vger.kernel.org Cc: tytso@mit.edu In-Reply-To: <20261203184938.422849-13-ablagodarenko@thelustrecollective.com> References: <20261203184938.422849-1-ablagodarenko@thelustrecollective.com> <20261203184938.422849-13-ablagodarenko@thelustrecollective.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 23:01:59 +0000 X-Mailing-List: linux-ext4@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 *fil= p, [ ... ] > +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 =3D 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 =3D=3D 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 *d= ir, > + struct ext4_dirent_fid *dfid, > + struct ext4_filename *fname) [ ... ] > @@ -1419,11 +1419,18 @@ static void ext4_dirdata_set(struct ext4_dir_entr= y_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 =3D > + (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 *i= dmap, [ ... ] > +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 =3D 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 =3D 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 =3D 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 =3D 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 =3D ext4_add_fname(handle, dir, &old_fname, inode); > + else > + err2 =3D 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 */ > }; > =20 > +/* > + * 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 =3D=3D 0 is rejected with -EINVAL so callers cann= ot 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203184938.4228= 49-1-ablagodarenko@thelustrecollective.com?part=3D12