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