]>
Commit | Line | Data |
---|---|---|
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 | ||
8 | #include <linux/export.h> | |
9 | #include <linux/nsproxy.h> | |
10 | #include <linux/slab.h> | |
11 | #include <linux/sched/signal.h> | |
12 | #include <linux/user_namespace.h> | |
13 | #include <linux/proc_ns.h> | |
14 | #include <linux/highuid.h> | |
15 | #include <linux/cred.h> | |
16 | #include <linux/securebits.h> | |
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> | |
24 | #include <linux/projid.h> | |
25 | #include <linux/fs_struct.h> | |
26 | ||
27 | /* | |
28 | * sysctl determining whether unprivileged users may unshare a new | |
29 | * userns. Allowed by default | |
30 | */ | |
31 | int unprivileged_userns_clone = 1; | |
32 | ||
33 | static struct kmem_cache *user_ns_cachep __read_mostly; | |
34 | static DEFINE_MUTEX(userns_state_mutex); | |
35 | ||
36 | static bool new_idmap_permitted(const struct file *file, | |
37 | struct user_namespace *ns, int cap_setid, | |
38 | struct uid_gid_map *map); | |
39 | static void free_user_ns(struct work_struct *work); | |
40 | ||
41 | static struct ucounts *inc_user_namespaces(struct user_namespace *ns, kuid_t uid) | |
42 | { | |
43 | return inc_ucount(ns, uid, UCOUNT_USER_NAMESPACES); | |
44 | } | |
45 | ||
46 | static void dec_user_namespaces(struct ucounts *ucounts) | |
47 | { | |
48 | return dec_ucount(ucounts, UCOUNT_USER_NAMESPACES); | |
49 | } | |
50 | ||
51 | static 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; | |
60 | cred->cap_ambient = CAP_EMPTY_SET; | |
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 | ||
70 | /* | |
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. | |
77 | */ | |
78 | int create_user_ns(struct cred *new) | |
79 | { | |
80 | struct user_namespace *ns, *parent_ns = new->user_ns; | |
81 | kuid_t owner = new->euid; | |
82 | kgid_t group = new->egid; | |
83 | struct ucounts *ucounts; | |
84 | int ret, i; | |
85 | ||
86 | ret = -ENOSPC; | |
87 | if (parent_ns->level > 32) | |
88 | goto fail; | |
89 | ||
90 | ucounts = inc_user_namespaces(parent_ns, owner); | |
91 | if (!ucounts) | |
92 | goto fail; | |
93 | ||
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 | */ | |
100 | ret = -EPERM; | |
101 | if (current_chrooted()) | |
102 | goto fail_dec; | |
103 | ||
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 | */ | |
108 | ret = -EPERM; | |
109 | if (!kuid_has_mapping(parent_ns, owner) || | |
110 | !kgid_has_mapping(parent_ns, group)) | |
111 | goto fail_dec; | |
112 | ||
113 | ret = -ENOMEM; | |
114 | ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL); | |
115 | if (!ns) | |
116 | goto fail_dec; | |
117 | ||
118 | ret = ns_alloc_inum(&ns->ns); | |
119 | if (ret) | |
120 | goto fail_free; | |
121 | ns->ns.ops = &userns_operations; | |
122 | ||
123 | atomic_set(&ns->count, 1); | |
124 | /* Leave the new->user_ns reference with the new user namespace. */ | |
125 | ns->parent = parent_ns; | |
126 | ns->level = parent_ns->level + 1; | |
127 | ns->owner = owner; | |
128 | ns->group = group; | |
129 | INIT_WORK(&ns->work, free_user_ns); | |
130 | for (i = 0; i < UCOUNT_COUNTS; i++) { | |
131 | ns->ucount_max[i] = INT_MAX; | |
132 | } | |
133 | ns->ucounts = ucounts; | |
134 | ||
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 | ||
140 | #ifdef CONFIG_PERSISTENT_KEYRINGS | |
141 | init_rwsem(&ns->persistent_keyring_register_sem); | |
142 | #endif | |
143 | ret = -ENOMEM; | |
144 | if (!setup_userns_sysctls(ns)) | |
145 | goto fail_keyring; | |
146 | ||
147 | set_cred_user_ns(new, ns); | |
148 | return 0; | |
149 | fail_keyring: | |
150 | #ifdef CONFIG_PERSISTENT_KEYRINGS | |
151 | key_put(ns->persistent_keyring_register); | |
152 | #endif | |
153 | ns_free_inum(&ns->ns); | |
154 | fail_free: | |
155 | kmem_cache_free(user_ns_cachep, ns); | |
156 | fail_dec: | |
157 | dec_user_namespaces(ucounts); | |
158 | fail: | |
159 | return ret; | |
160 | } | |
161 | ||
162 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) | |
163 | { | |
164 | struct cred *cred; | |
165 | int err = -ENOMEM; | |
166 | ||
167 | if (!(unshare_flags & CLONE_NEWUSER)) | |
168 | return 0; | |
169 | ||
170 | cred = prepare_creds(); | |
171 | if (cred) { | |
172 | err = create_user_ns(cred); | |
173 | if (err) | |
174 | put_cred(cred); | |
175 | else | |
176 | *new_cred = cred; | |
177 | } | |
178 | ||
179 | return err; | |
180 | } | |
181 | ||
182 | static void free_user_ns(struct work_struct *work) | |
183 | { | |
184 | struct user_namespace *parent, *ns = | |
185 | container_of(work, struct user_namespace, work); | |
186 | ||
187 | do { | |
188 | struct ucounts *ucounts = ns->ucounts; | |
189 | parent = ns->parent; | |
190 | retire_userns_sysctls(ns); | |
191 | #ifdef CONFIG_PERSISTENT_KEYRINGS | |
192 | key_put(ns->persistent_keyring_register); | |
193 | #endif | |
194 | ns_free_inum(&ns->ns); | |
195 | kmem_cache_free(user_ns_cachep, ns); | |
196 | dec_user_namespaces(ucounts); | |
197 | ns = parent; | |
198 | } while (atomic_dec_and_test(&parent->count)); | |
199 | } | |
200 | ||
201 | void __put_user_ns(struct user_namespace *ns) | |
202 | { | |
203 | schedule_work(&ns->work); | |
204 | } | |
205 | EXPORT_SYMBOL(__put_user_ns); | |
206 | ||
207 | static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count) | |
208 | { | |
209 | unsigned idx, extents; | |
210 | u32 first, last, id2; | |
211 | ||
212 | id2 = id + count - 1; | |
213 | ||
214 | /* Find the matching extent */ | |
215 | extents = map->nr_extents; | |
216 | smp_rmb(); | |
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 | ||
233 | static 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; | |
240 | smp_rmb(); | |
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 | ||
256 | static 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; | |
263 | smp_rmb(); | |
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; | |
269 | } | |
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 | |
289 | * for and handle INVALID_UID being returned. INVALID_UID | |
290 | * may be tested for using uid_valid(). | |
291 | */ | |
292 | kuid_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 | } | |
297 | EXPORT_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 | */ | |
311 | uid_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 | } | |
316 | EXPORT_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 | */ | |
336 | uid_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 | } | |
345 | EXPORT_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 | |
350 | * @gid: group identifier | |
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 | */ | |
360 | kgid_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 | } | |
365 | EXPORT_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 | */ | |
379 | gid_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 | } | |
384 | EXPORT_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 | */ | |
403 | gid_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 | } | |
412 | EXPORT_SYMBOL(from_kgid_munged); | |
413 | ||
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 | */ | |
427 | kprojid_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 | } | |
432 | EXPORT_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 | */ | |
446 | projid_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 | } | |
451 | EXPORT_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 | */ | |
471 | projid_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 | } | |
480 | EXPORT_SYMBOL(from_kprojid_munged); | |
481 | ||
482 | ||
483 | static 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; | |
489 | ||
490 | lower_ns = seq_user_ns(seq); | |
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; | |
502 | } | |
503 | ||
504 | static int gid_m_show(struct seq_file *seq, void *v) | |
505 | { | |
506 | struct user_namespace *ns = seq->private; | |
507 | struct uid_gid_extent *extent = v; | |
508 | struct user_namespace *lower_ns; | |
509 | gid_t lower; | |
510 | ||
511 | lower_ns = seq_user_ns(seq); | |
512 | if ((lower_ns == ns) && lower_ns->parent) | |
513 | lower_ns = lower_ns->parent; | |
514 | ||
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 | ||
525 | static 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 | ||
546 | static void *m_start(struct seq_file *seq, loff_t *ppos, | |
547 | struct uid_gid_map *map) | |
548 | { | |
549 | struct uid_gid_extent *extent = NULL; | |
550 | loff_t pos = *ppos; | |
551 | ||
552 | if (pos < map->nr_extents) { | |
553 | osb(); | |
554 | extent = &map->extent[pos]; | |
555 | } | |
556 | ||
557 | return extent; | |
558 | } | |
559 | ||
560 | static 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 | ||
567 | static 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 | ||
574 | static 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 | ||
581 | static void *m_next(struct seq_file *seq, void *v, loff_t *pos) | |
582 | { | |
583 | (*pos)++; | |
584 | return seq->op->start(seq, pos); | |
585 | } | |
586 | ||
587 | static void m_stop(struct seq_file *seq, void *v) | |
588 | { | |
589 | return; | |
590 | } | |
591 | ||
592 | const struct seq_operations proc_uid_seq_operations = { | |
593 | .start = uid_m_start, | |
594 | .stop = m_stop, | |
595 | .next = m_next, | |
596 | .show = uid_m_show, | |
597 | }; | |
598 | ||
599 | const struct seq_operations proc_gid_seq_operations = { | |
600 | .start = gid_m_start, | |
601 | .stop = m_stop, | |
602 | .next = m_next, | |
603 | .show = gid_m_show, | |
604 | }; | |
605 | ||
606 | const struct seq_operations proc_projid_seq_operations = { | |
607 | .start = projid_m_start, | |
608 | .stop = m_stop, | |
609 | .next = m_next, | |
610 | .show = projid_m_show, | |
611 | }; | |
612 | ||
613 | static bool mappings_overlap(struct uid_gid_map *new_map, | |
614 | struct uid_gid_extent *extent) | |
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 | ||
649 | static 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; | |
659 | struct uid_gid_extent *extent = NULL; | |
660 | char *kbuf = NULL, *pos, *next_line; | |
661 | ssize_t ret = -EINVAL; | |
662 | ||
663 | /* | |
664 | * The userns_state_mutex serializes all writes to any given map. | |
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 | |
679 | * order and smp_rmb() is guaranteed that we don't have crazy | |
680 | * architectures returning stale data. | |
681 | */ | |
682 | mutex_lock(&userns_state_mutex); | |
683 | ||
684 | ret = -EPERM; | |
685 | /* Only allow one successful write to the map */ | |
686 | if (map->nr_extents != 0) | |
687 | goto out; | |
688 | ||
689 | /* | |
690 | * Adjusting namespace settings requires capabilities on the target. | |
691 | */ | |
692 | if (cap_valid(cap_setid) && !file_ns_capable(file, ns, CAP_SYS_ADMIN)) | |
693 | goto out; | |
694 | ||
695 | /* Only allow < page size writes at the beginning of the file */ | |
696 | ret = -EINVAL; | |
697 | if ((*ppos != 0) || (count >= PAGE_SIZE)) | |
698 | goto out; | |
699 | ||
700 | /* Slurp in the user data */ | |
701 | kbuf = memdup_user_nul(buf, count); | |
702 | if (IS_ERR(kbuf)) { | |
703 | ret = PTR_ERR(kbuf); | |
704 | kbuf = NULL; | |
705 | goto out; | |
706 | } | |
707 | ||
708 | /* Parse the user data */ | |
709 | ret = -EINVAL; | |
710 | pos = kbuf; | |
711 | new_map.nr_extents = 0; | |
712 | for (; pos; pos = next_line) { | |
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; | |
722 | } | |
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) || | |
746 | (extent->lower_first == (u32) -1)) | |
747 | goto out; | |
748 | ||
749 | /* Verify count is not zero and does not cause the | |
750 | * extent to wrap | |
751 | */ | |
752 | if ((extent->first + extent->count) <= extent->first) | |
753 | goto out; | |
754 | if ((extent->lower_first + extent->count) <= | |
755 | extent->lower_first) | |
756 | goto out; | |
757 | ||
758 | /* Do the ranges in extent overlap any previous extents? */ | |
759 | if (mappings_overlap(&new_map, extent)) | |
760 | goto out; | |
761 | ||
762 | new_map.nr_extents++; | |
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; | |
768 | } | |
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. */ | |
775 | if (!new_idmap_permitted(file, ns, cap_setid, &new_map)) | |
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; | |
806 | out: | |
807 | mutex_unlock(&userns_state_mutex); | |
808 | kfree(kbuf); | |
809 | return ret; | |
810 | } | |
811 | ||
812 | ssize_t proc_uid_map_write(struct file *file, const char __user *buf, | |
813 | size_t size, loff_t *ppos) | |
814 | { | |
815 | struct seq_file *seq = file->private_data; | |
816 | struct user_namespace *ns = seq->private; | |
817 | struct user_namespace *seq_ns = seq_user_ns(seq); | |
818 | ||
819 | if (!ns->parent) | |
820 | return -EPERM; | |
821 | ||
822 | if ((seq_ns != ns) && (seq_ns != ns->parent)) | |
823 | return -EPERM; | |
824 | ||
825 | return map_write(file, buf, size, ppos, CAP_SETUID, | |
826 | &ns->uid_map, &ns->parent->uid_map); | |
827 | } | |
828 | ||
829 | ssize_t proc_gid_map_write(struct file *file, const char __user *buf, | |
830 | size_t size, loff_t *ppos) | |
831 | { | |
832 | struct seq_file *seq = file->private_data; | |
833 | struct user_namespace *ns = seq->private; | |
834 | struct user_namespace *seq_ns = seq_user_ns(seq); | |
835 | ||
836 | if (!ns->parent) | |
837 | return -EPERM; | |
838 | ||
839 | if ((seq_ns != ns) && (seq_ns != ns->parent)) | |
840 | return -EPERM; | |
841 | ||
842 | return map_write(file, buf, size, ppos, CAP_SETGID, | |
843 | &ns->gid_map, &ns->parent->gid_map); | |
844 | } | |
845 | ||
846 | ssize_t proc_projid_map_write(struct file *file, const char __user *buf, | |
847 | size_t size, loff_t *ppos) | |
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 | ||
864 | static bool new_idmap_permitted(const struct file *file, | |
865 | struct user_namespace *ns, int cap_setid, | |
866 | struct uid_gid_map *new_map) | |
867 | { | |
868 | const struct cred *cred = file->f_cred; | |
869 | /* Don't allow mappings that would allow anything that wouldn't | |
870 | * be allowed without the establishment of unprivileged mappings. | |
871 | */ | |
872 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1) && | |
873 | uid_eq(ns->owner, cred->euid)) { | |
874 | u32 id = new_map->extent[0].lower_first; | |
875 | if (cap_setid == CAP_SETUID) { | |
876 | kuid_t uid = make_kuid(ns->parent, id); | |
877 | if (uid_eq(uid, cred->euid)) | |
878 | return true; | |
879 | } else if (cap_setid == CAP_SETGID) { | |
880 | kgid_t gid = make_kgid(ns->parent, id); | |
881 | if (!(ns->flags & USERNS_SETGROUPS_ALLOWED) && | |
882 | gid_eq(gid, cred->egid)) | |
883 | return true; | |
884 | } | |
885 | } | |
886 | ||
887 | /* Allow anyone to set a mapping that doesn't require privilege */ | |
888 | if (!cap_valid(cap_setid)) | |
889 | return true; | |
890 | ||
891 | /* Allow the specified ids if we have the appropriate capability | |
892 | * (CAP_SETUID or CAP_SETGID) over the parent user namespace. | |
893 | * And the opener of the id file also had the approprpiate capability. | |
894 | */ | |
895 | if (ns_capable(ns->parent, cap_setid) && | |
896 | file_ns_capable(file, ns->parent, cap_setid)) | |
897 | return true; | |
898 | ||
899 | return false; | |
900 | } | |
901 | ||
902 | int 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 | ||
913 | ssize_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; | |
973 | out: | |
974 | return ret; | |
975 | out_unlock: | |
976 | mutex_unlock(&userns_state_mutex); | |
977 | goto out; | |
978 | } | |
979 | ||
980 | bool userns_may_setgroups(const struct user_namespace *ns) | |
981 | { | |
982 | bool allowed; | |
983 | ||
984 | mutex_lock(&userns_state_mutex); | |
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; | |
989 | /* Is setgroups allowed? */ | |
990 | allowed = allowed && (ns->flags & USERNS_SETGROUPS_ALLOWED); | |
991 | mutex_unlock(&userns_state_mutex); | |
992 | ||
993 | return allowed; | |
994 | } | |
995 | ||
996 | /* | |
997 | * Returns true if @ns is the same namespace as or a descendant of | |
998 | * @target_ns. | |
999 | */ | |
1000 | bool 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 | } | |
1009 | EXPORT_SYMBOL(current_in_userns); | |
1010 | ||
1011 | static inline struct user_namespace *to_user_ns(struct ns_common *ns) | |
1012 | { | |
1013 | return container_of(ns, struct user_namespace, ns); | |
1014 | } | |
1015 | ||
1016 | static struct ns_common *userns_get(struct task_struct *task) | |
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 | ||
1024 | return user_ns ? &user_ns->ns : NULL; | |
1025 | } | |
1026 | ||
1027 | static void userns_put(struct ns_common *ns) | |
1028 | { | |
1029 | put_user_ns(to_user_ns(ns)); | |
1030 | } | |
1031 | ||
1032 | static int userns_install(struct nsproxy *nsproxy, struct ns_common *ns) | |
1033 | { | |
1034 | struct user_namespace *user_ns = to_user_ns(ns); | |
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 | ||
1043 | /* Tasks that share a thread group must share a user namespace */ | |
1044 | if (!thread_group_empty(current)) | |
1045 | return -EINVAL; | |
1046 | ||
1047 | if (current->fs->users != 1) | |
1048 | return -EINVAL; | |
1049 | ||
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 | ||
1063 | struct 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 | ||
1081 | static struct user_namespace *userns_owner(struct ns_common *ns) | |
1082 | { | |
1083 | return to_user_ns(ns)->parent; | |
1084 | } | |
1085 | ||
1086 | const 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, | |
1092 | .owner = userns_owner, | |
1093 | .get_parent = ns_get_owner, | |
1094 | }; | |
1095 | ||
1096 | static __init int user_namespaces_init(void) | |
1097 | { | |
1098 | user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); | |
1099 | return 0; | |
1100 | } | |
1101 | subsys_initcall(user_namespaces_init); |