Linux EXT4 FS development
 help / color / mirror / Atom feed
* [PATCH] ext4: restore the old mode if the ACL update fails
@ 2026-12-02 15:14 Alan
  2026-12-02 15:37 ` sashiko-bot
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Alan @ 2026-12-02 15:14 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, linux-kernel, stable

ext4_setattr() applies setattr_copy() and mark_inode_dirty() before
posix_acl_chmod().  When chmod() only changes the mode, the new mode is
already dirty when the ACL update fails (for example with ENOSPC).  The
syscall returns an error, but the inode mode is persisted while the access
ACL still describes the old mode, violating the ACL/mode invariant.

Save the old mode and restore it (and mark the inode dirty again) when
posix_acl_chmod() fails, so a failed chmod() leaves both the mode and the
ACL at their old values.

Tested on Linux 6.12.1 and 7.4-rc3 with two files sharing an access ACL on
a full filesystem.  chmod(A, 0700) returns ENOSPC.  Before the change A's
mode was 0700 while the ACL mask remained r-x; after the change A's mode
remains 0650 with the same ACL.  The control file B is unchanged and
e2fsck -fn passes in both cases.

Fixes: 64e178a7118b ("ext2/3/4: use generic posix ACL infrastructure")
Cc: stable@vger.kernel.org
Signed-off-by: Alan <3164729207@qq.com>
---
 fs/ext4/inode.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b4d581aa5ee1ae..9de412540ddef1 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -7029,6 +7029,7 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct iattr *attr)
 {
 	struct inode *inode = d_inode(dentry);
+	umode_t old_mode = inode->i_mode;
 	int error, rc = 0;
 	int orphan = 0;
 	const unsigned int ia_valid = attr->ia_valid;
@@ -7173,8 +7174,19 @@ 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))
+	if (!error && (ia_valid & ATTR_MODE)) {
 		rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
+		if (rc) {
+			/*
+			 * The mode was already copied into the inode and marked
+			 * dirty.  If the ACL update failed, chmod() returns an
+			 * error but the new mode must not remain on disk while
+			 * the ACL still describes the old mode.
+			 */
+			inode->i_mode = old_mode;
+			mark_inode_dirty(inode);
+		}
+	}
 
 err_out:
 	if  (error)

base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] ext4: restore the old mode if the ACL update fails
  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  8:33 ` [PATCH v3] " Alan
  2 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-02 15:37 UTC (permalink / raw)
  To: Alan; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] ext4: restoring old_mode in ext4_setattr() re-enables
  S_ISUID/S_ISGID
--

commit 25d855847832145fc418799835c609dfdd478477
Author: Alan <3164729207@qq.com>

ext4: restore the old mode if the ACL update fails

This commit saves the previous inode->i_mode in ext4_setattr() and restores
it if posix_acl_chmod() fails after setattr_copy() has already dirtied the
new mode.

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index b4d581aa5ee1a..9de412540ddef 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -7029,6 +7029,7 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
>  		 struct iattr *attr)
>  {
>  	struct inode *inode = d_inode(dentry);
> +	umode_t old_mode = inode->i_mode;
>  	int error, rc = 0;
>  	int orphan = 0;
>  	const unsigned int ia_valid = attr->ia_valid;
> @@ -7173,8 +7174,19 @@ 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))
> +	if (!error && (ia_valid & ATTR_MODE)) {
>  		rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
> +		if (rc) {
> +			/*
> +			 * The mode was already copied into the inode and marked
> +			 * dirty.  If the ACL update failed, chmod() returns an
> +			 * error but the new mode must not remain on disk while
> +			 * the ACL still describes the old mode.
> +			 */
> +			inode->i_mode = old_mode;
> +			mark_inode_dirty(inode);
> +		}
> +	}

[Severity: High]
Can restoring the full old_mode re-enable S_ISUID or S_ISGID after the file
has already been truncated or chowned?

During truncate() or chown() on a setuid or setgid file, notify_change()
translates ATTR_KILL_SUID and ATTR_KILL_SGID into ATTR_MODE with those bits
cleared in attr->ia_mode.  ext4_setattr() commits UID/GID updates and
performs truncation before calling setattr_copy() and posix_acl_chmod().

