2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation, version 2 of the
8 #include <linux/export.h>
9 #include <linux/nsproxy.h>
10 #include <linux/slab.h>
11 #include <linux/sched/signal.h>
12 #include <linux/user_namespace.h>
13 #include <linux/proc_ns.h>
14 #include <linux/highuid.h>
15 #include <linux/cred.h>
16 #include <linux/securebits.h>
17 #include <linux/keyctl.h>
18 #include <linux/key-type.h>
19 #include <keys/user-type.h>
20 #include <linux/seq_file.h>
22 #include <linux/uaccess.h>
23 #include <linux/ctype.h>
24 #include <linux/projid.h>
25 #include <linux/fs_struct.h>
26 #include <linux/bsearch.h>
27 #include <linux/sort.h>
30 * sysctl determining whether unprivileged users may unshare a new
31 * userns. Allowed by default
33 int unprivileged_userns_clone
= 1;
35 static struct kmem_cache
*user_ns_cachep __read_mostly
;
36 static DEFINE_MUTEX(userns_state_mutex
);
38 static bool new_idmap_permitted(const struct file
*file
,
39 struct user_namespace
*ns
, int cap_setid
,
40 struct uid_gid_map
*map
);
41 static void free_user_ns(struct work_struct
*work
);
43 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
45 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
48 static void dec_user_namespaces(struct ucounts
*ucounts
)
50 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
53 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
55 /* Start with the same capabilities as init but useless for doing
56 * anything as the capabilities are bound to the new user namespace.
58 cred
->securebits
= SECUREBITS_DEFAULT
;
59 cred
->cap_inheritable
= CAP_EMPTY_SET
;
60 cred
->cap_permitted
= CAP_FULL_SET
;
61 cred
->cap_effective
= CAP_FULL_SET
;
62 cred
->cap_ambient
= CAP_EMPTY_SET
;
63 cred
->cap_bset
= CAP_FULL_SET
;
65 key_put(cred
->request_key_auth
);
66 cred
->request_key_auth
= NULL
;
68 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
69 cred
->user_ns
= user_ns
;
73 * Create a new user namespace, deriving the creator from the user in the
74 * passed credentials, and replacing that user with the new root user for the
77 * This is called by copy_creds(), which will finish setting the target task's
80 int create_user_ns(struct cred
*new)
82 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
83 kuid_t owner
= new->euid
;
84 kgid_t group
= new->egid
;
85 struct ucounts
*ucounts
;
89 if (parent_ns
->level
> 32)
92 ucounts
= inc_user_namespaces(parent_ns
, owner
);
97 * Verify that we can not violate the policy of which files
98 * may be accessed that is specified by the root directory,
99 * by verifing that the root directory is at the root of the
100 * mount namespace which allows all files to be accessed.
103 if (current_chrooted())
106 /* The creator needs a mapping in the parent user namespace
107 * or else we won't be able to reasonably tell userspace who
108 * created a user_namespace.
111 if (!kuid_has_mapping(parent_ns
, owner
) ||
112 !kgid_has_mapping(parent_ns
, group
))
116 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
120 ret
= ns_alloc_inum(&ns
->ns
);
123 ns
->ns
.ops
= &userns_operations
;
125 atomic_set(&ns
->count
, 1);
126 /* Leave the new->user_ns reference with the new user namespace. */
127 ns
->parent
= parent_ns
;
128 ns
->level
= parent_ns
->level
+ 1;
131 INIT_WORK(&ns
->work
, free_user_ns
);
132 for (i
= 0; i
< UCOUNT_COUNTS
; i
++) {
133 ns
->ucount_max
[i
] = INT_MAX
;
135 ns
->ucounts
= ucounts
;
137 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
138 mutex_lock(&userns_state_mutex
);
139 ns
->flags
= parent_ns
->flags
;
140 mutex_unlock(&userns_state_mutex
);
142 #ifdef CONFIG_PERSISTENT_KEYRINGS
143 init_rwsem(&ns
->persistent_keyring_register_sem
);
146 if (!setup_userns_sysctls(ns
))
149 set_cred_user_ns(new, ns
);
152 #ifdef CONFIG_PERSISTENT_KEYRINGS
153 key_put(ns
->persistent_keyring_register
);
155 ns_free_inum(&ns
->ns
);
157 kmem_cache_free(user_ns_cachep
, ns
);
159 dec_user_namespaces(ucounts
);
164 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
169 if (!(unshare_flags
& CLONE_NEWUSER
))
172 cred
= prepare_creds();
174 err
= create_user_ns(cred
);
184 static void free_user_ns(struct work_struct
*work
)
186 struct user_namespace
*parent
, *ns
=
187 container_of(work
, struct user_namespace
, work
);
190 struct ucounts
*ucounts
= ns
->ucounts
;
192 if (ns
->gid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
193 kfree(ns
->gid_map
.forward
);
194 kfree(ns
->gid_map
.reverse
);
196 if (ns
->uid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
197 kfree(ns
->uid_map
.forward
);
198 kfree(ns
->uid_map
.reverse
);
200 if (ns
->projid_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
201 kfree(ns
->projid_map
.forward
);
202 kfree(ns
->projid_map
.reverse
);
204 retire_userns_sysctls(ns
);
205 #ifdef CONFIG_PERSISTENT_KEYRINGS
206 key_put(ns
->persistent_keyring_register
);
208 ns_free_inum(&ns
->ns
);
209 kmem_cache_free(user_ns_cachep
, ns
);
210 dec_user_namespaces(ucounts
);
212 } while (atomic_dec_and_test(&parent
->count
));
215 void __put_user_ns(struct user_namespace
*ns
)
217 schedule_work(&ns
->work
);
219 EXPORT_SYMBOL(__put_user_ns
);
222 * idmap_key struct holds the information necessary to find an idmapping in a
223 * sorted idmap array. It is passed to cmp_map_id() as first argument.
226 bool map_up
; /* true -> id from kid; false -> kid from id */
227 u32 id
; /* id to find */
228 u32 count
; /* == 0 unless used with map_id_range_down() */
232 * cmp_map_id - Function to be passed to bsearch() to find the requested
233 * idmapping. Expects struct idmap_key to be passed via @k.
235 static int cmp_map_id(const void *k
, const void *e
)
237 u32 first
, last
, id2
;
238 const struct idmap_key
*key
= k
;
239 const struct uid_gid_extent
*el
= e
;
241 id2
= key
->id
+ key
->count
- 1;
243 /* handle map_id_{down,up}() */
245 first
= el
->lower_first
;
249 last
= first
+ el
->count
- 1;
251 if (key
->id
>= first
&& key
->id
<= last
&&
252 (id2
>= first
&& id2
<= last
))
255 if (key
->id
< first
|| id2
< first
)
262 * map_id_range_down_max - Find idmap via binary search in ordered idmap array.
263 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
265 static struct uid_gid_extent
*
266 map_id_range_down_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
268 struct idmap_key key
;
274 return bsearch(&key
, map
->forward
, extents
,
275 sizeof(struct uid_gid_extent
), cmp_map_id
);
279 * map_id_range_down_base - Find idmap via binary search in static extent array.
280 * Can only be called if number of mappings is equal or less than
281 * UID_GID_MAP_MAX_BASE_EXTENTS.
283 static struct uid_gid_extent
*
284 map_id_range_down_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
, u32 count
)
287 u32 first
, last
, id2
;
289 id2
= id
+ count
- 1;
291 /* Find the matching extent */
292 for (idx
= 0; idx
< extents
; idx
++) {
293 first
= map
->extent
[idx
].first
;
294 last
= first
+ map
->extent
[idx
].count
- 1;
295 if (id
>= first
&& id
<= last
&&
296 (id2
>= first
&& id2
<= last
))
297 return &map
->extent
[idx
];
302 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
304 struct uid_gid_extent
*extent
;
305 unsigned extents
= map
->nr_extents
;
308 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
309 extent
= map_id_range_down_base(extents
, map
, id
, count
);
311 extent
= map_id_range_down_max(extents
, map
, id
, count
);
313 /* Map the id or note failure */
315 id
= (id
- extent
->first
) + extent
->lower_first
;
322 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
324 return map_id_range_down(map
, id
, 1);
328 * map_id_up_base - Find idmap via binary search in static extent array.
329 * Can only be called if number of mappings is equal or less than
330 * UID_GID_MAP_MAX_BASE_EXTENTS.
332 static struct uid_gid_extent
*
333 map_id_up_base(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
338 /* Find the matching extent */
339 for (idx
= 0; idx
< extents
; idx
++) {
340 first
= map
->extent
[idx
].lower_first
;
341 last
= first
+ map
->extent
[idx
].count
- 1;
342 if (id
>= first
&& id
<= last
)
343 return &map
->extent
[idx
];
349 * map_id_up_max - Find idmap via binary search in ordered idmap array.
350 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
352 static struct uid_gid_extent
*
353 map_id_up_max(unsigned extents
, struct uid_gid_map
*map
, u32 id
)
355 struct idmap_key key
;
361 return bsearch(&key
, map
->reverse
, extents
,
362 sizeof(struct uid_gid_extent
), cmp_map_id
);
365 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
367 struct uid_gid_extent
*extent
;
368 unsigned extents
= map
->nr_extents
;
371 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
372 extent
= map_id_up_base(extents
, map
, id
);
374 extent
= map_id_up_max(extents
, map
, id
);
376 /* Map the id or note failure */
378 id
= (id
- extent
->lower_first
) + extent
->first
;
386 * make_kuid - Map a user-namespace uid pair into a kuid.
387 * @ns: User namespace that the uid is in
388 * @uid: User identifier
390 * Maps a user-namespace uid pair into a kernel internal kuid,
391 * and returns that kuid.
393 * When there is no mapping defined for the user-namespace uid
394 * pair INVALID_UID is returned. Callers are expected to test
395 * for and handle INVALID_UID being returned. INVALID_UID
396 * may be tested for using uid_valid().
398 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
400 /* Map the uid to a global kernel uid */
401 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
403 EXPORT_SYMBOL(make_kuid
);
406 * from_kuid - Create a uid from a kuid user-namespace pair.
407 * @targ: The user namespace we want a uid in.
408 * @kuid: The kernel internal uid to start with.
410 * Map @kuid into the user-namespace specified by @targ and
411 * return the resulting uid.
413 * There is always a mapping into the initial user_namespace.
415 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
417 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
419 /* Map the uid from a global kernel uid */
420 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
422 EXPORT_SYMBOL(from_kuid
);
425 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
426 * @targ: The user namespace we want a uid in.
427 * @kuid: The kernel internal uid to start with.
429 * Map @kuid into the user-namespace specified by @targ and
430 * return the resulting uid.
432 * There is always a mapping into the initial user_namespace.
434 * Unlike from_kuid from_kuid_munged never fails and always
435 * returns a valid uid. This makes from_kuid_munged appropriate
436 * for use in syscalls like stat and getuid where failing the
437 * system call and failing to provide a valid uid are not an
440 * If @kuid has no mapping in @targ overflowuid is returned.
442 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
445 uid
= from_kuid(targ
, kuid
);
447 if (uid
== (uid_t
) -1)
451 EXPORT_SYMBOL(from_kuid_munged
);
454 * make_kgid - Map a user-namespace gid pair into a kgid.
455 * @ns: User namespace that the gid is in
456 * @gid: group identifier
458 * Maps a user-namespace gid pair into a kernel internal kgid,
459 * and returns that kgid.
461 * When there is no mapping defined for the user-namespace gid
462 * pair INVALID_GID is returned. Callers are expected to test
463 * for and handle INVALID_GID being returned. INVALID_GID may be
464 * tested for using gid_valid().
466 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
468 /* Map the gid to a global kernel gid */
469 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
471 EXPORT_SYMBOL(make_kgid
);
474 * from_kgid - Create a gid from a kgid user-namespace pair.
475 * @targ: The user namespace we want a gid in.
476 * @kgid: The kernel internal gid to start with.
478 * Map @kgid into the user-namespace specified by @targ and
479 * return the resulting gid.
481 * There is always a mapping into the initial user_namespace.
483 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
485 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
487 /* Map the gid from a global kernel gid */
488 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
490 EXPORT_SYMBOL(from_kgid
);
493 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
494 * @targ: The user namespace we want a gid in.
495 * @kgid: The kernel internal gid to start with.
497 * Map @kgid into the user-namespace specified by @targ and
498 * return the resulting gid.
500 * There is always a mapping into the initial user_namespace.
502 * Unlike from_kgid from_kgid_munged never fails and always
503 * returns a valid gid. This makes from_kgid_munged appropriate
504 * for use in syscalls like stat and getgid where failing the
505 * system call and failing to provide a valid gid are not options.
507 * If @kgid has no mapping in @targ overflowgid is returned.
509 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
512 gid
= from_kgid(targ
, kgid
);
514 if (gid
== (gid_t
) -1)
518 EXPORT_SYMBOL(from_kgid_munged
);
521 * make_kprojid - Map a user-namespace projid pair into a kprojid.
522 * @ns: User namespace that the projid is in
523 * @projid: Project identifier
525 * Maps a user-namespace uid pair into a kernel internal kuid,
526 * and returns that kuid.
528 * When there is no mapping defined for the user-namespace projid
529 * pair INVALID_PROJID is returned. Callers are expected to test
530 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
531 * may be tested for using projid_valid().
533 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
535 /* Map the uid to a global kernel uid */
536 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
538 EXPORT_SYMBOL(make_kprojid
);
541 * from_kprojid - Create a projid from a kprojid user-namespace pair.
542 * @targ: The user namespace we want a projid in.
543 * @kprojid: The kernel internal project identifier to start with.
545 * Map @kprojid into the user-namespace specified by @targ and
546 * return the resulting projid.
548 * There is always a mapping into the initial user_namespace.
550 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
552 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
554 /* Map the uid from a global kernel uid */
555 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
557 EXPORT_SYMBOL(from_kprojid
);
560 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
561 * @targ: The user namespace we want a projid in.
562 * @kprojid: The kernel internal projid to start with.
564 * Map @kprojid into the user-namespace specified by @targ and
565 * return the resulting projid.
567 * There is always a mapping into the initial user_namespace.
569 * Unlike from_kprojid from_kprojid_munged never fails and always
570 * returns a valid projid. This makes from_kprojid_munged
571 * appropriate for use in syscalls like stat and where
572 * failing the system call and failing to provide a valid projid are
575 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
577 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
580 projid
= from_kprojid(targ
, kprojid
);
582 if (projid
== (projid_t
) -1)
583 projid
= OVERFLOW_PROJID
;
586 EXPORT_SYMBOL(from_kprojid_munged
);
589 static int uid_m_show(struct seq_file
*seq
, void *v
)
591 struct user_namespace
*ns
= seq
->private;
592 struct uid_gid_extent
*extent
= v
;
593 struct user_namespace
*lower_ns
;
596 lower_ns
= seq_user_ns(seq
);
597 if ((lower_ns
== ns
) && lower_ns
->parent
)
598 lower_ns
= lower_ns
->parent
;
600 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
602 seq_printf(seq
, "%10u %10u %10u\n",
610 static int gid_m_show(struct seq_file
*seq
, void *v
)
612 struct user_namespace
*ns
= seq
->private;
613 struct uid_gid_extent
*extent
= v
;
614 struct user_namespace
*lower_ns
;
617 lower_ns
= seq_user_ns(seq
);
618 if ((lower_ns
== ns
) && lower_ns
->parent
)
619 lower_ns
= lower_ns
->parent
;
621 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
623 seq_printf(seq
, "%10u %10u %10u\n",
631 static int projid_m_show(struct seq_file
*seq
, void *v
)
633 struct user_namespace
*ns
= seq
->private;
634 struct uid_gid_extent
*extent
= v
;
635 struct user_namespace
*lower_ns
;
638 lower_ns
= seq_user_ns(seq
);
639 if ((lower_ns
== ns
) && lower_ns
->parent
)
640 lower_ns
= lower_ns
->parent
;
642 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
644 seq_printf(seq
, "%10u %10u %10u\n",
652 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
653 struct uid_gid_map
*map
)
656 unsigned extents
= map
->nr_extents
;
662 if (extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
663 return &map
->extent
[pos
];
665 return &map
->forward
[pos
];
668 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
670 struct user_namespace
*ns
= seq
->private;
672 return m_start(seq
, ppos
, &ns
->uid_map
);
675 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
677 struct user_namespace
*ns
= seq
->private;
679 return m_start(seq
, ppos
, &ns
->gid_map
);
682 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
684 struct user_namespace
*ns
= seq
->private;
686 return m_start(seq
, ppos
, &ns
->projid_map
);
689 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
692 return seq
->op
->start(seq
, pos
);
695 static void m_stop(struct seq_file
*seq
, void *v
)
700 const struct seq_operations proc_uid_seq_operations
= {
701 .start
= uid_m_start
,
707 const struct seq_operations proc_gid_seq_operations
= {
708 .start
= gid_m_start
,
714 const struct seq_operations proc_projid_seq_operations
= {
715 .start
= projid_m_start
,
718 .show
= projid_m_show
,
721 static bool mappings_overlap(struct uid_gid_map
*new_map
,
722 struct uid_gid_extent
*extent
)
724 u32 upper_first
, lower_first
, upper_last
, lower_last
;
727 upper_first
= extent
->first
;
728 lower_first
= extent
->lower_first
;
729 upper_last
= upper_first
+ extent
->count
- 1;
730 lower_last
= lower_first
+ extent
->count
- 1;
732 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
733 u32 prev_upper_first
, prev_lower_first
;
734 u32 prev_upper_last
, prev_lower_last
;
735 struct uid_gid_extent
*prev
;
737 if (new_map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
738 prev
= &new_map
->extent
[idx
];
740 prev
= &new_map
->forward
[idx
];
742 prev_upper_first
= prev
->first
;
743 prev_lower_first
= prev
->lower_first
;
744 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
745 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
747 /* Does the upper range intersect a previous extent? */
748 if ((prev_upper_first
<= upper_last
) &&
749 (prev_upper_last
>= upper_first
))
752 /* Does the lower range intersect a previous extent? */
753 if ((prev_lower_first
<= lower_last
) &&
754 (prev_lower_last
>= lower_first
))
761 * insert_extent - Safely insert a new idmap extent into struct uid_gid_map.
762 * Takes care to allocate a 4K block of memory if the number of mappings exceeds
763 * UID_GID_MAP_MAX_BASE_EXTENTS.
765 static int insert_extent(struct uid_gid_map
*map
, struct uid_gid_extent
*extent
)
767 struct uid_gid_extent
*dest
;
769 if (map
->nr_extents
== UID_GID_MAP_MAX_BASE_EXTENTS
) {
770 struct uid_gid_extent
*forward
;
772 /* Allocate memory for 340 mappings. */
773 forward
= kmalloc(sizeof(struct uid_gid_extent
) *
774 UID_GID_MAP_MAX_EXTENTS
, GFP_KERNEL
);
778 /* Copy over memory. Only set up memory for the forward pointer.
779 * Defer the memory setup for the reverse pointer.
781 memcpy(forward
, map
->extent
,
782 map
->nr_extents
* sizeof(map
->extent
[0]));
784 map
->forward
= forward
;
788 if (map
->nr_extents
< UID_GID_MAP_MAX_BASE_EXTENTS
)
789 dest
= &map
->extent
[map
->nr_extents
];
791 dest
= &map
->forward
[map
->nr_extents
];
798 /* cmp function to sort() forward mappings */
799 static int cmp_extents_forward(const void *a
, const void *b
)
801 const struct uid_gid_extent
*e1
= a
;
802 const struct uid_gid_extent
*e2
= b
;
804 if (e1
->first
< e2
->first
)
807 if (e1
->first
> e2
->first
)
813 /* cmp function to sort() reverse mappings */
814 static int cmp_extents_reverse(const void *a
, const void *b
)
816 const struct uid_gid_extent
*e1
= a
;
817 const struct uid_gid_extent
*e2
= b
;
819 if (e1
->lower_first
< e2
->lower_first
)
822 if (e1
->lower_first
> e2
->lower_first
)
829 * sort_idmaps - Sorts an array of idmap entries.
830 * Can only be called if number of mappings exceeds UID_GID_MAP_MAX_BASE_EXTENTS.
832 static int sort_idmaps(struct uid_gid_map
*map
)
834 if (map
->nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
837 /* Sort forward array. */
838 sort(map
->forward
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
839 cmp_extents_forward
, NULL
);
841 /* Only copy the memory from forward we actually need. */
842 map
->reverse
= kmemdup(map
->forward
,
843 map
->nr_extents
* sizeof(struct uid_gid_extent
),
848 /* Sort reverse array. */
849 sort(map
->reverse
, map
->nr_extents
, sizeof(struct uid_gid_extent
),
850 cmp_extents_reverse
, NULL
);
855 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
856 size_t count
, loff_t
*ppos
,
858 struct uid_gid_map
*map
,
859 struct uid_gid_map
*parent_map
)
861 struct seq_file
*seq
= file
->private_data
;
862 struct user_namespace
*ns
= seq
->private;
863 struct uid_gid_map new_map
;
865 struct uid_gid_extent extent
;
866 char *kbuf
= NULL
, *pos
, *next_line
;
867 ssize_t ret
= -EINVAL
;
870 * The userns_state_mutex serializes all writes to any given map.
872 * Any map is only ever written once.
874 * An id map fits within 1 cache line on most architectures.
876 * On read nothing needs to be done unless you are on an
877 * architecture with a crazy cache coherency model like alpha.
879 * There is a one time data dependency between reading the
880 * count of the extents and the values of the extents. The
881 * desired behavior is to see the values of the extents that
882 * were written before the count of the extents.
884 * To achieve this smp_wmb() is used on guarantee the write
885 * order and smp_rmb() is guaranteed that we don't have crazy
886 * architectures returning stale data.
888 mutex_lock(&userns_state_mutex
);
890 memset(&new_map
, 0, sizeof(struct uid_gid_map
));
893 /* Only allow one successful write to the map */
894 if (map
->nr_extents
!= 0)
898 * Adjusting namespace settings requires capabilities on the target.
900 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
903 /* Only allow < page size writes at the beginning of the file */
905 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
908 /* Slurp in the user data */
909 kbuf
= memdup_user_nul(buf
, count
);
916 /* Parse the user data */
919 for (; pos
; pos
= next_line
) {
921 /* Find the end of line and ensure I don't look past it */
922 next_line
= strchr(pos
, '\n');
926 if (*next_line
== '\0')
930 pos
= skip_spaces(pos
);
931 extent
.first
= simple_strtoul(pos
, &pos
, 10);
935 pos
= skip_spaces(pos
);
936 extent
.lower_first
= simple_strtoul(pos
, &pos
, 10);
940 pos
= skip_spaces(pos
);
941 extent
.count
= simple_strtoul(pos
, &pos
, 10);
942 if (*pos
&& !isspace(*pos
))
945 /* Verify there is not trailing junk on the line */
946 pos
= skip_spaces(pos
);
950 /* Verify we have been given valid starting values */
951 if ((extent
.first
== (u32
) -1) ||
952 (extent
.lower_first
== (u32
) -1))
955 /* Verify count is not zero and does not cause the
958 if ((extent
.first
+ extent
.count
) <= extent
.first
)
960 if ((extent
.lower_first
+ extent
.count
) <=
964 /* Do the ranges in extent overlap any previous extents? */
965 if (mappings_overlap(&new_map
, &extent
))
968 if ((new_map
.nr_extents
+ 1) == UID_GID_MAP_MAX_EXTENTS
&&
972 ret
= insert_extent(&new_map
, &extent
);
977 /* Be very certaint the new map actually exists */
978 if (new_map
.nr_extents
== 0)
982 /* Validate the user is allowed to use user id's mapped to. */
983 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
986 ret
= sort_idmaps(&new_map
);
991 /* Map the lower ids from the parent user namespace to the
992 * kernel global id space.
994 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
995 struct uid_gid_extent
*e
;
998 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
)
999 e
= &new_map
.extent
[idx
];
1001 e
= &new_map
.forward
[idx
];
1003 lower_first
= map_id_range_down(parent_map
,
1007 /* Fail if we can not map the specified extent to
1008 * the kernel global id space.
1010 if (lower_first
== (u32
) -1)
1013 e
->lower_first
= lower_first
;
1016 /* Install the map */
1017 if (new_map
.nr_extents
<= UID_GID_MAP_MAX_BASE_EXTENTS
) {
1018 memcpy(map
->extent
, new_map
.extent
,
1019 new_map
.nr_extents
* sizeof(new_map
.extent
[0]));
1021 map
->forward
= new_map
.forward
;
1022 map
->reverse
= new_map
.reverse
;
1025 map
->nr_extents
= new_map
.nr_extents
;
1030 if (ret
< 0 && new_map
.nr_extents
> UID_GID_MAP_MAX_BASE_EXTENTS
) {
1031 kfree(new_map
.forward
);
1032 kfree(new_map
.reverse
);
1033 map
->forward
= NULL
;
1034 map
->reverse
= NULL
;
1035 map
->nr_extents
= 0;
1038 mutex_unlock(&userns_state_mutex
);
1043 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
1044 size_t size
, loff_t
*ppos
)
1046 struct seq_file
*seq
= file
->private_data
;
1047 struct user_namespace
*ns
= seq
->private;
1048 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1053 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1056 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
1057 &ns
->uid_map
, &ns
->parent
->uid_map
);
1060 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
1061 size_t size
, loff_t
*ppos
)
1063 struct seq_file
*seq
= file
->private_data
;
1064 struct user_namespace
*ns
= seq
->private;
1065 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1070 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1073 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
1074 &ns
->gid_map
, &ns
->parent
->gid_map
);
1077 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
1078 size_t size
, loff_t
*ppos
)
1080 struct seq_file
*seq
= file
->private_data
;
1081 struct user_namespace
*ns
= seq
->private;
1082 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
1087 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
1090 /* Anyone can set any valid project id no capability needed */
1091 return map_write(file
, buf
, size
, ppos
, -1,
1092 &ns
->projid_map
, &ns
->parent
->projid_map
);
1095 static bool new_idmap_permitted(const struct file
*file
,
1096 struct user_namespace
*ns
, int cap_setid
,
1097 struct uid_gid_map
*new_map
)
1099 const struct cred
*cred
= file
->f_cred
;
1100 /* Don't allow mappings that would allow anything that wouldn't
1101 * be allowed without the establishment of unprivileged mappings.
1103 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
1104 uid_eq(ns
->owner
, cred
->euid
)) {
1105 u32 id
= new_map
->extent
[0].lower_first
;
1106 if (cap_setid
== CAP_SETUID
) {
1107 kuid_t uid
= make_kuid(ns
->parent
, id
);
1108 if (uid_eq(uid
, cred
->euid
))
1110 } else if (cap_setid
== CAP_SETGID
) {
1111 kgid_t gid
= make_kgid(ns
->parent
, id
);
1112 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
1113 gid_eq(gid
, cred
->egid
))
1118 /* Allow anyone to set a mapping that doesn't require privilege */
1119 if (!cap_valid(cap_setid
))
1122 /* Allow the specified ids if we have the appropriate capability
1123 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
1124 * And the opener of the id file also had the approprpiate capability.
1126 if (ns_capable(ns
->parent
, cap_setid
) &&
1127 file_ns_capable(file
, ns
->parent
, cap_setid
))
1133 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
1135 struct user_namespace
*ns
= seq
->private;
1136 unsigned long userns_flags
= READ_ONCE(ns
->flags
);
1138 seq_printf(seq
, "%s\n",
1139 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
1144 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
1145 size_t count
, loff_t
*ppos
)
1147 struct seq_file
*seq
= file
->private_data
;
1148 struct user_namespace
*ns
= seq
->private;
1150 bool setgroups_allowed
;
1153 /* Only allow a very narrow range of strings to be written */
1155 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
1158 /* What was written? */
1160 if (copy_from_user(kbuf
, buf
, count
))
1165 /* What is being requested? */
1167 if (strncmp(pos
, "allow", 5) == 0) {
1169 setgroups_allowed
= true;
1171 else if (strncmp(pos
, "deny", 4) == 0) {
1173 setgroups_allowed
= false;
1178 /* Verify there is not trailing junk on the line */
1179 pos
= skip_spaces(pos
);
1184 mutex_lock(&userns_state_mutex
);
1185 if (setgroups_allowed
) {
1186 /* Enabling setgroups after setgroups has been disabled
1189 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
1192 /* Permanently disabling setgroups after setgroups has
1193 * been enabled by writing the gid_map is not allowed.
1195 if (ns
->gid_map
.nr_extents
!= 0)
1197 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
1199 mutex_unlock(&userns_state_mutex
);
1201 /* Report a successful write */
1207 mutex_unlock(&userns_state_mutex
);
1211 bool userns_may_setgroups(const struct user_namespace
*ns
)
1215 mutex_lock(&userns_state_mutex
);
1216 /* It is not safe to use setgroups until a gid mapping in
1217 * the user namespace has been established.
1219 allowed
= ns
->gid_map
.nr_extents
!= 0;
1220 /* Is setgroups allowed? */
1221 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
1222 mutex_unlock(&userns_state_mutex
);
1228 * Returns true if @child is the same namespace or a descendant of
1231 bool in_userns(const struct user_namespace
*ancestor
,
1232 const struct user_namespace
*child
)
1234 const struct user_namespace
*ns
;
1235 for (ns
= child
; ns
->level
> ancestor
->level
; ns
= ns
->parent
)
1237 return (ns
== ancestor
);
1240 bool current_in_userns(const struct user_namespace
*target_ns
)
1242 return in_userns(target_ns
, current_user_ns());
1244 EXPORT_SYMBOL(current_in_userns
);
1246 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1248 return container_of(ns
, struct user_namespace
, ns
);
1251 static struct ns_common
*userns_get(struct task_struct
*task
)
1253 struct user_namespace
*user_ns
;
1256 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1259 return user_ns
? &user_ns
->ns
: NULL
;
1262 static void userns_put(struct ns_common
*ns
)
1264 put_user_ns(to_user_ns(ns
));
1267 static int userns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
1269 struct user_namespace
*user_ns
= to_user_ns(ns
);
1272 /* Don't allow gaining capabilities by reentering
1273 * the same user namespace.
1275 if (user_ns
== current_user_ns())
1278 /* Tasks that share a thread group must share a user namespace */
1279 if (!thread_group_empty(current
))
1282 if (current
->fs
->users
!= 1)
1285 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1288 cred
= prepare_creds();
1292 put_user_ns(cred
->user_ns
);
1293 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1295 return commit_creds(cred
);
1298 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1300 struct user_namespace
*my_user_ns
= current_user_ns();
1301 struct user_namespace
*owner
, *p
;
1303 /* See if the owner is in the current user namespace */
1304 owner
= p
= ns
->ops
->owner(ns
);
1307 return ERR_PTR(-EPERM
);
1308 if (p
== my_user_ns
)
1313 return &get_user_ns(owner
)->ns
;
1316 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1318 return to_user_ns(ns
)->parent
;
1321 const struct proc_ns_operations userns_operations
= {
1323 .type
= CLONE_NEWUSER
,
1326 .install
= userns_install
,
1327 .owner
= userns_owner
,
1328 .get_parent
= ns_get_owner
,
1331 static __init
int user_namespaces_init(void)
1333 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1336 subsys_initcall(user_namespaces_init
);