Re: [PATCH] kernfs: refresh the inode before changing attributes
From: Tejun Heo
Date: Thu Dec 03 2026 - 14:03:49 EST
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@xxxxxxxx>
Acked-by: Tejun Heo <tj@xxxxxxxxxx>
> + 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