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;
62 /* The creator needs a mapping in the parent user namespace
63 * or else we won't be able to reasonably tell userspace who
64 * created a user_namespace.
66 if (!kuid_has_mapping(parent_ns, owner) ||
67 !kgid_has_mapping(parent_ns, group))
70 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
75 /* Leave the new->user_ns reference with the new user namespace. */
76 ns->parent = parent_ns;
80 set_cred_user_ns(new, ns);
85 void free_user_ns(struct kref *kref)
87 struct user_namespace *parent, *ns =
88 container_of(kref, struct user_namespace, kref);
91 kmem_cache_free(user_ns_cachep, ns);
94 EXPORT_SYMBOL(free_user_ns);
96 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
98 unsigned idx, extents;
101 id2 = id + count - 1;
103 /* Find the matching extent */
104 extents = map->nr_extents;
105 smp_read_barrier_depends();
106 for (idx = 0; idx < extents; idx++) {
107 first = map->extent[idx].first;
108 last = first + map->extent[idx].count - 1;
109 if (id >= first && id <= last &&
110 (id2 >= first && id2 <= last))
113 /* Map the id or note failure */
115 id = (id - first) + map->extent[idx].lower_first;
122 static u32 map_id_down(struct uid_gid_map *map, u32 id)
124 unsigned idx, extents;
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)
136 /* Map the id or note failure */
138 id = (id - first) + map->extent[idx].lower_first;
145 static u32 map_id_up(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].lower_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].first;
169 * make_kuid - Map a user-namespace uid pair into a kuid.
170 * @ns: User namespace that the uid is in
171 * @uid: User identifier
173 * Maps a user-namespace uid pair into a kernel internal kuid,
174 * and returns that kuid.
176 * When there is no mapping defined for the user-namespace uid
177 * pair INVALID_UID is returned. Callers are expected to test
178 * for and handle handle INVALID_UID being returned. INVALID_UID
179 * may be tested for using uid_valid().
181 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
183 /* Map the uid to a global kernel uid */
184 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
186 EXPORT_SYMBOL(make_kuid);
189 * from_kuid - Create a uid from a kuid user-namespace pair.
190 * @targ: The user namespace we want a uid in.
191 * @kuid: The kernel internal uid to start with.
193 * Map @kuid into the user-namespace specified by @targ and
194 * return the resulting uid.
196 * There is always a mapping into the initial user_namespace.
198 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
200 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
202 /* Map the uid from a global kernel uid */
203 return map_id_up(&targ->uid_map, __kuid_val(kuid));
205 EXPORT_SYMBOL(from_kuid);
208 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
209 * @targ: The user namespace we want a uid in.
210 * @kuid: The kernel internal uid to start with.
212 * Map @kuid into the user-namespace specified by @targ and
213 * return the resulting uid.
215 * There is always a mapping into the initial user_namespace.
217 * Unlike from_kuid from_kuid_munged never fails and always
218 * returns a valid uid. This makes from_kuid_munged appropriate
219 * for use in syscalls like stat and getuid where failing the
220 * system call and failing to provide a valid uid are not an
223 * If @kuid has no mapping in @targ overflowuid is returned.
225 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
228 uid = from_kuid(targ, kuid);
230 if (uid == (uid_t) -1)
234 EXPORT_SYMBOL(from_kuid_munged);
237 * make_kgid - Map a user-namespace gid pair into a kgid.
238 * @ns: User namespace that the gid is in
239 * @uid: group identifier
241 * Maps a user-namespace gid pair into a kernel internal kgid,
242 * and returns that kgid.
244 * When there is no mapping defined for the user-namespace gid
245 * pair INVALID_GID is returned. Callers are expected to test
246 * for and handle INVALID_GID being returned. INVALID_GID may be
247 * tested for using gid_valid().
249 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
251 /* Map the gid to a global kernel gid */
252 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
254 EXPORT_SYMBOL(make_kgid);
257 * from_kgid - Create a gid from a kgid user-namespace pair.
258 * @targ: The user namespace we want a gid in.
259 * @kgid: The kernel internal gid to start with.
261 * Map @kgid into the user-namespace specified by @targ and
262 * return the resulting gid.
264 * There is always a mapping into the initial user_namespace.
266 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
268 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
270 /* Map the gid from a global kernel gid */
271 return map_id_up(&targ->gid_map, __kgid_val(kgid));
273 EXPORT_SYMBOL(from_kgid);
276 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
277 * @targ: The user namespace we want a gid in.
278 * @kgid: The kernel internal gid to start with.
280 * Map @kgid into the user-namespace specified by @targ and
281 * return the resulting gid.
283 * There is always a mapping into the initial user_namespace.
285 * Unlike from_kgid from_kgid_munged never fails and always
286 * returns a valid gid. This makes from_kgid_munged appropriate
287 * for use in syscalls like stat and getgid where failing the
288 * system call and failing to provide a valid gid are not options.
290 * If @kgid has no mapping in @targ overflowgid is returned.
292 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
295 gid = from_kgid(targ, kgid);
297 if (gid == (gid_t) -1)
301 EXPORT_SYMBOL(from_kgid_munged);
304 * make_kprojid - Map a user-namespace projid pair into a kprojid.
305 * @ns: User namespace that the projid is in
306 * @projid: Project identifier
308 * Maps a user-namespace uid pair into a kernel internal kuid,
309 * and returns that kuid.
311 * When there is no mapping defined for the user-namespace projid
312 * pair INVALID_PROJID is returned. Callers are expected to test
313 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
314 * may be tested for using projid_valid().
316 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
318 /* Map the uid to a global kernel uid */
319 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
321 EXPORT_SYMBOL(make_kprojid);
324 * from_kprojid - Create a projid from a kprojid user-namespace pair.
325 * @targ: The user namespace we want a projid in.
326 * @kprojid: The kernel internal project identifier to start with.
328 * Map @kprojid into the user-namespace specified by @targ and
329 * return the resulting projid.
331 * There is always a mapping into the initial user_namespace.
333 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
335 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
337 /* Map the uid from a global kernel uid */
338 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
340 EXPORT_SYMBOL(from_kprojid);
343 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
344 * @targ: The user namespace we want a projid in.
345 * @kprojid: The kernel internal projid to start with.
347 * Map @kprojid into the user-namespace specified by @targ and
348 * return the resulting projid.
350 * There is always a mapping into the initial user_namespace.
352 * Unlike from_kprojid from_kprojid_munged never fails and always
353 * returns a valid projid. This makes from_kprojid_munged
354 * appropriate for use in syscalls like stat and where
355 * failing the system call and failing to provide a valid projid are
358 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
360 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
363 projid = from_kprojid(targ, kprojid);
365 if (projid == (projid_t) -1)
366 projid = OVERFLOW_PROJID;
369 EXPORT_SYMBOL(from_kprojid_munged);
372 static int uid_m_show(struct seq_file *seq, void *v)
374 struct user_namespace *ns = seq->private;
375 struct uid_gid_extent *extent = v;
376 struct user_namespace *lower_ns;
379 lower_ns = current_user_ns();
380 if ((lower_ns == ns) && lower_ns->parent)
381 lower_ns = lower_ns->parent;
383 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
385 seq_printf(seq, "%10u %10u %10u\n",
393 static int gid_m_show(struct seq_file *seq, void *v)
395 struct user_namespace *ns = seq->private;
396 struct uid_gid_extent *extent = v;
397 struct user_namespace *lower_ns;
400 lower_ns = current_user_ns();
401 if ((lower_ns == ns) && lower_ns->parent)
402 lower_ns = lower_ns->parent;
404 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
406 seq_printf(seq, "%10u %10u %10u\n",
414 static int projid_m_show(struct seq_file *seq, void *v)
416 struct user_namespace *ns = seq->private;
417 struct uid_gid_extent *extent = v;
418 struct user_namespace *lower_ns;
421 lower_ns = seq_user_ns(seq);
422 if ((lower_ns == ns) && lower_ns->parent)
423 lower_ns = lower_ns->parent;
425 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
427 seq_printf(seq, "%10u %10u %10u\n",
435 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
437 struct uid_gid_extent *extent = NULL;
440 if (pos < map->nr_extents)
441 extent = &map->extent[pos];
446 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
448 struct user_namespace *ns = seq->private;
450 return m_start(seq, ppos, &ns->uid_map);
453 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
455 struct user_namespace *ns = seq->private;
457 return m_start(seq, ppos, &ns->gid_map);
460 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
462 struct user_namespace *ns = seq->private;
464 return m_start(seq, ppos, &ns->projid_map);
467 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
470 return seq->op->start(seq, pos);
473 static void m_stop(struct seq_file *seq, void *v)
478 struct seq_operations proc_uid_seq_operations = {
479 .start = uid_m_start,
485 struct seq_operations proc_gid_seq_operations = {
486 .start = gid_m_start,
492 struct seq_operations proc_projid_seq_operations = {
493 .start = projid_m_start,
496 .show = projid_m_show,
499 static DEFINE_MUTEX(id_map_mutex);
501 static ssize_t map_write(struct file *file, const char __user *buf,
502 size_t count, loff_t *ppos,
504 struct uid_gid_map *map,
505 struct uid_gid_map *parent_map)
507 struct seq_file *seq = file->private_data;
508 struct user_namespace *ns = seq->private;
509 struct uid_gid_map new_map;
511 struct uid_gid_extent *extent, *last = NULL;
512 unsigned long page = 0;
513 char *kbuf, *pos, *next_line;
514 ssize_t ret = -EINVAL;
517 * The id_map_mutex serializes all writes to any given map.
519 * Any map is only ever written once.
521 * An id map fits within 1 cache line on most architectures.
523 * On read nothing needs to be done unless you are on an
524 * architecture with a crazy cache coherency model like alpha.
526 * There is a one time data dependency between reading the
527 * count of the extents and the values of the extents. The
528 * desired behavior is to see the values of the extents that
529 * were written before the count of the extents.
531 * To achieve this smp_wmb() is used on guarantee the write
532 * order and smp_read_barrier_depends() is guaranteed that we
533 * don't have crazy architectures returning stale data.
536 mutex_lock(&id_map_mutex);
539 /* Only allow one successful write to the map */
540 if (map->nr_extents != 0)
543 /* Require the appropriate privilege CAP_SETUID or CAP_SETGID
544 * over the user namespace in order to set the id mapping.
546 if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid))
551 page = __get_free_page(GFP_TEMPORARY);
552 kbuf = (char *) page;
556 /* Only allow <= page size writes at the beginning of the file */
558 if ((*ppos != 0) || (count >= PAGE_SIZE))
561 /* Slurp in the user data */
563 if (copy_from_user(kbuf, buf, count))
567 /* Parse the user data */
570 new_map.nr_extents = 0;
571 for (;pos; pos = next_line) {
572 extent = &new_map.extent[new_map.nr_extents];
574 /* Find the end of line and ensure I don't look past it */
575 next_line = strchr(pos, '\n');
579 if (*next_line == '\0')
583 pos = skip_spaces(pos);
584 extent->first = simple_strtoul(pos, &pos, 10);
588 pos = skip_spaces(pos);
589 extent->lower_first = simple_strtoul(pos, &pos, 10);
593 pos = skip_spaces(pos);
594 extent->count = simple_strtoul(pos, &pos, 10);
595 if (*pos && !isspace(*pos))
598 /* Verify there is not trailing junk on the line */
599 pos = skip_spaces(pos);
603 /* Verify we have been given valid starting values */
604 if ((extent->first == (u32) -1) ||
605 (extent->lower_first == (u32) -1 ))
608 /* Verify count is not zero and does not cause the extent to wrap */
609 if ((extent->first + extent->count) <= extent->first)
611 if ((extent->lower_first + extent->count) <= extent->lower_first)
614 /* For now only accept extents that are strictly in order */
616 (((last->first + last->count) > extent->first) ||
617 ((last->lower_first + last->count) > extent->lower_first)))
620 new_map.nr_extents++;
623 /* Fail if the file contains too many extents */
624 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
628 /* Be very certaint the new map actually exists */
629 if (new_map.nr_extents == 0)
633 /* Validate the user is allowed to use user id's mapped to. */
634 if (!new_idmap_permitted(ns, cap_setid, &new_map))
637 /* Map the lower ids from the parent user namespace to the
638 * kernel global id space.
640 for (idx = 0; idx < new_map.nr_extents; idx++) {
642 extent = &new_map.extent[idx];
644 lower_first = map_id_range_down(parent_map,
648 /* Fail if we can not map the specified extent to
649 * the kernel global id space.
651 if (lower_first == (u32) -1)
654 extent->lower_first = lower_first;
657 /* Install the map */
658 memcpy(map->extent, new_map.extent,
659 new_map.nr_extents*sizeof(new_map.extent[0]));
661 map->nr_extents = new_map.nr_extents;
666 mutex_unlock(&id_map_mutex);
672 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
674 struct seq_file *seq = file->private_data;
675 struct user_namespace *ns = seq->private;
680 return map_write(file, buf, size, ppos, CAP_SETUID,
681 &ns->uid_map, &ns->parent->uid_map);
684 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
686 struct seq_file *seq = file->private_data;
687 struct user_namespace *ns = seq->private;
692 return map_write(file, buf, size, ppos, CAP_SETGID,
693 &ns->gid_map, &ns->parent->gid_map);
696 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
698 struct seq_file *seq = file->private_data;
699 struct user_namespace *ns = seq->private;
700 struct user_namespace *seq_ns = seq_user_ns(seq);
705 if ((seq_ns != ns) && (seq_ns != ns->parent))
708 /* Anyone can set any valid project id no capability needed */
709 return map_write(file, buf, size, ppos, -1,
710 &ns->projid_map, &ns->parent->projid_map);
713 static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid,
714 struct uid_gid_map *new_map)
716 /* Allow mapping to your own filesystem ids */
717 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
718 u32 id = new_map->extent[0].lower_first;
719 if (cap_setid == CAP_SETUID) {
720 kuid_t uid = make_kuid(ns->parent, id);
721 if (uid_eq(uid, current_fsuid()))
724 else if (cap_setid == CAP_SETGID) {
725 kgid_t gid = make_kgid(ns->parent, id);
726 if (gid_eq(gid, current_fsgid()))
731 /* Allow anyone to set a mapping that doesn't require privilege */
732 if (!cap_valid(cap_setid))
735 /* Allow the specified ids if we have the appropriate capability
736 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
738 if (ns_capable(ns->parent, cap_setid))
744 static void *userns_get(struct task_struct *task)
746 struct user_namespace *user_ns;
749 user_ns = get_user_ns(__task_cred(task)->user_ns);
755 static void userns_put(void *ns)
760 static int userns_install(struct nsproxy *nsproxy, void *ns)
762 struct user_namespace *user_ns = ns;
765 /* Don't allow gaining capabilities by reentering
766 * the same user namespace.
768 if (user_ns == current_user_ns())
771 /* Threaded many not enter a different user namespace */
772 if (atomic_read(¤t->mm->mm_users) > 1)
775 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
778 cred = prepare_creds();
782 put_user_ns(cred->user_ns);
783 set_cred_user_ns(cred, get_user_ns(user_ns));
785 return commit_creds(cred);
788 const struct proc_ns_operations userns_operations = {
790 .type = CLONE_NEWUSER,
793 .install = userns_install,
796 static __init int user_namespaces_init(void)
798 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
801 module_init(user_namespaces_init);