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