If posix_acl_chmod() then fails (for example with -ENOSPC when unsharing a
shared xattr block), writing back old_mode restores S_ISUID and S_ISGID on
an inode whose ownership or contents have already been modified on disk.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v2] ext4: restore mode precisely when ACL chmod fails
  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 ` Alan
  2026-12-03  4:57   ` sashiko-bot
  2026-12-03  8:33 ` [PATCH v3] " Alan
  2 siblings, 1 reply; 8+ messages in thread
From: Alan @ 2026-12-03  4:32 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, linux-kernel, stable

ext4_setattr() dirties the new mode before posix_acl_chmod(). When the ACL
update fails before changing the access ACL, restoring the old mode avoids
leaving the inode mode inconsistent with the ACL.

Restore the complete old mode for a failed mode-only chmod, but do not
revive setid bits cleared for privilege changes. An ACL update may have
partially succeeded before returning an error; this patch restores i_mode
only.

Tested on Linux 7.4-rc3 (d135ab453a7400): ENOSPC chmod and blkdebug
truncate-EIO tests passed; e2fsck -fn passed.

Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Cc: stable@vger.kernel.org
Signed-off-by: Alan <3164729207@qq.com>
---
 fs/ext4/inode.c | 27 ++++++++++++++++++++++++++-
 1 file changed, 26 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b4d581aa5ee1ae..ec5fb850c5d409 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -7029,6 +7029,7 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct iattr *attr)
 {
 	struct inode *inode = d_inode(dentry);
+	umode_t old_mode = inode->i_mode;
 	int error, rc = 0;
 	int orphan = 0;
 	const unsigned int ia_valid = attr->ia_valid;
@@ -7173,8 +7174,32 @@ 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))
+	if (!error && (ia_valid & ATTR_MODE)) {
 		rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
+		if (rc) {
+			umode_t new_mode = inode->i_mode;
+
+			/*
+			 * The mode was already copied into the inode and marked
+			 * dirty.  If the ACL update failed, chmod() returns an
+			 * error, so restore the old mode.
+			 *
+			 * Keep setid bits cleared when this setattr also revokes
+			 * privilege.  The VFS reports this through ATTR_KILL_* for
+			 * truncate/chown; NFS SETATTR can combine owner and mode
+			 * changes and clear setid directly in ia_mode instead.
+			 * A failed mode-only change, on the other hand, restores
+			 * the old mode completely.
+			 */
+			inode->i_mode = old_mode;
+			if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_KILL_SUID |
+				ATTR_KILL_SGID))
+				inode->i_mode = (old_mode & ~(S_ISUID | S_ISGID)) |
+					(old_mode & new_mode &
+					 (S_ISUID | S_ISGID));
+			mark_inode_dirty(inode);
+		}
+	}
 
 err_out:
 	if  (error)

base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v2] ext4: restore mode precisely when ACL chmod fails
  2026-12-03  4:32 ` [PATCH v2] ext4: restore mode precisely when ACL chmod fails Alan
@ 2026-12-03  4:57   ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03  4:57 UTC (permalink / raw)
  To: Alan; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: ext4_setattr() rollback can revive S_ISGID|S_IXGRP
  after chown
- [Medium] ext4: ext4_setattr() rollback strips S_ISGID from directories
  on error
- [Medium] ext4: ext4_setattr() rollback via mark_inode_dirty() drops
  journal errors
- [Medium] ext4: mode rollback in ext4_setattr() skips i_version and
  ctime updates
- [Medium] ext4: two-step i_mode store in ext4_setattr() exposes
  transient setid bits
--

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

ext4: restore mode precisely when ACL chmod fails

This commit saves inode->i_mode on entry to ext4_setattr() and restores
it when posix_acl_chmod() fails, keeping S_ISUID and S_ISGID cleared if
the setattr also changed ownership or carried ATTR_KILL_SUID or
ATTR_KILL_SGID.

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

[ ... ]

