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