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>
24 static struct kmem_cache
*user_ns_cachep __read_mostly
;
25 static DEFINE_MUTEX(userns_state_mutex
);
27 static bool new_idmap_permitted(const struct file
*file
,
28 struct user_namespace
*ns
, int cap_setid
,
29 struct uid_gid_map
*map
);
30 static void free_user_ns(struct work_struct
*work
);
32 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
34 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
37 static void dec_user_namespaces(struct ucounts
*ucounts
)
39 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
42 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
44 /* Start with the same capabilities as init but useless for doing
45 * anything as the capabilities are bound to the new user namespace.
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
;
51 cred
->cap_ambient
= CAP_EMPTY_SET
;
52 cred
->cap_bset
= CAP_FULL_SET
;
54 key_put(cred
->request_key_auth
);
55 cred
->request_key_auth
= NULL
;
57 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
58 cred
->user_ns
= user_ns
;
61 static unsigned long enforced_nproc_rlimit(void)
63 unsigned long limit
= RLIM_INFINITY
;
65 /* Is RLIMIT_NPROC currently enforced? */
66 if (!uid_eq(current_uid(), GLOBAL_ROOT_UID
) ||
67 (current_user_ns() != &init_user_ns
))
68 limit
= rlimit(RLIMIT_NPROC
);
74 * Create a new user namespace, deriving the creator from the user in the
75 * passed credentials, and replacing that user with the new root user for the
78 * This is called by copy_creds(), which will finish setting the target task's
81 int create_user_ns(struct cred
*new)
83 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
84 kuid_t owner
= new->euid
;
85 kgid_t group
= new->egid
;
86 struct ucounts
*ucounts
;
90 if (parent_ns
->level
> 32)
93 ucounts
= inc_user_namespaces(parent_ns
, owner
);
98 * Verify that we can not violate the policy of which files
99 * may be accessed that is specified by the root directory,
100 * by verifying that the root directory is at the root of the
101 * mount namespace which allows all files to be accessed.
104 if (current_chrooted())
107 /* The creator needs a mapping in the parent user namespace
108 * or else we won't be able to reasonably tell userspace who
109 * created a user_namespace.
112 if (!kuid_has_mapping(parent_ns
, owner
) ||
113 !kgid_has_mapping(parent_ns
, group
))
117 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
121 ns
->parent_could_setfcap
= cap_raised(new->cap_effective
, CAP_SETFCAP
);
122 ret
= ns_alloc_inum(&ns
->ns
);
125 ns
->ns
.ops
= &userns_operations
;
127 refcount_set(&ns
->ns
.count
, 1);
128 /* Leave the new->user_ns reference with the new user namespace. */
129 ns
->parent
= parent_ns
;
130 ns
->level
= parent_ns
->level
+ 1;
133 INIT_WORK(&ns
->work
, free_user_ns
);
134 for (i
= 0; i
< MAX_PER_NAMESPACE_UCOUNTS
; i
++) {
135 ns
->ucount_max
[i
] = INT_MAX
;
137 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_NPROC
, enforced_nproc_rlimit());
138 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_MSGQUEUE
, rlimit(RLIMIT_MSGQUEUE
));
139 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_SIGPENDING
, rlimit(RLIMIT_SIGPENDING
));
140 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_MEMLOCK
, rlimit(RLIMIT_MEMLOCK
));
141 ns
->ucounts
= ucounts
;
143 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
144 mutex_lock(&userns_state_mutex
);
145 ns
->flags
= parent_ns
->flags
;
146 mutex_unlock(&userns_state_mutex
);
149 INIT_LIST_HEAD(&ns
->keyring_name_list
);
150 init_rwsem(&ns
->keyring_sem
);
153 if (!setup_userns_sysctls(ns
))
156 set_cred_user_ns(new, ns
);
159 #ifdef CONFIG_PERSISTENT_KEYRINGS
160 key_put(ns
->persistent_keyring_register
);
162 ns_free_inum(&ns
->ns
);
164 kmem_cache_free(user_ns_cachep
, ns
);
166 dec_user_namespaces(ucounts
);
171 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
176 if (!(unshare_flags
& CLONE_NEWUSER
))
179 cred
= prepare_creds();
181 err
= create_user_ns(cred
);
191 static void free_user_ns(struct work_struct
*work
)
193 struct user_namespace
*parent
, *ns
=
194 container_of(work
, struct user_namespace
, work
);
197 struct ucounts
*ucounts
= ns
->ucounts
;
199 if (ns
->gid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
200 kfree(ns
->gid_map
.forward
);
201 kfree(ns
->gid_map
.reverse
);
203 if (ns
->uid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
204 kfree(ns
->uid_map
.forward
);
205 kfree(ns
->uid_map
.reverse
);
207 if (ns
->projid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
208 kfree(ns
->projid_map
.forward
);
209 kfree(ns
->projid_map
.reverse
);
211 retire_userns_sysctls(ns
);
212 key_free_user_ns(ns
);
213 ns_free_inum(&ns
->ns
);
214 kmem_cache_free(user_ns_cachep
, ns
);
215 dec_user_namespaces(ucounts
);
217 } while (refcount_dec_and_test(&parent
->ns
.count
));
220 void __put_user_ns(struct user_namespace
*ns
)
222 schedule_work(&ns
->work
);
224 EXPORT_SYMBOL(__put_user_ns
);
227 * idmap_key struct holds the information necessary to find an idmapping in a
228 * sorted idmap array. It is passed to cmp_map_id() as first argument.
231 bool map_up
; /* true -> id from kid; false -> kid from id */
232 u32 id
; /* id to find */
233 u32 count
; /* == 0 unless used with map_id_range_down() */
237 * cmp_map_id - Function to be passed to bsearch() to find the requested
238 * idmapping. Expects struct idmap_key to be passed via @k.
240 static int cmp_map_id(const void *k
, const void *e
)
242 u32 first
, last
, id2
;
243 const struct idmap_key
*key
= k
;
244 const struct uid_gid_extent
*el
= e
;
246 id2
= key
->id
+ key
->count
- 1;
248 /* handle map_id_{down,up}() */
250 first
= el
->lower_first
;
254 last
= first
+ el
->count
- 1;
256 if (key
->id
>= first
&& key
->id
<= last
&&
257 (id2
>= first
&& id2
<= last
))
260 if (key
->id
< first
|| id2
< first
)
267 * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
268 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
270 static struct uid_gid_extent
*
271 map_id_range_down_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
273 struct idmap_key key
;
279 return bsearch(&key
, map
->forward
, extents
,
280 sizeof(struct uid_gid_extent
), cmp_map_id
);
284 * map_id_range_down_base - Find idmap via binary search in static extent array.
285 * Can only be called if number of mappings is equal or less than
286 * UID_GID_MAP_MAX_BASE_EXTENTS.
288 static struct uid_gid_extent
*
289 map_id_range_down_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
292 u32 first
, last
, id2
;
294 id2
= id
+ count
- 1;
296 /* Find the matching extent */
297 for (idx
= 0; idx
< extents
; idx
++) {
298 first
= map
->extent
[idx
].first
;
299 last
= first
+ map
->extent
[idx
].count
- 1;
300 if (id
>= first
&& id
<= last
&&
301 (id2
>= first
&& id2
<= last
))
302 return &map
->extent
[idx
];
307 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
309 struct uid_gid_extent
*extent
;
310 unsigned extents
= map
->nr_extents
;
313 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
314 extent
= map_id_range_down_base(extents
, map
, id
, count
);
316 extent
= map_id_range_down_max(extents
, map
, id
, count
);
318 /* Map the id or note failure */
320 id
= (id
- extent
->first
) + extent
->lower_first
;
327 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
329 return map_id_range_down(map
, id
, 1);
333 * map_id_up_base - Find idmap via binary search in static extent array.
334 * Can only be called if number of mappings is equal or less than
335 * UID_GID_MAP_MAX_BASE_EXTENTS.
337 static struct uid_gid_extent
*
338 map_id_up_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
343 /* Find the matching extent */
344 for (idx
= 0; idx
< extents
; idx
++) {
345 first
= map
->extent
[idx
].lower_first
;
346 last
= first
+ map
->extent
[idx
].count
- 1;
347 if (id
>= first
&& id
<= last
)
348 return &map
->extent
[idx
];
354 * map_id_up_max - Find idmap via binary search in ordered idmap array.
355 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
357 static struct uid_gid_extent
*
358 map_id_up_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
360 struct idmap_key key
;
366 return bsearch(&key
, map
->reverse
, extents
,
367 sizeof(struct uid_gid_extent
), cmp_map_id
);
370 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
372 struct uid_gid_extent
*extent
;
373 unsigned extents
= map
->nr_extents
;
376 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
377 extent
= map_id_up_base(extents
, map
, id
);
379 extent
= map_id_up_max(extents
, map
, id
);
381 /* Map the id or note failure */
383 id
= (id
- extent
->lower_first
) + extent
->first
;
391 * make_kuid - Map a user-namespace uid pair into a kuid.
392 * @ns: User namespace that the uid is in
393 * @uid: User identifier
395 * Maps a user-namespace uid pair into a kernel internal kuid,
396 * and returns that kuid.
398 * When there is no mapping defined for the user-namespace uid
399 * pair INVALID_UID is returned. Callers are expected to test
400 * for and handle INVALID_UID being returned. INVALID_UID
401 * may be tested for using uid_valid().
403 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
405 /* Map the uid to a global kernel uid */
406 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
408 EXPORT_SYMBOL(make_kuid
);
411 * from_kuid - Create a uid from a kuid user-namespace pair.
412 * @targ: The user namespace we want a uid in.
413 * @kuid: The kernel internal uid to start with.
415 * Map @kuid into the user-namespace specified by @targ and
416 * return the resulting uid.
418 * There is always a mapping into the initial user_namespace.
420 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
422 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
424 /* Map the uid from a global kernel uid */
425 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
427 EXPORT_SYMBOL(from_kuid
);
430 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
431 * @targ: The user namespace we want a uid in.
432 * @kuid: The kernel internal uid to start with.
434 * Map @kuid into the user-namespace specified by @targ and
435 * return the resulting uid.
437 * There is always a mapping into the initial user_namespace.
439 * Unlike from_kuid from_kuid_munged never fails and always
440 * returns a valid uid. This makes from_kuid_munged appropriate
441 * for use in syscalls like stat and getuid where failing the
442 * system call and failing to provide a valid uid are not an
445 * If @kuid has no mapping in @targ overflowuid is returned.
447 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
450 uid
= from_kuid(targ
, kuid
);
452 if (uid
== (uid_t
) -1)
456 EXPORT_SYMBOL(from_kuid_munged
);
459 * make_kgid - Map a user-namespace gid pair into a kgid.
460 * @ns: User namespace that the gid is in
461 * @gid: group identifier
463 * Maps a user-namespace gid pair into a kernel internal kgid,
464 * and returns that kgid.
466 * When there is no mapping defined for the user-namespace gid
467 * pair INVALID_GID is returned. Callers are expected to test
468 * for and handle INVALID_GID being returned. INVALID_GID may be
469 * tested for using gid_valid().
471 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
473 /* Map the gid to a global kernel gid */
474 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
476 EXPORT_SYMBOL(make_kgid
);
479 * from_kgid - Create a gid from a kgid user-namespace pair.
480 * @targ: The user namespace we want a gid in.
481 * @kgid: The kernel internal gid to start with.
483 * Map @kgid into the user-namespace specified by @targ and
484 * return the resulting gid.
486 * There is always a mapping into the initial user_namespace.
488 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
490 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
492 /* Map the gid from a global kernel gid */
493 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
495 EXPORT_SYMBOL(from_kgid
);
498 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
499 * @targ: The user namespace we want a gid in.
500 * @kgid: The kernel internal gid to start with.
502 * Map @kgid into the user-namespace specified by @targ and
503 * return the resulting gid.
505 * There is always a mapping into the initial user_namespace.
507 * Unlike from_kgid from_kgid_munged never fails and always
508 * returns a valid gid. This makes from_kgid_munged appropriate
509 * for use in syscalls like stat and getgid where failing the
510 * system call and failing to provide a valid gid are not options.
512 * If @kgid has no mapping in @targ overflowgid is returned.
514 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
517 gid
= from_kgid(targ
, kgid
);
519 if (gid
== (gid_t
) -1)
523 EXPORT_SYMBOL(from_kgid_munged
);
526 * make_kprojid - Map a user-namespace projid pair into a kprojid.
527 * @ns: User namespace that the projid is in
528 * @projid: Project identifier
530 * Maps a user-namespace uid pair into a kernel internal kuid,
531 * and returns that kuid.
533 * When there is no mapping defined for the user-namespace projid
534 * pair INVALID_PROJID is returned. Callers are expected to test
535 * for and handle INVALID_PROJID being returned. INVALID_PROJID
536 * may be tested for using projid_valid().
538 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
540 /* Map the uid to a global kernel uid */
541 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
543 EXPORT_SYMBOL(make_kprojid
);
546 * from_kprojid - Create a projid from a kprojid user-namespace pair.
547 * @targ: The user namespace we want a projid in.
548 * @kprojid: The kernel internal project identifier to start with.
550 * Map @kprojid into the user-namespace specified by @targ and
551 * return the resulting projid.
553 * There is always a mapping into the initial user_namespace.
555 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
557 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
559 /* Map the uid from a global kernel uid */
560 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
562 EXPORT_SYMBOL(from_kprojid
);
565 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
566 * @targ: The user namespace we want a projid in.
567 * @kprojid: The kernel internal projid to start with.
569 * Map @kprojid into the user-namespace specified by @targ and
570 * return the resulting projid.
572 * There is always a mapping into the initial user_namespace.
574 * Unlike from_kprojid from_kprojid_munged never fails and always
575 * returns a valid projid. This makes from_kprojid_munged
576 * appropriate for use in syscalls like stat and where
577 * failing the system call and failing to provide a valid projid are
580 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
582 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
585 projid
= from_kprojid(targ
, kprojid
);
587 if (projid
== (projid_t
) -1)
588 projid
= OVERFLOW_PROJID
;
591 EXPORT_SYMBOL(from_kprojid_munged
);
594 static int uid_m_show(struct seq_file
*seq
, void *v
)
596 struct user_namespace
*ns
= seq
->private;
597 struct uid_gid_extent
*extent
= v
;
598 struct user_namespace
*lower_ns
;
601 lower_ns
= seq_user_ns(seq
);
602 if ((lower_ns
== ns
) && lower_ns
->parent
)
603 lower_ns
= lower_ns
->parent
;
605 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
607 seq_printf(seq
, "%10u %10u %10u\n",
615 static int gid_m_show(struct seq_file
*seq
, void *v
)
617 struct user_namespace
*ns
= seq
->private;
618 struct uid_gid_extent
*extent
= v
;
619 struct user_namespace
*lower_ns
;
622 lower_ns
= seq_user_ns(seq
);
623 if ((lower_ns
== ns
) && lower_ns
->parent
)
624 lower_ns
= lower_ns
->parent
;
626 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
628 seq_printf(seq
, "%10u %10u %10u\n",
636 static int projid_m_show(struct seq_file
*seq
, void *v
)
638 struct user_namespace
*ns
= seq
->private;
639 struct uid_gid_extent
*extent
= v
;
640 struct user_namespace
*lower_ns
;
643 lower_ns
= seq_user_ns(seq
);
644 if ((lower_ns
== ns
) && lower_ns
->parent
)
645 lower_ns
= lower_ns
->parent
;
647 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
649 seq_printf(seq
, "%10u %10u %10u\n",
657 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
658 struct uid_gid_map
*map
)
661 unsigned extents
= map
->nr_extents
;
667 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
668 return &map
->extent
[pos
];
670 return &map
->forward
[pos
];
673 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
675 struct user_namespace
*ns
= seq
->private;
677 return m_start(seq
, ppos
, &ns
->uid_map
);
680 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
682 struct user_namespace
*ns
= seq
->private;
684 return m_start(seq
, ppos
, &ns
->gid_map
);
687 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
689 struct user_namespace
*ns
= seq
->private;
691 return m_start(seq
, ppos
, &ns
->projid_map
);
694 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
697 return seq
->op
->start(seq
, pos
);
700 static void m_stop(struct seq_file
*seq
, void *v
)
705 const struct seq_operations proc_uid_seq_operations
= {
706 .start
= uid_m_start
,
712 const struct seq_operations proc_gid_seq_operations
= {
713 .start
= gid_m_start
,
719 const struct seq_operations proc_projid_seq_operations
= {
720 .start
= projid_m_start
,
723 .show
= projid_m_show
,
726 static bool mappings_overlap(struct uid_gid_map
*new_map
,
727 struct uid_gid_extent
*extent
)
729 u32 upper_first
, lower_first
, upper_last
, lower_last
;
732 upper_first
= extent
->first
;
733 lower_first
= extent
->lower_first
;
734 upper_last
= upper_first
+ extent
->count
- 1;
735 lower_last
= lower_first
+ extent
->count
- 1;
737 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
738 u32 prev_upper_first
, prev_lower_first
;
739 u32 prev_upper_last
, prev_lower_last
;
740 struct uid_gid_extent
*prev
;
742 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
743 prev
= &new_map
->extent
[idx
];
745 prev
= &new_map
->forward
[idx
];
747 prev_upper_first
= prev
->first
;
748 prev_lower_first
= prev
->lower_first
;
749 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
750 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
752 /* Does the upper range intersect a previous extent? */
753 if ((prev_upper_first
<= upper_last
) &&
754 (prev_upper_last
>= upper_first
))
757 /* Does the lower range intersect a previous extent? */
758 if ((prev_lower_first
<= lower_last
) &&
759 (prev_lower_last
>= lower_first
))
766 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
767 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
768 * UID_GID_MAP_MAX_BASE_EXTENTS.
770 static int insert_extent(struct uid_gid_map
*map
, struct uid_gid_extent
*extent
)
772 struct uid_gid_extent
*dest
;
774 if (map
->nr_extents
== UID_GID_MAP_MAX_BASE_EXTENTS
) {
775 struct uid_gid_extent
*forward
;
777 /* Allocate memory for 340 mappings. */
778 forward
= kmalloc_array(UID_GID_MAP_MAX_EXTENTS
,
779 sizeof(struct uid_gid_extent
),
784 /* Copy over memory. Only set up memory for the forward pointer.
785 * Defer the memory setup for the reverse pointer.
787 memcpy(forward
, map
->extent
,
788 map
->nr_extents
* sizeof(map
->extent
[0]));
790 map
->forward
= forward
;
794 if (map
->nr_extents
< UID_GID_MAP_MAX_BASE_EXTENTS
)
795 dest
= &map
->extent
[map
->nr_extents
];
797 dest
= &map
->forward
[map
->nr_extents
];
804 /* cmp function to sort() forward mappings */
805 static int cmp_extents_forward(const void *a
, const void *b
)
807 const struct uid_gid_extent
*e1
= a
;
808 const struct uid_gid_extent
*e2
= b
;
810 if (e1
->first
< e2
->first
)
813 if (e1
->first
> e2
->first
)
819 /* cmp function to sort() reverse mappings */
820 static int cmp_extents_reverse(const void *a
, const void *b
)
822 const struct uid_gid_extent
*e1
= a
;
823 const struct uid_gid_extent
*e2
= b
;
825 if (e1
->lower_first
< e2
->lower_first
)
828 if (e1
->lower_first
> e2
->lower_first
)
835 * sort_idmaps - Sorts an array of idmap entries.
836 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
838 static int sort_idmaps(struct uid_gid_map
*map
)
840 if (map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
843 /* Sort forward array. */
844 sort(map
->forward
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
845 cmp_extents_forward
, NULL
);
847 /* Only copy the memory from forward we actually need. */
848 map
->reverse
= kmemdup(map
->forward
,
849 map
->nr_extents
* sizeof(struct uid_gid_extent
),
854 /* Sort reverse array. */
855 sort(map
->reverse
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
856 cmp_extents_reverse
, NULL
);
862 * verify_root_map() - check the uid 0 mapping
863 * @file: idmapping file
864 * @map_ns: user namespace of the target process
865 * @new_map: requested idmap
867 * If a process requests mapping parent uid 0 into the new ns, verify that the
868 * process writing the map had the CAP_SETFCAP capability as the target process
869 * will be able to write fscaps that are valid in ancestor user namespaces.
871 * Return: true if the mapping is allowed, false if not.
873 static bool verify_root_map(const struct file
*file
,
874 struct user_namespace
*map_ns
,
875 struct uid_gid_map
*new_map
)
878 const struct user_namespace
*file_ns
= file
->f_cred
->user_ns
;
879 struct uid_gid_extent
*extent0
= NULL
;
881 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
882 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
883 extent0
= &new_map
->extent
[idx
];
885 extent0
= &new_map
->forward
[idx
];
886 if (extent0
->lower_first
== 0)
895 if (map_ns
== file_ns
) {
896 /* The process unshared its ns and is writing to its own
897 * /proc/self/uid_map. User already has full capabilites in
898 * the new namespace. Verify that the parent had CAP_SETFCAP
901 if (!file_ns
->parent_could_setfcap
)
904 /* Process p1 is writing to uid_map of p2, who is in a child
905 * user namespace to p1's. Verify that the opener of the map
906 * file has CAP_SETFCAP against the parent of the new map
908 if (!file_ns_capable(file
, map_ns
->parent
, CAP_SETFCAP
))
915 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
916 size_t count
, loff_t
*ppos
,
918 struct uid_gid_map
*map
,
919 struct uid_gid_map
*parent_map
)
921 struct seq_file
*seq
= file
->private_data
;
922 struct user_namespace
*map_ns
= seq
->private;
923 struct uid_gid_map new_map
;
925 struct uid_gid_extent extent
;
926 char *kbuf
= NULL
, *pos
, *next_line
;
929 /* Only allow < page size writes at the beginning of the file */
930 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
933 /* Slurp in the user data */
934 kbuf
= memdup_user_nul(buf
, count
);
936 return PTR_ERR(kbuf
);
939 * The userns_state_mutex serializes all writes to any given map.
941 * Any map is only ever written once.
943 * An id map fits within 1 cache line on most architectures.
945 * On read nothing needs to be done unless you are on an
946 * architecture with a crazy cache coherency model like alpha.
948 * There is a one time data dependency between reading the
949 * count of the extents and the values of the extents. The
950 * desired behavior is to see the values of the extents that
951 * were written before the count of the extents.
953 * To achieve this smp_wmb() is used on guarantee the write
954 * order and smp_rmb() is guaranteed that we don't have crazy
955 * architectures returning stale data.
957 mutex_lock(&userns_state_mutex
);
959 memset(&new_map
, 0, sizeof(struct uid_gid_map
));
962 /* Only allow one successful write to the map */
963 if (map
->nr_extents
!= 0)
967 * Adjusting namespace settings requires capabilities on the target.
969 if (cap_valid(cap_setid
) && !file_ns_capable(file
, map_ns
, CAP_SYS_ADMIN
))
972 /* Parse the user data */
975 for (; pos
; pos
= next_line
) {
977 /* Find the end of line and ensure I don't look past it */
978 next_line
= strchr(pos
, '\n');
982 if (*next_line
== '\0')
986 pos
= skip_spaces(pos
);
987 extent
.first
= simple_strtoul(pos
, &pos
, 10);
991 pos
= skip_spaces(pos
);
992 extent
.lower_first
= simple_strtoul(pos
, &pos
, 10);
996 pos
= skip_spaces(pos
);
997 extent
.count
= simple_strtoul(pos
, &pos
, 10);
998 if (*pos
&& !isspace(*pos
))
1001 /* Verify there is not trailing junk on the line */
1002 pos
= skip_spaces(pos
);
1006 /* Verify we have been given valid starting values */
1007 if ((extent
.first
== (u32
) -1) ||
1008 (extent
.lower_first
== (u32
) -1))
1011 /* Verify count is not zero and does not cause the
1014 if ((extent
.first
+ extent
.count
) <= extent
.first
)
1016 if ((extent
.lower_first
+ extent
.count
) <=
1020 /* Do the ranges in extent overlap any previous extents? */
1021 if (mappings_overlap(&new_map
, &extent
))
1024 if ((new_map
.nr_extents
+ 1) == UID_GID_MAP_MAX_EXTENTS
&&
1025 (next_line
!= NULL
))
1028 ret
= insert_extent(&new_map
, &extent
);
1033 /* Be very certain the new map actually exists */
1034 if (new_map
.nr_extents
== 0)
1038 /* Validate the user is allowed to use user id's mapped to. */
1039 if (!new_idmap_permitted(file
, map_ns
, cap_setid
, &new_map
))
1043 /* Map the lower ids from the parent user namespace to the
1044 * kernel global id space.
1046 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
1047 struct uid_gid_extent
*e
;
1050 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
1051 e
= &new_map
.extent
[idx
];
1053 e
= &new_map
.forward
[idx
];
1055 lower_first
= map_id_range_down(parent_map
,
1059 /* Fail if we can not map the specified extent to
1060 * the kernel global id space.
1062 if (lower_first
== (u32
) -1)
1065 e
->lower_first
= lower_first
;
1069 * If we want to use binary search for lookup, this clones the extent
1070 * array and sorts both copies.
1072 ret
= sort_idmaps(&new_map
);
1076 /* Install the map */
1077 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
) {
1078 memcpy(map
->extent
, new_map
.extent
,
1079 new_map
.nr_extents
* sizeof(new_map
.extent
[0]));
1081 map
->forward
= new_map
.forward
;
1082 map
->reverse
= new_map
.reverse
;
1085 map
->nr_extents
= new_map
.nr_extents
;
1090 if (ret
< 0 && new_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
1091 kfree(new_map
.forward
);
1092 kfree(new_map
.reverse
);
1093 map
->forward
= NULL
;
1094 map
->reverse
= NULL
;
1095 map
->nr_extents
= 0;
1098 mutex_unlock(&userns_state_mutex
);
1103 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
1104 size_t size
, loff_t
*ppos
)
1106 struct seq_file
*seq
= file
->private_data
;
1107 struct user_namespace
*ns
= seq
->private;
1108 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1113 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1116 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
1117 &ns
->uid_map
, &ns
->parent
->uid_map
);
1120 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
1121 size_t size
, loff_t
*ppos
)
1123 struct seq_file
*seq
= file
->private_data
;
1124 struct user_namespace
*ns
= seq
->private;
1125 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1130 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1133 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
1134 &ns
->gid_map
, &ns
->parent
->gid_map
);
1137 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
1138 size_t size
, loff_t
*ppos
)
1140 struct seq_file
*seq
= file
->private_data
;
1141 struct user_namespace
*ns
= seq
->private;
1142 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1147 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1150 /* Anyone can set any valid project id no capability needed */
1151 return map_write(file
, buf
, size
, ppos
, -1,
1152 &ns
->projid_map
, &ns
->parent
->projid_map
);
1155 static bool new_idmap_permitted(const struct file
*file
,
1156 struct user_namespace
*ns
, int cap_setid
,
1157 struct uid_gid_map
*new_map
)
1159 const struct cred
*cred
= file
->f_cred
;
1161 if (cap_setid
== CAP_SETUID
&& !verify_root_map(file
, ns
, new_map
))
1164 /* Don't allow mappings that would allow anything that wouldn't
1165 * be allowed without the establishment of unprivileged mappings.
1167 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
1168 uid_eq(ns
->owner
, cred
->euid
)) {
1169 u32 id
= new_map
->extent
[0].lower_first
;
1170 if (cap_setid
== CAP_SETUID
) {
1171 kuid_t uid
= make_kuid(ns
->parent
, id
);
1172 if (uid_eq(uid
, cred
->euid
))
1174 } else if (cap_setid
== CAP_SETGID
) {
1175 kgid_t gid
= make_kgid(ns
->parent
, id
);
1176 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
1177 gid_eq(gid
, cred
->egid
))
1182 /* Allow anyone to set a mapping that doesn't require privilege */
1183 if (!cap_valid(cap_setid
))
1186 /* Allow the specified ids if we have the appropriate capability
1187 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
1188 * And the opener of the id file also has the appropriate capability.
1190 if (ns_capable(ns
->parent
, cap_setid
) &&
1191 file_ns_capable(file
, ns
->parent
, cap_setid
))
1197 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
1199 struct user_namespace
*ns
= seq
->private;
1200 unsigned long userns_flags
= READ_ONCE(ns
->flags
);
1202 seq_printf(seq
, "%s\n",
1203 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
1208 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
1209 size_t count
, loff_t
*ppos
)
1211 struct seq_file
*seq
= file
->private_data
;
1212 struct user_namespace
*ns
= seq
->private;
1214 bool setgroups_allowed
;
1217 /* Only allow a very narrow range of strings to be written */
1219 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
1222 /* What was written? */
1224 if (copy_from_user(kbuf
, buf
, count
))
1229 /* What is being requested? */
1231 if (strncmp(pos
, "allow", 5) == 0) {
1233 setgroups_allowed
= true;
1235 else if (strncmp(pos
, "deny", 4) == 0) {
1237 setgroups_allowed
= false;
1242 /* Verify there is not trailing junk on the line */
1243 pos
= skip_spaces(pos
);
1248 mutex_lock(&userns_state_mutex
);
1249 if (setgroups_allowed
) {
1250 /* Enabling setgroups after setgroups has been disabled
1253 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
1256 /* Permanently disabling setgroups after setgroups has
1257 * been enabled by writing the gid_map is not allowed.
1259 if (ns
->gid_map
.nr_extents
!= 0)
1261 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
1263 mutex_unlock(&userns_state_mutex
);
1265 /* Report a successful write */
1271 mutex_unlock(&userns_state_mutex
);
1275 bool userns_may_setgroups(const struct user_namespace
*ns
)
1279 mutex_lock(&userns_state_mutex
);
1280 /* It is not safe to use setgroups until a gid mapping in
1281 * the user namespace has been established.
1283 allowed
= ns
->gid_map
.nr_extents
!= 0;
1284 /* Is setgroups allowed? */
1285 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
1286 mutex_unlock(&userns_state_mutex
);
1292 * Returns true if @child is the same namespace or a descendant of
1295 bool in_userns(const struct user_namespace
*ancestor
,
1296 const struct user_namespace
*child
)
1298 const struct user_namespace
*ns
;
1299 for (ns
= child
; ns
->level
> ancestor
->level
; ns
= ns
->parent
)
1301 return (ns
== ancestor
);
1304 bool current_in_userns(const struct user_namespace
*target_ns
)
1306 return in_userns(target_ns
, current_user_ns());
1308 EXPORT_SYMBOL(current_in_userns
);
1310 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1312 return container_of(ns
, struct user_namespace
, ns
);
1315 static struct ns_common
*userns_get(struct task_struct
*task
)
1317 struct user_namespace
*user_ns
;
1320 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1323 return user_ns
? &user_ns
->ns
: NULL
;
1326 static void userns_put(struct ns_common
*ns
)
1328 put_user_ns(to_user_ns(ns
));
1331 static int userns_install(struct nsset
*nsset
, struct ns_common
*ns
)
1333 struct user_namespace
*user_ns
= to_user_ns(ns
);
1336 /* Don't allow gaining capabilities by reentering
1337 * the same user namespace.
1339 if (user_ns
== current_user_ns())
1342 /* Tasks that share a thread group must share a user namespace */
1343 if (!thread_group_empty(current
))
1346 if (current
->fs
->users
!= 1)
1349 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1352 cred
= nsset_cred(nsset
);
1356 put_user_ns(cred
->user_ns
);
1357 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1359 if (set_cred_ucounts(cred
) < 0)
1365 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1367 struct user_namespace
*my_user_ns
= current_user_ns();
1368 struct user_namespace
*owner
, *p
;
1370 /* See if the owner is in the current user namespace */
1371 owner
= p
= ns
->ops
->owner(ns
);
1374 return ERR_PTR(-EPERM
);
1375 if (p
== my_user_ns
)
1380 return &get_user_ns(owner
)->ns
;
1383 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1385 return to_user_ns(ns
)->parent
;
1388 const struct proc_ns_operations userns_operations
= {
1390 .type
= CLONE_NEWUSER
,
1393 .install
= userns_install
,
1394 .owner
= userns_owner
,
1395 .get_parent
= ns_get_owner
,
1398 static __init
int user_namespaces_init(void)
1400 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
| SLAB_ACCOUNT
);
1403 subsys_initcall(user_namespaces_init
);