Skip to content

Commit aeb3ae9

Browse files
committed
userns: Add an explicit reference to the parent user namespace
I am about to remove the struct user_namespace reference from struct user_struct. So keep an explicit track of the parent user namespace. Take advantage of this new reference and replace instances of user_ns->creator->user_ns with user_ns->parent. Acked-by: Serge Hallyn <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent 0093ccb commit aeb3ae9

File tree

3 files changed

+8
-8
lines changed

3 files changed

+8
-8
lines changed

include/linux/user_namespace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
struct user_namespace {
1313
struct kref kref;
1414
struct hlist_head uidhash_table[UIDHASH_SZ];
15+
struct user_namespace *parent;
1516
struct user_struct *creator;
1617
struct work_struct destroyer;
1718
};

kernel/user_namespace.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ int create_user_ns(struct cred *new)
4545
}
4646

4747
/* set the new root user in the credentials under preparation */
48+
ns->parent = parent_ns;
4849
ns->creator = new->user;
4950
new->user = root_user;
5051
new->uid = new->euid = new->suid = new->fsuid = 0;
@@ -60,8 +61,6 @@ int create_user_ns(struct cred *new)
6061
/* Leave the reference to our user_ns with the new cred */
6162
new->user_ns = ns;
6263

63-
put_user_ns(parent_ns);
64-
6564
return 0;
6665
}
6766

@@ -72,10 +71,12 @@ int create_user_ns(struct cred *new)
7271
*/
7372
static void free_user_ns_work(struct work_struct *work)
7473
{
75-
struct user_namespace *ns =
74+
struct user_namespace *parent, *ns =
7675
container_of(work, struct user_namespace, destroyer);
76+
parent = ns->parent;
7777
free_uid(ns->creator);
7878
kmem_cache_free(user_ns_cachep, ns);
79+
put_user_ns(parent);
7980
}
8081

8182
void free_user_ns(struct kref *kref)
@@ -99,8 +100,7 @@ uid_t user_ns_map_uid(struct user_namespace *to, const struct cred *cred, uid_t
99100
/* Is cred->user the creator of the target user_ns
100101
* or the creator of one of it's parents?
101102
*/
102-
for ( tmp = to; tmp != &init_user_ns;
103-
tmp = tmp->creator->user_ns ) {
103+
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
104104
if (cred->user == tmp->creator) {
105105
return (uid_t)0;
106106
}
@@ -120,8 +120,7 @@ gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t
120120
/* Is cred->user the creator of the target user_ns
121121
* or the creator of one of it's parents?
122122
*/
123-
for ( tmp = to; tmp != &init_user_ns;
124-
tmp = tmp->creator->user_ns ) {
123+
for ( tmp = to; tmp != &init_user_ns; tmp = tmp->parent ) {
125124
if (cred->user == tmp->creator) {
126125
return (gid_t)0;
127126
}

security/commoncap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
9292
*If you have a capability in a parent user ns, then you have
9393
* it over all children user namespaces as well.
9494
*/
95-
targ_ns = targ_ns->creator->user_ns;
95+
targ_ns = targ_ns->parent;
9696
}
9797

9898
/* We never get here */

0 commit comments

Comments
 (0)