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_fs.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>
25 static struct kmem_cache *user_ns_cachep __read_mostly;
27 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
28 struct uid_gid_map *map);
30 static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
32 /* Start with the same capabilities as init but useless for doing
33 * anything as the capabilities are bound to the new user namespace.
35 cred->securebits = SECUREBITS_DEFAULT;
36 cred->cap_inheritable = CAP_EMPTY_SET;
37 cred->cap_permitted = CAP_FULL_SET;
38 cred->cap_effective = CAP_FULL_SET;
39 cred->cap_bset = CAP_FULL_SET;
41 key_put(cred->request_key_auth);
42 cred->request_key_auth = NULL;
44 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
45 cred->user_ns = user_ns;
49 * Create a new user namespace, deriving the creator from the user in the
50 * passed credentials, and replacing that user with the new root user for the
53 * This is called by copy_creds(), which will finish setting the target task's
56 int create_user_ns(struct cred *new)
58 struct user_namespace *ns, *parent_ns = new->user_ns;
59 kuid_t owner = new->euid;
60 kgid_t group = new->egid;
63 /* The creator needs a mapping in the parent user namespace
64 * or else we won't be able to reasonably tell userspace who
65 * created a user_namespace.
67 if (!kuid_has_mapping(parent_ns, owner) ||
68 !kgid_has_mapping(parent_ns, group))
71 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
75 ret = proc_alloc_inum(&ns->proc_inum);
77 kmem_cache_free(user_ns_cachep, ns);
81 atomic_set(&ns->count, 1);
82 /* Leave the new->user_ns reference with the new user namespace. */
83 ns->parent = parent_ns;
87 set_cred_user_ns(new, ns);
92 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
96 if (!(unshare_flags & CLONE_NEWUSER))
99 cred = prepare_creds();
104 return create_user_ns(cred);
107 void free_user_ns(struct user_namespace *ns)
109 struct user_namespace *parent;
113 proc_free_inum(ns->proc_inum);
114 kmem_cache_free(user_ns_cachep, ns);
116 } while (atomic_dec_and_test(&parent->count));
118 EXPORT_SYMBOL(free_user_ns);
120 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
122 unsigned idx, extents;
123 u32 first, last, id2;
125 id2 = id + count - 1;
127 /* Find the matching extent */
128 extents = map->nr_extents;
129 smp_read_barrier_depends();
130 for (idx = 0; idx < extents; idx++) {
131 first = map->extent[idx].first;
132 last = first + map->extent[idx].count - 1;
133 if (id >= first && id <= last &&
134 (id2 >= first && id2 <= last))
137 /* Map the id or note failure */
139 id = (id - first) + map->extent[idx].lower_first;
146 static u32 map_id_down(struct uid_gid_map *map, u32 id)
148 unsigned idx, extents;
151 /* Find the matching extent */
152 extents = map->nr_extents;
153 smp_read_barrier_depends();
154 for (idx = 0; idx < extents; idx++) {
155 first = map->extent[idx].first;
156 last = first + map->extent[idx].count - 1;
157 if (id >= first && id <= last)
160 /* Map the id or note failure */
162 id = (id - first) + map->extent[idx].lower_first;
169 static u32 map_id_up(struct uid_gid_map *map, u32 id)
171 unsigned idx, extents;
174 /* Find the matching extent */
175 extents = map->nr_extents;
176 smp_read_barrier_depends();
177 for (idx = 0; idx < extents; idx++) {
178 first = map->extent[idx].lower_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].first;
193 * make_kuid - Map a user-namespace uid pair into a kuid.
194 * @ns: User namespace that the uid is in
195 * @uid: User identifier
197 * Maps a user-namespace uid pair into a kernel internal kuid,
198 * and returns that kuid.
200 * When there is no mapping defined for the user-namespace uid
201 * pair INVALID_UID is returned. Callers are expected to test
202 * for and handle handle INVALID_UID being returned. INVALID_UID
203 * may be tested for using uid_valid().
205 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
207 /* Map the uid to a global kernel uid */
208 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
210 EXPORT_SYMBOL(make_kuid);
213 * from_kuid - Create a uid from a kuid user-namespace pair.
214 * @targ: The user namespace we want a uid in.
215 * @kuid: The kernel internal uid to start with.
217 * Map @kuid into the user-namespace specified by @targ and
218 * return the resulting uid.
220 * There is always a mapping into the initial user_namespace.
222 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
224 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
226 /* Map the uid from a global kernel uid */
227 return map_id_up(&targ->uid_map, __kuid_val(kuid));
229 EXPORT_SYMBOL(from_kuid);
232 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
233 * @targ: The user namespace we want a uid in.
234 * @kuid: The kernel internal uid to start with.
236 * Map @kuid into the user-namespace specified by @targ and
237 * return the resulting uid.
239 * There is always a mapping into the initial user_namespace.
241 * Unlike from_kuid from_kuid_munged never fails and always
242 * returns a valid uid. This makes from_kuid_munged appropriate
243 * for use in syscalls like stat and getuid where failing the
244 * system call and failing to provide a valid uid are not an
247 * If @kuid has no mapping in @targ overflowuid is returned.
249 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
252 uid = from_kuid(targ, kuid);
254 if (uid == (uid_t) -1)
258 EXPORT_SYMBOL(from_kuid_munged);
261 * make_kgid - Map a user-namespace gid pair into a kgid.
262 * @ns: User namespace that the gid is in
263 * @uid: group identifier
265 * Maps a user-namespace gid pair into a kernel internal kgid,
266 * and returns that kgid.
268 * When there is no mapping defined for the user-namespace gid
269 * pair INVALID_GID is returned. Callers are expected to test
270 * for and handle INVALID_GID being returned. INVALID_GID may be
271 * tested for using gid_valid().
273 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
275 /* Map the gid to a global kernel gid */
276 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
278 EXPORT_SYMBOL(make_kgid);
281 * from_kgid - Create a gid from a kgid user-namespace pair.
282 * @targ: The user namespace we want a gid in.
283 * @kgid: The kernel internal gid to start with.
285 * Map @kgid into the user-namespace specified by @targ and
286 * return the resulting gid.
288 * There is always a mapping into the initial user_namespace.
290 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
292 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
294 /* Map the gid from a global kernel gid */
295 return map_id_up(&targ->gid_map, __kgid_val(kgid));
297 EXPORT_SYMBOL(from_kgid);
300 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
301 * @targ: The user namespace we want a gid in.
302 * @kgid: The kernel internal gid to start with.
304 * Map @kgid into the user-namespace specified by @targ and
305 * return the resulting gid.
307 * There is always a mapping into the initial user_namespace.
309 * Unlike from_kgid from_kgid_munged never fails and always
310 * returns a valid gid. This makes from_kgid_munged appropriate
311 * for use in syscalls like stat and getgid where failing the
312 * system call and failing to provide a valid gid are not options.
314 * If @kgid has no mapping in @targ overflowgid is returned.
316 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
319 gid = from_kgid(targ, kgid);
321 if (gid == (gid_t) -1)
325 EXPORT_SYMBOL(from_kgid_munged);
328 * make_kprojid - Map a user-namespace projid pair into a kprojid.
329 * @ns: User namespace that the projid is in
330 * @projid: Project identifier
332 * Maps a user-namespace uid pair into a kernel internal kuid,
333 * and returns that kuid.
335 * When there is no mapping defined for the user-namespace projid
336 * pair INVALID_PROJID is returned. Callers are expected to test
337 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
338 * may be tested for using projid_valid().
340 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
342 /* Map the uid to a global kernel uid */
343 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
345 EXPORT_SYMBOL(make_kprojid);
348 * from_kprojid - Create a projid from a kprojid user-namespace pair.
349 * @targ: The user namespace we want a projid in.
350 * @kprojid: The kernel internal project identifier to start with.
352 * Map @kprojid into the user-namespace specified by @targ and
353 * return the resulting projid.
355 * There is always a mapping into the initial user_namespace.
357 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
359 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
361 /* Map the uid from a global kernel uid */
362 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
364 EXPORT_SYMBOL(from_kprojid);
367 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
368 * @targ: The user namespace we want a projid in.
369 * @kprojid: The kernel internal projid to start with.
371 * Map @kprojid into the user-namespace specified by @targ and
372 * return the resulting projid.
374 * There is always a mapping into the initial user_namespace.
376 * Unlike from_kprojid from_kprojid_munged never fails and always
377 * returns a valid projid. This makes from_kprojid_munged
378 * appropriate for use in syscalls like stat and where
379 * failing the system call and failing to provide a valid projid are
382 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
384 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
387 projid = from_kprojid(targ, kprojid);
389 if (projid == (projid_t) -1)
390 projid = OVERFLOW_PROJID;
393 EXPORT_SYMBOL(from_kprojid_munged);
396 static int uid_m_show(struct seq_file *seq, void *v)
398 struct user_namespace *ns = seq->private;
399 struct uid_gid_extent *extent = v;
400 struct user_namespace *lower_ns;
403 lower_ns = seq_user_ns(seq);
404 if ((lower_ns == ns) && lower_ns->parent)
405 lower_ns = lower_ns->parent;
407 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
409 seq_printf(seq, "%10u %10u %10u\n",
417 static int gid_m_show(struct seq_file *seq, void *v)
419 struct user_namespace *ns = seq->private;
420 struct uid_gid_extent *extent = v;
421 struct user_namespace *lower_ns;
424 lower_ns = seq_user_ns(seq);
425 if ((lower_ns == ns) && lower_ns->parent)
426 lower_ns = lower_ns->parent;
428 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
430 seq_printf(seq, "%10u %10u %10u\n",
438 static int projid_m_show(struct seq_file *seq, void *v)
440 struct user_namespace *ns = seq->private;
441 struct uid_gid_extent *extent = v;
442 struct user_namespace *lower_ns;
445 lower_ns = seq_user_ns(seq);
446 if ((lower_ns == ns) && lower_ns->parent)
447 lower_ns = lower_ns->parent;
449 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
451 seq_printf(seq, "%10u %10u %10u\n",
459 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
461 struct uid_gid_extent *extent = NULL;
464 if (pos < map->nr_extents)
465 extent = &map->extent[pos];
470 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
472 struct user_namespace *ns = seq->private;
474 return m_start(seq, ppos, &ns->uid_map);
477 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
479 struct user_namespace *ns = seq->private;
481 return m_start(seq, ppos, &ns->gid_map);
484 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
486 struct user_namespace *ns = seq->private;
488 return m_start(seq, ppos, &ns->projid_map);
491 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
494 return seq->op->start(seq, pos);
497 static void m_stop(struct seq_file *seq, void *v)
502 struct seq_operations proc_uid_seq_operations = {
503 .start = uid_m_start,
509 struct seq_operations proc_gid_seq_operations = {
510 .start = gid_m_start,
516 struct seq_operations proc_projid_seq_operations = {
517 .start = projid_m_start,
520 .show = projid_m_show,
523 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
525 u32 upper_first, lower_first, upper_last, lower_last;
528 upper_first = extent->first;
529 lower_first = extent->lower_first;
530 upper_last = upper_first + extent->count - 1;
531 lower_last = lower_first + extent->count - 1;
533 for (idx = 0; idx < new_map->nr_extents; idx++) {
534 u32 prev_upper_first, prev_lower_first;
535 u32 prev_upper_last, prev_lower_last;
536 struct uid_gid_extent *prev;
538 prev = &new_map->extent[idx];
540 prev_upper_first = prev->first;
541 prev_lower_first = prev->lower_first;
542 prev_upper_last = prev_upper_first + prev->count - 1;
543 prev_lower_last = prev_lower_first + prev->count - 1;
545 /* Does the upper range intersect a previous extent? */
546 if ((prev_upper_first <= upper_last) &&
547 (prev_upper_last >= upper_first))
550 /* Does the lower range intersect a previous extent? */
551 if ((prev_lower_first <= lower_last) &&
552 (prev_lower_last >= lower_first))
559 static DEFINE_MUTEX(id_map_mutex);
561 static ssize_t map_write(struct file *file, const char __user *buf,
562 size_t count, loff_t *ppos,
564 struct uid_gid_map *map,
565 struct uid_gid_map *parent_map)
567 struct seq_file *seq = file->private_data;
568 struct user_namespace *ns = seq->private;
569 struct uid_gid_map new_map;
571 struct uid_gid_extent *extent = NULL;
572 unsigned long page = 0;
573 char *kbuf, *pos, *next_line;
574 ssize_t ret = -EINVAL;
577 * The id_map_mutex serializes all writes to any given map.
579 * Any map is only ever written once.
581 * An id map fits within 1 cache line on most architectures.
583 * On read nothing needs to be done unless you are on an
584 * architecture with a crazy cache coherency model like alpha.
586 * There is a one time data dependency between reading the
587 * count of the extents and the values of the extents. The
588 * desired behavior is to see the values of the extents that
589 * were written before the count of the extents.
591 * To achieve this smp_wmb() is used on guarantee the write
592 * order and smp_read_barrier_depends() is guaranteed that we
593 * don't have crazy architectures returning stale data.
596 mutex_lock(&id_map_mutex);
599 /* Only allow one successful write to the map */
600 if (map->nr_extents != 0)
603 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
604 * over the user namespace in order to set the id mapping.
606 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
611 page = __get_free_page(GFP_TEMPORARY);
612 kbuf = (char *) page;
616 /* Only allow <= page size writes at the beginning of the file */
618 if ((*ppos != 0) || (count >= PAGE_SIZE))
621 /* Slurp in the user data */
623 if (copy_from_user(kbuf, buf, count))
627 /* Parse the user data */
630 new_map.nr_extents = 0;
631 for (;pos; pos = next_line) {
632 extent = &new_map.extent[new_map.nr_extents];
634 /* Find the end of line and ensure I don't look past it */
635 next_line = strchr(pos, '\n');
639 if (*next_line == '\0')
643 pos = skip_spaces(pos);
644 extent->first = simple_strtoul(pos, &pos, 10);
648 pos = skip_spaces(pos);
649 extent->lower_first = simple_strtoul(pos, &pos, 10);
653 pos = skip_spaces(pos);
654 extent->count = simple_strtoul(pos, &pos, 10);
655 if (*pos && !isspace(*pos))
658 /* Verify there is not trailing junk on the line */
659 pos = skip_spaces(pos);
663 /* Verify we have been given valid starting values */
664 if ((extent->first == (u32) -1) ||
665 (extent->lower_first == (u32) -1 ))
668 /* Verify count is not zero and does not cause the extent to wrap */
669 if ((extent->first + extent->count) <= extent->first)
671 if ((extent->lower_first + extent->count) <= extent->lower_first)
674 /* Do the ranges in extent overlap any previous extents? */
675 if (mappings_overlap(&new_map, extent))
678 new_map.nr_extents++;
680 /* Fail if the file contains too many extents */
681 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
685 /* Be very certaint the new map actually exists */
686 if (new_map.nr_extents == 0)
690 /* Validate the user is allowed to use user id's mapped to. */
691 if (!new_idmap_permitted(ns, cap_setid, &new_map))
694 /* Map the lower ids from the parent user namespace to the
695 * kernel global id space.
697 for (idx = 0; idx < new_map.nr_extents; idx++) {
699 extent = &new_map.extent[idx];
701 lower_first = map_id_range_down(parent_map,
705 /* Fail if we can not map the specified extent to
706 * the kernel global id space.
708 if (lower_first == (u32) -1)
711 extent->lower_first = lower_first;
714 /* Install the map */
715 memcpy(map->extent, new_map.extent,
716 new_map.nr_extents*sizeof(new_map.extent[0]));
718 map->nr_extents = new_map.nr_extents;
723 mutex_unlock(&id_map_mutex);
729 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
731 struct seq_file *seq = file->private_data;
732 struct user_namespace *ns = seq->private;
733 struct user_namespace *seq_ns = seq_user_ns(seq);
738 if ((seq_ns != ns) && (seq_ns != ns->parent))
741 return map_write(file, buf, size, ppos, CAP_SETUID,
742 &ns->uid_map, &ns->parent->uid_map);
745 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
747 struct seq_file *seq = file->private_data;
748 struct user_namespace *ns = seq->private;
749 struct user_namespace *seq_ns = seq_user_ns(seq);
754 if ((seq_ns != ns) && (seq_ns != ns->parent))
757 return map_write(file, buf, size, ppos, CAP_SETGID,
758 &ns->gid_map, &ns->parent->gid_map);
761 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
763 struct seq_file *seq = file->private_data;
764 struct user_namespace *ns = seq->private;
765 struct user_namespace *seq_ns = seq_user_ns(seq);
770 if ((seq_ns != ns) && (seq_ns != ns->parent))
773 /* Anyone can set any valid project id no capability needed */
774 return map_write(file, buf, size, ppos, -1,
775 &ns->projid_map, &ns->parent->projid_map);
778 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
779 struct uid_gid_map *new_map)
781 /* Allow mapping to your own filesystem ids */
782 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
783 u32 id = new_map->extent[0].lower_first;
784 if (cap_setid == CAP_SETUID) {
785 kuid_t uid = make_kuid(ns->parent, id);
786 if (uid_eq(uid, current_fsuid()))
789 else if (cap_setid == CAP_SETGID) {
790 kgid_t gid = make_kgid(ns->parent, id);
791 if (gid_eq(gid, current_fsgid()))
796 /* Allow anyone to set a mapping that doesn't require privilege */
797 if (!cap_valid(cap_setid))
800 /* Allow the specified ids if we have the appropriate capability
801 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
803 if (ns_capable(ns->parent, cap_setid))
809 static void *userns_get(struct task_struct *task)
811 struct user_namespace *user_ns;
814 user_ns = get_user_ns(__task_cred(task)->user_ns);
820 static void userns_put(void *ns)
825 static int userns_install(struct nsproxy *nsproxy, void *ns)
827 struct user_namespace *user_ns = ns;
830 /* Don't allow gaining capabilities by reentering
831 * the same user namespace.
833 if (user_ns == current_user_ns())
836 /* Threaded processes may not enter a different user namespace */
837 if (atomic_read(¤t->mm->mm_users) > 1)
840 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
843 cred = prepare_creds();
847 put_user_ns(cred->user_ns);
848 set_cred_user_ns(cred, get_user_ns(user_ns));
850 return commit_creds(cred);
853 static unsigned int userns_inum(void *ns)
855 struct user_namespace *user_ns = ns;
856 return user_ns->proc_inum;
859 const struct proc_ns_operations userns_operations = {
861 .type = CLONE_NEWUSER,
864 .install = userns_install,
868 static __init int user_namespaces_init(void)
870 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
873 module_init(user_namespaces_init);