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