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