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>
26 static struct kmem_cache
*user_ns_cachep __read_mostly
;
27 static DEFINE_MUTEX(userns_state_mutex
);
29 static bool new_idmap_permitted(const struct file
*file
,
30 struct user_namespace
*ns
, int cap_setid
,
31 struct uid_gid_map
*map
);
33 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
35 /* Start with the same capabilities as init but useless for doing
36 * anything as the capabilities are bound to the new user namespace.
38 cred
->securebits
= SECUREBITS_DEFAULT
;
39 cred
->cap_inheritable
= CAP_EMPTY_SET
;
40 cred
->cap_permitted
= CAP_FULL_SET
;
41 cred
->cap_effective
= CAP_FULL_SET
;
42 cred
->cap_bset
= CAP_FULL_SET
;
44 key_put(cred
->request_key_auth
);
45 cred
->request_key_auth
= NULL
;
47 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
48 cred
->user_ns
= user_ns
;
52 * Create a new user namespace, deriving the creator from the user in the
53 * passed credentials, and replacing that user with the new root user for the
56 * This is called by copy_creds(), which will finish setting the target task's
59 int create_user_ns(struct cred
*new)
61 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
62 kuid_t owner
= new->euid
;
63 kgid_t group
= new->egid
;
66 if (parent_ns
->level
> 32)
70 * Verify that we can not violate the policy of which files
71 * may be accessed that is specified by the root directory,
72 * by verifing that the root directory is at the root of the
73 * mount namespace which allows all files to be accessed.
75 if (current_chrooted())
78 /* The creator needs a mapping in the parent user namespace
79 * or else we won't be able to reasonably tell userspace who
80 * created a user_namespace.
82 if (!kuid_has_mapping(parent_ns
, owner
) ||
83 !kgid_has_mapping(parent_ns
, group
))
86 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
90 ret
= proc_alloc_inum(&ns
->proc_inum
);
92 kmem_cache_free(user_ns_cachep
, ns
);
96 atomic_set(&ns
->count
, 1);
97 /* Leave the new->user_ns reference with the new user namespace. */
98 ns
->parent
= parent_ns
;
99 ns
->level
= parent_ns
->level
+ 1;
103 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
104 mutex_lock(&userns_state_mutex
);
105 ns
->flags
= parent_ns
->flags
;
106 mutex_unlock(&userns_state_mutex
);
108 set_cred_user_ns(new, ns
);
110 #ifdef CONFIG_PERSISTENT_KEYRINGS
111 init_rwsem(&ns
->persistent_keyring_register_sem
);
116 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
121 if (!(unshare_flags
& CLONE_NEWUSER
))
124 cred
= prepare_creds();
126 err
= create_user_ns(cred
);
136 void free_user_ns(struct user_namespace
*ns
)
138 struct user_namespace
*parent
;
142 #ifdef CONFIG_PERSISTENT_KEYRINGS
143 key_put(ns
->persistent_keyring_register
);
145 proc_free_inum(ns
->proc_inum
);
146 kmem_cache_free(user_ns_cachep
, ns
);
148 } while (atomic_dec_and_test(&parent
->count
));
150 EXPORT_SYMBOL(free_user_ns
);
152 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
154 unsigned idx
, extents
;
155 u32 first
, last
, id2
;
157 id2
= id
+ count
- 1;
159 /* Find the matching extent */
160 extents
= map
->nr_extents
;
162 for (idx
= 0; idx
< extents
; idx
++) {
163 first
= map
->extent
[idx
].first
;
164 last
= first
+ map
->extent
[idx
].count
- 1;
165 if (id
>= first
&& id
<= last
&&
166 (id2
>= first
&& id2
<= last
))
169 /* Map the id or note failure */
171 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
178 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
180 unsigned idx
, extents
;
183 /* Find the matching extent */
184 extents
= map
->nr_extents
;
186 for (idx
= 0; idx
< extents
; idx
++) {
187 first
= map
->extent
[idx
].first
;
188 last
= first
+ map
->extent
[idx
].count
- 1;
189 if (id
>= first
&& id
<= last
)
192 /* Map the id or note failure */
194 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
201 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
203 unsigned idx
, extents
;
206 /* Find the matching extent */
207 extents
= map
->nr_extents
;
209 for (idx
= 0; idx
< extents
; idx
++) {
210 first
= map
->extent
[idx
].lower_first
;
211 last
= first
+ map
->extent
[idx
].count
- 1;
212 if (id
>= first
&& id
<= last
)
215 /* Map the id or note failure */
217 id
= (id
- first
) + map
->extent
[idx
].first
;
225 * make_kuid - Map a user-namespace uid pair into a kuid.
226 * @ns: User namespace that the uid is in
227 * @uid: User identifier
229 * Maps a user-namespace uid pair into a kernel internal kuid,
230 * and returns that kuid.
232 * When there is no mapping defined for the user-namespace uid
233 * pair INVALID_UID is returned. Callers are expected to test
234 * for and handle INVALID_UID being returned. INVALID_UID
235 * may be tested for using uid_valid().
237 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
239 /* Map the uid to a global kernel uid */
240 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
242 EXPORT_SYMBOL(make_kuid
);
245 * from_kuid - Create a uid from a kuid user-namespace pair.
246 * @targ: The user namespace we want a uid in.
247 * @kuid: The kernel internal uid to start with.
249 * Map @kuid into the user-namespace specified by @targ and
250 * return the resulting uid.
252 * There is always a mapping into the initial user_namespace.
254 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
256 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
258 /* Map the uid from a global kernel uid */
259 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
261 EXPORT_SYMBOL(from_kuid
);
264 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
265 * @targ: The user namespace we want a uid in.
266 * @kuid: The kernel internal uid to start with.
268 * Map @kuid into the user-namespace specified by @targ and
269 * return the resulting uid.
271 * There is always a mapping into the initial user_namespace.
273 * Unlike from_kuid from_kuid_munged never fails and always
274 * returns a valid uid. This makes from_kuid_munged appropriate
275 * for use in syscalls like stat and getuid where failing the
276 * system call and failing to provide a valid uid are not an
279 * If @kuid has no mapping in @targ overflowuid is returned.
281 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
284 uid
= from_kuid(targ
, kuid
);
286 if (uid
== (uid_t
) -1)
290 EXPORT_SYMBOL(from_kuid_munged
);
293 * make_kgid - Map a user-namespace gid pair into a kgid.
294 * @ns: User namespace that the gid is in
295 * @gid: group identifier
297 * Maps a user-namespace gid pair into a kernel internal kgid,
298 * and returns that kgid.
300 * When there is no mapping defined for the user-namespace gid
301 * pair INVALID_GID is returned. Callers are expected to test
302 * for and handle INVALID_GID being returned. INVALID_GID may be
303 * tested for using gid_valid().
305 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
307 /* Map the gid to a global kernel gid */
308 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
310 EXPORT_SYMBOL(make_kgid
);
313 * from_kgid - Create a gid from a kgid user-namespace pair.
314 * @targ: The user namespace we want a gid in.
315 * @kgid: The kernel internal gid to start with.
317 * Map @kgid into the user-namespace specified by @targ and
318 * return the resulting gid.
320 * There is always a mapping into the initial user_namespace.
322 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
324 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
326 /* Map the gid from a global kernel gid */
327 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
329 EXPORT_SYMBOL(from_kgid
);
332 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
333 * @targ: The user namespace we want a gid in.
334 * @kgid: The kernel internal gid to start with.
336 * Map @kgid into the user-namespace specified by @targ and
337 * return the resulting gid.
339 * There is always a mapping into the initial user_namespace.
341 * Unlike from_kgid from_kgid_munged never fails and always
342 * returns a valid gid. This makes from_kgid_munged appropriate
343 * for use in syscalls like stat and getgid where failing the
344 * system call and failing to provide a valid gid are not options.
346 * If @kgid has no mapping in @targ overflowgid is returned.
348 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
351 gid
= from_kgid(targ
, kgid
);
353 if (gid
== (gid_t
) -1)
357 EXPORT_SYMBOL(from_kgid_munged
);
360 * make_kprojid - Map a user-namespace projid pair into a kprojid.
361 * @ns: User namespace that the projid is in
362 * @projid: Project identifier
364 * Maps a user-namespace uid pair into a kernel internal kuid,
365 * and returns that kuid.
367 * When there is no mapping defined for the user-namespace projid
368 * pair INVALID_PROJID is returned. Callers are expected to test
369 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
370 * may be tested for using projid_valid().
372 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
374 /* Map the uid to a global kernel uid */
375 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
377 EXPORT_SYMBOL(make_kprojid
);
380 * from_kprojid - Create a projid from a kprojid user-namespace pair.
381 * @targ: The user namespace we want a projid in.
382 * @kprojid: The kernel internal project identifier to start with.
384 * Map @kprojid into the user-namespace specified by @targ and
385 * return the resulting projid.
387 * There is always a mapping into the initial user_namespace.
389 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
391 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
393 /* Map the uid from a global kernel uid */
394 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
396 EXPORT_SYMBOL(from_kprojid
);
399 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
400 * @targ: The user namespace we want a projid in.
401 * @kprojid: The kernel internal projid to start with.
403 * Map @kprojid into the user-namespace specified by @targ and
404 * return the resulting projid.
406 * There is always a mapping into the initial user_namespace.
408 * Unlike from_kprojid from_kprojid_munged never fails and always
409 * returns a valid projid. This makes from_kprojid_munged
410 * appropriate for use in syscalls like stat and where
411 * failing the system call and failing to provide a valid projid are
414 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
416 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
419 projid
= from_kprojid(targ
, kprojid
);
421 if (projid
== (projid_t
) -1)
422 projid
= OVERFLOW_PROJID
;
425 EXPORT_SYMBOL(from_kprojid_munged
);
428 static int uid_m_show(struct seq_file
*seq
, void *v
)
430 struct user_namespace
*ns
= seq
->private;
431 struct uid_gid_extent
*extent
= v
;
432 struct user_namespace
*lower_ns
;
435 lower_ns
= seq_user_ns(seq
);
436 if ((lower_ns
== ns
) && lower_ns
->parent
)
437 lower_ns
= lower_ns
->parent
;
439 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
441 seq_printf(seq
, "%10u %10u %10u\n",
449 static int gid_m_show(struct seq_file
*seq
, void *v
)
451 struct user_namespace
*ns
= seq
->private;
452 struct uid_gid_extent
*extent
= v
;
453 struct user_namespace
*lower_ns
;
456 lower_ns
= seq_user_ns(seq
);
457 if ((lower_ns
== ns
) && lower_ns
->parent
)
458 lower_ns
= lower_ns
->parent
;
460 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
462 seq_printf(seq
, "%10u %10u %10u\n",
470 static int projid_m_show(struct seq_file
*seq
, void *v
)
472 struct user_namespace
*ns
= seq
->private;
473 struct uid_gid_extent
*extent
= v
;
474 struct user_namespace
*lower_ns
;
477 lower_ns
= seq_user_ns(seq
);
478 if ((lower_ns
== ns
) && lower_ns
->parent
)
479 lower_ns
= lower_ns
->parent
;
481 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
483 seq_printf(seq
, "%10u %10u %10u\n",
491 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
492 struct uid_gid_map
*map
)
494 struct uid_gid_extent
*extent
= NULL
;
497 if (pos
< map
->nr_extents
)
498 extent
= &map
->extent
[pos
];
503 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
505 struct user_namespace
*ns
= seq
->private;
507 return m_start(seq
, ppos
, &ns
->uid_map
);
510 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
512 struct user_namespace
*ns
= seq
->private;
514 return m_start(seq
, ppos
, &ns
->gid_map
);
517 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
519 struct user_namespace
*ns
= seq
->private;
521 return m_start(seq
, ppos
, &ns
->projid_map
);
524 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
527 return seq
->op
->start(seq
, pos
);
530 static void m_stop(struct seq_file
*seq
, void *v
)
535 const struct seq_operations proc_uid_seq_operations
= {
536 .start
= uid_m_start
,
542 const struct seq_operations proc_gid_seq_operations
= {
543 .start
= gid_m_start
,
549 const struct seq_operations proc_projid_seq_operations
= {
550 .start
= projid_m_start
,
553 .show
= projid_m_show
,
556 static bool mappings_overlap(struct uid_gid_map
*new_map
,
557 struct uid_gid_extent
*extent
)
559 u32 upper_first
, lower_first
, upper_last
, lower_last
;
562 upper_first
= extent
->first
;
563 lower_first
= extent
->lower_first
;
564 upper_last
= upper_first
+ extent
->count
- 1;
565 lower_last
= lower_first
+ extent
->count
- 1;
567 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
568 u32 prev_upper_first
, prev_lower_first
;
569 u32 prev_upper_last
, prev_lower_last
;
570 struct uid_gid_extent
*prev
;
572 prev
= &new_map
->extent
[idx
];
574 prev_upper_first
= prev
->first
;
575 prev_lower_first
= prev
->lower_first
;
576 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
577 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
579 /* Does the upper range intersect a previous extent? */
580 if ((prev_upper_first
<= upper_last
) &&
581 (prev_upper_last
>= upper_first
))
584 /* Does the lower range intersect a previous extent? */
585 if ((prev_lower_first
<= lower_last
) &&
586 (prev_lower_last
>= lower_first
))
592 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
593 size_t count
, loff_t
*ppos
,
595 struct uid_gid_map
*map
,
596 struct uid_gid_map
*parent_map
)
598 struct seq_file
*seq
= file
->private_data
;
599 struct user_namespace
*ns
= seq
->private;
600 struct uid_gid_map new_map
;
602 struct uid_gid_extent
*extent
= NULL
;
603 unsigned long page
= 0;
604 char *kbuf
, *pos
, *next_line
;
605 ssize_t ret
= -EINVAL
;
608 * The userns_state_mutex serializes all writes to any given map.
610 * Any map is only ever written once.
612 * An id map fits within 1 cache line on most architectures.
614 * On read nothing needs to be done unless you are on an
615 * architecture with a crazy cache coherency model like alpha.
617 * There is a one time data dependency between reading the
618 * count of the extents and the values of the extents. The
619 * desired behavior is to see the values of the extents that
620 * were written before the count of the extents.
622 * To achieve this smp_wmb() is used on guarantee the write
623 * order and smp_rmb() is guaranteed that we don't have crazy
624 * architectures returning stale data.
626 mutex_lock(&userns_state_mutex
);
629 /* Only allow one successful write to the map */
630 if (map
->nr_extents
!= 0)
634 * Adjusting namespace settings requires capabilities on the target.
636 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
641 page
= __get_free_page(GFP_TEMPORARY
);
642 kbuf
= (char *) page
;
646 /* Only allow <= page size writes at the beginning of the file */
648 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
651 /* Slurp in the user data */
653 if (copy_from_user(kbuf
, buf
, count
))
657 /* Parse the user data */
660 new_map
.nr_extents
= 0;
661 for (; pos
; pos
= next_line
) {
662 extent
= &new_map
.extent
[new_map
.nr_extents
];
664 /* Find the end of line and ensure I don't look past it */
665 next_line
= strchr(pos
, '\n');
669 if (*next_line
== '\0')
673 pos
= skip_spaces(pos
);
674 extent
->first
= simple_strtoul(pos
, &pos
, 10);
678 pos
= skip_spaces(pos
);
679 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
683 pos
= skip_spaces(pos
);
684 extent
->count
= simple_strtoul(pos
, &pos
, 10);
685 if (*pos
&& !isspace(*pos
))
688 /* Verify there is not trailing junk on the line */
689 pos
= skip_spaces(pos
);
693 /* Verify we have been given valid starting values */
694 if ((extent
->first
== (u32
) -1) ||
695 (extent
->lower_first
== (u32
) -1))
698 /* Verify count is not zero and does not cause the
701 if ((extent
->first
+ extent
->count
) <= extent
->first
)
703 if ((extent
->lower_first
+ extent
->count
) <=
707 /* Do the ranges in extent overlap any previous extents? */
708 if (mappings_overlap(&new_map
, extent
))
711 new_map
.nr_extents
++;
713 /* Fail if the file contains too many extents */
714 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
718 /* Be very certaint the new map actually exists */
719 if (new_map
.nr_extents
== 0)
723 /* Validate the user is allowed to use user id's mapped to. */
724 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
727 /* Map the lower ids from the parent user namespace to the
728 * kernel global id space.
730 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
732 extent
= &new_map
.extent
[idx
];
734 lower_first
= map_id_range_down(parent_map
,
738 /* Fail if we can not map the specified extent to
739 * the kernel global id space.
741 if (lower_first
== (u32
) -1)
744 extent
->lower_first
= lower_first
;
747 /* Install the map */
748 memcpy(map
->extent
, new_map
.extent
,
749 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
751 map
->nr_extents
= new_map
.nr_extents
;
756 mutex_unlock(&userns_state_mutex
);
762 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
763 size_t size
, loff_t
*ppos
)
765 struct seq_file
*seq
= file
->private_data
;
766 struct user_namespace
*ns
= seq
->private;
767 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
772 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
775 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
776 &ns
->uid_map
, &ns
->parent
->uid_map
);
779 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
780 size_t size
, loff_t
*ppos
)
782 struct seq_file
*seq
= file
->private_data
;
783 struct user_namespace
*ns
= seq
->private;
784 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
789 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
792 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
793 &ns
->gid_map
, &ns
->parent
->gid_map
);
796 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
797 size_t size
, loff_t
*ppos
)
799 struct seq_file
*seq
= file
->private_data
;
800 struct user_namespace
*ns
= seq
->private;
801 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
806 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
809 /* Anyone can set any valid project id no capability needed */
810 return map_write(file
, buf
, size
, ppos
, -1,
811 &ns
->projid_map
, &ns
->parent
->projid_map
);
814 static bool new_idmap_permitted(const struct file
*file
,
815 struct user_namespace
*ns
, int cap_setid
,
816 struct uid_gid_map
*new_map
)
818 const struct cred
*cred
= file
->f_cred
;
819 /* Don't allow mappings that would allow anything that wouldn't
820 * be allowed without the establishment of unprivileged mappings.
822 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
823 uid_eq(ns
->owner
, cred
->euid
)) {
824 u32 id
= new_map
->extent
[0].lower_first
;
825 if (cap_setid
== CAP_SETUID
) {
826 kuid_t uid
= make_kuid(ns
->parent
, id
);
827 if (uid_eq(uid
, cred
->euid
))
829 } else if (cap_setid
== CAP_SETGID
) {
830 kgid_t gid
= make_kgid(ns
->parent
, id
);
831 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
832 gid_eq(gid
, cred
->egid
))
837 /* Allow anyone to set a mapping that doesn't require privilege */
838 if (!cap_valid(cap_setid
))
841 /* Allow the specified ids if we have the appropriate capability
842 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
843 * And the opener of the id file also had the approprpiate capability.
845 if (ns_capable(ns
->parent
, cap_setid
) &&
846 file_ns_capable(file
, ns
->parent
, cap_setid
))
852 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
854 struct user_namespace
*ns
= seq
->private;
855 unsigned long userns_flags
= ACCESS_ONCE(ns
->flags
);
857 seq_printf(seq
, "%s\n",
858 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
863 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
864 size_t count
, loff_t
*ppos
)
866 struct seq_file
*seq
= file
->private_data
;
867 struct user_namespace
*ns
= seq
->private;
869 bool setgroups_allowed
;
872 /* Only allow a very narrow range of strings to be written */
874 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
877 /* What was written? */
879 if (copy_from_user(kbuf
, buf
, count
))
884 /* What is being requested? */
886 if (strncmp(pos
, "allow", 5) == 0) {
888 setgroups_allowed
= true;
890 else if (strncmp(pos
, "deny", 4) == 0) {
892 setgroups_allowed
= false;
897 /* Verify there is not trailing junk on the line */
898 pos
= skip_spaces(pos
);
903 mutex_lock(&userns_state_mutex
);
904 if (setgroups_allowed
) {
905 /* Enabling setgroups after setgroups has been disabled
908 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
911 /* Permanently disabling setgroups after setgroups has
912 * been enabled by writing the gid_map is not allowed.
914 if (ns
->gid_map
.nr_extents
!= 0)
916 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
918 mutex_unlock(&userns_state_mutex
);
920 /* Report a successful write */
926 mutex_unlock(&userns_state_mutex
);
930 bool userns_may_setgroups(const struct user_namespace
*ns
)
934 mutex_lock(&userns_state_mutex
);
935 /* It is not safe to use setgroups until a gid mapping in
936 * the user namespace has been established.
938 allowed
= ns
->gid_map
.nr_extents
!= 0;
939 /* Is setgroups allowed? */
940 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
941 mutex_unlock(&userns_state_mutex
);
946 static void *userns_get(struct task_struct
*task
)
948 struct user_namespace
*user_ns
;
951 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
957 static void userns_put(void *ns
)
962 static int userns_install(struct nsproxy
*nsproxy
, void *ns
)
964 struct user_namespace
*user_ns
= ns
;
967 /* Don't allow gaining capabilities by reentering
968 * the same user namespace.
970 if (user_ns
== current_user_ns())
973 /* Threaded processes may not enter a different user namespace */
974 if (atomic_read(¤t
->mm
->mm_users
) > 1)
977 if (current
->fs
->users
!= 1)
980 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
983 cred
= prepare_creds();
987 put_user_ns(cred
->user_ns
);
988 set_cred_user_ns(cred
, get_user_ns(user_ns
));
990 return commit_creds(cred
);
993 static unsigned int userns_inum(void *ns
)
995 struct user_namespace
*user_ns
= ns
;
996 return user_ns
->proc_inum
;
999 const struct proc_ns_operations userns_operations
= {
1001 .type
= CLONE_NEWUSER
,
1004 .install
= userns_install
,
1005 .inum
= userns_inum
,
1008 static __init
int user_namespaces_init(void)
1010 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1013 subsys_initcall(user_namespaces_init
);