[v4,2/3] nsproxy: attach to namespaces via pidfds

Submitted by Michal Koutný on June 24, 2020, 11:44 a.m.

Details

Message ID 20200624114437.GA117125@blackbook
State New
Series "nsproxy: attach to multiple namespaces"
Headers show

Commit Message

Michal Koutný June 24, 2020, 11:44 a.m.
Hi.

On Tue, May 05, 2020 at 04:04:31PM +0200, Christian Brauner <christian.brauner@ubuntu.com> wrote:
> -SYSCALL_DEFINE2(setns, int, fd, int, nstype)
> +SYSCALL_DEFINE2(setns, int, fd, int, flags)
> [...]
> -	file = proc_ns_fget(fd);
> -	if (IS_ERR(file))
> -		return PTR_ERR(file);
> +	int err = 0;
>  
> -	err = -EINVAL;
> -	ns = get_proc_ns(file_inode(file));
> -	if (nstype && (ns->ops->type != nstype))
> +	file = fget(fd);
> +	if (!file)
> +		return -EBADF;
> +
> +	if (proc_ns_file(file)) {
> +		ns = get_proc_ns(file_inode(file));
> +		if (flags && (ns->ops->type != flags))
> +			err = -EINVAL;
> +		flags = ns->ops->type;
> +	} else if (pidfd_pid(file)) {
> +		err = check_setns_flags(flags);
> +	} else {
> +		err = -EBADF;
> +	}
> +	if (err)
>  		goto out;
>  
> -	err = prepare_nsset(ns->ops->type, &nsset);
> +	err = prepare_nsset(flags, &nsset);
>  	if (err)
>  		goto out;
This modification changed the returned error when a valid file
descriptor is passed but it doesn't represent a namespace (nor pidfd).
The error is now EBADF although originally and per man page it
was/should be EINVAL.

A change like below would restore it, however, I see it may be less
consistent with other pidfd calls(?), then I'd suggest updating the
manpage to capture this.


I noticed this breaks systemd self tests [1].

Regards,
Michal


[1] https://github.com/systemd/systemd/blob/a1ba8c5b71164665ccb53c9cec384e5eef7d3689/src/test/test-seccomp.c#L246

Patch hide | download patch | download mbox

--- a/kernel/nsproxy.c
+++ b/kernel/nsproxy.c
@@ -531,7 +531,7 @@  SYSCALL_DEFINE2(setns, int, fd, int, flags)
        } else if (!IS_ERR(pidfd_pid(file))) {
                err = check_setns_flags(flags);
        } else {
-               err = -EBADF;
+               err = -EINVAL;
        }
        if (err)
                goto out;

Comments

Christian Brauner June 24, 2020, 11:54 a.m.
On Wed, Jun 24, 2020 at 01:44:37PM +0200, Michal Koutný wrote:
> Hi.
> 
> On Tue, May 05, 2020 at 04:04:31PM +0200, Christian Brauner <christian.brauner@ubuntu.com> wrote:
> > -SYSCALL_DEFINE2(setns, int, fd, int, nstype)
> > +SYSCALL_DEFINE2(setns, int, fd, int, flags)
> > [...]
> > -	file = proc_ns_fget(fd);
> > -	if (IS_ERR(file))
> > -		return PTR_ERR(file);
> > +	int err = 0;
> >  
> > -	err = -EINVAL;
> > -	ns = get_proc_ns(file_inode(file));
> > -	if (nstype && (ns->ops->type != nstype))
> > +	file = fget(fd);
> > +	if (!file)
> > +		return -EBADF;
> > +
> > +	if (proc_ns_file(file)) {
> > +		ns = get_proc_ns(file_inode(file));
> > +		if (flags && (ns->ops->type != flags))
> > +			err = -EINVAL;
> > +		flags = ns->ops->type;
> > +	} else if (pidfd_pid(file)) {
> > +		err = check_setns_flags(flags);
> > +	} else {
> > +		err = -EBADF;
> > +	}
> > +	if (err)
> >  		goto out;
> >  
> > -	err = prepare_nsset(ns->ops->type, &nsset);
> > +	err = prepare_nsset(flags, &nsset);
> >  	if (err)
> >  		goto out;
> This modification changed the returned error when a valid file
> descriptor is passed but it doesn't represent a namespace (nor pidfd).
> The error is now EBADF although originally and per man page it
> was/should be EINVAL.
> 
> A change like below would restore it, however, I see it may be less
> consistent with other pidfd calls(?), then I'd suggest updating the
> manpage to capture this.
> 
> --- a/kernel/nsproxy.c
> +++ b/kernel/nsproxy.c
> @@ -531,7 +531,7 @@ SYSCALL_DEFINE2(setns, int, fd, int, flags)
>         } else if (!IS_ERR(pidfd_pid(file))) {
>                 err = check_setns_flags(flags);
>         } else {
> -               err = -EBADF;
> +               err = -EINVAL;
>         }
>         if (err)
>                 goto out;
> 
> I noticed this breaks systemd self tests [1].

Yep, I already have a fix for this in my tree based on a previous report
from LTP. It's sitting in linux-next:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/commit/?h=fixes&id=e571d4ee334719727f22cce30c4c74471d4ef68a
with selftests:
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/commit/?h=fixes&id=86f56395feb2b106b125c47e72192e37da5dd088

I'll send it to Linus this week.

Thanks!
Christian
Michal Koutný June 24, 2020, 12:01 p.m.
On Wed, Jun 24, 2020 at 01:54:56PM +0200, Christian Brauner <christian.brauner@ubuntu.com> wrote:
> Yep, I already have a fix for this in my tree based on a previous
> report from LTP.
Perfect. (Sorry for the noise then.)

Thanks,
Michal