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