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