> @@ -7173,8 +7174,32 @@ 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))
> +	if (!error && (ia_valid & ATTR_MODE)) {
>  		rc = posix_acl_chmod(idmap, dentry, inode->i_mode);
> +		if (rc) {
> +			umode_t new_mode = inode->i_mode;

[ ... ]

> +			inode->i_mode = old_mode;
> +			if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_KILL_SUID |
> +				ATTR_KILL_SGID))
> +				inode->i_mode = (old_mode & ~(S_ISUID | S_ISGID)) |
> +					(old_mode & new_mode &
> +					 (S_ISUID | S_ISGID));

[Severity: Medium]
Can this rollback leave a regular file with both S_ISGID and S_IXGRP set
after its owner or group has already been changed?

nfsd_sanitize_attrs() clears S_ISGID on a combined owner and mode SETATTR
only when ia_mode has S_IXGRP set:

fs/nfsd/vfs.c:nfsd_sanitize_attrs() {
    ...
	if (!S_ISDIR(inode->i_mode) &&
	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
		iap->ia_valid |= ATTR_KILL_PRIV;
		if (iap->ia_valid & ATTR_MODE) {
			iap->ia_mode &= ~S_ISUID;
			if (iap->ia_mode & S_IXGRP)
				iap->ia_mode &= ~S_ISGID;
		}
    ...
}

If a 02755 file receives a combined owner and mode SETATTR with ia_mode
02644, nfsd_sanitize_attrs() leaves S_ISGID set because S_IXGRP is
cleared.  ext4_setattr() commits the new UID/GID first via i_uid_update()
and i_gid_update(), and if posix_acl_chmod() then fails, this rollback
restores S_IXGRP from old_mode while keeping S_ISGID from new_mode.

That leaves an executable setgid file (02755) under the new ownership,
which bprm_fill_uid() honors on execve().  Evaluating setid stripping
against the restored mode (clearing S_ISUID and checking
setattr_should_drop_sgid() for non-directories on ATTR_UID/ATTR_GID, plus
honoring ATTR_KILL_SUID/ATTR_KILL_SGID) instead of intersecting with
new_mode avoids this regression.

[Severity: Medium]
Does this strip S_ISGID from directories when a combined ownership and
mode setattr fails?

Under VFS rules (chown_common() and nfsd_sanitize_attrs()), ownership
changes on directories do not drop S_ISUID or S_ISGID, because S_ISGID on
a directory controls group inheritance rather than execution privilege.  If
a combined ATTR_UID/ATTR_GID and ATTR_MODE setattr from NFSD or ksmbd
tries to change a 02755 directory to 00755 and posix_acl_chmod() fails,
this branch clears S_ISGID from the restored mode, silently breaking group
inheritance even though chmod failed.

The same check also strips S_ISGID from non-executable regular files when
ATTR_UID/ATTR_GID is set without checking !S_ISDIR(inode->i_mode) and
setattr_should_drop_sgid().

[Severity: Medium]
Does writing unmasked old_mode directly to inode->i_mode before masking
S_ISUID and S_ISGID open a race with concurrent inode readers and writers?

CPU0                              CPU1
ext4_setattr()
  inode->i_mode = old_mode
                                  touch_atime() -> ext4_dirty_inode()
                                    ext4_do_update_inode()
                                      ext4_fill_raw_inode()
                                        raw_inode->i_mode = inode->i_mode
                                        <-- snapshots unmasked setid bits
  inode->i_mode = masked_mode
  mark_inode_dirty(inode)

ext4_fill_raw_inode() runs under ei->i_raw_lock without inode->i_rwsem, so
a concurrent atime update can journal the transient unmasked setid bits.
If the following mark_inode_dirty() silently fails in ext4_dirty_inode()
or a crash occurs before it commits, those setid bits persist on disk
alongside the already-applied chown or truncate; lockless readers like
generic_fillattr() can also observe the intermediate mode.

Computing the target mode in a local variable first and only updating
inode->i_mode and calling mark_inode_dirty() when it differs from new_mode
avoids the transient store and skips a redundant journal transaction when
ATTR_KILL_SUID/ATTR_KILL_SGID already matched the masked mode.

> +			mark_inode_dirty(inode);

[Severity: Medium]
Can rolling back inode->i_mode via mark_inode_dirty() silently lose the
on-disk update if ext4_journal_start() fails?

ext4_dirty_inode() starts a new transaction and returns silently if
ext4_journal_start() fails (for example with -ENOMEM under the same memory
pressure that failed posix_acl_chmod()).  Because ext4_write_inode() does
not re-copy inode->i_mode into raw_inode, the on-disk buffer keeps
new_mode while in-memory inode->i_mode holds old_mode, so the failed mode
reappears after eviction or remount.

On -ENOSPC, ext4_set_acl() calls ext4_should_retry_alloc() ->
jbd2_journal_force_commit_nested(), which can commit the earlier
transaction containing new_mode before rollback runs, breaking crash
atomicity.  While posix_acl_chmod() sleeps, lockless
acl_permission_check() can also evaluate permissions against the
unvalidated new_mode, and i_ctime and i_version remain mutated after a
failed mode-only chmod.

Updating the ACL before dirtying the mode change, or performing both in a
single JBD2 transaction, avoids split transactions and silent rollback
failures.

[Severity: Medium]
Does reverting inode->i_mode without advancing i_version or ctime leave
concurrent statx() and NFSv4 caches stuck on new_mode?

CPU0                              CPU1
ext4_setattr()
  inode_inc_iversion() (V -> V+1)
  setattr_copy() (new_mode, T1)
  posix_acl_chmod() (sleeps)
                                  ext4_getattr() -> generic_fillattr()
                                    caches (new_mode, V+1, T1)
  inode->i_mode = old_mode
  mark_inode_dirty(inode)
                                  revalidates: sees (V+1, T1) unchanged
                                  <-- keeps stale new_mode cached

ext4_getattr() reads inode->i_mode, ctime, and inode_query_iversion()
without inode->i_rwsem.  Because the rollback does not call
inode_maybe_inc_iversion() or update ctime, a reader that observed
new_mode during posix_acl_chmod() sees the same change cookie and ctime
afterward and retains new_mode until an unrelated modification updates the
inode.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v3] ext4: restore mode precisely when ACL chmod fails
  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  8:33 ` Alan
  2026-12-03  9:03   ` sashiko-bot
  2026-12-03  9:21   ` [PATCH v4] " Alan
  2 siblings, 2 replies; 8+ messages in thread
From: Alan @ 2026-12-03  8:33 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, linux-kernel, stable

ext4_setattr() copies the new mode into the inode and marks it dirty
before calling posix_acl_chmod().  If the ACL update fails, chmod() returns
an error but the new mode is already dirty; on disk it can disagree with
the old access ACL.

Update the access ACL before dirtying the mode.  If the ACL update fails,
restore the old mode in memory without persisting the new one.  Preserve
setid bits that VFS required to be cleared for chown or truncate, and
persist those clears even on failure.  Advance i_version and ctime after
restoring the old mode so concurrent statx() and NFSv4 readers cannot keep
the transiently visible new mode cached.

Tested with an ENOSPC chmod and blkdebug ACL-block EIO during truncate;
e2fsck -fn passes in both cases.

Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Cc: stable@vger.kernel.org
Signed-off-by: Alan <3164729207@qq.com>
---
 fs/ext4/inode.c | 69 ++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 60 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b4d581aa5ee1ae..24d04ee94a37b7 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -7029,6 +7029,8 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct iattr *attr)
 {
 	struct inode *inode = d_inode(dentry);
+	umode_t old_mode = inode->i_mode;
+	int kill_sgid = 0;
 	int error, rc = 0;
 	int orphan = 0;
 	const unsigned int ia_valid = attr->ia_valid;
@@ -7096,6 +7098,16 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		}
 	}
 
+	/*
+	 * When ownership changes, setgid must be cleared for non-directories
+	 * according to the same rules as setattr_copy().  Evaluate this after
+	 * the new i_gid has been installed but before i_mode changes; an ACL
+	 * failure may need to restore the old permission bits without reviving
+	 * setid.
+	 */
+	if (!S_ISDIR(inode->i_mode) && (ia_valid & (ATTR_UID | ATTR_GID)))
+		kill_sgid = setattr_should_drop_sgid(idmap, inode);
+
 	if (attr->ia_valid & ATTR_SIZE) {
 		loff_t oldsize = inode->i_size;
 		int shrink = (attr->ia_size < inode->i_size);
@@ -7159,13 +7171,6 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		filemap_invalidate_unlock(inode->i_mapping);
 	}
 
-	if (!error) {
-		if (inc_ivers)
-			inode_inc_iversion(inode);
-		setattr_copy(idmap, inode, attr);
-		mark_inode_dirty(inode);
-	}
-
 	/*
 	 * If the call to ext4_truncate failed to get a transaction handle at
 	 * all, we need to clean up the in-core orphan list manually.
@@ -7173,8 +7178,54 @@ 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;
+
+		/*
+		 * Update the ACL before dirtying the new mode.  If the ACL
+		 * update fails, the old mode is still the one on disk, so we
+		 * only need to restore the in-memory mode and invalidate any
+		 * cached attributes.
+		 */
+		inode->i_mode = new_mode;
+		rc = posix_acl_chmod(idmap, dentry, new_mode);
+		if (rc) {
+			umode_t restore = old_mode;
+
+			if (ia_valid & ATTR_KILL_SUID)
+				restore &= ~S_ISUID;
+			if (ia_valid & ATTR_KILL_SGID)
+				restore &= ~S_ISGID;
+			if (!S_ISDIR(old_mode) &&
+			    (ia_valid & (ATTR_UID | ATTR_GID))) {
+				restore &= ~S_ISUID;
+				if (kill_sgid)
+					restore &= ~S_ISGID;
+			}
+
+			inode->i_mode = restore;
+			inode_inc_iversion(inode);
+			inode_set_ctime_current(inode);
+			/*
+			 * A failed mode-only chmod leaves the on-disk mode
+			 * untouched.  If VFS nevertheless required setid bits
+			 * to be dropped (chown/truncate), persist that part.
+			 */
+			if (restore != old_mode)
+				mark_inode_dirty(inode);
+		}
+	}
+
+	if (!error && !rc) {
+		if (inc_ivers)
+			inode_inc_iversion(inode);
+		setattr_copy(idmap, inode, attr);
+		mark_inode_dirty(inode);
+	}
 
 err_out:
 	if  (error)

base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v3] ext4: restore mode precisely when ACL chmod fails
  2026-12-03  8:33 ` [PATCH v3] " Alan
