Linux EXT4 FS development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Alan <3164729207@qq.com>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH v4] ext4: restore mode precisely when ACL chmod fails
Date: Thu, 03 Dec 2026 09:48:11 +0000	[thread overview]
Message-ID: <sashiko-outbox-247499@kernel.org> (raw)
In-Reply-To: <tencent_DEB80DE95137BB46140E9E6CB67DE0180008@qq.com>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] ext4: split ACL and i_mode updates across separate JBD2
  transactions
- [High] ext4: ext4_setattr() can persist uid/gid with uncleared setid
  bits on disk
- [Medium] ext4: ext4_acl_chmod() ignores ext4_journal_stop() error
  return
- [Medium] ext4: ext4_setattr() bypasses inode_set_ctime_deleg() for
  ATTR_CTIME_SET
- [Medium] ext4: ext4_setattr() unconditionally stamps ctime after
  setattr_copy()
- [Low] ext4: ext4_setattr() updates ctime on ATTR_MODE without
  ATTR_CTIME
- [Low] ext4: ext4_acl_chmod() has unused params and duplicates
  ext4_set_acl()
- [Low] ext4: ext4_setattr() dirties inode on failed setattr with
  unchanged uid/gid
- [Low] ext4: ext4_setattr() runs redundant in_group_or_capable() before
  ACL chmod
--

commit a1eb006c5ae5fa01330ebb5ffaa3d22edaa6640c
Author: Alan <3164729207@qq.com>

ext4: restore mode precisely when ACL chmod fails

This commit adds ext4_acl_chmod() and reorders ext4_setattr() to update
the access ACL before calling setattr_copy() and mark_inode_dirty(), with
a recovery path to persist ownership or setid changes if the ACL update
fails.

> diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
> index 59fac55a24261..26ae3c60091d6 100644
> --- a/fs/ext4/acl.c
> +++ b/fs/ext4/acl.c
> @@ -269,6 +269,75 @@ ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	return error;
>  }
>  
> +/*
> + * Update the access ACL for a mode change without touching inode->i_mode.
> + * ext4_setattr() uses this so a failed ACL update cannot persist the new
> + * mode through an unrelated mark_inode_dirty() from the xattr path.
> + */
> +int
> +ext4_acl_chmod(const struct mnt_idmap *idmap, struct dentry *dentry,
> +	       umode_t mode)
> +{
> +	struct inode *inode = d_inode(dentry);
> +	struct posix_acl *acl;
> +	handle_t *handle;
> +	int error, credits, retries = 0;
> +
> +	if (!IS_POSIXACL(inode))
> +		return 0;
> +	if (!inode->i_op->set_acl)
> +		return -EOPNOTSUPP;

[Severity: Low]
This isn't a bug, but idmap is unused here and dentry is only used to
extract d_inode(dentry) for an ext4-internal helper called only from
ext4_setattr().  The inode->i_op->set_acl check is also unnecessary since
ext4_acl_chmod() never calls ->set_acl and notify_change() already
rejects symlinks.

The journal and -ENOSPC retry loop below also duplicates ext4_set_acl();
sharing a common helper would avoid the duplication and make it easier to
keep the ACL and i_mode updates in a single transaction.

[ ... ]

> +	error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, acl,
> +			       0 /* xattr_flags */);
> +	ext4_journal_stop(handle);

[Severity: High]
Does stopping the handle here split the access ACL update and the
inode->i_mode update across two separate JBD2 transactions?

Inside __ext4_set_acl() -> ext4_xattr_set_handle(), ext4_mark_iloc_dirty()
serializes the old inode->i_mode into the first transaction, and for
IS_SYNC(inode) files ext4_handle_sync() commits it synchronously before
ext4_setattr() updates inode->i_mode via setattr_copy() and
mark_inode_dirty() -> ext4_dirty_inode() in a second, non-synchronous
transaction.

