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