@ 2026-12-03  9:03   ` sashiko-bot
  2026-12-03  9:21   ` [PATCH v4] " Alan
  1 sibling, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03  9:03 UTC (permalink / raw)
  To: Alan; +Cc: linux-ext4, tytso

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [High] ext4: speculative i_mode write before posix_acl_chmod()
  persists failed mode
- [Medium] ext4: failed posix_acl_chmod() leaves bumped i_version and
  ctime unpersisted
- [Medium] ext4: ext4_setattr() skips mark_inode_dirty() after chown on
  ACL failure
- [Medium] ext4: setattr_copy() after posix_acl_chmod() rewinds ctime on
  ext2/ext3
- [Low] ext4: generic_fillattr() can tear transient new_mode with
  post-restore ctime
--

commit 10d9436fd4de4b1a7b1d93757679541afdb6d7d4
Author: Alan <3164729207@qq.com>

ext4: restore mode precisely when ACL chmod fails

This commit moves posix_acl_chmod() ahead of setattr_copy() and
mark_inode_dirty() in ext4_setattr() so a failed ACL update restores the
old mode (while preserving setid drops) and advances i_version and ctime.

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

[ ... ]

> @@ -7173,8 +7178,54 @@ 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;
> +
> +		/*
> +		 * Update the ACL before dirtying the new mode.  If the ACL
> +		 * update fails, the old mode is still the one on disk, so we
> +		 * only need to restore the in-memory mode and invalidate any
> +		 * cached attributes.
> +		 */
> +		inode->i_mode = new_mode;
> +		rc = posix_acl_chmod(idmap, dentry, new_mode);

