]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - kernel/user_namespace.c
UBUNTU: snapcraft.yaml: install the apq8016-sbc and msm8916-mtp dtb files
[mirror_ubuntu-zesty-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
505 if (pos < map->nr_extents)
506 extent = &map->extent[pos];
507
508 return extent;
509}
510
511static void *uid_m_start(struct seq_file *seq, loff_t *ppos)
512{
513 struct user_namespace *ns = seq->private;
514
515 return m_start(seq, ppos, &ns->uid_map);
516}
517
518static void *gid_m_start(struct seq_file *seq, loff_t *ppos)
519{
520 struct user_namespace *ns = seq->private;
521
522 return m_start(seq, ppos, &ns->gid_map);
523}
524
f76d207a
EB
525static void *projid_m_start(struct seq_file *seq, loff_t *ppos)
526{
527 struct user_namespace *ns = seq->private;
528
529 return m_start(seq, ppos, &ns->projid_map);
530}
531
22d917d8
EB
532static void *m_next(struct seq_file *seq, void *v, loff_t *pos)
533{
534 (*pos)++;
535 return seq->op->start(seq, pos);
536}
537
538static void m_stop(struct seq_file *seq, void *v)
539{
540 return;
541}
542
ccf94f1b 543const struct seq_operations proc_uid_seq_operations = {
22d917d8
EB
544 .start = uid_m_start,
545 .stop = m_stop,
546 .next = m_next,
547 .show = uid_m_show,
548};
549
ccf94f1b 550const struct seq_operations proc_gid_seq_operations = {
22d917d8
EB
551 .start = gid_m_start,
552 .stop = m_stop,
553 .next = m_next,
554 .show = gid_m_show,
555};
556
ccf94f1b 557const struct seq_operations proc_projid_seq_operations = {
f76d207a
EB
558 .start = projid_m_start,
559 .stop = m_stop,
560 .next = m_next,
561 .show = projid_m_show,
562};
563
68a9a435
FF
564static bool mappings_overlap(struct uid_gid_map *new_map,
565 struct uid_gid_extent *extent)
0bd14b4f
EB
566{
567 u32 upper_first, lower_first, upper_last, lower_last;
568 unsigned idx;
569
570 upper_first = extent->first;
571 lower_first = extent->lower_first;
572 upper_last = upper_first + extent->count - 1;
573 lower_last = lower_first + extent->count - 1;
574
575 for (idx = 0; idx < new_map->nr_extents; idx++) {
576 u32 prev_upper_first, prev_lower_first;
577 u32 prev_upper_last, prev_lower_last;
578 struct uid_gid_extent *prev;
579
580 prev = &new_map->extent[idx];
581
582 prev_upper_first = prev->first;
583 prev_lower_first = prev->lower_first;
584 prev_upper_last = prev_upper_first + prev->count - 1;
585 prev_lower_last = prev_lower_first + prev->count - 1;
586
587 /* Does the upper range intersect a previous extent? */
588 if ((prev_upper_first <= upper_last) &&
589 (prev_upper_last >= upper_first))
590 return true;
591
592 /* Does the lower range intersect a previous extent? */
593 if ((prev_lower_first <= lower_last) &&
594 (prev_lower_last >= lower_first))
595 return true;
596 }
597 return false;
598}
599
22d917d8
EB
600static ssize_t map_write(struct file *file, const char __user *buf,
601 size_t count, loff_t *ppos,
602 int cap_setid,
603 struct uid_gid_map *map,
604 struct uid_gid_map *parent_map)
605{
606 struct seq_file *seq = file->private_data;
607 struct user_namespace *ns = seq->private;
608 struct uid_gid_map new_map;
609 unsigned idx;
0bd14b4f 610 struct uid_gid_extent *extent = NULL;
22d917d8
EB
611 unsigned long page = 0;
612 char *kbuf, *pos, *next_line;
613 ssize_t ret = -EINVAL;
614
615 /*
f0d62aec 616 * The userns_state_mutex serializes all writes to any given map.
22d917d8
EB
617 *
618 * Any map is only ever written once.
619 *
620 * An id map fits within 1 cache line on most architectures.
621 *
622 * On read nothing needs to be done unless you are on an
623 * architecture with a crazy cache coherency model like alpha.
624 *
625 * There is a one time data dependency between reading the
626 * count of the extents and the values of the extents. The
627 * desired behavior is to see the values of the extents that
628 * were written before the count of the extents.
629 *
630 * To achieve this smp_wmb() is used on guarantee the write
e79323bd
MP
631 * order and smp_rmb() is guaranteed that we don't have crazy
632 * architectures returning stale data.
22d917d8 633 */
f0d62aec 634 mutex_lock(&userns_state_mutex);
22d917d8
EB
635
636 ret = -EPERM;
637 /* Only allow one successful write to the map */
638 if (map->nr_extents != 0)
639 goto out;
640
41c21e35
AL
641 /*
642 * Adjusting namespace settings requires capabilities on the target.
5c1469de 643 */
41c21e35 644 if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN))
22d917d8
EB
645 goto out;
646
647 /* Get a buffer */
648 ret = -ENOMEM;
649 page = __get_free_page(GFP_TEMPORARY);
650 kbuf = (char *) page;
651 if (!page)
652 goto out;
653
36476bea 654 /* Only allow < page size writes at the beginning of the file */
22d917d8
EB
655 ret = -EINVAL;
656 if ((*ppos != 0) || (count >= PAGE_SIZE))
657 goto out;
658
659 /* Slurp in the user data */
660 ret = -EFAULT;
661 if (copy_from_user(kbuf, buf, count))
662 goto out;
663 kbuf[count] = '\0';
664
665 /* Parse the user data */
666 ret = -EINVAL;
667 pos = kbuf;
668 new_map.nr_extents = 0;
68a9a435 669 for (; pos; pos = next_line) {
22d917d8
EB
670 extent = &new_map.extent[new_map.nr_extents];
671
672 /* Find the end of line and ensure I don't look past it */
673 next_line = strchr(pos, '\n');
674 if (next_line) {
675 *next_line = '\0';
676 next_line++;
677 if (*next_line == '\0')
678 next_line = NULL;
5c1469de 679 }
22d917d8
EB
680
681 pos = skip_spaces(pos);
682 extent->first = simple_strtoul(pos, &pos, 10);
683 if (!isspace(*pos))
684 goto out;
685
686 pos = skip_spaces(pos);
687 extent->lower_first = simple_strtoul(pos, &pos, 10);
688 if (!isspace(*pos))
689 goto out;
690
691 pos = skip_spaces(pos);
692 extent->count = simple_strtoul(pos, &pos, 10);
693 if (*pos && !isspace(*pos))
694 goto out;
695
696 /* Verify there is not trailing junk on the line */
697 pos = skip_spaces(pos);
698 if (*pos != '\0')
699 goto out;
700
701 /* Verify we have been given valid starting values */
702 if ((extent->first == (u32) -1) ||
68a9a435 703 (extent->lower_first == (u32) -1))
22d917d8
EB
704 goto out;
705
68a9a435
FF
706 /* Verify count is not zero and does not cause the
707 * extent to wrap
708 */
22d917d8
EB
709 if ((extent->first + extent->count) <= extent->first)
710 goto out;
68a9a435
FF
711 if ((extent->lower_first + extent->count) <=
712 extent->lower_first)
22d917d8
EB
713 goto out;
714
0bd14b4f
EB
715 /* Do the ranges in extent overlap any previous extents? */
716 if (mappings_overlap(&new_map, extent))
22d917d8
EB
717 goto out;
718
719 new_map.nr_extents++;
22d917d8
EB
720
721 /* Fail if the file contains too many extents */
722 if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) &&
723 (next_line != NULL))
724 goto out;
5c1469de 725 }
22d917d8
EB
726 /* Be very certaint the new map actually exists */
727 if (new_map.nr_extents == 0)
728 goto out;
729
730 ret = -EPERM;
731 /* Validate the user is allowed to use user id's mapped to. */
6708075f 732 if (!new_idmap_permitted(file, ns, cap_setid, &new_map))
22d917d8
EB
733 goto out;
734
735 /* Map the lower ids from the parent user namespace to the
736 * kernel global id space.
737 */
738 for (idx = 0; idx < new_map.nr_extents; idx++) {
739 u32 lower_first;
740 extent = &new_map.extent[idx];
741
742 lower_first = map_id_range_down(parent_map,
743 extent->lower_first,
744 extent->count);
745
746 /* Fail if we can not map the specified extent to
747 * the kernel global id space.
748 */
749 if (lower_first == (u32) -1)
750 goto out;
751
752 extent->lower_first = lower_first;
753 }
754
755 /* Install the map */
756 memcpy(map->extent, new_map.extent,
757 new_map.nr_extents*sizeof(new_map.extent[0]));
758 smp_wmb();
759 map->nr_extents = new_map.nr_extents;
760
761 *ppos = count;
762 ret = count;
763out:
f0d62aec 764 mutex_unlock(&userns_state_mutex);
22d917d8
EB
765 if (page)
766 free_page(page);
767 return ret;
768}
769
68a9a435
FF
770ssize_t proc_uid_map_write(struct file *file, const char __user *buf,
771 size_t size, loff_t *ppos)
22d917d8
EB
772{
773 struct seq_file *seq = file->private_data;
774 struct user_namespace *ns = seq->private;
c450f371 775 struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d8
EB
776
777 if (!ns->parent)
778 return -EPERM;
779
c450f371
EB
780 if ((seq_ns != ns) && (seq_ns != ns->parent))
781 return -EPERM;
782
22d917d8
EB
783 return map_write(file, buf, size, ppos, CAP_SETUID,
784 &ns->uid_map, &ns->parent->uid_map);
785}
786
68a9a435
FF
787ssize_t proc_gid_map_write(struct file *file, const char __user *buf,
788 size_t size, loff_t *ppos)
22d917d8
EB
789{
790 struct seq_file *seq = file->private_data;
791 struct user_namespace *ns = seq->private;
c450f371 792 struct user_namespace *seq_ns = seq_user_ns(seq);
22d917d8
EB
793
794 if (!ns->parent)
795 return -EPERM;
796
c450f371
EB
797 if ((seq_ns != ns) && (seq_ns != ns->parent))
798 return -EPERM;
799
22d917d8
EB
800 return map_write(file, buf, size, ppos, CAP_SETGID,
801 &ns->gid_map, &ns->parent->gid_map);
802}
803
68a9a435
FF
804ssize_t proc_projid_map_write(struct file *file, const char __user *buf,
805 size_t size, loff_t *ppos)
f76d207a
EB
806{
807 struct seq_file *seq = file->private_data;
808 struct user_namespace *ns = seq->private;
809 struct user_namespace *seq_ns = seq_user_ns(seq);
810
811 if (!ns->parent)
812 return -EPERM;
813
814 if ((seq_ns != ns) && (seq_ns != ns->parent))
815 return -EPERM;
816
817 /* Anyone can set any valid project id no capability needed */
818 return map_write(file, buf, size, ppos, -1,
819 &ns->projid_map, &ns->parent->projid_map);
820}
821
68a9a435 822static bool new_idmap_permitted(const struct file *file,
6708075f 823 struct user_namespace *ns, int cap_setid,
22d917d8
EB
824 struct uid_gid_map *new_map)
825{
f95d7918 826 const struct cred *cred = file->f_cred;
0542f17b
EB
827 /* Don't allow mappings that would allow anything that wouldn't
828 * be allowed without the establishment of unprivileged mappings.
829 */
f95d7918
EB
830 if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) &&
831 uid_eq(ns->owner, cred->euid)) {
37657da3
EB
832 u32 id = new_map->extent[0].lower_first;
833 if (cap_setid == CAP_SETUID) {
834 kuid_t uid = make_kuid(ns->parent, id);
f95d7918 835 if (uid_eq(uid, cred->euid))
37657da3 836 return true;
68a9a435 837 } else if (cap_setid == CAP_SETGID) {
37657da3 838 kgid_t gid = make_kgid(ns->parent, id);
66d2f338
EB
839 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) &&
840 gid_eq(gid, cred->egid))
37657da3
EB
841 return true;
842 }
843 }
844
f76d207a
EB
845 /* Allow anyone to set a mapping that doesn't require privilege */
846 if (!cap_valid(cap_setid))
847 return true;
848
22d917d8
EB
849 /* Allow the specified ids if we have the appropriate capability
850 * (CAP_SETUID or CAP_SETGID) over the parent user namespace.
6708075f 851 * And the opener of the id file also had the approprpiate capability.
22d917d8 852 */
6708075f
EB
853 if (ns_capable(ns->parent, cap_setid) &&
854 file_ns_capable(file, ns->parent, cap_setid))
22d917d8 855 return true;
5c1469de 856
22d917d8 857 return false;
5c1469de 858}
6164281a 859
9cc46516
EB
860int proc_setgroups_show(struct seq_file *seq, void *v)
861{
862 struct user_namespace *ns = seq->private;
863 unsigned long userns_flags = ACCESS_ONCE(ns->flags);
864
865 seq_printf(seq, "%s\n",
866 (userns_flags & USERNS_SETGROUPS_ALLOWED) ?
867 "allow" : "deny");
868 return 0;
869}
870
871ssize_t proc_setgroups_write(struct file *file, const char __user *buf,
872 size_t count, loff_t *ppos)
873{
874 struct seq_file *seq = file->private_data;
875 struct user_namespace *ns = seq->private;
876 char kbuf[8], *pos;
877 bool setgroups_allowed;
878 ssize_t ret;
879
880 /* Only allow a very narrow range of strings to be written */
881 ret = -EINVAL;
882 if ((*ppos != 0) || (count >= sizeof(kbuf)))
883 goto out;
884
885 /* What was written? */
886 ret = -EFAULT;
887 if (copy_from_user(kbuf, buf, count))
888 goto out;
889 kbuf[count] = '\0';
890 pos = kbuf;
891
892 /* What is being requested? */
893 ret = -EINVAL;
894 if (strncmp(pos, "allow", 5) == 0) {
895 pos += 5;
896 setgroups_allowed = true;
897 }
898 else if (strncmp(pos, "deny", 4) == 0) {
899 pos += 4;
900 setgroups_allowed = false;
901 }
902 else
903 goto out;
904
905 /* Verify there is not trailing junk on the line */
906 pos = skip_spaces(pos);
907 if (*pos != '\0')
908 goto out;
909
910 ret = -EPERM;
911 mutex_lock(&userns_state_mutex);
912 if (setgroups_allowed) {
913 /* Enabling setgroups after setgroups has been disabled
914 * is not allowed.
915 */
916 if (!(ns->flags & USERNS_SETGROUPS_ALLOWED))
917 goto out_unlock;
918 } else {
919 /* Permanently disabling setgroups after setgroups has
920 * been enabled by writing the gid_map is not allowed.
921 */
922 if (ns->gid_map.nr_extents != 0)
923 goto out_unlock;
924 ns->flags &= ~USERNS_SETGROUPS_ALLOWED;
925 }
926 mutex_unlock(&userns_state_mutex);
927
928 /* Report a successful write */
929 *ppos = count;
930 ret = count;
931out:
932 return ret;
933out_unlock:
934 mutex_unlock(&userns_state_mutex);
935 goto out;
936}
937
273d2c67
EB
938bool userns_may_setgroups(const struct user_namespace *ns)
939{
940 bool allowed;
941
f0d62aec 942 mutex_lock(&userns_state_mutex);
273d2c67
EB
943 /* It is not safe to use setgroups until a gid mapping in
944 * the user namespace has been established.
945 */
946 allowed = ns->gid_map.nr_extents != 0;
9cc46516
EB
947 /* Is setgroups allowed? */
948 allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED);
f0d62aec 949 mutex_unlock(&userns_state_mutex);
273d2c67
EB
950
951 return allowed;
952}
953
4147df9e
SF
954/*
955 * Returns true if @ns is the same namespace as or a descendant of
956 * @target_ns.
957 */
2c95625c 958bool current_in_userns(const struct user_namespace *target_ns)
4147df9e 959{
2c95625c
SF
960 struct user_namespace *ns;
961 for (ns = current_user_ns(); ns; ns = ns->parent) {
4147df9e
SF
962 if (ns == target_ns)
963 return true;
964 }
965 return false;
966}
880c817e 967EXPORT_SYMBOL(current_in_userns);
4147df9e 968
3c041184
AV
969static inline struct user_namespace *to_user_ns(struct ns_common *ns)
970{
971 return container_of(ns, struct user_namespace, ns);
972}
973
64964528 974static struct ns_common *userns_get(struct task_struct *task)
cde1975b
EB
975{
976 struct user_namespace *user_ns;
977
978 rcu_read_lock();
979 user_ns = get_user_ns(__task_cred(task)->user_ns);
980 rcu_read_unlock();
981
3c041184 982 return user_ns ? &user_ns->ns : NULL;
cde1975b
EB
983}
984
64964528 985static void userns_put(struct ns_common *ns)
cde1975b 986{
3c041184 987 put_user_ns(to_user_ns(ns));
cde1975b
EB
988}
989
64964528 990static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns)
cde1975b 991{
3c041184 992 struct user_namespace *user_ns = to_user_ns(ns);
cde1975b
EB
993 struct cred *cred;
994
995 /* Don't allow gaining capabilities by reentering
996 * the same user namespace.
997 */
998 if (user_ns == current_user_ns())
999 return -EINVAL;
1000
faf00da5
EB
1001 /* Tasks that share a thread group must share a user namespace */
1002 if (!thread_group_empty(current))
cde1975b
EB
1003 return -EINVAL;
1004
e66eded8
EB
1005 if (current->fs->users != 1)
1006 return -EINVAL;
1007
cde1975b
EB
1008 if (!ns_capable(user_ns, CAP_SYS_ADMIN))
1009 return -EPERM;
1010
1011 cred = prepare_creds();
1012 if (!cred)
1013 return -ENOMEM;
1014
1015 put_user_ns(cred->user_ns);
1016 set_cred_user_ns(cred, get_user_ns(user_ns));
1017
1018 return commit_creds(cred);
1019}
1020
1021const struct proc_ns_operations userns_operations = {
1022 .name = "user",
1023 .type = CLONE_NEWUSER,
1024 .get = userns_get,
1025 .put = userns_put,
1026 .install = userns_install,
1027};
1028
6164281a
PE
1029static __init int user_namespaces_init(void)
1030{
1031 user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
1032 return 0;
1033}
c96d6660 1034subsys_initcall(user_namespaces_init);