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>
24 #include <linux/fs_struct.h>
26 static struct kmem_cache *user_ns_cachep __read_mostly;
28 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
29 struct uid_gid_map *map);
31 static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
33 /* Start with the same capabilities as init but useless for doing
34 * anything as the capabilities are bound to the new user namespace.
36 cred->securebits = SECUREBITS_DEFAULT;
37 cred->cap_inheritable = CAP_EMPTY_SET;
38 cred->cap_permitted = CAP_FULL_SET;
39 cred->cap_effective = CAP_FULL_SET;
40 cred->cap_bset = CAP_FULL_SET;
42 key_put(cred->request_key_auth);
43 cred->request_key_auth = NULL;
45 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
46 cred->user_ns = user_ns;
50 * Create a new user namespace, deriving the creator from the user in the
51 * passed credentials, and replacing that user with the new root user for the
54 * This is called by copy_creds(), which will finish setting the target task's
57 int create_user_ns(struct cred *new)
59 struct user_namespace *ns, *parent_ns = new->user_ns;
60 kuid_t owner = new->euid;
61 kgid_t group = new->egid;
64 /* The creator needs a mapping in the parent user namespace
65 * or else we won't be able to reasonably tell userspace who
66 * created a user_namespace.
68 if (!kuid_has_mapping(parent_ns, owner) ||
69 !kgid_has_mapping(parent_ns, group))
72 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
76 ret = proc_alloc_inum(&ns->proc_inum);
78 kmem_cache_free(user_ns_cachep, ns);
82 atomic_set(&ns->count, 1);
83 /* Leave the new->user_ns reference with the new user namespace. */
84 ns->parent = parent_ns;
88 set_cred_user_ns(new, ns);
93 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
97 if (!(unshare_flags & CLONE_NEWUSER))
100 cred = prepare_creds();
105 return create_user_ns(cred);
108 void free_user_ns(struct user_namespace *ns)
110 struct user_namespace *parent;
114 proc_free_inum(ns->proc_inum);
115 kmem_cache_free(user_ns_cachep, ns);
117 } while (atomic_dec_and_test(&parent->count));
119 EXPORT_SYMBOL(free_user_ns);
121 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
123 unsigned idx, extents;
124 u32 first, last, id2;
126 id2 = id + count - 1;
128 /* Find the matching extent */
129 extents = map->nr_extents;
130 smp_read_barrier_depends();
131 for (idx = 0; idx < extents; idx++) {
132 first = map->extent[idx].first;
133 last = first + map->extent[idx].count - 1;
134 if (id >= first && id <= last &&
135 (id2 >= first && id2 <= last))
138 /* Map the id or note failure */
140 id = (id - first) + map->extent[idx].lower_first;
147 static u32 map_id_down(struct uid_gid_map *map, u32 id)
149 unsigned idx, extents;
152 /* Find the matching extent */
153 extents = map->nr_extents;
154 smp_read_barrier_depends();
155 for (idx = 0; idx < extents; idx++) {
156 first = map->extent[idx].first;
157 last = first + map->extent[idx].count - 1;
158 if (id >= first && id <= last)
161 /* Map the id or note failure */
163 id = (id - first) + map->extent[idx].lower_first;
170 static u32 map_id_up(struct uid_gid_map *map, u32 id)
172 unsigned idx, extents;
175 /* Find the matching extent */
176 extents = map->nr_extents;
177 smp_read_barrier_depends();
178 for (idx = 0; idx < extents; idx++) {
179 first = map->extent[idx].lower_first;
180 last = first + map->extent[idx].count - 1;
181 if (id >= first && id <= last)
184 /* Map the id or note failure */
186 id = (id - first) + map->extent[idx].first;
194 * make_kuid - Map a user-namespace uid pair into a kuid.
195 * @ns: User namespace that the uid is in
196 * @uid: User identifier
198 * Maps a user-namespace uid pair into a kernel internal kuid,
199 * and returns that kuid.
201 * When there is no mapping defined for the user-namespace uid
202 * pair INVALID_UID is returned. Callers are expected to test
203 * for and handle handle INVALID_UID being returned. INVALID_UID
204 * may be tested for using uid_valid().
206 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
208 /* Map the uid to a global kernel uid */
209 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
211 EXPORT_SYMBOL(make_kuid);
214 * from_kuid - Create a uid from a kuid user-namespace pair.
215 * @targ: The user namespace we want a uid in.
216 * @kuid: The kernel internal uid to start with.
218 * Map @kuid into the user-namespace specified by @targ and
219 * return the resulting uid.
221 * There is always a mapping into the initial user_namespace.
223 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
225 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
227 /* Map the uid from a global kernel uid */
228 return map_id_up(&targ->uid_map, __kuid_val(kuid));
230 EXPORT_SYMBOL(from_kuid);
233 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
234 * @targ: The user namespace we want a uid in.
235 * @kuid: The kernel internal uid to start with.
237 * Map @kuid into the user-namespace specified by @targ and
238 * return the resulting uid.
240 * There is always a mapping into the initial user_namespace.
242 * Unlike from_kuid from_kuid_munged never fails and always
243 * returns a valid uid. This makes from_kuid_munged appropriate
244 * for use in syscalls like stat and getuid where failing the
245 * system call and failing to provide a valid uid are not an
248 * If @kuid has no mapping in @targ overflowuid is returned.
250 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
253 uid = from_kuid(targ, kuid);
255 if (uid == (uid_t) -1)
259 EXPORT_SYMBOL(from_kuid_munged);
262 * make_kgid - Map a user-namespace gid pair into a kgid.
263 * @ns: User namespace that the gid is in
264 * @uid: group identifier
266 * Maps a user-namespace gid pair into a kernel internal kgid,
267 * and returns that kgid.
269 * When there is no mapping defined for the user-namespace gid
270 * pair INVALID_GID is returned. Callers are expected to test
271 * for and handle INVALID_GID being returned. INVALID_GID may be
272 * tested for using gid_valid().
274 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
276 /* Map the gid to a global kernel gid */
277 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
279 EXPORT_SYMBOL(make_kgid);
282 * from_kgid - Create a gid from a kgid user-namespace pair.
283 * @targ: The user namespace we want a gid in.
284 * @kgid: The kernel internal gid to start with.
286 * Map @kgid into the user-namespace specified by @targ and
287 * return the resulting gid.
289 * There is always a mapping into the initial user_namespace.
291 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
293 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
295 /* Map the gid from a global kernel gid */
296 return map_id_up(&targ->gid_map, __kgid_val(kgid));
298 EXPORT_SYMBOL(from_kgid);
301 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
302 * @targ: The user namespace we want a gid in.
303 * @kgid: The kernel internal gid to start with.
305 * Map @kgid into the user-namespace specified by @targ and
306 * return the resulting gid.
308 * There is always a mapping into the initial user_namespace.
310 * Unlike from_kgid from_kgid_munged never fails and always
311 * returns a valid gid. This makes from_kgid_munged appropriate
312 * for use in syscalls like stat and getgid where failing the
313 * system call and failing to provide a valid gid are not options.
315 * If @kgid has no mapping in @targ overflowgid is returned.
317 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
320 gid = from_kgid(targ, kgid);
322 if (gid == (gid_t) -1)
326 EXPORT_SYMBOL(from_kgid_munged);
329 * make_kprojid - Map a user-namespace projid pair into a kprojid.
330 * @ns: User namespace that the projid is in
331 * @projid: Project identifier
333 * Maps a user-namespace uid pair into a kernel internal kuid,
334 * and returns that kuid.
336 * When there is no mapping defined for the user-namespace projid
337 * pair INVALID_PROJID is returned. Callers are expected to test
338 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
339 * may be tested for using projid_valid().
341 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
343 /* Map the uid to a global kernel uid */
344 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
346 EXPORT_SYMBOL(make_kprojid);
349 * from_kprojid - Create a projid from a kprojid user-namespace pair.
350 * @targ: The user namespace we want a projid in.
351 * @kprojid: The kernel internal project identifier to start with.
353 * Map @kprojid into the user-namespace specified by @targ and
354 * return the resulting projid.
356 * There is always a mapping into the initial user_namespace.
358 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
360 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
362 /* Map the uid from a global kernel uid */
363 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
365 EXPORT_SYMBOL(from_kprojid);
368 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
369 * @targ: The user namespace we want a projid in.
370 * @kprojid: The kernel internal projid to start with.
372 * Map @kprojid into the user-namespace specified by @targ and
373 * return the resulting projid.
375 * There is always a mapping into the initial user_namespace.
377 * Unlike from_kprojid from_kprojid_munged never fails and always
378 * returns a valid projid. This makes from_kprojid_munged
379 * appropriate for use in syscalls like stat and where
380 * failing the system call and failing to provide a valid projid are
383 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
385 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
388 projid = from_kprojid(targ, kprojid);
390 if (projid == (projid_t) -1)
391 projid = OVERFLOW_PROJID;
394 EXPORT_SYMBOL(from_kprojid_munged);
397 static int uid_m_show(struct seq_file *seq, void *v)
399 struct user_namespace *ns = seq->private;
400 struct uid_gid_extent *extent = v;
401 struct user_namespace *lower_ns;
404 lower_ns = seq_user_ns(seq);
405 if ((lower_ns == ns) && lower_ns->parent)
406 lower_ns = lower_ns->parent;
408 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
410 seq_printf(seq, "%10u %10u %10u\n",
418 static int gid_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_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
431 seq_printf(seq, "%10u %10u %10u\n",
439 static int projid_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_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
452 seq_printf(seq, "%10u %10u %10u\n",
460 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
462 struct uid_gid_extent *extent = NULL;
465 if (pos < map->nr_extents)
466 extent = &map->extent[pos];
471 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
473 struct user_namespace *ns = seq->private;
475 return m_start(seq, ppos, &ns->uid_map);
478 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
480 struct user_namespace *ns = seq->private;
482 return m_start(seq, ppos, &ns->gid_map);
485 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
487 struct user_namespace *ns = seq->private;
489 return m_start(seq, ppos, &ns->projid_map);
492 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
495 return seq->op->start(seq, pos);
498 static void m_stop(struct seq_file *seq, void *v)
503 struct seq_operations proc_uid_seq_operations = {
504 .start = uid_m_start,
510 struct seq_operations proc_gid_seq_operations = {
511 .start = gid_m_start,
517 struct seq_operations proc_projid_seq_operations = {
518 .start = projid_m_start,
521 .show = projid_m_show,
524 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
526 u32 upper_first, lower_first, upper_last, lower_last;
529 upper_first = extent->first;
530 lower_first = extent->lower_first;
531 upper_last = upper_first + extent->count - 1;
532 lower_last = lower_first + extent->count - 1;
534 for (idx = 0; idx < new_map->nr_extents; idx++) {
535 u32 prev_upper_first, prev_lower_first;
536 u32 prev_upper_last, prev_lower_last;
537 struct uid_gid_extent *prev;
539 prev = &new_map->extent[idx];
541 prev_upper_first = prev->first;
542 prev_lower_first = prev->lower_first;
543 prev_upper_last = prev_upper_first + prev->count - 1;
544 prev_lower_last = prev_lower_first + prev->count - 1;
546 /* Does the upper range intersect a previous extent? */
547 if ((prev_upper_first <= upper_last) &&
548 (prev_upper_last >= upper_first))
551 /* Does the lower range intersect a previous extent? */
552 if ((prev_lower_first <= lower_last) &&
553 (prev_lower_last >= lower_first))
560 static DEFINE_MUTEX(id_map_mutex);
562 static ssize_t map_write(struct file *file, const char __user *buf,
563 size_t count, loff_t *ppos,
565 struct uid_gid_map *map,
566 struct uid_gid_map *parent_map)
568 struct seq_file *seq = file->private_data;
569 struct user_namespace *ns = seq->private;
570 struct uid_gid_map new_map;
572 struct uid_gid_extent *extent = NULL;
573 unsigned long page = 0;
574 char *kbuf, *pos, *next_line;
575 ssize_t ret = -EINVAL;
578 * The id_map_mutex serializes all writes to any given map.
580 * Any map is only ever written once.
582 * An id map fits within 1 cache line on most architectures.
584 * On read nothing needs to be done unless you are on an
585 * architecture with a crazy cache coherency model like alpha.
587 * There is a one time data dependency between reading the
588 * count of the extents and the values of the extents. The
589 * desired behavior is to see the values of the extents that
590 * were written before the count of the extents.
592 * To achieve this smp_wmb() is used on guarantee the write
593 * order and smp_read_barrier_depends() is guaranteed that we
594 * don't have crazy architectures returning stale data.
597 mutex_lock(&id_map_mutex);
600 /* Only allow one successful write to the map */
601 if (map->nr_extents != 0)
604 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
605 * over the user namespace in order to set the id mapping.
607 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
612 page = __get_free_page(GFP_TEMPORARY);
613 kbuf = (char *) page;
617 /* Only allow <= page size writes at the beginning of the file */
619 if ((*ppos != 0) || (count >= PAGE_SIZE))
622 /* Slurp in the user data */
624 if (copy_from_user(kbuf, buf, count))
628 /* Parse the user data */
631 new_map.nr_extents = 0;
632 for (;pos; pos = next_line) {
633 extent = &new_map.extent[new_map.nr_extents];
635 /* Find the end of line and ensure I don't look past it */
636 next_line = strchr(pos, '\n');
640 if (*next_line == '\0')
644 pos = skip_spaces(pos);
645 extent->first = simple_strtoul(pos, &pos, 10);
649 pos = skip_spaces(pos);
650 extent->lower_first = simple_strtoul(pos, &pos, 10);
654 pos = skip_spaces(pos);
655 extent->count = simple_strtoul(pos, &pos, 10);
656 if (*pos && !isspace(*pos))
659 /* Verify there is not trailing junk on the line */
660 pos = skip_spaces(pos);
664 /* Verify we have been given valid starting values */
665 if ((extent->first == (u32) -1) ||
666 (extent->lower_first == (u32) -1 ))
669 /* Verify count is not zero and does not cause the extent to wrap */
670 if ((extent->first + extent->count) <= extent->first)
672 if ((extent->lower_first + extent->count) <= extent->lower_first)
675 /* Do the ranges in extent overlap any previous extents? */
676 if (mappings_overlap(&new_map, extent))
679 new_map.nr_extents++;
681 /* Fail if the file contains too many extents */
682 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
686 /* Be very certaint the new map actually exists */
687 if (new_map.nr_extents == 0)
691 /* Validate the user is allowed to use user id's mapped to. */
692 if (!new_idmap_permitted(ns, cap_setid, &new_map))
695 /* Map the lower ids from the parent user namespace to the
696 * kernel global id space.
698 for (idx = 0; idx < new_map.nr_extents; idx++) {
700 extent = &new_map.extent[idx];
702 lower_first = map_id_range_down(parent_map,
706 /* Fail if we can not map the specified extent to
707 * the kernel global id space.
709 if (lower_first == (u32) -1)
712 extent->lower_first = lower_first;
715 /* Install the map */
716 memcpy(map->extent, new_map.extent,
717 new_map.nr_extents*sizeof(new_map.extent[0]));
719 map->nr_extents = new_map.nr_extents;
724 mutex_unlock(&id_map_mutex);
730 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
732 struct seq_file *seq = file->private_data;
733 struct user_namespace *ns = seq->private;
734 struct user_namespace *seq_ns = seq_user_ns(seq);
739 if ((seq_ns != ns) && (seq_ns != ns->parent))
742 return map_write(file, buf, size, ppos, CAP_SETUID,
743 &ns->uid_map, &ns->parent->uid_map);
746 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
748 struct seq_file *seq = file->private_data;
749 struct user_namespace *ns = seq->private;
750 struct user_namespace *seq_ns = seq_user_ns(seq);
755 if ((seq_ns != ns) && (seq_ns != ns->parent))
758 return map_write(file, buf, size, ppos, CAP_SETGID,
759 &ns->gid_map, &ns->parent->gid_map);
762 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
764 struct seq_file *seq = file->private_data;
765 struct user_namespace *ns = seq->private;
766 struct user_namespace *seq_ns = seq_user_ns(seq);
771 if ((seq_ns != ns) && (seq_ns != ns->parent))
774 /* Anyone can set any valid project id no capability needed */
775 return map_write(file, buf, size, ppos, -1,
776 &ns->projid_map, &ns->parent->projid_map);
779 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
780 struct uid_gid_map *new_map)
782 /* Allow mapping to your own filesystem ids */
783 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
784 u32 id = new_map->extent[0].lower_first;
785 if (cap_setid == CAP_SETUID) {
786 kuid_t uid = make_kuid(ns->parent, id);
787 if (uid_eq(uid, current_fsuid()))
790 else if (cap_setid == CAP_SETGID) {
791 kgid_t gid = make_kgid(ns->parent, id);
792 if (gid_eq(gid, current_fsgid()))
797 /* Allow anyone to set a mapping that doesn't require privilege */
798 if (!cap_valid(cap_setid))
801 /* Allow the specified ids if we have the appropriate capability
802 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
804 if (ns_capable(ns->parent, cap_setid))
810 static void *userns_get(struct task_struct *task)
812 struct user_namespace *user_ns;
815 user_ns = get_user_ns(__task_cred(task)->user_ns);
821 static void userns_put(void *ns)
826 static int userns_install(struct nsproxy *nsproxy, void *ns)
828 struct user_namespace *user_ns = ns;
831 /* Don't allow gaining capabilities by reentering
832 * the same user namespace.
834 if (user_ns == current_user_ns())
837 /* Threaded processes may not enter a different user namespace */
838 if (atomic_read(¤t->mm->mm_users) > 1)
841 if (current->fs->users != 1)
844 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
847 cred = prepare_creds();
851 put_user_ns(cred->user_ns);
852 set_cred_user_ns(cred, get_user_ns(user_ns));
854 return commit_creds(cred);
857 static unsigned int userns_inum(void *ns)
859 struct user_namespace *user_ns = ns;
860 return user_ns->proc_inum;
863 const struct proc_ns_operations userns_operations = {
865 .type = CLONE_NEWUSER,
868 .install = userns_install,
872 static __init int user_namespaces_init(void)
874 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
877 module_init(user_namespaces_init);