diff options
author | Eric W. Biederman <ebiederm@xmission.com> | 2011-11-17 04:32:59 -0500 |
---|---|---|
committer | Eric W. Biederman <ebiederm@xmission.com> | 2012-04-26 05:00:59 -0400 |
commit | 783291e6900292521a3895583785e0c04a56c5b3 (patch) | |
tree | 9dd368a25ea61b5913646b1d93ec99e865c058ba /kernel/user_namespace.c | |
parent | 7b44ab978b77a91b327058a0f4db7e6fcdb90b92 (diff) |
userns: Simplify the user_namespace by making userns->creator a kuid.
- Transform userns->creator from a user_struct reference to a simple
kuid_t, kgid_t pair.
In cap_capable this allows the check to see if we are the creator of
a namespace to become the classic suser style euid permission check.
This allows us to remove the need for a struct cred in the mapping
functions and still be able to dispaly the user namespace creators
uid and gid as 0.
- Remove the now unnecessary delayed_work in free_user_ns.
All that is left for free_user_ns to do is to call kmem_cache_free
and put_user_ns. Those functions can be called in any context
so call them directly from free_user_ns removing the need for delayed work.
Acked-by: Serge Hallyn <serge.hallyn@canonical.com>
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Diffstat (limited to 'kernel/user_namespace.c')
-rw-r--r-- | kernel/user_namespace.c | 42 |
1 files changed, 20 insertions, 22 deletions
diff --git a/kernel/user_namespace.c b/kernel/user_namespace.c index 898e973bd1e8..ed08836558e0 100644 --- a/kernel/user_namespace.c +++ b/kernel/user_namespace.c | |||
@@ -27,6 +27,16 @@ int create_user_ns(struct cred *new) | |||
27 | { | 27 | { |
28 | struct user_namespace *ns, *parent_ns = new->user_ns; | 28 | struct user_namespace *ns, *parent_ns = new->user_ns; |
29 | struct user_struct *root_user; | 29 | struct user_struct *root_user; |
30 | kuid_t owner = make_kuid(new->user_ns, new->euid); | ||
31 | kgid_t group = make_kgid(new->user_ns, new->egid); | ||
32 | |||
33 | /* The creator needs a mapping in the parent user namespace | ||
34 | * or else we won't be able to reasonably tell userspace who | ||
35 | * created a user_namespace. | ||
36 | */ | ||
37 | if (!kuid_has_mapping(parent_ns, owner) || | ||
38 | !kgid_has_mapping(parent_ns, group)) | ||
39 | return -EPERM; | ||
30 | 40 | ||
31 | ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL); | 41 | ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL); |
32 | if (!ns) | 42 | if (!ns) |
@@ -43,7 +53,9 @@ int create_user_ns(struct cred *new) | |||
43 | 53 | ||
44 | /* set the new root user in the credentials under preparation */ | 54 | /* set the new root user in the credentials under preparation */ |
45 | ns->parent = parent_ns; | 55 | ns->parent = parent_ns; |
46 | ns->creator = new->user; | 56 | ns->owner = owner; |
57 | ns->group = group; | ||
58 | free_uid(new->user); | ||
47 | new->user = root_user; | 59 | new->user = root_user; |
48 | new->uid = new->euid = new->suid = new->fsuid = 0; | 60 | new->uid = new->euid = new->suid = new->fsuid = 0; |
49 | new->gid = new->egid = new->sgid = new->fsgid = 0; | 61 | new->gid = new->egid = new->sgid = new->fsgid = 0; |
@@ -63,35 +75,22 @@ int create_user_ns(struct cred *new) | |||
63 | #endif | 75 | #endif |
64 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ | 76 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ |
65 | 77 | ||
66 | /* Leave the reference to our user_ns with the new cred */ | 78 | /* Leave the new->user_ns reference with the new user namespace. */ |
79 | /* Leave the reference to our user_ns with the new cred. */ | ||
67 | new->user_ns = ns; | 80 | new->user_ns = ns; |
68 | 81 | ||
69 | return 0; | 82 | return 0; |
70 | } | 83 | } |
71 | 84 | ||
72 | /* | 85 | void free_user_ns(struct kref *kref) |
73 | * Deferred destructor for a user namespace. This is required because | ||
74 | * free_user_ns() may be called with uidhash_lock held, but we need to call | ||
75 | * back to free_uid() which will want to take the lock again. | ||
76 | */ | ||
77 | static void free_user_ns_work(struct work_struct *work) | ||
78 | { | 86 | { |
79 | struct user_namespace *parent, *ns = | 87 | struct user_namespace *parent, *ns = |
80 | container_of(work, struct user_namespace, destroyer); | 88 | container_of(kref, struct user_namespace, kref); |
89 | |||
81 | parent = ns->parent; | 90 | parent = ns->parent; |
82 | free_uid(ns->creator); | ||
83 | kmem_cache_free(user_ns_cachep, ns); | 91 | kmem_cache_free(user_ns_cachep, ns); |
84 | put_user_ns(parent); | 92 | put_user_ns(parent); |
85 | } | 93 | } |
86 | |||
87 | void free_user_ns(struct kref *kref) | ||
88 | { | ||
89 | struct user_namespace *ns = | ||
90 | container_of(kref, struct user_namespace, kref); | ||
91 | |||
92 | INIT_WORK(&ns->destroyer, free_user_ns_work); | ||
93 | schedule_work(&ns->destroyer); | ||
94 | } | ||
95 | EXPORT_SYMBOL(free_user_ns); | 94 | EXPORT_SYMBOL(free_user_ns); |
96 | 95 | ||
97 | uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid) | 96 | uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid) |
@@ -101,12 +100,11 @@ uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t | |||
101 | if (likely(to == cred->user_ns)) | 100 | if (likely(to == cred->user_ns)) |
102 | return uid; | 101 | return uid; |
103 | 102 | ||
104 | |||
105 | /* Is cred->user the creator of the target user_ns | 103 | /* Is cred->user the creator of the target user_ns |
106 | * or the creator of one of it's parents? | 104 | * or the creator of one of it's parents? |
107 | */ | 105 | */ |
108 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { | 106 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { |
109 | if (cred->user == tmp->creator) { | 107 | if (uid_eq(cred->user->uid, tmp->owner)) { |
110 | return (uid_t)0; | 108 | return (uid_t)0; |
111 | } | 109 | } |
112 | } | 110 | } |
@@ -126,7 +124,7 @@ gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t | |||
126 | * or the creator of one of it's parents? | 124 | * or the creator of one of it's parents? |
127 | */ | 125 | */ |
128 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { | 126 | for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) { |
129 | if (cred->user == tmp->creator) { | 127 | if (uid_eq(cred->user->uid, tmp->owner)) { |
130 | return (gid_t)0; | 128 | return (gid_t)0; |
131 | } | 129 | } |
132 | } | 130 | } |