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);
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 kref *kref)
109 struct user_namespace *parent, *ns =
110 container_of(kref, struct user_namespace, kref);
113 proc_free_inum(ns->proc_inum);
114 kmem_cache_free(user_ns_cachep, ns);
117 EXPORT_SYMBOL(free_user_ns);
119 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
121 unsigned idx, extents;
122 u32 first, last, id2;
124 id2 = id + count - 1;
126 /* Find the matching extent */
127 extents = map->nr_extents;
128 smp_read_barrier_depends();
129 for (idx = 0; idx < extents; idx++) {
130 first = map->extent[idx].first;
131 last = first + map->extent[idx].count - 1;
132 if (id >= first && id <= last &&
133 (id2 >= first && id2 <= last))
136 /* Map the id or note failure */
138 id = (id - first) + map->extent[idx].lower_first;
145 static u32 map_id_down(struct uid_gid_map *map, u32 id)
147 unsigned idx, extents;
150 /* Find the matching extent */
151 extents = map->nr_extents;
152 smp_read_barrier_depends();
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)
159 /* Map the id or note failure */
161 id = (id - first) + map->extent[idx].lower_first;
168 static u32 map_id_up(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].lower_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].first;
192 * make_kuid - Map a user-namespace uid pair into a kuid.
193 * @ns: User namespace that the uid is in
194 * @uid: User identifier
196 * Maps a user-namespace uid pair into a kernel internal kuid,
197 * and returns that kuid.
199 * When there is no mapping defined for the user-namespace uid
200 * pair INVALID_UID is returned. Callers are expected to test
201 * for and handle handle INVALID_UID being returned. INVALID_UID
202 * may be tested for using uid_valid().
204 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
206 /* Map the uid to a global kernel uid */
207 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
209 EXPORT_SYMBOL(make_kuid);
212 * from_kuid - Create a uid from a kuid user-namespace pair.
213 * @targ: The user namespace we want a uid in.
214 * @kuid: The kernel internal uid to start with.
216 * Map @kuid into the user-namespace specified by @targ and
217 * return the resulting uid.
219 * There is always a mapping into the initial user_namespace.
221 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
223 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
225 /* Map the uid from a global kernel uid */
226 return map_id_up(&targ->uid_map, __kuid_val(kuid));
228 EXPORT_SYMBOL(from_kuid);
231 * from_kuid_munged - 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 * Unlike from_kuid from_kuid_munged never fails and always
241 * returns a valid uid. This makes from_kuid_munged appropriate
242 * for use in syscalls like stat and getuid where failing the
243 * system call and failing to provide a valid uid are not an
246 * If @kuid has no mapping in @targ overflowuid is returned.
248 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
251 uid = from_kuid(targ, kuid);
253 if (uid == (uid_t) -1)
257 EXPORT_SYMBOL(from_kuid_munged);
260 * make_kgid - Map a user-namespace gid pair into a kgid.
261 * @ns: User namespace that the gid is in
262 * @uid: group identifier
264 * Maps a user-namespace gid pair into a kernel internal kgid,
265 * and returns that kgid.
267 * When there is no mapping defined for the user-namespace gid
268 * pair INVALID_GID is returned. Callers are expected to test
269 * for and handle INVALID_GID being returned. INVALID_GID may be
270 * tested for using gid_valid().
272 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
274 /* Map the gid to a global kernel gid */
275 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
277 EXPORT_SYMBOL(make_kgid);
280 * from_kgid - Create a gid from a kgid user-namespace pair.
281 * @targ: The user namespace we want a gid in.
282 * @kgid: The kernel internal gid to start with.
284 * Map @kgid into the user-namespace specified by @targ and
285 * return the resulting gid.
287 * There is always a mapping into the initial user_namespace.
289 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
291 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
293 /* Map the gid from a global kernel gid */
294 return map_id_up(&targ->gid_map, __kgid_val(kgid));
296 EXPORT_SYMBOL(from_kgid);
299 * from_kgid_munged - 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 * Unlike from_kgid from_kgid_munged never fails and always
309 * returns a valid gid. This makes from_kgid_munged appropriate
310 * for use in syscalls like stat and getgid where failing the
311 * system call and failing to provide a valid gid are not options.
313 * If @kgid has no mapping in @targ overflowgid is returned.
315 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
318 gid = from_kgid(targ, kgid);
320 if (gid == (gid_t) -1)
324 EXPORT_SYMBOL(from_kgid_munged);
327 * make_kprojid - Map a user-namespace projid pair into a kprojid.
328 * @ns: User namespace that the projid is in
329 * @projid: Project identifier
331 * Maps a user-namespace uid pair into a kernel internal kuid,
332 * and returns that kuid.
334 * When there is no mapping defined for the user-namespace projid
335 * pair INVALID_PROJID is returned. Callers are expected to test
336 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
337 * may be tested for using projid_valid().
339 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
341 /* Map the uid to a global kernel uid */
342 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
344 EXPORT_SYMBOL(make_kprojid);
347 * from_kprojid - Create a projid from a kprojid user-namespace pair.
348 * @targ: The user namespace we want a projid in.
349 * @kprojid: The kernel internal project identifier to start with.
351 * Map @kprojid into the user-namespace specified by @targ and
352 * return the resulting projid.
354 * There is always a mapping into the initial user_namespace.
356 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
358 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
360 /* Map the uid from a global kernel uid */
361 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
363 EXPORT_SYMBOL(from_kprojid);
366 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
367 * @targ: The user namespace we want a projid in.
368 * @kprojid: The kernel internal projid 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 * Unlike from_kprojid from_kprojid_munged never fails and always
376 * returns a valid projid. This makes from_kprojid_munged
377 * appropriate for use in syscalls like stat and where
378 * failing the system call and failing to provide a valid projid are
381 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
383 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
386 projid = from_kprojid(targ, kprojid);
388 if (projid == (projid_t) -1)
389 projid = OVERFLOW_PROJID;
392 EXPORT_SYMBOL(from_kprojid_munged);
395 static int uid_m_show(struct seq_file *seq, void *v)
397 struct user_namespace *ns = seq->private;
398 struct uid_gid_extent *extent = v;
399 struct user_namespace *lower_ns;
402 lower_ns = seq_user_ns(seq);
403 if ((lower_ns == ns) && lower_ns->parent)
404 lower_ns = lower_ns->parent;
406 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
408 seq_printf(seq, "%10u %10u %10u\n",
416 static int gid_m_show(struct seq_file *seq, void *v)
418 struct user_namespace *ns = seq->private;
419 struct uid_gid_extent *extent = v;
420 struct user_namespace *lower_ns;
423 lower_ns = seq_user_ns(seq);
424 if ((lower_ns == ns) && lower_ns->parent)
425 lower_ns = lower_ns->parent;
427 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
429 seq_printf(seq, "%10u %10u %10u\n",
437 static int projid_m_show(struct seq_file *seq, void *v)
439 struct user_namespace *ns = seq->private;
440 struct uid_gid_extent *extent = v;
441 struct user_namespace *lower_ns;
444 lower_ns = seq_user_ns(seq);
445 if ((lower_ns == ns) && lower_ns->parent)
446 lower_ns = lower_ns->parent;
448 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
450 seq_printf(seq, "%10u %10u %10u\n",
458 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
460 struct uid_gid_extent *extent = NULL;
463 if (pos < map->nr_extents)
464 extent = &map->extent[pos];
469 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
471 struct user_namespace *ns = seq->private;
473 return m_start(seq, ppos, &ns->uid_map);
476 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
478 struct user_namespace *ns = seq->private;
480 return m_start(seq, ppos, &ns->gid_map);
483 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
485 struct user_namespace *ns = seq->private;
487 return m_start(seq, ppos, &ns->projid_map);
490 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
493 return seq->op->start(seq, pos);
496 static void m_stop(struct seq_file *seq, void *v)
501 struct seq_operations proc_uid_seq_operations = {
502 .start = uid_m_start,
508 struct seq_operations proc_gid_seq_operations = {
509 .start = gid_m_start,
515 struct seq_operations proc_projid_seq_operations = {
516 .start = projid_m_start,
519 .show = projid_m_show,
522 static DEFINE_MUTEX(id_map_mutex);
524 static ssize_t map_write(struct file *file, const char __user *buf,
525 size_t count, loff_t *ppos,
527 struct uid_gid_map *map,
528 struct uid_gid_map *parent_map)
530 struct seq_file *seq = file->private_data;
531 struct user_namespace *ns = seq->private;
532 struct uid_gid_map new_map;
534 struct uid_gid_extent *extent, *last = NULL;
535 unsigned long page = 0;
536 char *kbuf, *pos, *next_line;
537 ssize_t ret = -EINVAL;
540 * The id_map_mutex serializes all writes to any given map.
542 * Any map is only ever written once.
544 * An id map fits within 1 cache line on most architectures.
546 * On read nothing needs to be done unless you are on an
547 * architecture with a crazy cache coherency model like alpha.
549 * There is a one time data dependency between reading the
550 * count of the extents and the values of the extents. The
551 * desired behavior is to see the values of the extents that
552 * were written before the count of the extents.
554 * To achieve this smp_wmb() is used on guarantee the write
555 * order and smp_read_barrier_depends() is guaranteed that we
556 * don't have crazy architectures returning stale data.
559 mutex_lock(&id_map_mutex);
562 /* Only allow one successful write to the map */
563 if (map->nr_extents != 0)
566 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
567 * over the user namespace in order to set the id mapping.
569 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
574 page = __get_free_page(GFP_TEMPORARY);
575 kbuf = (char *) page;
579 /* Only allow <= page size writes at the beginning of the file */
581 if ((*ppos != 0) || (count >= PAGE_SIZE))
584 /* Slurp in the user data */
586 if (copy_from_user(kbuf, buf, count))
590 /* Parse the user data */
593 new_map.nr_extents = 0;
594 for (;pos; pos = next_line) {
595 extent = &new_map.extent[new_map.nr_extents];
597 /* Find the end of line and ensure I don't look past it */
598 next_line = strchr(pos, '\n');
602 if (*next_line == '\0')
606 pos = skip_spaces(pos);
607 extent->first = simple_strtoul(pos, &pos, 10);
611 pos = skip_spaces(pos);
612 extent->lower_first = simple_strtoul(pos, &pos, 10);
616 pos = skip_spaces(pos);
617 extent->count = simple_strtoul(pos, &pos, 10);
618 if (*pos && !isspace(*pos))
621 /* Verify there is not trailing junk on the line */
622 pos = skip_spaces(pos);
626 /* Verify we have been given valid starting values */
627 if ((extent->first == (u32) -1) ||
628 (extent->lower_first == (u32) -1 ))
631 /* Verify count is not zero and does not cause the extent to wrap */
632 if ((extent->first + extent->count) <= extent->first)
634 if ((extent->lower_first + extent->count) <= extent->lower_first)
637 /* For now only accept extents that are strictly in order */
639 (((last->first + last->count) > extent->first) ||
640 ((last->lower_first + last->count) > extent->lower_first)))
643 new_map.nr_extents++;
646 /* Fail if the file contains too many extents */
647 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
651 /* Be very certaint the new map actually exists */
652 if (new_map.nr_extents == 0)
656 /* Validate the user is allowed to use user id's mapped to. */
657 if (!new_idmap_permitted(ns, cap_setid, &new_map))
660 /* Map the lower ids from the parent user namespace to the
661 * kernel global id space.
663 for (idx = 0; idx < new_map.nr_extents; idx++) {
665 extent = &new_map.extent[idx];
667 lower_first = map_id_range_down(parent_map,
671 /* Fail if we can not map the specified extent to
672 * the kernel global id space.
674 if (lower_first == (u32) -1)
677 extent->lower_first = lower_first;
680 /* Install the map */
681 memcpy(map->extent, new_map.extent,
682 new_map.nr_extents*sizeof(new_map.extent[0]));
684 map->nr_extents = new_map.nr_extents;
689 mutex_unlock(&id_map_mutex);
695 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
697 struct seq_file *seq = file->private_data;
698 struct user_namespace *ns = seq->private;
699 struct user_namespace *seq_ns = seq_user_ns(seq);
704 if ((seq_ns != ns) && (seq_ns != ns->parent))
707 return map_write(file, buf, size, ppos, CAP_SETUID,
708 &ns->uid_map, &ns->parent->uid_map);
711 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
713 struct seq_file *seq = file->private_data;
714 struct user_namespace *ns = seq->private;
715 struct user_namespace *seq_ns = seq_user_ns(seq);
720 if ((seq_ns != ns) && (seq_ns != ns->parent))
723 return map_write(file, buf, size, ppos, CAP_SETGID,
724 &ns->gid_map, &ns->parent->gid_map);
727 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
729 struct seq_file *seq = file->private_data;
730 struct user_namespace *ns = seq->private;
731 struct user_namespace *seq_ns = seq_user_ns(seq);
736 if ((seq_ns != ns) && (seq_ns != ns->parent))
739 /* Anyone can set any valid project id no capability needed */
740 return map_write(file, buf, size, ppos, -1,
741 &ns->projid_map, &ns->parent->projid_map);
744 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
745 struct uid_gid_map *new_map)
747 /* Allow mapping to your own filesystem ids */
748 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
749 u32 id = new_map->extent[0].lower_first;
750 if (cap_setid == CAP_SETUID) {
751 kuid_t uid = make_kuid(ns->parent, id);
752 if (uid_eq(uid, current_fsuid()))
755 else if (cap_setid == CAP_SETGID) {
756 kgid_t gid = make_kgid(ns->parent, id);
757 if (gid_eq(gid, current_fsgid()))
762 /* Allow anyone to set a mapping that doesn't require privilege */
763 if (!cap_valid(cap_setid))
766 /* Allow the specified ids if we have the appropriate capability
767 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
769 if (ns_capable(ns->parent, cap_setid))
775 static void *userns_get(struct task_struct *task)
777 struct user_namespace *user_ns;
780 user_ns = get_user_ns(__task_cred(task)->user_ns);
786 static void userns_put(void *ns)
791 static int userns_install(struct nsproxy *nsproxy, void *ns)
793 struct user_namespace *user_ns = ns;
796 /* Don't allow gaining capabilities by reentering
797 * the same user namespace.
799 if (user_ns == current_user_ns())
802 /* Threaded processes may not enter a different user namespace */
803 if (atomic_read(¤t->mm->mm_users) > 1)
806 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
809 cred = prepare_creds();
813 put_user_ns(cred->user_ns);
814 set_cred_user_ns(cred, get_user_ns(user_ns));
816 return commit_creds(cred);
819 static unsigned int userns_inum(void *ns)
821 struct user_namespace *user_ns = ns;
822 return user_ns->proc_inum;
825 const struct proc_ns_operations userns_operations = {
827 .type = CLONE_NEWUSER,
830 .install = userns_install,
834 static __init int user_namespaces_init(void)
836 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
839 module_init(user_namespaces_init);