Skip to content

Commit 783291e

Browse files
committed
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 <[email protected]> Signed-off-by: Eric W. Biederman <[email protected]>
1 parent 7b44ab9 commit 783291e

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

include/linux/user_namespace.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
struct user_namespace {
1010
struct kref kref;
1111
struct user_namespace *parent;
12-
struct user_struct *creator;
13-
struct work_struct destroyer;
12+
kuid_t owner;
13+
kgid_t group;
1414
};
1515

1616
extern struct user_namespace init_user_ns;

kernel/user.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ struct user_namespace init_user_ns = {
2525
.kref = {
2626
.refcount = ATOMIC_INIT(3),
2727
},
28-
.creator = &root_user,
28+
.owner = GLOBAL_ROOT_UID,
29+
.group = GLOBAL_ROOT_GID,
2930
};
3031
EXPORT_SYMBOL_GPL(init_user_ns);
3132

@@ -54,9 +55,9 @@ struct hlist_head uidhash_table[UIDHASH_SZ];
5455
*/
5556
static DEFINE_SPINLOCK(uidhash_lock);
5657

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 */
5859
struct user_struct root_user = {
59-
.__count = ATOMIC_INIT(2),
60+
.__count = ATOMIC_INIT(1),
6061
.processes = ATOMIC_INIT(1),
6162
.files = ATOMIC_INIT(0),
6263
.sigpending = ATOMIC_INIT(0),

kernel/user_namespace.c

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ int create_user_ns(struct cred *new)
2727
{
2828
struct user_namespace *ns, *parent_ns = new->user_ns;
2929
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;
3040

3141
ns = kmem_cache_alloc(user_ns_cachep, GFP_KERNEL);
3242
if (!ns)
@@ -43,7 +53,9 @@ int create_user_ns(struct cred *new)
4353

4454
/* set the new root user in the credentials under preparation */
4555
ns->parent = parent_ns;
46-
ns->creator = new->user;
56+
ns->owner = owner;
57+
ns->group = group;
58+
free_uid(new->user);
4759
new->user = root_user;
4860
new->uid = new->euid = new->suid = new->fsuid = 0;
4961
new->gid = new->egid = new->sgid = new->fsgid = 0;
@@ -63,35 +75,22 @@ int create_user_ns(struct cred *new)
6375
#endif
6476
/* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
6577

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. */
6780
new->user_ns = ns;
6881

6982
return 0;
7083
}
7184

72-
/*
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)
85+
void free_user_ns(struct kref *kref)
7886
{
7987
struct user_namespace *parent, *ns =
80-
container_of(work, struct user_namespace, destroyer);
88+
container_of(kref, struct user_namespace, kref);
89+
8190
parent = ns->parent;
82-
free_uid(ns->creator);
8391
kmem_cache_free(user_ns_cachep, ns);
8492
put_user_ns(parent);
8593
}
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-
}
9594
EXPORT_SYMBOL(free_user_ns);
9695

9796
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
101100
if (likely(to == cred->user_ns))
102101
return uid;
103102

104-
105103
/* Is cred->user the creator of the target user_ns
106104
* or the creator of one of it's parents?
107105
*/
108106
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)) {
110108
return (uid_t)0;
111109
}
112110
}
@@ -126,7 +124,7 @@ gid_t user_ns_map_gid(struct user_namespace *to, const struct cred *cred, gid_t
126124
* or the creator of one of it's parents?
127125
*/
128126
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)) {
130128
return (gid_t)0;
131129
}
132130
}

security/commoncap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ int cap_capable(const struct cred *cred, struct user_namespace *targ_ns,
7676
int cap, int audit)
7777
{
7878
for (;;) {
79-
/* The creator of the user namespace has all caps. */
80-
if (targ_ns != &init_user_ns && targ_ns->creator == cred->user)
79+
/* The owner of the user namespace has all caps. */
80+
if (targ_ns != &init_user_ns && uid_eq(targ_ns->owner,
81+
make_kuid(cred->user_ns, cred->euid)))
8182
return 0;
8283

8384
/* Do we have the necessary capabilities? */

0 commit comments

Comments
 (0)