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;
65 if (parent_ns->level > 32)
69 * Verify that we can not violate the policy of which files
70 * may be accessed that is specified by the root directory,
71 * by verifing that the root directory is at the root of the
72 * mount namespace which allows all files to be accessed.
74 if (current_chrooted())
77 /* The creator needs a mapping in the parent user namespace
78 * or else we won't be able to reasonably tell userspace who
79 * created a user_namespace.
81 if (!kuid_has_mapping(parent_ns, owner) ||
82 !kgid_has_mapping(parent_ns, group))
85 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
89 ret = proc_alloc_inum(&ns->proc_inum);
91 kmem_cache_free(user_ns_cachep, ns);
95 atomic_set(&ns->count, 1);
96 /* Leave the new->user_ns reference with the new user namespace. */
97 ns->parent = parent_ns;
98 ns->level = parent_ns->level + 1;
102 set_cred_user_ns(new, ns);
104 update_mnt_policy(ns);
109 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
114 if (!(unshare_flags & CLONE_NEWUSER))
117 cred = prepare_creds();
119 err = create_user_ns(cred);
129 void free_user_ns(struct user_namespace *ns)
131 struct user_namespace *parent;
135 proc_free_inum(ns->proc_inum);
136 kmem_cache_free(user_ns_cachep, ns);
138 } while (atomic_dec_and_test(&parent->count));
140 EXPORT_SYMBOL(free_user_ns);
142 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
144 unsigned idx, extents;
145 u32 first, last, id2;
147 id2 = id + count - 1;
149 /* Find the matching extent */
150 extents = map->nr_extents;
151 smp_read_barrier_depends();
152 for (idx = 0; idx < extents; idx++) {
153 first = map->extent[idx].first;
154 last = first + map->extent[idx].count - 1;
155 if (id >= first && id <= last &&
156 (id2 >= first && id2 <= last))
159 /* Map the id or note failure */
161 id = (id - first) + map->extent[idx].lower_first;
168 static u32 map_id_down(struct uid_gid_map *map, u32 id)
170 unsigned idx, extents;
173 /* Find the matching extent */
174 extents = map->nr_extents;
175 smp_read_barrier_depends();
176 for (idx = 0; idx < extents; idx++) {
177 first = map->extent[idx].first;
178 last = first + map->extent[idx].count - 1;
179 if (id >= first && id <= last)
182 /* Map the id or note failure */
184 id = (id - first) + map->extent[idx].lower_first;
191 static u32 map_id_up(struct uid_gid_map *map, u32 id)
193 unsigned idx, extents;
196 /* Find the matching extent */
197 extents = map->nr_extents;
198 smp_read_barrier_depends();
199 for (idx = 0; idx < extents; idx++) {
200 first = map->extent[idx].lower_first;
201 last = first + map->extent[idx].count - 1;
202 if (id >= first && id <= last)
205 /* Map the id or note failure */
207 id = (id - first) + map->extent[idx].first;
215 * make_kuid - Map a user-namespace uid pair into a kuid.
216 * @ns: User namespace that the uid is in
217 * @uid: User identifier
219 * Maps a user-namespace uid pair into a kernel internal kuid,
220 * and returns that kuid.
222 * When there is no mapping defined for the user-namespace uid
223 * pair INVALID_UID is returned. Callers are expected to test
224 * for and handle handle INVALID_UID being returned. INVALID_UID
225 * may be tested for using uid_valid().
227 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
229 /* Map the uid to a global kernel uid */
230 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
232 EXPORT_SYMBOL(make_kuid);
235 * from_kuid - Create a uid from a kuid user-namespace pair.
236 * @targ: The user namespace we want a uid in.
237 * @kuid: The kernel internal uid to start with.
239 * Map @kuid into the user-namespace specified by @targ and
240 * return the resulting uid.
242 * There is always a mapping into the initial user_namespace.
244 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
246 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
248 /* Map the uid from a global kernel uid */
249 return map_id_up(&targ->uid_map, __kuid_val(kuid));
251 EXPORT_SYMBOL(from_kuid);
254 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
255 * @targ: The user namespace we want a uid in.
256 * @kuid: The kernel internal uid to start with.
258 * Map @kuid into the user-namespace specified by @targ and
259 * return the resulting uid.
261 * There is always a mapping into the initial user_namespace.
263 * Unlike from_kuid from_kuid_munged never fails and always
264 * returns a valid uid. This makes from_kuid_munged appropriate
265 * for use in syscalls like stat and getuid where failing the
266 * system call and failing to provide a valid uid are not an
269 * If @kuid has no mapping in @targ overflowuid is returned.
271 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
274 uid = from_kuid(targ, kuid);
276 if (uid == (uid_t) -1)
280 EXPORT_SYMBOL(from_kuid_munged);
283 * make_kgid - Map a user-namespace gid pair into a kgid.
284 * @ns: User namespace that the gid is in
285 * @uid: group identifier
287 * Maps a user-namespace gid pair into a kernel internal kgid,
288 * and returns that kgid.
290 * When there is no mapping defined for the user-namespace gid
291 * pair INVALID_GID is returned. Callers are expected to test
292 * for and handle INVALID_GID being returned. INVALID_GID may be
293 * tested for using gid_valid().
295 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
297 /* Map the gid to a global kernel gid */
298 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
300 EXPORT_SYMBOL(make_kgid);
303 * from_kgid - Create a gid from a kgid user-namespace pair.
304 * @targ: The user namespace we want a gid in.
305 * @kgid: The kernel internal gid to start with.
307 * Map @kgid into the user-namespace specified by @targ and
308 * return the resulting gid.
310 * There is always a mapping into the initial user_namespace.
312 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
314 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
316 /* Map the gid from a global kernel gid */
317 return map_id_up(&targ->gid_map, __kgid_val(kgid));
319 EXPORT_SYMBOL(from_kgid);
322 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
323 * @targ: The user namespace we want a gid in.
324 * @kgid: The kernel internal gid to start with.
326 * Map @kgid into the user-namespace specified by @targ and
327 * return the resulting gid.
329 * There is always a mapping into the initial user_namespace.
331 * Unlike from_kgid from_kgid_munged never fails and always
332 * returns a valid gid. This makes from_kgid_munged appropriate
333 * for use in syscalls like stat and getgid where failing the
334 * system call and failing to provide a valid gid are not options.
336 * If @kgid has no mapping in @targ overflowgid is returned.
338 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
341 gid = from_kgid(targ, kgid);
343 if (gid == (gid_t) -1)
347 EXPORT_SYMBOL(from_kgid_munged);
350 * make_kprojid - Map a user-namespace projid pair into a kprojid.
351 * @ns: User namespace that the projid is in
352 * @projid: Project identifier
354 * Maps a user-namespace uid pair into a kernel internal kuid,
355 * and returns that kuid.
357 * When there is no mapping defined for the user-namespace projid
358 * pair INVALID_PROJID is returned. Callers are expected to test
359 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
360 * may be tested for using projid_valid().
362 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
364 /* Map the uid to a global kernel uid */
365 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
367 EXPORT_SYMBOL(make_kprojid);
370 * from_kprojid - Create a projid from a kprojid user-namespace pair.
371 * @targ: The user namespace we want a projid in.
372 * @kprojid: The kernel internal project identifier to start with.
374 * Map @kprojid into the user-namespace specified by @targ and
375 * return the resulting projid.
377 * There is always a mapping into the initial user_namespace.
379 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
381 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
383 /* Map the uid from a global kernel uid */
384 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
386 EXPORT_SYMBOL(from_kprojid);
389 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
390 * @targ: The user namespace we want a projid in.
391 * @kprojid: The kernel internal projid to start with.
393 * Map @kprojid into the user-namespace specified by @targ and
394 * return the resulting projid.
396 * There is always a mapping into the initial user_namespace.
398 * Unlike from_kprojid from_kprojid_munged never fails and always
399 * returns a valid projid. This makes from_kprojid_munged
400 * appropriate for use in syscalls like stat and where
401 * failing the system call and failing to provide a valid projid are
404 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
406 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
409 projid = from_kprojid(targ, kprojid);
411 if (projid == (projid_t) -1)
412 projid = OVERFLOW_PROJID;
415 EXPORT_SYMBOL(from_kprojid_munged);
418 static int uid_m_show(struct seq_file *seq, void *v)
420 struct user_namespace *ns = seq->private;
421 struct uid_gid_extent *extent = v;
422 struct user_namespace *lower_ns;
425 lower_ns = seq_user_ns(seq);
426 if ((lower_ns == ns) && lower_ns->parent)
427 lower_ns = lower_ns->parent;
429 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
431 seq_printf(seq, "%10u %10u %10u\n",
439 static int gid_m_show(struct seq_file *seq, void *v)
441 struct user_namespace *ns = seq->private;
442 struct uid_gid_extent *extent = v;
443 struct user_namespace *lower_ns;
446 lower_ns = seq_user_ns(seq);
447 if ((lower_ns == ns) && lower_ns->parent)
448 lower_ns = lower_ns->parent;
450 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
452 seq_printf(seq, "%10u %10u %10u\n",
460 static int projid_m_show(struct seq_file *seq, void *v)
462 struct user_namespace *ns = seq->private;
463 struct uid_gid_extent *extent = v;
464 struct user_namespace *lower_ns;
467 lower_ns = seq_user_ns(seq);
468 if ((lower_ns == ns) && lower_ns->parent)
469 lower_ns = lower_ns->parent;
471 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
473 seq_printf(seq, "%10u %10u %10u\n",
481 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
483 struct uid_gid_extent *extent = NULL;
486 if (pos < map->nr_extents)
487 extent = &map->extent[pos];
492 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
494 struct user_namespace *ns = seq->private;
496 return m_start(seq, ppos, &ns->uid_map);
499 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
501 struct user_namespace *ns = seq->private;
503 return m_start(seq, ppos, &ns->gid_map);
506 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
508 struct user_namespace *ns = seq->private;
510 return m_start(seq, ppos, &ns->projid_map);
513 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
516 return seq->op->start(seq, pos);
519 static void m_stop(struct seq_file *seq, void *v)
524 struct seq_operations proc_uid_seq_operations = {
525 .start = uid_m_start,
531 struct seq_operations proc_gid_seq_operations = {
532 .start = gid_m_start,
538 struct seq_operations proc_projid_seq_operations = {
539 .start = projid_m_start,
542 .show = projid_m_show,
545 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
547 u32 upper_first, lower_first, upper_last, lower_last;
550 upper_first = extent->first;
551 lower_first = extent->lower_first;
552 upper_last = upper_first + extent->count - 1;
553 lower_last = lower_first + extent->count - 1;
555 for (idx = 0; idx < new_map->nr_extents; idx++) {
556 u32 prev_upper_first, prev_lower_first;
557 u32 prev_upper_last, prev_lower_last;
558 struct uid_gid_extent *prev;
560 prev = &new_map->extent[idx];
562 prev_upper_first = prev->first;
563 prev_lower_first = prev->lower_first;
564 prev_upper_last = prev_upper_first + prev->count - 1;
565 prev_lower_last = prev_lower_first + prev->count - 1;
567 /* Does the upper range intersect a previous extent? */
568 if ((prev_upper_first <= upper_last) &&
569 (prev_upper_last >= upper_first))
572 /* Does the lower range intersect a previous extent? */
573 if ((prev_lower_first <= lower_last) &&
574 (prev_lower_last >= lower_first))
581 static DEFINE_MUTEX(id_map_mutex);
583 static ssize_t map_write(struct file *file, const char __user *buf,
584 size_t count, loff_t *ppos,
586 struct uid_gid_map *map,
587 struct uid_gid_map *parent_map)
589 struct seq_file *seq = file->private_data;
590 struct user_namespace *ns = seq->private;
591 struct uid_gid_map new_map;
593 struct uid_gid_extent *extent = NULL;
594 unsigned long page = 0;
595 char *kbuf, *pos, *next_line;
596 ssize_t ret = -EINVAL;
599 * The id_map_mutex serializes all writes to any given map.
601 * Any map is only ever written once.
603 * An id map fits within 1 cache line on most architectures.
605 * On read nothing needs to be done unless you are on an
606 * architecture with a crazy cache coherency model like alpha.
608 * There is a one time data dependency between reading the
609 * count of the extents and the values of the extents. The
610 * desired behavior is to see the values of the extents that
611 * were written before the count of the extents.
613 * To achieve this smp_wmb() is used on guarantee the write
614 * order and smp_read_barrier_depends() is guaranteed that we
615 * don't have crazy architectures returning stale data.
618 mutex_lock(&id_map_mutex);
621 /* Only allow one successful write to the map */
622 if (map->nr_extents != 0)
626 * Adjusting namespace settings requires capabilities on the target.
628 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
633 page = __get_free_page(GFP_TEMPORARY);
634 kbuf = (char *) page;
638 /* Only allow <= page size writes at the beginning of the file */
640 if ((*ppos != 0) || (count >= PAGE_SIZE))
643 /* Slurp in the user data */
645 if (copy_from_user(kbuf, buf, count))
649 /* Parse the user data */
652 new_map.nr_extents = 0;
653 for (;pos; pos = next_line) {
654 extent = &new_map.extent[new_map.nr_extents];
656 /* Find the end of line and ensure I don't look past it */
657 next_line = strchr(pos, '\n');
661 if (*next_line == '\0')
665 pos = skip_spaces(pos);
666 extent->first = simple_strtoul(pos, &pos, 10);
670 pos = skip_spaces(pos);
671 extent->lower_first = simple_strtoul(pos, &pos, 10);
675 pos = skip_spaces(pos);
676 extent->count = simple_strtoul(pos, &pos, 10);
677 if (*pos && !isspace(*pos))
680 /* Verify there is not trailing junk on the line */
681 pos = skip_spaces(pos);
685 /* Verify we have been given valid starting values */
686 if ((extent->first == (u32) -1) ||
687 (extent->lower_first == (u32) -1 ))
690 /* Verify count is not zero and does not cause the extent to wrap */
691 if ((extent->first + extent->count) <= extent->first)
693 if ((extent->lower_first + extent->count) <= extent->lower_first)
696 /* Do the ranges in extent overlap any previous extents? */
697 if (mappings_overlap(&new_map, extent))
700 new_map.nr_extents++;
702 /* Fail if the file contains too many extents */
703 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
707 /* Be very certaint the new map actually exists */
708 if (new_map.nr_extents == 0)
712 /* Validate the user is allowed to use user id's mapped to. */
713 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
716 /* Map the lower ids from the parent user namespace to the
717 * kernel global id space.
719 for (idx = 0; idx < new_map.nr_extents; idx++) {
721 extent = &new_map.extent[idx];
723 lower_first = map_id_range_down(parent_map,
727 /* Fail if we can not map the specified extent to
728 * the kernel global id space.
730 if (lower_first == (u32) -1)
733 extent->lower_first = lower_first;
736 /* Install the map */
737 memcpy(map->extent, new_map.extent,
738 new_map.nr_extents*sizeof(new_map.extent[0]));
740 map->nr_extents = new_map.nr_extents;
745 mutex_unlock(&id_map_mutex);
751 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
753 struct seq_file *seq = file->private_data;
754 struct user_namespace *ns = seq->private;
755 struct user_namespace *seq_ns = seq_user_ns(seq);
760 if ((seq_ns != ns) && (seq_ns != ns->parent))
763 return map_write(file, buf, size, ppos, CAP_SETUID,
764 &ns->uid_map, &ns->parent->uid_map);
767 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
769 struct seq_file *seq = file->private_data;
770 struct user_namespace *ns = seq->private;
771 struct user_namespace *seq_ns = seq_user_ns(seq);
776 if ((seq_ns != ns) && (seq_ns != ns->parent))
779 return map_write(file, buf, size, ppos, CAP_SETGID,
780 &ns->gid_map, &ns->parent->gid_map);
783 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
785 struct seq_file *seq = file->private_data;
786 struct user_namespace *ns = seq->private;
787 struct user_namespace *seq_ns = seq_user_ns(seq);
792 if ((seq_ns != ns) && (seq_ns != ns->parent))
795 /* Anyone can set any valid project id no capability needed */
796 return map_write(file, buf, size, ppos, -1,
797 &ns->projid_map, &ns->parent->projid_map);
800 static bool new_idmap_permitted(const struct file *file,
801 struct user_namespace *ns, int cap_setid,
802 struct uid_gid_map *new_map)
804 /* Allow mapping to your own filesystem ids */
805 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
806 u32 id = new_map->extent[0].lower_first;
807 if (cap_setid == CAP_SETUID) {
808 kuid_t uid = make_kuid(ns->parent, id);
809 if (uid_eq(uid, file->f_cred->fsuid))
812 else if (cap_setid == CAP_SETGID) {
813 kgid_t gid = make_kgid(ns->parent, id);
814 if (gid_eq(gid, file->f_cred->fsgid))
819 /* Allow anyone to set a mapping that doesn't require privilege */
820 if (!cap_valid(cap_setid))
823 /* Allow the specified ids if we have the appropriate capability
824 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
825 * And the opener of the id file also had the approprpiate capability.
827 if (ns_capable(ns->parent, cap_setid) &&
828 file_ns_capable(file, ns->parent, cap_setid))
834 static void *userns_get(struct task_struct *task)
836 struct user_namespace *user_ns;
839 user_ns = get_user_ns(__task_cred(task)->user_ns);
845 static void userns_put(void *ns)
850 static int userns_install(struct nsproxy *nsproxy, void *ns)
852 struct user_namespace *user_ns = ns;
855 /* Don't allow gaining capabilities by reentering
856 * the same user namespace.
858 if (user_ns == current_user_ns())
861 /* Threaded processes may not enter a different user namespace */
862 if (atomic_read(¤t->mm->mm_users) > 1)
865 if (current->fs->users != 1)
868 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
871 cred = prepare_creds();
875 put_user_ns(cred->user_ns);
876 set_cred_user_ns(cred, get_user_ns(user_ns));
878 return commit_creds(cred);
881 static unsigned int userns_inum(void *ns)
883 struct user_namespace *user_ns = ns;
884 return user_ns->proc_inum;
887 const struct proc_ns_operations userns_operations = {
889 .type = CLONE_NEWUSER,
892 .install = userns_install,
896 static __init int user_namespaces_init(void)
898 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
901 module_init(user_namespaces_init);