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>
27 static struct kmem_cache
*user_ns_cachep __read_mostly
;
28 static DEFINE_MUTEX(userns_state_mutex
);
30 static bool new_idmap_permitted(const struct file
*file
,
31 struct user_namespace
*ns
, int cap_setid
,
32 struct uid_gid_map
*map
);
33 static void free_user_ns(struct work_struct
*work
);
35 static struct ucounts
*inc_user_namespaces(struct user_namespace
*ns
, kuid_t uid
)
37 return inc_ucount(ns
, uid
, UCOUNT_USER_NAMESPACES
);
40 static void dec_user_namespaces(struct ucounts
*ucounts
)
42 return dec_ucount(ucounts
, UCOUNT_USER_NAMESPACES
);
45 static void set_cred_user_ns(struct cred
*cred
, struct user_namespace
*user_ns
)
47 /* Start with the same capabilities as init but useless for doing
48 * anything as the capabilities are bound to the new user namespace.
50 cred
->securebits
= SECUREBITS_DEFAULT
;
51 cred
->cap_inheritable
= CAP_EMPTY_SET
;
52 cred
->cap_permitted
= CAP_FULL_SET
;
53 cred
->cap_effective
= CAP_FULL_SET
;
54 cred
->cap_ambient
= CAP_EMPTY_SET
;
55 cred
->cap_bset
= CAP_FULL_SET
;
57 key_put(cred
->request_key_auth
);
58 cred
->request_key_auth
= NULL
;
60 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
61 cred
->user_ns
= user_ns
;
65 * Create a new user namespace, deriving the creator from the user in the
66 * passed credentials, and replacing that user with the new root user for the
69 * This is called by copy_creds(), which will finish setting the target task's
72 int create_user_ns(struct cred
*new)
74 struct user_namespace
*ns
, *parent_ns
= new->user_ns
;
75 kuid_t owner
= new->euid
;
76 kgid_t group
= new->egid
;
77 struct ucounts
*ucounts
;
81 if (parent_ns
->level
> 32)
84 ucounts
= inc_user_namespaces(parent_ns
, owner
);
89 * Verify that we can not violate the policy of which files
90 * may be accessed that is specified by the root directory,
91 * by verifing that the root directory is at the root of the
92 * mount namespace which allows all files to be accessed.
95 if (current_chrooted())
98 /* The creator needs a mapping in the parent user namespace
99 * or else we won't be able to reasonably tell userspace who
100 * created a user_namespace.
103 if (!kuid_has_mapping(parent_ns
, owner
) ||
104 !kgid_has_mapping(parent_ns
, group
))
108 ns
= kmem_cache_zalloc(user_ns_cachep
, GFP_KERNEL
);
112 ret
= ns_alloc_inum(&ns
->ns
);
115 ns
->ns
.ops
= &userns_operations
;
117 atomic_set(&ns
->count
, 1);
118 /* Leave the new->user_ns reference with the new user namespace. */
119 ns
->parent
= parent_ns
;
120 ns
->level
= parent_ns
->level
+ 1;
123 INIT_WORK(&ns
->work
, free_user_ns
);
124 for (i
= 0; i
< UCOUNT_COUNTS
; i
++) {
125 ns
->ucount_max
[i
] = INT_MAX
;
127 ns
->ucounts
= ucounts
;
129 /* Inherit USERNS_SETGROUPS_ALLOWED from our parent */
130 mutex_lock(&userns_state_mutex
);
131 ns
->flags
= parent_ns
->flags
;
132 mutex_unlock(&userns_state_mutex
);
134 #ifdef CONFIG_PERSISTENT_KEYRINGS
135 init_rwsem(&ns
->persistent_keyring_register_sem
);
138 if (!setup_userns_sysctls(ns
))
141 set_cred_user_ns(new, ns
);
144 #ifdef CONFIG_PERSISTENT_KEYRINGS
145 key_put(ns
->persistent_keyring_register
);
147 ns_free_inum(&ns
->ns
);
149 kmem_cache_free(user_ns_cachep
, ns
);
151 dec_user_namespaces(ucounts
);
156 int unshare_userns(unsigned long unshare_flags
, struct cred
**new_cred
)
161 if (!(unshare_flags
& CLONE_NEWUSER
))
164 cred
= prepare_creds();
166 err
= create_user_ns(cred
);
176 static void free_user_ns(struct work_struct
*work
)
178 struct user_namespace
*parent
, *ns
=
179 container_of(work
, struct user_namespace
, work
);
182 struct ucounts
*ucounts
= ns
->ucounts
;
184 retire_userns_sysctls(ns
);
185 #ifdef CONFIG_PERSISTENT_KEYRINGS
186 key_put(ns
->persistent_keyring_register
);
188 ns_free_inum(&ns
->ns
);
189 kmem_cache_free(user_ns_cachep
, ns
);
190 dec_user_namespaces(ucounts
);
192 } while (atomic_dec_and_test(&parent
->count
));
195 void __put_user_ns(struct user_namespace
*ns
)
197 schedule_work(&ns
->work
);
199 EXPORT_SYMBOL(__put_user_ns
);
201 static u32
map_id_range_down(struct uid_gid_map
*map
, u32 id
, u32 count
)
203 unsigned idx
, extents
;
204 u32 first
, last
, id2
;
206 id2
= id
+ count
- 1;
208 /* Find the matching extent */
209 extents
= map
->nr_extents
;
211 for (idx
= 0; idx
< extents
; idx
++) {
212 first
= map
->extent
[idx
].first
;
213 last
= first
+ map
->extent
[idx
].count
- 1;
214 if (id
>= first
&& id
<= last
&&
215 (id2
>= first
&& id2
<= last
))
218 /* Map the id or note failure */
220 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
227 static u32
map_id_down(struct uid_gid_map
*map
, u32 id
)
229 unsigned idx
, extents
;
232 /* Find the matching extent */
233 extents
= map
->nr_extents
;
235 for (idx
= 0; idx
< extents
; idx
++) {
236 first
= map
->extent
[idx
].first
;
237 last
= first
+ map
->extent
[idx
].count
- 1;
238 if (id
>= first
&& id
<= last
)
241 /* Map the id or note failure */
243 id
= (id
- first
) + map
->extent
[idx
].lower_first
;
250 static u32
map_id_up(struct uid_gid_map
*map
, u32 id
)
252 unsigned idx
, extents
;
255 /* Find the matching extent */
256 extents
= map
->nr_extents
;
258 for (idx
= 0; idx
< extents
; idx
++) {
259 first
= map
->extent
[idx
].lower_first
;
260 last
= first
+ map
->extent
[idx
].count
- 1;
261 if (id
>= first
&& id
<= last
)
264 /* Map the id or note failure */
266 id
= (id
- first
) + map
->extent
[idx
].first
;
274 * make_kuid - Map a user-namespace uid pair into a kuid.
275 * @ns: User namespace that the uid is in
276 * @uid: User identifier
278 * Maps a user-namespace uid pair into a kernel internal kuid,
279 * and returns that kuid.
281 * When there is no mapping defined for the user-namespace uid
282 * pair INVALID_UID is returned. Callers are expected to test
283 * for and handle INVALID_UID being returned. INVALID_UID
284 * may be tested for using uid_valid().
286 kuid_t
make_kuid(struct user_namespace
*ns
, uid_t uid
)
288 /* Map the uid to a global kernel uid */
289 return KUIDT_INIT(map_id_down(&ns
->uid_map
, uid
));
291 EXPORT_SYMBOL(make_kuid
);
294 * from_kuid - Create a uid from a kuid user-namespace pair.
295 * @targ: The user namespace we want a uid in.
296 * @kuid: The kernel internal uid to start with.
298 * Map @kuid into the user-namespace specified by @targ and
299 * return the resulting uid.
301 * There is always a mapping into the initial user_namespace.
303 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
305 uid_t
from_kuid(struct user_namespace
*targ
, kuid_t kuid
)
307 /* Map the uid from a global kernel uid */
308 return map_id_up(&targ
->uid_map
, __kuid_val(kuid
));
310 EXPORT_SYMBOL(from_kuid
);
313 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
314 * @targ: The user namespace we want a uid in.
315 * @kuid: The kernel internal uid to start with.
317 * Map @kuid into the user-namespace specified by @targ and
318 * return the resulting uid.
320 * There is always a mapping into the initial user_namespace.
322 * Unlike from_kuid from_kuid_munged never fails and always
323 * returns a valid uid. This makes from_kuid_munged appropriate
324 * for use in syscalls like stat and getuid where failing the
325 * system call and failing to provide a valid uid are not an
328 * If @kuid has no mapping in @targ overflowuid is returned.
330 uid_t
from_kuid_munged(struct user_namespace
*targ
, kuid_t kuid
)
333 uid
= from_kuid(targ
, kuid
);
335 if (uid
== (uid_t
) -1)
339 EXPORT_SYMBOL(from_kuid_munged
);
342 * make_kgid - Map a user-namespace gid pair into a kgid.
343 * @ns: User namespace that the gid is in
344 * @gid: group identifier
346 * Maps a user-namespace gid pair into a kernel internal kgid,
347 * and returns that kgid.
349 * When there is no mapping defined for the user-namespace gid
350 * pair INVALID_GID is returned. Callers are expected to test
351 * for and handle INVALID_GID being returned. INVALID_GID may be
352 * tested for using gid_valid().
354 kgid_t
make_kgid(struct user_namespace
*ns
, gid_t gid
)
356 /* Map the gid to a global kernel gid */
357 return KGIDT_INIT(map_id_down(&ns
->gid_map
, gid
));
359 EXPORT_SYMBOL(make_kgid
);
362 * from_kgid - Create a gid from a kgid user-namespace pair.
363 * @targ: The user namespace we want a gid in.
364 * @kgid: The kernel internal gid to start with.
366 * Map @kgid into the user-namespace specified by @targ and
367 * return the resulting gid.
369 * There is always a mapping into the initial user_namespace.
371 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
373 gid_t
from_kgid(struct user_namespace
*targ
, kgid_t kgid
)
375 /* Map the gid from a global kernel gid */
376 return map_id_up(&targ
->gid_map
, __kgid_val(kgid
));
378 EXPORT_SYMBOL(from_kgid
);
381 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
382 * @targ: The user namespace we want a gid in.
383 * @kgid: The kernel internal gid to start with.
385 * Map @kgid into the user-namespace specified by @targ and
386 * return the resulting gid.
388 * There is always a mapping into the initial user_namespace.
390 * Unlike from_kgid from_kgid_munged never fails and always
391 * returns a valid gid. This makes from_kgid_munged appropriate
392 * for use in syscalls like stat and getgid where failing the
393 * system call and failing to provide a valid gid are not options.
395 * If @kgid has no mapping in @targ overflowgid is returned.
397 gid_t
from_kgid_munged(struct user_namespace
*targ
, kgid_t kgid
)
400 gid
= from_kgid(targ
, kgid
);
402 if (gid
== (gid_t
) -1)
406 EXPORT_SYMBOL(from_kgid_munged
);
409 * make_kprojid - Map a user-namespace projid pair into a kprojid.
410 * @ns: User namespace that the projid is in
411 * @projid: Project identifier
413 * Maps a user-namespace uid pair into a kernel internal kuid,
414 * and returns that kuid.
416 * When there is no mapping defined for the user-namespace projid
417 * pair INVALID_PROJID is returned. Callers are expected to test
418 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
419 * may be tested for using projid_valid().
421 kprojid_t
make_kprojid(struct user_namespace
*ns
, projid_t projid
)
423 /* Map the uid to a global kernel uid */
424 return KPROJIDT_INIT(map_id_down(&ns
->projid_map
, projid
));
426 EXPORT_SYMBOL(make_kprojid
);
429 * from_kprojid - Create a projid from a kprojid user-namespace pair.
430 * @targ: The user namespace we want a projid in.
431 * @kprojid: The kernel internal project identifier to start with.
433 * Map @kprojid into the user-namespace specified by @targ and
434 * return the resulting projid.
436 * There is always a mapping into the initial user_namespace.
438 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
440 projid_t
from_kprojid(struct user_namespace
*targ
, kprojid_t kprojid
)
442 /* Map the uid from a global kernel uid */
443 return map_id_up(&targ
->projid_map
, __kprojid_val(kprojid
));
445 EXPORT_SYMBOL(from_kprojid
);
448 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
449 * @targ: The user namespace we want a projid in.
450 * @kprojid: The kernel internal projid to start with.
452 * Map @kprojid into the user-namespace specified by @targ and
453 * return the resulting projid.
455 * There is always a mapping into the initial user_namespace.
457 * Unlike from_kprojid from_kprojid_munged never fails and always
458 * returns a valid projid. This makes from_kprojid_munged
459 * appropriate for use in syscalls like stat and where
460 * failing the system call and failing to provide a valid projid are
463 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
465 projid_t
from_kprojid_munged(struct user_namespace
*targ
, kprojid_t kprojid
)
468 projid
= from_kprojid(targ
, kprojid
);
470 if (projid
== (projid_t
) -1)
471 projid
= OVERFLOW_PROJID
;
474 EXPORT_SYMBOL(from_kprojid_munged
);
477 static int uid_m_show(struct seq_file
*seq
, void *v
)
479 struct user_namespace
*ns
= seq
->private;
480 struct uid_gid_extent
*extent
= v
;
481 struct user_namespace
*lower_ns
;
484 lower_ns
= seq_user_ns(seq
);
485 if ((lower_ns
== ns
) && lower_ns
->parent
)
486 lower_ns
= lower_ns
->parent
;
488 lower
= from_kuid(lower_ns
, KUIDT_INIT(extent
->lower_first
));
490 seq_printf(seq
, "%10u %10u %10u\n",
498 static int gid_m_show(struct seq_file
*seq
, void *v
)
500 struct user_namespace
*ns
= seq
->private;
501 struct uid_gid_extent
*extent
= v
;
502 struct user_namespace
*lower_ns
;
505 lower_ns
= seq_user_ns(seq
);
506 if ((lower_ns
== ns
) && lower_ns
->parent
)
507 lower_ns
= lower_ns
->parent
;
509 lower
= from_kgid(lower_ns
, KGIDT_INIT(extent
->lower_first
));
511 seq_printf(seq
, "%10u %10u %10u\n",
519 static int projid_m_show(struct seq_file
*seq
, void *v
)
521 struct user_namespace
*ns
= seq
->private;
522 struct uid_gid_extent
*extent
= v
;
523 struct user_namespace
*lower_ns
;
526 lower_ns
= seq_user_ns(seq
);
527 if ((lower_ns
== ns
) && lower_ns
->parent
)
528 lower_ns
= lower_ns
->parent
;
530 lower
= from_kprojid(lower_ns
, KPROJIDT_INIT(extent
->lower_first
));
532 seq_printf(seq
, "%10u %10u %10u\n",
540 static void *m_start(struct seq_file
*seq
, loff_t
*ppos
,
541 struct uid_gid_map
*map
)
543 struct uid_gid_extent
*extent
= NULL
;
546 if (pos
< map
->nr_extents
)
547 extent
= &map
->extent
[pos
];
552 static void *uid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
554 struct user_namespace
*ns
= seq
->private;
556 return m_start(seq
, ppos
, &ns
->uid_map
);
559 static void *gid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
561 struct user_namespace
*ns
= seq
->private;
563 return m_start(seq
, ppos
, &ns
->gid_map
);
566 static void *projid_m_start(struct seq_file
*seq
, loff_t
*ppos
)
568 struct user_namespace
*ns
= seq
->private;
570 return m_start(seq
, ppos
, &ns
->projid_map
);
573 static void *m_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
576 return seq
->op
->start(seq
, pos
);
579 static void m_stop(struct seq_file
*seq
, void *v
)
584 const struct seq_operations proc_uid_seq_operations
= {
585 .start
= uid_m_start
,
591 const struct seq_operations proc_gid_seq_operations
= {
592 .start
= gid_m_start
,
598 const struct seq_operations proc_projid_seq_operations
= {
599 .start
= projid_m_start
,
602 .show
= projid_m_show
,
605 static bool mappings_overlap(struct uid_gid_map
*new_map
,
606 struct uid_gid_extent
*extent
)
608 u32 upper_first
, lower_first
, upper_last
, lower_last
;
611 upper_first
= extent
->first
;
612 lower_first
= extent
->lower_first
;
613 upper_last
= upper_first
+ extent
->count
- 1;
614 lower_last
= lower_first
+ extent
->count
- 1;
616 for (idx
= 0; idx
< new_map
->nr_extents
; idx
++) {
617 u32 prev_upper_first
, prev_lower_first
;
618 u32 prev_upper_last
, prev_lower_last
;
619 struct uid_gid_extent
*prev
;
621 prev
= &new_map
->extent
[idx
];
623 prev_upper_first
= prev
->first
;
624 prev_lower_first
= prev
->lower_first
;
625 prev_upper_last
= prev_upper_first
+ prev
->count
- 1;
626 prev_lower_last
= prev_lower_first
+ prev
->count
- 1;
628 /* Does the upper range intersect a previous extent? */
629 if ((prev_upper_first
<= upper_last
) &&
630 (prev_upper_last
>= upper_first
))
633 /* Does the lower range intersect a previous extent? */
634 if ((prev_lower_first
<= lower_last
) &&
635 (prev_lower_last
>= lower_first
))
641 static ssize_t
map_write(struct file
*file
, const char __user
*buf
,
642 size_t count
, loff_t
*ppos
,
644 struct uid_gid_map
*map
,
645 struct uid_gid_map
*parent_map
)
647 struct seq_file
*seq
= file
->private_data
;
648 struct user_namespace
*ns
= seq
->private;
649 struct uid_gid_map new_map
;
651 struct uid_gid_extent
*extent
= NULL
;
652 char *kbuf
= NULL
, *pos
, *next_line
;
653 ssize_t ret
= -EINVAL
;
656 * The userns_state_mutex serializes all writes to any given map.
658 * Any map is only ever written once.
660 * An id map fits within 1 cache line on most architectures.
662 * On read nothing needs to be done unless you are on an
663 * architecture with a crazy cache coherency model like alpha.
665 * There is a one time data dependency between reading the
666 * count of the extents and the values of the extents. The
667 * desired behavior is to see the values of the extents that
668 * were written before the count of the extents.
670 * To achieve this smp_wmb() is used on guarantee the write
671 * order and smp_rmb() is guaranteed that we don't have crazy
672 * architectures returning stale data.
674 mutex_lock(&userns_state_mutex
);
677 /* Only allow one successful write to the map */
678 if (map
->nr_extents
!= 0)
682 * Adjusting namespace settings requires capabilities on the target.
684 if (cap_valid(cap_setid
) && !file_ns_capable(file
, ns
, CAP_SYS_ADMIN
))
687 /* Only allow < page size writes at the beginning of the file */
689 if ((*ppos
!= 0) || (count
>= PAGE_SIZE
))
692 /* Slurp in the user data */
693 kbuf
= memdup_user_nul(buf
, count
);
700 /* Parse the user data */
703 new_map
.nr_extents
= 0;
704 for (; pos
; pos
= next_line
) {
705 extent
= &new_map
.extent
[new_map
.nr_extents
];
707 /* Find the end of line and ensure I don't look past it */
708 next_line
= strchr(pos
, '\n');
712 if (*next_line
== '\0')
716 pos
= skip_spaces(pos
);
717 extent
->first
= simple_strtoul(pos
, &pos
, 10);
721 pos
= skip_spaces(pos
);
722 extent
->lower_first
= simple_strtoul(pos
, &pos
, 10);
726 pos
= skip_spaces(pos
);
727 extent
->count
= simple_strtoul(pos
, &pos
, 10);
728 if (*pos
&& !isspace(*pos
))
731 /* Verify there is not trailing junk on the line */
732 pos
= skip_spaces(pos
);
736 /* Verify we have been given valid starting values */
737 if ((extent
->first
== (u32
) -1) ||
738 (extent
->lower_first
== (u32
) -1))
741 /* Verify count is not zero and does not cause the
744 if ((extent
->first
+ extent
->count
) <= extent
->first
)
746 if ((extent
->lower_first
+ extent
->count
) <=
750 /* Do the ranges in extent overlap any previous extents? */
751 if (mappings_overlap(&new_map
, extent
))
754 new_map
.nr_extents
++;
756 /* Fail if the file contains too many extents */
757 if ((new_map
.nr_extents
== UID_GID_MAP_MAX_EXTENTS
) &&
761 /* Be very certaint the new map actually exists */
762 if (new_map
.nr_extents
== 0)
766 /* Validate the user is allowed to use user id's mapped to. */
767 if (!new_idmap_permitted(file
, ns
, cap_setid
, &new_map
))
770 /* Map the lower ids from the parent user namespace to the
771 * kernel global id space.
773 for (idx
= 0; idx
< new_map
.nr_extents
; idx
++) {
775 extent
= &new_map
.extent
[idx
];
777 lower_first
= map_id_range_down(parent_map
,
781 /* Fail if we can not map the specified extent to
782 * the kernel global id space.
784 if (lower_first
== (u32
) -1)
787 extent
->lower_first
= lower_first
;
790 /* Install the map */
791 memcpy(map
->extent
, new_map
.extent
,
792 new_map
.nr_extents
*sizeof(new_map
.extent
[0]));
794 map
->nr_extents
= new_map
.nr_extents
;
799 mutex_unlock(&userns_state_mutex
);
804 ssize_t
proc_uid_map_write(struct file
*file
, const char __user
*buf
,
805 size_t size
, loff_t
*ppos
)
807 struct seq_file
*seq
= file
->private_data
;
808 struct user_namespace
*ns
= seq
->private;
809 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
814 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
817 return map_write(file
, buf
, size
, ppos
, CAP_SETUID
,
818 &ns
->uid_map
, &ns
->parent
->uid_map
);
821 ssize_t
proc_gid_map_write(struct file
*file
, const char __user
*buf
,
822 size_t size
, loff_t
*ppos
)
824 struct seq_file
*seq
= file
->private_data
;
825 struct user_namespace
*ns
= seq
->private;
826 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
831 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
834 return map_write(file
, buf
, size
, ppos
, CAP_SETGID
,
835 &ns
->gid_map
, &ns
->parent
->gid_map
);
838 ssize_t
proc_projid_map_write(struct file
*file
, const char __user
*buf
,
839 size_t size
, loff_t
*ppos
)
841 struct seq_file
*seq
= file
->private_data
;
842 struct user_namespace
*ns
= seq
->private;
843 struct user_namespace
*seq_ns
= seq_user_ns(seq
);
848 if ((seq_ns
!= ns
) && (seq_ns
!= ns
->parent
))
851 /* Anyone can set any valid project id no capability needed */
852 return map_write(file
, buf
, size
, ppos
, -1,
853 &ns
->projid_map
, &ns
->parent
->projid_map
);
856 static bool new_idmap_permitted(const struct file
*file
,
857 struct user_namespace
*ns
, int cap_setid
,
858 struct uid_gid_map
*new_map
)
860 const struct cred
*cred
= file
->f_cred
;
861 /* Don't allow mappings that would allow anything that wouldn't
862 * be allowed without the establishment of unprivileged mappings.
864 if ((new_map
->nr_extents
== 1) && (new_map
->extent
[0].count
== 1) &&
865 uid_eq(ns
->owner
, cred
->euid
)) {
866 u32 id
= new_map
->extent
[0].lower_first
;
867 if (cap_setid
== CAP_SETUID
) {
868 kuid_t uid
= make_kuid(ns
->parent
, id
);
869 if (uid_eq(uid
, cred
->euid
))
871 } else if (cap_setid
== CAP_SETGID
) {
872 kgid_t gid
= make_kgid(ns
->parent
, id
);
873 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
) &&
874 gid_eq(gid
, cred
->egid
))
879 /* Allow anyone to set a mapping that doesn't require privilege */
880 if (!cap_valid(cap_setid
))
883 /* Allow the specified ids if we have the appropriate capability
884 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
885 * And the opener of the id file also had the approprpiate capability.
887 if (ns_capable(ns
->parent
, cap_setid
) &&
888 file_ns_capable(file
, ns
->parent
, cap_setid
))
894 int proc_setgroups_show(struct seq_file
*seq
, void *v
)
896 struct user_namespace
*ns
= seq
->private;
897 unsigned long userns_flags
= ACCESS_ONCE(ns
->flags
);
899 seq_printf(seq
, "%s\n",
900 (userns_flags
& USERNS_SETGROUPS_ALLOWED
) ?
905 ssize_t
proc_setgroups_write(struct file
*file
, const char __user
*buf
,
906 size_t count
, loff_t
*ppos
)
908 struct seq_file
*seq
= file
->private_data
;
909 struct user_namespace
*ns
= seq
->private;
911 bool setgroups_allowed
;
914 /* Only allow a very narrow range of strings to be written */
916 if ((*ppos
!= 0) || (count
>= sizeof(kbuf
)))
919 /* What was written? */
921 if (copy_from_user(kbuf
, buf
, count
))
926 /* What is being requested? */
928 if (strncmp(pos
, "allow", 5) == 0) {
930 setgroups_allowed
= true;
932 else if (strncmp(pos
, "deny", 4) == 0) {
934 setgroups_allowed
= false;
939 /* Verify there is not trailing junk on the line */
940 pos
= skip_spaces(pos
);
945 mutex_lock(&userns_state_mutex
);
946 if (setgroups_allowed
) {
947 /* Enabling setgroups after setgroups has been disabled
950 if (!(ns
->flags
& USERNS_SETGROUPS_ALLOWED
))
953 /* Permanently disabling setgroups after setgroups has
954 * been enabled by writing the gid_map is not allowed.
956 if (ns
->gid_map
.nr_extents
!= 0)
958 ns
->flags
&= ~USERNS_SETGROUPS_ALLOWED
;
960 mutex_unlock(&userns_state_mutex
);
962 /* Report a successful write */
968 mutex_unlock(&userns_state_mutex
);
972 bool userns_may_setgroups(const struct user_namespace
*ns
)
976 mutex_lock(&userns_state_mutex
);
977 /* It is not safe to use setgroups until a gid mapping in
978 * the user namespace has been established.
980 allowed
= ns
->gid_map
.nr_extents
!= 0;
981 /* Is setgroups allowed? */
982 allowed
= allowed
&& (ns
->flags
& USERNS_SETGROUPS_ALLOWED
);
983 mutex_unlock(&userns_state_mutex
);
989 * Returns true if @ns is the same namespace as or a descendant of
992 bool current_in_userns(const struct user_namespace
*target_ns
)
994 struct user_namespace
*ns
;
995 for (ns
= current_user_ns(); ns
; ns
= ns
->parent
) {
1002 static inline struct user_namespace
*to_user_ns(struct ns_common
*ns
)
1004 return container_of(ns
, struct user_namespace
, ns
);
1007 static struct ns_common
*userns_get(struct task_struct
*task
)
1009 struct user_namespace
*user_ns
;
1012 user_ns
= get_user_ns(__task_cred(task
)->user_ns
);
1015 return user_ns
? &user_ns
->ns
: NULL
;
1018 static void userns_put(struct ns_common
*ns
)
1020 put_user_ns(to_user_ns(ns
));
1023 static int userns_install(struct nsproxy
*nsproxy
, struct ns_common
*ns
)
1025 struct user_namespace
*user_ns
= to_user_ns(ns
);
1028 /* Don't allow gaining capabilities by reentering
1029 * the same user namespace.
1031 if (user_ns
== current_user_ns())
1034 /* Tasks that share a thread group must share a user namespace */
1035 if (!thread_group_empty(current
))
1038 if (current
->fs
->users
!= 1)
1041 if (!ns_capable(user_ns
, CAP_SYS_ADMIN
))
1044 cred
= prepare_creds();
1048 put_user_ns(cred
->user_ns
);
1049 set_cred_user_ns(cred
, get_user_ns(user_ns
));
1051 return commit_creds(cred
);
1054 struct ns_common
*ns_get_owner(struct ns_common
*ns
)
1056 struct user_namespace
*my_user_ns
= current_user_ns();
1057 struct user_namespace
*owner
, *p
;
1059 /* See if the owner is in the current user namespace */
1060 owner
= p
= ns
->ops
->owner(ns
);
1063 return ERR_PTR(-EPERM
);
1064 if (p
== my_user_ns
)
1069 return &get_user_ns(owner
)->ns
;
1072 static struct user_namespace
*userns_owner(struct ns_common
*ns
)
1074 return to_user_ns(ns
)->parent
;
1077 const struct proc_ns_operations userns_operations
= {
1079 .type
= CLONE_NEWUSER
,
1082 .install
= userns_install
,
1083 .owner
= userns_owner
,
1084 .get_parent
= ns_get_owner
,
1087 static __init
int user_namespaces_init(void)
1089 user_ns_cachep
= KMEM_CACHE(user_namespace
, SLAB_PANIC
);
1092 subsys_initcall(user_namespaces_init
);