[PATCH v3] ext4: restore mode precisely when ACL chmod fails
From: Alan
Date: Thu Dec 03 2026 - 03:34:18 EST
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@xxxxxxxxxxxxxxx
Signed-off-by: Alan <3164729207@xxxxxx>
---
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