This is a durability regression for IS_SYNC inodes and opens a
crash-consistency window: if a crash or journal abort drops the second
transaction after chmod(0640) on a 0604 file with an ACL, disk retains
the new ACL with stale i_mode 0604, and acl_permission_check() skips ACL
evaluation when (mode & S_IRWXG) is zero, honoring the stale other-read
bit.  Updating inode->i_mode and calling ext4_mark_inode_dirty(handle,
inode) inside this same handle after __ext4_set_acl() succeeds, as
ext4_set_acl() does, would keep both updates atomic on disk.

[Severity: Medium]
Can ignoring the return value of ext4_journal_stop(handle) silently drop
synchronous commit or handle-abort errors?

When IS_SYNC(inode) is set, ext4_xattr_set_handle() marks the handle
synchronous so ext4_journal_stop() waits for commit and returns any I/O
or abort error.  Discarding it lets ext4_setattr() proceed with
setattr_copy() and return 0 to userspace despite the failed commit.
Capturing error2 = ext4_journal_stop(handle) and assigning error = error2
when !error, as ext4_xattr_set() does, would propagate the failure.

[ ... ]

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index b4d581aa5ee1a..4869ca72d22e2 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c

[ ... ]

> @@ -7173,8 +7178,67 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>  	if (orphan && inode->i_nlink)
>  		ext4_orphan_del(NULL, inode);
>  
> -	if (!error && (ia_valid & ATTR_MODE))
> -		rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
> +	if (!error && (ia_valid & ATTR_MODE)) {
> +		umode_t new_mode = attr->ia_mode;
> +
> +		if (!in_group_or_capable(idmap, inode,
> +					 i_gid_into_vfsgid(idmap, inode)))
> +			new_mode &= ~S_ISGID;

[Severity: Low]
Is this in_group_or_capable() check dead work before ext4_acl_chmod()?

ext4_acl_chmod() passes new_mode only to __posix_acl_chmod_masq() (which
reads only S_IRWXU, S_IRWXG, and S_IRWXO) and posix_acl_equiv_mode(),
ignoring S_ISGID, and setattr_copy() immediately runs the same
in_group_or_capable() check again.  When inode->i_gid is not in the
caller's groups, this triggers a duplicate audited CAP_FSETID LSM check
on every chmod().

> +
> +		/*
> +		 * Update the ACL without touching inode->i_mode.  The xattr
> +		 * path can dirty the inode (for example through dquot) while
> +		 * this runs; changing i_mode here would let that write persist
> +		 * a mode whose ACL update later fails.
> +		 */
> +		rc = ext4_acl_chmod(idmap, dentry, new_mode);

[Severity: High]
Can moving setattr_copy() after ext4_acl_chmod() persist a new i_uid or
i_gid on disk with S_ISUID or S_ISGID still set?

Earlier in ext4_setattr(), the ownership transaction writes the new i_uid
and i_gid to the journal while inode->i_mode still retains S_ISUID and
S_ISGID.  Before this patch, setattr_copy() and mark_inode_dirty()
cleared those bits immediately; now ext4_acl_chmod() runs first and can
force-commit that state via IS_SYNC(inode) or ext4_should_retry_alloc()
on -ENOSPC, or abort the journal on an xattr I/O error so the recovery
mark_inode_dirty() silently drops the clear in ext4_dirty_inode().

When a privileged chown() changes a user-created 04755 ACL executable to
root, a crash or journal abort in this window can leave a setuid-root
executable on disk.

[ ... ]

> +			if (restore != old_mode ||
> +			    (ia_valid & (ATTR_UID | ATTR_GID))) {
> +				inode->i_mode = restore;

[Severity: Low]
Should this check whether ownership actually changed instead of testing
ia_valid & (ATTR_UID | ATTR_GID)?

Callers such as __nfsd_setattr() can pass ATTR_UID or ATTR_GID alongside
ATTR_MODE even when the requested uid/gid already matches the inode, so
the earlier ownership transaction does not run.  If ext4_acl_chmod() then
fails on a directory or non-setid file, this still advances ctime,
increments i_version, and dirties the inode even though no attribute
changed.

> +				if (ia_valid & ATTR_CTIME_SET)
> +					inode_set_ctime_to_ts(inode,
> +							      attr->ia_ctime);
> +				else
> +					inode_set_ctime_current(inode);

[Severity: Medium]
Can calling inode_set_ctime_to_ts() for ATTR_CTIME_SET violate ext4's
FS_MGTIME contract?

Because ext4_fs_type sets FS_MGTIME, concurrent stat() callers run
fill_mg_cmtime() locklessly and can race with the plain WRITE_ONCE()
stores in inode_set_ctime_to_ts():

CPU0                              CPU1
ext4_setattr()
  inode_set_ctime_to_ts()
    WRITE_ONCE(i_ctime_sec, ...)
                                  fill_mg_cmtime()
                                    atomic_fetch_or(I_CTIME_QUERIED, pcn)
    WRITE_ONCE(i_ctime_nsec, ...) <-- clears I_CTIME_QUERIED

It also skips inode_set_ctime_deleg()'s monotonicity and future-clamping
checks when an NFSv4 delegated setattr combines ATTR_CTIME_SET with
ownership and mode changes.  Using inode_set_ctime_deleg() here would
match setattr_copy_mgtime().

[ ... ]

> +	if (!error && !rc) {
> +		if (inc_ivers)
> +			inode_inc_iversion(inode);
> +		setattr_copy(idmap, inode, attr);
> +		/*
> +		 * ext4_xattr_set_handle() advanced ctime while updating the
> +		 * ACL.  Do not let setattr_copy() rewind it to the timestamp
> +		 * snapshotted before the ACL transaction.
> +		 */
> +		if ((ia_valid & ATTR_MODE) && !(ia_valid & ATTR_CTIME_SET))
> +			inode_set_ctime_current(inode);

[Severity: Medium]
Can this unconditional inode_set_ctime_current() break i_mtime == i_ctime
on FS_MGTIME ext4 mounts and add redundant multigrain timestamp work?

Because ext4_fs_type sets FS_MGTIME, setattr_copy() dispatches to
setattr_copy_mgtime(), which already calls inode_set_ctime_current() when
ATTR_CTIME is set (never rewinding to attr->ia_ctime) and reuses that
same timestamp for i_mtime.  Calling inode_set_ctime_current() a second
time on a combined ATTR_MODE | ATTR_MTIME | ATTR_CTIME operation such as
truncate() on a setid file can advance i_ctime past i_mtime if a
concurrent stat() sets I_CTIME_QUERIED or the coarse clock ticks between
the two calls.

Rewind protection is only needed when !is_mgtime(inode) && (ia_valid &
ATTR_CTIME) && !(ia_valid & ATTR_CTIME_SET) and an ACL xattr was actually
updated.

[Severity: Low]
Does this also advance i_ctime when ATTR_MODE is passed without
ATTR_CTIME?

Internal callers such as ovl_set_attr() and __remove_privs() invoke
notify_change() with ATTR_MODE and without ATTR_CTIME, where
setattr_copy() intentionally leaves i_ctime untouched.  This regression
updates i_ctime unconditionally even when no POSIX ACL exists.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_DEB80DE95137BB46140E9E6CB67DE0180008@qq.com?part=1

      reply	other threads:[~2026-12-03  9:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 15:14 [PATCH] ext4: restore the old mode if the ACL update fails Alan
2026-12-02 15:37 ` sashiko-bot
2026-12-03  4:32 ` [PATCH v2] ext4: restore mode precisely when ACL chmod fails Alan
2026-12-03  4:57   ` sashiko-bot
2026-12-03  8:33 ` [PATCH v3] " Alan
2026-12-03  9:03   ` sashiko-bot
2026-12-03  9:21   ` [PATCH v4] " Alan
2026-12-03  9:48     ` sashiko-bot [this message]

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-247499@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=3164729207@qq.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