Linux filesystem development
 help / color / mirror / Atom feed
* [PATCH] kernfs: use the refreshed mode to drop setid on chown
@ 2026-12-04 15:35 Josef Bacik
  2026-12-04 15:58 ` Tejun Heo
  0 siblings, 1 reply; 2+ messages in thread
From: Josef Bacik @ 2026-12-04 15:35 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Tejun Heo
  Cc: Christian Brauner, driver-core, linux-fsdevel, linux-kernel

A chown through an inode with an old copy of the mode can leave setuid
or setgid set on the node, drop a setgid bit that should stay, or fail
with EPERM for a caller that neither owns the node nor has CAP_FOWNER.
All three reproduce with sysfs mounted in two network namespaces, a
chmod through one mount and the chown through the other.

notify_change() sets ATTR_MODE only if the copy has a bit to drop, and
chown_common() goes by the copy for ATTR_KILL_SGID. "kernfs: refresh
the inode before changing attributes", which this builds on, computes
the mode again but keeps both decisions.

Fix by making both decisions again from the refreshed inode, which also
means EPERM for such a caller where only the node has a bit to drop, as
with a current copy.

Fixes: 3ff195b011d7 ("sysfs: Implement sysfs tagged directory support.")
Reported-by: Tejun Heo <tj@kernel.org>
Closes: https://lore.kernel.org/all/1347ee3f0a83e14e2ef5db37147e3966@kernel.org/
Signed-off-by: Josef Bacik <josef@bacik.io>
---
Applies on top of
https://lore.kernel.org/all/20261203-b4-kernfs-setattr-refresh-v1-1-b66a2529427e@bacik.io/

 fs/kernfs/inode.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 88d0c0c875184..ef0c0093e1c50 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -117,12 +117,25 @@ static int kernfs_setattr_refresh(struct kernfs_node *kn, struct inode *inode,
 
 	kernfs_refresh_inode(kn, inode);
 
+	/*
+	 * ATTR_KILL_SUID without ATTR_FORCE is a chown, which picked
+	 * ATTR_KILL_SGID by the cached inode too.
+	 */
+	if ((ia_valid & (ATTR_KILL_SUID | ATTR_FORCE)) == ATTR_KILL_SUID) {
+		ia_valid &= ~ATTR_KILL_SGID;
+		ia_valid |= setattr_should_drop_sgid(&nop_mnt_idmap, inode);
+	}
+
 	if (ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
 		iattr->ia_mode = inode->i_mode;
 		if (ia_valid & ATTR_KILL_SUID)
 			iattr->ia_mode &= ~S_ISUID;
 		if (ia_valid & ATTR_KILL_SGID)
 			iattr->ia_mode &= ~S_ISGID;
+
+		iattr->ia_valid &= ~ATTR_MODE;
+		if (iattr->ia_mode != inode->i_mode)
+			iattr->ia_valid |= ATTR_MODE;
 	}
 
 	/* may_setattr() would take kernfs_iattr_rwsem again */
---
base-commit: b8e026b973764a52b92c0cdc1d3cf96f2abc1058
change-id: 20261203-b4-kernfs-chown-mode-8dfef37010fb
prerequisite-change-id: 20261203-b4-kernfs-setattr-refresh-b66a2529427e:v1
prerequisite-patch-id: bccd06fdcc285b16afbaea74d74e5a7e77c2518d

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-04 15:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:35 [PATCH] kernfs: use the refreshed mode to drop setid on chown Josef Bacik
2026-12-04 15:58 ` Tejun Heo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox