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/sched/signal.h>
12#include <linux/user_namespace.h>
13#include <linux/proc_ns.h>
14#include <linux/highuid.h>
15#include <linux/cred.h>
16#include <linux/securebits.h>
17#include <linux/keyctl.h>
18#include <linux/key-type.h>
19#include <keys/user-type.h>
20#include <linux/seq_file.h>
21#include <linux/fs.h>
22#include <linux/uaccess.h>
23#include <linux/ctype.h>
24#include <linux/projid.h>
25#include <linux/fs_struct.h>
26#include <linux/bsearch.h>
27#include <linux/sort.h>
28
29static struct kmem_cache *user_ns_cachep __read_mostly;
30static DEFINE_MUTEX(userns_state_mutex);
31
32static bool new_idmap_permitted(const struct file *file,
33 struct user_namespace *ns, int cap_setid,
34 struct uid_gid_map *map);
35static void free_user_ns(struct work_struct *work);
36
37static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid)
38{
39 return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES);
40}
41
42static void dec_user_namespaces(struct ucounts *ucounts)
43{
44 return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES);
45}
46
47static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns)
48{
49
50
51
52 cred->securebits = SECUREBITS_DEFAULT;
53 cred->cap_inheritable = CAP_EMPTY_SET;
54 cred->cap_permitted = CAP_FULL_SET;
55 cred->cap_effective = CAP_FULL_SET;
56 cred->cap_ambient = CAP_EMPTY_SET;
57 cred->cap_bset = CAP_FULL_SET;
58#ifdef CONFIG_KEYS
59 key_put(cred->request_key_auth);
60 cred->request_key_auth = NULL;
61#endif
62
63 cred->user_ns = user_ns;
64}
65
66
67
68
69
70
71
72
73
74int create_user_ns(struct cred *new)
75{
76 struct user_namespace *ns, *parent_ns = new->user_ns;
77 kuid_t owner = new->euid;
78 kgid_t group = new->egid;
79 struct ucounts *ucounts;
80 int ret, i;
81
82 ret = -ENOSPC;
83 if (parent_ns->level > 32)
84 goto fail;
85
86 ucounts = inc_user_namespaces(parent_ns, owner);
87 if (!ucounts)
88 goto fail;
89
90
91
92
93
94
95
96 ret = -EPERM;
97 if (current_chrooted())
98 goto fail_dec;
99
100
101
102
103
104 ret = -EPERM;
105 if (!kuid_has_mapping(parent_ns, owner) ||
106 !kgid_has_mapping(parent_ns, group))
107 goto fail_dec;
108
109 ret = -ENOMEM;
110 ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL);
111 if (!ns)
112 goto fail_dec;
113
114 ns->parent_could_setfcap = cap_raised(new->cap_effective, CAP_SETFCAP);
115 ret = ns_alloc_inum(&ns->ns);
116 if (ret)
117 goto fail_free;
118 ns->ns.ops = &userns_operations;
119
120 atomic_set(&ns->count, 1);
121
122 ns->parent = parent_ns;
123 ns->level = parent_ns->level + 1;
124 ns->owner = owner;
125 ns->group = group;
126 INIT_WORK(&ns->work, free_user_ns);
127 for (i = 0; i < UCOUNT_COUNTS; i++) {
128 ns->ucount_max[i] = INT_MAX;
129 }
130 ns->ucounts = ucounts;
131
132
133 mutex_lock(&userns_state_mutex);
134 ns->flags = parent_ns->flags;
135 mutex_unlock(&userns_state_mutex);
136
137#ifdef CONFIG_PERSISTENT_KEYRINGS
138 init_rwsem(&ns->persistent_keyring_register_sem);
139#endif
140 ret = -ENOMEM;
141 if (!setup_userns_sysctls(ns))
142 goto fail_keyring;
143
144 set_cred_user_ns(new, ns);
145 return 0;
146fail_keyring:
147#ifdef CONFIG_PERSISTENT_KEYRINGS
148 key_put(ns->persistent_keyring_register);
149#endif
150 ns_free_inum(&ns->ns);
151fail_free:
152 kmem_cache_free(user_ns_cachep, ns);
153fail_dec:
154 dec_user_namespaces(ucounts);
155fail:
156 return ret;
157}
158
159int unshare_userns(unsigned long unshare_flags, struct cred **new_cred)
160{
161 struct cred *cred;
162 int err = -ENOMEM;
163
164 if (!(unshare_flags & CLONE_NEWUSER))
165 return 0;
166
167 cred = prepare_creds();
168 if (cred) {
169 err = create_user_ns(cred);
170 if (err)
171 put_cred(cred);
172 else
173 *new_cred = cred;
174 }
175
176 return err;
177}
178
179static void free_user_ns(struct work_struct *work)
180{
181 struct user_namespace *parent, *ns =
182 container_of(work, struct user_namespace, work);
183
184 do {
185 struct ucounts *ucounts = ns->ucounts;
186 parent = ns->parent;
187 if (ns->gid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
188 kfree(ns->gid_map.forward);
189 kfree(ns->gid_map.reverse);
190 }
191 if (ns->uid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
192 kfree(ns->uid_map.forward);
193 kfree(ns->uid_map.reverse);
194 }
195 if (ns->projid_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
196 kfree(ns->projid_map.forward);
197 kfree(ns->projid_map.reverse);
198 }
199 retire_userns_sysctls(ns);
200#ifdef CONFIG_PERSISTENT_KEYRINGS
201 key_put(ns->persistent_keyring_register);
202#endif
203 ns_free_inum(&ns->ns);
204 kmem_cache_free(user_ns_cachep, ns);
205 dec_user_namespaces(ucounts);
206 ns = parent;
207 } while (atomic_dec_and_test(&parent->count));
208}
209
210void __put_user_ns(struct user_namespace *ns)
211{
212 schedule_work(&ns->work);
213}
214EXPORT_SYMBOL(__put_user_ns);
215
216
217
218
219
220struct idmap_key {
221 bool map_up;
222 u32 id;
223 u32 count;
224};
225
226
227
228
229
230static int cmp_map_id(const void *k, const void *e)
231{
232 u32 first, last, id2;
233 const struct idmap_key *key = k;
234 const struct uid_gid_extent *el = e;
235
236 id2 = key->id + key->count - 1;
237
238
239 if (key->map_up)
240 first = el->lower_first;
241 else
242 first = el->first;
243
244 last = first + el->count - 1;
245
246 if (key->id >= first && key->id <= last &&
247 (id2 >= first && id2 <= last))
248 return 0;
249
250 if (key->id < first || id2 < first)
251 return -1;
252
253 return 1;
254}
255
256
257
258
259
260static struct uid_gid_extent *
261map_id_range_down_max(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
262{
263 struct idmap_key key;
264
265 key.map_up = false;
266 key.count = count;
267 key.id = id;
268
269 return bsearch(&key, map->forward, extents,
270 sizeof(struct uid_gid_extent), cmp_map_id);
271}
272
273
274
275
276
277
278static struct uid_gid_extent *
279map_id_range_down_base(unsigned extents, struct uid_gid_map *map, u32 id, u32 count)
280{
281 unsigned idx;
282 u32 first, last, id2;
283
284 id2 = id + count - 1;
285
286
287 for (idx = 0; idx < extents; idx++) {
288 first = map->extent[idx].first;
289 last = first + map->extent[idx].count - 1;
290 if (id >= first && id <= last &&
291 (id2 >= first && id2 <= last))
292 return &map->extent[idx];
293 }
294 return NULL;
295}
296
297static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count)
298{
299 struct uid_gid_extent *extent;
300 unsigned extents = map->nr_extents;
301 smp_rmb();
302
303 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
304 extent = map_id_range_down_base(extents, map, id, count);
305 else
306 extent = map_id_range_down_max(extents, map, id, count);
307
308
309 if (extent)
310 id = (id - extent->first) + extent->lower_first;
311 else
312 id = (u32) -1;
313
314 return id;
315}
316
317static u32 map_id_down(struct uid_gid_map *map, u32 id)
318{
319 return map_id_range_down(map, id, 1);
320}
321
322
323
324
325
326
327static struct uid_gid_extent *
328map_id_up_base(unsigned extents, struct uid_gid_map *map, u32 id)
329{
330 unsigned idx;
331 u32 first, last;
332
333
334 for (idx = 0; idx < extents; idx++) {
335 first = map->extent[idx].lower_first;
336 last = first + map->extent[idx].count - 1;
337 if (id >= first && id <= last)
338 return &map->extent[idx];
339 }
340 return NULL;
341}
342
343
344
345
346
347static struct uid_gid_extent *
348map_id_up_max(unsigned extents, struct uid_gid_map *map, u32 id)
349{
350 struct idmap_key key;
351
352 key.map_up = true;
353 key.count = 1;
354 key.id = id;
355
356 return bsearch(&key, map->reverse, extents,
357 sizeof(struct uid_gid_extent), cmp_map_id);
358}
359
360static u32 map_id_up(struct uid_gid_map *map, u32 id)
361{
362 struct uid_gid_extent *extent;
363 unsigned extents = map->nr_extents;
364 smp_rmb();
365
366 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
367 extent = map_id_up_base(extents, map, id);
368 else
369 extent = map_id_up_max(extents, map, id);
370
371
372 if (extent)
373 id = (id - extent->lower_first) + extent->first;
374 else
375 id = (u32) -1;
376
377 return id;
378}
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393kuid_t make_kuid(struct user_namespace *ns, uid_t uid)
394{
395
396 return KUIDT_INIT(map_id_down(&ns->uid_map, uid));
397}
398EXPORT_SYMBOL(make_kuid);
399
400
401
402
403
404
405
406
407
408
409
410
411
412uid_t from_kuid(struct user_namespace *targ, kuid_t kuid)
413{
414
415 return map_id_up(&targ->uid_map, __kuid_val(kuid));
416}
417EXPORT_SYMBOL(from_kuid);
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid)
438{
439 uid_t uid;
440 uid = from_kuid(targ, kuid);
441
442 if (uid == (uid_t) -1)
443 uid = overflowuid;
444 return uid;
445}
446EXPORT_SYMBOL(from_kuid_munged);
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461kgid_t make_kgid(struct user_namespace *ns, gid_t gid)
462{
463
464 return KGIDT_INIT(map_id_down(&ns->gid_map, gid));
465}
466EXPORT_SYMBOL(make_kgid);
467
468
469
470
471
472
473
474
475
476
477
478
479
480gid_t from_kgid(struct user_namespace *targ, kgid_t kgid)
481{
482
483 return map_id_up(&targ->gid_map, __kgid_val(kgid));
484}
485EXPORT_SYMBOL(from_kgid);
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid)
505{
506 gid_t gid;
507 gid = from_kgid(targ, kgid);
508
509 if (gid == (gid_t) -1)
510 gid = overflowgid;
511 return gid;
512}
513EXPORT_SYMBOL(from_kgid_munged);
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid)
529{
530
531 return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid));
532}
533EXPORT_SYMBOL(make_kprojid);
534
535
536
537
538
539
540
541
542
543
544
545
546
547projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid)
548{
549
550 return map_id_up(&targ->projid_map, __kprojid_val(kprojid));
551}
552EXPORT_SYMBOL(from_kprojid);
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid)
573{
574 projid_t projid;
575 projid = from_kprojid(targ, kprojid);
576
577 if (projid == (projid_t) -1)
578 projid = OVERFLOW_PROJID;
579 return projid;
580}
581EXPORT_SYMBOL(from_kprojid_munged);
582
583
584static int uid_m_show(struct seq_file *seq, void *v)
585{
586 struct user_namespace *ns = seq->private;
587 struct uid_gid_extent *extent = v;
588 struct user_namespace *lower_ns;
589 uid_t lower;
590
591 lower_ns = seq_user_ns(seq);
592 if ((lower_ns == ns) && lower_ns->parent)
593 lower_ns = lower_ns->parent;
594
595 lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first));
596
597 seq_printf(seq, "%10u %10u %10u\n",
598 extent->first,
599 lower,
600 extent->count);
601
602 return 0;
603}
604
605static int gid_m_show(struct seq_file *seq, void *v)
606{
607 struct user_namespace *ns = seq->private;
608 struct uid_gid_extent *extent = v;
609 struct user_namespace *lower_ns;
610 gid_t lower;
611
612 lower_ns = seq_user_ns(seq);
613 if ((lower_ns == ns) && lower_ns->parent)
614 lower_ns = lower_ns->parent;
615
616 lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first));
617
618 seq_printf(seq, "%10u %10u %10u\n",
619 extent->first,
620 lower,
621 extent->count);
622
623 return 0;
624}
625
626static int projid_m_show(struct seq_file *seq, void *v)
627{
628 struct user_namespace *ns = seq->private;
629 struct uid_gid_extent *extent = v;
630 struct user_namespace *lower_ns;
631 projid_t lower;
632
633 lower_ns = seq_user_ns(seq);
634 if ((lower_ns == ns) && lower_ns->parent)
635 lower_ns = lower_ns->parent;
636
637 lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first));
638
639 seq_printf(seq, "%10u %10u %10u\n",
640 extent->first,
641 lower,
642 extent->count);
643
644 return 0;
645}
646
647static void *m_start(struct seq_file *seq, loff_t *ppos,
648 struct uid_gid_map *map)
649{
650 loff_t pos = *ppos;
651 unsigned extents = map->nr_extents;
652 smp_rmb();
653
654 if (pos >= extents)
655 return NULL;
656
657 if (extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
658 return &map->extent[pos];
659
660 return &map->forward[pos];
661}
662
663static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
664{
665 struct user_namespace *ns = seq->private;
666
667 return m_start(seq, ppos, &ns->uid_map);
668}
669
670static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
671{
672 struct user_namespace *ns = seq->private;
673
674 return m_start(seq, ppos, &ns->gid_map);
675}
676
677static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
678{
679 struct user_namespace *ns = seq->private;
680
681 return m_start(seq, ppos, &ns->projid_map);
682}
683
684static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
685{
686 (*pos)++;
687 return seq->op->start(seq, pos);
688}
689
690static void m_stop(struct seq_file *seq, void *v)
691{
692 return;
693}
694
695const struct seq_operations proc_uid_seq_operations = {
696 .start = uid_m_start,
697 .stop = m_stop,
698 .next = m_next,
699 .show = uid_m_show,
700};
701
702const struct seq_operations proc_gid_seq_operations = {
703 .start = gid_m_start,
704 .stop = m_stop,
705 .next = m_next,
706 .show = gid_m_show,
707};
708
709const struct seq_operations proc_projid_seq_operations = {
710 .start = projid_m_start,
711 .stop = m_stop,
712 .next = m_next,
713 .show = projid_m_show,
714};
715
716static bool mappings_overlap(struct uid_gid_map *new_map,
717 struct uid_gid_extent *extent)
718{
719 u32 upper_first, lower_first, upper_last, lower_last;
720 unsigned idx;
721
722 upper_first = extent->first;
723 lower_first = extent->lower_first;
724 upper_last = upper_first + extent->count - 1;
725 lower_last = lower_first + extent->count - 1;
726
727 for (idx = 0; idx < new_map->nr_extents; idx++) {
728 u32 prev_upper_first, prev_lower_first;
729 u32 prev_upper_last, prev_lower_last;
730 struct uid_gid_extent *prev;
731
732 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
733 prev = &new_map->extent[idx];
734 else
735 prev = &new_map->forward[idx];
736
737 prev_upper_first = prev->first;
738 prev_lower_first = prev->lower_first;
739 prev_upper_last = prev_upper_first + prev->count - 1;
740 prev_lower_last = prev_lower_first + prev->count - 1;
741
742
743 if ((prev_upper_first <= upper_last) &&
744 (prev_upper_last >= upper_first))
745 return true;
746
747
748 if ((prev_lower_first <= lower_last) &&
749 (prev_lower_last >= lower_first))
750 return true;
751 }
752 return false;
753}
754
755
756
757
758
759
760static int insert_extent(struct uid_gid_map *map, struct uid_gid_extent *extent)
761{
762 struct uid_gid_extent *dest;
763
764 if (map->nr_extents == UID_GID_MAP_MAX_BASE_EXTENTS) {
765 struct uid_gid_extent *forward;
766
767
768 forward = kmalloc_array(UID_GID_MAP_MAX_EXTENTS,
769 sizeof(struct uid_gid_extent),
770 GFP_KERNEL);
771 if (!forward)
772 return -ENOMEM;
773
774
775
776
777 memcpy(forward, map->extent,
778 map->nr_extents * sizeof(map->extent[0]));
779
780 map->forward = forward;
781 map->reverse = NULL;
782 }
783
784 if (map->nr_extents < UID_GID_MAP_MAX_BASE_EXTENTS)
785 dest = &map->extent[map->nr_extents];
786 else
787 dest = &map->forward[map->nr_extents];
788
789 *dest = *extent;
790 map->nr_extents++;
791 return 0;
792}
793
794
795static int cmp_extents_forward(const void *a, const void *b)
796{
797 const struct uid_gid_extent *e1 = a;
798 const struct uid_gid_extent *e2 = b;
799
800 if (e1->first < e2->first)
801 return -1;
802
803 if (e1->first > e2->first)
804 return 1;
805
806 return 0;
807}
808
809
810static int cmp_extents_reverse(const void *a, const void *b)
811{
812 const struct uid_gid_extent *e1 = a;
813 const struct uid_gid_extent *e2 = b;
814
815 if (e1->lower_first < e2->lower_first)
816 return -1;
817
818 if (e1->lower_first > e2->lower_first)
819 return 1;
820
821 return 0;
822}
823
824
825
826
827
828static int sort_idmaps(struct uid_gid_map *map)
829{
830 if (map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
831 return 0;
832
833
834 sort(map->forward, map->nr_extents, sizeof(struct uid_gid_extent),
835 cmp_extents_forward, NULL);
836
837
838 map->reverse = kmemdup(map->forward,
839 map->nr_extents * sizeof(struct uid_gid_extent),
840 GFP_KERNEL);
841 if (!map->reverse)
842 return -ENOMEM;
843
844
845 sort(map->reverse, map->nr_extents, sizeof(struct uid_gid_extent),
846 cmp_extents_reverse, NULL);
847
848 return 0;
849}
850
851
852
853
854
855
856
857
858
859
860
861
862
863static bool verify_root_map(const struct file *file,
864 struct user_namespace *map_ns,
865 struct uid_gid_map *new_map)
866{
867 int idx;
868 const struct user_namespace *file_ns = file->f_cred->user_ns;
869 struct uid_gid_extent *extent0 = NULL;
870
871 for (idx = 0; idx < new_map->nr_extents; idx++) {
872 if (new_map->nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
873 extent0 = &new_map->extent[idx];
874 else
875 extent0 = &new_map->forward[idx];
876 if (extent0->lower_first == 0)
877 break;
878
879 extent0 = NULL;
880 }
881
882 if (!extent0)
883 return true;
884
885 if (map_ns == file_ns) {
886
887
888
889
890
891 if (!file_ns->parent_could_setfcap)
892 return false;
893 } else {
894
895
896
897
898 if (!file_ns_capable(file, map_ns->parent, CAP_SETFCAP))
899 return false;
900 }
901
902 return true;
903}
904
905static ssize_t map_write(struct file *file, const char __user *buf,
906 size_t count, loff_t *ppos,
907 int cap_setid,
908 struct uid_gid_map *map,
909 struct uid_gid_map *parent_map)
910{
911 struct seq_file *seq = file->private_data;
912 struct user_namespace *map_ns = seq->private;
913 struct uid_gid_map new_map;
914 unsigned idx;
915 struct uid_gid_extent extent;
916 char *kbuf = NULL, *pos, *next_line;
917 ssize_t ret = -EINVAL;
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938 mutex_lock(&userns_state_mutex);
939
940 memset(&new_map, 0, sizeof(struct uid_gid_map));
941
942 ret = -EPERM;
943
944 if (map->nr_extents != 0)
945 goto out;
946
947
948
949
950 if (cap_valid(cap_setid) && !file_ns_capable(file, map_ns, CAP_SYS_ADMIN))
951 goto out;
952
953
954 ret = -EINVAL;
955 if ((*ppos != 0) || (count >= PAGE_SIZE))
956 goto out;
957
958
959 kbuf = memdup_user_nul(buf, count);
960 if (IS_ERR(kbuf)) {
961 ret = PTR_ERR(kbuf);
962 kbuf = NULL;
963 goto out;
964 }
965
966
967 ret = -EINVAL;
968 pos = kbuf;
969 for (; pos; pos = next_line) {
970
971
972 next_line = strchr(pos, '\n');
973 if (next_line) {
974 *next_line = '\0';
975 next_line++;
976 if (*next_line == '\0')
977 next_line = NULL;
978 }
979
980 pos = skip_spaces(pos);
981 extent.first = simple_strtoul(pos, &pos, 10);
982 if (!isspace(*pos))
983 goto out;
984
985 pos = skip_spaces(pos);
986 extent.lower_first = simple_strtoul(pos, &pos, 10);
987 if (!isspace(*pos))
988 goto out;
989
990 pos = skip_spaces(pos);
991 extent.count = simple_strtoul(pos, &pos, 10);
992 if (*pos && !isspace(*pos))
993 goto out;
994
995
996 pos = skip_spaces(pos);
997 if (*pos != '\0')
998 goto out;
999
1000
1001 if ((extent.first == (u32) -1) ||
1002 (extent.lower_first == (u32) -1))
1003 goto out;
1004
1005
1006
1007
1008 if ((extent.first + extent.count) <= extent.first)
1009 goto out;
1010 if ((extent.lower_first + extent.count) <=
1011 extent.lower_first)
1012 goto out;
1013
1014
1015 if (mappings_overlap(&new_map, &extent))
1016 goto out;
1017
1018 if ((new_map.nr_extents + 1) == UID_GID_MAP_MAX_EXTENTS &&
1019 (next_line != NULL))
1020 goto out;
1021
1022 ret = insert_extent(&new_map, &extent);
1023 if (ret < 0)
1024 goto out;
1025 ret = -EINVAL;
1026 }
1027
1028 if (new_map.nr_extents == 0)
1029 goto out;
1030
1031 ret = -EPERM;
1032
1033 if (!new_idmap_permitted(file, map_ns, cap_setid, &new_map))
1034 goto out;
1035
1036 ret = -EPERM;
1037
1038
1039
1040 for (idx = 0; idx < new_map.nr_extents; idx++) {
1041 struct uid_gid_extent *e;
1042 u32 lower_first;
1043
1044 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS)
1045 e = &new_map.extent[idx];
1046 else
1047 e = &new_map.forward[idx];
1048
1049 lower_first = map_id_range_down(parent_map,
1050 e->lower_first,
1051 e->count);
1052
1053
1054
1055
1056 if (lower_first == (u32) -1)
1057 goto out;
1058
1059 e->lower_first = lower_first;
1060 }
1061
1062
1063
1064
1065
1066 ret = sort_idmaps(&new_map);
1067 if (ret < 0)
1068 goto out;
1069
1070
1071 if (new_map.nr_extents <= UID_GID_MAP_MAX_BASE_EXTENTS) {
1072 memcpy(map->extent, new_map.extent,
1073 new_map.nr_extents * sizeof(new_map.extent[0]));
1074 } else {
1075 map->forward = new_map.forward;
1076 map->reverse = new_map.reverse;
1077 }
1078 smp_wmb();
1079 map->nr_extents = new_map.nr_extents;
1080
1081 *ppos = count;
1082 ret = count;
1083out:
1084 if (ret < 0 && new_map.nr_extents > UID_GID_MAP_MAX_BASE_EXTENTS) {
1085 kfree(new_map.forward);
1086 kfree(new_map.reverse);
1087 map->forward = NULL;
1088 map->reverse = NULL;
1089 map->nr_extents = 0;
1090 }
1091
1092 mutex_unlock(&userns_state_mutex);
1093 kfree(kbuf);
1094 return ret;
1095}
1096
1097ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
1098 size_t size, loff_t *ppos)
1099{
1100 struct seq_file *seq = file->private_data;
1101 struct user_namespace *ns = seq->private;
1102 struct user_namespace *seq_ns = seq_user_ns(seq);
1103
1104 if (!ns->parent)
1105 return -EPERM;
1106
1107 if ((seq_ns != ns) && (seq_ns != ns->parent))
1108 return -EPERM;
1109
1110 return map_write(file, buf, size, ppos, CAP_SETUID,
1111 &ns->uid_map, &ns->parent->uid_map);
1112}
1113
1114ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
1115 size_t size, loff_t *ppos)
1116{
1117 struct seq_file *seq = file->private_data;
1118 struct user_namespace *ns = seq->private;
1119 struct user_namespace *seq_ns = seq_user_ns(seq);
1120
1121 if (!ns->parent)
1122 return -EPERM;
1123
1124 if ((seq_ns != ns) && (seq_ns != ns->parent))
1125 return -EPERM;
1126
1127 return map_write(file, buf, size, ppos, CAP_SETGID,
1128 &ns->gid_map, &ns->parent->gid_map);
1129}
1130
1131ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
1132 size_t size, loff_t *ppos)
1133{
1134 struct seq_file *seq = file->private_data;
1135 struct user_namespace *ns = seq->private;
1136 struct user_namespace *seq_ns = seq_user_ns(seq);
1137
1138 if (!ns->parent)
1139 return -EPERM;
1140
1141 if ((seq_ns != ns) && (seq_ns != ns->parent))
1142 return -EPERM;
1143
1144
1145 return map_write(file, buf, size, ppos, -1,
1146 &ns->projid_map, &ns->parent->projid_map);
1147}
1148
1149static bool new_idmap_permitted(const struct file *file,
1150 struct user_namespace *ns, int cap_setid,
1151 struct uid_gid_map *new_map)
1152{
1153 const struct cred *cred = file->f_cred;
1154
1155 if (cap_setid == CAP_SETUID && !verify_root_map(file, ns, new_map))
1156 return false;
1157
1158
1159
1160
1161 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
1162 uid_eq(ns->owner, cred->euid)) {
1163 u32 id = new_map->extent[0].lower_first;
1164 if (cap_setid == CAP_SETUID) {
1165 kuid_t uid = make_kuid(ns->parent, id);
1166 if (uid_eq(uid, cred->euid))
1167 return true;
1168 } else if (cap_setid == CAP_SETGID) {
1169 kgid_t gid = make_kgid(ns->parent, id);
1170 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
1171 gid_eq(gid, cred->egid))
1172 return true;
1173 }
1174 }
1175
1176
1177 if (!cap_valid(cap_setid))
1178 return true;
1179
1180
1181
1182
1183
1184 if (ns_capable(ns->parent, cap_setid) &&
1185 file_ns_capable(file, ns->parent, cap_setid))
1186 return true;
1187
1188 return false;
1189}
1190
1191int proc_setgroups_show(struct seq_file *seq, void *v)
1192{
1193 struct user_namespace *ns = seq->private;
1194 unsigned long userns_flags = READ_ONCE(ns->flags);
1195
1196 seq_printf(seq, "%s\n",
1197 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
1198 "allow" : "deny");
1199 return 0;
1200}
1201
1202ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
1203 size_t count, loff_t *ppos)
1204{
1205 struct seq_file *seq = file->private_data;
1206 struct user_namespace *ns = seq->private;
1207 char kbuf[8], *pos;
1208 bool setgroups_allowed;
1209 ssize_t ret;
1210
1211
1212 ret = -EINVAL;
1213 if ((*ppos != 0) || (count >= sizeof(kbuf)))
1214 goto out;
1215
1216
1217 ret = -EFAULT;
1218 if (copy_from_user(kbuf, buf, count))
1219 goto out;
1220 kbuf[count] = '\0';
1221 pos = kbuf;
1222
1223
1224 ret = -EINVAL;
1225 if (strncmp(pos, "allow", 5) == 0) {
1226 pos += 5;
1227 setgroups_allowed = true;
1228 }
1229 else if (strncmp(pos, "deny", 4) == 0) {
1230 pos += 4;
1231 setgroups_allowed = false;
1232 }
1233 else
1234 goto out;
1235
1236
1237 pos = skip_spaces(pos);
1238 if (*pos != '\0')
1239 goto out;
1240
1241 ret = -EPERM;
1242 mutex_lock(&userns_state_mutex);
1243 if (setgroups_allowed) {
1244
1245
1246
1247 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
1248 goto out_unlock;
1249 } else {
1250
1251
1252
1253 if (ns->gid_map.nr_extents != 0)
1254 goto out_unlock;
1255 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
1256 }
1257 mutex_unlock(&userns_state_mutex);
1258
1259
1260 *ppos = count;
1261 ret = count;
1262out:
1263 return ret;
1264out_unlock:
1265 mutex_unlock(&userns_state_mutex);
1266 goto out;
1267}
1268
1269bool userns_may_setgroups(const struct user_namespace *ns)
1270{
1271 bool allowed;
1272
1273 mutex_lock(&userns_state_mutex);
1274
1275
1276
1277 allowed = ns->gid_map.nr_extents != 0;
1278
1279 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
1280 mutex_unlock(&userns_state_mutex);
1281
1282 return allowed;
1283}
1284
1285
1286
1287
1288
1289bool in_userns(const struct user_namespace *ancestor,
1290 const struct user_namespace *child)
1291{
1292 const struct user_namespace *ns;
1293 for (ns = child; ns->level > ancestor->level; ns = ns->parent)
1294 ;
1295 return (ns == ancestor);
1296}
1297
1298bool current_in_userns(const struct user_namespace *target_ns)
1299{
1300 return in_userns(target_ns, current_user_ns());
1301}
1302EXPORT_SYMBOL(current_in_userns);
1303
1304static inline struct user_namespace *to_user_ns(struct ns_common *ns)
1305{
1306 return container_of(ns, struct user_namespace, ns);
1307}
1308
1309static struct ns_common *userns_get(struct task_struct *task)
1310{
1311 struct user_namespace *user_ns;
1312
1313 rcu_read_lock();
1314 user_ns = get_user_ns(__task_cred(task)->user_ns);
1315 rcu_read_unlock();
1316
1317 return user_ns ? &user_ns->ns : NULL;
1318}
1319
1320static void userns_put(struct ns_common *ns)
1321{
1322 put_user_ns(to_user_ns(ns));
1323}
1324
1325static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
1326{
1327 struct user_namespace *user_ns = to_user_ns(ns);
1328 struct cred *cred;
1329
1330
1331
1332
1333 if (user_ns == current_user_ns())
1334 return -EINVAL;
1335
1336
1337 if (!thread_group_empty(current))
1338 return -EINVAL;
1339
1340 if (current->fs->users != 1)
1341 return -EINVAL;
1342
1343 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1344 return -EPERM;
1345
1346 cred = prepare_creds();
1347 if (!cred)
1348 return -ENOMEM;
1349
1350 put_user_ns(cred->user_ns);
1351 set_cred_user_ns(cred, get_user_ns(user_ns));
1352
1353 return commit_creds(cred);
1354}
1355
1356struct ns_common *ns_get_owner(struct ns_common *ns)
1357{
1358 struct user_namespace *my_user_ns = current_user_ns();
1359 struct user_namespace *owner, *p;
1360
1361
1362 owner = p = ns->ops->owner(ns);
1363 for (;;) {
1364 if (!p)
1365 return ERR_PTR(-EPERM);
1366 if (p == my_user_ns)
1367 break;
1368 p = p->parent;
1369 }
1370
1371 return &get_user_ns(owner)->ns;
1372}
1373
1374static struct user_namespace *userns_owner(struct ns_common *ns)
1375{
1376 return to_user_ns(ns)->parent;
1377}
1378
1379const struct proc_ns_operations userns_operations = {
1380 .name = "user",
1381 .type = CLONE_NEWUSER,
1382 .get = userns_get,
1383 .put = userns_put,
1384 .install = userns_install,
1385 .owner = userns_owner,
1386 .get_parent = ns_get_owner,
1387};
1388
1389static __init int user_namespaces_init(void)
1390{
1391 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1392 return 0;
1393}
1394subsys_initcall(user_namespaces_init);
1395