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
);
39 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
41 /* Start with the same capabilities as init but useless for doing
42 * anything as the capabilities are bound to the new user namespace.
44 cred
->securebits
= SECUREBITS_DEFAULT
;
45 cred
->cap_inheritable
= CAP_EMPTY_SET
;
46 cred
->cap_permitted
= CAP_FULL_SET
;
47 cred
->cap_effective
= CAP_FULL_SET
;
48 cred
->cap_ambient
= CAP_EMPTY_SET
;
49 cred
->cap_bset
= CAP_FULL_SET
;
51 key_put(cred
->request_key_auth
);
52 cred
->request_key_auth
= NULL
;
54 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
55 cred
->user_ns
= user_ns
;
59 * Create a new user namespace, deriving the creator from the user in the
60 * passed credentials, and replacing that user with the new root user for the
63 * This is called by copy_creds(), which will finish setting the target task's
66 int create_user_ns(struct cred
*new)
68 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
69 kuid_t owner
= new->euid
;
70 kgid_t group
= new->egid
;
73 if (parent_ns
->level
> 32)
77 * Verify that we can not violate the policy of which files
78 * may be accessed that is specified by the root directory,
79 * by verifing that the root directory is at the root of the
80 * mount namespace which allows all files to be accessed.
82 if (current_chrooted())
85 /* The creator needs a mapping in the parent user namespace
86 * or else we won't be able to reasonably tell userspace who
87 * created a user_namespace.
89 if (!kuid_has_mapping(parent_ns
, owner
) ||
90 !kgid_has_mapping(parent_ns
, group
))
93 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
97 ret
= ns_alloc_inum(&ns
->ns
);
99 kmem_cache_free(user_ns_cachep
, ns
);
102 ns
->ns
.ops
= &userns_operations
;
104 atomic_set(&ns
->count
, 1);
105 /* Leave the new->user_ns reference with the new user namespace. */
106 ns
->parent
= parent_ns
;
107 ns
->level
= parent_ns
->level
+ 1;
111 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
112 mutex_lock(&userns_state_mutex
);
113 ns
->flags
= parent_ns
->flags
;
114 mutex_unlock(&userns_state_mutex
);
116 set_cred_user_ns(new, ns
);
118 #ifdef CONFIG_PERSISTENT_KEYRINGS
119 init_rwsem(&ns
->persistent_keyring_register_sem
);
124 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
129 if (!(unshare_flags
& CLONE_NEWUSER
))
132 cred
= prepare_creds();
134 err
= create_user_ns(cred
);
144 void free_user_ns(struct user_namespace
*ns
)
146 struct user_namespace
*parent
;
150 #ifdef CONFIG_PERSISTENT_KEYRINGS
151 key_put(ns
->persistent_keyring_register
);
153 ns_free_inum(&ns
->ns
);
154 kmem_cache_free(user_ns_cachep
, ns
);
156 } while (atomic_dec_and_test(&parent
->count
));
158 EXPORT_SYMBOL(free_user_ns
);
160 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
162 unsigned idx
, extents
;
163 u32 first
, last
, id2
;
165 id2
= id
+ count
- 1;
167 /* Find the matching extent */
168 extents
= map
->nr_extents
;
170 for (idx
= 0; idx
< extents
; idx
++) {
171 first
= map
->extent
[idx
].first
;
172 last
= first
+ map
->extent
[idx
].count
- 1;
173 if (id
>= first
&& id
<= last
&&
174 (id2
>= first
&& id2
<= last
))
177 /* Map the id or note failure */
179 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
186 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
188 unsigned idx
, extents
;
191 /* Find the matching extent */
192 extents
= map
->nr_extents
;
194 for (idx
= 0; idx
< extents
; idx
++) {
195 first
= map
->extent
[idx
].first
;
196 last
= first
+ map
->extent
[idx
].count
- 1;
197 if (id
>= first
&& id
<= last
)
200 /* Map the id or note failure */
202 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
209 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
211 unsigned idx
, extents
;
214 /* Find the matching extent */
215 extents
= map
->nr_extents
;
217 for (idx
= 0; idx
< extents
; idx
++) {
218 first
= map
->extent
[idx
].lower_first
;
219 last
= first
+ map
->extent
[idx
].count
- 1;
220 if (id
>= first
&& id
<= last
)
223 /* Map the id or note failure */
225 id
= (id
- first
) + map
->extent
[idx
].first
;
233 * make_kuid - Map a user-namespace uid pair into a kuid.
234 * @ns: User namespace that the uid is in
235 * @uid: User identifier
237 * Maps a user-namespace uid pair into a kernel internal kuid,
238 * and returns that kuid.
240 * When there is no mapping defined for the user-namespace uid
241 * pair INVALID_UID is returned. Callers are expected to test
242 * for and handle INVALID_UID being returned. INVALID_UID
243 * may be tested for using uid_valid().
245 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
247 /* Map the uid to a global kernel uid */
248 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
250 EXPORT_SYMBOL(make_kuid
);
253 * from_kuid - Create a uid from a kuid user-namespace pair.
254 * @targ: The user namespace we want a uid in.
255 * @kuid: The kernel internal uid to start with.
257 * Map @kuid into the user-namespace specified by @targ and
258 * return the resulting uid.
260 * There is always a mapping into the initial user_namespace.
262 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
264 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
266 /* Map the uid from a global kernel uid */
267 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
269 EXPORT_SYMBOL(from_kuid
);
272 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
273 * @targ: The user namespace we want a uid in.
274 * @kuid: The kernel internal uid to start with.
276 * Map @kuid into the user-namespace specified by @targ and
277 * return the resulting uid.
279 * There is always a mapping into the initial user_namespace.
281 * Unlike from_kuid from_kuid_munged never fails and always
282 * returns a valid uid. This makes from_kuid_munged appropriate
283 * for use in syscalls like stat and getuid where failing the
284 * system call and failing to provide a valid uid are not an
287 * If @kuid has no mapping in @targ overflowuid is returned.
289 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
292 uid
= from_kuid(targ
, kuid
);
294 if (uid
== (uid_t
) -1)
298 EXPORT_SYMBOL(from_kuid_munged
);
301 * make_kgid - Map a user-namespace gid pair into a kgid.
302 * @ns: User namespace that the gid is in
303 * @gid: group identifier
305 * Maps a user-namespace gid pair into a kernel internal kgid,
306 * and returns that kgid.
308 * When there is no mapping defined for the user-namespace gid
309 * pair INVALID_GID is returned. Callers are expected to test
310 * for and handle INVALID_GID being returned. INVALID_GID may be
311 * tested for using gid_valid().
313 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
315 /* Map the gid to a global kernel gid */
316 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
318 EXPORT_SYMBOL(make_kgid
);
321 * from_kgid - Create a gid from a kgid user-namespace pair.
322 * @targ: The user namespace we want a gid in.
323 * @kgid: The kernel internal gid to start with.
325 * Map @kgid into the user-namespace specified by @targ and
326 * return the resulting gid.
328 * There is always a mapping into the initial user_namespace.
330 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
332 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
334 /* Map the gid from a global kernel gid */
335 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
337 EXPORT_SYMBOL(from_kgid
);
340 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
341 * @targ: The user namespace we want a gid in.
342 * @kgid: The kernel internal gid to start with.
344 * Map @kgid into the user-namespace specified by @targ and
345 * return the resulting gid.
347 * There is always a mapping into the initial user_namespace.
349 * Unlike from_kgid from_kgid_munged never fails and always
350 * returns a valid gid. This makes from_kgid_munged appropriate
351 * for use in syscalls like stat and getgid where failing the
352 * system call and failing to provide a valid gid are not options.
354 * If @kgid has no mapping in @targ overflowgid is returned.
356 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
359 gid
= from_kgid(targ
, kgid
);
361 if (gid
== (gid_t
) -1)
365 EXPORT_SYMBOL(from_kgid_munged
);
368 * make_kprojid - Map a user-namespace projid pair into a kprojid.
369 * @ns: User namespace that the projid is in
370 * @projid: Project identifier
372 * Maps a user-namespace uid pair into a kernel internal kuid,
373 * and returns that kuid.
375 * When there is no mapping defined for the user-namespace projid
376 * pair INVALID_PROJID is returned. Callers are expected to test
377 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
378 * may be tested for using projid_valid().
380 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
382 /* Map the uid to a global kernel uid */
383 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
385 EXPORT_SYMBOL(make_kprojid
);
388 * from_kprojid - Create a projid from a kprojid user-namespace pair.
389 * @targ: The user namespace we want a projid in.
390 * @kprojid: The kernel internal project identifier to start with.
392 * Map @kprojid into the user-namespace specified by @targ and
393 * return the resulting projid.
395 * There is always a mapping into the initial user_namespace.
397 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
399 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
401 /* Map the uid from a global kernel uid */
402 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
404 EXPORT_SYMBOL(from_kprojid
);
407 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
408 * @targ: The user namespace we want a projid in.
409 * @kprojid: The kernel internal projid to start with.
411 * Map @kprojid into the user-namespace specified by @targ and
412 * return the resulting projid.
414 * There is always a mapping into the initial user_namespace.
416 * Unlike from_kprojid from_kprojid_munged never fails and always
417 * returns a valid projid. This makes from_kprojid_munged
418 * appropriate for use in syscalls like stat and where
419 * failing the system call and failing to provide a valid projid are
422 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
424 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
427 projid
= from_kprojid(targ
, kprojid
);
429 if (projid
== (projid_t
) -1)
430 projid
= OVERFLOW_PROJID
;
433 EXPORT_SYMBOL(from_kprojid_munged
);
436 static int uid_m_show(struct seq_file
*seq
, void *v
)
438 struct user_namespace
*ns
= seq
->private;
439 struct uid_gid_extent
*extent
= v
;
440 struct user_namespace
*lower_ns
;
443 lower_ns
= seq_user_ns(seq
);
444 if ((lower_ns
== ns
) && lower_ns
->parent
)
445 lower_ns
= lower_ns
->parent
;
447 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
449 seq_printf(seq
, "%10u %10u %10u\n",
457 static int gid_m_show(struct seq_file
*seq
, void *v
)
459 struct user_namespace
*ns
= seq
->private;
460 struct uid_gid_extent
*extent
= v
;
461 struct user_namespace
*lower_ns
;
464 lower_ns
= seq_user_ns(seq
);
465 if ((lower_ns
== ns
) && lower_ns
->parent
)
466 lower_ns
= lower_ns
->parent
;
468 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
470 seq_printf(seq
, "%10u %10u %10u\n",
478 static int projid_m_show(struct seq_file
*seq
, void *v
)
480 struct user_namespace
*ns
= seq
->private;
481 struct uid_gid_extent
*extent
= v
;
482 struct user_namespace
*lower_ns
;
485 lower_ns
= seq_user_ns(seq
);
486 if ((lower_ns
== ns
) && lower_ns
->parent
)
487 lower_ns
= lower_ns
->parent
;
489 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
491 seq_printf(seq
, "%10u %10u %10u\n",
499 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
500 struct uid_gid_map
*map
)
502 struct uid_gid_extent
*extent
= NULL
;
505 if (pos
< map
->nr_extents
) {
507 extent
= &map
->extent
[pos
];
513 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
515 struct user_namespace
*ns
= seq
->private;
517 return m_start(seq
, ppos
, &ns
->uid_map
);
520 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
522 struct user_namespace
*ns
= seq
->private;
524 return m_start(seq
, ppos
, &ns
->gid_map
);
527 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
529 struct user_namespace
*ns
= seq
->private;
531 return m_start(seq
, ppos
, &ns
->projid_map
);
534 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
537 return seq
->op
->start(seq
, pos
);
540 static void m_stop(struct seq_file
*seq
, void *v
)
545 const struct seq_operations proc_uid_seq_operations
= {
546 .start
= uid_m_start
,
552 const struct seq_operations proc_gid_seq_operations
= {
553 .start
= gid_m_start
,
559 const struct seq_operations proc_projid_seq_operations
= {
560 .start
= projid_m_start
,
563 .show
= projid_m_show
,
566 static bool mappings_overlap(struct uid_gid_map
*new_map
,
567 struct uid_gid_extent
*extent
)
569 u32 upper_first
, lower_first
, upper_last
, lower_last
;
572 upper_first
= extent
->first
;
573 lower_first
= extent
->lower_first
;
574 upper_last
= upper_first
+ extent
->count
- 1;
575 lower_last
= lower_first
+ extent
->count
- 1;
577 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
578 u32 prev_upper_first
, prev_lower_first
;
579 u32 prev_upper_last
, prev_lower_last
;
580 struct uid_gid_extent
*prev
;
582 prev
= &new_map
->extent
[idx
];
584 prev_upper_first
= prev
->first
;
585 prev_lower_first
= prev
->lower_first
;
586 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
587 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
589 /* Does the upper range intersect a previous extent? */
590 if ((prev_upper_first
<= upper_last
) &&
591 (prev_upper_last
>= upper_first
))
594 /* Does the lower range intersect a previous extent? */
595 if ((prev_lower_first
<= lower_last
) &&
596 (prev_lower_last
>= lower_first
))
602 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
603 size_t count
, loff_t
*ppos
,
605 struct uid_gid_map
*map
,
606 struct uid_gid_map
*parent_map
)
608 struct seq_file
*seq
= file
->private_data
;
609 struct user_namespace
*ns
= seq
->private;
610 struct uid_gid_map new_map
;
612 struct uid_gid_extent
*extent
= NULL
;
613 unsigned long page
= 0;
614 char *kbuf
, *pos
, *next_line
;
615 ssize_t ret
= -EINVAL
;
618 * The userns_state_mutex serializes all writes to any given map.
620 * Any map is only ever written once.
622 * An id map fits within 1 cache line on most architectures.
624 * On read nothing needs to be done unless you are on an
625 * architecture with a crazy cache coherency model like alpha.
627 * There is a one time data dependency between reading the
628 * count of the extents and the values of the extents. The
629 * desired behavior is to see the values of the extents that
630 * were written before the count of the extents.
632 * To achieve this smp_wmb() is used on guarantee the write
633 * order and smp_rmb() is guaranteed that we don't have crazy
634 * architectures returning stale data.
636 mutex_lock(&userns_state_mutex
);
639 /* Only allow one successful write to the map */
640 if (map
->nr_extents
!= 0)
644 * Adjusting namespace settings requires capabilities on the target.
646 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
651 page
= __get_free_page(GFP_TEMPORARY
);
652 kbuf
= (char *) page
;
656 /* Only allow < page size writes at the beginning of the file */
658 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
661 /* Slurp in the user data */
663 if (copy_from_user(kbuf
, buf
, count
))
667 /* Parse the user data */
670 new_map
.nr_extents
= 0;
671 for (; pos
; pos
= next_line
) {
672 extent
= &new_map
.extent
[new_map
.nr_extents
];
674 /* Find the end of line and ensure I don't look past it */
675 next_line
= strchr(pos
, '\n');
679 if (*next_line
== '\0')
683 pos
= skip_spaces(pos
);
684 extent
->first
= simple_strtoul(pos
, &pos
, 10);
688 pos
= skip_spaces(pos
);
689 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
693 pos
= skip_spaces(pos
);
694 extent
->count
= simple_strtoul(pos
, &pos
, 10);
695 if (*pos
&& !isspace(*pos
))
698 /* Verify there is not trailing junk on the line */
699 pos
= skip_spaces(pos
);
703 /* Verify we have been given valid starting values */
704 if ((extent
->first
== (u32
) -1) ||
705 (extent
->lower_first
== (u32
) -1))
708 /* Verify count is not zero and does not cause the
711 if ((extent
->first
+ extent
->count
) <= extent
->first
)
713 if ((extent
->lower_first
+ extent
->count
) <=
717 /* Do the ranges in extent overlap any previous extents? */
718 if (mappings_overlap(&new_map
, extent
))
721 new_map
.nr_extents
++;
723 /* Fail if the file contains too many extents */
724 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
728 /* Be very certaint the new map actually exists */
729 if (new_map
.nr_extents
== 0)
733 /* Validate the user is allowed to use user id's mapped to. */
734 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
737 /* Map the lower ids from the parent user namespace to the
738 * kernel global id space.
740 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
742 extent
= &new_map
.extent
[idx
];
744 lower_first
= map_id_range_down(parent_map
,
748 /* Fail if we can not map the specified extent to
749 * the kernel global id space.
751 if (lower_first
== (u32
) -1)
754 extent
->lower_first
= lower_first
;
757 /* Install the map */
758 memcpy(map
->extent
, new_map
.extent
,
759 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
761 map
->nr_extents
= new_map
.nr_extents
;
766 mutex_unlock(&userns_state_mutex
);
772 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
773 size_t size
, loff_t
*ppos
)
775 struct seq_file
*seq
= file
->private_data
;
776 struct user_namespace
*ns
= seq
->private;
777 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
782 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
785 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
786 &ns
->uid_map
, &ns
->parent
->uid_map
);
789 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
790 size_t size
, loff_t
*ppos
)
792 struct seq_file
*seq
= file
->private_data
;
793 struct user_namespace
*ns
= seq
->private;
794 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
799 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
802 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
803 &ns
->gid_map
, &ns
->parent
->gid_map
);
806 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
807 size_t size
, loff_t
*ppos
)
809 struct seq_file
*seq
= file
->private_data
;
810 struct user_namespace
*ns
= seq
->private;
811 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
816 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
819 /* Anyone can set any valid project id no capability needed */
820 return map_write(file
, buf
, size
, ppos
, -1,
821 &ns
->projid_map
, &ns
->parent
->projid_map
);
824 static bool new_idmap_permitted(const struct file
*file
,
825 struct user_namespace
*ns
, int cap_setid
,
826 struct uid_gid_map
*new_map
)
828 const struct cred
*cred
= file
->f_cred
;
829 /* Don't allow mappings that would allow anything that wouldn't
830 * be allowed without the establishment of unprivileged mappings.
832 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
833 uid_eq(ns
->owner
, cred
->euid
)) {
834 u32 id
= new_map
->extent
[0].lower_first
;
835 if (cap_setid
== CAP_SETUID
) {
836 kuid_t uid
= make_kuid(ns
->parent
, id
);
837 if (uid_eq(uid
, cred
->euid
))
839 } else if (cap_setid
== CAP_SETGID
) {
840 kgid_t gid
= make_kgid(ns
->parent
, id
);
841 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
842 gid_eq(gid
, cred
->egid
))
847 /* Allow anyone to set a mapping that doesn't require privilege */
848 if (!cap_valid(cap_setid
))
851 /* Allow the specified ids if we have the appropriate capability
852 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
853 * And the opener of the id file also had the approprpiate capability.
855 if (ns_capable(ns
->parent
, cap_setid
) &&
856 file_ns_capable(file
, ns
->parent
, cap_setid
))
862 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
864 struct user_namespace
*ns
= seq
->private;
865 unsigned long userns_flags
= ACCESS_ONCE(ns
->flags
);
867 seq_printf(seq
, "%s\n",
868 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
873 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
874 size_t count
, loff_t
*ppos
)
876 struct seq_file
*seq
= file
->private_data
;
877 struct user_namespace
*ns
= seq
->private;
879 bool setgroups_allowed
;
882 /* Only allow a very narrow range of strings to be written */
884 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
887 /* What was written? */
889 if (copy_from_user(kbuf
, buf
, count
))
894 /* What is being requested? */
896 if (strncmp(pos
, "allow", 5) == 0) {
898 setgroups_allowed
= true;
900 else if (strncmp(pos
, "deny", 4) == 0) {
902 setgroups_allowed
= false;
907 /* Verify there is not trailing junk on the line */
908 pos
= skip_spaces(pos
);
913 mutex_lock(&userns_state_mutex
);
914 if (setgroups_allowed
) {
915 /* Enabling setgroups after setgroups has been disabled
918 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
921 /* Permanently disabling setgroups after setgroups has
922 * been enabled by writing the gid_map is not allowed.
924 if (ns
->gid_map
.nr_extents
!= 0)
926 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
928 mutex_unlock(&userns_state_mutex
);
930 /* Report a successful write */
936 mutex_unlock(&userns_state_mutex
);
940 bool userns_may_setgroups(const struct user_namespace
*ns
)
944 mutex_lock(&userns_state_mutex
);
945 /* It is not safe to use setgroups until a gid mapping in
946 * the user namespace has been established.
948 allowed
= ns
->gid_map
.nr_extents
!= 0;
949 /* Is setgroups allowed? */
950 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
951 mutex_unlock(&userns_state_mutex
);
957 * Returns true if @ns is the same namespace as or a descendant of
960 bool current_in_userns(const struct user_namespace
*target_ns
)
962 struct user_namespace
*ns
;
963 for (ns
= current_user_ns(); ns
; ns
= ns
->parent
) {
969 EXPORT_SYMBOL(current_in_userns
);
971 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
973 return container_of(ns
, struct user_namespace
, ns
);
976 static struct ns_common
*userns_get(struct task_struct
*task
)
978 struct user_namespace
*user_ns
;
981 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
984 return user_ns
? &user_ns
->ns
: NULL
;
987 static void userns_put(struct ns_common
*ns
)
989 put_user_ns(to_user_ns(ns
));
992 static int userns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
994 struct user_namespace
*user_ns
= to_user_ns(ns
);
997 /* Don't allow gaining capabilities by reentering
998 * the same user namespace.
1000 if (user_ns
== current_user_ns())
1003 /* Tasks that share a thread group must share a user namespace */
1004 if (!thread_group_empty(current
))
1007 if (current
->fs
->users
!= 1)
1010 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1013 cred
= prepare_creds();
1017 put_user_ns(cred
->user_ns
);
1018 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1020 return commit_creds(cred
);
1023 const struct proc_ns_operations userns_operations
= {
1025 .type
= CLONE_NEWUSER
,
1028 .install
= userns_install
,
1031 static __init
int user_namespaces_init(void)
1033 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1036 subsys_initcall(user_namespaces_init
);