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;
66 * Verify that we can not violate the policy of which files
67 * may be accessed that is specified by the root directory,
68 * by verifing that the root directory is at the root of the
69 * mount namespace which allows all files to be accessed.
71 if (current_chrooted())
74 /* The creator needs a mapping in the parent user namespace
75 * or else we won't be able to reasonably tell userspace who
76 * created a user_namespace.
78 if (!kuid_has_mapping(parent_ns, owner) ||
79 !kgid_has_mapping(parent_ns, group))
82 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
86 ret = proc_alloc_inum(&ns->proc_inum);
88 kmem_cache_free(user_ns_cachep, ns);
92 atomic_set(&ns->count, 1);
93 /* Leave the new->user_ns reference with the new user namespace. */
94 ns->parent = parent_ns;
98 set_cred_user_ns(new, ns);
100 update_mnt_policy(ns);
105 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
109 if (!(unshare_flags & CLONE_NEWUSER))
112 cred = prepare_creds();
117 return create_user_ns(cred);
120 void free_user_ns(struct user_namespace *ns)
122 struct user_namespace *parent;
126 proc_free_inum(ns->proc_inum);
127 kmem_cache_free(user_ns_cachep, ns);
129 } while (atomic_dec_and_test(&parent->count));
131 EXPORT_SYMBOL(free_user_ns);
133 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
135 unsigned idx, extents;
136 u32 first, last, id2;
138 id2 = id + count - 1;
140 /* Find the matching extent */
141 extents = map->nr_extents;
142 smp_read_barrier_depends();
143 for (idx = 0; idx < extents; idx++) {
144 first = map->extent[idx].first;
145 last = first + map->extent[idx].count - 1;
146 if (id >= first && id <= last &&
147 (id2 >= first && id2 <= last))
150 /* Map the id or note failure */
152 id = (id - first) + map->extent[idx].lower_first;
159 static u32 map_id_down(struct uid_gid_map *map, u32 id)
161 unsigned idx, extents;
164 /* Find the matching extent */
165 extents = map->nr_extents;
166 smp_read_barrier_depends();
167 for (idx = 0; idx < extents; idx++) {
168 first = map->extent[idx].first;
169 last = first + map->extent[idx].count - 1;
170 if (id >= first && id <= last)
173 /* Map the id or note failure */
175 id = (id - first) + map->extent[idx].lower_first;
182 static u32 map_id_up(struct uid_gid_map *map, u32 id)
184 unsigned idx, extents;
187 /* Find the matching extent */
188 extents = map->nr_extents;
189 smp_read_barrier_depends();
190 for (idx = 0; idx < extents; idx++) {
191 first = map->extent[idx].lower_first;
192 last = first + map->extent[idx].count - 1;
193 if (id >= first && id <= last)
196 /* Map the id or note failure */
198 id = (id - first) + map->extent[idx].first;
206 * make_kuid - Map a user-namespace uid pair into a kuid.
207 * @ns: User namespace that the uid is in
208 * @uid: User identifier
210 * Maps a user-namespace uid pair into a kernel internal kuid,
211 * and returns that kuid.
213 * When there is no mapping defined for the user-namespace uid
214 * pair INVALID_UID is returned. Callers are expected to test
215 * for and handle handle INVALID_UID being returned. INVALID_UID
216 * may be tested for using uid_valid().
218 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
220 /* Map the uid to a global kernel uid */
221 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
223 EXPORT_SYMBOL(make_kuid);
226 * from_kuid - Create a uid from a kuid user-namespace pair.
227 * @targ: The user namespace we want a uid in.
228 * @kuid: The kernel internal uid to start with.
230 * Map @kuid into the user-namespace specified by @targ and
231 * return the resulting uid.
233 * There is always a mapping into the initial user_namespace.
235 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
237 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
239 /* Map the uid from a global kernel uid */
240 return map_id_up(&targ->uid_map, __kuid_val(kuid));
242 EXPORT_SYMBOL(from_kuid);
245 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
246 * @targ: The user namespace we want a uid in.
247 * @kuid: The kernel internal uid to start with.
249 * Map @kuid into the user-namespace specified by @targ and
250 * return the resulting uid.
252 * There is always a mapping into the initial user_namespace.
254 * Unlike from_kuid from_kuid_munged never fails and always
255 * returns a valid uid. This makes from_kuid_munged appropriate
256 * for use in syscalls like stat and getuid where failing the
257 * system call and failing to provide a valid uid are not an
260 * If @kuid has no mapping in @targ overflowuid is returned.
262 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
265 uid = from_kuid(targ, kuid);
267 if (uid == (uid_t) -1)
271 EXPORT_SYMBOL(from_kuid_munged);
274 * make_kgid - Map a user-namespace gid pair into a kgid.
275 * @ns: User namespace that the gid is in
276 * @uid: group identifier
278 * Maps a user-namespace gid pair into a kernel internal kgid,
279 * and returns that kgid.
281 * When there is no mapping defined for the user-namespace gid
282 * pair INVALID_GID is returned. Callers are expected to test
283 * for and handle INVALID_GID being returned. INVALID_GID may be
284 * tested for using gid_valid().
286 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
288 /* Map the gid to a global kernel gid */
289 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
291 EXPORT_SYMBOL(make_kgid);
294 * from_kgid - Create a gid from a kgid user-namespace pair.
295 * @targ: The user namespace we want a gid in.
296 * @kgid: The kernel internal gid to start with.
298 * Map @kgid into the user-namespace specified by @targ and
299 * return the resulting gid.
301 * There is always a mapping into the initial user_namespace.
303 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
305 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
307 /* Map the gid from a global kernel gid */
308 return map_id_up(&targ->gid_map, __kgid_val(kgid));
310 EXPORT_SYMBOL(from_kgid);
313 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
314 * @targ: The user namespace we want a gid in.
315 * @kgid: The kernel internal gid to start with.
317 * Map @kgid into the user-namespace specified by @targ and
318 * return the resulting gid.
320 * There is always a mapping into the initial user_namespace.
322 * Unlike from_kgid from_kgid_munged never fails and always
323 * returns a valid gid. This makes from_kgid_munged appropriate
324 * for use in syscalls like stat and getgid where failing the
325 * system call and failing to provide a valid gid are not options.
327 * If @kgid has no mapping in @targ overflowgid is returned.
329 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
332 gid = from_kgid(targ, kgid);
334 if (gid == (gid_t) -1)
338 EXPORT_SYMBOL(from_kgid_munged);
341 * make_kprojid - Map a user-namespace projid pair into a kprojid.
342 * @ns: User namespace that the projid is in
343 * @projid: Project identifier
345 * Maps a user-namespace uid pair into a kernel internal kuid,
346 * and returns that kuid.
348 * When there is no mapping defined for the user-namespace projid
349 * pair INVALID_PROJID is returned. Callers are expected to test
350 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
351 * may be tested for using projid_valid().
353 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
355 /* Map the uid to a global kernel uid */
356 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
358 EXPORT_SYMBOL(make_kprojid);
361 * from_kprojid - Create a projid from a kprojid user-namespace pair.
362 * @targ: The user namespace we want a projid in.
363 * @kprojid: The kernel internal project identifier to start with.
365 * Map @kprojid into the user-namespace specified by @targ and
366 * return the resulting projid.
368 * There is always a mapping into the initial user_namespace.
370 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
372 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
374 /* Map the uid from a global kernel uid */
375 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
377 EXPORT_SYMBOL(from_kprojid);
380 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
381 * @targ: The user namespace we want a projid in.
382 * @kprojid: The kernel internal projid to start with.
384 * Map @kprojid into the user-namespace specified by @targ and
385 * return the resulting projid.
387 * There is always a mapping into the initial user_namespace.
389 * Unlike from_kprojid from_kprojid_munged never fails and always
390 * returns a valid projid. This makes from_kprojid_munged
391 * appropriate for use in syscalls like stat and where
392 * failing the system call and failing to provide a valid projid are
395 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
397 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
400 projid = from_kprojid(targ, kprojid);
402 if (projid == (projid_t) -1)
403 projid = OVERFLOW_PROJID;
406 EXPORT_SYMBOL(from_kprojid_munged);
409 static int uid_m_show(struct seq_file *seq, void *v)
411 struct user_namespace *ns = seq->private;
412 struct uid_gid_extent *extent = v;
413 struct user_namespace *lower_ns;
416 lower_ns = seq_user_ns(seq);
417 if ((lower_ns == ns) && lower_ns->parent)
418 lower_ns = lower_ns->parent;
420 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
422 seq_printf(seq, "%10u %10u %10u\n",
430 static int gid_m_show(struct seq_file *seq, void *v)
432 struct user_namespace *ns = seq->private;
433 struct uid_gid_extent *extent = v;
434 struct user_namespace *lower_ns;
437 lower_ns = seq_user_ns(seq);
438 if ((lower_ns == ns) && lower_ns->parent)
439 lower_ns = lower_ns->parent;
441 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
443 seq_printf(seq, "%10u %10u %10u\n",
451 static int projid_m_show(struct seq_file *seq, void *v)
453 struct user_namespace *ns = seq->private;
454 struct uid_gid_extent *extent = v;
455 struct user_namespace *lower_ns;
458 lower_ns = seq_user_ns(seq);
459 if ((lower_ns == ns) && lower_ns->parent)
460 lower_ns = lower_ns->parent;
462 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
464 seq_printf(seq, "%10u %10u %10u\n",
472 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
474 struct uid_gid_extent *extent = NULL;
477 if (pos < map->nr_extents)
478 extent = &map->extent[pos];
483 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
485 struct user_namespace *ns = seq->private;
487 return m_start(seq, ppos, &ns->uid_map);
490 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
492 struct user_namespace *ns = seq->private;
494 return m_start(seq, ppos, &ns->gid_map);
497 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
499 struct user_namespace *ns = seq->private;
501 return m_start(seq, ppos, &ns->projid_map);
504 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
507 return seq->op->start(seq, pos);
510 static void m_stop(struct seq_file *seq, void *v)
515 struct seq_operations proc_uid_seq_operations = {
516 .start = uid_m_start,
522 struct seq_operations proc_gid_seq_operations = {
523 .start = gid_m_start,
529 struct seq_operations proc_projid_seq_operations = {
530 .start = projid_m_start,
533 .show = projid_m_show,
536 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
538 u32 upper_first, lower_first, upper_last, lower_last;
541 upper_first = extent->first;
542 lower_first = extent->lower_first;
543 upper_last = upper_first + extent->count - 1;
544 lower_last = lower_first + extent->count - 1;
546 for (idx = 0; idx < new_map->nr_extents; idx++) {
547 u32 prev_upper_first, prev_lower_first;
548 u32 prev_upper_last, prev_lower_last;
549 struct uid_gid_extent *prev;
551 prev = &new_map->extent[idx];
553 prev_upper_first = prev->first;
554 prev_lower_first = prev->lower_first;
555 prev_upper_last = prev_upper_first + prev->count - 1;
556 prev_lower_last = prev_lower_first + prev->count - 1;
558 /* Does the upper range intersect a previous extent? */
559 if ((prev_upper_first <= upper_last) &&
560 (prev_upper_last >= upper_first))
563 /* Does the lower range intersect a previous extent? */
564 if ((prev_lower_first <= lower_last) &&
565 (prev_lower_last >= lower_first))
572 static DEFINE_MUTEX(id_map_mutex);
574 static ssize_t map_write(struct file *file, const char __user *buf,
575 size_t count, loff_t *ppos,
577 struct uid_gid_map *map,
578 struct uid_gid_map *parent_map)
580 struct seq_file *seq = file->private_data;
581 struct user_namespace *ns = seq->private;
582 struct uid_gid_map new_map;
584 struct uid_gid_extent *extent = NULL;
585 unsigned long page = 0;
586 char *kbuf, *pos, *next_line;
587 ssize_t ret = -EINVAL;
590 * The id_map_mutex serializes all writes to any given map.
592 * Any map is only ever written once.
594 * An id map fits within 1 cache line on most architectures.
596 * On read nothing needs to be done unless you are on an
597 * architecture with a crazy cache coherency model like alpha.
599 * There is a one time data dependency between reading the
600 * count of the extents and the values of the extents. The
601 * desired behavior is to see the values of the extents that
602 * were written before the count of the extents.
604 * To achieve this smp_wmb() is used on guarantee the write
605 * order and smp_read_barrier_depends() is guaranteed that we
606 * don't have crazy architectures returning stale data.
609 mutex_lock(&id_map_mutex);
612 /* Only allow one successful write to the map */
613 if (map->nr_extents != 0)
617 * Adjusting namespace settings requires capabilities on the target.
619 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
624 page = __get_free_page(GFP_TEMPORARY);
625 kbuf = (char *) page;
629 /* Only allow <= page size writes at the beginning of the file */
631 if ((*ppos != 0) || (count >= PAGE_SIZE))
634 /* Slurp in the user data */
636 if (copy_from_user(kbuf, buf, count))
640 /* Parse the user data */
643 new_map.nr_extents = 0;
644 for (;pos; pos = next_line) {
645 extent = &new_map.extent[new_map.nr_extents];
647 /* Find the end of line and ensure I don't look past it */
648 next_line = strchr(pos, '\n');
652 if (*next_line == '\0')
656 pos = skip_spaces(pos);
657 extent->first = simple_strtoul(pos, &pos, 10);
661 pos = skip_spaces(pos);
662 extent->lower_first = simple_strtoul(pos, &pos, 10);
666 pos = skip_spaces(pos);
667 extent->count = simple_strtoul(pos, &pos, 10);
668 if (*pos && !isspace(*pos))
671 /* Verify there is not trailing junk on the line */
672 pos = skip_spaces(pos);
676 /* Verify we have been given valid starting values */
677 if ((extent->first == (u32) -1) ||
678 (extent->lower_first == (u32) -1 ))
681 /* Verify count is not zero and does not cause the extent to wrap */
682 if ((extent->first + extent->count) <= extent->first)
684 if ((extent->lower_first + extent->count) <= extent->lower_first)
687 /* Do the ranges in extent overlap any previous extents? */
688 if (mappings_overlap(&new_map, extent))
691 new_map.nr_extents++;
693 /* Fail if the file contains too many extents */
694 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
698 /* Be very certaint the new map actually exists */
699 if (new_map.nr_extents == 0)
703 /* Validate the user is allowed to use user id's mapped to. */
704 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
707 /* Map the lower ids from the parent user namespace to the
708 * kernel global id space.
710 for (idx = 0; idx < new_map.nr_extents; idx++) {
712 extent = &new_map.extent[idx];
714 lower_first = map_id_range_down(parent_map,
718 /* Fail if we can not map the specified extent to
719 * the kernel global id space.
721 if (lower_first == (u32) -1)
724 extent->lower_first = lower_first;
727 /* Install the map */
728 memcpy(map->extent, new_map.extent,
729 new_map.nr_extents*sizeof(new_map.extent[0]));
731 map->nr_extents = new_map.nr_extents;
736 mutex_unlock(&id_map_mutex);
742 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
744 struct seq_file *seq = file->private_data;
745 struct user_namespace *ns = seq->private;
746 struct user_namespace *seq_ns = seq_user_ns(seq);
751 if ((seq_ns != ns) && (seq_ns != ns->parent))
754 return map_write(file, buf, size, ppos, CAP_SETUID,
755 &ns->uid_map, &ns->parent->uid_map);
758 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
760 struct seq_file *seq = file->private_data;
761 struct user_namespace *ns = seq->private;
762 struct user_namespace *seq_ns = seq_user_ns(seq);
767 if ((seq_ns != ns) && (seq_ns != ns->parent))
770 return map_write(file, buf, size, ppos, CAP_SETGID,
771 &ns->gid_map, &ns->parent->gid_map);
774 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
776 struct seq_file *seq = file->private_data;
777 struct user_namespace *ns = seq->private;
778 struct user_namespace *seq_ns = seq_user_ns(seq);
783 if ((seq_ns != ns) && (seq_ns != ns->parent))
786 /* Anyone can set any valid project id no capability needed */
787 return map_write(file, buf, size, ppos, -1,
788 &ns->projid_map, &ns->parent->projid_map);
791 static bool new_idmap_permitted(const struct file *file,
792 struct user_namespace *ns, int cap_setid,
793 struct uid_gid_map *new_map)
795 /* Allow mapping to your own filesystem ids */
796 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
797 u32 id = new_map->extent[0].lower_first;
798 if (cap_setid == CAP_SETUID) {
799 kuid_t uid = make_kuid(ns->parent, id);
800 if (uid_eq(uid, file->f_cred->fsuid))
803 else if (cap_setid == CAP_SETGID) {
804 kgid_t gid = make_kgid(ns->parent, id);
805 if (gid_eq(gid, file->f_cred->fsgid))
810 /* Allow anyone to set a mapping that doesn't require privilege */
811 if (!cap_valid(cap_setid))
814 /* Allow the specified ids if we have the appropriate capability
815 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
816 * And the opener of the id file also had the approprpiate capability.
818 if (ns_capable(ns->parent, cap_setid) &&
819 file_ns_capable(file, ns->parent, cap_setid))
825 static void *userns_get(struct task_struct *task)
827 struct user_namespace *user_ns;
830 user_ns = get_user_ns(__task_cred(task)->user_ns);
836 static void userns_put(void *ns)
841 static int userns_install(struct nsproxy *nsproxy, void *ns)
843 struct user_namespace *user_ns = ns;
846 /* Don't allow gaining capabilities by reentering
847 * the same user namespace.
849 if (user_ns == current_user_ns())
852 /* Threaded processes may not enter a different user namespace */
853 if (atomic_read(¤t->mm->mm_users) > 1)
856 if (current->fs->users != 1)
859 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
862 cred = prepare_creds();
866 put_user_ns(cred->user_ns);
867 set_cred_user_ns(cred, get_user_ns(user_ns));
869 return commit_creds(cred);
872 static unsigned int userns_inum(void *ns)
874 struct user_namespace *user_ns = ns;
875 return user_ns->proc_inum;
878 const struct proc_ns_operations userns_operations = {
880 .type = CLONE_NEWUSER,
883 .install = userns_install,
887 static __init int user_namespaces_init(void)
889 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
892 module_init(user_namespaces_init);