[Severity: High]
Can writing inode->i_mode before posix_acl_chmod() persist the rejected
new_mode on disk when the ACL update fails?

Inside posix_acl_chmod(), ext4_xattr_block_set() calls dquot_alloc_block(),
whose dquot_alloc_space() unconditionally calls mark_inode_dirty() even with
quotas disabled.  That runs ext4_dirty_inode() -> ext4_fill_raw_inode() and
copies new_mode into raw_inode->i_mode.

If block allocation or journaling then fails (for example with -ENOSPC or
-EIO), ext4_setattr() restores old_mode in memory and skips
mark_inode_dirty() when restore == old_mode.  After inode eviction or
remount, the inode reloads with new_mode disagreeing with the old ACL.

Concurrent paths that dirty the inode without inode->i_rwsem, such as
touch_atime() or writeback, can also flush new_mode while posix_acl_chmod()
sleeps.  During that window, lockless acl_permission_check() also evaluates
permissions against the unapproved new_mode.

Separately, when restore != old_mode, rollback calls void mark_inode_dirty(),
which silently drops ext4_journal_start() failures in ext4_dirty_inode() and
can leave stale setid bits on disk.  Updating the ACL xattr before changing
inode->i_mode avoids these issues.

[ ... ]

> +			inode->i_mode = restore;
> +			inode_inc_iversion(inode);
> +			inode_set_ctime_current(inode);

