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 set_cred_user_ns(new, ns);
105 update_mnt_policy(ns);
110 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
115 if (!(unshare_flags & CLONE_NEWUSER))
118 cred = prepare_creds();
120 err = create_user_ns(cred);
130 void free_user_ns(struct user_namespace *ns)
132 struct user_namespace *parent;
136 proc_free_inum(ns->proc_inum);
137 kmem_cache_free(user_ns_cachep, ns);
139 } while (atomic_dec_and_test(&parent->count));
141 EXPORT_SYMBOL(free_user_ns);
143 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
145 unsigned idx, extents;
146 u32 first, last, id2;
148 id2 = id + count - 1;
150 /* Find the matching extent */
151 extents = map->nr_extents;
153 for (idx = 0; idx < extents; idx++) {
154 first = map->extent[idx].first;
155 last = first + map->extent[idx].count - 1;
156 if (id >= first && id <= last &&
157 (id2 >= first && id2 <= last))
160 /* Map the id or note failure */
162 id = (id - first) + map->extent[idx].lower_first;
169 static u32 map_id_down(struct uid_gid_map *map, u32 id)
171 unsigned idx, extents;
174 /* Find the matching extent */
175 extents = map->nr_extents;
177 for (idx = 0; idx < extents; idx++) {
178 first = map->extent[idx].first;
179 last = first + map->extent[idx].count - 1;
180 if (id >= first && id <= last)
183 /* Map the id or note failure */
185 id = (id - first) + map->extent[idx].lower_first;
192 static u32 map_id_up(struct uid_gid_map *map, u32 id)
194 unsigned idx, extents;
197 /* Find the matching extent */
198 extents = map->nr_extents;
200 for (idx = 0; idx < extents; idx++) {
201 first = map->extent[idx].lower_first;
202 last = first + map->extent[idx].count - 1;
203 if (id >= first && id <= last)
206 /* Map the id or note failure */
208 id = (id - first) + map->extent[idx].first;
216 * make_kuid - Map a user-namespace uid pair into a kuid.
217 * @ns: User namespace that the uid is in
218 * @uid: User identifier
220 * Maps a user-namespace uid pair into a kernel internal kuid,
221 * and returns that kuid.
223 * When there is no mapping defined for the user-namespace uid
224 * pair INVALID_UID is returned. Callers are expected to test
225 * for and handle handle INVALID_UID being returned. INVALID_UID
226 * may be tested for using uid_valid().
228 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
230 /* Map the uid to a global kernel uid */
231 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
233 EXPORT_SYMBOL(make_kuid);
236 * from_kuid - Create a uid from a kuid user-namespace pair.
237 * @targ: The user namespace we want a uid in.
238 * @kuid: The kernel internal uid to start with.
240 * Map @kuid into the user-namespace specified by @targ and
241 * return the resulting uid.
243 * There is always a mapping into the initial user_namespace.
245 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
247 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
249 /* Map the uid from a global kernel uid */
250 return map_id_up(&targ->uid_map, __kuid_val(kuid));
252 EXPORT_SYMBOL(from_kuid);
255 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
256 * @targ: The user namespace we want a uid in.
257 * @kuid: The kernel internal uid to start with.
259 * Map @kuid into the user-namespace specified by @targ and
260 * return the resulting uid.
262 * There is always a mapping into the initial user_namespace.
264 * Unlike from_kuid from_kuid_munged never fails and always
265 * returns a valid uid. This makes from_kuid_munged appropriate
266 * for use in syscalls like stat and getuid where failing the
267 * system call and failing to provide a valid uid are not an
270 * If @kuid has no mapping in @targ overflowuid is returned.
272 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
275 uid = from_kuid(targ, kuid);
277 if (uid == (uid_t) -1)
281 EXPORT_SYMBOL(from_kuid_munged);
284 * make_kgid - Map a user-namespace gid pair into a kgid.
285 * @ns: User namespace that the gid is in
286 * @uid: group identifier
288 * Maps a user-namespace gid pair into a kernel internal kgid,
289 * and returns that kgid.
291 * When there is no mapping defined for the user-namespace gid
292 * pair INVALID_GID is returned. Callers are expected to test
293 * for and handle INVALID_GID being returned. INVALID_GID may be
294 * tested for using gid_valid().
296 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
298 /* Map the gid to a global kernel gid */
299 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
301 EXPORT_SYMBOL(make_kgid);
304 * from_kgid - Create a gid from a kgid user-namespace pair.
305 * @targ: The user namespace we want a gid in.
306 * @kgid: The kernel internal gid to start with.
308 * Map @kgid into the user-namespace specified by @targ and
309 * return the resulting gid.
311 * There is always a mapping into the initial user_namespace.
313 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
315 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
317 /* Map the gid from a global kernel gid */
318 return map_id_up(&targ->gid_map, __kgid_val(kgid));
320 EXPORT_SYMBOL(from_kgid);
323 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
324 * @targ: The user namespace we want a gid in.
325 * @kgid: The kernel internal gid to start with.
327 * Map @kgid into the user-namespace specified by @targ and
328 * return the resulting gid.
330 * There is always a mapping into the initial user_namespace.
332 * Unlike from_kgid from_kgid_munged never fails and always
333 * returns a valid gid. This makes from_kgid_munged appropriate
334 * for use in syscalls like stat and getgid where failing the
335 * system call and failing to provide a valid gid are not options.
337 * If @kgid has no mapping in @targ overflowgid is returned.
339 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
342 gid = from_kgid(targ, kgid);
344 if (gid == (gid_t) -1)
348 EXPORT_SYMBOL(from_kgid_munged);
351 * make_kprojid - Map a user-namespace projid pair into a kprojid.
352 * @ns: User namespace that the projid is in
353 * @projid: Project identifier
355 * Maps a user-namespace uid pair into a kernel internal kuid,
356 * and returns that kuid.
358 * When there is no mapping defined for the user-namespace projid
359 * pair INVALID_PROJID is returned. Callers are expected to test
360 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
361 * may be tested for using projid_valid().
363 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
365 /* Map the uid to a global kernel uid */
366 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
368 EXPORT_SYMBOL(make_kprojid);
371 * from_kprojid - Create a projid from a kprojid user-namespace pair.
372 * @targ: The user namespace we want a projid in.
373 * @kprojid: The kernel internal project identifier to start with.
375 * Map @kprojid into the user-namespace specified by @targ and
376 * return the resulting projid.
378 * There is always a mapping into the initial user_namespace.
380 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
382 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
384 /* Map the uid from a global kernel uid */
385 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
387 EXPORT_SYMBOL(from_kprojid);
390 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
391 * @targ: The user namespace we want a projid in.
392 * @kprojid: The kernel internal projid to start with.
394 * Map @kprojid into the user-namespace specified by @targ and
395 * return the resulting projid.
397 * There is always a mapping into the initial user_namespace.
399 * Unlike from_kprojid from_kprojid_munged never fails and always
400 * returns a valid projid. This makes from_kprojid_munged
401 * appropriate for use in syscalls like stat and where
402 * failing the system call and failing to provide a valid projid are
405 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
407 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
410 projid = from_kprojid(targ, kprojid);
412 if (projid == (projid_t) -1)
413 projid = OVERFLOW_PROJID;
416 EXPORT_SYMBOL(from_kprojid_munged);
419 static int uid_m_show(struct seq_file *seq, void *v)
421 struct user_namespace *ns = seq->private;
422 struct uid_gid_extent *extent = v;
423 struct user_namespace *lower_ns;
426 lower_ns = seq_user_ns(seq);
427 if ((lower_ns == ns) && lower_ns->parent)
428 lower_ns = lower_ns->parent;
430 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
432 seq_printf(seq, "%10u %10u %10u\n",
440 static int gid_m_show(struct seq_file *seq, void *v)
442 struct user_namespace *ns = seq->private;
443 struct uid_gid_extent *extent = v;
444 struct user_namespace *lower_ns;
447 lower_ns = seq_user_ns(seq);
448 if ((lower_ns == ns) && lower_ns->parent)
449 lower_ns = lower_ns->parent;
451 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
453 seq_printf(seq, "%10u %10u %10u\n",
461 static int projid_m_show(struct seq_file *seq, void *v)
463 struct user_namespace *ns = seq->private;
464 struct uid_gid_extent *extent = v;
465 struct user_namespace *lower_ns;
468 lower_ns = seq_user_ns(seq);
469 if ((lower_ns == ns) && lower_ns->parent)
470 lower_ns = lower_ns->parent;
472 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
474 seq_printf(seq, "%10u %10u %10u\n",
482 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
484 struct uid_gid_extent *extent = NULL;
487 if (pos < map->nr_extents)
488 extent = &map->extent[pos];
493 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
495 struct user_namespace *ns = seq->private;
497 return m_start(seq, ppos, &ns->uid_map);
500 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
502 struct user_namespace *ns = seq->private;
504 return m_start(seq, ppos, &ns->gid_map);
507 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
509 struct user_namespace *ns = seq->private;
511 return m_start(seq, ppos, &ns->projid_map);
514 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
517 return seq->op->start(seq, pos);
520 static void m_stop(struct seq_file *seq, void *v)
525 struct seq_operations proc_uid_seq_operations = {
526 .start = uid_m_start,
532 struct seq_operations proc_gid_seq_operations = {
533 .start = gid_m_start,
539 struct seq_operations proc_projid_seq_operations = {
540 .start = projid_m_start,
543 .show = projid_m_show,
546 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
548 u32 upper_first, lower_first, upper_last, lower_last;
551 upper_first = extent->first;
552 lower_first = extent->lower_first;
553 upper_last = upper_first + extent->count - 1;
554 lower_last = lower_first + extent->count - 1;
556 for (idx = 0; idx < new_map->nr_extents; idx++) {
557 u32 prev_upper_first, prev_lower_first;
558 u32 prev_upper_last, prev_lower_last;
559 struct uid_gid_extent *prev;
561 prev = &new_map->extent[idx];
563 prev_upper_first = prev->first;
564 prev_lower_first = prev->lower_first;
565 prev_upper_last = prev_upper_first + prev->count - 1;
566 prev_lower_last = prev_lower_first + prev->count - 1;
568 /* Does the upper range intersect a previous extent? */
569 if ((prev_upper_first <= upper_last) &&
570 (prev_upper_last >= upper_first))
573 /* Does the lower range intersect a previous extent? */
574 if ((prev_lower_first <= lower_last) &&
575 (prev_lower_last >= lower_first))
581 static ssize_t map_write(struct file *file, const char __user *buf,
582 size_t count, loff_t *ppos,
584 struct uid_gid_map *map,
585 struct uid_gid_map *parent_map)
587 struct seq_file *seq = file->private_data;
588 struct user_namespace *ns = seq->private;
589 struct uid_gid_map new_map;
591 struct uid_gid_extent *extent = NULL;
592 unsigned long page = 0;
593 char *kbuf, *pos, *next_line;
594 ssize_t ret = -EINVAL;
597 * The userns_state_mutex serializes all writes to any given map.
599 * Any map is only ever written once.
601 * An id map fits within 1 cache line on most architectures.
603 * On read nothing needs to be done unless you are on an
604 * architecture with a crazy cache coherency model like alpha.
606 * There is a one time data dependency between reading the
607 * count of the extents and the values of the extents. The
608 * desired behavior is to see the values of the extents that
609 * were written before the count of the extents.
611 * To achieve this smp_wmb() is used on guarantee the write
612 * order and smp_rmb() is guaranteed that we don't have crazy
613 * architectures returning stale data.
615 mutex_lock(&userns_state_mutex);
618 /* Only allow one successful write to the map */
619 if (map->nr_extents != 0)
623 * Adjusting namespace settings requires capabilities on the target.
625 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
630 page = __get_free_page(GFP_TEMPORARY);
631 kbuf = (char *) page;
635 /* Only allow <= page size writes at the beginning of the file */
637 if ((*ppos != 0) || (count >= PAGE_SIZE))
640 /* Slurp in the user data */
642 if (copy_from_user(kbuf, buf, count))
646 /* Parse the user data */
649 new_map.nr_extents = 0;
650 for (;pos; pos = next_line) {
651 extent = &new_map.extent[new_map.nr_extents];
653 /* Find the end of line and ensure I don't look past it */
654 next_line = strchr(pos, '\n');
658 if (*next_line == '\0')
662 pos = skip_spaces(pos);
663 extent->first = simple_strtoul(pos, &pos, 10);
667 pos = skip_spaces(pos);
668 extent->lower_first = simple_strtoul(pos, &pos, 10);
672 pos = skip_spaces(pos);
673 extent->count = simple_strtoul(pos, &pos, 10);
674 if (*pos && !isspace(*pos))
677 /* Verify there is not trailing junk on the line */
678 pos = skip_spaces(pos);
682 /* Verify we have been given valid starting values */
683 if ((extent->first == (u32) -1) ||
684 (extent->lower_first == (u32) -1 ))
687 /* Verify count is not zero and does not cause the extent to wrap */
688 if ((extent->first + extent->count) <= extent->first)
690 if ((extent->lower_first + extent->count) <= extent->lower_first)
693 /* Do the ranges in extent overlap any previous extents? */
694 if (mappings_overlap(&new_map, extent))
697 new_map.nr_extents++;
699 /* Fail if the file contains too many extents */
700 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
704 /* Be very certaint the new map actually exists */
705 if (new_map.nr_extents == 0)
709 /* Validate the user is allowed to use user id's mapped to. */
710 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
713 /* Map the lower ids from the parent user namespace to the
714 * kernel global id space.
716 for (idx = 0; idx < new_map.nr_extents; idx++) {
718 extent = &new_map.extent[idx];
720 lower_first = map_id_range_down(parent_map,
724 /* Fail if we can not map the specified extent to
725 * the kernel global id space.
727 if (lower_first == (u32) -1)
730 extent->lower_first = lower_first;
733 /* Install the map */
734 memcpy(map->extent, new_map.extent,
735 new_map.nr_extents*sizeof(new_map.extent[0]));
737 map->nr_extents = new_map.nr_extents;
742 mutex_unlock(&userns_state_mutex);
748 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
750 struct seq_file *seq = file->private_data;
751 struct user_namespace *ns = seq->private;
752 struct user_namespace *seq_ns = seq_user_ns(seq);
757 if ((seq_ns != ns) && (seq_ns != ns->parent))
760 return map_write(file, buf, size, ppos, CAP_SETUID,
761 &ns->uid_map, &ns->parent->uid_map);
764 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
766 struct seq_file *seq = file->private_data;
767 struct user_namespace *ns = seq->private;
768 struct user_namespace *seq_ns = seq_user_ns(seq);
773 if ((seq_ns != ns) && (seq_ns != ns->parent))
776 return map_write(file, buf, size, ppos, CAP_SETGID,
777 &ns->gid_map, &ns->parent->gid_map);
780 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, 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 /* Anyone can set any valid project id no capability needed */
793 return map_write(file, buf, size, ppos, -1,
794 &ns->projid_map, &ns->parent->projid_map);
797 static bool new_idmap_permitted(const struct file *file,
798 struct user_namespace *ns, int cap_setid,
799 struct uid_gid_map *new_map)
801 const struct cred *cred = file->f_cred;
802 /* Don't allow mappings that would allow anything that wouldn't
803 * be allowed without the establishment of unprivileged mappings.
805 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
806 uid_eq(ns->owner, cred->euid)) {
807 u32 id = new_map->extent[0].lower_first;
808 if (cap_setid == CAP_SETUID) {
809 kuid_t uid = make_kuid(ns->parent, id);
810 if (uid_eq(uid, cred->euid))
815 /* Allow anyone to set a mapping that doesn't require privilege */
816 if (!cap_valid(cap_setid))
819 /* Allow the specified ids if we have the appropriate capability
820 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
821 * And the opener of the id file also had the approprpiate capability.
823 if (ns_capable(ns->parent, cap_setid) &&
824 file_ns_capable(file, ns->parent, cap_setid))
830 bool userns_may_setgroups(const struct user_namespace *ns)
834 mutex_lock(&userns_state_mutex);
835 /* It is not safe to use setgroups until a gid mapping in
836 * the user namespace has been established.
838 allowed = ns->gid_map.nr_extents != 0;
839 mutex_unlock(&userns_state_mutex);
844 static void *userns_get(struct task_struct *task)
846 struct user_namespace *user_ns;
849 user_ns = get_user_ns(__task_cred(task)->user_ns);
855 static void userns_put(void *ns)
860 static int userns_install(struct nsproxy *nsproxy, void *ns)
862 struct user_namespace *user_ns = ns;
865 /* Don't allow gaining capabilities by reentering
866 * the same user namespace.
868 if (user_ns == current_user_ns())
871 /* Threaded processes may not enter a different user namespace */
872 if (atomic_read(¤t->mm->mm_users) > 1)
875 if (current->fs->users != 1)
878 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
881 cred = prepare_creds();
885 put_user_ns(cred->user_ns);
886 set_cred_user_ns(cred, get_user_ns(user_ns));
888 return commit_creds(cred);
891 static unsigned int userns_inum(void *ns)
893 struct user_namespace *user_ns = ns;
894 return user_ns->proc_inum;
897 const struct proc_ns_operations userns_operations = {
899 .type = CLONE_NEWUSER,
902 .install = userns_install,
906 static __init int user_namespaces_init(void)
908 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
911 module_init(user_namespaces_init);