Skip to contentSkip to conversation

user namespace : add unshare

PATCH In review Cedric Le Goater 2 participants 4 messages Jun 8, 2007 – Jun 11, 2007

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 user namespace : add unshare

From: Serge E. Hallyn <[email protected]>

Changelog: Fix !CONFIG_USER_NS clone with CLONE_NEWUSER so it returns -EINVAL
rather than 0, so that userspace knows they didn't get a new user namespace.

---

 include/linux/sched.h          |    1 
 include/linux/user_namespace.h |    4 +++
 kernel/fork.c                  |    2 -
 kernel/nsproxy.c               |    5 ++--
 kernel/user_namespace.c        |   46 ++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 54 insertions(+), 4 deletions(-)
include/linux/sched.h +1 -0
Index: 2.6.22-rc4-mm2/include/linux/sched.h
===================================================================
@@ -25,6 +25,7 @@
 #define CLONE_STOPPED		0x02000000	/* Start in stopped state */
 #define CLONE_NEWUTS		0x04000000	/* New utsname group? */
 #define CLONE_NEWIPC		0x08000000	/* New ipcs */
+#define CLONE_NEWUSER		0x10000000	/* New user namespace */
 
 /*
  * Scheduling policies
include/linux/user_namespace.h +4 -0
Index: 2.6.22-rc4-mm2/include/linux/user_namespace.h
===================================================================
@@ -4,6 +4,7 @@
 #include <linux/kref.h>
 #include <linux/nsproxy.h>
 #include <linux/sched.h>
+#include <linux/err.h>
 
 #define UIDHASH_BITS	(CONFIG_BASE_SMALL ? 3 : 8)
 #define UIDHASH_SZ	(1 << UIDHASH_BITS)
@@ -45,6 +46,9 @@ static inline struct user_namespace *get
 static inline struct user_namespace *copy_user_ns(int flags,
 						  struct user_namespace *old_ns)
 {
+	if (flags & CLONE_NEWUSER)
+		return ERR_PTR(-EINVAL);
+
 	return NULL;
 }
 
kernel/fork.c +1 -1
Index: 2.6.22-rc4-mm2/kernel/fork.c
===================================================================
@@ -1616,7 +1616,7 @@ asmlinkage long sys_unshare(unsigned lon
 	err = -EINVAL;
 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
-				CLONE_NEWUTS|CLONE_NEWIPC))
+				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER))
 		goto bad_unshare_out;
 
 	if ((err = unshare_thread(unshare_flags)))
kernel/nsproxy.c +3 -2
Index: 2.6.22-rc4-mm2/kernel/nsproxy.c
===================================================================
@@ -117,7 +117,7 @@ int copy_namespaces(int flags, struct ta
 
 	get_nsproxy(old_ns);
 
-	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
+	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER)))
 		return 0;
 
 	if (!capable(CAP_SYS_ADMIN)) {
@@ -163,7 +163,8 @@ int unshare_nsproxy_namespaces(unsigned 
 	struct nsproxy *old_ns = current->nsproxy;
 	int err = 0;
 
-	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
+	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
+			       CLONE_NEWUSER)))
 		return 0;
 
 	if (!capable(CAP_SYS_ADMIN))
kernel/user_namespace.c +45 -1

58 lines not on this page · raw message

maintainer
#1 lore raw
Quoted 3 lines · On Fri, 08 Jun 2007 17:14:12 +0200 Cedric Le Goater <[email protected]> wrote:

Changelog: Fix !CONFIG_USER_NS clone with CLONE_NEWUSER so it returns -EINVAL
rather than 0, so that userspace knows they didn't get a new user namespace.

This changelog doesn't seem to match this patch.

#2 lore raw
Quoted 8 lines · Andrew Morton
Quoted 3 lines · On Fri, 08 Jun 2007 17:14:12 +0200 Cedric Le Goater <[email protected]> wrote:

Changelog: Fix !CONFIG_USER_NS clone with CLONE_NEWUSER so it returns -EINVAL
rather than 0, so that userspace knows they didn't get a new user namespace.

This changelog doesn't seem to match this patch.

Indeed. the changelog entry is now deprecated since I removed CONFIG_USER_NS.

that was a fix for the issue you reported about that patch breaking the mouse under FC5 when the kernel was compiled without CONFIG_USER_NS.

C.

author Acked-by Pavel Emelianov
#3 lore raw
Quoted 7 lines · Cedric Le Goater
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Fri, 08 Jun 2007 17:14:12 +0200 Cedric Le Goater <[email protected]> wrote:

Changelog: Fix !CONFIG_USER_NS clone with CLONE_NEWUSER so it returns -EINVAL
rather than 0, so that userspace knows they didn't get a new user namespace.

Quoted 7 lines
Quoted 1 line

This changelog doesn't seem to match this patch.

Indeed. the changelog entry is now deprecated since I removed CONFIG_USER_NS.

that was a fix for the issue you reported about that patch breaking the mouse under FC5 when the kernel was compiled without CONFIG_USER_NS.

So here's a more appropriate changelog for the patch.

C.

This patch enables the unshare of user namespaces.

It adds a new clone flag CLONE_NEWUSER and implements copy_user_ns() which resets the current user_struct and adds a new root user (uid ==
0)

For now, unsharing the user namespace allows a process to reset its user_struct accounting and uid 0 in the new user namespace should be contained using appropriate means, for instance selinux

The plan, when the full support is complete (all uid checks covered), is to keep the original user's rights in the original namespace, and let a process become uid 0 in the new namespace, with full capabilities to the new namespace.

---

 include/linux/sched.h          |    1 
 include/linux/user_namespace.h |    4 +++
 kernel/fork.c                  |    2 -
 kernel/nsproxy.c               |    5 ++--
 kernel/user_namespace.c        |   46 ++++++++++++++++++++++++++++++++++++++++-
 5 files changed, 54 insertions(+), 4 deletions(-)
include/linux/sched.h +1 -0
Index: 2.6.22-rc4-mm2/include/linux/sched.h
===================================================================
@@ -25,6 +25,7 @@
 #define CLONE_STOPPED		0x02000000	/* Start in stopped state */
 #define CLONE_NEWUTS		0x04000000	/* New utsname group? */
 #define CLONE_NEWIPC		0x08000000	/* New ipcs */