[Severity: Low]
Is advancing i_version and ctime after restoring inode->i_mode enough to stop
concurrent statx() or NFSv4 GETATTR readers from caching new_mode?

ext4_getattr() calls generic_fillattr() without inode->i_rwsem, and
generic_fillattr() reads inode->i_mode before reading ctime and
change_cookie:

CPU0 (ext4_setattr)               CPU1 (ext4_getattr)
inode->i_mode = new_mode
posix_acl_chmod() -> error
                                  generic_fillattr()
                                    stat->mode = inode->i_mode
inode->i_mode = restore
inode_inc_iversion(inode)
inode_set_ctime_current(inode)
                                    read ctime, change_cookie  <-- torn

Because the steady-state inode now has that same ctime and change_cookie,
later revalidations see no change and keep the rejected new_mode cached.

> +			/*
> +			 * A failed mode-only chmod leaves the on-disk mode
> +			 * untouched.  If VFS nevertheless required setid bits
> +			 * to be dropped (chown/truncate), persist that part.
> +			 */
> +			if (restore != old_mode)
> +				mark_inode_dirty(inode);
> +		}
> +	}

[Severity: Medium]
Does skipping mark_inode_dirty() when restore == old_mode leave the bumped
i_version and ctime unpersisted on disk?

On a failed mode-only chmod(), clean inode eviction or reboot reloads the
older on-disk i_version and ctime in __ext4_iget(), rolling both backwards.
That breaks i_version monotonicity and lets NFSv4 or statx() readers that
saw the transient new_mode revalidate and keep it cached.

Advancing st_ctime on a failed mode-only chmod() also violates POSIX chmod()
semantics.

> +
> +	if (!error && !rc) {
> +		if (inc_ivers)
> +			inode_inc_iversion(inode);

[Severity: Medium]
What happens on a compound chown + chmod when posix_acl_chmod() fails on a
non-setid file or a directory?

Callers such as __nfsd_setattr() and ksmbd's set_info_sec() submit ATTR_UID
or ATTR_GID together with ATTR_MODE in one notify_change() call.  Earlier in
ext4_setattr(), i_uid_update() and i_gid_update() already committed the new
ownership to disk with the old ctime and i_version.

Because restore == old_mode skips mark_inode_dirty() above and this block
now skips setattr_copy() and mark_inode_dirty() when rc is non-zero, the
updated ctime and i_version are never written to disk and roll backwards on
eviction while the ownership change remains persisted.

> +		setattr_copy(idmap, inode, attr);
> +		mark_inode_dirty(inode);
> +	}

[Severity: Medium]
Can calling setattr_copy() after posix_acl_chmod() rewind i_ctime on ext2
and ext3 mounts handled by ext4?

notify_change() snapshots attr->ia_ctime before calling ext4_setattr(), and
posix_acl_chmod() -> ext4_xattr_set_handle() then advances i_ctime to a
newer timestamp via inode_set_ctime_current().  Because ext2_fs_type and
ext3_fs_type do not set FS_MGTIME, is_mgtime(inode) is false.

