From 9dca3b0c62a1038d9184f9639504f5ab3741462d Mon Sep 17 00:00:00 2001 From: "Eric W. Biederman" Date: Thu, 26 Jul 2012 01:13:20 -0700 Subject: [PATCH] userns: Allow unprivileged use of setns. - Push the permission check from the core setns syscall into the setns install methods where the user namespace of the target namespace can be determined, and used in a ns_capable call. Acked-by: Serge Hallyn Signed-off-by: "Eric W. Biederman" Change-Id: I655fbd54531112ae7fe425a4055c2583a41deb78 --- ipc/namespace.c | 6 +++++- kernel/nsproxy.c | 3 --- kernel/utsname.c | 7 ++++++- net/core/net_namespace.c | 7 ++++++- 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/ipc/namespace.c b/ipc/namespace.c index cd7f7330d68..92ce6227196 100644 --- a/ipc/namespace.c +++ b/ipc/namespace.c @@ -169,8 +169,12 @@ static void ipcns_put(void *ns) return put_ipc_ns(ns); } -static int ipcns_install(struct nsproxy *nsproxy, void *ns) +static int ipcns_install(struct nsproxy *nsproxy, void *new) { + struct ipc_namespace *ns = new; + if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + /* Ditch state from the old ipc namespace */ exit_sem(current); put_ipc_ns(nsproxy->ipc_ns); diff --git a/kernel/nsproxy.c b/kernel/nsproxy.c index b08eaeb8dab..c94f60fceed 100644 --- a/kernel/nsproxy.c +++ b/kernel/nsproxy.c @@ -241,9 +241,6 @@ SYSCALL_DEFINE2(setns, int, fd, int, nstype) struct file *file; int err; - if (!capable(CAP_SYS_ADMIN)) - return -EPERM; - file = proc_ns_fget(fd); if (IS_ERR(file)) return PTR_ERR(file); diff --git a/kernel/utsname.c b/kernel/utsname.c index ce3d44b4187..74926463b87 100644 --- a/kernel/utsname.c +++ b/kernel/utsname.c @@ -110,8 +110,13 @@ static void utsns_put(void *ns) put_uts_ns(ns); } -static int utsns_install(struct nsproxy *nsproxy, void *ns) +static int utsns_install(struct nsproxy *nsproxy, void *new) { + struct uts_namespace *ns = new; + + if (!ns_capable(ns->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + get_uts_ns(ns); put_uts_ns(nsproxy->uts_ns); nsproxy->uts_ns = ns; diff --git a/net/core/net_namespace.c b/net/core/net_namespace.c index a397e3173ce..93b5c8996ac 100644 --- a/net/core/net_namespace.c +++ b/net/core/net_namespace.c @@ -653,8 +653,13 @@ static void netns_put(void *ns) static int netns_install(struct nsproxy *nsproxy, void *ns) { + struct net *net = ns; + + if (!ns_capable(net->user_ns, CAP_SYS_ADMIN)) + return -EPERM; + put_net(nsproxy->net_ns); - nsproxy->net_ns = get_net(ns); + nsproxy->net_ns = get_net(net); return 0; }