All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Mikhalitsyn <alex@amutable.com>
To: Josef Bacik <josef@bacik.io>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Tejun Heo <tj@kernel.org>
Cc: Christian Brauner <brauner@kernel.org>,
	driver-core@lists.linux.dev,  linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] kernfs: refresh the inode before changing attributes
Date: Fri, 04 Dec 2026 10:38:34 +0100	[thread overview]
Message-ID: <0592ebcfff28c908d72b8be856d37b46b1a2398d.camel@amutable.com> (raw)
In-Reply-To: <20261203-b4-kernfs-setattr-refresh-v1-1-b66a2529427e@bacik.io>

On Thu, 2026-12-03 at 10:51 -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>

LGTM

Please feel free to add:

Reviewed-by: Alexander Mikhalitsyn <alex@amutable.com>

Kind regards,
Alex

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

      parent reply	other threads:[~2026-12-04  9:38 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:51 [PATCH] kernfs: refresh the inode before changing attributes Josef Bacik
2026-12-03 19:03 ` Tejun Heo
2026-12-04  9:38 ` Alexander Mikhalitsyn [this message]

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=0592ebcfff28c908d72b8be856d37b46b1a2398d.camel@amutable.com \
    --to=alex@amutable.com \
    --cc=brauner@kernel.org \
    --cc=driver-core@lists.linux.dev \
    --cc=gregkh@linuxfoundation.org \
    --cc=josef@bacik.io \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tj@kernel.org \
    /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.