Re: [PATCH] kernfs: use the refreshed mode to drop setid on chown
From: Tejun Heo
Date: Fri Dec 04 2026 - 10:59:04 EST
On Fri, Dec 04, 2026 at 10:35:15AM -0500, Josef Bacik wrote:
> 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@xxxxxxxxxx>
> Closes: https://lore.kernel.org/all/1347ee3f0a83e14e2ef5db37147e3966@xxxxxxxxxx/
> Signed-off-by: Josef Bacik <josef@xxxxxxxx>
Acked-by: Tejun Heo <tj@xxxxxxxxxx>
Thanks.
--
tejun