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