* [PATCH] kernfs: refresh the inode before changing attributes
@ 2026-12-03 15:51 Josef Bacik
2026-12-03 19:03 ` Tejun Heo
0 siblings, 1 reply; 2+ messages in thread
From: Josef Bacik @ 2026-12-03 15:51 UTC (permalink / raw)
To: Greg Kroah-Hartman, Tejun Heo
Cc: Christian Brauner, driver-core, linux-fsdevel, linux-kernel
The owner and mode of a node are kept in the kernfs_node. An inode only
has a copy, which ->permission() and ->getattr() bring up to date.
kernfs_iop_setattr() does not, so setattr_prepare() can go by old values
and, for one, fail a chmod by the owner of the node with EPERM.
Refresh the inode first. notify_change() has looked at the copy as well
by then, so compute the mode for ATTR_KILL_S[UG]ID and check ATTR_TOUCH
again.
Fixes: 3ff195b011d7 ("sysfs: Implement sysfs tagged directory support.")
Signed-off-by: Josef Bacik <josef@bacik.io>
---
fs/kernfs/inode.c | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/fs/kernfs/inode.c b/fs/kernfs/inode.c
index 6630b29d7c078..88d0c0c875184 100644
--- a/fs/kernfs/inode.c
+++ b/fs/kernfs/inode.c
@@ -107,6 +107,31 @@ int kernfs_setattr(struct kernfs_node *kn, const struct iattr *iattr)
return ret;
}
+static void kernfs_refresh_inode(struct kernfs_node *kn, struct inode *inode);
+
+/* Redo what notify_change() decided by the inode as it was cached. */
+static int kernfs_setattr_refresh(struct kernfs_node *kn, struct inode *inode,
+ struct iattr *iattr)
+{
+ unsigned int ia_valid = iattr->ia_valid;
+
+ kernfs_refresh_inode(kn, 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;
+ }
+
+ /* may_setattr() would take kernfs_iattr_rwsem again */
+ if ((ia_valid & ATTR_TOUCH) &&
+ !inode_owner_or_capable(&nop_mnt_idmap, inode))
+ return generic_permission(&nop_mnt_idmap, inode, MAY_WRITE);
+ return 0;
+}
+
int kernfs_iop_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
struct iattr *iattr)
{
@@ -120,6 +145,10 @@ int kernfs_iop_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
root = kernfs_root(kn);
down_write(&root->kernfs_iattr_rwsem);
+ error = kernfs_setattr_refresh(kn, inode, iattr);
+ if (error)
+ goto out;
+
error = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
if (error)
goto out;
---
base-commit: b8e026b973764a52b92c0cdc1d3cf96f2abc1058
change-id: 20261203-b4-kernfs-setattr-refresh-b66a2529427e
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] kernfs: refresh the inode before changing attributes
2026-12-03 15:51 [PATCH] kernfs: refresh the inode before changing attributes Josef Bacik
@ 2026-12-03 19:03 ` Tejun Heo
0 siblings, 0 replies; 2+ messages in thread
From: Tejun Heo @ 2026-12-03 19:03 UTC (permalink / raw)
To: Josef Bacik
Cc: Greg Kroah-Hartman, Christian Brauner, driver-core, linux-fsdevel,
linux-kernel
On Thu, Dec 03, 2026 at 10:51:06AM -0500, Josef Bacik wrote:
> The owner and mode of a node are kept in the kernfs_node. An inode only
> has a copy, which ->permission() and ->getattr() bring up to date.
> kernfs_iop_setattr() does not, so setattr_prepare() can go by old values
> and, for one, fail a chmod by the owner of the node with EPERM.
>
> Refresh the inode first. notify_change() has looked at the copy as well
> by then, so compute the mode for ATTR_KILL_S[UG]ID and check ATTR_TOUCH
> again.
>
> Fixes: 3ff195b011d7 ("sysfs: Implement sysfs tagged directory support.")
> Signed-off-by: Josef Bacik <josef@bacik.io>
Acked-by: Tejun Heo <tj@kernel.org>
> + 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;
> + }
notify_change() also decides whether to set ATTR_MODE from the old i_mode
and that part isn't redone. If the cached mode has no setuid bit and the
node has one, a chown leaves the bit in place. The other way around,
ATTR_MODE stays set and the chown fails with -EPERM for a caller without
CAP_FOWNER. Both behave the same without the patch, so a follow-up is fine.
Maybe clear ATTR_MODE here and set it again from the refreshed mode?
Thanks.
--
tejun
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-12-03 19:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-03 15:51 [PATCH] kernfs: refresh the inode before changing attributes Josef Bacik
2026-12-03 19:03 ` Tejun Heo
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox