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=1796335324; b=g5OQH6i7i6TC3H1qe0fIH6BYVoh1px2Ax4MZWx+9JMc/oDwRPlkZLShkhp9WEZrqztat 40jxMwmiT4aS813IH61CKF4r8rd9kRdhauZpz3PzmzxIxis9YuDu9vhy4yUfDtPOEcK4h OQj76hvNXy6XZJE9SCSGtifzzWHMwKcizq3yAj6qxA+i1s85fK+CLKBtD3vNpwziyR4d7 mVxvUD+89CE4bm926TsBM0MWYJUCd2iMlBoTXe6Zs0X2MR3OWhl5C2U4M6C5ZDK+lCFPa 32ZRtrs2ptdGq6lE00Ct2n8YN2N7kikK905ND0p00kGz2dx2l8WgYO+8DVC+X5zTPKw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796335324; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To:Cc: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=w9vfz+QoXZBUIXYG4JmkmsaS0G6SLgSB4Qpt9Ik2T3c=; b=tjuCO6QhTZ2faeUfjDb20P3/vXtCLLneLADCJdsE81wfD334kg77V+2MsWx39XE9jT2r qqj2HF26/Hm+6rnTtwEllHBMCr40+iFu6cK2l4I1FB9tOGh2RMkUVnswQmxpFNCtfcpmB AQgTIQMMaNsXHEyrX2vHyZwe/7atkRBiEl+kOAuj7vixdXVRTge4uB7LaL+tvgjaBVD16 zOg4aFTp4IWAdbNQboNWnKFJGsvbyoLYCfCZhjfRVsfMYAMmBRkjmckxJHMh/hy57yr1q /+JVyiKERlc4O7LHwq4nt+w5JgL7VOdgQ1/+11ZzDhAx+1yPb2cMIFZObX3crYVO3sA== 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=ncumAh+c; 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 84360606A32C for ; Thu, 3 Dec 2026 22:02:04 +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=ncumAh+c Received: by smtp.kernel.org (Postfix) with ESMTPSA id E4BD21F000CE; Thu, 3 Dec 2026 22:02:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796335324; bh=w9vfz+QoXZBUIXYG4JmkmsaS0G6SLgSB4Qpt9Ik2T3c=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=ncumAh+ckkfM3sdmKeDmIUDQjbHdeWdcOhevNmIMFTWSGr/l6HO34Qw3/EvqHyMZh UGie8Xu6RZYWKccd0YTUPc/3ZV3QZJ/NzGbuMFYIHlBY2HkHkdo2sroHG+HDmvJ2A2 wZ2H6fwB64mQVaa/1I4sYYGHcbnmAX95XR69dhVQl03If1+uxRjYTuX5DBv+fojSJi pd+XllY9VAloK7Q0IUceZYmF7qyzEQHZifcyFmBRmBiKz2LHKDFSEpu/fMQHy1uZiK w7YmfFkuNRlsvZB2w4r8liMeRwphUhv8gwpCvZ9KxqFPzHpQy+c4TEhF68kWAhRHmF fWx2RYZnCW6Vg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v15 10/13] ext4: add dirdata LUFID support for directory entry rename Reply-To: sashiko-reviews@lists.linux.dev To: "Artem Blagodarenko" , linux-ext4@vger.kernel.org Cc: tytso@mit.edu In-Reply-To: <20261203184938.422849-11-ablagodarenko@thelustrecollective.com> References: <20261203184938.422849-1-ablagodarenko@thelustrecollective.com> <20261203184938.422849-11-ablagodarenko@thelustrecollective.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 22:02:03 +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 8 potential issues to consider: - [Medium] ext4: unnecessary kmalloc() and copy of LUFID wrapper in ext4_rename() - [Medium] ext4: TOCTOU double-fetch of dirent fields in rename LUFID helpers - [Medium] ext4: stale dot-dot LUFID left by ext4_rename_dir_finish() on dir move - [Medium] ext4: ext4_setent() ignores ext4_dirent_add_lufid() failure and drops LUFID - [Low] ext4: inconsistent EXT4_FEATURE_INCOMPAT_FILETYPE gating in rename dirdata - [Low] ext4: dead EXT4_DIRENT_LUFID masking in ext4_rename() and ext4_cross_rename() - [Low] ext4: undocumented __ext4_link() cleanup mixed into rename patch - [Low] ext4: explicit GFP_NOFS allocations inside active transaction in rename -- commit e5f6d4f2d5e4add0a5ae9ac53340b2594e4b354f Author: Artem Blagodarenko ext4: add dirdata LUFID support for directory entry rename This commit extends ext4_setent() to replace or drop dirdata LUFID records while preserving CFHASH, and snapshots source LUFIDs in ext4_rename() and ext4_cross_rename() so they travel with the renamed inode. > diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c > index 12ffd34cf46d0..8a1c292ddd2f4 100644 > --- a/fs/ext4/namei.c > +++ b/fs/ext4/namei.c > @@ -3467,11 +3467,6 @@ int __ext4_link(struct inode *dir, struct inode *i= node, > if (IS_DIRSYNC(dir)) > ext4_handle_sync(handle); > =20 > - /* Fast-commit does not record dirdata in its journal format; force a > - * full journal commit so the LUFID survives crash+replay. */ > - if (ext4_dentry_get_fid(dir->i_sb, dentry ? dentry->d_fsdata : NULL)) > - ext4_fc_mark_ineligible(dir->i_sb, EXT4_FC_REASON_DIRDATA, handle); > - > inode_set_ctime_current(inode); [Severity: Low] This removes the duplicate ext4_fc_mark_ineligible() check from __ext4_link() that was added in commit "ext4: dirdata feature", but neither the subject nor the changelog mentions it. Should this cleanup be folded back into that earlier commit or noted in the commit message? [ ... ] > @@ -3645,8 +3640,81 @@ static int ext4_rename_dir_finish(handle_t *handle= , struct ext4_renament *ent, > return 0; > } [Severity: Medium] Does ext4_rename_dir_finish() leave a stale LUFID on ".." when a directory is moved to a different parent? In ext4_rename() and ext4_cross_rename(), ext4_rename_dir_finish() updates ent->parent_de->inode to the new parent's inode number without updating or clearing EXT4_DIRENT_LUFID on ent->parent_de. On a dirdata filesystem with pre-existing ".." LUFIDs (such as an ldiskfs volume), the old parent's LUFID remains attached to the new parent's inode number. Handling HTree directories here will also need to account for dx_root_info sitting immediately after ".." if its length changes. > +static void ext4_dirent_drop_lufid(struct ext4_dir_entry_2 *de, > + unsigned int rec_len) > +{ > + unsigned int off =3D EXT4_BASE_DIR_LEN + de->name_len + 1; > + unsigned int dlen, end, llen; > + > + if (!(de->file_type & EXT4_DIRENT_LUFID)) > + return; > + dlen =3D ext4_dirent_get_data_len(de, rec_len); > + if (!dlen) { > + de->file_type &=3D EXT4_FT_MASK; > + return; > + } > + /* ext4_dirent_get_data_len() checked every record against rec_len */ > + llen =3D *((u8 *)de + off); > + end =3D EXT4_BASE_DIR_LEN + de->name_len + dlen; > + memmove((char *)de + off, (char *)de + off + llen, end - off - llen); > + de->file_type &=3D ~EXT4_DIRENT_LUFID; > +} [ ... ] > +static bool ext4_dirent_add_lufid(struct inode *dir, > + struct ext4_dir_entry_2 *de, > + unsigned int rec_len, > + const struct ext4_dirent_fid *fid) > +{ > + unsigned int off =3D EXT4_BASE_DIR_LEN + de->name_len + 1; > + unsigned int flen =3D fid->df_header.ddh_length; > + unsigned int dlen =3D ext4_dirent_get_data_len(de, rec_len); > + unsigned int end; > + > + if ((de->file_type & ~EXT4_FT_MASK) && !dlen) > + return false; > + /* without a CFHASH record the hash sits right after the name */ > + if (ext4_hash_in_dirent(dir) && !(de->file_type & EXT4_DIRENT_CFHASH)) > + return false; > + end =3D dlen ? EXT4_BASE_DIR_LEN + de->name_len + dlen : off; > + if (end + flen > rec_len) > + return false; > + memmove((char *)de + off + flen, (char *)de + off, end - off); > + *((char *)de + EXT4_BASE_DIR_LEN + de->name_len) =3D 0; > + memcpy((char *)de + off, fid, flen); > + de->file_type |=3D EXT4_DIRENT_LUFID; > + return true; > +} [ ... ] > @@ -3656,8 +3724,33 @@ static int ext4_setent(handle_t *handle, struct ex= t4_renament *ent, > if (retval) > return retval; > ent->de->inode =3D cpu_to_le32(ino); > - if (ext4_has_feature_filetype(ent->dir->i_sb)) > - ent->de->file_type =3D file_type; > + if (ext4_has_feature_filetype(ent->dir->i_sb)) { > + if (ext4_has_feature_dirdata(ent->dir->i_sb)) { > + unsigned int rec_len =3D > + ext4_rec_len_from_disk(ent->de->rec_len, > + ent->dir->i_sb->s_blocksize); > + unsigned int off =3D > + EXT4_BASE_DIR_LEN + ent->de->name_len + 1; > + struct ext4_dirent_fid *fid =3D > + (struct ext4_dirent_fid *)((char *)ent->de + off); > + > + if (src_fid && > + (ent->de->file_type & EXT4_DIRENT_LUFID) && > + ext4_dirent_get_data_len(ent->de, rec_len) && > + fid->df_header.ddh_length =3D=3D > + src_fid->df_header.ddh_length) { > + memcpy(fid, src_fid, > + src_fid->df_header.ddh_length); [Severity: Medium] Can concurrent writes to the directory buffer cause an unsigned underflow or out-of-bounds access in these three helpers? ext4_dirent_get_data_len() and ext4_lufid_snapshot() both use READ_ONCE() to guard against the directory buffer changing under them, but these helpers re-read on-disk fields after validation: - In ext4_dirent_drop_lufid(), llen and de->name_len are re-read after ext4_dirent_get_data_len(). If the on-disk LUFID length grows past end - off, end - off - llen underflows unsigned int to ~4 GiB in memmove(). - In ext4_dirent_add_lufid(), de->name_len is read separately for off, end, and the NUL write, allowing end < off (underflowing end - off in memmove()) or a NUL write past rec_len. - In ext4_setent(), fid->df_header.ddh_length is re-fetched after ext4_dirent_get_data_len() without checking off + src_fid->df_header.ddh_length <=3D rec_len, allowing memcpy() to write past rec_len. A concurrent writer to the buffer (such as a corrupted image with cross-linked directory blocks or writes to a mounted block device) could corrupt kernel memory. Reading name_len and the LUFID length once with READ_ONCE() and bounds-checking against rec_len before memmove()/memcpy() would close the race. > + } else { > + ext4_dirent_drop_lufid(ent->de, rec_len); > + if (src_fid) > + ext4_dirent_add_lufid(ent->dir, ent->de, > + rec_len, src_fid); > + } [Severity: Medium] Should ext4_setent() silently drop the source LUFID when ext4_dirent_add_lufid() returns false? Because ext4_insert_dentry_data() trims rec_len to ext4_dir_entry_len() when another entry is inserted after it, any non-tail entry created without a LUFID has at most 3 bytes of padding and cannot fit a 17-byte or larger LUFID in place. When ext4_rename() overwrites such an entry, ext4_setent() ignores the false return and ext4_rename_delete() then removes old.de, permanently losing the LUFID without returning an error (the same happens in ext4_cross_rename() and on RENAME_WHITEOUT rollback after compaction). Since ext4_rename() already reserves entry-addition credits, could it delete and re-add the destination entry when the LUFID does not fit in place? [ ... ] > @@ -3793,6 +3887,69 @@ static struct inode *ext4_whiteout_for_rename(cons= t struct mnt_idmap *idmap, [ ... ] > + if (dlen <=3D sizeof(snap->buf)) { > + memcpy(snap->buf, disk_fid, dlen); > + snap->fid =3D (struct ext4_dirent_fid *)snap->buf; > + } else { > + snap->fid =3D kmemdup(disk_fid, dlen, GFP_NOFS); [Severity: Low] Can this kmemdup() (and the kmalloc() in ext4_rename()) use GFP_KERNEL instead of GFP_NOFS? Both run inside an active transaction handle, where start_this_handle() and ext4_get_nojournal() already set memalloc_nofs_save() for the lifetime of the handle. Also, because old.de is already looked up before starting the transaction, snapshotting before ext4_whiteout_for_rename() would avoid creating and then orphaning a whiteout inode on -ENOMEM. [ ... ] > @@ -3926,29 +4086,67 @@ static int ext4_rename(const struct mnt_idmap *id= map, struct inode *old_dir, [ ... ] > + if (ext4_has_feature_dirdata(old.dir->i_sb)) { > + /* Snapshot before whiteout's ext4_setent() clears the LUFID > + * flag in old.de; the snapshot is also needed for the !new.bh > + * path below where we must carry the LUFID to the new entry. */ > + retval =3D ext4_lufid_snapshot(old.de, > + old.dir->i_sb->s_blocksize, > + &old_snap); > + if (retval) > + goto end_rename; [Severity: Low] Should this check (and the matching one in ext4_cross_rename()) also gate on ext4_has_feature_filetype(), or should mount reject DIRDATA without FILETYPE? ext4_setent() nests its dirdata handling inside ext4_has_feature_filetype(), while ext4_rename() and ext4_cross_rename() only check ext4_has_feature_dirdata(). If a filesystem is mounted with DIRDATA set and FILETYPE clear, rename will snapshot bogus LUFIDs and propagate them on the !new.bh path while ext4_setent() skips dirdata updates on overwrite and cross-rename. > + if (!old_snap.fid) > + old_file_type &=3D ~EXT4_DIRENT_LUFID; > + } [Severity: Low] Is clearing EXT4_DIRENT_LUFID from old_file_type here (and from old_de_file_type and new_file_type in ext4_cross_rename()) dead code? ext4_setent() unconditionally masks its file_type argument with EXT4_FT_MASK, so any upper dirdata bits in the parameter are ignored. > + /* Allocate the LUFID wrapper for ext4_add_entry() before any > + * destructive changes to the old entry. An OOM failure after > + * ext4_setent() has already compacted the extensions would trigger > + * the lossy resetent path; pre-allocating here avoids that. */ > + if (!new.bh && old_snap.fid) { > + unsigned fid_size =3D old_snap.fid->df_header.ddh_length; > + > + edp =3D kmalloc(offsetof(struct ext4_dentry_param, > + edp_dfid) + fid_size, GFP_NOFS); > + if (!edp) { > + retval =3D -ENOMEM; > + goto end_rename; > + } > + edp->edp_magic =3D EXT4_LUFID_MAGIC; > + memcpy(&edp->edp_dfid, old_snap.fid, fid_size); > + } [ ... ] > if (!new.bh) { > + new.dentry->d_fsdata =3D edp; > retval =3D ext4_add_entry(handle, new.dentry, old.inode); > + new.dentry->d_fsdata =3D NULL; > if (retval) > goto end_rename; [Severity: Medium] Does this kmalloc() defeat the inline stack buffer in ext4_lufid_snap on every non-overwriting rename of a LUFID-bearing entry? When !new.bh && old_snap.fid, ext4_rename() allocates edp and copies old_snap.fid a second time while holding an active JBD2 transaction handle and parent directory locks, only for static helper ext4_add_entry() -> __ext4_add_entry() to unwrap it straight back to struct ext4_dirent_fid * via ext4_dentry_get_fid(). This adds a per-rename slab allocation and an avoidable -ENOMEM failure path. Passing old_snap.fid directly to __ext4_add_entry() (after the fscrypt_is_nokey_name() check) or embedding edp_magic in struct ext4_lufid_snap would avoid the allocation and extra copy. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203184938.4228= 49-1-ablagodarenko@thelustrecollective.com?part=3D10