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;
65 * Verify that we can not violate the policy of which files
66 * may be accessed that is specified by the root directory,
67 * by verifing that the root directory is at the root of the
68 * mount namespace which allows all files to be accessed.
70 if (current_chrooted())
73 /* The creator needs a mapping in the parent user namespace
74 * or else we won't be able to reasonably tell userspace who
75 * created a user_namespace.
77 if (!kuid_has_mapping(parent_ns, owner) ||
78 !kgid_has_mapping(parent_ns, group))
81 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
85 ret = proc_alloc_inum(&ns->proc_inum);
87 kmem_cache_free(user_ns_cachep, ns);
91 atomic_set(&ns->count, 1);
92 /* Leave the new->user_ns reference with the new user namespace. */
93 ns->parent = parent_ns;
97 set_cred_user_ns(new, ns);
99 update_mnt_policy(ns);
104 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
108 if (!(unshare_flags & CLONE_NEWUSER))
111 cred = prepare_creds();
116 return create_user_ns(cred);
119 void free_user_ns(struct user_namespace *ns)
121 struct user_namespace *parent;
125 proc_free_inum(ns->proc_inum);
126 kmem_cache_free(user_ns_cachep, ns);
128 } while (atomic_dec_and_test(&parent->count));
130 EXPORT_SYMBOL(free_user_ns);
132 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
134 unsigned idx, extents;
135 u32 first, last, id2;
137 id2 = id + count - 1;
139 /* Find the matching extent */
140 extents = map->nr_extents;
141 smp_read_barrier_depends();
142 for (idx = 0; idx < extents; idx++) {
143 first = map->extent[idx].first;
144 last = first + map->extent[idx].count - 1;
145 if (id >= first && id <= last &&
146 (id2 >= first && id2 <= last))
149 /* Map the id or note failure */
151 id = (id - first) + map->extent[idx].lower_first;
158 static u32 map_id_down(struct uid_gid_map *map, u32 id)
160 unsigned idx, extents;
163 /* Find the matching extent */
164 extents = map->nr_extents;
165 smp_read_barrier_depends();
166 for (idx = 0; idx < extents; idx++) {
167 first = map->extent[idx].first;
168 last = first + map->extent[idx].count - 1;
169 if (id >= first && id <= last)
172 /* Map the id or note failure */
174 id = (id - first) + map->extent[idx].lower_first;
181 static u32 map_id_up(struct uid_gid_map *map, u32 id)
183 unsigned idx, extents;
186 /* Find the matching extent */
187 extents = map->nr_extents;
188 smp_read_barrier_depends();
189 for (idx = 0; idx < extents; idx++) {
190 first = map->extent[idx].lower_first;
191 last = first + map->extent[idx].count - 1;
192 if (id >= first && id <= last)
195 /* Map the id or note failure */
197 id = (id - first) + map->extent[idx].first;
205 * make_kuid - Map a user-namespace uid pair into a kuid.
206 * @ns: User namespace that the uid is in
207 * @uid: User identifier
209 * Maps a user-namespace uid pair into a kernel internal kuid,
210 * and returns that kuid.
212 * When there is no mapping defined for the user-namespace uid
213 * pair INVALID_UID is returned. Callers are expected to test
214 * for and handle handle INVALID_UID being returned. INVALID_UID
215 * may be tested for using uid_valid().
217 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
219 /* Map the uid to a global kernel uid */
220 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
222 EXPORT_SYMBOL(make_kuid);
225 * from_kuid - Create a uid from a kuid user-namespace pair.
226 * @targ: The user namespace we want a uid in.
227 * @kuid: The kernel internal uid to start with.
229 * Map @kuid into the user-namespace specified by @targ and
230 * return the resulting uid.
232 * There is always a mapping into the initial user_namespace.
234 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
236 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
238 /* Map the uid from a global kernel uid */
239 return map_id_up(&targ->uid_map, __kuid_val(kuid));
241 EXPORT_SYMBOL(from_kuid);
244 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
245 * @targ: The user namespace we want a uid in.
246 * @kuid: The kernel internal uid to start with.
248 * Map @kuid into the user-namespace specified by @targ and
249 * return the resulting uid.
251 * There is always a mapping into the initial user_namespace.
253 * Unlike from_kuid from_kuid_munged never fails and always
254 * returns a valid uid. This makes from_kuid_munged appropriate
255 * for use in syscalls like stat and getuid where failing the
256 * system call and failing to provide a valid uid are not an
259 * If @kuid has no mapping in @targ overflowuid is returned.
261 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
264 uid = from_kuid(targ, kuid);
266 if (uid == (uid_t) -1)
270 EXPORT_SYMBOL(from_kuid_munged);
273 * make_kgid - Map a user-namespace gid pair into a kgid.
274 * @ns: User namespace that the gid is in
275 * @uid: group identifier
277 * Maps a user-namespace gid pair into a kernel internal kgid,
278 * and returns that kgid.
280 * When there is no mapping defined for the user-namespace gid
281 * pair INVALID_GID is returned. Callers are expected to test
282 * for and handle INVALID_GID being returned. INVALID_GID may be
283 * tested for using gid_valid().
285 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
287 /* Map the gid to a global kernel gid */
288 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
290 EXPORT_SYMBOL(make_kgid);
293 * from_kgid - Create a gid from a kgid user-namespace pair.
294 * @targ: The user namespace we want a gid in.
295 * @kgid: The kernel internal gid to start with.
297 * Map @kgid into the user-namespace specified by @targ and
298 * return the resulting gid.
300 * There is always a mapping into the initial user_namespace.
302 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
304 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
306 /* Map the gid from a global kernel gid */
307 return map_id_up(&targ->gid_map, __kgid_val(kgid));
309 EXPORT_SYMBOL(from_kgid);
312 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
313 * @targ: The user namespace we want a gid in.
314 * @kgid: The kernel internal gid to start with.
316 * Map @kgid into the user-namespace specified by @targ and
317 * return the resulting gid.
319 * There is always a mapping into the initial user_namespace.
321 * Unlike from_kgid from_kgid_munged never fails and always
322 * returns a valid gid. This makes from_kgid_munged appropriate
323 * for use in syscalls like stat and getgid where failing the
324 * system call and failing to provide a valid gid are not options.
326 * If @kgid has no mapping in @targ overflowgid is returned.
328 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
331 gid = from_kgid(targ, kgid);
333 if (gid == (gid_t) -1)
337 EXPORT_SYMBOL(from_kgid_munged);
340 * make_kprojid - Map a user-namespace projid pair into a kprojid.
341 * @ns: User namespace that the projid is in
342 * @projid: Project identifier
344 * Maps a user-namespace uid pair into a kernel internal kuid,
345 * and returns that kuid.
347 * When there is no mapping defined for the user-namespace projid
348 * pair INVALID_PROJID is returned. Callers are expected to test
349 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
350 * may be tested for using projid_valid().
352 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
354 /* Map the uid to a global kernel uid */
355 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
357 EXPORT_SYMBOL(make_kprojid);
360 * from_kprojid - Create a projid from a kprojid user-namespace pair.
361 * @targ: The user namespace we want a projid in.
362 * @kprojid: The kernel internal project identifier to start with.
364 * Map @kprojid into the user-namespace specified by @targ and
365 * return the resulting projid.
367 * There is always a mapping into the initial user_namespace.
369 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
371 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
373 /* Map the uid from a global kernel uid */
374 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
376 EXPORT_SYMBOL(from_kprojid);
379 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
380 * @targ: The user namespace we want a projid in.
381 * @kprojid: The kernel internal projid to start with.
383 * Map @kprojid into the user-namespace specified by @targ and
384 * return the resulting projid.
386 * There is always a mapping into the initial user_namespace.
388 * Unlike from_kprojid from_kprojid_munged never fails and always
389 * returns a valid projid. This makes from_kprojid_munged
390 * appropriate for use in syscalls like stat and where
391 * failing the system call and failing to provide a valid projid are
394 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
396 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
399 projid = from_kprojid(targ, kprojid);
401 if (projid == (projid_t) -1)
402 projid = OVERFLOW_PROJID;
405 EXPORT_SYMBOL(from_kprojid_munged);
408 static int uid_m_show(struct seq_file *seq, void *v)
410 struct user_namespace *ns = seq->private;
411 struct uid_gid_extent *extent = v;
412 struct user_namespace *lower_ns;
415 lower_ns = seq_user_ns(seq);
416 if ((lower_ns == ns) && lower_ns->parent)
417 lower_ns = lower_ns->parent;
419 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
421 seq_printf(seq, "%10u %10u %10u\n",
429 static int gid_m_show(struct seq_file *seq, void *v)
431 struct user_namespace *ns = seq->private;
432 struct uid_gid_extent *extent = v;
433 struct user_namespace *lower_ns;
436 lower_ns = seq_user_ns(seq);
437 if ((lower_ns == ns) && lower_ns->parent)
438 lower_ns = lower_ns->parent;
440 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
442 seq_printf(seq, "%10u %10u %10u\n",
450 static int projid_m_show(struct seq_file *seq, void *v)
452 struct user_namespace *ns = seq->private;
453 struct uid_gid_extent *extent = v;
454 struct user_namespace *lower_ns;
457 lower_ns = seq_user_ns(seq);
458 if ((lower_ns == ns) && lower_ns->parent)
459 lower_ns = lower_ns->parent;
461 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
463 seq_printf(seq, "%10u %10u %10u\n",
471 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
473 struct uid_gid_extent *extent = NULL;
476 if (pos < map->nr_extents)
477 extent = &map->extent[pos];
482 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
484 struct user_namespace *ns = seq->private;
486 return m_start(seq, ppos, &ns->uid_map);
489 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
491 struct user_namespace *ns = seq->private;
493 return m_start(seq, ppos, &ns->gid_map);
496 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
498 struct user_namespace *ns = seq->private;
500 return m_start(seq, ppos, &ns->projid_map);
503 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
506 return seq->op->start(seq, pos);
509 static void m_stop(struct seq_file *seq, void *v)
514 struct seq_operations proc_uid_seq_operations = {
515 .start = uid_m_start,
521 struct seq_operations proc_gid_seq_operations = {
522 .start = gid_m_start,
528 struct seq_operations proc_projid_seq_operations = {
529 .start = projid_m_start,
532 .show = projid_m_show,
535 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
537 u32 upper_first, lower_first, upper_last, lower_last;
540 upper_first = extent->first;
541 lower_first = extent->lower_first;
542 upper_last = upper_first + extent->count - 1;
543 lower_last = lower_first + extent->count - 1;
545 for (idx = 0; idx < new_map->nr_extents; idx++) {
546 u32 prev_upper_first, prev_lower_first;
547 u32 prev_upper_last, prev_lower_last;
548 struct uid_gid_extent *prev;
550 prev = &new_map->extent[idx];
552 prev_upper_first = prev->first;
553 prev_lower_first = prev->lower_first;
554 prev_upper_last = prev_upper_first + prev->count - 1;
555 prev_lower_last = prev_lower_first + prev->count - 1;
557 /* Does the upper range intersect a previous extent? */
558 if ((prev_upper_first <= upper_last) &&
559 (prev_upper_last >= upper_first))
562 /* Does the lower range intersect a previous extent? */
563 if ((prev_lower_first <= lower_last) &&
564 (prev_lower_last >= lower_first))
571 static DEFINE_MUTEX(id_map_mutex);
573 static ssize_t map_write(struct file *file, const char __user *buf,
574 size_t count, loff_t *ppos,
576 struct uid_gid_map *map,
577 struct uid_gid_map *parent_map)
579 struct seq_file *seq = file->private_data;
580 struct user_namespace *ns = seq->private;
581 struct uid_gid_map new_map;
583 struct uid_gid_extent *extent = NULL;
584 unsigned long page = 0;
585 char *kbuf, *pos, *next_line;
586 ssize_t ret = -EINVAL;
589 * The id_map_mutex serializes all writes to any given map.
591 * Any map is only ever written once.
593 * An id map fits within 1 cache line on most architectures.
595 * On read nothing needs to be done unless you are on an
596 * architecture with a crazy cache coherency model like alpha.
598 * There is a one time data dependency between reading the
599 * count of the extents and the values of the extents. The
600 * desired behavior is to see the values of the extents that
601 * were written before the count of the extents.
603 * To achieve this smp_wmb() is used on guarantee the write
604 * order and smp_read_barrier_depends() is guaranteed that we
605 * don't have crazy architectures returning stale data.
608 mutex_lock(&id_map_mutex);
611 /* Only allow one successful write to the map */
612 if (map->nr_extents != 0)
615 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
616 * over the user namespace in order to set the id mapping.
618 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
623 page = __get_free_page(GFP_TEMPORARY);
624 kbuf = (char *) page;
628 /* Only allow <= page size writes at the beginning of the file */
630 if ((*ppos != 0) || (count >= PAGE_SIZE))
633 /* Slurp in the user data */
635 if (copy_from_user(kbuf, buf, count))
639 /* Parse the user data */
642 new_map.nr_extents = 0;
643 for (;pos; pos = next_line) {
644 extent = &new_map.extent[new_map.nr_extents];
646 /* Find the end of line and ensure I don't look past it */
647 next_line = strchr(pos, '\n');
651 if (*next_line == '\0')
655 pos = skip_spaces(pos);
656 extent->first = simple_strtoul(pos, &pos, 10);
660 pos = skip_spaces(pos);
661 extent->lower_first = simple_strtoul(pos, &pos, 10);
665 pos = skip_spaces(pos);
666 extent->count = simple_strtoul(pos, &pos, 10);
667 if (*pos && !isspace(*pos))
670 /* Verify there is not trailing junk on the line */
671 pos = skip_spaces(pos);
675 /* Verify we have been given valid starting values */
676 if ((extent->first == (u32) -1) ||
677 (extent->lower_first == (u32) -1 ))
680 /* Verify count is not zero and does not cause the extent to wrap */
681 if ((extent->first + extent->count) <= extent->first)
683 if ((extent->lower_first + extent->count) <= extent->lower_first)
686 /* Do the ranges in extent overlap any previous extents? */
687 if (mappings_overlap(&new_map, extent))
690 new_map.nr_extents++;
692 /* Fail if the file contains too many extents */
693 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
697 /* Be very certaint the new map actually exists */
698 if (new_map.nr_extents == 0)
702 /* Validate the user is allowed to use user id's mapped to. */
703 if (!new_idmap_permitted(ns, cap_setid, &new_map))
706 /* Map the lower ids from the parent user namespace to the
707 * kernel global id space.
709 for (idx = 0; idx < new_map.nr_extents; idx++) {
711 extent = &new_map.extent[idx];
713 lower_first = map_id_range_down(parent_map,
717 /* Fail if we can not map the specified extent to
718 * the kernel global id space.
720 if (lower_first == (u32) -1)
723 extent->lower_first = lower_first;
726 /* Install the map */
727 memcpy(map->extent, new_map.extent,
728 new_map.nr_extents*sizeof(new_map.extent[0]));
730 map->nr_extents = new_map.nr_extents;
735 mutex_unlock(&id_map_mutex);
741 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
743 struct seq_file *seq = file->private_data;
744 struct user_namespace *ns = seq->private;
745 struct user_namespace *seq_ns = seq_user_ns(seq);
750 if ((seq_ns != ns) && (seq_ns != ns->parent))
753 return map_write(file, buf, size, ppos, CAP_SETUID,
754 &ns->uid_map, &ns->parent->uid_map);
757 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
759 struct seq_file *seq = file->private_data;
760 struct user_namespace *ns = seq->private;
761 struct user_namespace *seq_ns = seq_user_ns(seq);
766 if ((seq_ns != ns) && (seq_ns != ns->parent))
769 return map_write(file, buf, size, ppos, CAP_SETGID,
770 &ns->gid_map, &ns->parent->gid_map);
773 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
775 struct seq_file *seq = file->private_data;
776 struct user_namespace *ns = seq->private;
777 struct user_namespace *seq_ns = seq_user_ns(seq);
782 if ((seq_ns != ns) && (seq_ns != ns->parent))
785 /* Anyone can set any valid project id no capability needed */
786 return map_write(file, buf, size, ppos, -1,
787 &ns->projid_map, &ns->parent->projid_map);
790 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
791 struct uid_gid_map *new_map)
793 /* Allow mapping to your own filesystem ids */
794 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
795 u32 id = new_map->extent[0].lower_first;
796 if (cap_setid == CAP_SETUID) {
797 kuid_t uid = make_kuid(ns->parent, id);
798 if (uid_eq(uid, current_fsuid()))
801 else if (cap_setid == CAP_SETGID) {
802 kgid_t gid = make_kgid(ns->parent, id);
803 if (gid_eq(gid, current_fsgid()))
808 /* Allow anyone to set a mapping that doesn't require privilege */
809 if (!cap_valid(cap_setid))
812 /* Allow the specified ids if we have the appropriate capability
813 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
815 if (ns_capable(ns->parent, cap_setid))
821 static void *userns_get(struct task_struct *task)
823 struct user_namespace *user_ns;
826 user_ns = get_user_ns(__task_cred(task)->user_ns);
832 static void userns_put(void *ns)
837 static int userns_install(struct nsproxy *nsproxy, void *ns)
839 struct user_namespace *user_ns = ns;
842 /* Don't allow gaining capabilities by reentering
843 * the same user namespace.
845 if (user_ns == current_user_ns())
848 /* Threaded processes may not enter a different user namespace */
849 if (atomic_read(¤t->mm->mm_users) > 1)
852 if (current->fs->users != 1)
855 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
858 cred = prepare_creds();
862 put_user_ns(cred->user_ns);
863 set_cred_user_ns(cred, get_user_ns(user_ns));
865 return commit_creds(cred);
868 static unsigned int userns_inum(void *ns)
870 struct user_namespace *user_ns = ns;
871 return user_ns->proc_inum;
874 const struct proc_ns_operations userns_operations = {
876 .type = CLONE_NEWUSER,
879 .install = userns_install,
883 static __init int user_namespaces_init(void)
885 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
888 module_init(user_namespaces_init);