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