nsproxy: attach to multiple namespaces
Subsystems (9)
v3 cover nsproxy: attach to multiple namespaces
This is v3.
The permission bits have already seen some vetting which has been helpful and allowed us to drop the ns_capable_cred() patch. That's the only major change.
All selftests pass. People interested in playing with this can get it from three locations as usual (it's not yet in my for-next of course):
https://github.com/brauner/linux/tree/setns_pidfd
https://gitlab.com/brauner/linux/-/commits/setns_pidfd
https://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux.git/log/?h=setns_pidfd
May the Fourth be with you.
Christian
Christian Brauner (3):
nsproxy: add struct nsset
nsproxy: attach to namespaces via pidfds
selftests/pidfd: add pidfd setns tests
fs/namespace.c | 15 +- fs/nsfs.c | 7 +- include/linux/mnt_namespace.h | 2 + include/linux/nsproxy.h | 24 + include/linux/proc_fs.h | 6 + include/linux/proc_ns.h | 4 +- ipc/namespace.c | 7 +- kernel/cgroup/namespace.c | 5 +- kernel/nsproxy.c | 306 ++++++++++- kernel/pid_namespace.c | 5 +- kernel/time/namespace.c | 5 +- kernel/user_namespace.c | 8 +- kernel/utsname.c | 5 +- net/core/net_namespace.c | 5 +- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- tools/testing/selftests/pidfd/config | 6 + .../selftests/pidfd/pidfd_setns_test.c | 473 ++++++++++++++++++ 18 files changed, 839 insertions(+), 48 deletions(-) create mode 100644 tools/testing/selftests/pidfd/config create mode 100644 tools/testing/selftests/pidfd/pidfd_setns_test.c
base-commit: ae83d0b416db002fe95601e7f97f64b59514d936
Signature · 1 line
2.26.2
PATCH v3 1/3 nsproxy: add struct nsset
Add a simple struct nsset. It holds all necessary pieces to switch to a new set of namespaces without leaving a task in a half-switched state which we will make use of in the next patch. This patch simply switches the existing setns logic over without causing a change in setns() behavior. This brings setns() closer to how unshare() works(). The prepare_ns() function is responsible to prepare all necessary information. This has two reasons. First it minimizes dependencies between individual namespaces, i.e. all install handler can expect that all fields are properly initialized independent in what order they are called in. Second, this makes the code easier to maintain and easier to follow if it needs to be changed.
---
/* v2 */
patch introduced
/* v3 */
- Eric W. Biederman <[email protected]>:
- Remove the prior ns_capable_cred() patch and simplify the permission
check from ns_capable_cred(nsset, nsset->cred->user_ns, CAP_SYS_ADMIN))
to from ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN)).
patch introduced
---
fs/namespace.c | 10 ++--
include/linux/mnt_namespace.h | 1 +
include/linux/nsproxy.h | 24 +++++++++
include/linux/proc_ns.h | 4 +-
ipc/namespace.c | 7 ++-
kernel/cgroup/namespace.c | 5 +-
kernel/nsproxy.c | 94 ++++++++++++++++++++++++++++++-----
kernel/pid_namespace.c | 5 +-
kernel/time/namespace.c | 5 +-
kernel/user_namespace.c | 8 +--
kernel/utsname.c | 5 +-
net/core/net_namespace.c | 5 +-
12 files changed, 137 insertions(+), 36 deletions(-)fs/namespace.c +6 -4
@@ -3954,16 +3954,18 @@ static void mntns_put(struct ns_common *ns)put_mnt_ns (to_mnt_ns (ns)); } -static int mntns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int mntns_install (struct nsset *nsset,struct ns_common *ns) { -struct fs_struct *fs = current->fs; +struct nsproxy *nsproxy = nsset->nsproxy; +struct fs_struct *fs = nsset->fs;struct mnt_namespace *mnt_ns =to_mnt_ns (ns), *old_mnt_ns; +struct user_namespace *user_ns = nsset->cred->user_ns;struct path root;int err;if (!ns_capable (mnt_ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_CHROOT ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (user_ns,CAP_SYS_CHROOT ) || + !ns_capable (user_ns,CAP_SYS_ADMIN ))return -EPERM ;if (is_anon_ns (mnt_ns))
include/linux/mnt_namespace.h +1 -0
@@ -6,6 +6,7 @@struct mnt_namespace ;struct fs_struct ;struct user_namespace ; +struct ns_common ;extern struct mnt_namespace *copy_mnt_ns (unsigned long ,struct mnt_namespace *,struct user_namespace *,struct fs_struct *);
include/linux/nsproxy.h +24 -0
@@ -41,6 +41,30 @@ struct nsproxy { };extern struct nsproxy init_nsproxy; +/* +* A structure to encompass all bits needed to install +* a partial or complete new set of namespaces. +* +* If a new user namespace is requested cred will +* point to a modifiable set of credentials. If a pointer +* to a modifiable set is needed nsset_cred() must be +* used and tested. +*/ +struct nsset { +unsigned flags; +struct nsproxy *nsproxy; +struct fs_struct *fs; +const struct cred *cred; +}; + +static inline struct cred *nsset_cred (struct nsset *set) +{ +if (set->flags &CLONE_NEWUSER ) +return (struct cred *)set->cred; + +return NULL ; +} +/* * the namespaces access rules are: *
include/linux/proc_ns.h +2 -2
@@ -8,7 +8,7 @@#include <linux/ns_common.h> struct pid_namespace ; -struct nsproxy ; +struct nsset ;struct path ;struct task_struct ;struct inode ;
@@ -19,7 +19,7 @@ struct proc_ns_operations {int type;struct ns_common *(*get)(struct task_struct *task);void (*put)(struct ns_common *ns); -int (*install)(struct nsproxy *nsproxy,struct ns_common *ns); +int (*install)(struct nsset *nsset,struct ns_common *ns);struct user_namespace *(*owner)(struct ns_common *ns);struct ns_common *(*get_parent)(struct ns_common *ns); } __randomize_layout;
ipc/namespace.c +3 -4
@@ -177,15 +177,14 @@ static void ipcns_put(struct ns_common *ns)return put_ipc_ns (to_ipc_ns (ns)); } -static int ipcns_install (struct nsproxy *nsproxy,struct ns_common *new) +static int ipcns_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct ipc_namespace *ns =to_ipc_ns (new);if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ; -/* Ditch state from the old ipc namespace */ -exit_sem (current);put_ipc_ns (nsproxy->ipc_ns); nsproxy->ipc_ns =get_ipc_ns (ns);return 0 ;
kernel/cgroup/namespace.c +3 -2
@@ -95,11 +95,12 @@ static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns) return container_of(ns, struct cgroup_namespace, ns); } -static int cgroupns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int cgroupns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct cgroup_namespace *cgroup_ns =to_cg_ns (ns); -if (!ns_capable (current_user_ns (),CAP_SYS_ADMIN ) || +if (!ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ) || !ns_capable (cgroup_ns->user_ns,CAP_SYS_ADMIN ))return -EPERM ;
kernel/nsproxy.c +82 -12
120 lines not on this page · raw message
kernel/pid_namespace.c +3 -2
@@ -378,13 +378,14 @@ static void pidns_put(struct ns_common *ns)put_pid_ns (to_pid_ns (ns)); } -static int pidns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int pidns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct pid_namespace *active =task_active_pid_ns (current);struct pid_namespace *ancestor, *new =to_pid_ns (ns);if (!ns_capable (new->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;/*
kernel/time/namespace.c +3 -2
@@ -280,8 +280,9 @@ static void timens_put(struct ns_common *ns)put_time_ns (to_time_ns (ns)); } -static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) +static int timens_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct time_namespace *ns =to_time_ns (new);int err;
@@ -289,7 +290,7 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)return -EUSERS ;if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;timens_set_vvar_page (current, ns);
kernel/user_namespace.c +4 -4
@@ -1253,7 +1253,7 @@ static void userns_put(struct ns_common *ns)put_user_ns (to_user_ns (ns)); } -static int userns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int userns_install (struct nsset *nsset,struct ns_common *ns) {struct user_namespace *user_ns =to_user_ns (ns);struct cred *cred;
@@ -1274,14 +1274,14 @@ static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)if (!ns_capable (user_ns,CAP_SYS_ADMIN ))return -EPERM ; - cred =prepare_creds (); + cred =nsset_cred (nsset);if (!cred) -return -ENOMEM ; +return -EINVAL ;put_user_ns (cred->user_ns);set_cred_user_ns (cred,get_user_ns (user_ns)); -return commit_creds (cred); +return 0 ; }struct ns_common *ns_get_owner (struct ns_common *ns)
kernel/utsname.c +3 -2
@@ -140,12 +140,13 @@ static void utsns_put(struct ns_common *ns)put_uts_ns (to_uts_ns (ns)); } -static int utsns_install (struct nsproxy *nsproxy,struct ns_common *new) +static int utsns_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct uts_namespace *ns =to_uts_ns (new);if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;get_uts_ns (ns);
net/core/net_namespace.c +3 -2
@@ -1353,12 +1353,13 @@ static void netns_put(struct ns_common *ns)put_net (to_net_ns (ns)); } -static int netns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int netns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct net *net =to_net_ns (ns);if (!ns_capable (net->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;put_net (nsproxy->net_ns);
Signature · 1 line
2.26.2
Re: [PATCH v3 1/3] nsproxy: add struct nsset
Quoted 10 lines · Christian Brauner
Add a simple struct nsset. It holds all necessary pieces to switch to a new set of namespaces without leaving a task in a half-switched state which we will make use of in the next patch. This patch simply switches the existing setns logic over without causing a change in
setns()behavior. This bringssetns()closer to howunshare()works(). Theprepare_ns()function is responsible to prepare all necessary information. This has two reasons. First it minimizes dependencies between individual namespaces, i.e. all install handler can expect that all fields are properly initialized independent in what order they are called in. Second, this makes the code easier to maintain and easier to follow if it needs to be changed.
This is buggy.
Your code assume that nstype == 0 is invalid.
Passing nstype == 0 means don't verify the kind of file descriptor passed.
Quite frankly doing nstype & CLONE_XYZ is wrong. It always needs to be nstype == CLONE_XYZ.
Maybe we change that in a later patch but here where you are just upgrading the infrastructure semantics changes are not ok.
Eric
Quoted 412 lines
Cc Eric W. Biederman <[email protected]>Cc Serge Hallyn <[email protected]>Cc Jann Horn <[email protected]>Cc Michael Kerrisk <[email protected]>Cc Aleksa Sarai <[email protected]>Signed-off-by Christian Brauner <[email protected]>---
/* v2 */
patch introduced/* v3 */
- Eric W. Biederman <[email protected]>:
- Remove the priorns_capable_cred()patch and simplify the permissioncheck from ns_capable_cred(nsset, nsset->cred->user_ns, CAP_SYS_ADMIN)) to from ns_capable(nsset->cred->user_ns, CAP_SYS_ADMIN)). patch introduced --- fs/namespace.c | 10 ++-- include/linux/mnt_namespace.h | 1 + include/linux/nsproxy.h | 24 +++++++++ include/linux/proc_ns.h | 4 +- ipc/namespace.c | 7 ++- kernel/cgroup/namespace.c | 5 +- kernel/nsproxy.c | 94 ++++++++++++++++++++++++++++++----- kernel/pid_namespace.c | 5 +- kernel/time/namespace.c | 5 +- kernel/user_namespace.c | 8 +-- kernel/utsname.c | 5 +- net/core/net_namespace.c | 5 +- 12 files changed, 137 insertions(+), 36 deletions(-)fs/namespace.c +6
-4@@ -3954,16 +3954,18 @@ static void mntns_put(struct ns_common *ns)put_mnt_ns (to_mnt_ns (ns)); } -static int mntns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int mntns_install (struct nsset *nsset,struct ns_common *ns) { -struct fs_struct *fs = current->fs; +struct nsproxy *nsproxy = nsset->nsproxy; +struct fs_struct *fs = nsset->fs;struct mnt_namespace *mnt_ns =to_mnt_ns (ns), *old_mnt_ns; +struct user_namespace *user_ns = nsset->cred->user_ns;struct path root;int err;if (!ns_capable (mnt_ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_CHROOT ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (user_ns,CAP_SYS_CHROOT ) || + !ns_capable (user_ns,CAP_SYS_ADMIN ))return -EPERM ;if (is_anon_ns (mnt_ns))include/linux/mnt_namespace.h +1
-0@@ -6,6 +6,7 @@struct mnt_namespace ;struct fs_struct ;struct user_namespace ; +struct ns_common ;extern struct mnt_namespace *copy_mnt_ns (unsigned long ,struct mnt_namespace *,struct user_namespace *,struct fs_struct *);include/linux/nsproxy.h +24
-0@@ -41,6 +41,30 @@ struct nsproxy { };extern struct nsproxy init_nsproxy; +/* +* A structure to encompass all bits needed to install +* a partial or complete new set of namespaces. +* +* If a new user namespace is requested cred will +* point to a modifiable set of credentials. If a pointer +* to a modifiable set is needed nsset_cred() must be +* used and tested. +*/ +struct nsset { +unsigned flags; +struct nsproxy *nsproxy; +struct fs_struct *fs; +const struct cred *cred; +}; + +static inline struct cred *nsset_cred (struct nsset *set) +{ +if (set->flags &CLONE_NEWUSER ) +return (struct cred *)set->cred; + +return NULL ; +} +/* * the namespaces access rules are: * include/linux/proc_ns.h +2
-2@@ -8,7 +8,7 @@#include <linux/ns_common.h> struct pid_namespace ; -struct nsproxy ; +struct nsset ;struct path ;struct task_struct ;struct inode ;@@ -19,7 +19,7 @@ struct proc_ns_operations {int type;struct ns_common *(*get)(struct task_struct *task);void (*put)(struct ns_common *ns); -int (*install)(struct nsproxy *nsproxy,struct ns_common *ns); +int (*install)(struct nsset *nsset,struct ns_common *ns);struct user_namespace *(*owner)(struct ns_common *ns);struct ns_common *(*get_parent)(struct ns_common *ns); } __randomize_layout;ipc/namespace.c +3
-4@@ -177,15 +177,14 @@ static void ipcns_put(struct ns_common *ns)return put_ipc_ns (to_ipc_ns (ns)); } -static int ipcns_install (struct nsproxy *nsproxy,struct ns_common *new) +static int ipcns_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct ipc_namespace *ns =to_ipc_ns (new);if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ; -/* Ditch state from the old ipc namespace */ -exit_sem (current);put_ipc_ns (nsproxy->ipc_ns); nsproxy->ipc_ns =get_ipc_ns (ns);return 0 ;kernel/cgroup/namespace.c +3
-2@@ -95,11 +95,12 @@ static inline struct cgroup_namespace *to_cg_ns(struct ns_common *ns) return container_of(ns, struct cgroup_namespace, ns); } -static int cgroupns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int cgroupns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct cgroup_namespace *cgroup_ns =to_cg_ns (ns); -if (!ns_capable (current_user_ns (),CAP_SYS_ADMIN ) || +if (!ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ) || !ns_capable (cgroup_ns->user_ns,CAP_SYS_ADMIN ))return -EPERM ;kernel/nsproxy.c +82
-12@@ -19,6 +19,7 @@#include <net/net_namespace.h> #include <linux/ipc_namespace.h> #include <linux/time_namespace.h> +#include <linux/fs_struct.h> #include <linux/proc_ns.h> #include <linux/file.h> #include <linux/syscalls.h> @@ -257,12 +258,85 @@ void exit_task_namespaces(struct task_struct *p)switch_task_namespaces (p,NULL ); } +static void put_nsset (struct nsset *nsset) +{ +unsigned flags = nsset->flags; + +if (flags &CLONE_NEWUSER ) +put_cred (nsset_cred (nsset)); +if (nsset->nsproxy) +free_nsproxy (nsset->nsproxy); +} + +static int prepare_nsset (unsigned flags,struct nsset *nsset) +{ +struct task_struct *me = current; + + nsset->nsproxy =create_new_namespaces (0 , me,current_user_ns (), me->fs); +if (IS_ERR (nsset->nsproxy)) +return PTR_ERR (nsset->nsproxy); + +if (flags &CLONE_NEWUSER ) + nsset->cred =prepare_creds (); +else + nsset->cred =current_cred (); +if (!nsset->cred) + goto out; + +if (flags &CLONE_NEWNS ) + nsset->fs = me->fs; + + nsset->flags = flags; +return 0 ; + +out: +put_nsset (nsset); +return -ENOMEM ; +} + +/* +* This is the point of no return. There are just a few namespaces +* that do some actual work here and it's sufficiently minimal that +* a separate ns_common operation seems unnecessary for now. +* Unshare is doing the same thing. If we'll end up needing to do +* more in a given namespace or a helper here is ultimately not +* exported anymore a simple commit handler for each namespace +* should be added to ns_common. +*/ +static void commit_nsset (struct nsset *nsset) +{ +unsigned flags = nsset->flags; +struct task_struct *me = current; + +#ifdef CONFIG_USER_NS +if (flags &CLONE_NEWUSER ) { +/* transfer ownership */ +commit_creds (nsset_cred (nsset)); + nsset->cred =NULL ; + } +#endif + +#ifdef CONFIG_IPC_NS +if (flags &CLONE_NEWIPC ) +exit_sem (me); +#endif + +/* transfer ownership */ +switch_task_namespaces (me, nsset->nsproxy); + nsset->nsproxy =NULL ; +} + +static inline int validate_ns (struct nsset *nsset,struct ns_common *ns) +{ +return ns->ops->install (nsset, ns); +} + SYSCALL_DEFINE2(setns, int, fd, int, nstype) {struct task_struct *tsk = current; -struct nsproxy *new_nsproxy;struct file *file;struct ns_common *ns; +struct nsset nsset = {};int err; file =proc_ns_fget (fd);@@ -274,20 +348,16 @@ SYSCALL_DEFINE2(setns, int, fd, int, nstype)if (nstype && (ns->ops->type != nstype)) goto out; - new_nsproxy =create_new_namespaces (0 , tsk,current_user_ns (), tsk->fs); -if (IS_ERR (new_nsproxy)) { - err =PTR_ERR (new_nsproxy); + err =prepare_nsset (nstype, &nsset); +if (err) goto out; - } - err = ns->ops->install (new_nsproxy, ns); -if (err) { -free_nsproxy (new_nsproxy); - goto out; + err =validate_ns (&nsset, ns); +if (!err) { +commit_nsset (&nsset); +perf_event_namespaces (tsk); } -switch_task_namespaces (tsk, new_nsproxy); - -perf_event_namespaces (tsk); +put_nsset (&nsset); out:fput (file);return err;kernel/pid_namespace.c +3
-2@@ -378,13 +378,14 @@ static void pidns_put(struct ns_common *ns)put_pid_ns (to_pid_ns (ns)); } -static int pidns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int pidns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct pid_namespace *active =task_active_pid_ns (current);struct pid_namespace *ancestor, *new =to_pid_ns (ns);if (!ns_capable (new->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;/* kernel/time/namespace.c +3
-2@@ -280,8 +280,9 @@ static void timens_put(struct ns_common *ns)put_time_ns (to_time_ns (ns)); } -static int timens_install (struct nsproxy *nsproxy,struct ns_common *new) +static int timens_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct time_namespace *ns =to_time_ns (new);int err;@@ -289,7 +290,7 @@ static int timens_install(struct nsproxy *nsproxy, struct ns_common *new)return -EUSERS ;if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;timens_set_vvar_page (current, ns);kernel/user_namespace.c +4
-4@@ -1253,7 +1253,7 @@ static void userns_put(struct ns_common *ns)put_user_ns (to_user_ns (ns)); } -static int userns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int userns_install (struct nsset *nsset,struct ns_common *ns) {struct user_namespace *user_ns =to_user_ns (ns);struct cred *cred;@@ -1274,14 +1274,14 @@ static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)if (!ns_capable (user_ns,CAP_SYS_ADMIN ))return -EPERM ; - cred =prepare_creds (); + cred =nsset_cred (nsset);if (!cred) -return -ENOMEM ; +return -EINVAL ;put_user_ns (cred->user_ns);set_cred_user_ns (cred,get_user_ns (user_ns)); -return commit_creds (cred); +return 0 ; }struct ns_common *ns_get_owner (struct ns_common *ns)kernel/utsname.c +3
-2@@ -140,12 +140,13 @@ static void utsns_put(struct ns_common *ns)put_uts_ns (to_uts_ns (ns)); } -static int utsns_install (struct nsproxy *nsproxy,struct ns_common *new) +static int utsns_install (struct nsset *nsset,struct ns_common *new) { +struct nsproxy *nsproxy = nsset->nsproxy;struct uts_namespace *ns =to_uts_ns (new);if (!ns_capable (ns->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;get_uts_ns (ns);net/core/net_namespace.c +3
-2@@ -1353,12 +1353,13 @@ static void netns_put(struct ns_common *ns)put_net (to_net_ns (ns)); } -static int netns_install (struct nsproxy *nsproxy,struct ns_common *ns) +static int netns_install (struct nsset *nsset,struct ns_common *ns) { +struct nsproxy *nsproxy = nsset->nsproxy;struct net *net =to_net_ns (ns);if (!ns_capable (net->user_ns,CAP_SYS_ADMIN ) || - !ns_capable (current_user_ns (),CAP_SYS_ADMIN )) + !ns_capable (nsset->cred->user_ns,CAP_SYS_ADMIN ))return -EPERM ;put_net (nsproxy->net_ns);
Re: [PATCH v3 1/3] nsproxy: add struct nsset
Quoted 13 lines · Eric W. Biederman
Quoted 10 lines · Christian Brauner
Add a simple struct nsset. It holds all necessary pieces to switch to a new set of namespaces without leaving a task in a half-switched state which we will make use of in the next patch. This patch simply switches the existing setns logic over without causing a change in
setns()behavior. This bringssetns()closer to howunshare()works(). Theprepare_ns()function is responsible to prepare all necessary information. This has two reasons. First it minimizes dependencies between individual namespaces, i.e. all install handler can expect that all fields are properly initialized independent in what order they are called in. Second, this makes the code easier to maintain and easier to follow if it needs to be changed.
Quoted 3 lines
This is buggy.
Your code assume that nstype == 0 is invalid.
Yep, good catch! That's a bug from rearranging the patches.
Quoted 6 lines
Passing nstype == 0 means don't verify the kind of file descriptor passed.
Quite frankly doing nstype &
CLONE_XYZis wrong. It always needs to be nstype ==CLONE_XYZ.
I mean, I can do the nstype == CLONE_NEW* in the preparatory patch and then switch to flags & CLONE_NEW* later.
Let me fix this right now.
Christian
PATCH v3 2/3 nsproxy: attach to namespaces via pidfds
In reply to Christian BraunerFor quite a while we have been thinking about using pidfds to attach to namespaces. This patchset has existed for about a year already but we've wanted to wait to see how the general api would be received and adopted. Now that more and more programs in userspace have started using pidfds for process management it's time to send this one out.
This patch makes it possible to use pidfds to attach to the namespaces of another process, i.e. they can be passed as the first argument to the setns() syscall. When only a single namespace type is specified the semantics are equivalent to passing an nsfd. That means setns(nsfd, CLONE_NEWNET) equals setns(pidfd, CLONE_NEWNET). However, when a pidfd is passed, multiple namespace flags can be specified in the second setns() argument and setns() will attach the caller to all the specified namespaces all at once or to none of them. Specifying 0 is not valid together with a pidfd.
The obvious example where this is useful is a standard container manager interacting with a running container: pushing and pulling files or directories, injecting mounts, attaching/execing any kind of process, managing network devices all these operations require attaching to all or at least multiple namespaces at the same time. Given that nowadays most containers are spawned with all namespaces enabled we're currently looking at at least 14 syscalls, 7 to open the /proc/<pid>/ns/<ns> nsfds, another 7 to actually perform the namespace switch. With time namespaces we're looking at about 16 syscalls.
(We could amortize the first 7 or 8 syscalls for opening the nsfds by
stashing them in each container's monitor process but that would mean we need to send around those file descriptors through unix sockets everytime we want to interact with the container or keep on-disk state. Even in scenarios where a caller wants to join a particular namespace in a particular order callers still profit from batching other namespaces. That mostly applies to the user namespace but all container runtimes I found join the user namespace first no matter if it privileges or deprivileges the container similar to how unshare behaves.)
With pidfds this becomes a single syscall no matter how many namespaces are supposed to be attached to.
A decently designed, large-scale container manager usually isn't the parent of any of the containers it spawns so the containers don't die when it crashes or needs to update or reinitialize. This means that for the manager to interact with containers through pids is inherently racy especially on systems where the maximum pid number is not significicantly bumped. This is even more problematic since we often spawn and manage thousands or ten-thousands of containers. Interacting with a container through a pid thus can become risky quite quickly. Especially since we allow for an administrator to enable advanced features such as syscall interception where we're performing syscalls in lieu of the container. In all of those cases we use pidfds if they are available and we pass them around as stable references. Using them to setns() to the target process' namespaces is as reliable as using nsfds. Either the target process is already dead and we get ESRCH or we manage to attach to its namespaces but we can't accidently attach to another process' namespaces. So pidfds lend themselves to be used with this api. The other main advantage is that with this change the pidfd becomes the only relevant token for most container interactions and it's the only token we need to create and send around.
Apart from significiantly reducing the number of syscalls from double digit to single digit which is a decent reason post-spectre/meltdown this also allows to switch to a set of namespaces atomically, i.e. either attaching to all the specified namespaces succeeds or we fail. If we fail we haven't changed a single namespace. There are currently three namespaces that can fail (other than for ENOMEM which really is not very interesting since we then have other problems anyway) for non-trivial reasons, user, mount, and pid namespaces. We can fail to attach to a pid namespace if it is not our current active pid namespace or a descendant of it. We can fail to attach to a user namespace because we are multi-threaded or because our current mount namespace shares filesystem state with other tasks, or because we're trying to setns() to the same user namespace, i.e. the target task has the same user namespace as we do. We can fail to attach to a mount namespace because it shares filesystem state with other tasks or because we fail to lookup the new root for the new mount namespace. In most non-pathological scenarios these issues can be somewhat mitigated. But there are cases where we're half-attached to some namespace and failing to attach to another one. I've talked about some of these problem during the hallway track (something only the pre-COVID-19 generation will remember) of Plumbers in Los Angeles in 2018(?). Even if all these issues could be avoided with super careful userspace coding it would be nicer to have this done in-kernel. Pidfds seem to lend themselves nicely for this.
The other neat thing about this is that setns() becomes an actual counterpart to the namespace bits of unshare().
---
I'm currently still planning to send this series for v5.8 since it's still early enough so it can sit in linux-next for quite a while. /* v2 */
- Michael Kerrisk <[email protected]>:
- Michael pointed out that the semantics for setns(nsfd, 0) and setns(pidfd, 0) are not comparable. setns(pidfd, 0) is now disallowed completely. Users wanting to attach to all namespaces should simply specify them explicitly just as with unshare() and clone3().
- Jann Horn <[email protected]>:
- Jann pointed out that the setns() in its first form wasn't atomic in that userspace could end up in an intermediate state where e.g. the process had moved into the target user namespace but failed to move into the target mount namespace.
In this new version I've removed all intermediate states. There's an installation/preparation state and a commit state similar to prepare_creds() and commit_creds().
/* v3 */
- Christian Brauner <[email protected]>:
- The patchset is mostly unchanged. It was only fixed-up in response to changes in earlier patches.
--- fs/namespace.c | 5 + fs/nsfs.c | 7 +- include/linux/mnt_namespace.h | 1 + include/linux/proc_fs.h | 6 + kernel/nsproxy.c | 228 +++++++++++++++++++++++++++++++--- 5 files changed, 228 insertions(+), 19 deletions(-)
fs/namespace.c +5 -0
@@ -1733,6 +1733,11 @@ static struct mnt_namespace *to_mnt_ns(struct ns_common *ns) return container_of(ns, struct mnt_namespace, ns); } +struct ns_common *from_mnt_ns (struct mnt_namespace *mnt) +{ +return &mnt->ns; +} +static bool mnt_ns_loop (struct dentry *dentry) {/* Could bind mounting the mount namespace inode cause a
fs/nsfs.c +6 -1
@@ -229,6 +229,11 @@ int ns_get_name(char *buf, size_t size, struct task_struct *task,return res; } +bool proc_ns_file (const struct file *file) +{ +return file->f_op == &ns_file_operations; +} +struct file *proc_ns_fget (int fd) {struct file *file;
@@ -237,7 +242,7 @@ struct file *proc_ns_fget(int fd)if (!file)return ERR_PTR (-EBADF ); -if (file->f_op != &ns_file_operations) +if (!proc_ns_file (file)) goto out_invalid;return file;
include/linux/mnt_namespace.h +1 -0
@@ -11,6 +11,7 @@ struct ns_common;extern struct mnt_namespace *copy_mnt_ns (unsigned long ,struct mnt_namespace *,struct user_namespace *,struct fs_struct *);extern void put_mnt_ns (struct mnt_namespace *ns); +extern struct ns_common *from_mnt_ns (struct mnt_namespace *);extern const struct file_operations proc_mounts_operations;extern const struct file_operations proc_mountinfo_operations;
include/linux/proc_fs.h +6 -0
@@ -104,6 +104,7 @@ struct proc_dir_entry *proc_create_net_single_write(const char *name, umode_t moproc_write_t write, void *data);extern struct pid *tgid_pidfd_to_pid (const struct file *file); +extern bool proc_ns_file (const struct file *file);#ifdef CONFIG_PROC_PID_ARCH_STATUS /*
@@ -159,6 +160,11 @@ static inline struct pid *tgid_pidfd_to_pid(const struct file *file) return ERR_PTR(-EBADF); } +static inline bool proc_ns_file (const struct file *file) +{ +return false; +} +#endif /* CONFIG_PROC_FS */ struct net ;
kernel/nsproxy.c +210 -18
281 lines not on this page · raw message
Signature · 1 line
2.26.2
Re: [PATCH v3 2/3] nsproxy: attach to namespaces via pidfds
I am not thrilled about treating nstype as a flags fields when it is not currently. It was my hope when I designed the interface that not treating nstype as a flags field would save us from the problem of bits running out.
That aside. It would be very good if the default version of setting everything from a pidfd would set the root directory from the process it is copying everything else from.
Eric
Re: [PATCH v3 2/3] nsproxy: attach to namespaces via pidfds
Quoted 5 lines · Eric W. Biederman
I am not thrilled about treating nstype as a flags fields when it is not currently. It was my hope when I designed the interface that not treating nstype as a flags field would save us from the problem of bits running out.
Hm, I researched the setns() syscall history before that and I didn't see that reasoning anywhere. The "nstype" arg was originally advertised on the list as "having a flags field is useful in general".
Quoted 4 lines
That aside. It would be very good if the default version of setting everything from a pidfd would set the root directory from the process it is copying everything else from.
I'm not sure I follow completely. If you specify CLONE_NEWNS then we do set the root directory with set_fs_root() in commit_nsset(). Or are you saying we should always do that independent of whether or not CLONE_NEWNS is specified? And if so could you explain why we'd want that? I'm sure I'm missing something!
Christian
Re: [PATCH v3 2/3] nsproxy: attach to namespaces via pidfds
Quoted 10 lines · Christian Brauner
Quoted 5 lines · Eric W. Biederman
I am not thrilled about treating nstype as a flags fields when it is not currently. It was my hope when I designed the interface that not treating nstype as a flags field would save us from the problem of bits running out.
Hm, I researched the
setns()syscall history before that and I didn't see that reasoning anywhere. The "nstype" arg was originally advertised on the list as "having a flags field is useful in general".
Take a look at the code. At the end of the day nstype is not treated at all like a flags field.
It isn't a very important point. And it was certainly easier to use the existing bits for essentially their existing meanings. But it was certainly something I was thinking at the time.
I think I left it as we can see either way, depending on how things evolve.
I can imagine a use for a nstype being a single namespace from a pidfd. Do you have any actual usecases for setting some but not all of the namespaces from a pidfd? If we don't have a compelling reason I would like to kick that can down the road a ways farther.
I am also remembering that that setns freed the low 8 bits. Which gave some freedom beyond clone.
Quoted 9 lines
Quoted 3 lines
That aside. It would be very good if the default version of setting everything from a pidfd would set the root directory from the process it is copying everything else from.
I'm not sure I follow completely. If you specify
CLONE_NEWNSthen we do set the root directory withset_fs_root()incommit_nsset(). Or are you saying we should always do that independent of whether or notCLONE_NEWNSis specified? And if so could you explain why we'd want that? I'm sure I'm missing something!
I am suggesting that when we do:
"setns(pidfd, 0)" or "setns(pidfd, SETNS_PIDFD)"
That the result is not just the namespaces changing but also the root directory changing to the pids root directory. Something where the whole is greater than the parts.
Eric
Re: [PATCH v3 2/3] nsproxy: attach to namespaces via pidfds
Quoted 9 lines · Eric W. Biederman
Quoted 7 lines · Christian Brauner
Quoted 5 lines · Eric W. Biederman
I am not thrilled about treating nstype as a flags fields when it is not currently. It was my hope when I designed the interface that not treating nstype as a flags field would save us from the problem of bits running out.
Quoted 6 lines
Quoted 3 lines
Hm, I researched the
setns()syscall history before that and I didn't see that reasoning anywhere. The "nstype" arg was originally advertised on the list as "having a flags field is useful in general".Take a look at the code. At the end of the day nstype is not treated at all like a flags field.
Oh, I wasn't trying to dispute that. I was just pointing at the history where using it as a flags field in the future wasn't in principle out of the question.
Quoted 5 lines
It isn't a very important point. And it was certainly easier to use the existing bits for essentially their existing meanings. But it was certainly something I was thinking at the time.
Quoted 7 lines
I think I left it as we can see either way, depending on how things evolve.
I can imagine a use for a nstype being a single namespace from a pidfd. Do you have any actual usecases for setting some but not all of the namespaces from a pidfd? If we don't have a compelling reason I would like to kick that can down the road a ways farther.
Yeah, I think so. We already have a few use-cases. The syscall interception stuff selectively attaches to subsets of namespaces depending on what namespaces are needed to emulate a given syscall. And the exec logic let's users select what namespaces to attach to. It's common to setns to a subset of namespaces to perform operations with privilege and then later attach others (often the userns).
Quoted 14 lines
I am also remembering that that setns freed the low 8 bits. Which gave some freedom beyond clone.
Quoted 9 lines
Quoted 3 lines
That aside. It would be very good if the default version of setting everything from a pidfd would set the root directory from the process it is copying everything else from.
I'm not sure I follow completely. If you specify
CLONE_NEWNSthen we do set the root directory withset_fs_root()incommit_nsset(). Or are you saying we should always do that independent of whether or notCLONE_NEWNSis specified? And if so could you explain why we'd want that? I'm sure I'm missing something!
Quoted 7 lines
I am suggesting that when we do:
"
setns(pidfd, 0)" or "setns(pidfd, SETNS_PIDFD)"That the result is not just the namespaces changing but also the root directory changing to the pids root directory. Something where the whole is greater than the parts.
Ok, I can see that being useful. But If we do this, then a new flag would be quite helpful. (I also think Michael had some reservations against re-using 0 for something like this.)
But let me suggest moving your phrase from above down to here and say that we could kick that can down the road for a follow-up extension?
Christian
PATCH v3 3/3 selftests/pidfd: add pidfd setns tests
In reply to Christian BraunerThis is basically a test-suite for setns() and as of now contains:
- test that we can't pass garbage flags
- test that we can't attach to the namespaces of task that has already exited
- test that we can incrementally setns into all namespaces of a target task using a pidfd
- test that we can setns atomically into all namespaces of a target task
- test that we can't cross setns into a user namespace outside of our user namespace hierarchy
- test that we can't setns into namespaces owned by user namespaces over which we are not privileged
---
/* v2 */
patch introduced
/* v3 */
- Christian Brauner <[email protected]>:
- Minor tweaks in variable naming and a few tiny fixes.
--- tools/testing/selftests/pidfd/.gitignore | 1 + tools/testing/selftests/pidfd/Makefile | 3 +- tools/testing/selftests/pidfd/config | 6 + .../selftests/pidfd/pidfd_setns_test.c | 473 ++++++++++++++++++ 4 files changed, 482 insertions(+), 1 deletion(-) create mode 100644 tools/testing/selftests/pidfd/config create mode 100644 tools/testing/selftests/pidfd/pidfd_setns_test.c
tools/testing/selftests/pidfd/.gitignore +1 -0
@@ -5,3 +5,4 @@ pidfd_test pidfd_wait pidfd_fdinfo_test pidfd_getfd_test +pidfd_setns_test
tools/testing/selftests/pidfd/Makefile +2 -1
@@ -1,7 +1,8 @@ # SPDX-License-Identifier: GPL-2.0-only CFLAGS += -g -I../../../../usr/include/ -pthread -TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test pidfd_poll_test pidfd_wait pidfd_getfd_test +TEST_GEN_PROGS := pidfd_test pidfd_fdinfo_test pidfd_open_test \ + pidfd_poll_test pidfd_wait pidfd_getfd_test pidfd_setns_test include ../lib.mk
tools/testing/selftests/pidfd/config new file +6 -0
@@ -0,0 +1,6 @@ +CONFIG_UTS_NS=y +CONFIG_IPC_NS=y +CONFIG_USER_NS=y +CONFIG_PID_NS=y +CONFIG_NET_NS=y +CONFIG_CGROUPS=y
tools/testing/selftests/pidfd/pidfd_setns_test.c new file +473 -0
473 lines not on this page · raw message
Signature · 1 line
2.26.2