]>
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 | ||
9984de1a | 8 | #include <linux/export.h> |
acce292c | 9 | #include <linux/nsproxy.h> |
1aeb272c | 10 | #include <linux/slab.h> |
3f07c014 | 11 | #include <linux/sched/signal.h> |
acce292c | 12 | #include <linux/user_namespace.h> |
0bb80f24 | 13 | #include <linux/proc_ns.h> |
5c1469de | 14 | #include <linux/highuid.h> |
18b6e041 | 15 | #include <linux/cred.h> |
973c5914 | 16 | #include <linux/securebits.h> |
22d917d8 EB |
17 | #include <linux/keyctl.h> |
18 | #include <linux/key-type.h> | |
19 | #include <keys/user-type.h> | |
20 | #include <linux/seq_file.h> | |
21 | #include <linux/fs.h> | |
22 | #include <linux/uaccess.h> | |
23 | #include <linux/ctype.h> | |
f76d207a | 24 | #include <linux/projid.h> |
e66eded8 | 25 | #include <linux/fs_struct.h> |
6397fac4 CB |
26 | #include <linux/bsearch.h> |
27 | #include <linux/sort.h> | |
acce292c | 28 | |
6164281a | 29 | static struct kmem_cache *user_ns_cachep __read_mostly; |
f0d62aec | 30 | static DEFINE_MUTEX(userns_state_mutex); |
6164281a | 31 | |
6708075f EB |
32 | static bool new_idmap_permitted(const struct file *file, |
33 | struct user_namespace *ns, int cap_setid, | |
22d917d8 | 34 | struct uid_gid_map *map); |
b032132c | 35 | static void free_user_ns(struct work_struct *work); |
22d917d8 | 36 | |
25f9c081 EB |
37 | static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid) |
38 | { | |
39 | return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES); | |
40 | } | |
41 | ||
42 | static void dec_user_namespaces(struct ucounts *ucounts) | |
43 | { | |
44 | return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES); | |
45 | } | |
46 | ||
cde1975b EB |
47 | static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns) |
48 | { | |
49 | /* Start with the same capabilities as init but useless for doing | |
50 | * anything as the capabilities are bound to the new user namespace. | |
51 | */ | |
52 | cred->securebits = SECUREBITS_DEFAULT; | |
53 | cred->cap_inheritable = CAP_EMPTY_SET; | |
54 | cred->cap_permitted = CAP_FULL_SET; | |
55 | cred->cap_effective = CAP_FULL_SET; | |
58319057 | 56 | cred->cap_ambient = CAP_EMPTY_SET; |
cde1975b EB |
57 | cred->cap_bset = CAP_FULL_SET; |
58 | #ifdef CONFIG_KEYS | |
59 | key_put(cred->request_key_auth); | |
60 | cred->request_key_auth = NULL; | |
61 | #endif | |
62 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ | |
63 | cred->user_ns = user_ns; | |
64 | } | |
65 | ||
77ec739d | 66 | /* |
18b6e041 SH |
67 | * Create a new user namespace, deriving the creator from the user in the |
68 | * passed credentials, and replacing that user with the new root user for the | |
69 | * new namespace. | |
70 | * | |
71 | * This is called by copy_creds(), which will finish setting the target task's | |
72 | * credentials. | |
77ec739d | 73 | */ |
18b6e041 | 74 | int create_user_ns(struct cred *new) |
77ec739d | 75 | { |
0093ccb6 | 76 | struct user_namespace *ns, *parent_ns = new->user_ns; |
078de5f7 EB |
77 | kuid_t owner = new->euid; |
78 | kgid_t group = new->egid; | |
f6b2db1a | 79 | struct ucounts *ucounts; |
25f9c081 | 80 | int ret, i; |
783291e6 | 81 | |
df75e774 | 82 | ret = -ENOSPC; |
8742f229 | 83 | if (parent_ns->level > 32) |
b376c3e1 EB |
84 | goto fail; |
85 | ||
f6b2db1a EB |
86 | ucounts = inc_user_namespaces(parent_ns, owner); |
87 | if (!ucounts) | |
b376c3e1 | 88 | goto fail; |
8742f229 | 89 | |
3151527e EB |
90 | /* |
91 | * Verify that we can not violate the policy of which files | |
92 | * may be accessed that is specified by the root directory, | |
93 | * by verifing that the root directory is at the root of the | |
94 | * mount namespace which allows all files to be accessed. | |
95 | */ | |
b376c3e1 | 96 | ret = -EPERM; |
3151527e | 97 | if (current_chrooted()) |
b376c3e1 | 98 | goto fail_dec; |
3151527e | 99 | |
783291e6 EB |
100 | /* The creator needs a mapping in the parent user namespace |
101 | * or else we won't be able to reasonably tell userspace who | |
102 | * created a user_namespace. | |
103 | */ | |
b376c3e1 | 104 | ret = -EPERM; |
783291e6 EB |
105 | if (!kuid_has_mapping(parent_ns, owner) || |
106 | !kgid_has_mapping(parent_ns, group)) | |
b376c3e1 | 107 | goto fail_dec; |
77ec739d | 108 | |
b376c3e1 | 109 | ret = -ENOMEM; |
22d917d8 | 110 | ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL); |
77ec739d | 111 | if (!ns) |
b376c3e1 | 112 | goto fail_dec; |
77ec739d | 113 | |
6344c433 | 114 | ret = ns_alloc_inum(&ns->ns); |
b376c3e1 EB |
115 | if (ret) |
116 | goto fail_free; | |
33c42940 | 117 | ns->ns.ops = &userns_operations; |
98f842e6 | 118 | |
c61a2810 | 119 | atomic_set(&ns->count, 1); |
cde1975b | 120 | /* Leave the new->user_ns reference with the new user namespace. */ |
aeb3ae9d | 121 | ns->parent = parent_ns; |
8742f229 | 122 | ns->level = parent_ns->level + 1; |
783291e6 EB |
123 | ns->owner = owner; |
124 | ns->group = group; | |
b032132c | 125 | INIT_WORK(&ns->work, free_user_ns); |
25f9c081 EB |
126 | for (i = 0; i < UCOUNT_COUNTS; i++) { |
127 | ns->ucount_max[i] = INT_MAX; | |
128 | } | |
f6b2db1a | 129 | ns->ucounts = ucounts; |
22d917d8 | 130 | |
9cc46516 EB |
131 | /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */ |
132 | mutex_lock(&userns_state_mutex); | |
133 | ns->flags = parent_ns->flags; | |
134 | mutex_unlock(&userns_state_mutex); | |
135 | ||
b206f281 DH |
136 | #ifdef CONFIG_KEYS |
137 | INIT_LIST_HEAD(&ns->keyring_name_list); | |
138 | #endif | |
f36f8c75 DH |
139 | #ifdef CONFIG_PERSISTENT_KEYRINGS |
140 | init_rwsem(&ns->persistent_keyring_register_sem); | |
141 | #endif | |
dbec2846 EB |
142 | ret = -ENOMEM; |
143 | if (!setup_userns_sysctls(ns)) | |
144 | goto fail_keyring; | |
145 | ||
146 | set_cred_user_ns(new, ns); | |
18b6e041 | 147 | return 0; |
dbec2846 EB |
148 | fail_keyring: |
149 | #ifdef CONFIG_PERSISTENT_KEYRINGS | |
150 | key_put(ns->persistent_keyring_register); | |
151 | #endif | |
152 | ns_free_inum(&ns->ns); | |
b376c3e1 | 153 | fail_free: |
dbec2846 | 154 | kmem_cache_free(user_ns_cachep, ns); |
b376c3e1 | 155 | fail_dec: |
f6b2db1a | 156 | dec_user_namespaces(ucounts); |
b376c3e1 | 157 | fail: |
dbec2846 | 158 | return ret; |
acce292c CLG |
159 | } |
160 | ||
b2e0d987 EB |
161 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) |
162 | { | |
163 | struct cred *cred; | |
6160968c | 164 | int err = -ENOMEM; |
b2e0d987 EB |
165 | |
166 | if (!(unshare_flags & CLONE_NEWUSER)) | |
167 | return 0; | |
168 | ||
169 | cred = prepare_creds(); | |
6160968c ON |
170 | if (cred) { |
171 | err = create_user_ns(cred); | |
172 | if (err) | |
173 | put_cred(cred); | |
174 | else | |
175 | *new_cred = cred; | |
176 | } | |
b2e0d987 | 177 | |
6160968c | 178 | return err; |
b2e0d987 EB |
179 | } |
180 | ||
b032132c | 181 | static void free_user_ns(struct work_struct *work) |
acce292c | 182 | { |
b032132c EB |
183 | struct user_namespace *parent, *ns = |
184 | container_of(work, struct user_namespace, work); | |
783291e6 | 185 | |
c61a2810 | 186 | do { |
f6b2db1a | 187 | struct ucounts *ucounts = ns->ucounts; |
c61a2810 | 188 | parent = ns->parent; |
6397fac4 CB |
189 | if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { |
190 | kfree(ns->gid_map.forward); | |
191 | kfree(ns->gid_map.reverse); | |
192 | } | |
193 | if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { | |
194 | kfree(ns->uid_map.forward); | |
195 | kfree(ns->uid_map.reverse); | |
196 | } | |
197 | if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { | |
198 | kfree(ns->projid_map.forward); | |
199 | kfree(ns->projid_map.reverse); | |
200 | } | |
dbec2846 | 201 | retire_userns_sysctls(ns); |
b206f281 | 202 | key_free_user_ns(ns); |
6344c433 | 203 | ns_free_inum(&ns->ns); |
c61a2810 | 204 | kmem_cache_free(user_ns_cachep, ns); |
f6b2db1a | 205 | dec_user_namespaces(ucounts); |
c61a2810 EB |
206 | ns = parent; |
207 | } while (atomic_dec_and_test(&parent->count)); | |
acce292c | 208 | } |
b032132c EB |
209 | |
210 | void __put_user_ns(struct user_namespace *ns) | |
211 | { | |
212 | schedule_work(&ns->work); | |
213 | } | |
214 | EXPORT_SYMBOL(__put_user_ns); | |
5c1469de | 215 | |
6397fac4 CB |
216 | /** |
217 | * idmap_key struct holds the information necessary to find an idmapping in a | |
218 | * sorted idmap array. It is passed to cmp_map_id() as first argument. | |
219 | */ | |
220 | struct idmap_key { | |
221 | bool map_up; /* true -> id from kid; false -> kid from id */ | |
222 | u32 id; /* id to find */ | |
223 | u32 count; /* == 0 unless used with map_id_range_down() */ | |
224 | }; | |
225 | ||
226 | /** | |
227 | * cmp_map_id - Function to be passed to bsearch() to find the requested | |
228 | * idmapping. Expects struct idmap_key to be passed via @k. | |
229 | */ | |
230 | static int cmp_map_id(const void *k, const void *e) | |
231 | { | |
232 | u32 first, last, id2; | |
233 | const struct idmap_key *key = k; | |
234 | const struct uid_gid_extent *el = e; | |
235 | ||
11a8b927 | 236 | id2 = key->id + key->count - 1; |
6397fac4 CB |
237 | |
238 | /* handle map_id_{down,up}() */ | |
239 | if (key->map_up) | |
240 | first = el->lower_first; | |
241 | else | |
242 | first = el->first; | |
243 | ||
244 | last = first + el->count - 1; | |
245 | ||
246 | if (key->id >= first && key->id <= last && | |
247 | (id2 >= first && id2 <= last)) | |
248 | return 0; | |
249 | ||
250 | if (key->id < first || id2 < first) | |
251 | return -1; | |
252 | ||
253 | return 1; | |
254 | } | |
255 | ||
256 | /** | |
257 | * map_id_range_down_max - Find idmap via binary search in ordered idmap array. | |
258 | * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. | |
259 | */ | |
3edf652f EB |
260 | static struct uid_gid_extent * |
261 | map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count) | |
5c1469de | 262 | { |
6397fac4 CB |
263 | struct idmap_key key; |
264 | ||
265 | key.map_up = false; | |
266 | key.count = count; | |
267 | key.id = id; | |
268 | ||
3edf652f EB |
269 | return bsearch(&key, map->forward, extents, |
270 | sizeof(struct uid_gid_extent), cmp_map_id); | |
6397fac4 CB |
271 | } |
272 | ||
273 | /** | |
274 | * map_id_range_down_base - Find idmap via binary search in static extent array. | |
275 | * Can only be called if number of mappings is equal or less than | |
276 | * UID_GID_MAP_MAX_BASE_EXTENTS. | |
277 | */ | |
3edf652f EB |
278 | static struct uid_gid_extent * |
279 | map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count) | |
5c1469de | 280 | { |
3edf652f | 281 | unsigned idx; |
22d917d8 | 282 | u32 first, last, id2; |
5c1469de | 283 | |
22d917d8 | 284 | id2 = id + count - 1; |
5c1469de | 285 | |
22d917d8 | 286 | /* Find the matching extent */ |
22d917d8 EB |
287 | for (idx = 0; idx < extents; idx++) { |
288 | first = map->extent[idx].first; | |
289 | last = first + map->extent[idx].count - 1; | |
290 | if (id >= first && id <= last && | |
291 | (id2 >= first && id2 <= last)) | |
3edf652f | 292 | return &map->extent[idx]; |
22d917d8 | 293 | } |
3edf652f | 294 | return NULL; |
22d917d8 EB |
295 | } |
296 | ||
6397fac4 CB |
297 | static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count) |
298 | { | |
3edf652f EB |
299 | struct uid_gid_extent *extent; |
300 | unsigned extents = map->nr_extents; | |
6397fac4 CB |
301 | smp_rmb(); |
302 | ||
303 | if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) | |
3edf652f EB |
304 | extent = map_id_range_down_base(extents, map, id, count); |
305 | else | |
306 | extent = map_id_range_down_max(extents, map, id, count); | |
307 | ||
22d917d8 | 308 | /* Map the id or note failure */ |
3edf652f EB |
309 | if (extent) |
310 | id = (id - extent->first) + extent->lower_first; | |
22d917d8 EB |
311 | else |
312 | id = (u32) -1; | |
313 | ||
314 | return id; | |
315 | } | |
316 | ||
317 | static u32 map_id_down(struct uid_gid_map *map, u32 id) | |
318 | { | |
ece66133 | 319 | return map_id_range_down(map, id, 1); |
6397fac4 CB |
320 | } |
321 | ||
322 | /** | |
323 | * map_id_up_base - Find idmap via binary search in static extent array. | |
324 | * Can only be called if number of mappings is equal or less than | |
325 | * UID_GID_MAP_MAX_BASE_EXTENTS. | |
326 | */ | |
3edf652f EB |
327 | static struct uid_gid_extent * |
328 | map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id) | |
22d917d8 | 329 | { |
3edf652f | 330 | unsigned idx; |
22d917d8 EB |
331 | u32 first, last; |
332 | ||
333 | /* Find the matching extent */ | |
22d917d8 | 334 | for (idx = 0; idx < extents; idx++) { |
22d917d8 | 335 | first = map->extent[idx].lower_first; |
22d917d8 EB |
336 | last = first + map->extent[idx].count - 1; |
337 | if (id >= first && id <= last) | |
3edf652f | 338 | return &map->extent[idx]; |
22d917d8 | 339 | } |
3edf652f | 340 | return NULL; |
22d917d8 | 341 | } |
22d917d8 | 342 | |
6397fac4 CB |
343 | /** |
344 | * map_id_up_max - Find idmap via binary search in ordered idmap array. | |
345 | * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. | |
346 | */ | |
3edf652f EB |
347 | static struct uid_gid_extent * |
348 | map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id) | |
6397fac4 | 349 | { |
6397fac4 CB |
350 | struct idmap_key key; |
351 | ||
352 | key.map_up = true; | |
11a8b927 | 353 | key.count = 1; |
6397fac4 CB |
354 | key.id = id; |
355 | ||
3edf652f EB |
356 | return bsearch(&key, map->reverse, extents, |
357 | sizeof(struct uid_gid_extent), cmp_map_id); | |
22d917d8 EB |
358 | } |
359 | ||
360 | static u32 map_id_up(struct uid_gid_map *map, u32 id) | |
361 | { | |
3edf652f EB |
362 | struct uid_gid_extent *extent; |
363 | unsigned extents = map->nr_extents; | |
e79323bd | 364 | smp_rmb(); |
6397fac4 | 365 | |
3edf652f EB |
366 | if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) |
367 | extent = map_id_up_base(extents, map, id); | |
368 | else | |
369 | extent = map_id_up_max(extents, map, id); | |
370 | ||
22d917d8 | 371 | /* Map the id or note failure */ |
6397fac4 CB |
372 | if (extent) |
373 | id = (id - extent->lower_first) + extent->first; | |
22d917d8 EB |
374 | else |
375 | id = (u32) -1; | |
376 | ||
377 | return id; | |
378 | } | |
379 | ||
380 | /** | |
381 | * make_kuid - Map a user-namespace uid pair into a kuid. | |
382 | * @ns: User namespace that the uid is in | |
383 | * @uid: User identifier | |
384 | * | |
385 | * Maps a user-namespace uid pair into a kernel internal kuid, | |
386 | * and returns that kuid. | |
387 | * | |
388 | * When there is no mapping defined for the user-namespace uid | |
389 | * pair INVALID_UID is returned. Callers are expected to test | |
b080e047 | 390 | * for and handle INVALID_UID being returned. INVALID_UID |
22d917d8 EB |
391 | * may be tested for using uid_valid(). |
392 | */ | |
393 | kuid_t make_kuid(struct user_namespace *ns, uid_t uid) | |
394 | { | |
395 | /* Map the uid to a global kernel uid */ | |
396 | return KUIDT_INIT(map_id_down(&ns->uid_map, uid)); | |
397 | } | |
398 | EXPORT_SYMBOL(make_kuid); | |
399 | ||
400 | /** | |
401 | * from_kuid - Create a uid from a kuid user-namespace pair. | |
402 | * @targ: The user namespace we want a uid in. | |
403 | * @kuid: The kernel internal uid to start with. | |
404 | * | |
405 | * Map @kuid into the user-namespace specified by @targ and | |
406 | * return the resulting uid. | |
407 | * | |
408 | * There is always a mapping into the initial user_namespace. | |
409 | * | |
410 | * If @kuid has no mapping in @targ (uid_t)-1 is returned. | |
411 | */ | |
412 | uid_t from_kuid(struct user_namespace *targ, kuid_t kuid) | |
413 | { | |
414 | /* Map the uid from a global kernel uid */ | |
415 | return map_id_up(&targ->uid_map, __kuid_val(kuid)); | |
416 | } | |
417 | EXPORT_SYMBOL(from_kuid); | |
418 | ||
419 | /** | |
420 | * from_kuid_munged - Create a uid from a kuid user-namespace pair. | |
421 | * @targ: The user namespace we want a uid in. | |
422 | * @kuid: The kernel internal uid to start with. | |
423 | * | |
424 | * Map @kuid into the user-namespace specified by @targ and | |
425 | * return the resulting uid. | |
426 | * | |
427 | * There is always a mapping into the initial user_namespace. | |
428 | * | |
429 | * Unlike from_kuid from_kuid_munged never fails and always | |
430 | * returns a valid uid. This makes from_kuid_munged appropriate | |
431 | * for use in syscalls like stat and getuid where failing the | |
432 | * system call and failing to provide a valid uid are not an | |
433 | * options. | |
434 | * | |
435 | * If @kuid has no mapping in @targ overflowuid is returned. | |
436 | */ | |
437 | uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid) | |
438 | { | |
439 | uid_t uid; | |
440 | uid = from_kuid(targ, kuid); | |
441 | ||
442 | if (uid == (uid_t) -1) | |
443 | uid = overflowuid; | |
444 | return uid; | |
445 | } | |
446 | EXPORT_SYMBOL(from_kuid_munged); | |
447 | ||
448 | /** | |
449 | * make_kgid - Map a user-namespace gid pair into a kgid. | |
450 | * @ns: User namespace that the gid is in | |
68a9a435 | 451 | * @gid: group identifier |
22d917d8 EB |
452 | * |
453 | * Maps a user-namespace gid pair into a kernel internal kgid, | |
454 | * and returns that kgid. | |
455 | * | |
456 | * When there is no mapping defined for the user-namespace gid | |
457 | * pair INVALID_GID is returned. Callers are expected to test | |
458 | * for and handle INVALID_GID being returned. INVALID_GID may be | |
459 | * tested for using gid_valid(). | |
460 | */ | |
461 | kgid_t make_kgid(struct user_namespace *ns, gid_t gid) | |
462 | { | |
463 | /* Map the gid to a global kernel gid */ | |
464 | return KGIDT_INIT(map_id_down(&ns->gid_map, gid)); | |
465 | } | |
466 | EXPORT_SYMBOL(make_kgid); | |
467 | ||
468 | /** | |
469 | * from_kgid - Create a gid from a kgid user-namespace pair. | |
470 | * @targ: The user namespace we want a gid in. | |
471 | * @kgid: The kernel internal gid to start with. | |
472 | * | |
473 | * Map @kgid into the user-namespace specified by @targ and | |
474 | * return the resulting gid. | |
475 | * | |
476 | * There is always a mapping into the initial user_namespace. | |
477 | * | |
478 | * If @kgid has no mapping in @targ (gid_t)-1 is returned. | |
479 | */ | |
480 | gid_t from_kgid(struct user_namespace *targ, kgid_t kgid) | |
481 | { | |
482 | /* Map the gid from a global kernel gid */ | |
483 | return map_id_up(&targ->gid_map, __kgid_val(kgid)); | |
484 | } | |
485 | EXPORT_SYMBOL(from_kgid); | |
486 | ||
487 | /** | |
488 | * from_kgid_munged - Create a gid from a kgid user-namespace pair. | |
489 | * @targ: The user namespace we want a gid in. | |
490 | * @kgid: The kernel internal gid to start with. | |
491 | * | |
492 | * Map @kgid into the user-namespace specified by @targ and | |
493 | * return the resulting gid. | |
494 | * | |
495 | * There is always a mapping into the initial user_namespace. | |
496 | * | |
497 | * Unlike from_kgid from_kgid_munged never fails and always | |
498 | * returns a valid gid. This makes from_kgid_munged appropriate | |
499 | * for use in syscalls like stat and getgid where failing the | |
500 | * system call and failing to provide a valid gid are not options. | |
501 | * | |
502 | * If @kgid has no mapping in @targ overflowgid is returned. | |
503 | */ | |
504 | gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid) | |
505 | { | |
506 | gid_t gid; | |
507 | gid = from_kgid(targ, kgid); | |
508 | ||
509 | if (gid == (gid_t) -1) | |
510 | gid = overflowgid; | |
511 | return gid; | |
512 | } | |
513 | EXPORT_SYMBOL(from_kgid_munged); | |
514 | ||
f76d207a EB |
515 | /** |
516 | * make_kprojid - Map a user-namespace projid pair into a kprojid. | |
517 | * @ns: User namespace that the projid is in | |
518 | * @projid: Project identifier | |
519 | * | |
520 | * Maps a user-namespace uid pair into a kernel internal kuid, | |
521 | * and returns that kuid. | |
522 | * | |
523 | * When there is no mapping defined for the user-namespace projid | |
524 | * pair INVALID_PROJID is returned. Callers are expected to test | |
525 | * for and handle handle INVALID_PROJID being returned. INVALID_PROJID | |
526 | * may be tested for using projid_valid(). | |
527 | */ | |
528 | kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid) | |
529 | { | |
530 | /* Map the uid to a global kernel uid */ | |
531 | return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid)); | |
532 | } | |
533 | EXPORT_SYMBOL(make_kprojid); | |
534 | ||
535 | /** | |
536 | * from_kprojid - Create a projid from a kprojid user-namespace pair. | |
537 | * @targ: The user namespace we want a projid in. | |
538 | * @kprojid: The kernel internal project identifier to start with. | |
539 | * | |
540 | * Map @kprojid into the user-namespace specified by @targ and | |
541 | * return the resulting projid. | |
542 | * | |
543 | * There is always a mapping into the initial user_namespace. | |
544 | * | |
545 | * If @kprojid has no mapping in @targ (projid_t)-1 is returned. | |
546 | */ | |
547 | projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid) | |
548 | { | |
549 | /* Map the uid from a global kernel uid */ | |
550 | return map_id_up(&targ->projid_map, __kprojid_val(kprojid)); | |
551 | } | |
552 | EXPORT_SYMBOL(from_kprojid); | |
553 | ||
554 | /** | |
555 | * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair. | |
556 | * @targ: The user namespace we want a projid in. | |
557 | * @kprojid: The kernel internal projid to start with. | |
558 | * | |
559 | * Map @kprojid into the user-namespace specified by @targ and | |
560 | * return the resulting projid. | |
561 | * | |
562 | * There is always a mapping into the initial user_namespace. | |
563 | * | |
564 | * Unlike from_kprojid from_kprojid_munged never fails and always | |
565 | * returns a valid projid. This makes from_kprojid_munged | |
566 | * appropriate for use in syscalls like stat and where | |
567 | * failing the system call and failing to provide a valid projid are | |
568 | * not an options. | |
569 | * | |
570 | * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned. | |
571 | */ | |
572 | projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid) | |
573 | { | |
574 | projid_t projid; | |
575 | projid = from_kprojid(targ, kprojid); | |
576 | ||
577 | if (projid == (projid_t) -1) | |
578 | projid = OVERFLOW_PROJID; | |
579 | return projid; | |
580 | } | |
581 | EXPORT_SYMBOL(from_kprojid_munged); | |
582 | ||
583 | ||
22d917d8 EB |
584 | static int uid_m_show(struct seq_file *seq, void *v) |
585 | { | |
586 | struct user_namespace *ns = seq->private; | |
587 | struct uid_gid_extent *extent = v; | |
588 | struct user_namespace *lower_ns; | |
589 | uid_t lower; | |
5c1469de | 590 | |
c450f371 | 591 | lower_ns = seq_user_ns(seq); |
22d917d8 EB |
592 | if ((lower_ns == ns) && lower_ns->parent) |
593 | lower_ns = lower_ns->parent; | |
594 | ||
595 | lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first)); | |
596 | ||
597 | seq_printf(seq, "%10u %10u %10u\n", | |
598 | extent->first, | |
599 | lower, | |
600 | extent->count); | |
601 | ||
602 | return 0; | |
5c1469de EB |
603 | } |
604 | ||
22d917d8 | 605 | static int gid_m_show(struct seq_file *seq, void *v) |
5c1469de | 606 | { |
22d917d8 EB |
607 | struct user_namespace *ns = seq->private; |
608 | struct uid_gid_extent *extent = v; | |
609 | struct user_namespace *lower_ns; | |
610 | gid_t lower; | |
5c1469de | 611 | |
c450f371 | 612 | lower_ns = seq_user_ns(seq); |
22d917d8 EB |
613 | if ((lower_ns == ns) && lower_ns->parent) |
614 | lower_ns = lower_ns->parent; | |
5c1469de | 615 | |
22d917d8 EB |
616 | lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first)); |
617 | ||
618 | seq_printf(seq, "%10u %10u %10u\n", | |
619 | extent->first, | |
620 | lower, | |
621 | extent->count); | |
622 | ||
623 | return 0; | |
624 | } | |
625 | ||
f76d207a EB |
626 | static int projid_m_show(struct seq_file *seq, void *v) |
627 | { | |
628 | struct user_namespace *ns = seq->private; | |
629 | struct uid_gid_extent *extent = v; | |
630 | struct user_namespace *lower_ns; | |
631 | projid_t lower; | |
632 | ||
633 | lower_ns = seq_user_ns(seq); | |
634 | if ((lower_ns == ns) && lower_ns->parent) | |
635 | lower_ns = lower_ns->parent; | |
636 | ||
637 | lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first)); | |
638 | ||
639 | seq_printf(seq, "%10u %10u %10u\n", | |
640 | extent->first, | |
641 | lower, | |
642 | extent->count); | |
643 | ||
644 | return 0; | |
645 | } | |
646 | ||
68a9a435 FF |
647 | static void *m_start(struct seq_file *seq, loff_t *ppos, |
648 | struct uid_gid_map *map) | |
22d917d8 | 649 | { |
22d917d8 | 650 | loff_t pos = *ppos; |
d5e7b3c5 EB |
651 | unsigned extents = map->nr_extents; |
652 | smp_rmb(); | |
22d917d8 | 653 | |
d5e7b3c5 | 654 | if (pos >= extents) |
6397fac4 | 655 | return NULL; |
22d917d8 | 656 | |
d5e7b3c5 | 657 | if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS) |
6397fac4 | 658 | return &map->extent[pos]; |
22d917d8 | 659 | |
6397fac4 | 660 | return &map->forward[pos]; |
22d917d8 EB |
661 | } |
662 | ||
663 | static void *uid_m_start(struct seq_file *seq, loff_t *ppos) | |
664 | { | |
665 | struct user_namespace *ns = seq->private; | |
666 | ||
667 | return m_start(seq, ppos, &ns->uid_map); | |
668 | } | |
669 | ||
670 | static void *gid_m_start(struct seq_file *seq, loff_t *ppos) | |
671 | { | |
672 | struct user_namespace *ns = seq->private; | |
673 | ||
674 | return m_start(seq, ppos, &ns->gid_map); | |
675 | } | |
676 | ||
f76d207a EB |
677 | static void *projid_m_start(struct seq_file *seq, loff_t *ppos) |
678 | { | |
679 | struct user_namespace *ns = seq->private; | |
680 | ||
681 | return m_start(seq, ppos, &ns->projid_map); | |
682 | } | |
683 | ||
22d917d8 EB |
684 | static void *m_next(struct seq_file *seq, void *v, loff_t *pos) |
685 | { | |
686 | (*pos)++; | |
687 | return seq->op->start(seq, pos); | |
688 | } | |
689 | ||
690 | static void m_stop(struct seq_file *seq, void *v) | |
691 | { | |
692 | return; | |
693 | } | |
694 | ||
ccf94f1b | 695 | const struct seq_operations proc_uid_seq_operations = { |
22d917d8 EB |
696 | .start = uid_m_start, |
697 | .stop = m_stop, | |
698 | .next = m_next, | |
699 | .show = uid_m_show, | |
700 | }; | |
701 | ||
ccf94f1b | 702 | const struct seq_operations proc_gid_seq_operations = { |
22d917d8 EB |
703 | .start = gid_m_start, |
704 | .stop = m_stop, | |
705 | .next = m_next, | |
706 | .show = gid_m_show, | |
707 | }; | |
708 | ||
ccf94f1b | 709 | const struct seq_operations proc_projid_seq_operations = { |
f76d207a EB |
710 | .start = projid_m_start, |
711 | .stop = m_stop, | |
712 | .next = m_next, | |
713 | .show = projid_m_show, | |
714 | }; | |
715 | ||
68a9a435 FF |
716 | static bool mappings_overlap(struct uid_gid_map *new_map, |
717 | struct uid_gid_extent *extent) | |
0bd14b4f EB |
718 | { |
719 | u32 upper_first, lower_first, upper_last, lower_last; | |
720 | unsigned idx; | |
721 | ||
722 | upper_first = extent->first; | |
723 | lower_first = extent->lower_first; | |
724 | upper_last = upper_first + extent->count - 1; | |
725 | lower_last = lower_first + extent->count - 1; | |
726 | ||
727 | for (idx = 0; idx < new_map->nr_extents; idx++) { | |
728 | u32 prev_upper_first, prev_lower_first; | |
729 | u32 prev_upper_last, prev_lower_last; | |
730 | struct uid_gid_extent *prev; | |
731 | ||
6397fac4 CB |
732 | if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) |
733 | prev = &new_map->extent[idx]; | |
734 | else | |
735 | prev = &new_map->forward[idx]; | |
0bd14b4f EB |
736 | |
737 | prev_upper_first = prev->first; | |
738 | prev_lower_first = prev->lower_first; | |
739 | prev_upper_last = prev_upper_first + prev->count - 1; | |
740 | prev_lower_last = prev_lower_first + prev->count - 1; | |
741 | ||
742 | /* Does the upper range intersect a previous extent? */ | |
743 | if ((prev_upper_first <= upper_last) && | |
744 | (prev_upper_last >= upper_first)) | |
745 | return true; | |
746 | ||
747 | /* Does the lower range intersect a previous extent? */ | |
748 | if ((prev_lower_first <= lower_last) && | |
749 | (prev_lower_last >= lower_first)) | |
750 | return true; | |
751 | } | |
752 | return false; | |
753 | } | |
754 | ||
6397fac4 CB |
755 | /** |
756 | * insert_extent - Safely insert a new idmap extent into struct uid_gid_map. | |
757 | * Takes care to allocate a 4K block of memory if the number of mappings exceeds | |
758 | * UID_GID_MAP_MAX_BASE_EXTENTS. | |
759 | */ | |
760 | static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent) | |
761 | { | |
3fda0e73 | 762 | struct uid_gid_extent *dest; |
6397fac4 CB |
763 | |
764 | if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) { | |
765 | struct uid_gid_extent *forward; | |
766 | ||
767 | /* Allocate memory for 340 mappings. */ | |
6da2ec56 KC |
768 | forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS, |
769 | sizeof(struct uid_gid_extent), | |
770 | GFP_KERNEL); | |
6397fac4 CB |
771 | if (!forward) |
772 | return -ENOMEM; | |
773 | ||
774 | /* Copy over memory. Only set up memory for the forward pointer. | |
775 | * Defer the memory setup for the reverse pointer. | |
776 | */ | |
777 | memcpy(forward, map->extent, | |
778 | map->nr_extents * sizeof(map->extent[0])); | |
779 | ||
780 | map->forward = forward; | |
781 | map->reverse = NULL; | |
782 | } | |
783 | ||
3fda0e73 EB |
784 | if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS) |
785 | dest = &map->extent[map->nr_extents]; | |
786 | else | |
787 | dest = &map->forward[map->nr_extents]; | |
788 | ||
789 | *dest = *extent; | |
790 | map->nr_extents++; | |
6397fac4 CB |
791 | return 0; |
792 | } | |
793 | ||
794 | /* cmp function to sort() forward mappings */ | |
795 | static int cmp_extents_forward(const void *a, const void *b) | |
796 | { | |
797 | const struct uid_gid_extent *e1 = a; | |
798 | const struct uid_gid_extent *e2 = b; | |
799 | ||
800 | if (e1->first < e2->first) | |
801 | return -1; | |
802 | ||
803 | if (e1->first > e2->first) | |
804 | return 1; | |
805 | ||
806 | return 0; | |
807 | } | |
808 | ||
809 | /* cmp function to sort() reverse mappings */ | |
810 | static int cmp_extents_reverse(const void *a, const void *b) | |
811 | { | |
812 | const struct uid_gid_extent *e1 = a; | |
813 | const struct uid_gid_extent *e2 = b; | |
814 | ||
815 | if (e1->lower_first < e2->lower_first) | |
816 | return -1; | |
817 | ||
818 | if (e1->lower_first > e2->lower_first) | |
819 | return 1; | |
820 | ||
821 | return 0; | |
822 | } | |
823 | ||
824 | /** | |
825 | * sort_idmaps - Sorts an array of idmap entries. | |
826 | * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS. | |
827 | */ | |
828 | static int sort_idmaps(struct uid_gid_map *map) | |
829 | { | |
830 | if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) | |
831 | return 0; | |
832 | ||
833 | /* Sort forward array. */ | |
834 | sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent), | |
835 | cmp_extents_forward, NULL); | |
836 | ||
837 | /* Only copy the memory from forward we actually need. */ | |
838 | map->reverse = kmemdup(map->forward, | |
839 | map->nr_extents * sizeof(struct uid_gid_extent), | |
840 | GFP_KERNEL); | |
841 | if (!map->reverse) | |
842 | return -ENOMEM; | |
843 | ||
844 | /* Sort reverse array. */ | |
845 | sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent), | |
846 | cmp_extents_reverse, NULL); | |
847 | ||
848 | return 0; | |
849 | } | |
850 | ||
22d917d8 EB |
851 | static ssize_t map_write(struct file *file, const char __user *buf, |
852 | size_t count, loff_t *ppos, | |
853 | int cap_setid, | |
854 | struct uid_gid_map *map, | |
855 | struct uid_gid_map *parent_map) | |
856 | { | |
857 | struct seq_file *seq = file->private_data; | |
858 | struct user_namespace *ns = seq->private; | |
859 | struct uid_gid_map new_map; | |
860 | unsigned idx; | |
6397fac4 | 861 | struct uid_gid_extent extent; |
70f6cbb6 | 862 | char *kbuf = NULL, *pos, *next_line; |
5820f140 JH |
863 | ssize_t ret; |
864 | ||
865 | /* Only allow < page size writes at the beginning of the file */ | |
866 | if ((*ppos != 0) || (count >= PAGE_SIZE)) | |
867 | return -EINVAL; | |
868 | ||
869 | /* Slurp in the user data */ | |
870 | kbuf = memdup_user_nul(buf, count); | |
871 | if (IS_ERR(kbuf)) | |
872 | return PTR_ERR(kbuf); | |
22d917d8 EB |
873 | |
874 | /* | |
f0d62aec | 875 | * The userns_state_mutex serializes all writes to any given map. |
22d917d8 EB |
876 | * |
877 | * Any map is only ever written once. | |
878 | * | |
879 | * An id map fits within 1 cache line on most architectures. | |
880 | * | |
881 | * On read nothing needs to be done unless you are on an | |
882 | * architecture with a crazy cache coherency model like alpha. | |
883 | * | |
884 | * There is a one time data dependency between reading the | |
885 | * count of the extents and the values of the extents. The | |
886 | * desired behavior is to see the values of the extents that | |
887 | * were written before the count of the extents. | |
888 | * | |
889 | * To achieve this smp_wmb() is used on guarantee the write | |
e79323bd MP |
890 | * order and smp_rmb() is guaranteed that we don't have crazy |
891 | * architectures returning stale data. | |
22d917d8 | 892 | */ |
f0d62aec | 893 | mutex_lock(&userns_state_mutex); |
22d917d8 | 894 | |
6397fac4 CB |
895 | memset(&new_map, 0, sizeof(struct uid_gid_map)); |
896 | ||
22d917d8 EB |
897 | ret = -EPERM; |
898 | /* Only allow one successful write to the map */ | |
899 | if (map->nr_extents != 0) | |
900 | goto out; | |
901 | ||
41c21e35 AL |
902 | /* |
903 | * Adjusting namespace settings requires capabilities on the target. | |
5c1469de | 904 | */ |
41c21e35 | 905 | if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN)) |
22d917d8 EB |
906 | goto out; |
907 | ||
22d917d8 EB |
908 | /* Parse the user data */ |
909 | ret = -EINVAL; | |
910 | pos = kbuf; | |
68a9a435 | 911 | for (; pos; pos = next_line) { |
22d917d8 EB |
912 | |
913 | /* Find the end of line and ensure I don't look past it */ | |
914 | next_line = strchr(pos, '\n'); | |
915 | if (next_line) { | |
916 | *next_line = '\0'; | |
917 | next_line++; | |
918 | if (*next_line == '\0') | |
919 | next_line = NULL; | |
5c1469de | 920 | } |
22d917d8 EB |
921 | |
922 | pos = skip_spaces(pos); | |
6397fac4 | 923 | extent.first = simple_strtoul(pos, &pos, 10); |
22d917d8 EB |
924 | if (!isspace(*pos)) |
925 | goto out; | |
926 | ||
927 | pos = skip_spaces(pos); | |
6397fac4 | 928 | extent.lower_first = simple_strtoul(pos, &pos, 10); |
22d917d8 EB |
929 | if (!isspace(*pos)) |
930 | goto out; | |
931 | ||
932 | pos = skip_spaces(pos); | |
6397fac4 | 933 | extent.count = simple_strtoul(pos, &pos, 10); |
22d917d8 EB |
934 | if (*pos && !isspace(*pos)) |
935 | goto out; | |
936 | ||
937 | /* Verify there is not trailing junk on the line */ | |
938 | pos = skip_spaces(pos); | |
939 | if (*pos != '\0') | |
940 | goto out; | |
941 | ||
942 | /* Verify we have been given valid starting values */ | |
6397fac4 CB |
943 | if ((extent.first == (u32) -1) || |
944 | (extent.lower_first == (u32) -1)) | |
22d917d8 EB |
945 | goto out; |
946 | ||
68a9a435 FF |
947 | /* Verify count is not zero and does not cause the |
948 | * extent to wrap | |
949 | */ | |
6397fac4 | 950 | if ((extent.first + extent.count) <= extent.first) |
22d917d8 | 951 | goto out; |
6397fac4 CB |
952 | if ((extent.lower_first + extent.count) <= |
953 | extent.lower_first) | |
22d917d8 EB |
954 | goto out; |
955 | ||
0bd14b4f | 956 | /* Do the ranges in extent overlap any previous extents? */ |
6397fac4 | 957 | if (mappings_overlap(&new_map, &extent)) |
22d917d8 EB |
958 | goto out; |
959 | ||
6397fac4 | 960 | if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS && |
22d917d8 EB |
961 | (next_line != NULL)) |
962 | goto out; | |
6397fac4 CB |
963 | |
964 | ret = insert_extent(&new_map, &extent); | |
965 | if (ret < 0) | |
966 | goto out; | |
967 | ret = -EINVAL; | |
5c1469de | 968 | } |
22d917d8 EB |
969 | /* Be very certaint the new map actually exists */ |
970 | if (new_map.nr_extents == 0) | |
971 | goto out; | |
972 | ||
973 | ret = -EPERM; | |
974 | /* Validate the user is allowed to use user id's mapped to. */ | |
6708075f | 975 | if (!new_idmap_permitted(file, ns, cap_setid, &new_map)) |
22d917d8 EB |
976 | goto out; |
977 | ||
6397fac4 | 978 | ret = -EPERM; |
22d917d8 EB |
979 | /* Map the lower ids from the parent user namespace to the |
980 | * kernel global id space. | |
981 | */ | |
982 | for (idx = 0; idx < new_map.nr_extents; idx++) { | |
6397fac4 | 983 | struct uid_gid_extent *e; |
22d917d8 | 984 | u32 lower_first; |
6397fac4 CB |
985 | |
986 | if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) | |
987 | e = &new_map.extent[idx]; | |
988 | else | |
989 | e = &new_map.forward[idx]; | |
22d917d8 EB |
990 | |
991 | lower_first = map_id_range_down(parent_map, | |
6397fac4 CB |
992 | e->lower_first, |
993 | e->count); | |
22d917d8 EB |
994 | |
995 | /* Fail if we can not map the specified extent to | |
996 | * the kernel global id space. | |
997 | */ | |
998 | if (lower_first == (u32) -1) | |
999 | goto out; | |
1000 | ||
6397fac4 | 1001 | e->lower_first = lower_first; |
22d917d8 EB |
1002 | } |
1003 | ||
d2f007db JH |
1004 | /* |
1005 | * If we want to use binary search for lookup, this clones the extent | |
1006 | * array and sorts both copies. | |
1007 | */ | |
1008 | ret = sort_idmaps(&new_map); | |
1009 | if (ret < 0) | |
1010 | goto out; | |
1011 | ||
22d917d8 | 1012 | /* Install the map */ |
6397fac4 CB |
1013 | if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) { |
1014 | memcpy(map->extent, new_map.extent, | |
1015 | new_map.nr_extents * sizeof(new_map.extent[0])); | |
1016 | } else { | |
1017 | map->forward = new_map.forward; | |
1018 | map->reverse = new_map.reverse; | |
1019 | } | |
22d917d8 EB |
1020 | smp_wmb(); |
1021 | map->nr_extents = new_map.nr_extents; | |
1022 | ||
1023 | *ppos = count; | |
1024 | ret = count; | |
1025 | out: | |
6397fac4 CB |
1026 | if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) { |
1027 | kfree(new_map.forward); | |
1028 | kfree(new_map.reverse); | |
1029 | map->forward = NULL; | |
1030 | map->reverse = NULL; | |
1031 | map->nr_extents = 0; | |
1032 | } | |
1033 | ||
f0d62aec | 1034 | mutex_unlock(&userns_state_mutex); |
70f6cbb6 | 1035 | kfree(kbuf); |
22d917d8 EB |
1036 | return ret; |
1037 | } | |
1038 | ||
68a9a435 FF |
1039 | ssize_t proc_uid_map_write(struct file *file, const char __user *buf, |
1040 | size_t size, loff_t *ppos) | |
22d917d8 EB |
1041 | { |
1042 | struct seq_file *seq = file->private_data; | |
1043 | struct user_namespace *ns = seq->private; | |
c450f371 | 1044 | struct user_namespace *seq_ns = seq_user_ns(seq); |
22d917d8 EB |
1045 | |
1046 | if (!ns->parent) | |
1047 | return -EPERM; | |
1048 | ||
c450f371 EB |
1049 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
1050 | return -EPERM; | |
1051 | ||
22d917d8 EB |
1052 | return map_write(file, buf, size, ppos, CAP_SETUID, |
1053 | &ns->uid_map, &ns->parent->uid_map); | |
1054 | } | |
1055 | ||
68a9a435 FF |
1056 | ssize_t proc_gid_map_write(struct file *file, const char __user *buf, |
1057 | size_t size, loff_t *ppos) | |
22d917d8 EB |
1058 | { |
1059 | struct seq_file *seq = file->private_data; | |
1060 | struct user_namespace *ns = seq->private; | |
c450f371 | 1061 | struct user_namespace *seq_ns = seq_user_ns(seq); |
22d917d8 EB |
1062 | |
1063 | if (!ns->parent) | |
1064 | return -EPERM; | |
1065 | ||
c450f371 EB |
1066 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
1067 | return -EPERM; | |
1068 | ||
22d917d8 EB |
1069 | return map_write(file, buf, size, ppos, CAP_SETGID, |
1070 | &ns->gid_map, &ns->parent->gid_map); | |
1071 | } | |
1072 | ||
68a9a435 FF |
1073 | ssize_t proc_projid_map_write(struct file *file, const char __user *buf, |
1074 | size_t size, loff_t *ppos) | |
f76d207a EB |
1075 | { |
1076 | struct seq_file *seq = file->private_data; | |
1077 | struct user_namespace *ns = seq->private; | |
1078 | struct user_namespace *seq_ns = seq_user_ns(seq); | |
1079 | ||
1080 | if (!ns->parent) | |
1081 | return -EPERM; | |
1082 | ||
1083 | if ((seq_ns != ns) && (seq_ns != ns->parent)) | |
1084 | return -EPERM; | |
1085 | ||
1086 | /* Anyone can set any valid project id no capability needed */ | |
1087 | return map_write(file, buf, size, ppos, -1, | |
1088 | &ns->projid_map, &ns->parent->projid_map); | |
1089 | } | |
1090 | ||
68a9a435 | 1091 | static bool new_idmap_permitted(const struct file *file, |
6708075f | 1092 | struct user_namespace *ns, int cap_setid, |
22d917d8 EB |
1093 | struct uid_gid_map *new_map) |
1094 | { | |
f95d7918 | 1095 | const struct cred *cred = file->f_cred; |
0542f17b EB |
1096 | /* Don't allow mappings that would allow anything that wouldn't |
1097 | * be allowed without the establishment of unprivileged mappings. | |
1098 | */ | |
f95d7918 EB |
1099 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) && |
1100 | uid_eq(ns->owner, cred->euid)) { | |
37657da3 EB |
1101 | u32 id = new_map->extent[0].lower_first; |
1102 | if (cap_setid == CAP_SETUID) { | |
1103 | kuid_t uid = make_kuid(ns->parent, id); | |
f95d7918 | 1104 | if (uid_eq(uid, cred->euid)) |
37657da3 | 1105 | return true; |
68a9a435 | 1106 | } else if (cap_setid == CAP_SETGID) { |
37657da3 | 1107 | kgid_t gid = make_kgid(ns->parent, id); |
66d2f338 EB |
1108 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) && |
1109 | gid_eq(gid, cred->egid)) | |
37657da3 EB |
1110 | return true; |
1111 | } | |
1112 | } | |
1113 | ||
f76d207a EB |
1114 | /* Allow anyone to set a mapping that doesn't require privilege */ |
1115 | if (!cap_valid(cap_setid)) | |
1116 | return true; | |
1117 | ||
22d917d8 EB |
1118 | /* Allow the specified ids if we have the appropriate capability |
1119 | * (CAP_SETUID or CAP_SETGID) over the parent user namespace. | |
6708075f | 1120 | * And the opener of the id file also had the approprpiate capability. |
22d917d8 | 1121 | */ |
6708075f EB |
1122 | if (ns_capable(ns->parent, cap_setid) && |
1123 | file_ns_capable(file, ns->parent, cap_setid)) | |
22d917d8 | 1124 | return true; |
5c1469de | 1125 | |
22d917d8 | 1126 | return false; |
5c1469de | 1127 | } |
6164281a | 1128 | |
9cc46516 EB |
1129 | int proc_setgroups_show(struct seq_file *seq, void *v) |
1130 | { | |
1131 | struct user_namespace *ns = seq->private; | |
6aa7de05 | 1132 | unsigned long userns_flags = READ_ONCE(ns->flags); |
9cc46516 EB |
1133 | |
1134 | seq_printf(seq, "%s\n", | |
1135 | (userns_flags & USERNS_SETGROUPS_ALLOWED) ? | |
1136 | "allow" : "deny"); | |
1137 | return 0; | |
1138 | } | |
1139 | ||
1140 | ssize_t proc_setgroups_write(struct file *file, const char __user *buf, | |
1141 | size_t count, loff_t *ppos) | |
1142 | { | |
1143 | struct seq_file *seq = file->private_data; | |
1144 | struct user_namespace *ns = seq->private; | |
1145 | char kbuf[8], *pos; | |
1146 | bool setgroups_allowed; | |
1147 | ssize_t ret; | |
1148 | ||
1149 | /* Only allow a very narrow range of strings to be written */ | |
1150 | ret = -EINVAL; | |
1151 | if ((*ppos != 0) || (count >= sizeof(kbuf))) | |
1152 | goto out; | |
1153 | ||
1154 | /* What was written? */ | |
1155 | ret = -EFAULT; | |
1156 | if (copy_from_user(kbuf, buf, count)) | |
1157 | goto out; | |
1158 | kbuf[count] = '\0'; | |
1159 | pos = kbuf; | |
1160 | ||
1161 | /* What is being requested? */ | |
1162 | ret = -EINVAL; | |
1163 | if (strncmp(pos, "allow", 5) == 0) { | |
1164 | pos += 5; | |
1165 | setgroups_allowed = true; | |
1166 | } | |
1167 | else if (strncmp(pos, "deny", 4) == 0) { | |
1168 | pos += 4; | |
1169 | setgroups_allowed = false; | |
1170 | } | |
1171 | else | |
1172 | goto out; | |
1173 | ||
1174 | /* Verify there is not trailing junk on the line */ | |
1175 | pos = skip_spaces(pos); | |
1176 | if (*pos != '\0') | |
1177 | goto out; | |
1178 | ||
1179 | ret = -EPERM; | |
1180 | mutex_lock(&userns_state_mutex); | |
1181 | if (setgroups_allowed) { | |
1182 | /* Enabling setgroups after setgroups has been disabled | |
1183 | * is not allowed. | |
1184 | */ | |
1185 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED)) | |
1186 | goto out_unlock; | |
1187 | } else { | |
1188 | /* Permanently disabling setgroups after setgroups has | |
1189 | * been enabled by writing the gid_map is not allowed. | |
1190 | */ | |
1191 | if (ns->gid_map.nr_extents != 0) | |
1192 | goto out_unlock; | |
1193 | ns->flags &= ~USERNS_SETGROUPS_ALLOWED; | |
1194 | } | |
1195 | mutex_unlock(&userns_state_mutex); | |
1196 | ||
1197 | /* Report a successful write */ | |
1198 | *ppos = count; | |
1199 | ret = count; | |
1200 | out: | |
1201 | return ret; | |
1202 | out_unlock: | |
1203 | mutex_unlock(&userns_state_mutex); | |
1204 | goto out; | |
1205 | } | |
1206 | ||
273d2c67 EB |
1207 | bool userns_may_setgroups(const struct user_namespace *ns) |
1208 | { | |
1209 | bool allowed; | |
1210 | ||
f0d62aec | 1211 | mutex_lock(&userns_state_mutex); |
273d2c67 EB |
1212 | /* It is not safe to use setgroups until a gid mapping in |
1213 | * the user namespace has been established. | |
1214 | */ | |
1215 | allowed = ns->gid_map.nr_extents != 0; | |
9cc46516 EB |
1216 | /* Is setgroups allowed? */ |
1217 | allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); | |
f0d62aec | 1218 | mutex_unlock(&userns_state_mutex); |
273d2c67 EB |
1219 | |
1220 | return allowed; | |
1221 | } | |
1222 | ||
d07b846f | 1223 | /* |
a2b42626 EB |
1224 | * Returns true if @child is the same namespace or a descendant of |
1225 | * @ancestor. | |
d07b846f | 1226 | */ |
a2b42626 EB |
1227 | bool in_userns(const struct user_namespace *ancestor, |
1228 | const struct user_namespace *child) | |
1229 | { | |
1230 | const struct user_namespace *ns; | |
1231 | for (ns = child; ns->level > ancestor->level; ns = ns->parent) | |
1232 | ; | |
1233 | return (ns == ancestor); | |
1234 | } | |
1235 | ||
d07b846f SF |
1236 | bool current_in_userns(const struct user_namespace *target_ns) |
1237 | { | |
a2b42626 | 1238 | return in_userns(target_ns, current_user_ns()); |
d07b846f | 1239 | } |
73f03c2b | 1240 | EXPORT_SYMBOL(current_in_userns); |
d07b846f | 1241 | |
3c041184 AV |
1242 | static inline struct user_namespace *to_user_ns(struct ns_common *ns) |
1243 | { | |
1244 | return container_of(ns, struct user_namespace, ns); | |
1245 | } | |
1246 | ||
64964528 | 1247 | static struct ns_common *userns_get(struct task_struct *task) |
cde1975b EB |
1248 | { |
1249 | struct user_namespace *user_ns; | |
1250 | ||
1251 | rcu_read_lock(); | |
1252 | user_ns = get_user_ns(__task_cred(task)->user_ns); | |
1253 | rcu_read_unlock(); | |
1254 | ||
3c041184 | 1255 | return user_ns ? &user_ns->ns : NULL; |
cde1975b EB |
1256 | } |
1257 | ||
64964528 | 1258 | static void userns_put(struct ns_common *ns) |
cde1975b | 1259 | { |
3c041184 | 1260 | put_user_ns(to_user_ns(ns)); |
cde1975b EB |
1261 | } |
1262 | ||
64964528 | 1263 | static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns) |
cde1975b | 1264 | { |
3c041184 | 1265 | struct user_namespace *user_ns = to_user_ns(ns); |
cde1975b EB |
1266 | struct cred *cred; |
1267 | ||
1268 | /* Don't allow gaining capabilities by reentering | |
1269 | * the same user namespace. | |
1270 | */ | |
1271 | if (user_ns == current_user_ns()) | |
1272 | return -EINVAL; | |
1273 | ||
faf00da5 EB |
1274 | /* Tasks that share a thread group must share a user namespace */ |
1275 | if (!thread_group_empty(current)) | |
cde1975b EB |
1276 | return -EINVAL; |
1277 | ||
e66eded8 EB |
1278 | if (current->fs->users != 1) |
1279 | return -EINVAL; | |
1280 | ||
cde1975b EB |
1281 | if (!ns_capable(user_ns, CAP_SYS_ADMIN)) |
1282 | return -EPERM; | |
1283 | ||
1284 | cred = prepare_creds(); | |
1285 | if (!cred) | |
1286 | return -ENOMEM; | |
1287 | ||
1288 | put_user_ns(cred->user_ns); | |
1289 | set_cred_user_ns(cred, get_user_ns(user_ns)); | |
1290 | ||
1291 | return commit_creds(cred); | |
1292 | } | |
1293 | ||
bcac25a5 AV |
1294 | struct ns_common *ns_get_owner(struct ns_common *ns) |
1295 | { | |
1296 | struct user_namespace *my_user_ns = current_user_ns(); | |
1297 | struct user_namespace *owner, *p; | |
1298 | ||
1299 | /* See if the owner is in the current user namespace */ | |
1300 | owner = p = ns->ops->owner(ns); | |
1301 | for (;;) { | |
1302 | if (!p) | |
1303 | return ERR_PTR(-EPERM); | |
1304 | if (p == my_user_ns) | |
1305 | break; | |
1306 | p = p->parent; | |
1307 | } | |
1308 | ||
1309 | return &get_user_ns(owner)->ns; | |
1310 | } | |
1311 | ||
1312 | static struct user_namespace *userns_owner(struct ns_common *ns) | |
1313 | { | |
1314 | return to_user_ns(ns)->parent; | |
1315 | } | |
1316 | ||
cde1975b EB |
1317 | const struct proc_ns_operations userns_operations = { |
1318 | .name = "user", | |
1319 | .type = CLONE_NEWUSER, | |
1320 | .get = userns_get, | |
1321 | .put = userns_put, | |
1322 | .install = userns_install, | |
bcac25a5 | 1323 | .owner = userns_owner, |
a7306ed8 | 1324 | .get_parent = ns_get_owner, |
cde1975b EB |
1325 | }; |
1326 | ||
6164281a PE |
1327 | static __init int user_namespaces_init(void) |
1328 | { | |
1329 | user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); | |
1330 | return 0; | |
1331 | } | |
c96d6660 | 1332 | subsys_initcall(user_namespaces_init); |