linux-api.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] userns/capability: Add user namespace capability
@ 2015-10-17 15:58 Tobias Markus
       [not found] ` <5622700C.9090107-gyUQdkDHmHmHXe+LvDLADg@public.gmane.org>
  0 siblings, 1 reply; 21+ messages in thread
From: Tobias Markus @ 2015-10-17 15:58 UTC (permalink / raw)
  To: linux-kernel-u79uwXL29TY76Z2rM5mHXA
  Cc: Eric W. Biederman, Al Viro, Serge Hallyn, Andrew Morton,
	Andy Lutomirski, Christoph Lameter, Michael Kerrisk (man-pages),
	linux-security-module-u79uwXL29TY76Z2rM5mHXA,
	linux-api-u79uwXL29TY76Z2rM5mHXA,
	linux-man-u79uwXL29TY76Z2rM5mHXA

Add capability CAP_SYS_USER_NS.
Tasks having CAP_SYS_USER_NS are allowed to create a new user namespace
when calling clone or unshare with CLONE_NEWUSER.

Rationale:

Linux 3.8 saw the introduction of unpriviledged user namespaces,
allowing unpriviledged users (without CAP_SYS_ADMIN) to be a "fake" root
inside a separate user namespace. Before that, any namespace creation
required CAP_SYS_ADMIN (or, in practice, the user had to be root).
Unfortunately, there have been some security-relevant bugs in the
meantime. Because of the fairly complex nature of user namespaces, it is
reasonable to say that future vulnerabilties can not be excluded. Some
distributions even wholly disable user namespaces because of this.

Both options, user namespaces with and without CAP_SYS_ADMIN, can be
said to represent the extreme end of the spectrum. In practice, there is
no reason for every process to have the abilitiy to create user
namespaces. Indeed, only very few and specialized programs require user
namespaces. This seems to be a perfect fit for the (file) capability
system: Priviledged users could manually allow only a certain executable
to be able to create user namespaces by setting a certain capability,
I'd suggest the name CAP_SYS_USER_NS. Executables completely unrelated
to user namespaces should and can not create them.

The capability should only be required in the "root" user namespace (the
user namespace with level 0) though, to allow nested user namespaces to
work as intended. If a user namespace has a level greater than 0, the
original process must have had CAP_SYS_USER_NS, so it is "trusted" anyway.

One question remains though: Does this break userspace executables that
expect being able to create user namespaces without priviledge? Since
creating user namespaces without CAP_SYS_ADMIN was not possible before
Linux 3.8, programs should already expect a potential EPERM upon calling
clone. Since creating a user namespace without CAP_SYS_USER_NS would
also cause EPERM, we should be on the safe side.

Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: linux-api-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: linux-man-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
Cc: Eric W. Biederman <ebiederm-aS9lmoZGLiVWk0Htik3J/w@public.gmane.org>
Cc: Al Viro <viro-RmSDqhL/yNMiFSDQTTA3OLVCufUGDwFn@public.gmane.org>
Cc: Serge Hallyn <serge.hallyn-Z7WLFzj8eWMS+FvcfC7Uqw@public.gmane.org>
Cc: Andy Lutomirski <luto-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org>
Cc: Andrew Morton <akpm-de/tnXTf+JLsfHDXvbKv3WD2FQJk+8+b@public.gmane.org>
Cc: Christoph Lameter <cl-vYTEC60ixJUAvxtiuMwx3w@public.gmane.org>
Cc: Michael Kerrisk <mtk.manpages-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
Signed-off-by: Tobias Markus <tobias-gyUQdkDHmHmHXe+LvDLADg@public.gmane.org>
---
 include/uapi/linux/capability.h     | 5 ++++-
 kernel/cred.c                       | 7 ++++++-
 security/selinux/include/classmap.h | 2 +-
 3 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/include/uapi/linux/capability.h b/include/uapi/linux/capability.h
index 12c37a1..d83540f 100644
--- a/include/uapi/linux/capability.h
+++ b/include/uapi/linux/capability.h
@@ -351,8 +351,11 @@ struct vfs_cap_data {
  #define CAP_AUDIT_READ		37
 +/* Allow creating user namespaces (CLONE_NEWUSER) using clone() and unshare() */
 -#define CAP_LAST_CAP         CAP_AUDIT_READ
+#define CAP_SYS_USER_NS      38
+
+#define CAP_LAST_CAP         CAP_SYS_USER_NS
  #define cap_valid(x) ((x) >= 0 && (x) <= CAP_LAST_CAP)
 diff --git a/kernel/cred.c b/kernel/cred.c
index 71179a0..847d499 100644
--- a/kernel/cred.c
+++ b/kernel/cred.c
@@ -345,7 +345,12 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
 		return -ENOMEM;
  	if (clone_flags & CLONE_NEWUSER) {
-		ret = create_user_ns(new);
+		if (new->user_ns->level == 0 &&
+		    !has_capability(p, CAP_SYS_USER_NS)) {
+			ret = -EPERM;
+		} else {
+			ret = create_user_ns(new);
+		}
 		if (ret < 0)
 			goto error_put;
 	}
diff --git a/security/selinux/include/classmap.h b/security/selinux/include/classmap.h
index 5a4eef5..07cec76 100644
--- a/security/selinux/include/classmap.h
+++ b/security/selinux/include/classmap.h
@@ -39,7 +39,7 @@ struct security_class_mapping secclass_map[] = {
 	    "linux_immutable", "net_bind_service", "net_broadcast",
 	    "net_admin", "net_raw", "ipc_lock", "ipc_owner", "sys_module",
 	    "sys_rawio", "sys_chroot", "sys_ptrace", "sys_pacct", "sys_admin",
-	    "sys_boot", "sys_nice", "sys_resource", "sys_time",
+	    "sys_boot", "sys_nice", "sys_resource", "sys_time", "sys_user_ns",
 	    "sys_tty_config", "mknod", "lease", "audit_write",
 	    "audit_control", "setfcap", NULL } },
 	{ "filesystem",
-- 
2.6.1

^ permalink raw reply related	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2015-10-22 21:44 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-10-17 15:58 [PATCH] userns/capability: Add user namespace capability Tobias Markus
     [not found] ` <5622700C.9090107-gyUQdkDHmHmHXe+LvDLADg@public.gmane.org>
