linux-fsdevel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff Layton <jlayton@kernel.org>
To: Christian Brauner <brauner@kernel.org>
Cc: viro@zeniv.linux.org.uk, linux-fsdevel@vger.kernel.org,
	linux-nfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	Yongchen Yang <yoyang@redhat.com>
Subject: Re: [RFC PATCH] vfs: don't check may_create_in_sticky if the file is already open/created
Date: Wed, 27 Jul 2022 09:00:46 -0400	[thread overview]
Message-ID: <9f7a1304e3631274701b555589648e12696fa152.camel@kernel.org> (raw)
In-Reply-To: <20220727123207.akq6dlnuqviwtwx5@wittgenstein>

On Wed, 2022-07-27 at 14:32 +0200, Christian Brauner wrote:
> On Wed, Jul 27, 2022 at 08:04:34AM -0400, Jeff Layton wrote:
> > On Wed, 2022-07-27 at 13:34 +0200, Christian Brauner wrote:
> > > On Tue, Jul 26, 2022 at 04:27:56PM -0400, Jeff Layton wrote:
> > > > On Tue, 2022-07-26 at 16:23 -0400, Jeff Layton wrote:
> > > > > NFS server is exporting a sticky directory (mode 01777) with root
> > > > > squashing enabled. Client has protect_regular enabled and then tries to
> > > > > open a file as root in that directory. File is created (with ownership
> > > > > set to nobody:nobody) but the open syscall returns an error.
> > > > > 
> > > > > The problem is may_create_in_sticky, which rejects the open even though
> > > > > the file has already been created/opened. Only call may_create_in_sticky
> > > > > if the file hasn't already been opened or created.
> > > > > 
> > > > > Cc: Christian Brauner <brauner@kernel.org>
> > > > > Link: https://bugzilla.redhat.com/show_bug.cgi?id=1976829
> > > > > Reported-by: Yongchen Yang <yoyang@redhat.com>
> > > > > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > > > > ---
> > > > >  fs/namei.c | 13 +++++++++----
> > > > >  1 file changed, 9 insertions(+), 4 deletions(-)
> > > > > 
> > > > > diff --git a/fs/namei.c b/fs/namei.c
> > > > > index 1f28d3f463c3..7480b6dc8d27 100644
> > > > > --- a/fs/namei.c
> > > > > +++ b/fs/namei.c
> > > > > @@ -3495,10 +3495,15 @@ static int do_open(struct nameidata *nd,
> > > > >  			return -EEXIST;
> > > > >  		if (d_is_dir(nd->path.dentry))
> > > > >  			return -EISDIR;
> > > > > -		error = may_create_in_sticky(mnt_userns, nd,
> > > > > -					     d_backing_inode(nd->path.dentry));
> > > > > -		if (unlikely(error))
> > > > > -			return error;
> > > > > +		if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
> > > > > +			error = may_create_in_sticky(mnt_userns, nd,
> > > > > +						d_backing_inode(nd->path.dentry));
> > > > > +			if (unlikely(error)) {
> > > > > +				printk("%s: f_mode=0x%x oflag=0x%x\n",
> > > > > +					__func__, file->f_mode, open_flag);
> > > > > +				return error;
> > > > > +			}
> > > > > +		}
> > > > >  	}
> > > > >  	if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
> > > > >  		return -ENOTDIR;
> > > > 
> > > > I'm pretty sure this patch is the wrong approach, actually, since it
> > > > doesn't fix the regular (non-atomic) open codepath. Any thoughts on what
> > > 
> > > Hey Jeff,
> > > 
> > > I haven't quite understood why that won't work for the regular open
> > > codepaths. I'm probably missing something obvious.
> > > 
> > 
> > In the normal open codepaths, FMODE_OPENED | FMODE_CREATED are still
> > clear. If we're not doing an atomic_open (i.e. the dentry doesn't exist
> > yet or is negative), then nothing really happens until you get to the
> > vfs_open call.
> 
> Hm, so for atomic open with O_CREAT it's:
> 
> path_openat()
> -> open_last_lookups()
>    -> lookup_open()
>       /* 
>        * This is ->atomic_open() and FMODE_CREATED is set in the fs so
>        * for NFS it's done in:
>        * fs/nfs/dir.c:           file->f_mode |= FMODE_CREATED;
>        */
>       -> atomic_open()
> 
> and for regular O_CREAT open it's:
> 
> path_openat()
> -> open_last_lookups()
>    -> lookup_open()
>       {
>         if (!dentry->d_inode && (open_flag & O_CREAT)) {
>                 file->f_mode |= FMODE_CREATED;
>       }
> 
> 
> and that should all get surfaced to:
> 
> path_openat()
>    -> do_open()
>       -> may_create_in_sticky()
> 
> ?

Basically, yes, but we also need to deal with the case where the file
already exists. That's being denied currently too and I don't think it
should be.

-- 
Jeff Layton <jlayton@kernel.org>

  reply	other threads:[~2022-07-27 13:00 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-26 20:23 [RFC PATCH] vfs: don't check may_create_in_sticky if the file is already open/created Jeff Layton
2022-07-26 20:27 ` Jeff Layton
2022-07-27 11:34   ` Christian Brauner
2022-07-27 11:48     ` Christian Brauner
2022-07-27 12:04     ` Jeff Layton
2022-07-27 12:32       ` Christian Brauner
2022-07-27 13:00         ` Jeff Layton [this message]
2022-07-27 13:17           ` Christian Brauner

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=9f7a1304e3631274701b555589648e12696fa152.camel@kernel.org \
    --to=jlayton@kernel.org \
    --cc=brauner@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-nfs@vger.kernel.org \
    --cc=viro@zeniv.linux.org.uk \
    --cc=yoyang@redhat.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).