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. Restore the old permission bits when posix_acl_chmod() fails. Do not revive setid bits that VFS cleared for chown or truncate: honor ATTR_KILL_SUID and ATTR_KILL_SGID, and for a non-directory ownership change clear S_ISUID and, when setattr_should_drop_sgid() says so, S_ISGID. Directories keep S_ISGID because it controls group inheritance. A failed mode-only chmod restores the complete old mode. Tested on Linux 7.4-rc3 and 6.12.1 with ENOSPC chmod, setuid/setgid plus blkdebug ACL-block EIO during truncate, and successful chmod; e2fsck -fn passes in all 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 | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index b4d581aa5ee1ae..76f23c8be5291d 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -7029,10 +7029,13 @@ 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; bool inc_ivers = true; + bool owner_changed; error = ext4_emergency_state(inode->i_sb); if (unlikely(error)) @@ -7050,6 +7053,9 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, if (error) return error; + owner_changed = i_uid_needs_update(idmap, attr, inode) || + i_gid_needs_update(idmap, attr, inode); + error = fscrypt_prepare_setattr(dentry, attr); if (error) return error; @@ -7096,6 +7102,9 @@ int ext4_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, } } + if (owner_changed && !S_ISDIR(inode->i_mode)) + 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); @@ -7173,8 +7182,31 @@ 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 restore = old_mode; + + /* + * The mode was already copied into the inode and marked + * dirty. If the ACL update failed, restore the old + * permission bits, but do not revive setid bits that + * VFS cleared for chown or truncate. + */ + if (ia_valid & ATTR_KILL_SUID) + restore &= ~S_ISUID; + if (ia_valid & ATTR_KILL_SGID) + restore &= ~S_ISGID; + if (owner_changed && !S_ISDIR(old_mode)) { + restore &= ~S_ISUID; + if (kill_sgid) + restore &= ~S_ISGID; + } + + inode->i_mode = restore; + mark_inode_dirty(inode); + } + } err_out: if (error) base-commit: d135ab453a7400a34930bdda1ef4bdc8acfac80e -- 2.53.0