2015-10-17 20:17   ` Richard Weinberger
2015-10-18 20:13     ` Tobias Markus
     [not found]       ` <5623FD86.2030609-gyUQdkDHmHmHXe+LvDLADg@public.gmane.org>
2015-10-18 20:21         ` Richard Weinberger
     [not found]           ` <5623FF36.8080800-/L3Ra7n9ekc@public.gmane.org>
2015-10-18 20:41             ` Tobias Markus
2015-10-18 20:48               ` Richard Weinberger
     [not found]                 ` <56240599.3050903-/L3Ra7n9ekc@public.gmane.org>
2015-10-18 21:49                   ` Tobias Markus
2015-10-18 22:06                     ` Richard Weinberger
2015-10-19  0:28         ` Mike Frysinger
2015-10-17 21:55   ` Serge E. Hallyn
     [not found]     ` <20151017215501.GA22900-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2015-10-18 20:13       ` Tobias Markus
     [not found]         ` <5623FD82.4030902-gyUQdkDHmHmHXe+LvDLADg@public.gmane.org>
2015-10-19  1:41           ` Serge E. Hallyn
     [not found]             ` <20151019014112.GA1683-7LNsyQBKDXoIagZqoN9o3w@public.gmane.org>
2015-10-19 12:36               ` Yves-Alexis Perez
     [not found]                 ` <1445258180.4099.18.camel-8fiUuRrzOP0dnm+yROfE0A@public.gmane.org>
2015-10-19 12:48                   ` Richard Weinberger
2015-10-22 20:45           ` Eric W. Biederman
     [not found]             ` <87twpi63ai.fsf-JOvCrm2gF+uungPnsOpG7nhyD016LWXt@public.gmane.org>
2015-10-22 21:02               ` Andy Lutomirski
     [not found]                 ` <CALCETrWKN+Uzw_TYqVTGatNZ3LT5RbSM1WuYPoXeKQs9Yw_qjg-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-10-22 21:44                   ` Eric W. Biederman
2015-10-19 14:24   ` Austin S Hemmelgarn
     [not found]     ` <5624FD3B.2050401-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2015-10-21 18:53       ` Andy Lutomirski
     [not found]         ` <CALCETrWfZ9hXvLPtJnZhU-ZdoUbYNo-QSydMPvP6Q7Rp0oCQaw-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
2015-10-21 19:13           ` Austin S Hemmelgarn
2015-10-22 17:10             ` Andy Lutomirski

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).