aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric W. Biederman <ebiederm@xmission.com>2011-11-17 04:32:59 -0500
committerEric W. Biederman <ebiederm@xmission.com>2012-04-26 05:00:59 -0400
commit783291e6900292521a3895583785e0c04a56c5b3 (patch)
tree9dd368a25ea61b5913646b1d93ec99e865c058ba
parent7b44ab978b77a91b327058a0f4db7e6fcdb90b92 (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>
-rw-r--r--include/linux/user_namespace.h4
-rw-r--r--kernel/user.c7
-rw-r--r--kernel/user_namespace.c42
-rw-r--r--security/commoncap.c5
4 files changed, 29 insertions, 29 deletions
diff --git a/include/linux/user_namespace.h b/include/linux/user_namespace.h
index d767508db4f9..8a391bd53de2 100644
--- a/include/linux/user_namespace.h
+++ b/include/linux/user_namespace.h
@@ -9,8 +9,8 @@
9struct user_namespace { 9struct user_namespace {
10 struct kref kref; 10 struct kref kref;
11 struct user_namespace *parent; 11 struct user_namespace *parent;
12 struct user_struct *creator; 12 kuid_t owner;
13 struct work_struct destroyer; 13 kgid_t group;
14}; 14};
15 15
16extern struct user_namespace init_user_ns; 16extern struct user_namespace init_user_ns;
diff --git a/kernel/user.c b/kernel/user.c
index 025077e54a7c..cff385659175 100644
--- a/kernel/user.c
+++ b/kernel/user.c
@@ -25,7 +25,8 @@ struct user_namespace init_user_ns = {
25 .kref = { 25 .kref = {
26 .refcount = ATOMIC_INIT(3), 26 .refcount = ATOMIC_INIT(3),
27 }, 27 },
28 .creator = &root_user, 28 .owner = GLOBAL_ROOT_UID,
29 .group = GLOBAL_ROOT_GID,
29}; 30};
30EXPORT_SYMBOL_GPL(init_user_ns); 31EXPORT_SYMBOL_GPL(init_user_ns);
31 32
@@ -54,9 +55,9 @@ struct hlist_head uidhash_table[UIDHASH_SZ];
54 */ 55 */
55static DEFINE_SPINLOCK(uidhash_lock); 56static DEFINE_SPINLOCK(uidhash_lock);
56 57
57/* root_user.__count is 2, 1 for init task cred, 1 for init_user_ns->user_ns */ 58/* root_user.__count is 1, for init task cred */
58struct user_struct root_user = { 59struct user_struct root_user = {
59 .__count = ATOMIC_INIT(2), 60 .__count = ATOMIC_INIT(1),
60 .processes = ATOMIC_INIT(1), 61 .processes = ATOMIC_INIT(1),
61 .files = ATOMIC_INIT(0), 62 .files = ATOMIC_INIT(0),
62 .sigpending = ATOMIC_INIT(0), 63 .sigpending = ATOMIC_INIT(0),
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/* 85void 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 */
77static 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
87void 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}
95EXPORT_SYMBOL(free_user_ns); 94EXPORT_SYMBOL(free_user_ns);
96 95
97uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t uid) 96uid_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 }
diff --git a/security/commoncap.c b/security/commoncap.c
index 435d074853f3..f2399d8afbe0 100644
--- a/security/commoncap.c
+++ b/security/commoncap.c
@@ -76,8 +76,9 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
76 int cap, int audit) 76 int cap, int audit)
77{ 77{
78 for (;;) { 78 for (;;) {
79 /* The creator of the user namespace has all caps. */ 79 /* The owner of the user namespace has all caps. */
80 if (targ_ns != &init_user_ns && targ_ns->creator == cred->user) 80 if (targ_ns != &init_user_ns && uid_eq(targ_ns->owner,
81 make_kuid(cred->user_ns, cred->euid)))
81 return 0; 82 return 0;
82 83
83 /* Do we have the necessary capabilities? */ 84 /* Do we have the necessary capabilities? */