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