aboutsummaryrefslogtreecommitdiffstats
path: root/kernel/user_namespace.c
diff options
context:
space:
mode:
authorSerge Hallyn <serue@us.ibm.com>2008-10-15 17:38:45 -0400
committerSerge E. Hallyn <serue@us.ibm.com>2008-11-24 18:57:41 -0500
commit18b6e0414e42d95183f07d8177e3ff0241abd825 (patch)
tree91ca2f2d442055e31eb7bb551bf7060f3f4c4cc7 /kernel/user_namespace.c
parent9789cfe22e5d7bc10cad841a4ea96ecedb34b267 (diff)
User namespaces: set of cleanups (v2)
The user_ns is moved from nsproxy to user_struct, so that a struct cred by itself is sufficient to determine access (which it otherwise would not be). Corresponding ecryptfs fixes (by David Howells) are here as well. Fix refcounting. The following rules now apply: 1. The task pins the user struct. 2. The user struct pins its user namespace. 3. The user namespace pins the struct user which created it. User namespaces are cloned during copy_creds(). Unsharing a new user_ns is no longer possible. (We could re-add that, but it'll cause code duplication and doesn't seem useful if PAM doesn't need to clone user namespaces). When a user namespace is created, its first user (uid 0) gets empty keyrings and a clean group_info. This incorporates a previous patch by David Howells. Here is his original patch description: >I suggest adding the attached incremental patch. It makes the following >changes: > > (1) Provides a current_user_ns() macro to wrap accesses to current's user > namespace. > > (2) Fixes eCryptFS. > > (3) Renames create_new_userns() to create_user_ns() to be more consistent > with the other associated functions and because the 'new' in the name is > superfluous. > > (4) Moves the argument and permission checks made for CLONE_NEWUSER to the > beginning of do_fork() so that they're done prior to making any attempts > at allocation. > > (5) Calls create_user_ns() after prepare_creds(), and gives it the new creds > to fill in rather than have it return the new root user. I don't imagine > the new root user being used for anything other than filling in a cred > struct. > > This also permits me to get rid of a get_uid() and a free_uid(), as the > reference the creds were holding on the old user_struct can just be > transferred to the new namespace's creator pointer. > > (6) Makes create_user_ns() reset the UIDs and GIDs of the creds under > preparation rather than doing it in copy_creds(). > >David >Signed-off-by: David Howells <dhowells@redhat.com> Changelog: Oct 20: integrate dhowells comments 1. leave thread_keyring alone 2. use current_user_ns() in set_user() Signed-off-by: Serge Hallyn <serue@us.ibm.com>
Diffstat (limited to 'kernel/user_namespace.c')
-rw-r--r--kernel/user_namespace.c75
1 files changed, 30 insertions, 45 deletions
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c
index 0d9c51d67333..79084311ee57 100644
--- a/kernel/user_namespace.c
+++ b/kernel/user_namespace.c
@@ -9,70 +9,55 @@
9#include <linux/nsproxy.h> 9#include <linux/nsproxy.h>
10#include <linux/slab.h> 10#include <linux/slab.h>
11#include <linux/user_namespace.h> 11#include <linux/user_namespace.h>
12#include <linux/cred.h>
12 13
13/* 14/*
14 * Clone a new ns copying an original user ns, setting refcount to 1 15 * Create a new user namespace, deriving the creator from the user in the
15 * @old_ns: namespace to clone 16 * passed credentials, and replacing that user with the new root user for the
16 * Return NULL on error (failure to kmalloc), new ns otherwise 17 * new namespace.
18 *
19 * This is called by copy_creds(), which will finish setting the target task's
20 * credentials.
17 */ 21 */
18static struct user_namespace *clone_user_ns(struct user_namespace *old_ns) 22int create_user_ns(struct cred *new)
19{ 23{
20 struct user_namespace *ns; 24 struct user_namespace *ns;
21 struct user_struct *new_user; 25 struct user_struct *root_user;
22 struct cred *new;
23 int n; 26 int n;
24 27
25 ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL); 28 ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL);
26 if (!ns) 29 if (!ns)
27 return ERR_PTR(-ENOMEM); 30 return -ENOMEM;
28 31
29 kref_init(&ns->kref); 32 kref_init(&ns->kref);
30 33
31 for (n = 0; n < UIDHASH_SZ; ++n) 34 for (n = 0; n < UIDHASH_SZ; ++n)
32 INIT_HLIST_HEAD(ns->uidhash_table + n); 35 INIT_HLIST_HEAD(ns->uidhash_table + n);
33 36
34 /* Insert new root user. */ 37 /* Alloc new root user. */
35 ns->root_user = alloc_uid(ns, 0); 38 root_user = alloc_uid(ns, 0);
36 if (!ns->root_user) { 39 if (!root_user) {
37 kfree(ns); 40 kfree(ns);
38 return ERR_PTR(-ENOMEM); 41 return -ENOMEM;
39 } 42 }
40 43
41 /* Reset current->user with a new one */ 44 /* set the new root user in the credentials under preparation */
42 new_user = alloc_uid(ns, current_uid()); 45 ns->creator = new->user;
43 if (!new_user) { 46 new->user = root_user;
44 free_uid(ns->root_user); 47 new->uid = new->euid = new->suid = new->fsuid = 0;
45 kfree(ns); 48 new->gid = new->egid = new->sgid = new->fsgid = 0;
46 return ERR_PTR(-ENOMEM); 49 put_group_info(new->group_info);
47 } 50 new->group_info = get_group_info(&init_groups);
48 51#ifdef CONFIG_KEYS
49 /* Install the new user */ 52 key_put(new->request_key_auth);
50 new = prepare_creds(); 53 new->request_key_auth = NULL;
51 if (!new) { 54#endif
52 free_uid(new_user); 55 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
53 free_uid(ns->root_user);
54 kfree(ns);
55 }
56 free_uid(new->user);
57 new->user = new_user;
58 commit_creds(new);
59 return ns;
60}
61
62struct user_namespace * copy_user_ns(int flags, struct user_namespace *old_ns)
63{
64 struct user_namespace *new_ns;
65
66 BUG_ON(!old_ns);
67 get_user_ns(old_ns);
68
69 if (!(flags & CLONE_NEWUSER))
70 return old_ns;
71 56
72 new_ns = clone_user_ns(old_ns); 57 /* alloc_uid() incremented the userns refcount. Just set it to 1 */
58 kref_set(&ns->kref, 1);
73 59
74 put_user_ns(old_ns); 60 return 0;
75 return new_ns;
76} 61}
77 62
78void free_user_ns(struct kref *kref) 63void free_user_ns(struct kref *kref)
@@ -80,7 +65,7 @@ void free_user_ns(struct kref *kref)
80 struct user_namespace *ns; 65 struct user_namespace *ns;
81 66
82 ns = container_of(kref, struct user_namespace, kref); 67 ns = container_of(kref, struct user_namespace, kref);
83 release_uids(ns); 68 free_uid(ns->creator);
84 kfree(ns); 69 kfree(ns);
85} 70}
86EXPORT_SYMBOL(free_user_ns); 71EXPORT_SYMBOL(free_user_ns);