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