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