setattr_copy() therefore overwrites i_ctime with the earlier attr->ia_ctime
and mark_inode_dirty() persists it, breaking ctime monotonicity whenever the
clock advances while posix_acl_chmod() runs.

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH v4] ext4: restore mode precisely when ACL chmod fails
  2026-12-03  8:33 ` [PATCH v3] " Alan
  2026-12-03  9:03   ` sashiko-bot
@ 2026-12-03  9:21   ` Alan
  2026-12-03  9:48     ` sashiko-bot
  1 sibling, 1 reply; 8+ messages in thread
From: Alan @ 2026-12-03  9:21 UTC (permalink / raw)
  To: tytso
  Cc: adilger.kernel, libaokun, jack, ojaswin, ritesh.list, yi.zhang,
	linux-ext4, linux-kernel, stable, Alan

ext4_setattr() copies the new mode into the inode and marks it dirty
before calling posix_acl_chmod().  If the ACL update fails, chmod() returns
an error but the new mode is already dirty; on disk it can disagree with
the old access ACL.

Update the access ACL before changing the inode mode.  Add
ext4_acl_chmod() so the ACL can be updated with an explicit mode without
temporarily writing inode->i_mode; the xattr path can dirty the inode while
the ACL is being updated, and it must not observe a mode whose ACL update
may still fail.

Only after the ACL update succeeds does ext4_setattr() call setattr_copy()
and mark_inode_dirty().  If the ACL update fails, a failed mode-only chmod
leaves mode and ctime untouched.  A chown or truncate may already have
committed an ownership change or a setid drop, so persist that resulting
mode and freshness state.  Preserve VFS's setid clearing rules, including
directories keeping S_ISGID.  Do not let setattr_copy() rewind the ctime
advanced by the ACL transaction.

Tested with an ENOSPC chmod and blkdebug ACL-block EIO during truncate;
e2fsck -fn passes in both cases.

Fixes: ac27a0ec112a ("[PATCH] ext4: initial copy of files from ext3")
Cc: stable@vger.kernel.org
Signed-off-by: Alan <3164729207@qq.com>
---
 fs/ext4/acl.c   | 69 +++++++++++++++++++++++++++++++++++++++++
 fs/ext4/acl.h   |  9 ++++++
 fs/ext4/inode.c | 82 +++++++++++++++++++++++++++++++++++++++++++------
 3 files changed, 151 insertions(+), 9 deletions(-)

diff --git a/fs/ext4/acl.c b/fs/ext4/acl.c
index 59fac55a242610..26ae3c60091d63 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;
+
+	acl = get_inode_acl(inode, ACL_TYPE_ACCESS);
+	if (IS_ERR_OR_NULL(acl)) {
+		if (acl == ERR_PTR(-EOPNOTSUPP))
+			return 0;
+		return PTR_ERR(acl);
+	}
+
+	error = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
+	if (error)
+		goto out;
+
+	/*
+	 * Match posix_acl_update_mode()'s decision whether the ACL is fully
+	 * represented by mode, but leave i_mode to ext4_setattr().
+	 */
+	error = posix_acl_equiv_mode(acl, &mode);
+	if (error < 0)
+		goto out;
+	if (error == 0) {
+		posix_acl_release(acl);
+		acl = NULL;
+	}
+
+	error = dquot_initialize(inode);
+	if (error)
+		goto out;
+
+retry:
+	error = ext4_xattr_set_credits(inode,
+			acl ? ext4_acl_size(acl->a_count) : 0,
+			false /* is_create */, &credits);
+	if (error)
+		goto out;
+
+	handle = ext4_journal_start(inode, EXT4_HT_XATTR, credits);
+	if (IS_ERR(handle)) {
+		error = PTR_ERR(handle);
+		goto out;
+	}
+
+	error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS, acl,
+			       0 /* xattr_flags */);
+	ext4_journal_stop(handle);
+	if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
+		goto retry;
+out:
+	posix_acl_release(acl);
+	return error;
+}
+
 /*
  * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  *
diff --git a/fs/ext4/acl.h b/fs/ext4/acl.h
index a14838c5bc42d8..bc4c9d842d6df6 100644
--- a/fs/ext4/acl.h
+++ b/fs/ext4/acl.h
@@ -58,6 +58,8 @@ static inline int ext4_acl_count(size_t size)
 struct posix_acl *ext4_get_acl(struct inode *inode, int type, bool rcu);
 int ext4_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct posix_acl *acl, int type);
+int ext4_acl_chmod(const struct mnt_idmap *idmap, struct dentry *dentry,
+		   umode_t mode);
 extern int ext4_init_acl(handle_t *, struct inode *, struct inode *);
 
 #else  /* CONFIG_EXT4_FS_POSIX_ACL */
