All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: Alan <3164729207@qq.com>
Cc: linux-ext4@vger.kernel.org, tytso@mit.edu
Subject: Re: [PATCH v2] ext4: restore mode precisely when ACL chmod fails
Date: Thu, 03 Dec 2026 04:57:44 +0000	[thread overview]
Message-ID: <sashiko-outbox-247169@kernel.org> (raw)
In-Reply-To: <tencent_16875A36A63E6B8C6DDCD274A49B38A37D07@qq.com>

Thank you for your contribution! Sashiko AI review found 5 potential
issues to consider:
- [Medium] ext4: ext4_setattr() rollback can revive S_ISGID|S_IXGRP
  after chown
- [Medium] ext4: ext4_setattr() rollback strips S_ISGID from directories
  on error
- [Medium] ext4: ext4_setattr() rollback via mark_inode_dirty() drops
  journal errors
- [Medium] ext4: mode rollback in ext4_setattr() skips i_version and
  ctime updates
- [Medium] ext4: two-step i_mode store in ext4_setattr() exposes
  transient setid bits
--

commit b94d8fab0f3cb4ea38b7c97bc032debd416caa17
Author: Alan <3164729207@qq.com>

ext4: restore mode precisely when ACL chmod fails

This commit saves inode->i_mode on entry to ext4_setattr() and restores
it when posix_acl_chmod() fails, keeping S_ISUID and S_ISGID cleared if
the setattr also changed ownership or carried ATTR_KILL_SUID or
ATTR_KILL_SGID.

> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index b4d581aa5ee1a..ec5fb850c5d40 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c

[ ... ]

> @@ -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;

[ ... ]

> +			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));

[Severity: Medium]
Can this rollback leave a regular file with both S_ISGID and S_IXGRP set
after its owner or group has already been changed?

nfsd_sanitize_attrs() clears S_ISGID on a combined owner and mode SETATTR
only when ia_mode has S_IXGRP set:

fs/nfsd/vfs.c:nfsd_sanitize_attrs() {
    ...
	if (!S_ISDIR(inode->i_mode) &&
	    ((iap->ia_valid & ATTR_UID) || (iap->ia_valid & ATTR_GID))) {
		iap->ia_valid |= ATTR_KILL_PRIV;
		if (iap->ia_valid & ATTR_MODE) {
			iap->ia_mode &= ~S_ISUID;
			if (iap->ia_mode & S_IXGRP)
				iap->ia_mode &= ~S_ISGID;
		}
    ...
}

If a 02755 file receives a combined owner and mode SETATTR with ia_mode
02644, nfsd_sanitize_attrs() leaves S_ISGID set because S_IXGRP is
cleared.  ext4_setattr() commits the new UID/GID first via i_uid_update()
and i_gid_update(), and if posix_acl_chmod() then fails, this rollback
restores S_IXGRP from old_mode while keeping S_ISGID from new_mode.

That leaves an executable setgid file (02755) under the new ownership,
which bprm_fill_uid() honors on execve().  Evaluating setid stripping
against the restored mode (clearing S_ISUID and checking
setattr_should_drop_sgid() for non-directories on ATTR_UID/ATTR_GID, plus
honoring ATTR_KILL_SUID/ATTR_KILL_SGID) instead of intersecting with
new_mode avoids this regression.

[Severity: Medium]
Does this strip S_ISGID from directories when a combined ownership and
mode setattr fails?

Under VFS rules (chown_common() and nfsd_sanitize_attrs()), ownership
changes on directories do not drop S_ISUID or S_ISGID, because S_ISGID on
a directory controls group inheritance rather than execution privilege.  If
a combined ATTR_UID/ATTR_GID and ATTR_MODE setattr from NFSD or ksmbd
tries to change a 02755 directory to 00755 and posix_acl_chmod() fails,
this branch clears S_ISGID from the restored mode, silently breaking group
inheritance even though chmod failed.

The same check also strips S_ISGID from non-executable regular files when
ATTR_UID/ATTR_GID is set without checking !S_ISDIR(inode->i_mode) and
setattr_should_drop_sgid().

[Severity: Medium]
Does writing unmasked old_mode directly to inode->i_mode before masking
S_ISUID and S_ISGID open a race with concurrent inode readers and writers?

CPU0                              CPU1
ext4_setattr()
  inode->i_mode = old_mode
                                  touch_atime() -> ext4_dirty_inode()
                                    ext4_do_update_inode()
                                      ext4_fill_raw_inode()
                                        raw_inode->i_mode = inode->i_mode
                                        <-- snapshots unmasked setid bits
  inode->i_mode = masked_mode
  mark_inode_dirty(inode)

ext4_fill_raw_inode() runs under ei->i_raw_lock without inode->i_rwsem, so
a concurrent atime update can journal the transient unmasked setid bits.
If the following mark_inode_dirty() silently fails in ext4_dirty_inode()
or a crash occurs before it commits, those setid bits persist on disk
alongside the already-applied chown or truncate; lockless readers like
generic_fillattr() can also observe the intermediate mode.

Computing the target mode in a local variable first and only updating
inode->i_mode and calling mark_inode_dirty() when it differs from new_mode
avoids the transient store and skips a redundant journal transaction when
ATTR_KILL_SUID/ATTR_KILL_SGID already matched the masked mode.

> +			mark_inode_dirty(inode);

[Severity: Medium]
Can rolling back inode->i_mode via mark_inode_dirty() silently lose the
on-disk update if ext4_journal_start() fails?

ext4_dirty_inode() starts a new transaction and returns silently if
ext4_journal_start() fails (for example with -ENOMEM under the same memory
pressure that failed posix_acl_chmod()).  Because ext4_write_inode() does
not re-copy inode->i_mode into raw_inode, the on-disk buffer keeps
new_mode while in-memory inode->i_mode holds old_mode, so the failed mode
reappears after eviction or remount.

On -ENOSPC, ext4_set_acl() calls ext4_should_retry_alloc() ->
jbd2_journal_force_commit_nested(), which can commit the earlier
transaction containing new_mode before rollback runs, breaking crash
atomicity.  While posix_acl_chmod() sleeps, lockless
acl_permission_check() can also evaluate permissions against the
unvalidated new_mode, and i_ctime and i_version remain mutated after a
failed mode-only chmod.

Updating the ACL before dirtying the mode change, or performing both in a
single JBD2 transaction, avoids split transactions and silent rollback
failures.

[Severity: Medium]
Does reverting inode->i_mode without advancing i_version or ctime leave
concurrent statx() and NFSv4 caches stuck on new_mode?

CPU0                              CPU1
ext4_setattr()
  inode_inc_iversion() (V -> V+1)
  setattr_copy() (new_mode, T1)
  posix_acl_chmod() (sleeps)
                                  ext4_getattr() -> generic_fillattr()
                                    caches (new_mode, V+1, T1)
  inode->i_mode = old_mode
  mark_inode_dirty(inode)
                                  revalidates: sees (V+1, T1) unchanged
                                  <-- keeps stale new_mode cached

ext4_getattr() reads inode->i_mode, ctime, and inode_query_iversion()
without inode->i_rwsem.  Because the rollback does not call
inode_maybe_inc_iversion() or update ctime, a reader that observed
new_mode during posix_acl_chmod() sees the same change cookie and ctime
afterward and retains new_mode until an unrelated modification updates the
inode.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/tencent_16875A36A63E6B8C6DDCD274A49B38A37D07@qq.com?part=1

  reply	other threads:[~2026-12-03  4:57 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 [this message]
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

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-247169@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.