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/user_namespace.h>
12 #include <linux/proc_ns.h>
13 #include <linux/highuid.h>
14 #include <linux/cred.h>
15 #include <linux/securebits.h>
16 #include <linux/keyctl.h>
17 #include <linux/key-type.h>
18 #include <keys/user-type.h>
19 #include <linux/seq_file.h>
21 #include <linux/uaccess.h>
22 #include <linux/ctype.h>
23 #include <linux/projid.h>
24 #include <linux/fs_struct.h>
27 * sysctl determining whether unprivileged users may unshare a new
28 * userns. Allowed by default
30 int unprivileged_userns_clone
= 1;
32 static struct kmem_cache
*user_ns_cachep __read_mostly
;
33 static DEFINE_MUTEX(userns_state_mutex
);
35 static bool new_idmap_permitted(const struct file
*file
,
36 struct user_namespace
*ns
, int cap_setid
,
37 struct uid_gid_map
*map
);
38 static void free_user_ns(struct work_struct
*work
);
40 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
42 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
45 static void dec_user_namespaces(struct ucounts
*ucounts
)
47 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
50 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
52 /* Start with the same capabilities as init but useless for doing
53 * anything as the capabilities are bound to the new user namespace.
55 cred
->securebits
= SECUREBITS_DEFAULT
;
56 cred
->cap_inheritable
= CAP_EMPTY_SET
;
57 cred
->cap_permitted
= CAP_FULL_SET
;
58 cred
->cap_effective
= CAP_FULL_SET
;
59 cred
->cap_ambient
= CAP_EMPTY_SET
;
60 cred
->cap_bset
= CAP_FULL_SET
;
62 key_put(cred
->request_key_auth
);
63 cred
->request_key_auth
= NULL
;
65 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
66 cred
->user_ns
= user_ns
;
70 * Create a new user namespace, deriving the creator from the user in the
71 * passed credentials, and replacing that user with the new root user for the
74 * This is called by copy_creds(), which will finish setting the target task's
77 int create_user_ns(struct cred
*new)
79 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
80 kuid_t owner
= new->euid
;
81 kgid_t group
= new->egid
;
82 struct ucounts
*ucounts
;
86 if (parent_ns
->level
> 32)
89 ucounts
= inc_user_namespaces(parent_ns
, owner
);
94 * Verify that we can not violate the policy of which files
95 * may be accessed that is specified by the root directory,
96 * by verifing that the root directory is at the root of the
97 * mount namespace which allows all files to be accessed.
100 if (current_chrooted())
103 /* The creator needs a mapping in the parent user namespace
104 * or else we won't be able to reasonably tell userspace who
105 * created a user_namespace.
108 if (!kuid_has_mapping(parent_ns
, owner
) ||
109 !kgid_has_mapping(parent_ns
, group
))
113 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
117 ret
= ns_alloc_inum(&ns
->ns
);
120 ns
->ns
.ops
= &userns_operations
;
122 atomic_set(&ns
->count
, 1);
123 /* Leave the new->user_ns reference with the new user namespace. */
124 ns
->parent
= parent_ns
;
125 ns
->level
= parent_ns
->level
+ 1;
128 INIT_WORK(&ns
->work
, free_user_ns
);
129 for (i
= 0; i
< UCOUNT_COUNTS
; i
++) {
130 ns
->ucount_max
[i
] = INT_MAX
;
132 ns
->ucounts
= ucounts
;
134 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
135 mutex_lock(&userns_state_mutex
);
136 ns
->flags
= parent_ns
->flags
;
137 mutex_unlock(&userns_state_mutex
);
139 #ifdef CONFIG_PERSISTENT_KEYRINGS
140 init_rwsem(&ns
->persistent_keyring_register_sem
);
143 if (!setup_userns_sysctls(ns
))
146 set_cred_user_ns(new, ns
);
149 #ifdef CONFIG_PERSISTENT_KEYRINGS
150 key_put(ns
->persistent_keyring_register
);
152 ns_free_inum(&ns
->ns
);
154 kmem_cache_free(user_ns_cachep
, ns
);
156 dec_user_namespaces(ucounts
);
161 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
166 if (!(unshare_flags
& CLONE_NEWUSER
))
169 cred
= prepare_creds();
171 err
= create_user_ns(cred
);
181 static void free_user_ns(struct work_struct
*work
)
183 struct user_namespace
*parent
, *ns
=
184 container_of(work
, struct user_namespace
, work
);
187 struct ucounts
*ucounts
= ns
->ucounts
;
189 retire_userns_sysctls(ns
);
190 #ifdef CONFIG_PERSISTENT_KEYRINGS
191 key_put(ns
->persistent_keyring_register
);
193 ns_free_inum(&ns
->ns
);
194 kmem_cache_free(user_ns_cachep
, ns
);
195 dec_user_namespaces(ucounts
);
197 } while (atomic_dec_and_test(&parent
->count
));
200 void __put_user_ns(struct user_namespace
*ns
)
202 schedule_work(&ns
->work
);
204 EXPORT_SYMBOL(__put_user_ns
);
206 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
208 unsigned idx
, extents
;
209 u32 first
, last
, id2
;
211 id2
= id
+ count
- 1;
213 /* Find the matching extent */
214 extents
= map
->nr_extents
;
216 for (idx
= 0; idx
< extents
; idx
++) {
217 first
= map
->extent
[idx
].first
;
218 last
= first
+ map
->extent
[idx
].count
- 1;
219 if (id
>= first
&& id
<= last
&&
220 (id2
>= first
&& id2
<= last
))
223 /* Map the id or note failure */
225 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
232 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
234 unsigned idx
, extents
;
237 /* Find the matching extent */
238 extents
= map
->nr_extents
;
240 for (idx
= 0; idx
< extents
; idx
++) {
241 first
= map
->extent
[idx
].first
;
242 last
= first
+ map
->extent
[idx
].count
- 1;
243 if (id
>= first
&& id
<= last
)
246 /* Map the id or note failure */
248 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
255 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
257 unsigned idx
, extents
;
260 /* Find the matching extent */
261 extents
= map
->nr_extents
;
263 for (idx
= 0; idx
< extents
; idx
++) {
264 first
= map
->extent
[idx
].lower_first
;
265 last
= first
+ map
->extent
[idx
].count
- 1;
266 if (id
>= first
&& id
<= last
)
269 /* Map the id or note failure */
271 id
= (id
- first
) + map
->extent
[idx
].first
;
279 * make_kuid - Map a user-namespace uid pair into a kuid.
280 * @ns: User namespace that the uid is in
281 * @uid: User identifier
283 * Maps a user-namespace uid pair into a kernel internal kuid,
284 * and returns that kuid.
286 * When there is no mapping defined for the user-namespace uid
287 * pair INVALID_UID is returned. Callers are expected to test
288 * for and handle INVALID_UID being returned. INVALID_UID
289 * may be tested for using uid_valid().
291 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
293 /* Map the uid to a global kernel uid */
294 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
296 EXPORT_SYMBOL(make_kuid
);
299 * from_kuid - Create a uid from a kuid user-namespace pair.
300 * @targ: The user namespace we want a uid in.
301 * @kuid: The kernel internal uid to start with.
303 * Map @kuid into the user-namespace specified by @targ and
304 * return the resulting uid.
306 * There is always a mapping into the initial user_namespace.
308 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
310 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
312 /* Map the uid from a global kernel uid */
313 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
315 EXPORT_SYMBOL(from_kuid
);
318 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
319 * @targ: The user namespace we want a uid in.
320 * @kuid: The kernel internal uid to start with.
322 * Map @kuid into the user-namespace specified by @targ and
323 * return the resulting uid.
325 * There is always a mapping into the initial user_namespace.
327 * Unlike from_kuid from_kuid_munged never fails and always
328 * returns a valid uid. This makes from_kuid_munged appropriate
329 * for use in syscalls like stat and getuid where failing the
330 * system call and failing to provide a valid uid are not an
333 * If @kuid has no mapping in @targ overflowuid is returned.
335 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
338 uid
= from_kuid(targ
, kuid
);
340 if (uid
== (uid_t
) -1)
344 EXPORT_SYMBOL(from_kuid_munged
);
347 * make_kgid - Map a user-namespace gid pair into a kgid.
348 * @ns: User namespace that the gid is in
349 * @gid: group identifier
351 * Maps a user-namespace gid pair into a kernel internal kgid,
352 * and returns that kgid.
354 * When there is no mapping defined for the user-namespace gid
355 * pair INVALID_GID is returned. Callers are expected to test
356 * for and handle INVALID_GID being returned. INVALID_GID may be
357 * tested for using gid_valid().
359 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
361 /* Map the gid to a global kernel gid */
362 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
364 EXPORT_SYMBOL(make_kgid
);
367 * from_kgid - Create a gid from a kgid user-namespace pair.
368 * @targ: The user namespace we want a gid in.
369 * @kgid: The kernel internal gid to start with.
371 * Map @kgid into the user-namespace specified by @targ and
372 * return the resulting gid.
374 * There is always a mapping into the initial user_namespace.
376 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
378 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
380 /* Map the gid from a global kernel gid */
381 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
383 EXPORT_SYMBOL(from_kgid
);
386 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
387 * @targ: The user namespace we want a gid in.
388 * @kgid: The kernel internal gid to start with.
390 * Map @kgid into the user-namespace specified by @targ and
391 * return the resulting gid.
393 * There is always a mapping into the initial user_namespace.
395 * Unlike from_kgid from_kgid_munged never fails and always
396 * returns a valid gid. This makes from_kgid_munged appropriate
397 * for use in syscalls like stat and getgid where failing the
398 * system call and failing to provide a valid gid are not options.
400 * If @kgid has no mapping in @targ overflowgid is returned.
402 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
405 gid
= from_kgid(targ
, kgid
);
407 if (gid
== (gid_t
) -1)
411 EXPORT_SYMBOL(from_kgid_munged
);
414 * make_kprojid - Map a user-namespace projid pair into a kprojid.
415 * @ns: User namespace that the projid is in
416 * @projid: Project identifier
418 * Maps a user-namespace uid pair into a kernel internal kuid,
419 * and returns that kuid.
421 * When there is no mapping defined for the user-namespace projid
422 * pair INVALID_PROJID is returned. Callers are expected to test
423 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
424 * may be tested for using projid_valid().
426 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
428 /* Map the uid to a global kernel uid */
429 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
431 EXPORT_SYMBOL(make_kprojid
);
434 * from_kprojid - Create a projid from a kprojid user-namespace pair.
435 * @targ: The user namespace we want a projid in.
436 * @kprojid: The kernel internal project identifier to start with.
438 * Map @kprojid into the user-namespace specified by @targ and
439 * return the resulting projid.
441 * There is always a mapping into the initial user_namespace.
443 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
445 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
447 /* Map the uid from a global kernel uid */
448 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
450 EXPORT_SYMBOL(from_kprojid
);
453 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
454 * @targ: The user namespace we want a projid in.
455 * @kprojid: The kernel internal projid to start with.
457 * Map @kprojid into the user-namespace specified by @targ and
458 * return the resulting projid.
460 * There is always a mapping into the initial user_namespace.
462 * Unlike from_kprojid from_kprojid_munged never fails and always
463 * returns a valid projid. This makes from_kprojid_munged
464 * appropriate for use in syscalls like stat and where
465 * failing the system call and failing to provide a valid projid are
468 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
470 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
473 projid
= from_kprojid(targ
, kprojid
);
475 if (projid
== (projid_t
) -1)
476 projid
= OVERFLOW_PROJID
;
479 EXPORT_SYMBOL(from_kprojid_munged
);
482 static int uid_m_show(struct seq_file
*seq
, void *v
)
484 struct user_namespace
*ns
= seq
->private;
485 struct uid_gid_extent
*extent
= v
;
486 struct user_namespace
*lower_ns
;
489 lower_ns
= seq_user_ns(seq
);
490 if ((lower_ns
== ns
) && lower_ns
->parent
)
491 lower_ns
= lower_ns
->parent
;
493 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
495 seq_printf(seq
, "%10u %10u %10u\n",
503 static int gid_m_show(struct seq_file
*seq
, void *v
)
505 struct user_namespace
*ns
= seq
->private;
506 struct uid_gid_extent
*extent
= v
;
507 struct user_namespace
*lower_ns
;
510 lower_ns
= seq_user_ns(seq
);
511 if ((lower_ns
== ns
) && lower_ns
->parent
)
512 lower_ns
= lower_ns
->parent
;
514 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
516 seq_printf(seq
, "%10u %10u %10u\n",
524 static int projid_m_show(struct seq_file
*seq
, void *v
)
526 struct user_namespace
*ns
= seq
->private;
527 struct uid_gid_extent
*extent
= v
;
528 struct user_namespace
*lower_ns
;
531 lower_ns
= seq_user_ns(seq
);
532 if ((lower_ns
== ns
) && lower_ns
->parent
)
533 lower_ns
= lower_ns
->parent
;
535 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
537 seq_printf(seq
, "%10u %10u %10u\n",
545 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
546 struct uid_gid_map
*map
)
548 struct uid_gid_extent
*extent
= NULL
;
551 if (pos
< map
->nr_extents
)
552 extent
= &map
->extent
[pos
];
557 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
559 struct user_namespace
*ns
= seq
->private;
561 return m_start(seq
, ppos
, &ns
->uid_map
);
564 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
566 struct user_namespace
*ns
= seq
->private;
568 return m_start(seq
, ppos
, &ns
->gid_map
);
571 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
573 struct user_namespace
*ns
= seq
->private;
575 return m_start(seq
, ppos
, &ns
->projid_map
);
578 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
581 return seq
->op
->start(seq
, pos
);
584 static void m_stop(struct seq_file
*seq
, void *v
)
589 const struct seq_operations proc_uid_seq_operations
= {
590 .start
= uid_m_start
,
596 const struct seq_operations proc_gid_seq_operations
= {
597 .start
= gid_m_start
,
603 const struct seq_operations proc_projid_seq_operations
= {
604 .start
= projid_m_start
,
607 .show
= projid_m_show
,
610 static bool mappings_overlap(struct uid_gid_map
*new_map
,
611 struct uid_gid_extent
*extent
)
613 u32 upper_first
, lower_first
, upper_last
, lower_last
;
616 upper_first
= extent
->first
;
617 lower_first
= extent
->lower_first
;
618 upper_last
= upper_first
+ extent
->count
- 1;
619 lower_last
= lower_first
+ extent
->count
- 1;
621 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
622 u32 prev_upper_first
, prev_lower_first
;
623 u32 prev_upper_last
, prev_lower_last
;
624 struct uid_gid_extent
*prev
;
626 prev
= &new_map
->extent
[idx
];
628 prev_upper_first
= prev
->first
;
629 prev_lower_first
= prev
->lower_first
;
630 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
631 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
633 /* Does the upper range intersect a previous extent? */
634 if ((prev_upper_first
<= upper_last
) &&
635 (prev_upper_last
>= upper_first
))
638 /* Does the lower range intersect a previous extent? */
639 if ((prev_lower_first
<= lower_last
) &&
640 (prev_lower_last
>= lower_first
))
646 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
647 size_t count
, loff_t
*ppos
,
649 struct uid_gid_map
*map
,
650 struct uid_gid_map
*parent_map
)
652 struct seq_file
*seq
= file
->private_data
;
653 struct user_namespace
*ns
= seq
->private;
654 struct uid_gid_map new_map
;
656 struct uid_gid_extent
*extent
= NULL
;
657 char *kbuf
= NULL
, *pos
, *next_line
;
658 ssize_t ret
= -EINVAL
;
661 * The userns_state_mutex serializes all writes to any given map.
663 * Any map is only ever written once.
665 * An id map fits within 1 cache line on most architectures.
667 * On read nothing needs to be done unless you are on an
668 * architecture with a crazy cache coherency model like alpha.
670 * There is a one time data dependency between reading the
671 * count of the extents and the values of the extents. The
672 * desired behavior is to see the values of the extents that
673 * were written before the count of the extents.
675 * To achieve this smp_wmb() is used on guarantee the write
676 * order and smp_rmb() is guaranteed that we don't have crazy
677 * architectures returning stale data.
679 mutex_lock(&userns_state_mutex
);
682 /* Only allow one successful write to the map */
683 if (map
->nr_extents
!= 0)
687 * Adjusting namespace settings requires capabilities on the target.
689 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
692 /* Only allow < page size writes at the beginning of the file */
694 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
697 /* Slurp in the user data */
698 kbuf
= memdup_user_nul(buf
, count
);
705 /* Parse the user data */
708 new_map
.nr_extents
= 0;
709 for (; pos
; pos
= next_line
) {
710 extent
= &new_map
.extent
[new_map
.nr_extents
];
712 /* Find the end of line and ensure I don't look past it */
713 next_line
= strchr(pos
, '\n');
717 if (*next_line
== '\0')
721 pos
= skip_spaces(pos
);
722 extent
->first
= simple_strtoul(pos
, &pos
, 10);
726 pos
= skip_spaces(pos
);
727 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
731 pos
= skip_spaces(pos
);
732 extent
->count
= simple_strtoul(pos
, &pos
, 10);
733 if (*pos
&& !isspace(*pos
))
736 /* Verify there is not trailing junk on the line */
737 pos
= skip_spaces(pos
);
741 /* Verify we have been given valid starting values */
742 if ((extent
->first
== (u32
) -1) ||
743 (extent
->lower_first
== (u32
) -1))
746 /* Verify count is not zero and does not cause the
749 if ((extent
->first
+ extent
->count
) <= extent
->first
)
751 if ((extent
->lower_first
+ extent
->count
) <=
755 /* Do the ranges in extent overlap any previous extents? */
756 if (mappings_overlap(&new_map
, extent
))
759 new_map
.nr_extents
++;
761 /* Fail if the file contains too many extents */
762 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
766 /* Be very certaint the new map actually exists */
767 if (new_map
.nr_extents
== 0)
771 /* Validate the user is allowed to use user id's mapped to. */
772 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
775 /* Map the lower ids from the parent user namespace to the
776 * kernel global id space.
778 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
780 extent
= &new_map
.extent
[idx
];
782 lower_first
= map_id_range_down(parent_map
,
786 /* Fail if we can not map the specified extent to
787 * the kernel global id space.
789 if (lower_first
== (u32
) -1)
792 extent
->lower_first
= lower_first
;
795 /* Install the map */
796 memcpy(map
->extent
, new_map
.extent
,
797 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
799 map
->nr_extents
= new_map
.nr_extents
;
804 mutex_unlock(&userns_state_mutex
);
809 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
810 size_t size
, loff_t
*ppos
)
812 struct seq_file
*seq
= file
->private_data
;
813 struct user_namespace
*ns
= seq
->private;
814 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
819 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
822 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
823 &ns
->uid_map
, &ns
->parent
->uid_map
);
826 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
827 size_t size
, loff_t
*ppos
)
829 struct seq_file
*seq
= file
->private_data
;
830 struct user_namespace
*ns
= seq
->private;
831 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
836 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
839 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
840 &ns
->gid_map
, &ns
->parent
->gid_map
);
843 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
844 size_t size
, loff_t
*ppos
)
846 struct seq_file
*seq
= file
->private_data
;
847 struct user_namespace
*ns
= seq
->private;
848 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
853 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
856 /* Anyone can set any valid project id no capability needed */
857 return map_write(file
, buf
, size
, ppos
, -1,
858 &ns
->projid_map
, &ns
->parent
->projid_map
);
861 static bool new_idmap_permitted(const struct file
*file
,
862 struct user_namespace
*ns
, int cap_setid
,
863 struct uid_gid_map
*new_map
)
865 const struct cred
*cred
= file
->f_cred
;
866 /* Don't allow mappings that would allow anything that wouldn't
867 * be allowed without the establishment of unprivileged mappings.
869 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
870 uid_eq(ns
->owner
, cred
->euid
)) {
871 u32 id
= new_map
->extent
[0].lower_first
;
872 if (cap_setid
== CAP_SETUID
) {
873 kuid_t uid
= make_kuid(ns
->parent
, id
);
874 if (uid_eq(uid
, cred
->euid
))
876 } else if (cap_setid
== CAP_SETGID
) {
877 kgid_t gid
= make_kgid(ns
->parent
, id
);
878 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
879 gid_eq(gid
, cred
->egid
))
884 /* Allow anyone to set a mapping that doesn't require privilege */
885 if (!cap_valid(cap_setid
))
888 /* Allow the specified ids if we have the appropriate capability
889 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
890 * And the opener of the id file also had the approprpiate capability.
892 if (ns_capable(ns
->parent
, cap_setid
) &&
893 file_ns_capable(file
, ns
->parent
, cap_setid
))
899 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
901 struct user_namespace
*ns
= seq
->private;
902 unsigned long userns_flags
= ACCESS_ONCE(ns
->flags
);
904 seq_printf(seq
, "%s\n",
905 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
910 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
911 size_t count
, loff_t
*ppos
)
913 struct seq_file
*seq
= file
->private_data
;
914 struct user_namespace
*ns
= seq
->private;
916 bool setgroups_allowed
;
919 /* Only allow a very narrow range of strings to be written */
921 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
924 /* What was written? */
926 if (copy_from_user(kbuf
, buf
, count
))
931 /* What is being requested? */
933 if (strncmp(pos
, "allow", 5) == 0) {
935 setgroups_allowed
= true;
937 else if (strncmp(pos
, "deny", 4) == 0) {
939 setgroups_allowed
= false;
944 /* Verify there is not trailing junk on the line */
945 pos
= skip_spaces(pos
);
950 mutex_lock(&userns_state_mutex
);
951 if (setgroups_allowed
) {
952 /* Enabling setgroups after setgroups has been disabled
955 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
958 /* Permanently disabling setgroups after setgroups has
959 * been enabled by writing the gid_map is not allowed.
961 if (ns
->gid_map
.nr_extents
!= 0)
963 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
965 mutex_unlock(&userns_state_mutex
);
967 /* Report a successful write */
973 mutex_unlock(&userns_state_mutex
);
977 bool userns_may_setgroups(const struct user_namespace
*ns
)
981 mutex_lock(&userns_state_mutex
);
982 /* It is not safe to use setgroups until a gid mapping in
983 * the user namespace has been established.
985 allowed
= ns
->gid_map
.nr_extents
!= 0;
986 /* Is setgroups allowed? */
987 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
988 mutex_unlock(&userns_state_mutex
);
994 * Returns true if @ns is the same namespace as or a descendant of
997 bool current_in_userns(const struct user_namespace
*target_ns
)
999 struct user_namespace
*ns
;
1000 for (ns
= current_user_ns(); ns
; ns
= ns
->parent
) {
1001 if (ns
== target_ns
)
1006 EXPORT_SYMBOL(current_in_userns
);
1008 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1010 return container_of(ns
, struct user_namespace
, ns
);
1013 static struct ns_common
*userns_get(struct task_struct
*task
)
1015 struct user_namespace
*user_ns
;
1018 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1021 return user_ns
? &user_ns
->ns
: NULL
;
1024 static void userns_put(struct ns_common
*ns
)
1026 put_user_ns(to_user_ns(ns
));
1029 static int userns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
1031 struct user_namespace
*user_ns
= to_user_ns(ns
);
1034 /* Don't allow gaining capabilities by reentering
1035 * the same user namespace.
1037 if (user_ns
== current_user_ns())
1040 /* Tasks that share a thread group must share a user namespace */
1041 if (!thread_group_empty(current
))
1044 if (current
->fs
->users
!= 1)
1047 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1050 cred
= prepare_creds();
1054 put_user_ns(cred
->user_ns
);
1055 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1057 return commit_creds(cred
);
1060 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1062 struct user_namespace
*my_user_ns
= current_user_ns();
1063 struct user_namespace
*owner
, *p
;
1065 /* See if the owner is in the current user namespace */
1066 owner
= p
= ns
->ops
->owner(ns
);
1069 return ERR_PTR(-EPERM
);
1070 if (p
== my_user_ns
)
1075 return &get_user_ns(owner
)->ns
;
1078 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1080 return to_user_ns(ns
)->parent
;
1083 const struct proc_ns_operations userns_operations
= {
1085 .type
= CLONE_NEWUSER
,
1088 .install
= userns_install
,
1089 .owner
= userns_owner
,
1090 .get_parent
= ns_get_owner
,
1093 static __init
int user_namespaces_init(void)
1095 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1098 subsys_initcall(user_namespaces_init
);