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
;
67 static unsigned long enforced_nproc_rlimit(void)
69 unsigned long limit
= RLIM_INFINITY
;
71 /* Is RLIMIT_NPROC currently enforced? */
72 if (!uid_eq(current_uid(), GLOBAL_ROOT_UID
) ||
73 (current_user_ns() != &init_user_ns
))
74 limit
= rlimit(RLIMIT_NPROC
);
80 * Create a new user namespace, deriving the creator from the user in the
81 * passed credentials, and replacing that user with the new root user for the
84 * This is called by copy_creds(), which will finish setting the target task's
87 int create_user_ns(struct cred
*new)
89 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
90 kuid_t owner
= new->euid
;
91 kgid_t group
= new->egid
;
92 struct ucounts
*ucounts
;
96 if (parent_ns
->level
> 32)
99 ucounts
= inc_user_namespaces(parent_ns
, owner
);
104 * Verify that we can not violate the policy of which files
105 * may be accessed that is specified by the root directory,
106 * by verifying that the root directory is at the root of the
107 * mount namespace which allows all files to be accessed.
110 if (current_chrooted())
113 /* The creator needs a mapping in the parent user namespace
114 * or else we won't be able to reasonably tell userspace who
115 * created a user_namespace.
118 if (!kuid_has_mapping(parent_ns
, owner
) ||
119 !kgid_has_mapping(parent_ns
, group
))
123 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
127 ns
->parent_could_setfcap
= cap_raised(new->cap_effective
, CAP_SETFCAP
);
128 ret
= ns_alloc_inum(&ns
->ns
);
131 ns
->ns
.ops
= &userns_operations
;
133 refcount_set(&ns
->ns
.count
, 1);
134 /* Leave the new->user_ns reference with the new user namespace. */
135 ns
->parent
= parent_ns
;
136 ns
->level
= parent_ns
->level
+ 1;
139 INIT_WORK(&ns
->work
, free_user_ns
);
140 for (i
= 0; i
< MAX_PER_NAMESPACE_UCOUNTS
; i
++) {
141 ns
->ucount_max
[i
] = INT_MAX
;
143 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_NPROC
, enforced_nproc_rlimit());
144 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_MSGQUEUE
, rlimit(RLIMIT_MSGQUEUE
));
145 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_SIGPENDING
, rlimit(RLIMIT_SIGPENDING
));
146 set_rlimit_ucount_max(ns
, UCOUNT_RLIMIT_MEMLOCK
, rlimit(RLIMIT_MEMLOCK
));
147 ns
->ucounts
= ucounts
;
149 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
150 mutex_lock(&userns_state_mutex
);
151 ns
->flags
= parent_ns
->flags
;
152 mutex_unlock(&userns_state_mutex
);
155 INIT_LIST_HEAD(&ns
->keyring_name_list
);
156 init_rwsem(&ns
->keyring_sem
);
159 if (!setup_userns_sysctls(ns
))
162 set_cred_user_ns(new, ns
);
165 #ifdef CONFIG_PERSISTENT_KEYRINGS
166 key_put(ns
->persistent_keyring_register
);
168 ns_free_inum(&ns
->ns
);
170 kmem_cache_free(user_ns_cachep
, ns
);
172 dec_user_namespaces(ucounts
);
177 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
182 if (!(unshare_flags
& CLONE_NEWUSER
))
185 cred
= prepare_creds();
187 err
= create_user_ns(cred
);
197 static void free_user_ns(struct work_struct
*work
)
199 struct user_namespace
*parent
, *ns
=
200 container_of(work
, struct user_namespace
, work
);
203 struct ucounts
*ucounts
= ns
->ucounts
;
205 if (ns
->gid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
206 kfree(ns
->gid_map
.forward
);
207 kfree(ns
->gid_map
.reverse
);
209 if (ns
->uid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
210 kfree(ns
->uid_map
.forward
);
211 kfree(ns
->uid_map
.reverse
);
213 if (ns
->projid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
214 kfree(ns
->projid_map
.forward
);
215 kfree(ns
->projid_map
.reverse
);
217 retire_userns_sysctls(ns
);
218 key_free_user_ns(ns
);
219 ns_free_inum(&ns
->ns
);
220 kmem_cache_free(user_ns_cachep
, ns
);
221 dec_user_namespaces(ucounts
);
223 } while (refcount_dec_and_test(&parent
->ns
.count
));
226 void __put_user_ns(struct user_namespace
*ns
)
228 schedule_work(&ns
->work
);
230 EXPORT_SYMBOL(__put_user_ns
);
233 * idmap_key struct holds the information necessary to find an idmapping in a
234 * sorted idmap array. It is passed to cmp_map_id() as first argument.
237 bool map_up
; /* true -> id from kid; false -> kid from id */
238 u32 id
; /* id to find */
239 u32 count
; /* == 0 unless used with map_id_range_down() */
243 * cmp_map_id - Function to be passed to bsearch() to find the requested
244 * idmapping. Expects struct idmap_key to be passed via @k.
246 static int cmp_map_id(const void *k
, const void *e
)
248 u32 first
, last
, id2
;
249 const struct idmap_key
*key
= k
;
250 const struct uid_gid_extent
*el
= e
;
252 id2
= key
->id
+ key
->count
- 1;
254 /* handle map_id_{down,up}() */
256 first
= el
->lower_first
;
260 last
= first
+ el
->count
- 1;
262 if (key
->id
>= first
&& key
->id
<= last
&&
263 (id2
>= first
&& id2
<= last
))
266 if (key
->id
< first
|| id2
< first
)
273 * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
274 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
276 static struct uid_gid_extent
*
277 map_id_range_down_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
279 struct idmap_key key
;
285 return bsearch(&key
, map
->forward
, extents
,
286 sizeof(struct uid_gid_extent
), cmp_map_id
);
290 * map_id_range_down_base - Find idmap via binary search in static extent array.
291 * Can only be called if number of mappings is equal or less than
292 * UID_GID_MAP_MAX_BASE_EXTENTS.
294 static struct uid_gid_extent
*
295 map_id_range_down_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
298 u32 first
, last
, id2
;
300 id2
= id
+ count
- 1;
302 /* Find the matching extent */
303 for (idx
= 0; idx
< extents
; idx
++) {
304 first
= map
->extent
[idx
].first
;
305 last
= first
+ map
->extent
[idx
].count
- 1;
306 if (id
>= first
&& id
<= last
&&
307 (id2
>= first
&& id2
<= last
))
308 return &map
->extent
[idx
];
313 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
315 struct uid_gid_extent
*extent
;
316 unsigned extents
= map
->nr_extents
;
319 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
320 extent
= map_id_range_down_base(extents
, map
, id
, count
);
322 extent
= map_id_range_down_max(extents
, map
, id
, count
);
324 /* Map the id or note failure */
326 id
= (id
- extent
->first
) + extent
->lower_first
;
333 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
335 return map_id_range_down(map
, id
, 1);
339 * map_id_up_base - Find idmap via binary search in static extent array.
340 * Can only be called if number of mappings is equal or less than
341 * UID_GID_MAP_MAX_BASE_EXTENTS.
343 static struct uid_gid_extent
*
344 map_id_up_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
349 /* Find the matching extent */
350 for (idx
= 0; idx
< extents
; idx
++) {
351 first
= map
->extent
[idx
].lower_first
;
352 last
= first
+ map
->extent
[idx
].count
- 1;
353 if (id
>= first
&& id
<= last
)
354 return &map
->extent
[idx
];
360 * map_id_up_max - Find idmap via binary search in ordered idmap array.
361 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
363 static struct uid_gid_extent
*
364 map_id_up_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
366 struct idmap_key key
;
372 return bsearch(&key
, map
->reverse
, extents
,
373 sizeof(struct uid_gid_extent
), cmp_map_id
);
376 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
378 struct uid_gid_extent
*extent
;
379 unsigned extents
= map
->nr_extents
;
382 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
383 extent
= map_id_up_base(extents
, map
, id
);
385 extent
= map_id_up_max(extents
, map
, id
);
387 /* Map the id or note failure */
389 id
= (id
- extent
->lower_first
) + extent
->first
;
397 * make_kuid - Map a user-namespace uid pair into a kuid.
398 * @ns: User namespace that the uid is in
399 * @uid: User identifier
401 * Maps a user-namespace uid pair into a kernel internal kuid,
402 * and returns that kuid.
404 * When there is no mapping defined for the user-namespace uid
405 * pair INVALID_UID is returned. Callers are expected to test
406 * for and handle INVALID_UID being returned. INVALID_UID
407 * may be tested for using uid_valid().
409 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
411 /* Map the uid to a global kernel uid */
412 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
414 EXPORT_SYMBOL(make_kuid
);
417 * from_kuid - Create a uid from a kuid user-namespace pair.
418 * @targ: The user namespace we want a uid in.
419 * @kuid: The kernel internal uid to start with.
421 * Map @kuid into the user-namespace specified by @targ and
422 * return the resulting uid.
424 * There is always a mapping into the initial user_namespace.
426 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
428 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
430 /* Map the uid from a global kernel uid */
431 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
433 EXPORT_SYMBOL(from_kuid
);
436 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
437 * @targ: The user namespace we want a uid in.
438 * @kuid: The kernel internal uid to start with.
440 * Map @kuid into the user-namespace specified by @targ and
441 * return the resulting uid.
443 * There is always a mapping into the initial user_namespace.
445 * Unlike from_kuid from_kuid_munged never fails and always
446 * returns a valid uid. This makes from_kuid_munged appropriate
447 * for use in syscalls like stat and getuid where failing the
448 * system call and failing to provide a valid uid are not an
451 * If @kuid has no mapping in @targ overflowuid is returned.
453 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
456 uid
= from_kuid(targ
, kuid
);
458 if (uid
== (uid_t
) -1)
462 EXPORT_SYMBOL(from_kuid_munged
);
465 * make_kgid - Map a user-namespace gid pair into a kgid.
466 * @ns: User namespace that the gid is in
467 * @gid: group identifier
469 * Maps a user-namespace gid pair into a kernel internal kgid,
470 * and returns that kgid.
472 * When there is no mapping defined for the user-namespace gid
473 * pair INVALID_GID is returned. Callers are expected to test
474 * for and handle INVALID_GID being returned. INVALID_GID may be
475 * tested for using gid_valid().
477 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
479 /* Map the gid to a global kernel gid */
480 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
482 EXPORT_SYMBOL(make_kgid
);
485 * from_kgid - Create a gid from a kgid user-namespace pair.
486 * @targ: The user namespace we want a gid in.
487 * @kgid: The kernel internal gid to start with.
489 * Map @kgid into the user-namespace specified by @targ and
490 * return the resulting gid.
492 * There is always a mapping into the initial user_namespace.
494 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
496 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
498 /* Map the gid from a global kernel gid */
499 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
501 EXPORT_SYMBOL(from_kgid
);
504 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
505 * @targ: The user namespace we want a gid in.
506 * @kgid: The kernel internal gid to start with.
508 * Map @kgid into the user-namespace specified by @targ and
509 * return the resulting gid.
511 * There is always a mapping into the initial user_namespace.
513 * Unlike from_kgid from_kgid_munged never fails and always
514 * returns a valid gid. This makes from_kgid_munged appropriate
515 * for use in syscalls like stat and getgid where failing the
516 * system call and failing to provide a valid gid are not options.
518 * If @kgid has no mapping in @targ overflowgid is returned.
520 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
523 gid
= from_kgid(targ
, kgid
);
525 if (gid
== (gid_t
) -1)
529 EXPORT_SYMBOL(from_kgid_munged
);
532 * make_kprojid - Map a user-namespace projid pair into a kprojid.
533 * @ns: User namespace that the projid is in
534 * @projid: Project identifier
536 * Maps a user-namespace uid pair into a kernel internal kuid,
537 * and returns that kuid.
539 * When there is no mapping defined for the user-namespace projid
540 * pair INVALID_PROJID is returned. Callers are expected to test
541 * for and handle INVALID_PROJID being returned. INVALID_PROJID
542 * may be tested for using projid_valid().
544 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
546 /* Map the uid to a global kernel uid */
547 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
549 EXPORT_SYMBOL(make_kprojid
);
552 * from_kprojid - Create a projid from a kprojid user-namespace pair.
553 * @targ: The user namespace we want a projid in.
554 * @kprojid: The kernel internal project identifier to start with.
556 * Map @kprojid into the user-namespace specified by @targ and
557 * return the resulting projid.
559 * There is always a mapping into the initial user_namespace.
561 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
563 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
565 /* Map the uid from a global kernel uid */
566 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
568 EXPORT_SYMBOL(from_kprojid
);
571 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
572 * @targ: The user namespace we want a projid in.
573 * @kprojid: The kernel internal projid to start with.
575 * Map @kprojid into the user-namespace specified by @targ and
576 * return the resulting projid.
578 * There is always a mapping into the initial user_namespace.
580 * Unlike from_kprojid from_kprojid_munged never fails and always
581 * returns a valid projid. This makes from_kprojid_munged
582 * appropriate for use in syscalls like stat and where
583 * failing the system call and failing to provide a valid projid are
586 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
588 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
591 projid
= from_kprojid(targ
, kprojid
);
593 if (projid
== (projid_t
) -1)
594 projid
= OVERFLOW_PROJID
;
597 EXPORT_SYMBOL(from_kprojid_munged
);
600 static int uid_m_show(struct seq_file
*seq
, void *v
)
602 struct user_namespace
*ns
= seq
->private;
603 struct uid_gid_extent
*extent
= v
;
604 struct user_namespace
*lower_ns
;
607 lower_ns
= seq_user_ns(seq
);
608 if ((lower_ns
== ns
) && lower_ns
->parent
)
609 lower_ns
= lower_ns
->parent
;
611 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
613 seq_printf(seq
, "%10u %10u %10u\n",
621 static int gid_m_show(struct seq_file
*seq
, void *v
)
623 struct user_namespace
*ns
= seq
->private;
624 struct uid_gid_extent
*extent
= v
;
625 struct user_namespace
*lower_ns
;
628 lower_ns
= seq_user_ns(seq
);
629 if ((lower_ns
== ns
) && lower_ns
->parent
)
630 lower_ns
= lower_ns
->parent
;
632 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
634 seq_printf(seq
, "%10u %10u %10u\n",
642 static int projid_m_show(struct seq_file
*seq
, void *v
)
644 struct user_namespace
*ns
= seq
->private;
645 struct uid_gid_extent
*extent
= v
;
646 struct user_namespace
*lower_ns
;
649 lower_ns
= seq_user_ns(seq
);
650 if ((lower_ns
== ns
) && lower_ns
->parent
)
651 lower_ns
= lower_ns
->parent
;
653 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
655 seq_printf(seq
, "%10u %10u %10u\n",
663 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
664 struct uid_gid_map
*map
)
667 unsigned extents
= map
->nr_extents
;
673 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
674 return &map
->extent
[pos
];
676 return &map
->forward
[pos
];
679 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
681 struct user_namespace
*ns
= seq
->private;
683 return m_start(seq
, ppos
, &ns
->uid_map
);
686 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
688 struct user_namespace
*ns
= seq
->private;
690 return m_start(seq
, ppos
, &ns
->gid_map
);
693 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
695 struct user_namespace
*ns
= seq
->private;
697 return m_start(seq
, ppos
, &ns
->projid_map
);
700 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
703 return seq
->op
->start(seq
, pos
);
706 static void m_stop(struct seq_file
*seq
, void *v
)
711 const struct seq_operations proc_uid_seq_operations
= {
712 .start
= uid_m_start
,
718 const struct seq_operations proc_gid_seq_operations
= {
719 .start
= gid_m_start
,
725 const struct seq_operations proc_projid_seq_operations
= {
726 .start
= projid_m_start
,
729 .show
= projid_m_show
,
732 static bool mappings_overlap(struct uid_gid_map
*new_map
,
733 struct uid_gid_extent
*extent
)
735 u32 upper_first
, lower_first
, upper_last
, lower_last
;
738 upper_first
= extent
->first
;
739 lower_first
= extent
->lower_first
;
740 upper_last
= upper_first
+ extent
->count
- 1;
741 lower_last
= lower_first
+ extent
->count
- 1;
743 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
744 u32 prev_upper_first
, prev_lower_first
;
745 u32 prev_upper_last
, prev_lower_last
;
746 struct uid_gid_extent
*prev
;
748 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
749 prev
= &new_map
->extent
[idx
];
751 prev
= &new_map
->forward
[idx
];
753 prev_upper_first
= prev
->first
;
754 prev_lower_first
= prev
->lower_first
;
755 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
756 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
758 /* Does the upper range intersect a previous extent? */
759 if ((prev_upper_first
<= upper_last
) &&
760 (prev_upper_last
>= upper_first
))
763 /* Does the lower range intersect a previous extent? */
764 if ((prev_lower_first
<= lower_last
) &&
765 (prev_lower_last
>= lower_first
))
772 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
773 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
774 * UID_GID_MAP_MAX_BASE_EXTENTS.
776 static int insert_extent(struct uid_gid_map
*map
, struct uid_gid_extent
*extent
)
778 struct uid_gid_extent
*dest
;
780 if (map
->nr_extents
== UID_GID_MAP_MAX_BASE_EXTENTS
) {
781 struct uid_gid_extent
*forward
;
783 /* Allocate memory for 340 mappings. */
784 forward
= kmalloc_array(UID_GID_MAP_MAX_EXTENTS
,
785 sizeof(struct uid_gid_extent
),
790 /* Copy over memory. Only set up memory for the forward pointer.
791 * Defer the memory setup for the reverse pointer.
793 memcpy(forward
, map
->extent
,
794 map
->nr_extents
* sizeof(map
->extent
[0]));
796 map
->forward
= forward
;
800 if (map
->nr_extents
< UID_GID_MAP_MAX_BASE_EXTENTS
)
801 dest
= &map
->extent
[map
->nr_extents
];
803 dest
= &map
->forward
[map
->nr_extents
];
810 /* cmp function to sort() forward mappings */
811 static int cmp_extents_forward(const void *a
, const void *b
)
813 const struct uid_gid_extent
*e1
= a
;
814 const struct uid_gid_extent
*e2
= b
;
816 if (e1
->first
< e2
->first
)
819 if (e1
->first
> e2
->first
)
825 /* cmp function to sort() reverse mappings */
826 static int cmp_extents_reverse(const void *a
, const void *b
)
828 const struct uid_gid_extent
*e1
= a
;
829 const struct uid_gid_extent
*e2
= b
;
831 if (e1
->lower_first
< e2
->lower_first
)
834 if (e1
->lower_first
> e2
->lower_first
)
841 * sort_idmaps - Sorts an array of idmap entries.
842 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
844 static int sort_idmaps(struct uid_gid_map
*map
)
846 if (map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
849 /* Sort forward array. */
850 sort(map
->forward
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
851 cmp_extents_forward
, NULL
);
853 /* Only copy the memory from forward we actually need. */
854 map
->reverse
= kmemdup(map
->forward
,
855 map
->nr_extents
* sizeof(struct uid_gid_extent
),
860 /* Sort reverse array. */
861 sort(map
->reverse
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
862 cmp_extents_reverse
, NULL
);
868 * verify_root_map() - check the uid 0 mapping
869 * @file: idmapping file
870 * @map_ns: user namespace of the target process
871 * @new_map: requested idmap
873 * If a process requests mapping parent uid 0 into the new ns, verify that the
874 * process writing the map had the CAP_SETFCAP capability as the target process
875 * will be able to write fscaps that are valid in ancestor user namespaces.
877 * Return: true if the mapping is allowed, false if not.
879 static bool verify_root_map(const struct file
*file
,
880 struct user_namespace
*map_ns
,
881 struct uid_gid_map
*new_map
)
884 const struct user_namespace
*file_ns
= file
->f_cred
->user_ns
;
885 struct uid_gid_extent
*extent0
= NULL
;
887 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
888 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
889 extent0
= &new_map
->extent
[idx
];
891 extent0
= &new_map
->forward
[idx
];
892 if (extent0
->lower_first
== 0)
901 if (map_ns
== file_ns
) {
902 /* The process unshared its ns and is writing to its own
903 * /proc/self/uid_map. User already has full capabilites in
904 * the new namespace. Verify that the parent had CAP_SETFCAP
907 if (!file_ns
->parent_could_setfcap
)
910 /* Process p1 is writing to uid_map of p2, who is in a child
911 * user namespace to p1's. Verify that the opener of the map
912 * file has CAP_SETFCAP against the parent of the new map
914 if (!file_ns_capable(file
, map_ns
->parent
, CAP_SETFCAP
))
921 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
922 size_t count
, loff_t
*ppos
,
924 struct uid_gid_map
*map
,
925 struct uid_gid_map
*parent_map
)
927 struct seq_file
*seq
= file
->private_data
;
928 struct user_namespace
*map_ns
= seq
->private;
929 struct uid_gid_map new_map
;
931 struct uid_gid_extent extent
;
932 char *kbuf
= NULL
, *pos
, *next_line
;
935 /* Only allow < page size writes at the beginning of the file */
936 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
939 /* Slurp in the user data */
940 kbuf
= memdup_user_nul(buf
, count
);
942 return PTR_ERR(kbuf
);
945 * The userns_state_mutex serializes all writes to any given map.
947 * Any map is only ever written once.
949 * An id map fits within 1 cache line on most architectures.
951 * On read nothing needs to be done unless you are on an
952 * architecture with a crazy cache coherency model like alpha.
954 * There is a one time data dependency between reading the
955 * count of the extents and the values of the extents. The
956 * desired behavior is to see the values of the extents that
957 * were written before the count of the extents.
959 * To achieve this smp_wmb() is used on guarantee the write
960 * order and smp_rmb() is guaranteed that we don't have crazy
961 * architectures returning stale data.
963 mutex_lock(&userns_state_mutex
);
965 memset(&new_map
, 0, sizeof(struct uid_gid_map
));
968 /* Only allow one successful write to the map */
969 if (map
->nr_extents
!= 0)
973 * Adjusting namespace settings requires capabilities on the target.
975 if (cap_valid(cap_setid
) && !file_ns_capable(file
, map_ns
, CAP_SYS_ADMIN
))
978 /* Parse the user data */
981 for (; pos
; pos
= next_line
) {
983 /* Find the end of line and ensure I don't look past it */
984 next_line
= strchr(pos
, '\n');
988 if (*next_line
== '\0')
992 pos
= skip_spaces(pos
);
993 extent
.first
= simple_strtoul(pos
, &pos
, 10);
997 pos
= skip_spaces(pos
);
998 extent
.lower_first
= simple_strtoul(pos
, &pos
, 10);
1002 pos
= skip_spaces(pos
);
1003 extent
.count
= simple_strtoul(pos
, &pos
, 10);
1004 if (*pos
&& !isspace(*pos
))
1007 /* Verify there is not trailing junk on the line */
1008 pos
= skip_spaces(pos
);
1012 /* Verify we have been given valid starting values */
1013 if ((extent
.first
== (u32
) -1) ||
1014 (extent
.lower_first
== (u32
) -1))
1017 /* Verify count is not zero and does not cause the
1020 if ((extent
.first
+ extent
.count
) <= extent
.first
)
1022 if ((extent
.lower_first
+ extent
.count
) <=
1026 /* Do the ranges in extent overlap any previous extents? */
1027 if (mappings_overlap(&new_map
, &extent
))
1030 if ((new_map
.nr_extents
+ 1) == UID_GID_MAP_MAX_EXTENTS
&&
1031 (next_line
!= NULL
))
1034 ret
= insert_extent(&new_map
, &extent
);
1039 /* Be very certain the new map actually exists */
1040 if (new_map
.nr_extents
== 0)
1044 /* Validate the user is allowed to use user id's mapped to. */
1045 if (!new_idmap_permitted(file
, map_ns
, cap_setid
, &new_map
))
1049 /* Map the lower ids from the parent user namespace to the
1050 * kernel global id space.
1052 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
1053 struct uid_gid_extent
*e
;
1056 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
1057 e
= &new_map
.extent
[idx
];
1059 e
= &new_map
.forward
[idx
];
1061 lower_first
= map_id_range_down(parent_map
,
1065 /* Fail if we can not map the specified extent to
1066 * the kernel global id space.
1068 if (lower_first
== (u32
) -1)
1071 e
->lower_first
= lower_first
;
1075 * If we want to use binary search for lookup, this clones the extent
1076 * array and sorts both copies.
1078 ret
= sort_idmaps(&new_map
);
1082 /* Install the map */
1083 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
) {
1084 memcpy(map
->extent
, new_map
.extent
,
1085 new_map
.nr_extents
* sizeof(new_map
.extent
[0]));
1087 map
->forward
= new_map
.forward
;
1088 map
->reverse
= new_map
.reverse
;
1091 map
->nr_extents
= new_map
.nr_extents
;
1096 if (ret
< 0 && new_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
1097 kfree(new_map
.forward
);
1098 kfree(new_map
.reverse
);
1099 map
->forward
= NULL
;
1100 map
->reverse
= NULL
;
1101 map
->nr_extents
= 0;
1104 mutex_unlock(&userns_state_mutex
);
1109 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
1110 size_t size
, loff_t
*ppos
)
1112 struct seq_file
*seq
= file
->private_data
;
1113 struct user_namespace
*ns
= seq
->private;
1114 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1119 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1122 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
1123 &ns
->uid_map
, &ns
->parent
->uid_map
);
1126 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
1127 size_t size
, loff_t
*ppos
)
1129 struct seq_file
*seq
= file
->private_data
;
1130 struct user_namespace
*ns
= seq
->private;
1131 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1136 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1139 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
1140 &ns
->gid_map
, &ns
->parent
->gid_map
);
1143 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
1144 size_t size
, loff_t
*ppos
)
1146 struct seq_file
*seq
= file
->private_data
;
1147 struct user_namespace
*ns
= seq
->private;
1148 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1153 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1156 /* Anyone can set any valid project id no capability needed */
1157 return map_write(file
, buf
, size
, ppos
, -1,
1158 &ns
->projid_map
, &ns
->parent
->projid_map
);
1161 static bool new_idmap_permitted(const struct file
*file
,
1162 struct user_namespace
*ns
, int cap_setid
,
1163 struct uid_gid_map
*new_map
)
1165 const struct cred
*cred
= file
->f_cred
;
1167 if (cap_setid
== CAP_SETUID
&& !verify_root_map(file
, ns
, new_map
))
1170 /* Don't allow mappings that would allow anything that wouldn't
1171 * be allowed without the establishment of unprivileged mappings.
1173 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
1174 uid_eq(ns
->owner
, cred
->euid
)) {
1175 u32 id
= new_map
->extent
[0].lower_first
;
1176 if (cap_setid
== CAP_SETUID
) {
1177 kuid_t uid
= make_kuid(ns
->parent
, id
);
1178 if (uid_eq(uid
, cred
->euid
))
1180 } else if (cap_setid
== CAP_SETGID
) {
1181 kgid_t gid
= make_kgid(ns
->parent
, id
);
1182 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
1183 gid_eq(gid
, cred
->egid
))
1188 /* Allow anyone to set a mapping that doesn't require privilege */
1189 if (!cap_valid(cap_setid
))
1192 /* Allow the specified ids if we have the appropriate capability
1193 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
1194 * And the opener of the id file also has the appropriate capability.
1196 if (ns_capable(ns
->parent
, cap_setid
) &&
1197 file_ns_capable(file
, ns
->parent
, cap_setid
))
1203 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
1205 struct user_namespace
*ns
= seq
->private;
1206 unsigned long userns_flags
= READ_ONCE(ns
->flags
);
1208 seq_printf(seq
, "%s\n",
1209 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
1214 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
1215 size_t count
, loff_t
*ppos
)
1217 struct seq_file
*seq
= file
->private_data
;
1218 struct user_namespace
*ns
= seq
->private;
1220 bool setgroups_allowed
;
1223 /* Only allow a very narrow range of strings to be written */
1225 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
1228 /* What was written? */
1230 if (copy_from_user(kbuf
, buf
, count
))
1235 /* What is being requested? */
1237 if (strncmp(pos
, "allow", 5) == 0) {
1239 setgroups_allowed
= true;
1241 else if (strncmp(pos
, "deny", 4) == 0) {
1243 setgroups_allowed
= false;
1248 /* Verify there is not trailing junk on the line */
1249 pos
= skip_spaces(pos
);
1254 mutex_lock(&userns_state_mutex
);
1255 if (setgroups_allowed
) {
1256 /* Enabling setgroups after setgroups has been disabled
1259 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
1262 /* Permanently disabling setgroups after setgroups has
1263 * been enabled by writing the gid_map is not allowed.
1265 if (ns
->gid_map
.nr_extents
!= 0)
1267 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
1269 mutex_unlock(&userns_state_mutex
);
1271 /* Report a successful write */
1277 mutex_unlock(&userns_state_mutex
);
1281 bool userns_may_setgroups(const struct user_namespace
*ns
)
1285 mutex_lock(&userns_state_mutex
);
1286 /* It is not safe to use setgroups until a gid mapping in
1287 * the user namespace has been established.
1289 allowed
= ns
->gid_map
.nr_extents
!= 0;
1290 /* Is setgroups allowed? */
1291 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
1292 mutex_unlock(&userns_state_mutex
);
1298 * Returns true if @child is the same namespace or a descendant of
1301 bool in_userns(const struct user_namespace
*ancestor
,
1302 const struct user_namespace
*child
)
1304 const struct user_namespace
*ns
;
1305 for (ns
= child
; ns
->level
> ancestor
->level
; ns
= ns
->parent
)
1307 return (ns
== ancestor
);
1310 bool current_in_userns(const struct user_namespace
*target_ns
)
1312 return in_userns(target_ns
, current_user_ns());
1314 EXPORT_SYMBOL(current_in_userns
);
1316 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1318 return container_of(ns
, struct user_namespace
, ns
);
1321 static struct ns_common
*userns_get(struct task_struct
*task
)
1323 struct user_namespace
*user_ns
;
1326 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1329 return user_ns
? &user_ns
->ns
: NULL
;
1332 static void userns_put(struct ns_common
*ns
)
1334 put_user_ns(to_user_ns(ns
));
1337 static int userns_install(struct nsset
*nsset
, struct ns_common
*ns
)
1339 struct user_namespace
*user_ns
= to_user_ns(ns
);
1342 /* Don't allow gaining capabilities by reentering
1343 * the same user namespace.
1345 if (user_ns
== current_user_ns())
1348 /* Tasks that share a thread group must share a user namespace */
1349 if (!thread_group_empty(current
))
1352 if (current
->fs
->users
!= 1)
1355 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1358 cred
= nsset_cred(nsset
);
1362 put_user_ns(cred
->user_ns
);
1363 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1365 if (set_cred_ucounts(cred
) < 0)
1371 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1373 struct user_namespace
*my_user_ns
= current_user_ns();
1374 struct user_namespace
*owner
, *p
;
1376 /* See if the owner is in the current user namespace */
1377 owner
= p
= ns
->ops
->owner(ns
);
1380 return ERR_PTR(-EPERM
);
1381 if (p
== my_user_ns
)
1386 return &get_user_ns(owner
)->ns
;
1389 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1391 return to_user_ns(ns
)->parent
;
1394 const struct proc_ns_operations userns_operations
= {
1396 .type
= CLONE_NEWUSER
,
1399 .install
= userns_install
,
1400 .owner
= userns_owner
,
1401 .get_parent
= ns_get_owner
,
1404 static __init
int user_namespaces_init(void)
1406 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
| SLAB_ACCOUNT
);
1409 subsys_initcall(user_namespaces_init
);