Linux NFS development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jeff Layton <jlayton@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: [PATCH] vfs: bypass may_create_in_sticky check if task has CAP_FOWNER
Date: Wed, 27 Jul 2022 15:36:17 +0200	[thread overview]
Message-ID: <20220727133617.o7qxhasglx4a2ep5@wittgenstein> (raw)
In-Reply-To: <4085e655f6f22ab185f14cfb6a0c5dee9f12b55e.camel@kernel.org>

On Wed, Jul 27, 2022 at 09:29:37AM -0400, Jeff Layton wrote:
> On Wed, 2022-07-27 at 15:16 +0200, Christian Brauner wrote:
> > On Wed, Jul 27, 2022 at 08:55:35AM -0400, Jeff Layton wrote:
> > > On Wed, 2022-07-27 at 14:37 +0200, Christian Brauner wrote:
> > > > On Wed, Jul 27, 2022 at 08:30:48AM -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. Bypass the checks in
> > > > > may_create_in_sticky if the task has CAP_FOWNER in the given namespace.
> > > > > 
> > > > > Link: https://bugzilla.redhat.com/show_bug.cgi?id=1976829
> > > > > Reported-by: Yongchen Yang <yoyang@redhat.com>
> > > > > Suggested-by: Christian Brauner <brauner@kernel.org>
> > > > > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > > > > ---
> > > > >  fs/namei.c | 3 ++-
> > > > >  1 file changed, 2 insertions(+), 1 deletion(-)
> > > > > 
> > > > > diff --git a/fs/namei.c b/fs/namei.c
> > > > > index 1f28d3f463c3..170c2396ba29 100644
> > > > > --- a/fs/namei.c
> > > > > +++ b/fs/namei.c
> > > > > @@ -1230,7 +1230,8 @@ static int may_create_in_sticky(struct user_namespace *mnt_userns,
> > > > >  	    (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
> > > > >  	    likely(!(dir_mode & S_ISVTX)) ||
> > > > >  	    uid_eq(i_uid_into_mnt(mnt_userns, inode), dir_uid) ||
> > > > > -	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)))
> > > > > +	    uid_eq(current_fsuid(), i_uid_into_mnt(mnt_userns, inode)) ||
> > > > > +	    ns_capable(mnt_userns, CAP_FOWNER))
> > > > >  		return 0;
> > > > 
> > > > Hm, no. You really want inode_owner_or_capable() here..
> > > > You need to verify that you have a mapping for the inode->i_{g,u}id in
> > > > question and that you're having CAP_FOWNER in the caller's userns.
> > > > 
> > > 
> > > Ok, I should be able to make that change and test it out.
> > > 
> > > > I'm pretty sure we should also restrict this to the case were the caller
> > > > actually created the file otherwise we introduce a potential issue where
> > > > the caller is susceptible to data spoofing. For example, the file was
> > > > created by another user racing the caller's O_CREAT.
> > > 
> > > That won't be sufficient to fix the testcase, I think. If a file already
> > > exists in the sticky dir and is owned by nobody:nobody, do we really
> > > want to prevent root from opening it? I wouldn't think so.
> > 
> > Afaict, the whole stick behind the protected_regular thing in
> > may_create_in_sticky() thing is that you prevent scenarios where you can
> > be tricked into opening a file that you didn't intend to with O_CREAT.
> > 
> 
> Yuck. The proper way to get that protection is to use O_EXCL...

I'm not saying the interface was a particularly great idea. But it's at
least a sysctl...

> 
> > That's specifically also a protection for root. So say root specifies
> > O_CREAT but someone beats root to it and creates the file dumping
> > malicious data in there. The uid_eq() requirement is supposed to prevent
> > such attacks and it's a sysctl that userspace opted into.
> > 
> > We'd be relaxing that restriction quite a bit if we not just allow newly
> > created but also pre-existing file to be opened even with the CAP_FOWNER
> > requirement.
> > 
> > So the dd call should really fail if O_CREAT is passed but the file is
> > pre-existing, imho. It's a different story if dd created that file and
> > has CAP_FOWNER imho.
> 
> That's pretty nasty. So if I create a file as root in a sticky dir that
> doesn't exist, and then close it and try to open it again it'll fail
> with -EACCES? That's terribly confusing.

At least only if you try to re-open with O_CREAT and have this
protected_regular sysctl thingy turned on...

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

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-27 12:30 [PATCH] vfs: bypass may_create_in_sticky check if task has CAP_FOWNER Jeff Layton
2022-07-27 12:37 ` Christian Brauner
2022-07-27 12:55   ` Jeff Layton
2022-07-27 13:16     ` Christian Brauner
2022-07-27 13:29       ` Jeff Layton
2022-07-27 13:36         ` Christian Brauner [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=20220727133617.o7qxhasglx4a2ep5@wittgenstein \
    --to=brauner@kernel.org \
    --cc=jlayton@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