From: sashiko-bot@kernel.org
To: Alan <3164729207@qq.com>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH v5] ext4: restore mode precisely when ACL chmod fails
Date: Thu, 03 Dec 2026 11:49:22 +0000 [thread overview]
Message-ID: <sashiko-outbox-247681@kernel.org> (raw)
In-Reply-To: <tencent_A4888D4A1C88D882FA54300E719FD3396E09@qq.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: two-transaction mode rollback in ext4_setattr() loses
crash and EIO safety
- [Medium] ext4: mode rollback in ext4_setattr() does not advance
i_version or ctime
- [Medium] ext4: eager setattr_should_drop_sgid() on chown causes
spurious CAP_FSETID audit
- [Low] ext4: incomplete rollback leaves ctime dirty and unconditionally
dirties inode
- [Low] ext4: duplicate uid/gid update check in ext4_setattr()
--
commit 8c15ed51ea25d79a5b8e6c0f2df051e00ee6e8c4
Author: Alan <3164729207@qq.com>
ext4: restore mode precisely when ACL chmod fails
This commit saves the original inode mode and ownership-change state in
ext4_setattr() and restores the old permission bits (while keeping setid
bits cleared for chown or truncate) if posix_acl_chmod() fails.
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index b4d581aa5ee1a..76f23c8be5291 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
[ ... ]
> @@ -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;
[Severity: Low]
This duplicates the i_uid_needs_update() || i_gid_needs_update() check
right after dquot_initialize(), and evaluates it before early error
returns from fscrypt_prepare_setattr() and dquot_initialize(). Could the
existing ownership-update block simply reuse owner_changed (or set it)?
> @@ -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);
[Severity: Medium]
Can calling setattr_should_drop_sgid() unconditionally here trigger
spurious CAP_FSETID capability audits on normal chown() calls?
For a 02644 regular file (S_ISGID set, S_IXGRP clear) chowned from a
group the caller belongs to into one it does not, chown_common() checks
the old group without needing CAP_FSETID and leaves ATTR_MODE clear.
After i_gid_update() switches inode->i_gid to the new group, this call
reaches in_group_or_capable() -> capable_wrt_inode_uidgid() ->
ns_capable(CAP_FSETID), emitting false LSM audit denials or setting
PF_SUPERPRIV even though kill_sgid is never used.
Since inode->i_uid and inode->i_gid are not changed again before the
posix_acl_chmod() failure block, deferring this check into that error
path (using old_mode) avoids both the spurious capability check and the
extra work on the common chown() path.
> @@ -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);
> + }
> + }
[Severity: Medium]
Can this two-transaction rollback still leave the failed new mode on disk
after a crash or EIO?
setattr_copy() and mark_inode_dirty() journal the new inode->i_mode into
the raw inode buffer before posix_acl_chmod() runs. When ext4_set_acl()
hits -ENOSPC, ext4_should_retry_alloc() calls
jbd2_journal_force_commit_nested() whenever s_mb_free_pending is
non-zero, committing that first transaction to disk before returning
-ENOSPC. A crash or power loss before the rollback transaction commits
recovers the on-disk mode and ACL mismatch.
In addition, if posix_acl_chmod() fails due to an xattr EIO or corruption
that puts the filesystem into emergency read-only or aborts the journal,
mark_inode_dirty() -> ext4_dirty_inode() fails in ext4_journal_start()
and silently returns, leaving the raw inode buffer with the failed new
mode while in-memory inode->i_mode is reverted. Deferring the mode
update until posix_acl_chmod() succeeds (or updating both in one
transaction) would avoid both failure modes.
[Severity: Medium]
Can concurrent lockless getattr readers permanently cache the transient
failed mode?
CPU0 (chmod) CPU1 (getattr / nfsd)
ext4_setattr()
inode_inc_iversion() (V -> V+1)
setattr_copy() (mode=new, ctime=T1)
mark_inode_dirty()
posix_acl_chmod() ... blocks
vfs_getattr() -> generic_fillattr()
reads mode=new, ctime=T1,
change_cookie=V+1 <-- caches new mode
posix_acl_chmod() fails
inode->i_mode = restore
mark_inode_dirty() <-- V+1 and T1 unchanged
ext4_getattr() calls generic_fillattr() without inode->i_rwsem, so a
client such as nfsd can observe the transient new mode paired with the
bumped change_cookie and ctime. Because the rollback does not call
inode_inc_iversion() or inode_set_ctime_current(), later revalidations
see unchanged validators and keep serving the stale failed mode until an
unrelated metadata update occurs.
[Severity: Low]
Does this rollback leave st_ctime updated on disk after a failed
mode-only chmod()?
chmod_common() sets ATTR_MODE | ATTR_CTIME, so setattr_copy() updates
i_ctime before posix_acl_chmod() runs, and only inode->i_mode is
restored on failure. Also, when posix_acl_chmod() fails during a
truncate() or chown() where notify_change() added ATTR_MODE only to clear
setid bits, inode->i_mode already equals restore; guarding the rollback
with if (inode->i_mode != restore) avoids starting a redundant journal
transaction.
--
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_A4888D4A1C88D882FA54300E719FD3396E09@qq.com?part=1
prev parent reply other threads:[~2026-12-03 11:49 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
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
2026-12-03 11:29 ` [PATCH v5] " Alan
2026-12-03 11:49 ` sashiko-bot [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-247681@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=3164729207@qq.com \
--cc=linux-ext4@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=tytso@mit.edu \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox