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;
28 static bool new_idmap_permitted(const struct file *file,
29 struct user_namespace *ns, int cap_setid,
30 struct uid_gid_map *map);
32 static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
34 /* Start with the same capabilities as init but useless for doing
35 * anything as the capabilities are bound to the new user namespace.
37 cred->securebits = SECUREBITS_DEFAULT;
38 cred->cap_inheritable = CAP_EMPTY_SET;
39 cred->cap_permitted = CAP_FULL_SET;
40 cred->cap_effective = CAP_FULL_SET;
41 cred->cap_bset = CAP_FULL_SET;
43 key_put(cred->request_key_auth);
44 cred->request_key_auth = NULL;
46 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
47 cred->user_ns = user_ns;
51 * Create a new user namespace, deriving the creator from the user in the
52 * passed credentials, and replacing that user with the new root user for the
55 * This is called by copy_creds(), which will finish setting the target task's
58 int create_user_ns(struct cred *new)
60 struct user_namespace *ns, *parent_ns = new->user_ns;
61 kuid_t owner = new->euid;
62 kgid_t group = new->egid;
66 * Verify that we can not violate the policy of which files
67 * may be accessed that is specified by the root directory,
68 * by verifing that the root directory is at the root of the
69 * mount namespace which allows all files to be accessed.
71 if (current_chrooted())
74 /* The creator needs a mapping in the parent user namespace
75 * or else we won't be able to reasonably tell userspace who
76 * created a user_namespace.
78 if (!kuid_has_mapping(parent_ns, owner) ||
79 !kgid_has_mapping(parent_ns, group))
82 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
86 ret = proc_alloc_inum(&ns->proc_inum);
88 kmem_cache_free(user_ns_cachep, ns);
92 atomic_set(&ns->count, 1);
93 /* Leave the new->user_ns reference with the new user namespace. */
94 ns->parent = parent_ns;
98 set_cred_user_ns(new, ns);
100 update_mnt_policy(ns);
105 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
110 if (!(unshare_flags & CLONE_NEWUSER))
113 cred = prepare_creds();
115 err = create_user_ns(cred);
125 void free_user_ns(struct user_namespace *ns)
127 struct user_namespace *parent;
131 proc_free_inum(ns->proc_inum);
132 kmem_cache_free(user_ns_cachep, ns);
134 } while (atomic_dec_and_test(&parent->count));
136 EXPORT_SYMBOL(free_user_ns);
138 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
140 unsigned idx, extents;
141 u32 first, last, id2;
143 id2 = id + count - 1;
145 /* Find the matching extent */
146 extents = map->nr_extents;
147 smp_read_barrier_depends();
148 for (idx = 0; idx < extents; idx++) {
149 first = map->extent[idx].first;
150 last = first + map->extent[idx].count - 1;
151 if (id >= first && id <= last &&
152 (id2 >= first && id2 <= last))
155 /* Map the id or note failure */
157 id = (id - first) + map->extent[idx].lower_first;
164 static u32 map_id_down(struct uid_gid_map *map, u32 id)
166 unsigned idx, extents;
169 /* Find the matching extent */
170 extents = map->nr_extents;
171 smp_read_barrier_depends();
172 for (idx = 0; idx < extents; idx++) {
173 first = map->extent[idx].first;
174 last = first + map->extent[idx].count - 1;
175 if (id >= first && id <= last)
178 /* Map the id or note failure */
180 id = (id - first) + map->extent[idx].lower_first;
187 static u32 map_id_up(struct uid_gid_map *map, u32 id)
189 unsigned idx, extents;
192 /* Find the matching extent */
193 extents = map->nr_extents;
194 smp_read_barrier_depends();
195 for (idx = 0; idx < extents; idx++) {
196 first = map->extent[idx].lower_first;
197 last = first + map->extent[idx].count - 1;
198 if (id >= first && id <= last)
201 /* Map the id or note failure */
203 id = (id - first) + map->extent[idx].first;
211 * make_kuid - Map a user-namespace uid pair into a kuid.
212 * @ns: User namespace that the uid is in
213 * @uid: User identifier
215 * Maps a user-namespace uid pair into a kernel internal kuid,
216 * and returns that kuid.
218 * When there is no mapping defined for the user-namespace uid
219 * pair INVALID_UID is returned. Callers are expected to test
220 * for and handle handle INVALID_UID being returned. INVALID_UID
221 * may be tested for using uid_valid().
223 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
225 /* Map the uid to a global kernel uid */
226 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
228 EXPORT_SYMBOL(make_kuid);
231 * from_kuid - Create a uid from a kuid user-namespace pair.
232 * @targ: The user namespace we want a uid in.
233 * @kuid: The kernel internal uid to start with.
235 * Map @kuid into the user-namespace specified by @targ and
236 * return the resulting uid.
238 * There is always a mapping into the initial user_namespace.
240 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
242 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
244 /* Map the uid from a global kernel uid */
245 return map_id_up(&targ->uid_map, __kuid_val(kuid));
247 EXPORT_SYMBOL(from_kuid);
250 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
251 * @targ: The user namespace we want a uid in.
252 * @kuid: The kernel internal uid to start with.
254 * Map @kuid into the user-namespace specified by @targ and
255 * return the resulting uid.
257 * There is always a mapping into the initial user_namespace.
259 * Unlike from_kuid from_kuid_munged never fails and always
260 * returns a valid uid. This makes from_kuid_munged appropriate
261 * for use in syscalls like stat and getuid where failing the
262 * system call and failing to provide a valid uid are not an
265 * If @kuid has no mapping in @targ overflowuid is returned.
267 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
270 uid = from_kuid(targ, kuid);
272 if (uid == (uid_t) -1)
276 EXPORT_SYMBOL(from_kuid_munged);
279 * make_kgid - Map a user-namespace gid pair into a kgid.
280 * @ns: User namespace that the gid is in
281 * @uid: group identifier
283 * Maps a user-namespace gid pair into a kernel internal kgid,
284 * and returns that kgid.
286 * When there is no mapping defined for the user-namespace gid
287 * pair INVALID_GID is returned. Callers are expected to test
288 * for and handle INVALID_GID being returned. INVALID_GID may be
289 * tested for using gid_valid().
291 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
293 /* Map the gid to a global kernel gid */
294 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
296 EXPORT_SYMBOL(make_kgid);
299 * from_kgid - Create a gid from a kgid user-namespace pair.
300 * @targ: The user namespace we want a gid in.
301 * @kgid: The kernel internal gid to start with.
303 * Map @kgid into the user-namespace specified by @targ and
304 * return the resulting gid.
306 * There is always a mapping into the initial user_namespace.
308 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
310 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
312 /* Map the gid from a global kernel gid */
313 return map_id_up(&targ->gid_map, __kgid_val(kgid));
315 EXPORT_SYMBOL(from_kgid);
318 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
319 * @targ: The user namespace we want a gid in.
320 * @kgid: The kernel internal gid to start with.
322 * Map @kgid into the user-namespace specified by @targ and
323 * return the resulting gid.
325 * There is always a mapping into the initial user_namespace.
327 * Unlike from_kgid from_kgid_munged never fails and always
328 * returns a valid gid. This makes from_kgid_munged appropriate
329 * for use in syscalls like stat and getgid where failing the
330 * system call and failing to provide a valid gid are not options.
332 * If @kgid has no mapping in @targ overflowgid is returned.
334 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
337 gid = from_kgid(targ, kgid);
339 if (gid == (gid_t) -1)
343 EXPORT_SYMBOL(from_kgid_munged);
346 * make_kprojid - Map a user-namespace projid pair into a kprojid.
347 * @ns: User namespace that the projid is in
348 * @projid: Project identifier
350 * Maps a user-namespace uid pair into a kernel internal kuid,
351 * and returns that kuid.
353 * When there is no mapping defined for the user-namespace projid
354 * pair INVALID_PROJID is returned. Callers are expected to test
355 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
356 * may be tested for using projid_valid().
358 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
360 /* Map the uid to a global kernel uid */
361 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
363 EXPORT_SYMBOL(make_kprojid);
366 * from_kprojid - Create a projid from a kprojid user-namespace pair.
367 * @targ: The user namespace we want a projid in.
368 * @kprojid: The kernel internal project identifier to start with.
370 * Map @kprojid into the user-namespace specified by @targ and
371 * return the resulting projid.
373 * There is always a mapping into the initial user_namespace.
375 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
377 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
379 /* Map the uid from a global kernel uid */
380 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
382 EXPORT_SYMBOL(from_kprojid);
385 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
386 * @targ: The user namespace we want a projid in.
387 * @kprojid: The kernel internal projid to start with.
389 * Map @kprojid into the user-namespace specified by @targ and
390 * return the resulting projid.
392 * There is always a mapping into the initial user_namespace.
394 * Unlike from_kprojid from_kprojid_munged never fails and always
395 * returns a valid projid. This makes from_kprojid_munged
396 * appropriate for use in syscalls like stat and where
397 * failing the system call and failing to provide a valid projid are
400 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
402 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
405 projid = from_kprojid(targ, kprojid);
407 if (projid == (projid_t) -1)
408 projid = OVERFLOW_PROJID;
411 EXPORT_SYMBOL(from_kprojid_munged);
414 static int uid_m_show(struct seq_file *seq, void *v)
416 struct user_namespace *ns = seq->private;
417 struct uid_gid_extent *extent = v;
418 struct user_namespace *lower_ns;
421 lower_ns = seq_user_ns(seq);
422 if ((lower_ns == ns) && lower_ns->parent)
423 lower_ns = lower_ns->parent;
425 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
427 seq_printf(seq, "%10u %10u %10u\n",
435 static int gid_m_show(struct seq_file *seq, void *v)
437 struct user_namespace *ns = seq->private;
438 struct uid_gid_extent *extent = v;
439 struct user_namespace *lower_ns;
442 lower_ns = seq_user_ns(seq);
443 if ((lower_ns == ns) && lower_ns->parent)
444 lower_ns = lower_ns->parent;
446 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
448 seq_printf(seq, "%10u %10u %10u\n",
456 static int projid_m_show(struct seq_file *seq, void *v)
458 struct user_namespace *ns = seq->private;
459 struct uid_gid_extent *extent = v;
460 struct user_namespace *lower_ns;
463 lower_ns = seq_user_ns(seq);
464 if ((lower_ns == ns) && lower_ns->parent)
465 lower_ns = lower_ns->parent;
467 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
469 seq_printf(seq, "%10u %10u %10u\n",
477 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
479 struct uid_gid_extent *extent = NULL;
482 if (pos < map->nr_extents)
483 extent = &map->extent[pos];
488 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
490 struct user_namespace *ns = seq->private;
492 return m_start(seq, ppos, &ns->uid_map);
495 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
497 struct user_namespace *ns = seq->private;
499 return m_start(seq, ppos, &ns->gid_map);
502 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
504 struct user_namespace *ns = seq->private;
506 return m_start(seq, ppos, &ns->projid_map);
509 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
512 return seq->op->start(seq, pos);
515 static void m_stop(struct seq_file *seq, void *v)
520 struct seq_operations proc_uid_seq_operations = {
521 .start = uid_m_start,
527 struct seq_operations proc_gid_seq_operations = {
528 .start = gid_m_start,
534 struct seq_operations proc_projid_seq_operations = {
535 .start = projid_m_start,
538 .show = projid_m_show,
541 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
543 u32 upper_first, lower_first, upper_last, lower_last;
546 upper_first = extent->first;
547 lower_first = extent->lower_first;
548 upper_last = upper_first + extent->count - 1;
549 lower_last = lower_first + extent->count - 1;
551 for (idx = 0; idx < new_map->nr_extents; idx++) {
552 u32 prev_upper_first, prev_lower_first;
553 u32 prev_upper_last, prev_lower_last;
554 struct uid_gid_extent *prev;
556 prev = &new_map->extent[idx];
558 prev_upper_first = prev->first;
559 prev_lower_first = prev->lower_first;
560 prev_upper_last = prev_upper_first + prev->count - 1;
561 prev_lower_last = prev_lower_first + prev->count - 1;
563 /* Does the upper range intersect a previous extent? */
564 if ((prev_upper_first <= upper_last) &&
565 (prev_upper_last >= upper_first))
568 /* Does the lower range intersect a previous extent? */
569 if ((prev_lower_first <= lower_last) &&
570 (prev_lower_last >= lower_first))
577 static DEFINE_MUTEX(id_map_mutex);
579 static ssize_t map_write(struct file *file, const char __user *buf,
580 size_t count, loff_t *ppos,
582 struct uid_gid_map *map,
583 struct uid_gid_map *parent_map)
585 struct seq_file *seq = file->private_data;
586 struct user_namespace *ns = seq->private;
587 struct uid_gid_map new_map;
589 struct uid_gid_extent *extent = NULL;
590 unsigned long page = 0;
591 char *kbuf, *pos, *next_line;
592 ssize_t ret = -EINVAL;
595 * The id_map_mutex serializes all writes to any given map.
597 * Any map is only ever written once.
599 * An id map fits within 1 cache line on most architectures.
601 * On read nothing needs to be done unless you are on an
602 * architecture with a crazy cache coherency model like alpha.
604 * There is a one time data dependency between reading the
605 * count of the extents and the values of the extents. The
606 * desired behavior is to see the values of the extents that
607 * were written before the count of the extents.
609 * To achieve this smp_wmb() is used on guarantee the write
610 * order and smp_read_barrier_depends() is guaranteed that we
611 * don't have crazy architectures returning stale data.
614 mutex_lock(&id_map_mutex);
617 /* Only allow one successful write to the map */
618 if (map->nr_extents != 0)
622 * Adjusting namespace settings requires capabilities on the target.
624 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
629 page = __get_free_page(GFP_TEMPORARY);
630 kbuf = (char *) page;
634 /* Only allow <= page size writes at the beginning of the file */
636 if ((*ppos != 0) || (count >= PAGE_SIZE))
639 /* Slurp in the user data */
641 if (copy_from_user(kbuf, buf, count))
645 /* Parse the user data */
648 new_map.nr_extents = 0;
649 for (;pos; pos = next_line) {
650 extent = &new_map.extent[new_map.nr_extents];
652 /* Find the end of line and ensure I don't look past it */
653 next_line = strchr(pos, '\n');
657 if (*next_line == '\0')
661 pos = skip_spaces(pos);
662 extent->first = simple_strtoul(pos, &pos, 10);
666 pos = skip_spaces(pos);
667 extent->lower_first = simple_strtoul(pos, &pos, 10);
671 pos = skip_spaces(pos);
672 extent->count = simple_strtoul(pos, &pos, 10);
673 if (*pos && !isspace(*pos))
676 /* Verify there is not trailing junk on the line */
677 pos = skip_spaces(pos);
681 /* Verify we have been given valid starting values */
682 if ((extent->first == (u32) -1) ||
683 (extent->lower_first == (u32) -1 ))
686 /* Verify count is not zero and does not cause the extent to wrap */
687 if ((extent->first + extent->count) <= extent->first)
689 if ((extent->lower_first + extent->count) <= extent->lower_first)
692 /* Do the ranges in extent overlap any previous extents? */
693 if (mappings_overlap(&new_map, extent))
696 new_map.nr_extents++;
698 /* Fail if the file contains too many extents */
699 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
703 /* Be very certaint the new map actually exists */
704 if (new_map.nr_extents == 0)
708 /* Validate the user is allowed to use user id's mapped to. */
709 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
712 /* Map the lower ids from the parent user namespace to the
713 * kernel global id space.
715 for (idx = 0; idx < new_map.nr_extents; idx++) {
717 extent = &new_map.extent[idx];
719 lower_first = map_id_range_down(parent_map,
723 /* Fail if we can not map the specified extent to
724 * the kernel global id space.
726 if (lower_first == (u32) -1)
729 extent->lower_first = lower_first;
732 /* Install the map */
733 memcpy(map->extent, new_map.extent,
734 new_map.nr_extents*sizeof(new_map.extent[0]));
736 map->nr_extents = new_map.nr_extents;
741 mutex_unlock(&id_map_mutex);
747 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
749 struct seq_file *seq = file->private_data;
750 struct user_namespace *ns = seq->private;
751 struct user_namespace *seq_ns = seq_user_ns(seq);
756 if ((seq_ns != ns) && (seq_ns != ns->parent))
759 return map_write(file, buf, size, ppos, CAP_SETUID,
760 &ns->uid_map, &ns->parent->uid_map);
763 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, 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_SETGID,
776 &ns->gid_map, &ns->parent->gid_map);
779 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
781 struct seq_file *seq = file->private_data;
782 struct user_namespace *ns = seq->private;
783 struct user_namespace *seq_ns = seq_user_ns(seq);
788 if ((seq_ns != ns) && (seq_ns != ns->parent))
791 /* Anyone can set any valid project id no capability needed */
792 return map_write(file, buf, size, ppos, -1,
793 &ns->projid_map, &ns->parent->projid_map);
796 static bool new_idmap_permitted(const struct file *file,
797 struct user_namespace *ns, int cap_setid,
798 struct uid_gid_map *new_map)
800 /* Allow mapping to your own filesystem ids */
801 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
802 u32 id = new_map->extent[0].lower_first;
803 if (cap_setid == CAP_SETUID) {
804 kuid_t uid = make_kuid(ns->parent, id);
805 if (uid_eq(uid, file->f_cred->fsuid))
808 else if (cap_setid == CAP_SETGID) {
809 kgid_t gid = make_kgid(ns->parent, id);
810 if (gid_eq(gid, file->f_cred->fsgid))
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 static void *userns_get(struct task_struct *task)
832 struct user_namespace *user_ns;
835 user_ns = get_user_ns(__task_cred(task)->user_ns);
841 static void userns_put(void *ns)
846 static int userns_install(struct nsproxy *nsproxy, void *ns)
848 struct user_namespace *user_ns = ns;
851 /* Don't allow gaining capabilities by reentering
852 * the same user namespace.
854 if (user_ns == current_user_ns())
857 /* Threaded processes may not enter a different user namespace */
858 if (atomic_read(¤t->mm->mm_users) > 1)
861 if (current->fs->users != 1)
864 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
867 cred = prepare_creds();
871 put_user_ns(cred->user_ns);
872 set_cred_user_ns(cred, get_user_ns(user_ns));
874 return commit_creds(cred);
877 static unsigned int userns_inum(void *ns)
879 struct user_namespace *user_ns = ns;
880 return user_ns->proc_inum;
883 const struct proc_ns_operations userns_operations = {
885 .type = CLONE_NEWUSER,
888 .install = userns_install,
892 static __init int user_namespaces_init(void)
894 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
897 module_init(user_namespaces_init);