1
2
3
4
5
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
26static struct kmem_cache *user_ns_cachep __read_mostly;
27
28static bool new_idmap_permitted(const struct file *file,
29 struct user_namespace *ns, int cap_setid,
30 struct uid_gid_map *map);
31
32static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
33{
34
35
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_bset = CAP_FULL_SET;
42#ifdef CONFIG_KEYS
43 key_put(cred->request_key_auth);
44 cred->request_key_auth = NULL;
45#endif
46
47 cred->user_ns = user_ns;
48}
49
50
51
52
53
54
55
56
57
58int create_user_ns(struct cred *new)
59{
60 struct user_namespace *ns, *parent_ns = new->user_ns;
61 kuid_t owner = new->euid;
62 kgid_t group = new->egid;
63 int ret;
64
65 if (parent_ns->level > 32)
66 return -EUSERS;
67
68
69
70
71
72
73
74 if (current_chrooted())
75 return -EPERM;
76
77
78
79
80
81 if (!kuid_has_mapping(parent_ns, owner) ||
82 !kgid_has_mapping(parent_ns, group))
83 return -EPERM;
84
85 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
86 if (!ns)
87 return -ENOMEM;
88
89 ret = proc_alloc_inum(&ns->proc_inum);
90 if (ret) {
91 kmem_cache_free(user_ns_cachep, ns);
92 return ret;
93 }
94
95 atomic_set(&ns->count, 1);
96
97 ns->parent = parent_ns;
98 ns->level = parent_ns->level + 1;
99 ns->owner = owner;
100 ns->group = group;
101
102 set_cred_user_ns(new, ns);
103
104 update_mnt_policy(ns);
105
106 return 0;
107}
108
109int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
110{
111 struct cred *cred;
112 int err = -ENOMEM;
113
114 if (!(unshare_flags & CLONE_NEWUSER))
115 return 0;
116
117 cred = prepare_creds();
118 if (cred) {
119 err = create_user_ns(cred);
120 if (err)
121 put_cred(cred);
122 else
123 *new_cred = cred;
124 }
125
126 return err;
127}
128
129void free_user_ns(struct user_namespace *ns)
130{
131 struct user_namespace *parent;
132
133 do {
134 parent = ns->parent;
135 proc_free_inum(ns->proc_inum);
136 kmem_cache_free(user_ns_cachep, ns);
137 ns = parent;
138 } while (atomic_dec_and_test(&parent->count));
139}
140EXPORT_SYMBOL(free_user_ns);
141
142static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
143{
144 unsigned idx, extents;
145 u32 first, last, id2;
146
147 id2 = id + count - 1;
148
149
150 extents = map->nr_extents;
151 smp_read_barrier_depends();
152 for (idx = 0; idx < extents; idx++) {
153 first = map->extent[idx].first;
154 last = first + map->extent[idx].count - 1;
155 if (id >= first && id <= last &&
156 (id2 >= first && id2 <= last))
157 break;
158 }
159
160 if (idx < extents)
161 id = (id - first) + map->extent[idx].lower_first;
162 else
163 id = (u32) -1;
164
165 return id;
166}
167
168static u32 map_id_down(struct uid_gid_map *map, u32 id)
169{
170 unsigned idx, extents;
171 u32 first, last;
172
173
174 extents = map->nr_extents;
175 smp_read_barrier_depends();
176 for (idx = 0; idx < extents; idx++) {
177 first = map->extent[idx].first;
178 last = first + map->extent[idx].count - 1;
179 if (id >= first && id <= last)
180 break;
181 }
182
183 if (idx < extents)
184 id = (id - first) + map->extent[idx].lower_first;
185 else
186 id = (u32) -1;
187
188 return id;
189}
190
191static u32 map_id_up(struct uid_gid_map *map, u32 id)
192{
193 unsigned idx, extents;
194 u32 first, last;
195
196
197 extents = map->nr_extents;
198 smp_read_barrier_depends();
199 for (idx = 0; idx < extents; idx++) {
200 first = map->extent[idx].lower_first;
201 last = first + map->extent[idx].count - 1;
202 if (id >= first && id <= last)
203 break;
204 }
205
206 if (idx < extents)
207 id = (id - first) + map->extent[idx].first;
208 else
209 id = (u32) -1;
210
211 return id;
212}
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
228{
229
230 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
231}
232EXPORT_SYMBOL(make_kuid);
233
234
235
236
237
238
239
240
241
242
243
244
245
246uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
247{
248
249 return map_id_up(&targ->uid_map, __kuid_val(kuid));
250}
251EXPORT_SYMBOL(from_kuid);
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
272{
273 uid_t uid;
274 uid = from_kuid(targ, kuid);
275
276 if (uid == (uid_t) -1)
277 uid = overflowuid;
278 return uid;
279}
280EXPORT_SYMBOL(from_kuid_munged);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
296{
297
298 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
299}
300EXPORT_SYMBOL(make_kgid);
301
302
303
304
305
306
307
308
309
310
311
312
313
314gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
315{
316
317 return map_id_up(&targ->gid_map, __kgid_val(kgid));
318}
319EXPORT_SYMBOL(from_kgid);
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
339{
340 gid_t gid;
341 gid = from_kgid(targ, kgid);
342
343 if (gid == (gid_t) -1)
344 gid = overflowgid;
345 return gid;
346}
347EXPORT_SYMBOL(from_kgid_munged);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
363{
364
365 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
366}
367EXPORT_SYMBOL(make_kprojid);
368
369
370
371
372
373
374
375
376
377
378
379
380
381projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
382{
383
384 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
385}
386EXPORT_SYMBOL(from_kprojid);
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
407{
408 projid_t projid;
409 projid = from_kprojid(targ, kprojid);
410
411 if (projid == (projid_t) -1)
412 projid = OVERFLOW_PROJID;
413 return projid;
414}
415EXPORT_SYMBOL(from_kprojid_munged);
416
417
418static int uid_m_show(struct seq_file *seq, void *v)
419{
420 struct user_namespace *ns = seq->private;
421 struct uid_gid_extent *extent = v;
422 struct user_namespace *lower_ns;
423 uid_t lower;
424
425 lower_ns = seq_user_ns(seq);
426 if ((lower_ns == ns) && lower_ns->parent)
427 lower_ns = lower_ns->parent;
428
429 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
430
431 seq_printf(seq, "%10u %10u %10u\n",
432 extent->first,
433 lower,
434 extent->count);
435
436 return 0;
437}
438
439static int gid_m_show(struct seq_file *seq, void *v)
440{
441 struct user_namespace *ns = seq->private;
442 struct uid_gid_extent *extent = v;
443 struct user_namespace *lower_ns;
444 gid_t lower;
445
446 lower_ns = seq_user_ns(seq);
447 if ((lower_ns == ns) && lower_ns->parent)
448 lower_ns = lower_ns->parent;
449
450 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
451
452 seq_printf(seq, "%10u %10u %10u\n",
453 extent->first,
454 lower,
455 extent->count);
456
457 return 0;
458}
459
460static int projid_m_show(struct seq_file *seq, void *v)
461{
462 struct user_namespace *ns = seq->private;
463 struct uid_gid_extent *extent = v;
464 struct user_namespace *lower_ns;
465 projid_t lower;
466
467 lower_ns = seq_user_ns(seq);
468 if ((lower_ns == ns) && lower_ns->parent)
469 lower_ns = lower_ns->parent;
470
471 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
472
473 seq_printf(seq, "%10u %10u %10u\n",
474 extent->first,
475 lower,
476 extent->count);
477
478 return 0;
479}
480
481static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map)
482{
483 struct uid_gid_extent *extent = NULL;
484 loff_t pos = *ppos;
485
486 if (pos < map->nr_extents)
487 extent = &map->extent[pos];
488
489 return extent;
490}
491
492static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
493{
494 struct user_namespace *ns = seq->private;
495
496 return m_start(seq, ppos, &ns->uid_map);
497}
498
499static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
500{
501 struct user_namespace *ns = seq->private;
502
503 return m_start(seq, ppos, &ns->gid_map);
504}
505
506static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
507{
508 struct user_namespace *ns = seq->private;
509
510 return m_start(seq, ppos, &ns->projid_map);
511}
512
513static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
514{
515 (*pos)++;
516 return seq->op->start(seq, pos);
517}
518
519static void m_stop(struct seq_file *seq, void *v)
520{
521 return;
522}
523
524struct seq_operations proc_uid_seq_operations = {
525 .start = uid_m_start,
526 .stop = m_stop,
527 .next = m_next,
528 .show = uid_m_show,
529};
530
531struct seq_operations proc_gid_seq_operations = {
532 .start = gid_m_start,
533 .stop = m_stop,
534 .next = m_next,
535 .show = gid_m_show,
536};
537
538struct seq_operations proc_projid_seq_operations = {
539 .start = projid_m_start,
540 .stop = m_stop,
541 .next = m_next,
542 .show = projid_m_show,
543};
544
545static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent)
546{
547 u32 upper_first, lower_first, upper_last, lower_last;
548 unsigned idx;
549
550 upper_first = extent->first;
551 lower_first = extent->lower_first;
552 upper_last = upper_first + extent->count - 1;
553 lower_last = lower_first + extent->count - 1;
554
555 for (idx = 0; idx < new_map->nr_extents; idx++) {
556 u32 prev_upper_first, prev_lower_first;
557 u32 prev_upper_last, prev_lower_last;
558 struct uid_gid_extent *prev;
559
560 prev = &new_map->extent[idx];
561
562 prev_upper_first = prev->first;
563 prev_lower_first = prev->lower_first;
564 prev_upper_last = prev_upper_first + prev->count - 1;
565 prev_lower_last = prev_lower_first + prev->count - 1;
566
567
568 if ((prev_upper_first <= upper_last) &&
569 (prev_upper_last >= upper_first))
570 return true;
571
572
573 if ((prev_lower_first <= lower_last) &&
574 (prev_lower_last >= lower_first))
575 return true;
576 }
577 return false;
578}
579
580
581static DEFINE_MUTEX(id_map_mutex);
582
583static ssize_t map_write(struct file *file, const char __user *buf,
584 size_t count, loff_t *ppos,
585 int cap_setid,
586 struct uid_gid_map *map,
587 struct uid_gid_map *parent_map)
588{
589 struct seq_file *seq = file->private_data;
590 struct user_namespace *ns = seq->private;
591 struct uid_gid_map new_map;
592 unsigned idx;
593 struct uid_gid_extent *extent = NULL;
594 unsigned long page = 0;
595 char *kbuf, *pos, *next_line;
596 ssize_t ret = -EINVAL;
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618 mutex_lock(&id_map_mutex);
619
620 ret = -EPERM;
621
622 if (map->nr_extents != 0)
623 goto out;
624
625
626
627
628 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
629 goto out;
630
631
632 ret = -ENOMEM;
633 page = __get_free_page(GFP_TEMPORARY);
634 kbuf = (char *) page;
635 if (!page)
636 goto out;
637
638
639 ret = -EINVAL;
640 if ((*ppos != 0) || (count >= PAGE_SIZE))
641 goto out;
642
643
644 ret = -EFAULT;
645 if (copy_from_user(kbuf, buf, count))
646 goto out;
647 kbuf[count] = '\0';
648
649
650 ret = -EINVAL;
651 pos = kbuf;
652 new_map.nr_extents = 0;
653 for (;pos; pos = next_line) {
654 extent = &new_map.extent[new_map.nr_extents];
655
656
657 next_line = strchr(pos, '\n');
658 if (next_line) {
659 *next_line = '\0';
660 next_line++;
661 if (*next_line == '\0')
662 next_line = NULL;
663 }
664
665 pos = skip_spaces(pos);
666 extent->first = simple_strtoul(pos, &pos, 10);
667 if (!isspace(*pos))
668 goto out;
669
670 pos = skip_spaces(pos);
671 extent->lower_first = simple_strtoul(pos, &pos, 10);
672 if (!isspace(*pos))
673 goto out;
674
675 pos = skip_spaces(pos);
676 extent->count = simple_strtoul(pos, &pos, 10);
677 if (*pos && !isspace(*pos))
678 goto out;
679
680
681 pos = skip_spaces(pos);
682 if (*pos != '\0')
683 goto out;
684
685
686 if ((extent->first == (u32) -1) ||
687 (extent->lower_first == (u32) -1 ))
688 goto out;
689
690
691 if ((extent->first + extent->count) <= extent->first)
692 goto out;
693 if ((extent->lower_first + extent->count) <= extent->lower_first)
694 goto out;
695
696
697 if (mappings_overlap(&new_map, extent))
698 goto out;
699
700 new_map.nr_extents++;
701
702
703 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
704 (next_line != NULL))
705 goto out;
706 }
707
708 if (new_map.nr_extents == 0)
709 goto out;
710
711 ret = -EPERM;
712
713 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
714 goto out;
715
716
717
718
719 for (idx = 0; idx < new_map.nr_extents; idx++) {
720 u32 lower_first;
721 extent = &new_map.extent[idx];
722
723 lower_first = map_id_range_down(parent_map,
724 extent->lower_first,
725 extent->count);
726
727
728
729
730 if (lower_first == (u32) -1)
731 goto out;
732
733 extent->lower_first = lower_first;
734 }
735
736
737 memcpy(map->extent, new_map.extent,
738 new_map.nr_extents*sizeof(new_map.extent[0]));
739 smp_wmb();
740 map->nr_extents = new_map.nr_extents;
741
742 *ppos = count;
743 ret = count;
744out:
745 mutex_unlock(&id_map_mutex);
746 if (page)
747 free_page(page);
748 return ret;
749}
750
751ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
752{
753 struct seq_file *seq = file->private_data;
754 struct user_namespace *ns = seq->private;
755 struct user_namespace *seq_ns = seq_user_ns(seq);
756
757 if (!ns->parent)
758 return -EPERM;
759
760 if ((seq_ns != ns) && (seq_ns != ns->parent))
761 return -EPERM;
762
763 return map_write(file, buf, size, ppos, CAP_SETUID,
764 &ns->uid_map, &ns->parent->uid_map);
765}
766
767ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
768{
769 struct seq_file *seq = file->private_data;
770 struct user_namespace *ns = seq->private;
771 struct user_namespace *seq_ns = seq_user_ns(seq);
772
773 if (!ns->parent)
774 return -EPERM;
775
776 if ((seq_ns != ns) && (seq_ns != ns->parent))
777 return -EPERM;
778
779 return map_write(file, buf, size, ppos, CAP_SETGID,
780 &ns->gid_map, &ns->parent->gid_map);
781}
782
783ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos)
784{
785 struct seq_file *seq = file->private_data;
786 struct user_namespace *ns = seq->private;
787 struct user_namespace *seq_ns = seq_user_ns(seq);
788
789 if (!ns->parent)
790 return -EPERM;
791
792 if ((seq_ns != ns) && (seq_ns != ns->parent))
793 return -EPERM;
794
795
796 return map_write(file, buf, size, ppos, -1,
797 &ns->projid_map, &ns->parent->projid_map);
798}
799
800static bool new_idmap_permitted(const struct file *file,
801 struct user_namespace *ns, int cap_setid,
802 struct uid_gid_map *new_map)
803{
804
805 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) {
806 u32 id = new_map->extent[0].lower_first;
807 if (cap_setid == CAP_SETUID) {
808 kuid_t uid = make_kuid(ns->parent, id);
809 if (uid_eq(uid, file->f_cred->fsuid))
810 return true;
811 }
812 else if (cap_setid == CAP_SETGID) {
813 kgid_t gid = make_kgid(ns->parent, id);
814 if (gid_eq(gid, file->f_cred->fsgid))
815 return true;
816 }
817 }
818
819
820 if (!cap_valid(cap_setid))
821 return true;
822
823
824
825
826
827 if (ns_capable(ns->parent, cap_setid) &&
828 file_ns_capable(file, ns->parent, cap_setid))
829 return true;
830
831 return false;
832}
833
834static void *userns_get(struct task_struct *task)
835{
836 struct user_namespace *user_ns;
837
838 rcu_read_lock();
839 user_ns = get_user_ns(__task_cred(task)->user_ns);
840 rcu_read_unlock();
841
842 return user_ns;
843}
844
845static void userns_put(void *ns)
846{
847 put_user_ns(ns);
848}
849
850static int userns_install(struct nsproxy *nsproxy, void *ns)
851{
852 struct user_namespace *user_ns = ns;
853 struct cred *cred;
854
855
856
857
858 if (user_ns == current_user_ns())
859 return -EINVAL;
860
861
862 if (atomic_read(¤t->mm->mm_users) > 1)
863 return -EINVAL;
864
865 if (current->fs->users != 1)
866 return -EINVAL;
867
868 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
869 return -EPERM;
870
871 cred = prepare_creds();
872 if (!cred)
873 return -ENOMEM;
874
875 put_user_ns(cred->user_ns);
876 set_cred_user_ns(cred, get_user_ns(user_ns));
877
878 return commit_creds(cred);
879}
880
881static unsigned int userns_inum(void *ns)
882{
883 struct user_namespace *user_ns = ns;
884 return user_ns->proc_inum;
885}
886
887const struct proc_ns_operations userns_operations = {
888 .name = "user",
889 .type = CLONE_NEWUSER,
890 .get = userns_get,
891 .put = userns_put,
892 .install = userns_install,
893 .inum = userns_inum,
894};
895
896static __init int user_namespaces_init(void)
897{
898 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
899 return 0;
900}
901module_init(user_namespaces_init);
902