@@ -70,5 +72,12 @@ ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
 {
 	return 0;
 }
+
+static inline int
+ext4_acl_chmod(const struct mnt_idmap *idmap, struct dentry *dentry,
+	       umode_t mode)
+{
+	return 0;
+}
 #endif  /* CONFIG_EXT4_FS_POSIX_ACL */
 
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index b4d581aa5ee1ae..4869ca72d22e2f 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -7029,6 +7029,8 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		 struct iattr *attr)
 {
 	struct inode *inode = d_inode(dentry);
+	umode_t old_mode = inode->i_mode;
+	int kill_sgid = 0;
 	int error, rc = 0;
 	int orphan = 0;
 	const unsigned int ia_valid = attr->ia_valid;
@@ -7096,6 +7098,16 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		}
 	}
 
+	/*
+	 * When ownership changes, setgid must be cleared for non-directories
+	 * according to the same rules as setattr_copy().  Evaluate this after
+	 * the new i_gid has been installed but before i_mode changes; an ACL
+	 * failure may need to restore the old permission bits without reviving
+	 * setid.
+	 */
+	if (!S_ISDIR(inode->i_mode) && (ia_valid & (ATTR_UID | ATTR_GID)))
+		kill_sgid = setattr_should_drop_sgid(idmap, inode);
+
 	if (attr->ia_valid & ATTR_SIZE) {
 		loff_t oldsize = inode->i_size;
 		int shrink = (attr->ia_size < inode->i_size);
@@ -7159,13 +7171,6 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
 		filemap_invalidate_unlock(inode->i_mapping);
 	}
 
-	if (!error) {
-		if (inc_ivers)
-			inode_inc_iversion(inode);
-		setattr_copy(idmap, inode, attr);
-		mark_inode_dirty(inode);
-	}
-
 	/*
 	 * If the call to ext4_truncate failed to get a transaction handle at
 	 * all, we need to clean up the in-core orphan list manually.
@@ -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;
+
+		/*
+		 * 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);
+		if (rc) {
+			umode_t restore = old_mode;
+
+			if (ia_valid & ATTR_KILL_SUID)
+				restore &= ~S_ISUID;
+			if (ia_valid & ATTR_KILL_SGID)
+				restore &= ~S_ISGID;
+			if (!S_ISDIR(old_mode) &&
+			    (ia_valid & (ATTR_UID | ATTR_GID))) {
+				restore &= ~S_ISUID;
+				if (kill_sgid)
+					restore &= ~S_ISGID;
+			}
+
+			/*
+			 * Mode-only chmod failed: leave both mode and ctime
+			 * alone.  A failed chown/truncate still committed the
+			 * ownership change and/or setid drop, so persist the
+			 * resulting mode and freshness state.
+			 */
+			if (restore != old_mode ||
+			    (ia_valid & (ATTR_UID | ATTR_GID))) {
+				inode->i_mode = restore;
+				if (ia_valid & ATTR_CTIME_SET)
+					inode_set_ctime_to_ts(inode,
+							      attr->ia_ctime);
+				else
+					inode_set_ctime_current(inode);
+				inode_inc_iversion(inode);
+				mark_inode_dirty(inode);
+			}
+		}
+	}
+
+	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);
+		mark_inode_dirty(inode);
+	}
 
 err_out:
 	if  (error)

base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH v4] ext4: restore mode precisely when ACL chmod fails
  2026-12-03  9:21   ` [PATCH v4] " Alan
@ 2026-12-03  9:48     ` sashiko-bot
  0 siblings, 0 replies; 8+ messages in thread
From: sashiko-bot @ 2026-12-03  9:48 UTC (permalink / raw)
  To: Alan; +Cc: linux-ext4, tytso

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2026-12-03  9:48 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox