1 // SPDX-License-Identifier: GPL-2.0-only
3 #include <linux/export.h>
4 #include <linux/nsproxy.h>
5 #include <linux/slab.h>
6 #include <linux/sched/signal.h>
7 #include <linux/user_namespace.h>
8 #include <linux/proc_ns.h>
9 #include <linux/highuid.h>
10 #include <linux/cred.h>
11 #include <linux/securebits.h>
12 #include <linux/keyctl.h>
13 #include <linux/key-type.h>
14 #include <keys/user-type.h>
15 #include <linux/seq_file.h>
17 #include <linux/uaccess.h>
18 #include <linux/ctype.h>
19 #include <linux/projid.h>
20 #include <linux/fs_struct.h>
21 #include <linux/bsearch.h>
22 #include <linux/sort.h>
25 * sysctl determining whether unprivileged users may unshare a new
26 * userns. Allowed by default
28 int unprivileged_userns_clone
= 1;
30 static struct kmem_cache
*user_ns_cachep __read_mostly
;
31 static DEFINE_MUTEX(userns_state_mutex
);
33 static bool new_idmap_permitted(const struct file
*file
,
34 struct user_namespace
*ns
, int cap_setid
,
35 struct uid_gid_map
*map
);
36 static void free_user_ns(struct work_struct
*work
);
38 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
40 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
43 static void dec_user_namespaces(struct ucounts
*ucounts
)
45 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
48 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
50 /* Start with the same capabilities as init but useless for doing
51 * anything as the capabilities are bound to the new user namespace.
53 cred
->securebits
= SECUREBITS_DEFAULT
;
54 cred
->cap_inheritable
= CAP_EMPTY_SET
;
55 cred
->cap_permitted
= CAP_FULL_SET
;
56 cred
->cap_effective
= CAP_FULL_SET
;
57 cred
->cap_ambient
= CAP_EMPTY_SET
;
58 cred
->cap_bset
= CAP_FULL_SET
;
60 key_put(cred
->request_key_auth
);
61 cred
->request_key_auth
= NULL
;
63 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
64 cred
->user_ns
= user_ns
;
68 * Create a new user namespace, deriving the creator from the user in the
69 * passed credentials, and replacing that user with the new root user for the
72 * This is called by copy_creds(), which will finish setting the target task's
75 int create_user_ns(struct cred
*new)
77 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
78 kuid_t owner
= new->euid
;
79 kgid_t group
= new->egid
;
80 struct ucounts
*ucounts
;
84 if (parent_ns
->level
> 32)
87 ucounts
= inc_user_namespaces(parent_ns
, owner
);
92 * Verify that we can not violate the policy of which files
93 * may be accessed that is specified by the root directory,
94 * by verifing that the root directory is at the root of the
95 * mount namespace which allows all files to be accessed.
98 if (current_chrooted())
101 /* The creator needs a mapping in the parent user namespace
102 * or else we won't be able to reasonably tell userspace who
103 * created a user_namespace.
106 if (!kuid_has_mapping(parent_ns
, owner
) ||
107 !kgid_has_mapping(parent_ns
, group
))
111 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
115 ns
->parent_could_setfcap
= cap_raised(new->cap_effective
, CAP_SETFCAP
);
116 ret
= ns_alloc_inum(&ns
->ns
);
119 ns
->ns
.ops
= &userns_operations
;
121 refcount_set(&ns
->ns
.count
, 1);
122 /* Leave the new->user_ns reference with the new user namespace. */
123 ns
->parent
= parent_ns
;
124 ns
->level
= parent_ns
->level
+ 1;
127 INIT_WORK(&ns
->work
, free_user_ns
);
128 for (i
= 0; i
< UCOUNT_COUNTS
; i
++) {
129 ns
->ucount_max
[i
] = INT_MAX
;
131 ns
->ucounts
= ucounts
;
133 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
134 mutex_lock(&userns_state_mutex
);
135 ns
->flags
= parent_ns
->flags
;
136 mutex_unlock(&userns_state_mutex
);
139 INIT_LIST_HEAD(&ns
->keyring_name_list
);
140 init_rwsem(&ns
->keyring_sem
);
143 if (!setup_userns_sysctls(ns
))
146 set_cred_user_ns(new, ns
);
149 #ifdef CONFIG_PERSISTENT_KEYRINGS
150 key_put(ns
->persistent_keyring_register
);
152 ns_free_inum(&ns
->ns
);
154 kmem_cache_free(user_ns_cachep
, ns
);
156 dec_user_namespaces(ucounts
);
161 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
166 if (!(unshare_flags
& CLONE_NEWUSER
))
169 cred
= prepare_creds();
171 err
= create_user_ns(cred
);
181 static void free_user_ns(struct work_struct
*work
)
183 struct user_namespace
*parent
, *ns
=
184 container_of(work
, struct user_namespace
, work
);
187 struct ucounts
*ucounts
= ns
->ucounts
;
189 if (ns
->gid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
190 kfree(ns
->gid_map
.forward
);
191 kfree(ns
->gid_map
.reverse
);
193 if (ns
->uid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
194 kfree(ns
->uid_map
.forward
);
195 kfree(ns
->uid_map
.reverse
);
197 if (ns
->projid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
198 kfree(ns
->projid_map
.forward
);
199 kfree(ns
->projid_map
.reverse
);
201 retire_userns_sysctls(ns
);
202 key_free_user_ns(ns
);
203 ns_free_inum(&ns
->ns
);
204 kmem_cache_free(user_ns_cachep
, ns
);
205 dec_user_namespaces(ucounts
);
207 } while (refcount_dec_and_test(&parent
->ns
.count
));
210 void __put_user_ns(struct user_namespace
*ns
)
212 schedule_work(&ns
->work
);
214 EXPORT_SYMBOL(__put_user_ns
);
217 * idmap_key struct holds the information necessary to find an idmapping in a
218 * sorted idmap array. It is passed to cmp_map_id() as first argument.
221 bool map_up
; /* true -> id from kid; false -> kid from id */
222 u32 id
; /* id to find */
223 u32 count
; /* == 0 unless used with map_id_range_down() */
227 * cmp_map_id - Function to be passed to bsearch() to find the requested
228 * idmapping. Expects struct idmap_key to be passed via @k.
230 static int cmp_map_id(const void *k
, const void *e
)
232 u32 first
, last
, id2
;
233 const struct idmap_key
*key
= k
;
234 const struct uid_gid_extent
*el
= e
;
236 id2
= key
->id
+ key
->count
- 1;
238 /* handle map_id_{down,up}() */
240 first
= el
->lower_first
;
244 last
= first
+ el
->count
- 1;
246 if (key
->id
>= first
&& key
->id
<= last
&&
247 (id2
>= first
&& id2
<= last
))
250 if (key
->id
< first
|| id2
< first
)
257 * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
258 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
260 static struct uid_gid_extent
*
261 map_id_range_down_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
263 struct idmap_key key
;
269 return bsearch(&key
, map
->forward
, extents
,
270 sizeof(struct uid_gid_extent
), cmp_map_id
);
274 * map_id_range_down_base - Find idmap via binary search in static extent array.
275 * Can only be called if number of mappings is equal or less than
276 * UID_GID_MAP_MAX_BASE_EXTENTS.
278 static struct uid_gid_extent
*
279 map_id_range_down_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
282 u32 first
, last
, id2
;
284 id2
= id
+ count
- 1;
286 /* Find the matching extent */
287 for (idx
= 0; idx
< extents
; idx
++) {
288 first
= map
->extent
[idx
].first
;
289 last
= first
+ map
->extent
[idx
].count
- 1;
290 if (id
>= first
&& id
<= last
&&
291 (id2
>= first
&& id2
<= last
))
292 return &map
->extent
[idx
];
297 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
299 struct uid_gid_extent
*extent
;
300 unsigned extents
= map
->nr_extents
;
303 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
304 extent
= map_id_range_down_base(extents
, map
, id
, count
);
306 extent
= map_id_range_down_max(extents
, map
, id
, count
);
308 /* Map the id or note failure */
310 id
= (id
- extent
->first
) + extent
->lower_first
;
317 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
319 return map_id_range_down(map
, id
, 1);
323 * map_id_up_base - Find idmap via binary search in static extent array.
324 * Can only be called if number of mappings is equal or less than
325 * UID_GID_MAP_MAX_BASE_EXTENTS.
327 static struct uid_gid_extent
*
328 map_id_up_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
333 /* Find the matching extent */
334 for (idx
= 0; idx
< extents
; idx
++) {
335 first
= map
->extent
[idx
].lower_first
;
336 last
= first
+ map
->extent
[idx
].count
- 1;
337 if (id
>= first
&& id
<= last
)
338 return &map
->extent
[idx
];
344 * map_id_up_max - Find idmap via binary search in ordered idmap array.
345 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
347 static struct uid_gid_extent
*
348 map_id_up_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
350 struct idmap_key key
;
356 return bsearch(&key
, map
->reverse
, extents
,
357 sizeof(struct uid_gid_extent
), cmp_map_id
);
360 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
362 struct uid_gid_extent
*extent
;
363 unsigned extents
= map
->nr_extents
;
366 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
367 extent
= map_id_up_base(extents
, map
, id
);
369 extent
= map_id_up_max(extents
, map
, id
);
371 /* Map the id or note failure */
373 id
= (id
- extent
->lower_first
) + extent
->first
;
381 * make_kuid - Map a user-namespace uid pair into a kuid.
382 * @ns: User namespace that the uid is in
383 * @uid: User identifier
385 * Maps a user-namespace uid pair into a kernel internal kuid,
386 * and returns that kuid.
388 * When there is no mapping defined for the user-namespace uid
389 * pair INVALID_UID is returned. Callers are expected to test
390 * for and handle INVALID_UID being returned. INVALID_UID
391 * may be tested for using uid_valid().
393 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
395 /* Map the uid to a global kernel uid */
396 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
398 EXPORT_SYMBOL(make_kuid
);
401 * from_kuid - Create a uid from a kuid user-namespace pair.
402 * @targ: The user namespace we want a uid in.
403 * @kuid: The kernel internal uid to start with.
405 * Map @kuid into the user-namespace specified by @targ and
406 * return the resulting uid.
408 * There is always a mapping into the initial user_namespace.
410 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
412 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
414 /* Map the uid from a global kernel uid */
415 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
417 EXPORT_SYMBOL(from_kuid
);
420 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
421 * @targ: The user namespace we want a uid in.
422 * @kuid: The kernel internal uid to start with.
424 * Map @kuid into the user-namespace specified by @targ and
425 * return the resulting uid.
427 * There is always a mapping into the initial user_namespace.
429 * Unlike from_kuid from_kuid_munged never fails and always
430 * returns a valid uid. This makes from_kuid_munged appropriate
431 * for use in syscalls like stat and getuid where failing the
432 * system call and failing to provide a valid uid are not an
435 * If @kuid has no mapping in @targ overflowuid is returned.
437 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
440 uid
= from_kuid(targ
, kuid
);
442 if (uid
== (uid_t
) -1)
446 EXPORT_SYMBOL(from_kuid_munged
);
449 * make_kgid - Map a user-namespace gid pair into a kgid.
450 * @ns: User namespace that the gid is in
451 * @gid: group identifier
453 * Maps a user-namespace gid pair into a kernel internal kgid,
454 * and returns that kgid.
456 * When there is no mapping defined for the user-namespace gid
457 * pair INVALID_GID is returned. Callers are expected to test
458 * for and handle INVALID_GID being returned. INVALID_GID may be
459 * tested for using gid_valid().
461 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
463 /* Map the gid to a global kernel gid */
464 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
466 EXPORT_SYMBOL(make_kgid
);
469 * from_kgid - Create a gid from a kgid user-namespace pair.
470 * @targ: The user namespace we want a gid in.
471 * @kgid: The kernel internal gid to start with.
473 * Map @kgid into the user-namespace specified by @targ and
474 * return the resulting gid.
476 * There is always a mapping into the initial user_namespace.
478 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
480 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
482 /* Map the gid from a global kernel gid */
483 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
485 EXPORT_SYMBOL(from_kgid
);
488 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
489 * @targ: The user namespace we want a gid in.
490 * @kgid: The kernel internal gid to start with.
492 * Map @kgid into the user-namespace specified by @targ and
493 * return the resulting gid.
495 * There is always a mapping into the initial user_namespace.
497 * Unlike from_kgid from_kgid_munged never fails and always
498 * returns a valid gid. This makes from_kgid_munged appropriate
499 * for use in syscalls like stat and getgid where failing the
500 * system call and failing to provide a valid gid are not options.
502 * If @kgid has no mapping in @targ overflowgid is returned.
504 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
507 gid
= from_kgid(targ
, kgid
);
509 if (gid
== (gid_t
) -1)
513 EXPORT_SYMBOL(from_kgid_munged
);
516 * make_kprojid - Map a user-namespace projid pair into a kprojid.
517 * @ns: User namespace that the projid is in
518 * @projid: Project identifier
520 * Maps a user-namespace uid pair into a kernel internal kuid,
521 * and returns that kuid.
523 * When there is no mapping defined for the user-namespace projid
524 * pair INVALID_PROJID is returned. Callers are expected to test
525 * for and handle INVALID_PROJID being returned. INVALID_PROJID
526 * may be tested for using projid_valid().
528 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
530 /* Map the uid to a global kernel uid */
531 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
533 EXPORT_SYMBOL(make_kprojid
);
536 * from_kprojid - Create a projid from a kprojid user-namespace pair.
537 * @targ: The user namespace we want a projid in.
538 * @kprojid: The kernel internal project identifier to start with.
540 * Map @kprojid into the user-namespace specified by @targ and
541 * return the resulting projid.
543 * There is always a mapping into the initial user_namespace.
545 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
547 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
549 /* Map the uid from a global kernel uid */
550 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
552 EXPORT_SYMBOL(from_kprojid
);
555 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
556 * @targ: The user namespace we want a projid in.
557 * @kprojid: The kernel internal projid to start with.
559 * Map @kprojid into the user-namespace specified by @targ and
560 * return the resulting projid.
562 * There is always a mapping into the initial user_namespace.
564 * Unlike from_kprojid from_kprojid_munged never fails and always
565 * returns a valid projid. This makes from_kprojid_munged
566 * appropriate for use in syscalls like stat and where
567 * failing the system call and failing to provide a valid projid are
570 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
572 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
575 projid
= from_kprojid(targ
, kprojid
);
577 if (projid
== (projid_t
) -1)
578 projid
= OVERFLOW_PROJID
;
581 EXPORT_SYMBOL(from_kprojid_munged
);
584 static int uid_m_show(struct seq_file
*seq
, void *v
)
586 struct user_namespace
*ns
= seq
->private;
587 struct uid_gid_extent
*extent
= v
;
588 struct user_namespace
*lower_ns
;
591 lower_ns
= seq_user_ns(seq
);
592 if ((lower_ns
== ns
) && lower_ns
->parent
)
593 lower_ns
= lower_ns
->parent
;
595 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
597 seq_printf(seq
, "%10u %10u %10u\n",
605 static int gid_m_show(struct seq_file
*seq
, void *v
)
607 struct user_namespace
*ns
= seq
->private;
608 struct uid_gid_extent
*extent
= v
;
609 struct user_namespace
*lower_ns
;
612 lower_ns
= seq_user_ns(seq
);
613 if ((lower_ns
== ns
) && lower_ns
->parent
)
614 lower_ns
= lower_ns
->parent
;
616 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
618 seq_printf(seq
, "%10u %10u %10u\n",
626 static int projid_m_show(struct seq_file
*seq
, void *v
)
628 struct user_namespace
*ns
= seq
->private;
629 struct uid_gid_extent
*extent
= v
;
630 struct user_namespace
*lower_ns
;
633 lower_ns
= seq_user_ns(seq
);
634 if ((lower_ns
== ns
) && lower_ns
->parent
)
635 lower_ns
= lower_ns
->parent
;
637 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
639 seq_printf(seq
, "%10u %10u %10u\n",
647 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
648 struct uid_gid_map
*map
)
651 unsigned extents
= map
->nr_extents
;
657 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
658 return &map
->extent
[pos
];
660 return &map
->forward
[pos
];
663 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
665 struct user_namespace
*ns
= seq
->private;
667 return m_start(seq
, ppos
, &ns
->uid_map
);
670 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
672 struct user_namespace
*ns
= seq
->private;
674 return m_start(seq
, ppos
, &ns
->gid_map
);
677 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
679 struct user_namespace
*ns
= seq
->private;
681 return m_start(seq
, ppos
, &ns
->projid_map
);
684 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
687 return seq
->op
->start(seq
, pos
);
690 static void m_stop(struct seq_file
*seq
, void *v
)
695 const struct seq_operations proc_uid_seq_operations
= {
696 .start
= uid_m_start
,
702 const struct seq_operations proc_gid_seq_operations
= {
703 .start
= gid_m_start
,
709 const struct seq_operations proc_projid_seq_operations
= {
710 .start
= projid_m_start
,
713 .show
= projid_m_show
,
716 static bool mappings_overlap(struct uid_gid_map
*new_map
,
717 struct uid_gid_extent
*extent
)
719 u32 upper_first
, lower_first
, upper_last
, lower_last
;
722 upper_first
= extent
->first
;
723 lower_first
= extent
->lower_first
;
724 upper_last
= upper_first
+ extent
->count
- 1;
725 lower_last
= lower_first
+ extent
->count
- 1;
727 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
728 u32 prev_upper_first
, prev_lower_first
;
729 u32 prev_upper_last
, prev_lower_last
;
730 struct uid_gid_extent
*prev
;
732 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
733 prev
= &new_map
->extent
[idx
];
735 prev
= &new_map
->forward
[idx
];
737 prev_upper_first
= prev
->first
;
738 prev_lower_first
= prev
->lower_first
;
739 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
740 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
742 /* Does the upper range intersect a previous extent? */
743 if ((prev_upper_first
<= upper_last
) &&
744 (prev_upper_last
>= upper_first
))
747 /* Does the lower range intersect a previous extent? */
748 if ((prev_lower_first
<= lower_last
) &&
749 (prev_lower_last
>= lower_first
))
756 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
757 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
758 * UID_GID_MAP_MAX_BASE_EXTENTS.
760 static int insert_extent(struct uid_gid_map
*map
, struct uid_gid_extent
*extent
)
762 struct uid_gid_extent
*dest
;
764 if (map
->nr_extents
== UID_GID_MAP_MAX_BASE_EXTENTS
) {
765 struct uid_gid_extent
*forward
;
767 /* Allocate memory for 340 mappings. */
768 forward
= kmalloc_array(UID_GID_MAP_MAX_EXTENTS
,
769 sizeof(struct uid_gid_extent
),
774 /* Copy over memory. Only set up memory for the forward pointer.
775 * Defer the memory setup for the reverse pointer.
777 memcpy(forward
, map
->extent
,
778 map
->nr_extents
* sizeof(map
->extent
[0]));
780 map
->forward
= forward
;
784 if (map
->nr_extents
< UID_GID_MAP_MAX_BASE_EXTENTS
)
785 dest
= &map
->extent
[map
->nr_extents
];
787 dest
= &map
->forward
[map
->nr_extents
];
794 /* cmp function to sort() forward mappings */
795 static int cmp_extents_forward(const void *a
, const void *b
)
797 const struct uid_gid_extent
*e1
= a
;
798 const struct uid_gid_extent
*e2
= b
;
800 if (e1
->first
< e2
->first
)
803 if (e1
->first
> e2
->first
)
809 /* cmp function to sort() reverse mappings */
810 static int cmp_extents_reverse(const void *a
, const void *b
)
812 const struct uid_gid_extent
*e1
= a
;
813 const struct uid_gid_extent
*e2
= b
;
815 if (e1
->lower_first
< e2
->lower_first
)
818 if (e1
->lower_first
> e2
->lower_first
)
825 * sort_idmaps - Sorts an array of idmap entries.
826 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
828 static int sort_idmaps(struct uid_gid_map
*map
)
830 if (map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
833 /* Sort forward array. */
834 sort(map
->forward
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
835 cmp_extents_forward
, NULL
);
837 /* Only copy the memory from forward we actually need. */
838 map
->reverse
= kmemdup(map
->forward
,
839 map
->nr_extents
* sizeof(struct uid_gid_extent
),
844 /* Sort reverse array. */
845 sort(map
->reverse
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
846 cmp_extents_reverse
, NULL
);
852 * verify_root_map() - check the uid 0 mapping
853 * @file: idmapping file
854 * @map_ns: user namespace of the target process
855 * @new_map: requested idmap
857 * If a process requests mapping parent uid 0 into the new ns, verify that the
858 * process writing the map had the CAP_SETFCAP capability as the target process
859 * will be able to write fscaps that are valid in ancestor user namespaces.
861 * Return: true if the mapping is allowed, false if not.
863 static bool verify_root_map(const struct file
*file
,
864 struct user_namespace
*map_ns
,
865 struct uid_gid_map
*new_map
)
868 const struct user_namespace
*file_ns
= file
->f_cred
->user_ns
;
869 struct uid_gid_extent
*extent0
= NULL
;
871 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
872 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
873 extent0
= &new_map
->extent
[idx
];
875 extent0
= &new_map
->forward
[idx
];
876 if (extent0
->lower_first
== 0)
885 if (map_ns
== file_ns
) {
886 /* The process unshared its ns and is writing to its own
887 * /proc/self/uid_map. User already has full capabilites in
888 * the new namespace. Verify that the parent had CAP_SETFCAP
891 if (!file_ns
->parent_could_setfcap
)
894 /* Process p1 is writing to uid_map of p2, who is in a child
895 * user namespace to p1's. Verify that the opener of the map
896 * file has CAP_SETFCAP against the parent of the new map
898 if (!file_ns_capable(file
, map_ns
->parent
, CAP_SETFCAP
))
905 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
906 size_t count
, loff_t
*ppos
,
908 struct uid_gid_map
*map
,
909 struct uid_gid_map
*parent_map
)
911 struct seq_file
*seq
= file
->private_data
;
912 struct user_namespace
*map_ns
= seq
->private;
913 struct uid_gid_map new_map
;
915 struct uid_gid_extent extent
;
916 char *kbuf
= NULL
, *pos
, *next_line
;
919 /* Only allow < page size writes at the beginning of the file */
920 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
923 /* Slurp in the user data */
924 kbuf
= memdup_user_nul(buf
, count
);
926 return PTR_ERR(kbuf
);
929 * The userns_state_mutex serializes all writes to any given map.
931 * Any map is only ever written once.
933 * An id map fits within 1 cache line on most architectures.
935 * On read nothing needs to be done unless you are on an
936 * architecture with a crazy cache coherency model like alpha.
938 * There is a one time data dependency between reading the
939 * count of the extents and the values of the extents. The
940 * desired behavior is to see the values of the extents that
941 * were written before the count of the extents.
943 * To achieve this smp_wmb() is used on guarantee the write
944 * order and smp_rmb() is guaranteed that we don't have crazy
945 * architectures returning stale data.
947 mutex_lock(&userns_state_mutex
);
949 memset(&new_map
, 0, sizeof(struct uid_gid_map
));
952 /* Only allow one successful write to the map */
953 if (map
->nr_extents
!= 0)
957 * Adjusting namespace settings requires capabilities on the target.
959 if (cap_valid(cap_setid
) && !file_ns_capable(file
, map_ns
, CAP_SYS_ADMIN
))
962 /* Parse the user data */
965 for (; pos
; pos
= next_line
) {
967 /* Find the end of line and ensure I don't look past it */
968 next_line
= strchr(pos
, '\n');
972 if (*next_line
== '\0')
976 pos
= skip_spaces(pos
);
977 extent
.first
= simple_strtoul(pos
, &pos
, 10);
981 pos
= skip_spaces(pos
);
982 extent
.lower_first
= simple_strtoul(pos
, &pos
, 10);
986 pos
= skip_spaces(pos
);
987 extent
.count
= simple_strtoul(pos
, &pos
, 10);
988 if (*pos
&& !isspace(*pos
))
991 /* Verify there is not trailing junk on the line */
992 pos
= skip_spaces(pos
);
996 /* Verify we have been given valid starting values */
997 if ((extent
.first
== (u32
) -1) ||
998 (extent
.lower_first
== (u32
) -1))
1001 /* Verify count is not zero and does not cause the
1004 if ((extent
.first
+ extent
.count
) <= extent
.first
)
1006 if ((extent
.lower_first
+ extent
.count
) <=
1010 /* Do the ranges in extent overlap any previous extents? */
1011 if (mappings_overlap(&new_map
, &extent
))
1014 if ((new_map
.nr_extents
+ 1) == UID_GID_MAP_MAX_EXTENTS
&&
1015 (next_line
!= NULL
))
1018 ret
= insert_extent(&new_map
, &extent
);
1023 /* Be very certaint the new map actually exists */
1024 if (new_map
.nr_extents
== 0)
1028 /* Validate the user is allowed to use user id's mapped to. */
1029 if (!new_idmap_permitted(file
, map_ns
, cap_setid
, &new_map
))
1033 /* Map the lower ids from the parent user namespace to the
1034 * kernel global id space.
1036 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
1037 struct uid_gid_extent
*e
;
1040 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
1041 e
= &new_map
.extent
[idx
];
1043 e
= &new_map
.forward
[idx
];
1045 lower_first
= map_id_range_down(parent_map
,
1049 /* Fail if we can not map the specified extent to
1050 * the kernel global id space.
1052 if (lower_first
== (u32
) -1)
1055 e
->lower_first
= lower_first
;
1059 * If we want to use binary search for lookup, this clones the extent
1060 * array and sorts both copies.
1062 ret
= sort_idmaps(&new_map
);
1066 /* Install the map */
1067 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
) {
1068 memcpy(map
->extent
, new_map
.extent
,
1069 new_map
.nr_extents
* sizeof(new_map
.extent
[0]));
1071 map
->forward
= new_map
.forward
;
1072 map
->reverse
= new_map
.reverse
;
1075 map
->nr_extents
= new_map
.nr_extents
;
1080 if (ret
< 0 && new_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
1081 kfree(new_map
.forward
);
1082 kfree(new_map
.reverse
);
1083 map
->forward
= NULL
;
1084 map
->reverse
= NULL
;
1085 map
->nr_extents
= 0;
1088 mutex_unlock(&userns_state_mutex
);
1093 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
1094 size_t size
, loff_t
*ppos
)
1096 struct seq_file
*seq
= file
->private_data
;
1097 struct user_namespace
*ns
= seq
->private;
1098 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1103 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1106 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
1107 &ns
->uid_map
, &ns
->parent
->uid_map
);
1110 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
1111 size_t size
, loff_t
*ppos
)
1113 struct seq_file
*seq
= file
->private_data
;
1114 struct user_namespace
*ns
= seq
->private;
1115 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1120 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1123 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
1124 &ns
->gid_map
, &ns
->parent
->gid_map
);
1127 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
1128 size_t size
, loff_t
*ppos
)
1130 struct seq_file
*seq
= file
->private_data
;
1131 struct user_namespace
*ns
= seq
->private;
1132 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1137 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1140 /* Anyone can set any valid project id no capability needed */
1141 return map_write(file
, buf
, size
, ppos
, -1,
1142 &ns
->projid_map
, &ns
->parent
->projid_map
);
1145 static bool new_idmap_permitted(const struct file
*file
,
1146 struct user_namespace
*ns
, int cap_setid
,
1147 struct uid_gid_map
*new_map
)
1149 const struct cred
*cred
= file
->f_cred
;
1151 if (cap_setid
== CAP_SETUID
&& !verify_root_map(file
, ns
, new_map
))
1154 /* Don't allow mappings that would allow anything that wouldn't
1155 * be allowed without the establishment of unprivileged mappings.
1157 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
1158 uid_eq(ns
->owner
, cred
->euid
)) {
1159 u32 id
= new_map
->extent
[0].lower_first
;
1160 if (cap_setid
== CAP_SETUID
) {
1161 kuid_t uid
= make_kuid(ns
->parent
, id
);
1162 if (uid_eq(uid
, cred
->euid
))
1164 } else if (cap_setid
== CAP_SETGID
) {
1165 kgid_t gid
= make_kgid(ns
->parent
, id
);
1166 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
1167 gid_eq(gid
, cred
->egid
))
1172 /* Allow anyone to set a mapping that doesn't require privilege */
1173 if (!cap_valid(cap_setid
))
1176 /* Allow the specified ids if we have the appropriate capability
1177 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
1178 * And the opener of the id file also had the approprpiate capability.
1180 if (ns_capable(ns
->parent
, cap_setid
) &&
1181 file_ns_capable(file
, ns
->parent
, cap_setid
))
1187 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
1189 struct user_namespace
*ns
= seq
->private;
1190 unsigned long userns_flags
= READ_ONCE(ns
->flags
);
1192 seq_printf(seq
, "%s\n",
1193 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
1198 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
1199 size_t count
, loff_t
*ppos
)
1201 struct seq_file
*seq
= file
->private_data
;
1202 struct user_namespace
*ns
= seq
->private;
1204 bool setgroups_allowed
;
1207 /* Only allow a very narrow range of strings to be written */
1209 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
1212 /* What was written? */
1214 if (copy_from_user(kbuf
, buf
, count
))
1219 /* What is being requested? */
1221 if (strncmp(pos
, "allow", 5) == 0) {
1223 setgroups_allowed
= true;
1225 else if (strncmp(pos
, "deny", 4) == 0) {
1227 setgroups_allowed
= false;
1232 /* Verify there is not trailing junk on the line */
1233 pos
= skip_spaces(pos
);
1238 mutex_lock(&userns_state_mutex
);
1239 if (setgroups_allowed
) {
1240 /* Enabling setgroups after setgroups has been disabled
1243 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
1246 /* Permanently disabling setgroups after setgroups has
1247 * been enabled by writing the gid_map is not allowed.
1249 if (ns
->gid_map
.nr_extents
!= 0)
1251 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
1253 mutex_unlock(&userns_state_mutex
);
1255 /* Report a successful write */
1261 mutex_unlock(&userns_state_mutex
);
1265 bool userns_may_setgroups(const struct user_namespace
*ns
)
1269 mutex_lock(&userns_state_mutex
);
1270 /* It is not safe to use setgroups until a gid mapping in
1271 * the user namespace has been established.
1273 allowed
= ns
->gid_map
.nr_extents
!= 0;
1274 /* Is setgroups allowed? */
1275 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
1276 mutex_unlock(&userns_state_mutex
);
1282 * Returns true if @child is the same namespace or a descendant of
1285 bool in_userns(const struct user_namespace
*ancestor
,
1286 const struct user_namespace
*child
)
1288 const struct user_namespace
*ns
;
1289 for (ns
= child
; ns
->level
> ancestor
->level
; ns
= ns
->parent
)
1291 return (ns
== ancestor
);
1294 bool current_in_userns(const struct user_namespace
*target_ns
)
1296 return in_userns(target_ns
, current_user_ns());
1298 EXPORT_SYMBOL(current_in_userns
);
1300 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1302 return container_of(ns
, struct user_namespace
, ns
);
1305 static struct ns_common
*userns_get(struct task_struct
*task
)
1307 struct user_namespace
*user_ns
;
1310 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1313 return user_ns
? &user_ns
->ns
: NULL
;
1316 static void userns_put(struct ns_common
*ns
)
1318 put_user_ns(to_user_ns(ns
));
1321 static int userns_install(struct nsset
*nsset
, struct ns_common
*ns
)
1323 struct user_namespace
*user_ns
= to_user_ns(ns
);
1326 /* Don't allow gaining capabilities by reentering
1327 * the same user namespace.
1329 if (user_ns
== current_user_ns())
1332 /* Tasks that share a thread group must share a user namespace */
1333 if (!thread_group_empty(current
))
1336 if (current
->fs
->users
!= 1)
1339 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1342 cred
= nsset_cred(nsset
);
1346 put_user_ns(cred
->user_ns
);
1347 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1352 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1354 struct user_namespace
*my_user_ns
= current_user_ns();
1355 struct user_namespace
*owner
, *p
;
1357 /* See if the owner is in the current user namespace */
1358 owner
= p
= ns
->ops
->owner(ns
);
1361 return ERR_PTR(-EPERM
);
1362 if (p
== my_user_ns
)
1367 return &get_user_ns(owner
)->ns
;
1370 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1372 return to_user_ns(ns
)->parent
;
1375 const struct proc_ns_operations userns_operations
= {
1377 .type
= CLONE_NEWUSER
,
1380 .install
= userns_install
,
1381 .owner
= userns_owner
,
1382 .get_parent
= ns_get_owner
,
1385 static __init
int user_namespaces_init(void)
1387 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1390 subsys_initcall(user_namespaces_init
);