+#define CLONE_NEWUSER		0x10000000	/* New user namespace */
 
 /*
  * Scheduling policies
include/linux/user_namespace.h +4 -0
Index: 2.6.22-rc4-mm2/include/linux/user_namespace.h
===================================================================
@@ -4,6 +4,7 @@
 #include <linux/kref.h>
 #include <linux/nsproxy.h>
 #include <linux/sched.h>
+#include <linux/err.h>
 
 #define UIDHASH_BITS	(CONFIG_BASE_SMALL ? 3 : 8)
 #define UIDHASH_SZ	(1 << UIDHASH_BITS)
@@ -45,6 +46,9 @@ static inline struct user_namespace *get
 static inline struct user_namespace *copy_user_ns(int flags,
 						  struct user_namespace *old_ns)
 {
+	if (flags & CLONE_NEWUSER)
+		return ERR_PTR(-EINVAL);
+
 	return NULL;
 }
 
kernel/fork.c +1 -1
Index: 2.6.22-rc4-mm2/kernel/fork.c
===================================================================
@@ -1616,7 +1616,7 @@ asmlinkage long sys_unshare(unsigned lon
 	err = -EINVAL;
 	if (unshare_flags & ~(CLONE_THREAD|CLONE_FS|CLONE_NEWNS|CLONE_SIGHAND|
 				CLONE_VM|CLONE_FILES|CLONE_SYSVSEM|
-				CLONE_NEWUTS|CLONE_NEWIPC))
+				CLONE_NEWUTS|CLONE_NEWIPC|CLONE_NEWUSER))
 		goto bad_unshare_out;
 
 	if ((err = unshare_thread(unshare_flags)))
kernel/nsproxy.c +3 -2
Index: 2.6.22-rc4-mm2/kernel/nsproxy.c
===================================================================
@@ -117,7 +117,7 @@ int copy_namespaces(int flags, struct ta
 
 	get_nsproxy(old_ns);
 
-	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
+	if (!(flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC | CLONE_NEWUSER)))
 		return 0;
 
 	if (!capable(CAP_SYS_ADMIN)) {
@@ -163,7 +163,8 @@ int unshare_nsproxy_namespaces(unsigned 
 	struct nsproxy *old_ns = current->nsproxy;
 	int err = 0;
 
-	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC)))
+	if (!(unshare_flags & (CLONE_NEWNS | CLONE_NEWUTS | CLONE_NEWIPC |
+			       CLONE_NEWUSER)))
 		return 0;
 
 	if (!capable(CAP_SYS_ADMIN))
kernel/user_namespace.c +45 -1

58 lines not on this page · raw message