1 /*
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
5 * License.
6 */
7
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>
20 #include <linux/fs.h>
21 #include <linux/uaccess.h>
22 #include <linux/ctype.h>
23 #include <linux/projid.h>
24 #include <linux/fs_struct.h>
25
26 static struct kmem_cache *user_ns_cachep __read_mostly;
27
28 static bool new_idmap_permitted(const struct file *file,
29 struct user_namespace *ns, int cap_setid,
30 struct uid_gid_map *map);
31
set_cred_user_ns(struct cred * cred,struct user_namespace * user_ns)32 static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
33 {
34 /* Start with the same capabilities as init but useless for doing
35 * anything as the capabilities are bound to the new user namespace.
36 */
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_ambient = CAP_EMPTY_SET;
42 cred->cap_bset = CAP_FULL_SET;
43 #ifdef CONFIG_KEYS
44 key_put(cred->request_key_auth);
45 cred->request_key_auth = NULL;
46 #endif
47 /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */
48 cred->user_ns = user_ns;
49 }
50
51 /*
52 * Create a new user namespace, deriving the creator from the user in the
53 * passed credentials, and replacing that user with the new root user for the
54 * new namespace.
55 *
56 * This is called by copy_creds(), which will finish setting the target task's
57 * credentials.
58 */
create_user_ns(struct cred * new)59 int create_user_ns(struct cred *new)
60 {
61 struct user_namespace *ns, *parent_ns = new->user_ns;
62 kuid_t owner = new->euid;
63 kgid_t group = new->egid;
64 int ret;
65
66 /*
67 * Verify that we can not violate the policy of which files
68 * may be accessed that is specified by the root directory,
69 * by verifing that the root directory is at the root of the
70 * mount namespace which allows all files to be accessed.
71 */
72 if (current_chrooted())
73 return -EPERM;
74
75 /* The creator needs a mapping in the parent user namespace
76 * or else we won't be able to reasonably tell userspace who
77 * created a user_namespace.
78 */
79 if (!kuid_has_mapping(parent_ns, owner) ||
80 !kgid_has_mapping(parent_ns, group))
81 return -EPERM;
82
83 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
84 if (!ns)
85 return -ENOMEM;
86
87 ret = proc_alloc_inum(&ns->proc_inum);
88 if (ret) {
89 kmem_cache_free(user_ns_cachep, ns);
90 return ret;
91 }
92
93 atomic_set(&ns->count, 1);
94 /* Leave the new->user_ns reference with the new user namespace. */
95 ns->parent = parent_ns;
96 ns->owner = owner;
97 ns->group = group;
98
99 set_cred_user_ns(new, ns);
100
101 update_mnt_policy(ns);
102
103 return 0;
104 }
105
unshare_userns(unsigned long unshare_flags,struct cred ** new_cred)106 int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
107 {
108 struct cred *cred;
109
110 if (!(unshare_flags & CLONE_NEWUSER))
111 return 0;
112
113 cred = prepare_creds();
114 if (!cred)
115 return -ENOMEM;
116
117 *new_cred = cred;
118 return create_user_ns(cred);
119 }
120
free_user_ns(struct user_namespace * ns)121 void free_user_ns(struct user_namespace *ns)
122 {
123 struct user_namespace *parent;
124
125 do {
126 parent = ns->parent;
127 proc_free_inum(ns->proc_inum);
128 kmem_cache_free(user_ns_cachep, ns);
129 ns = parent;
130 } while (atomic_dec_and_test(&parent->count));
131 }
132 EXPORT_SYMBOL(free_user_ns);
133
map_id_range_down(struct uid_gid_map * map,u32 id,u32 count)134 static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
135 {
136 unsigned idx, extents;
137 u32 first, last, id2;
138
139 id2 = id + count - 1;
140
141 /* Find the matching extent */
142 extents = map->nr_extents;
143 smp_read_barrier_depends();
144 for (idx = 0; idx < extents; idx++) {
145 first = map->extent[idx].first;
146 last = first + map->extent[idx].count - 1;
147 if (id >= first && id <= last &&
148 (id2 >= first && id2 <= last))
149 break;
150 }
151 /* Map the id or note failure */
152 if (idx < extents)
153 id = (id - first) + map->extent[idx].lower_first;
154 else
155 id = (u32) -1;
156
157 return id;
158 }
159
map_id_down(struct uid_gid_map * map,u32 id)160 static u32 map_id_down(struct uid_gid_map *map, u32 id)
161 {
162 unsigned idx, extents;
163 u32 first, last;
164
165 /* Find the matching extent */
166 extents = map->nr_extents;
167 smp_read_barrier_depends();
168 for (idx = 0; idx < extents; idx++) {
169 first = map->extent[idx].first;
170 last = first + map->extent[idx].count - 1;
171 if (id >= first && id <= last)
172 break;
173 }
174 /* Map the id or note failure */
175 if (idx < extents)
176 id = (id - first) + map->extent[idx].lower_first;
177 else
178 id = (u32) -1;
179
180 return id;
181 }
182
map_id_up(struct uid_gid_map * map,u32 id)183 static u32 map_id_up(struct uid_gid_map *map, u32 id)
184 {
185 unsigned idx, extents;
186 u32 first, last;
187
188 /* Find the matching extent */
189 extents = map->nr_extents;
190 smp_read_barrier_depends();
191 for (idx = 0; idx < extents; idx++) {
192 first = map->extent[idx].lower_first;
193 last = first + map->extent[idx].count - 1;
194 if (id >= first && id <= last)
195 break;
196 }
197 /* Map the id or note failure */
198 if (idx < extents)
199 id = (id - first) + map->extent[idx].first;
200 else
201 id = (u32) -1;
202
203 return id;
204 }
205
206 /**
207 * make_kuid - Map a user-namespace uid pair into a kuid.
208 * @ns: User namespace that the uid is in
209 * @uid: User identifier
210 *
211 * Maps a user-namespace uid pair into a kernel internal kuid,
212 * and returns that kuid.
213 *
214 * When there is no mapping defined for the user-namespace uid
215 * pair INVALID_UID is returned. Callers are expected to test
216 * for and handle handle INVALID_UID being returned. INVALID_UID
217 * may be tested for using uid_valid().
218 */
make_kuid(struct user_namespace * ns,uid_t uid)219 kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
220 {
221 /* Map the uid to a global kernel uid */
222 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
223 }
224 EXPORT_SYMBOL(make_kuid);
225
226 /**
227 * from_kuid - Create a uid from a kuid user-namespace pair.
228 * @targ: The user namespace we want a uid in.
229 * @kuid: The kernel internal uid to start with.
230 *
231 * Map @kuid into the user-namespace specified by @targ and
232 * return the resulting uid.
233 *
234 * There is always a mapping into the initial user_namespace.
235 *
236 * If @kuid has no mapping in @targ (uid_t)-1 is returned.
237 */
from_kuid(struct user_namespace * targ,kuid_t kuid)238 uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
239 {
240 /* Map the uid from a global kernel uid */
241 return map_id_up(&targ->uid_map, __kuid_val(kuid));
242 }
243 EXPORT_SYMBOL(from_kuid);
244
245 /**
246 * from_kuid_munged - Create a uid from a kuid user-namespace pair.
247 * @targ: The user namespace we want a uid in.
248 * @kuid: The kernel internal uid to start with.
249 *
250 * Map @kuid into the user-namespace specified by @targ and
251 * return the resulting uid.
252 *
253 * There is always a mapping into the initial user_namespace.
254 *
255 * Unlike from_kuid from_kuid_munged never fails and always
256 * returns a valid uid. This makes from_kuid_munged appropriate
257 * for use in syscalls like stat and getuid where failing the
258 * system call and failing to provide a valid uid are not an
259 * options.
260 *
261 * If @kuid has no mapping in @targ overflowuid is returned.
262 */
from_kuid_munged(struct user_namespace * targ,kuid_t kuid)263 uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
264 {
265 uid_t uid;
266 uid = from_kuid(targ, kuid);
267
268 if (uid == (uid_t) -1)
269 uid = overflowuid;
270 return uid;
271 }
272 EXPORT_SYMBOL(from_kuid_munged);
273
274 /**
275 * make_kgid - Map a user-namespace gid pair into a kgid.
276 * @ns: User namespace that the gid is in
277 * @uid: group identifier
278 *
279 * Maps a user-namespace gid pair into a kernel internal kgid,
280 * and returns that kgid.
281 *
282 * When there is no mapping defined for the user-namespace gid
283 * pair INVALID_GID is returned. Callers are expected to test
284 * for and handle INVALID_GID being returned. INVALID_GID may be
285 * tested for using gid_valid().
286 */
make_kgid(struct user_namespace * ns,gid_t gid)287 kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
288 {
289 /* Map the gid to a global kernel gid */
290 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
291 }
292 EXPORT_SYMBOL(make_kgid);
293
294 /**
295 * from_kgid - Create a gid from a kgid user-namespace pair.
296 * @targ: The user namespace we want a gid in.
297 * @kgid: The kernel internal gid to start with.
298 *
299 * Map @kgid into the user-namespace specified by @targ and
300 * return the resulting gid.
301 *
302 * There is always a mapping into the initial user_namespace.
303 *
304 * If @kgid has no mapping in @targ (gid_t)-1 is returned.
305 */
from_kgid(struct user_namespace * targ,kgid_t kgid)306 gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
307 {
308 /* Map the gid from a global kernel gid */
309 return map_id_up(&targ->gid_map, __kgid_val(kgid));
310 }
311 EXPORT_SYMBOL(from_kgid);
312
313 /**
314 * from_kgid_munged - Create a gid from a kgid user-namespace pair.
315 * @targ: The user namespace we want a gid in.
316 * @kgid: The kernel internal gid to start with.
317 *
318 * Map @kgid into the user-namespace specified by @targ and
319 * return the resulting gid.
320 *
321 * There is always a mapping into the initial user_namespace.
322 *
323 * Unlike from_kgid from_kgid_munged never fails and always
324 * returns a valid gid. This makes from_kgid_munged appropriate
325 * for use in syscalls like stat and getgid where failing the
326 * system call and failing to provide a valid gid are not options.
327 *
328 * If @kgid has no mapping in @targ overflowgid is returned.
329 */
from_kgid_munged(struct user_namespace * targ,kgid_t kgid)330 gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
331 {
332 gid_t gid;
333 gid = from_kgid(targ, kgid);
334
335 if (gid == (gid_t) -1)
336 gid = overflowgid;
337 return gid;
338 }
339 EXPORT_SYMBOL(from_kgid_munged);
340
341 /**
342 * make_kprojid - Map a user-namespace projid pair into a kprojid.
343 * @ns: User namespace that the projid is in
344 * @projid: Project identifier
345 *
346 * Maps a user-namespace uid pair into a kernel internal kuid,
347 * and returns that kuid.
348 *
349 * When there is no mapping defined for the user-namespace projid
350 * pair INVALID_PROJID is returned. Callers are expected to test
351 * for and handle handle INVALID_PROJID being returned. INVALID_PROJID
352 * may be tested for using projid_valid().
353 */
make_kprojid(struct user_namespace * ns,projid_t projid)354 kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
355 {
356 /* Map the uid to a global kernel uid */
357 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
358 }
359 EXPORT_SYMBOL(make_kprojid);
360
361 /**
362 * from_kprojid - Create a projid from a kprojid user-namespace pair.
363 * @targ: The user namespace we want a projid in.
364 * @kprojid: The kernel internal project identifier to start with.
365 *
366 * Map @kprojid into the user-namespace specified by @targ and
367 * return the resulting projid.
368 *
369 * There is always a mapping into the initial user_namespace.
370 *
371 * If @kprojid has no mapping in @targ (projid_t)-1 is returned.
372 */
from_kprojid(struct user_namespace * targ,kprojid_t kprojid)373 projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
374 {
375 /* Map the uid from a global kernel uid */
376 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
377 }
378 EXPORT_SYMBOL(from_kprojid);
379
380 /**
381 * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair.
382 * @targ: The user namespace we want a projid in.
383 * @kprojid: The kernel internal projid to start with.
384 *
385 * Map @kprojid into the user-namespace specified by @targ and
386 * return the resulting projid.
387 *
388 * There is always a mapping into the initial user_namespace.
389 *
390 * Unlike from_kprojid from_kprojid_munged never fails and always
391 * returns a valid projid. This makes from_kprojid_munged
392 * appropriate for use in syscalls like stat and where
393 * failing the system call and failing to provide a valid projid are
394 * not an options.
395 *
396 * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned.
397 */
from_kprojid_munged(struct user_namespace * targ,kprojid_t kprojid)398 projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
399 {
400 projid_t projid;
401 projid = from_kprojid(targ, kprojid);
402
403 if (projid == (projid_t) -1)
404 projid = OVERFLOW_PROJID;
405 return projid;
406 }
407 EXPORT_SYMBOL(from_kprojid_munged);
408
409
uid_m_show(struct seq_file * seq,void * v)410 static int uid_m_show(struct seq_file *seq, void *v)
411 {
412 struct user_namespace *ns = seq->private;
413 struct uid_gid_extent *extent = v;
414 struct user_namespace *lower_ns;
415 uid_t lower;
416
417 lower_ns = seq_user_ns(seq);
418 if ((lower_ns == ns) && lower_ns->parent)
419 lower_ns = lower_ns->parent;
420
421 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
422
423 seq_printf(seq, "%10u %10u %10u\n",
424 extent->first,
425 lower,
426 extent->count);
427
428 return 0;
429 }
430
gid_m_show(struct seq_file * seq,void * v)431 static int gid_m_show(struct seq_file *seq, void *v)
432 {
433 struct user_namespace *ns = seq->private;
434 struct uid_gid_extent *extent = v;
435 struct user_namespace *lower_ns;
436 gid_t lower;
437
438 lower_ns = seq_user_ns(seq);
439 if ((lower_ns == ns) && lower_ns->parent)
440 lower_ns = lower_ns->parent;
441
442 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
443
444 seq_printf(seq, "%10u %10u %10u\n",
445 extent->first,
446 lower,
447 extent->count);
448
449 return 0;
450 }
451
projid_m_show(struct seq_file * seq,void * v)452 static int projid_m_show(struct seq_file *seq, void *v)
453 {
454 struct user_namespace *ns = seq->private;
455 struct uid_gid_extent *extent = v;
456 struct user_namespace *lower_ns;
457 projid_t lower;
458
459 lower_ns = seq_user_ns(seq);
460 if ((lower_ns == ns) && lower_ns->parent)
461 lower_ns = lower_ns->parent;
462
463 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
464
465 seq_printf(seq, "%10u %10u %10u\n",
466 extent->first,
467 lower,
468 extent->count);
469
470 return 0;
471 }
472
m_start(struct seq_file * seq,loff_t * ppos,struct uid_gid_map * map)473 static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
474 {
475 struct uid_gid_extent *extent = NULL;
476 loff_t pos = *ppos;
477
478 if (pos < map->nr_extents)
479 extent = &map->extent[pos];
480
481 return extent;
482 }
483
uid_m_start(struct seq_file * seq,loff_t * ppos)484 static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
485 {
486 struct user_namespace *ns = seq->private;
487
488 return m_start(seq, ppos, &ns->uid_map);
489 }
490
gid_m_start(struct seq_file * seq,loff_t * ppos)491 static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
492 {
493 struct user_namespace *ns = seq->private;
494
495 return m_start(seq, ppos, &ns->gid_map);
496 }
497
projid_m_start(struct seq_file * seq,loff_t * ppos)498 static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
499 {
500 struct user_namespace *ns = seq->private;
501
502 return m_start(seq, ppos, &ns->projid_map);
503 }
504
m_next(struct seq_file * seq,void * v,loff_t * pos)505 static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
506 {
507 (*pos)++;
508 return seq->op->start(seq, pos);
509 }
510
m_stop(struct seq_file * seq,void * v)511 static void m_stop(struct seq_file *seq, void *v)
512 {
513 return;
514 }
515
516 struct seq_operations proc_uid_seq_operations = {
517 .start = uid_m_start,
518 .stop = m_stop,
519 .next = m_next,
520 .show = uid_m_show,
521 };
522
523 struct seq_operations proc_gid_seq_operations = {
524 .start = gid_m_start,
525 .stop = m_stop,
526 .next = m_next,
527 .show = gid_m_show,
528 };
529
530 struct seq_operations proc_projid_seq_operations = {
531 .start = projid_m_start,
532 .stop = m_stop,
533 .next = m_next,
534 .show = projid_m_show,
535 };
536
mappings_overlap(struct uid_gid_map * new_map,struct uid_gid_extent * extent)537 static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
538 {
539 u32 upper_first, lower_first, upper_last, lower_last;
540 unsigned idx;
541
542 upper_first = extent->first;
543 lower_first = extent->lower_first;
544 upper_last = upper_first + extent->count - 1;
545 lower_last = lower_first + extent->count - 1;
546
547 for (idx = 0; idx < new_map->nr_extents; idx++) {
548 u32 prev_upper_first, prev_lower_first;
549 u32 prev_upper_last, prev_lower_last;
550 struct uid_gid_extent *prev;
551
552 prev = &new_map->extent[idx];
553
554 prev_upper_first = prev->first;
555 prev_lower_first = prev->lower_first;
556 prev_upper_last = prev_upper_first + prev->count - 1;
557 prev_lower_last = prev_lower_first + prev->count - 1;
558
559 /* Does the upper range intersect a previous extent? */
560 if ((prev_upper_first <= upper_last) &&
561 (prev_upper_last >= upper_first))
562 return true;
563
564 /* Does the lower range intersect a previous extent? */
565 if ((prev_lower_first <= lower_last) &&
566 (prev_lower_last >= lower_first))
567 return true;
568 }
569 return false;
570 }
571
572
573 static DEFINE_MUTEX(id_map_mutex);
574
map_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos,int cap_setid,struct uid_gid_map * map,struct uid_gid_map * parent_map)575 static ssize_t map_write(struct file *file, const char __user *buf,
576 size_t count, loff_t *ppos,
577 int cap_setid,
578 struct uid_gid_map *map,
579 struct uid_gid_map *parent_map)
580 {
581 struct seq_file *seq = file->private_data;
582 struct user_namespace *ns = seq->private;
583 struct uid_gid_map new_map;
584 unsigned idx;
585 struct uid_gid_extent *extent = NULL;
586 unsigned long page = 0;
587 char *kbuf, *pos, *next_line;
588 ssize_t ret = -EINVAL;
589
590 /*
591 * The id_map_mutex serializes all writes to any given map.
592 *
593 * Any map is only ever written once.
594 *
595 * An id map fits within 1 cache line on most architectures.
596 *
597 * On read nothing needs to be done unless you are on an
598 * architecture with a crazy cache coherency model like alpha.
599 *
600 * There is a one time data dependency between reading the
601 * count of the extents and the values of the extents. The
602 * desired behavior is to see the values of the extents that
603 * were written before the count of the extents.
604 *
605 * To achieve this smp_wmb() is used on guarantee the write
606 * order and smp_read_barrier_depends() is guaranteed that we
607 * don't have crazy architectures returning stale data.
608 *
609 */
610 mutex_lock(&id_map_mutex);
611
612 ret = -EPERM;
613 /* Only allow one successful write to the map */
614 if (map->nr_extents != 0)
615 goto out;
616
617 /*
618 * Adjusting namespace settings requires capabilities on the target.
619 */
620 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
621 goto out;
622
623 /* Get a buffer */
624 ret = -ENOMEM;
625 page = __get_free_page(GFP_TEMPORARY);
626 kbuf = (char *) page;
627 if (!page)
628 goto out;
629
630 /* Only allow <= page size writes at the beginning of the file */
631 ret = -EINVAL;
632 if ((*ppos != 0) || (count >= PAGE_SIZE))
633 goto out;
634
635 /* Slurp in the user data */
636 ret = -EFAULT;
637 if (copy_from_user(kbuf, buf, count))
638 goto out;
639 kbuf[count] = '\0';
640
641 /* Parse the user data */
642 ret = -EINVAL;
643 pos = kbuf;
644 new_map.nr_extents = 0;
645 for (;pos; pos = next_line) {
646 extent = &new_map.extent[new_map.nr_extents];
647
648 /* Find the end of line and ensure I don't look past it */
649 next_line = strchr(pos, '\n');
650 if (next_line) {
651 *next_line = '\0';
652 next_line++;
653 if (*next_line == '\0')
654 next_line = NULL;
655 }
656
657 pos = skip_spaces(pos);
658 extent->first = simple_strtoul(pos, &pos, 10);
659 if (!isspace(*pos))
660 goto out;
661
662 pos = skip_spaces(pos);
663 extent->lower_first = simple_strtoul(pos, &pos, 10);
664 if (!isspace(*pos))
665 goto out;
666
667 pos = skip_spaces(pos);
668 extent->count = simple_strtoul(pos, &pos, 10);
669 if (*pos && !isspace(*pos))
670 goto out;
671
672 /* Verify there is not trailing junk on the line */
673 pos = skip_spaces(pos);
674 if (*pos != '\0')
675 goto out;
676
677 /* Verify we have been given valid starting values */
678 if ((extent->first == (u32) -1) ||
679 (extent->lower_first == (u32) -1 ))
680 goto out;
681
682 /* Verify count is not zero and does not cause the extent to wrap */
683 if ((extent->first + extent->count) <= extent->first)
684 goto out;
685 if ((extent->lower_first + extent->count) <= extent->lower_first)
686 goto out;
687
688 /* Do the ranges in extent overlap any previous extents? */
689 if (mappings_overlap(&new_map, extent))
690 goto out;
691
692 new_map.nr_extents++;
693
694 /* Fail if the file contains too many extents */
695 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
696 (next_line != NULL))
697 goto out;
698 }
699 /* Be very certaint the new map actually exists */
700 if (new_map.nr_extents == 0)
701 goto out;
702
703 ret = -EPERM;
704 /* Validate the user is allowed to use user id's mapped to. */
705 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
706 goto out;
707
708 /* Map the lower ids from the parent user namespace to the
709 * kernel global id space.
710 */
711 for (idx = 0; idx < new_map.nr_extents; idx++) {
712 u32 lower_first;
713 extent = &new_map.extent[idx];
714
715 lower_first = map_id_range_down(parent_map,
716 extent->lower_first,
717 extent->count);
718
719 /* Fail if we can not map the specified extent to
720 * the kernel global id space.
721 */
722 if (lower_first == (u32) -1)
723 goto out;
724
725 extent->lower_first = lower_first;
726 }
727
728 /* Install the map */
729 memcpy(map->extent, new_map.extent,
730 new_map.nr_extents*sizeof(new_map.extent[0]));
731 smp_wmb();
732 map->nr_extents = new_map.nr_extents;
733
734 *ppos = count;
735 ret = count;
736 out:
737 mutex_unlock(&id_map_mutex);
738 if (page)
739 free_page(page);
740 return ret;
741 }
742
proc_uid_map_write(struct file * file,const char __user * buf,size_t size,loff_t * ppos)743 ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
744 {
745 struct seq_file *seq = file->private_data;
746 struct user_namespace *ns = seq->private;
747 struct user_namespace *seq_ns = seq_user_ns(seq);
748
749 if (!ns->parent)
750 return -EPERM;
751
752 if ((seq_ns != ns) && (seq_ns != ns->parent))
753 return -EPERM;
754
755 return map_write(file, buf, size, ppos, CAP_SETUID,
756 &ns->uid_map, &ns->parent->uid_map);
757 }
758
proc_gid_map_write(struct file * file,const char __user * buf,size_t size,loff_t * ppos)759 ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
760 {
761 struct seq_file *seq = file->private_data;
762 struct user_namespace *ns = seq->private;
763 struct user_namespace *seq_ns = seq_user_ns(seq);
764
765 if (!ns->parent)
766 return -EPERM;
767
768 if ((seq_ns != ns) && (seq_ns != ns->parent))
769 return -EPERM;
770
771 return map_write(file, buf, size, ppos, CAP_SETGID,
772 &ns->gid_map, &ns->parent->gid_map);
773 }
774
proc_projid_map_write(struct file * file,const char __user * buf,size_t size,loff_t * ppos)775 ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
776 {
777 struct seq_file *seq = file->private_data;
778 struct user_namespace *ns = seq->private;
779 struct user_namespace *seq_ns = seq_user_ns(seq);
780
781 if (!ns->parent)
782 return -EPERM;
783
784 if ((seq_ns != ns) && (seq_ns != ns->parent))
785 return -EPERM;
786
787 /* Anyone can set any valid project id no capability needed */
788 return map_write(file, buf, size, ppos, -1,
789 &ns->projid_map, &ns->parent->projid_map);
790 }
791
new_idmap_permitted(const struct file * file,struct user_namespace * ns,int cap_setid,struct uid_gid_map * new_map)792 static bool new_idmap_permitted(const struct file *file,
793 struct user_namespace *ns, int cap_setid,
794 struct uid_gid_map *new_map)
795 {
796 /* Allow mapping to your own filesystem ids */
797 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
798 u32 id = new_map->extent[0].lower_first;
799 if (cap_setid == CAP_SETUID) {
800 kuid_t uid = make_kuid(ns->parent, id);
801 if (uid_eq(uid, file->f_cred->fsuid))
802 return true;
803 }
804 else if (cap_setid == CAP_SETGID) {
805 kgid_t gid = make_kgid(ns->parent, id);
806 if (gid_eq(gid, file->f_cred->fsgid))
807 return true;
808 }
809 }
810
811 /* Allow anyone to set a mapping that doesn't require privilege */
812 if (!cap_valid(cap_setid))
813 return true;
814
815 /* Allow the specified ids if we have the appropriate capability
816 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
817 * And the opener of the id file also had the approprpiate capability.
818 */
819 if (ns_capable(ns->parent, cap_setid) &&
820 file_ns_capable(file, ns->parent, cap_setid))
821 return true;
822
823 return false;
824 }
825
userns_get(struct task_struct * task)826 static void *userns_get(struct task_struct *task)
827 {
828 struct user_namespace *user_ns;
829
830 rcu_read_lock();
831 user_ns = get_user_ns(__task_cred(task)->user_ns);
832 rcu_read_unlock();
833
834 return user_ns;
835 }
836
userns_put(void * ns)837 static void userns_put(void *ns)
838 {
839 put_user_ns(ns);
840 }
841
userns_install(struct nsproxy * nsproxy,void * ns)842 static int userns_install(struct nsproxy *nsproxy, void *ns)
843 {
844 struct user_namespace *user_ns = ns;
845 struct cred *cred;
846
847 /* Don't allow gaining capabilities by reentering
848 * the same user namespace.
849 */
850 if (user_ns == current_user_ns())
851 return -EINVAL;
852
853 /* Threaded processes may not enter a different user namespace */
854 if (atomic_read(¤t->mm->mm_users) > 1)
855 return -EINVAL;
856
857 if (current->fs->users != 1)
858 return -EINVAL;
859
860 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
861 return -EPERM;
862
863 cred = prepare_creds();
864 if (!cred)
865 return -ENOMEM;
866
867 put_user_ns(cred->user_ns);
868 set_cred_user_ns(cred, get_user_ns(user_ns));
869
870 return commit_creds(cred);
871 }
872
userns_inum(void * ns)873 static unsigned int userns_inum(void *ns)
874 {
875 struct user_namespace *user_ns = ns;
876 return user_ns->proc_inum;
877 }
878
879 const struct proc_ns_operations userns_operations = {
880 .name = "user",
881 .type = CLONE_NEWUSER,
882 .get = userns_get,
883 .put = userns_put,
884 .install = userns_install,
885 .inum = userns_inum,
886 };
887
user_namespaces_init(void)888 static __init int user_namespaces_init(void)
889 {
890 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
891 return 0;
892 }
893 module_init(user_namespaces_init);
894