Commit | Line | Data |
---|---|---|
acce292c CLG |
1 | /* |
2 | * This program is free software; you can redistribute it and/or | |
3 | * modify it under the terms of the GNU General Public License as | |
4 | * published by the Free Software Foundation, version 2 of the | |
5 | * License. | |
6 | */ | |
7 | ||
8 | #include <linux/module.h> | |
acce292c | 9 | #include <linux/nsproxy.h> |
1aeb272c | 10 | #include <linux/slab.h> |
acce292c CLG |
11 | #include <linux/user_namespace.h> |
12 | ||
77ec739d SH |
13 | /* |
14 | * Clone a new ns copying an original user ns, setting refcount to 1 | |
15 | * @old_ns: namespace to clone | |
16 | * Return NULL on error (failure to kmalloc), new ns otherwise | |
17 | */ | |
18 | static struct user_namespace *clone_user_ns(struct user_namespace *old_ns) | |
19 | { | |
20 | struct user_namespace *ns; | |
21 | struct user_struct *new_user; | |
d84f4f99 | 22 | struct cred *new; |
77ec739d SH |
23 | int n; |
24 | ||
25 | ns = kmalloc(sizeof(struct user_namespace), GFP_KERNEL); | |
26 | if (!ns) | |
467e9f4b | 27 | return ERR_PTR(-ENOMEM); |
77ec739d SH |
28 | |
29 | kref_init(&ns->kref); | |
30 | ||
31 | for (n = 0; n < UIDHASH_SZ; ++n) | |
735de223 | 32 | INIT_HLIST_HEAD(ns->uidhash_table + n); |
77ec739d SH |
33 | |
34 | /* Insert new root user. */ | |
35 | ns->root_user = alloc_uid(ns, 0); | |
36 | if (!ns->root_user) { | |
37 | kfree(ns); | |
467e9f4b | 38 | return ERR_PTR(-ENOMEM); |
77ec739d SH |
39 | } |
40 | ||
41 | /* Reset current->user with a new one */ | |
76aac0e9 | 42 | new_user = alloc_uid(ns, current_uid()); |
77ec739d SH |
43 | if (!new_user) { |
44 | free_uid(ns->root_user); | |
45 | kfree(ns); | |
467e9f4b | 46 | return ERR_PTR(-ENOMEM); |
77ec739d SH |
47 | } |
48 | ||
d84f4f99 DH |
49 | /* Install the new user */ |
50 | new = prepare_creds(); | |
51 | if (!new) { | |
52 | free_uid(new_user); | |
53 | free_uid(ns->root_user); | |
54 | kfree(ns); | |
55 | } | |
56 | free_uid(new->user); | |
57 | new->user = new_user; | |
58 | commit_creds(new); | |
77ec739d SH |
59 | return ns; |
60 | } | |
61 | ||
acce292c CLG |
62 | struct 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 | ||
77ec739d SH |
69 | if (!(flags & CLONE_NEWUSER)) |
70 | return old_ns; | |
71 | ||
72 | new_ns = clone_user_ns(old_ns); | |
73 | ||
74 | put_user_ns(old_ns); | |
acce292c CLG |
75 | return new_ns; |
76 | } | |
77 | ||
78 | void free_user_ns(struct kref *kref) | |
79 | { | |
80 | struct user_namespace *ns; | |
81 | ||
82 | ns = container_of(kref, struct user_namespace, kref); | |
28f300d2 | 83 | release_uids(ns); |
acce292c CLG |
84 | kfree(ns); |
85 | } | |
6a3fd92e | 86 | EXPORT_SYMBOL(free_user_ns); |