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