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>
28 * sysctl determining whether unprivileged users may unshare a new
29 * userns. Allowed by default
31 int unprivileged_userns_clone
= 1;
33 static struct kmem_cache
*user_ns_cachep __read_mostly
;
34 static DEFINE_MUTEX(userns_state_mutex
);
36 static bool new_idmap_permitted(const struct file
*file
,
37 struct user_namespace
*ns
, int cap_setid
,
38 struct uid_gid_map
*map
);
39 static void free_user_ns(struct work_struct
*work
);
41 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
43 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
46 static void dec_user_namespaces(struct ucounts
*ucounts
)
48 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
51 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
53 /* Start with the same capabilities as init but useless for doing
54 * anything as the capabilities are bound to the new user namespace.
56 cred
->securebits
= SECUREBITS_DEFAULT
;
57 cred
->cap_inheritable
= CAP_EMPTY_SET
;
58 cred
->cap_permitted
= CAP_FULL_SET
;
59 cred
->cap_effective
= CAP_FULL_SET
;
60 cred
->cap_ambient
= CAP_EMPTY_SET
;
61 cred
->cap_bset
= CAP_FULL_SET
;
63 key_put(cred
->request_key_auth
);
64 cred
->request_key_auth
= NULL
;
66 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
67 cred
->user_ns
= user_ns
;
71 * Create a new user namespace, deriving the creator from the user in the
72 * passed credentials, and replacing that user with the new root user for the
75 * This is called by copy_creds(), which will finish setting the target task's
78 int create_user_ns(struct cred
*new)
80 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
81 kuid_t owner
= new->euid
;
82 kgid_t group
= new->egid
;
83 struct ucounts
*ucounts
;
87 if (parent_ns
->level
> 32)
90 ucounts
= inc_user_namespaces(parent_ns
, owner
);
95 * Verify that we can not violate the policy of which files
96 * may be accessed that is specified by the root directory,
97 * by verifing that the root directory is at the root of the
98 * mount namespace which allows all files to be accessed.
101 if (current_chrooted())
104 /* The creator needs a mapping in the parent user namespace
105 * or else we won't be able to reasonably tell userspace who
106 * created a user_namespace.
109 if (!kuid_has_mapping(parent_ns
, owner
) ||
110 !kgid_has_mapping(parent_ns
, group
))
114 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
118 ret
= ns_alloc_inum(&ns
->ns
);
121 ns
->ns
.ops
= &userns_operations
;
123 atomic_set(&ns
->count
, 1);
124 /* Leave the new->user_ns reference with the new user namespace. */
125 ns
->parent
= parent_ns
;
126 ns
->level
= parent_ns
->level
+ 1;
129 INIT_WORK(&ns
->work
, free_user_ns
);
130 for (i
= 0; i
< UCOUNT_COUNTS
; i
++) {
131 ns
->ucount_max
[i
] = INT_MAX
;
133 ns
->ucounts
= ucounts
;
135 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
136 mutex_lock(&userns_state_mutex
);
137 ns
->flags
= parent_ns
->flags
;
138 mutex_unlock(&userns_state_mutex
);
140 #ifdef CONFIG_PERSISTENT_KEYRINGS
141 init_rwsem(&ns
->persistent_keyring_register_sem
);
144 if (!setup_userns_sysctls(ns
))
147 set_cred_user_ns(new, ns
);
150 #ifdef CONFIG_PERSISTENT_KEYRINGS
151 key_put(ns
->persistent_keyring_register
);
153 ns_free_inum(&ns
->ns
);
155 kmem_cache_free(user_ns_cachep
, ns
);
157 dec_user_namespaces(ucounts
);
162 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
167 if (!(unshare_flags
& CLONE_NEWUSER
))
170 cred
= prepare_creds();
172 err
= create_user_ns(cred
);
182 static void free_user_ns(struct work_struct
*work
)
184 struct user_namespace
*parent
, *ns
=
185 container_of(work
, struct user_namespace
, work
);
188 struct ucounts
*ucounts
= ns
->ucounts
;
190 retire_userns_sysctls(ns
);
191 #ifdef CONFIG_PERSISTENT_KEYRINGS
192 key_put(ns
->persistent_keyring_register
);
194 ns_free_inum(&ns
->ns
);
195 kmem_cache_free(user_ns_cachep
, ns
);
196 dec_user_namespaces(ucounts
);
198 } while (atomic_dec_and_test(&parent
->count
));
201 void __put_user_ns(struct user_namespace
*ns
)
203 schedule_work(&ns
->work
);
205 EXPORT_SYMBOL(__put_user_ns
);
207 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
209 unsigned idx
, extents
;
210 u32 first
, last
, id2
;
212 id2
= id
+ count
- 1;
214 /* Find the matching extent */
215 extents
= map
->nr_extents
;
217 for (idx
= 0; idx
< extents
; idx
++) {
218 first
= map
->extent
[idx
].first
;
219 last
= first
+ map
->extent
[idx
].count
- 1;
220 if (id
>= first
&& id
<= last
&&
221 (id2
>= first
&& id2
<= last
))
224 /* Map the id or note failure */
226 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
233 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
235 unsigned idx
, extents
;
238 /* Find the matching extent */
239 extents
= map
->nr_extents
;
241 for (idx
= 0; idx
< extents
; idx
++) {
242 first
= map
->extent
[idx
].first
;
243 last
= first
+ map
->extent
[idx
].count
- 1;
244 if (id
>= first
&& id
<= last
)
247 /* Map the id or note failure */
249 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
256 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
258 unsigned idx
, extents
;
261 /* Find the matching extent */
262 extents
= map
->nr_extents
;
264 for (idx
= 0; idx
< extents
; idx
++) {
265 first
= map
->extent
[idx
].lower_first
;
266 last
= first
+ map
->extent
[idx
].count
- 1;
267 if (id
>= first
&& id
<= last
)
270 /* Map the id or note failure */
272 id
= (id
- first
) + map
->extent
[idx
].first
;
280 * make_kuid - Map a user-namespace uid pair into a kuid.
281 * @ns: User namespace that the uid is in
282 * @uid: User identifier
284 * Maps a user-namespace uid pair into a kernel internal kuid,
285 * and returns that kuid.
287 * When there is no mapping defined for the user-namespace uid
288 * pair INVALID_UID is returned. Callers are expected to test
289 * for and handle INVALID_UID being returned. INVALID_UID
290 * may be tested for using uid_valid().
292 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
294 /* Map the uid to a global kernel uid */
295 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
297 EXPORT_SYMBOL(make_kuid
);
300 * from_kuid - Create a uid from a kuid user-namespace pair.
301 * @targ: The user namespace we want a uid in.
302 * @kuid: The kernel internal uid to start with.
304 * Map @kuid into the user-namespace specified by @targ and
305 * return the resulting uid.
307 * There is always a mapping into the initial user_namespace.
309 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
311 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
313 /* Map the uid from a global kernel uid */
314 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
316 EXPORT_SYMBOL(from_kuid
);
319 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
320 * @targ: The user namespace we want a uid in.
321 * @kuid: The kernel internal uid to start with.
323 * Map @kuid into the user-namespace specified by @targ and
324 * return the resulting uid.
326 * There is always a mapping into the initial user_namespace.
328 * Unlike from_kuid from_kuid_munged never fails and always
329 * returns a valid uid. This makes from_kuid_munged appropriate
330 * for use in syscalls like stat and getuid where failing the
331 * system call and failing to provide a valid uid are not an
334 * If @kuid has no mapping in @targ overflowuid is returned.
336 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
339 uid
= from_kuid(targ
, kuid
);
341 if (uid
== (uid_t
) -1)
345 EXPORT_SYMBOL(from_kuid_munged
);
348 * make_kgid - Map a user-namespace gid pair into a kgid.
349 * @ns: User namespace that the gid is in
350 * @gid: group identifier
352 * Maps a user-namespace gid pair into a kernel internal kgid,
353 * and returns that kgid.
355 * When there is no mapping defined for the user-namespace gid
356 * pair INVALID_GID is returned. Callers are expected to test
357 * for and handle INVALID_GID being returned. INVALID_GID may be
358 * tested for using gid_valid().
360 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
362 /* Map the gid to a global kernel gid */
363 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
365 EXPORT_SYMBOL(make_kgid
);
368 * from_kgid - Create a gid from a kgid user-namespace pair.
369 * @targ: The user namespace we want a gid in.
370 * @kgid: The kernel internal gid to start with.
372 * Map @kgid into the user-namespace specified by @targ and
373 * return the resulting gid.
375 * There is always a mapping into the initial user_namespace.
377 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
379 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
381 /* Map the gid from a global kernel gid */
382 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
384 EXPORT_SYMBOL(from_kgid
);
387 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
388 * @targ: The user namespace we want a gid in.
389 * @kgid: The kernel internal gid to start with.
391 * Map @kgid into the user-namespace specified by @targ and
392 * return the resulting gid.
394 * There is always a mapping into the initial user_namespace.
396 * Unlike from_kgid from_kgid_munged never fails and always
397 * returns a valid gid. This makes from_kgid_munged appropriate
398 * for use in syscalls like stat and getgid where failing the
399 * system call and failing to provide a valid gid are not options.
401 * If @kgid has no mapping in @targ overflowgid is returned.
403 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
406 gid
= from_kgid(targ
, kgid
);
408 if (gid
== (gid_t
) -1)
412 EXPORT_SYMBOL(from_kgid_munged
);
415 * make_kprojid - Map a user-namespace projid pair into a kprojid.
416 * @ns: User namespace that the projid is in
417 * @projid: Project identifier
419 * Maps a user-namespace uid pair into a kernel internal kuid,
420 * and returns that kuid.
422 * When there is no mapping defined for the user-namespace projid
423 * pair INVALID_PROJID is returned. Callers are expected to test
424 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
425 * may be tested for using projid_valid().
427 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
429 /* Map the uid to a global kernel uid */
430 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
432 EXPORT_SYMBOL(make_kprojid
);
435 * from_kprojid - Create a projid from a kprojid user-namespace pair.
436 * @targ: The user namespace we want a projid in.
437 * @kprojid: The kernel internal project identifier to start with.
439 * Map @kprojid into the user-namespace specified by @targ and
440 * return the resulting projid.
442 * There is always a mapping into the initial user_namespace.
444 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
446 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
448 /* Map the uid from a global kernel uid */
449 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
451 EXPORT_SYMBOL(from_kprojid
);
454 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
455 * @targ: The user namespace we want a projid in.
456 * @kprojid: The kernel internal projid to start with.
458 * Map @kprojid into the user-namespace specified by @targ and
459 * return the resulting projid.
461 * There is always a mapping into the initial user_namespace.
463 * Unlike from_kprojid from_kprojid_munged never fails and always
464 * returns a valid projid. This makes from_kprojid_munged
465 * appropriate for use in syscalls like stat and where
466 * failing the system call and failing to provide a valid projid are
469 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
471 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
474 projid
= from_kprojid(targ
, kprojid
);
476 if (projid
== (projid_t
) -1)
477 projid
= OVERFLOW_PROJID
;
480 EXPORT_SYMBOL(from_kprojid_munged
);
483 static int uid_m_show(struct seq_file
*seq
, void *v
)
485 struct user_namespace
*ns
= seq
->private;
486 struct uid_gid_extent
*extent
= v
;
487 struct user_namespace
*lower_ns
;
490 lower_ns
= seq_user_ns(seq
);
491 if ((lower_ns
== ns
) && lower_ns
->parent
)
492 lower_ns
= lower_ns
->parent
;
494 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
496 seq_printf(seq
, "%10u %10u %10u\n",
504 static int gid_m_show(struct seq_file
*seq
, void *v
)
506 struct user_namespace
*ns
= seq
->private;
507 struct uid_gid_extent
*extent
= v
;
508 struct user_namespace
*lower_ns
;
511 lower_ns
= seq_user_ns(seq
);
512 if ((lower_ns
== ns
) && lower_ns
->parent
)
513 lower_ns
= lower_ns
->parent
;
515 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
517 seq_printf(seq
, "%10u %10u %10u\n",
525 static int projid_m_show(struct seq_file
*seq
, void *v
)
527 struct user_namespace
*ns
= seq
->private;
528 struct uid_gid_extent
*extent
= v
;
529 struct user_namespace
*lower_ns
;
532 lower_ns
= seq_user_ns(seq
);
533 if ((lower_ns
== ns
) && lower_ns
->parent
)
534 lower_ns
= lower_ns
->parent
;
536 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
538 seq_printf(seq
, "%10u %10u %10u\n",
546 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
547 struct uid_gid_map
*map
)
549 struct uid_gid_extent
*extent
= NULL
;
552 if (pos
< map
->nr_extents
)
553 extent
= &map
->extent
[pos
];
558 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
560 struct user_namespace
*ns
= seq
->private;
562 return m_start(seq
, ppos
, &ns
->uid_map
);
565 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
567 struct user_namespace
*ns
= seq
->private;
569 return m_start(seq
, ppos
, &ns
->gid_map
);
572 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
574 struct user_namespace
*ns
= seq
->private;
576 return m_start(seq
, ppos
, &ns
->projid_map
);
579 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
582 return seq
->op
->start(seq
, pos
);
585 static void m_stop(struct seq_file
*seq
, void *v
)
590 const struct seq_operations proc_uid_seq_operations
= {
591 .start
= uid_m_start
,
597 const struct seq_operations proc_gid_seq_operations
= {
598 .start
= gid_m_start
,
604 const struct seq_operations proc_projid_seq_operations
= {
605 .start
= projid_m_start
,
608 .show
= projid_m_show
,
611 static bool mappings_overlap(struct uid_gid_map
*new_map
,
612 struct uid_gid_extent
*extent
)
614 u32 upper_first
, lower_first
, upper_last
, lower_last
;
617 upper_first
= extent
->first
;
618 lower_first
= extent
->lower_first
;
619 upper_last
= upper_first
+ extent
->count
- 1;
620 lower_last
= lower_first
+ extent
->count
- 1;
622 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
623 u32 prev_upper_first
, prev_lower_first
;
624 u32 prev_upper_last
, prev_lower_last
;
625 struct uid_gid_extent
*prev
;
627 prev
= &new_map
->extent
[idx
];
629 prev_upper_first
= prev
->first
;
630 prev_lower_first
= prev
->lower_first
;
631 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
632 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
634 /* Does the upper range intersect a previous extent? */
635 if ((prev_upper_first
<= upper_last
) &&
636 (prev_upper_last
>= upper_first
))
639 /* Does the lower range intersect a previous extent? */
640 if ((prev_lower_first
<= lower_last
) &&
641 (prev_lower_last
>= lower_first
))
647 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
648 size_t count
, loff_t
*ppos
,
650 struct uid_gid_map
*map
,
651 struct uid_gid_map
*parent_map
)
653 struct seq_file
*seq
= file
->private_data
;
654 struct user_namespace
*ns
= seq
->private;
655 struct uid_gid_map new_map
;
657 struct uid_gid_extent
*extent
= NULL
;
658 char *kbuf
= NULL
, *pos
, *next_line
;
659 ssize_t ret
= -EINVAL
;
662 * The userns_state_mutex serializes all writes to any given map.
664 * Any map is only ever written once.
666 * An id map fits within 1 cache line on most architectures.
668 * On read nothing needs to be done unless you are on an
669 * architecture with a crazy cache coherency model like alpha.
671 * There is a one time data dependency between reading the
672 * count of the extents and the values of the extents. The
673 * desired behavior is to see the values of the extents that
674 * were written before the count of the extents.
676 * To achieve this smp_wmb() is used on guarantee the write
677 * order and smp_rmb() is guaranteed that we don't have crazy
678 * architectures returning stale data.
680 mutex_lock(&userns_state_mutex
);
683 /* Only allow one successful write to the map */
684 if (map
->nr_extents
!= 0)
688 * Adjusting namespace settings requires capabilities on the target.
690 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
693 /* Only allow < page size writes at the beginning of the file */
695 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
698 /* Slurp in the user data */
699 kbuf
= memdup_user_nul(buf
, count
);
706 /* Parse the user data */
709 new_map
.nr_extents
= 0;
710 for (; pos
; pos
= next_line
) {
711 extent
= &new_map
.extent
[new_map
.nr_extents
];
713 /* Find the end of line and ensure I don't look past it */
714 next_line
= strchr(pos
, '\n');
718 if (*next_line
== '\0')
722 pos
= skip_spaces(pos
);
723 extent
->first
= simple_strtoul(pos
, &pos
, 10);
727 pos
= skip_spaces(pos
);
728 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
732 pos
= skip_spaces(pos
);
733 extent
->count
= simple_strtoul(pos
, &pos
, 10);
734 if (*pos
&& !isspace(*pos
))
737 /* Verify there is not trailing junk on the line */
738 pos
= skip_spaces(pos
);
742 /* Verify we have been given valid starting values */
743 if ((extent
->first
== (u32
) -1) ||
744 (extent
->lower_first
== (u32
) -1))
747 /* Verify count is not zero and does not cause the
750 if ((extent
->first
+ extent
->count
) <= extent
->first
)
752 if ((extent
->lower_first
+ extent
->count
) <=
756 /* Do the ranges in extent overlap any previous extents? */
757 if (mappings_overlap(&new_map
, extent
))
760 new_map
.nr_extents
++;
762 /* Fail if the file contains too many extents */
763 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
767 /* Be very certaint the new map actually exists */
768 if (new_map
.nr_extents
== 0)
772 /* Validate the user is allowed to use user id's mapped to. */
773 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
776 /* Map the lower ids from the parent user namespace to the
777 * kernel global id space.
779 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
781 extent
= &new_map
.extent
[idx
];
783 lower_first
= map_id_range_down(parent_map
,
787 /* Fail if we can not map the specified extent to
788 * the kernel global id space.
790 if (lower_first
== (u32
) -1)
793 extent
->lower_first
= lower_first
;
796 /* Install the map */
797 memcpy(map
->extent
, new_map
.extent
,
798 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
800 map
->nr_extents
= new_map
.nr_extents
;
805 mutex_unlock(&userns_state_mutex
);
810 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
811 size_t size
, loff_t
*ppos
)
813 struct seq_file
*seq
= file
->private_data
;
814 struct user_namespace
*ns
= seq
->private;
815 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
820 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
823 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
824 &ns
->uid_map
, &ns
->parent
->uid_map
);
827 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
828 size_t size
, loff_t
*ppos
)
830 struct seq_file
*seq
= file
->private_data
;
831 struct user_namespace
*ns
= seq
->private;
832 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
837 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
840 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
841 &ns
->gid_map
, &ns
->parent
->gid_map
);
844 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
845 size_t size
, loff_t
*ppos
)
847 struct seq_file
*seq
= file
->private_data
;
848 struct user_namespace
*ns
= seq
->private;
849 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
854 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
857 /* Anyone can set any valid project id no capability needed */
858 return map_write(file
, buf
, size
, ppos
, -1,
859 &ns
->projid_map
, &ns
->parent
->projid_map
);
862 static bool new_idmap_permitted(const struct file
*file
,
863 struct user_namespace
*ns
, int cap_setid
,
864 struct uid_gid_map
*new_map
)
866 const struct cred
*cred
= file
->f_cred
;
867 /* Don't allow mappings that would allow anything that wouldn't
868 * be allowed without the establishment of unprivileged mappings.
870 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
871 uid_eq(ns
->owner
, cred
->euid
)) {
872 u32 id
= new_map
->extent
[0].lower_first
;
873 if (cap_setid
== CAP_SETUID
) {
874 kuid_t uid
= make_kuid(ns
->parent
, id
);
875 if (uid_eq(uid
, cred
->euid
))
877 } else if (cap_setid
== CAP_SETGID
) {
878 kgid_t gid
= make_kgid(ns
->parent
, id
);
879 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
880 gid_eq(gid
, cred
->egid
))
885 /* Allow anyone to set a mapping that doesn't require privilege */
886 if (!cap_valid(cap_setid
))
889 /* Allow the specified ids if we have the appropriate capability
890 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
891 * And the opener of the id file also had the approprpiate capability.
893 if (ns_capable(ns
->parent
, cap_setid
) &&
894 file_ns_capable(file
, ns
->parent
, cap_setid
))
900 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
902 struct user_namespace
*ns
= seq
->private;
903 unsigned long userns_flags
= ACCESS_ONCE(ns
->flags
);
905 seq_printf(seq
, "%s\n",
906 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
911 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
912 size_t count
, loff_t
*ppos
)
914 struct seq_file
*seq
= file
->private_data
;
915 struct user_namespace
*ns
= seq
->private;
917 bool setgroups_allowed
;
920 /* Only allow a very narrow range of strings to be written */
922 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
925 /* What was written? */
927 if (copy_from_user(kbuf
, buf
, count
))
932 /* What is being requested? */
934 if (strncmp(pos
, "allow", 5) == 0) {
936 setgroups_allowed
= true;
938 else if (strncmp(pos
, "deny", 4) == 0) {
940 setgroups_allowed
= false;
945 /* Verify there is not trailing junk on the line */
946 pos
= skip_spaces(pos
);
951 mutex_lock(&userns_state_mutex
);
952 if (setgroups_allowed
) {
953 /* Enabling setgroups after setgroups has been disabled
956 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
959 /* Permanently disabling setgroups after setgroups has
960 * been enabled by writing the gid_map is not allowed.
962 if (ns
->gid_map
.nr_extents
!= 0)
964 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
966 mutex_unlock(&userns_state_mutex
);
968 /* Report a successful write */
974 mutex_unlock(&userns_state_mutex
);
978 bool userns_may_setgroups(const struct user_namespace
*ns
)
982 mutex_lock(&userns_state_mutex
);
983 /* It is not safe to use setgroups until a gid mapping in
984 * the user namespace has been established.
986 allowed
= ns
->gid_map
.nr_extents
!= 0;
987 /* Is setgroups allowed? */
988 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
989 mutex_unlock(&userns_state_mutex
);
995 * Returns true if @ns is the same namespace as or a descendant of
998 bool current_in_userns(const struct user_namespace
*target_ns
)
1000 struct user_namespace
*ns
;
1001 for (ns
= current_user_ns(); ns
; ns
= ns
->parent
) {
1002 if (ns
== target_ns
)
1007 EXPORT_SYMBOL(current_in_userns
);
1009 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1011 return container_of(ns
, struct user_namespace
, ns
);
1014 static struct ns_common
*userns_get(struct task_struct
*task
)
1016 struct user_namespace
*user_ns
;
1019 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1022 return user_ns
? &user_ns
->ns
: NULL
;
1025 static void userns_put(struct ns_common
*ns
)
1027 put_user_ns(to_user_ns(ns
));
1030 static int userns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
1032 struct user_namespace
*user_ns
= to_user_ns(ns
);
1035 /* Don't allow gaining capabilities by reentering
1036 * the same user namespace.
1038 if (user_ns
== current_user_ns())
1041 /* Tasks that share a thread group must share a user namespace */
1042 if (!thread_group_empty(current
))
1045 if (current
->fs
->users
!= 1)
1048 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1051 cred
= prepare_creds();
1055 put_user_ns(cred
->user_ns
);
1056 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1058 return commit_creds(cred
);
1061 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1063 struct user_namespace
*my_user_ns
= current_user_ns();
1064 struct user_namespace
*owner
, *p
;
1066 /* See if the owner is in the current user namespace */
1067 owner
= p
= ns
->ops
->owner(ns
);
1070 return ERR_PTR(-EPERM
);
1071 if (p
== my_user_ns
)
1076 return &get_user_ns(owner
)->ns
;
1079 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1081 return to_user_ns(ns
)->parent
;
1084 const struct proc_ns_operations userns_operations
= {
1086 .type
= CLONE_NEWUSER
,
1089 .install
= userns_install
,
1090 .owner
= userns_owner
,
1091 .get_parent
= ns_get_owner
,
1094 static __init
int user_namespaces_init(void)
1096 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1099 subsys_initcall(user_namespaces_init
);