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