]>
Commit | Line | Data |
---|---|---|
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> |
cde1975b | 12 | #include <linux/proc_fs.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 PE |
26 | static struct kmem_cache *user_ns_cachep __read_mostly; |
27 | ||
22d917d8 EB |
28 | static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid, |
29 | struct uid_gid_map *map); | |
30 | ||
cde1975b EB |
31 | static void set_cred_user_ns(struct cred *cred, struct user_namespace *user_ns) |
32 | { | |
33 | /* Start with the same capabilities as init but useless for doing | |
34 | * anything as the capabilities are bound to the new user namespace. | |
35 | */ | |
36 | cred->securebits = SECUREBITS_DEFAULT; | |
37 | cred->cap_inheritable = CAP_EMPTY_SET; | |
38 | cred->cap_permitted = CAP_FULL_SET; | |
39 | cred->cap_effective = CAP_FULL_SET; | |
40 | cred->cap_bset = CAP_FULL_SET; | |
41 | #ifdef CONFIG_KEYS | |
42 | key_put(cred->request_key_auth); | |
43 | cred->request_key_auth = NULL; | |
44 | #endif | |
45 | /* tgcred will be cleared in our caller bc CLONE_THREAD won't be set */ | |
46 | cred->user_ns = user_ns; | |
47 | } | |
48 | ||
77ec739d | 49 | /* |
18b6e041 SH |
50 | * Create a new user namespace, deriving the creator from the user in the |
51 | * passed credentials, and replacing that user with the new root user for the | |
52 | * new namespace. | |
53 | * | |
54 | * This is called by copy_creds(), which will finish setting the target task's | |
55 | * credentials. | |
77ec739d | 56 | */ |
18b6e041 | 57 | int create_user_ns(struct cred *new) |
77ec739d | 58 | { |
0093ccb6 | 59 | struct user_namespace *ns, *parent_ns = new->user_ns; |
078de5f7 EB |
60 | kuid_t owner = new->euid; |
61 | kgid_t group = new->egid; | |
98f842e6 | 62 | int ret; |
783291e6 EB |
63 | |
64 | /* The creator needs a mapping in the parent user namespace | |
65 | * or else we won't be able to reasonably tell userspace who | |
66 | * created a user_namespace. | |
67 | */ | |
68 | if (!kuid_has_mapping(parent_ns, owner) || | |
69 | !kgid_has_mapping(parent_ns, group)) | |
70 | return -EPERM; | |
77ec739d | 71 | |
22d917d8 | 72 | ns = kmem_cache_zalloc(user_ns_cachep, GFP_KERNEL); |
77ec739d | 73 | if (!ns) |
18b6e041 | 74 | return -ENOMEM; |
77ec739d | 75 | |
98f842e6 EB |
76 | ret = proc_alloc_inum(&ns->proc_inum); |
77 | if (ret) { | |
78 | kmem_cache_free(user_ns_cachep, ns); | |
79 | return ret; | |
80 | } | |
81 | ||
c61a2810 | 82 | atomic_set(&ns->count, 1); |
cde1975b | 83 | /* Leave the new->user_ns reference with the new user namespace. */ |
aeb3ae9d | 84 | ns->parent = parent_ns; |
783291e6 EB |
85 | ns->owner = owner; |
86 | ns->group = group; | |
22d917d8 | 87 | |
cde1975b | 88 | set_cred_user_ns(new, ns); |
0093ccb6 | 89 | |
18b6e041 | 90 | return 0; |
acce292c CLG |
91 | } |
92 | ||
b2e0d987 EB |
93 | int unshare_userns(unsigned long unshare_flags, struct cred **new_cred) |
94 | { | |
95 | struct cred *cred; | |
96 | ||
97 | if (!(unshare_flags & CLONE_NEWUSER)) | |
98 | return 0; | |
99 | ||
100 | cred = prepare_creds(); | |
101 | if (!cred) | |
102 | return -ENOMEM; | |
103 | ||
104 | *new_cred = cred; | |
105 | return create_user_ns(cred); | |
106 | } | |
107 | ||
c61a2810 | 108 | void free_user_ns(struct user_namespace *ns) |
acce292c | 109 | { |
c61a2810 | 110 | struct user_namespace *parent; |
783291e6 | 111 | |
c61a2810 EB |
112 | do { |
113 | parent = ns->parent; | |
114 | proc_free_inum(ns->proc_inum); | |
115 | kmem_cache_free(user_ns_cachep, ns); | |
116 | ns = parent; | |
117 | } while (atomic_dec_and_test(&parent->count)); | |
acce292c | 118 | } |
6a3fd92e | 119 | EXPORT_SYMBOL(free_user_ns); |
5c1469de | 120 | |
22d917d8 | 121 | static u32 map_id_range_down(struct uid_gid_map *map, u32 id, u32 count) |
5c1469de | 122 | { |
22d917d8 EB |
123 | unsigned idx, extents; |
124 | u32 first, last, id2; | |
5c1469de | 125 | |
22d917d8 | 126 | id2 = id + count - 1; |
5c1469de | 127 | |
22d917d8 EB |
128 | /* Find the matching extent */ |
129 | extents = map->nr_extents; | |
130 | smp_read_barrier_depends(); | |
131 | for (idx = 0; idx < extents; idx++) { | |
132 | first = map->extent[idx].first; | |
133 | last = first + map->extent[idx].count - 1; | |
134 | if (id >= first && id <= last && | |
135 | (id2 >= first && id2 <= last)) | |
136 | break; | |
137 | } | |
138 | /* Map the id or note failure */ | |
139 | if (idx < extents) | |
140 | id = (id - first) + map->extent[idx].lower_first; | |
141 | else | |
142 | id = (u32) -1; | |
143 | ||
144 | return id; | |
145 | } | |
146 | ||
147 | static u32 map_id_down(struct uid_gid_map *map, u32 id) | |
148 | { | |
149 | unsigned idx, extents; | |
150 | u32 first, last; | |
151 | ||
152 | /* Find the matching extent */ | |
153 | extents = map->nr_extents; | |
154 | smp_read_barrier_depends(); | |
155 | for (idx = 0; idx < extents; idx++) { | |
156 | first = map->extent[idx].first; | |
157 | last = first + map->extent[idx].count - 1; | |
158 | if (id >= first && id <= last) | |
159 | break; | |
160 | } | |
161 | /* Map the id or note failure */ | |
162 | if (idx < extents) | |
163 | id = (id - first) + map->extent[idx].lower_first; | |
164 | else | |
165 | id = (u32) -1; | |
166 | ||
167 | return id; | |
168 | } | |
169 | ||
170 | static u32 map_id_up(struct uid_gid_map *map, u32 id) | |
171 | { | |
172 | unsigned idx, extents; | |
173 | u32 first, last; | |
174 | ||
175 | /* Find the matching extent */ | |
176 | extents = map->nr_extents; | |
177 | smp_read_barrier_depends(); | |
178 | for (idx = 0; idx < extents; idx++) { | |
179 | first = map->extent[idx].lower_first; | |
180 | last = first + map->extent[idx].count - 1; | |
181 | if (id >= first && id <= last) | |
182 | break; | |
5c1469de | 183 | } |
22d917d8 EB |
184 | /* Map the id or note failure */ |
185 | if (idx < extents) | |
186 | id = (id - first) + map->extent[idx].first; | |
187 | else | |
188 | id = (u32) -1; | |
189 | ||
190 | return id; | |
191 | } | |
192 | ||
193 | /** | |
194 | * make_kuid - Map a user-namespace uid pair into a kuid. | |
195 | * @ns: User namespace that the uid is in | |
196 | * @uid: User identifier | |
197 | * | |
198 | * Maps a user-namespace uid pair into a kernel internal kuid, | |
199 | * and returns that kuid. | |
200 | * | |
201 | * When there is no mapping defined for the user-namespace uid | |
202 | * pair INVALID_UID is returned. Callers are expected to test | |
203 | * for and handle handle INVALID_UID being returned. INVALID_UID | |
204 | * may be tested for using uid_valid(). | |
205 | */ | |
206 | kuid_t make_kuid(struct user_namespace *ns, uid_t uid) | |
207 | { | |
208 | /* Map the uid to a global kernel uid */ | |
209 | return KUIDT_INIT(map_id_down(&ns->uid_map, uid)); | |
210 | } | |
211 | EXPORT_SYMBOL(make_kuid); | |
212 | ||
213 | /** | |
214 | * from_kuid - Create a uid from a kuid user-namespace pair. | |
215 | * @targ: The user namespace we want a uid in. | |
216 | * @kuid: The kernel internal uid to start with. | |
217 | * | |
218 | * Map @kuid into the user-namespace specified by @targ and | |
219 | * return the resulting uid. | |
220 | * | |
221 | * There is always a mapping into the initial user_namespace. | |
222 | * | |
223 | * If @kuid has no mapping in @targ (uid_t)-1 is returned. | |
224 | */ | |
225 | uid_t from_kuid(struct user_namespace *targ, kuid_t kuid) | |
226 | { | |
227 | /* Map the uid from a global kernel uid */ | |
228 | return map_id_up(&targ->uid_map, __kuid_val(kuid)); | |
229 | } | |
230 | EXPORT_SYMBOL(from_kuid); | |
231 | ||
232 | /** | |
233 | * from_kuid_munged - Create a uid from a kuid user-namespace pair. | |
234 | * @targ: The user namespace we want a uid in. | |
235 | * @kuid: The kernel internal uid to start with. | |
236 | * | |
237 | * Map @kuid into the user-namespace specified by @targ and | |
238 | * return the resulting uid. | |
239 | * | |
240 | * There is always a mapping into the initial user_namespace. | |
241 | * | |
242 | * Unlike from_kuid from_kuid_munged never fails and always | |
243 | * returns a valid uid. This makes from_kuid_munged appropriate | |
244 | * for use in syscalls like stat and getuid where failing the | |
245 | * system call and failing to provide a valid uid are not an | |
246 | * options. | |
247 | * | |
248 | * If @kuid has no mapping in @targ overflowuid is returned. | |
249 | */ | |
250 | uid_t from_kuid_munged(struct user_namespace *targ, kuid_t kuid) | |
251 | { | |
252 | uid_t uid; | |
253 | uid = from_kuid(targ, kuid); | |
254 | ||
255 | if (uid == (uid_t) -1) | |
256 | uid = overflowuid; | |
257 | return uid; | |
258 | } | |
259 | EXPORT_SYMBOL(from_kuid_munged); | |
260 | ||
261 | /** | |
262 | * make_kgid - Map a user-namespace gid pair into a kgid. | |
263 | * @ns: User namespace that the gid is in | |
264 | * @uid: group identifier | |
265 | * | |
266 | * Maps a user-namespace gid pair into a kernel internal kgid, | |
267 | * and returns that kgid. | |
268 | * | |
269 | * When there is no mapping defined for the user-namespace gid | |
270 | * pair INVALID_GID is returned. Callers are expected to test | |
271 | * for and handle INVALID_GID being returned. INVALID_GID may be | |
272 | * tested for using gid_valid(). | |
273 | */ | |
274 | kgid_t make_kgid(struct user_namespace *ns, gid_t gid) | |
275 | { | |
276 | /* Map the gid to a global kernel gid */ | |
277 | return KGIDT_INIT(map_id_down(&ns->gid_map, gid)); | |
278 | } | |
279 | EXPORT_SYMBOL(make_kgid); | |
280 | ||
281 | /** | |
282 | * from_kgid - Create a gid from a kgid user-namespace pair. | |
283 | * @targ: The user namespace we want a gid in. | |
284 | * @kgid: The kernel internal gid to start with. | |
285 | * | |
286 | * Map @kgid into the user-namespace specified by @targ and | |
287 | * return the resulting gid. | |
288 | * | |
289 | * There is always a mapping into the initial user_namespace. | |
290 | * | |
291 | * If @kgid has no mapping in @targ (gid_t)-1 is returned. | |
292 | */ | |
293 | gid_t from_kgid(struct user_namespace *targ, kgid_t kgid) | |
294 | { | |
295 | /* Map the gid from a global kernel gid */ | |
296 | return map_id_up(&targ->gid_map, __kgid_val(kgid)); | |
297 | } | |
298 | EXPORT_SYMBOL(from_kgid); | |
299 | ||
300 | /** | |
301 | * from_kgid_munged - Create a gid from a kgid user-namespace pair. | |
302 | * @targ: The user namespace we want a gid in. | |
303 | * @kgid: The kernel internal gid to start with. | |
304 | * | |
305 | * Map @kgid into the user-namespace specified by @targ and | |
306 | * return the resulting gid. | |
307 | * | |
308 | * There is always a mapping into the initial user_namespace. | |
309 | * | |
310 | * Unlike from_kgid from_kgid_munged never fails and always | |
311 | * returns a valid gid. This makes from_kgid_munged appropriate | |
312 | * for use in syscalls like stat and getgid where failing the | |
313 | * system call and failing to provide a valid gid are not options. | |
314 | * | |
315 | * If @kgid has no mapping in @targ overflowgid is returned. | |
316 | */ | |
317 | gid_t from_kgid_munged(struct user_namespace *targ, kgid_t kgid) | |
318 | { | |
319 | gid_t gid; | |
320 | gid = from_kgid(targ, kgid); | |
321 | ||
322 | if (gid == (gid_t) -1) | |
323 | gid = overflowgid; | |
324 | return gid; | |
325 | } | |
326 | EXPORT_SYMBOL(from_kgid_munged); | |
327 | ||
f76d207a EB |
328 | /** |
329 | * make_kprojid - Map a user-namespace projid pair into a kprojid. | |
330 | * @ns: User namespace that the projid is in | |
331 | * @projid: Project identifier | |
332 | * | |
333 | * Maps a user-namespace uid pair into a kernel internal kuid, | |
334 | * and returns that kuid. | |
335 | * | |
336 | * When there is no mapping defined for the user-namespace projid | |
337 | * pair INVALID_PROJID is returned. Callers are expected to test | |
338 | * for and handle handle INVALID_PROJID being returned. INVALID_PROJID | |
339 | * may be tested for using projid_valid(). | |
340 | */ | |
341 | kprojid_t make_kprojid(struct user_namespace *ns, projid_t projid) | |
342 | { | |
343 | /* Map the uid to a global kernel uid */ | |
344 | return KPROJIDT_INIT(map_id_down(&ns->projid_map, projid)); | |
345 | } | |
346 | EXPORT_SYMBOL(make_kprojid); | |
347 | ||
348 | /** | |
349 | * from_kprojid - Create a projid from a kprojid user-namespace pair. | |
350 | * @targ: The user namespace we want a projid in. | |
351 | * @kprojid: The kernel internal project identifier to start with. | |
352 | * | |
353 | * Map @kprojid into the user-namespace specified by @targ and | |
354 | * return the resulting projid. | |
355 | * | |
356 | * There is always a mapping into the initial user_namespace. | |
357 | * | |
358 | * If @kprojid has no mapping in @targ (projid_t)-1 is returned. | |
359 | */ | |
360 | projid_t from_kprojid(struct user_namespace *targ, kprojid_t kprojid) | |
361 | { | |
362 | /* Map the uid from a global kernel uid */ | |
363 | return map_id_up(&targ->projid_map, __kprojid_val(kprojid)); | |
364 | } | |
365 | EXPORT_SYMBOL(from_kprojid); | |
366 | ||
367 | /** | |
368 | * from_kprojid_munged - Create a projiid from a kprojid user-namespace pair. | |
369 | * @targ: The user namespace we want a projid in. | |
370 | * @kprojid: The kernel internal projid to start with. | |
371 | * | |
372 | * Map @kprojid into the user-namespace specified by @targ and | |
373 | * return the resulting projid. | |
374 | * | |
375 | * There is always a mapping into the initial user_namespace. | |
376 | * | |
377 | * Unlike from_kprojid from_kprojid_munged never fails and always | |
378 | * returns a valid projid. This makes from_kprojid_munged | |
379 | * appropriate for use in syscalls like stat and where | |
380 | * failing the system call and failing to provide a valid projid are | |
381 | * not an options. | |
382 | * | |
383 | * If @kprojid has no mapping in @targ OVERFLOW_PROJID is returned. | |
384 | */ | |
385 | projid_t from_kprojid_munged(struct user_namespace *targ, kprojid_t kprojid) | |
386 | { | |
387 | projid_t projid; | |
388 | projid = from_kprojid(targ, kprojid); | |
389 | ||
390 | if (projid == (projid_t) -1) | |
391 | projid = OVERFLOW_PROJID; | |
392 | return projid; | |
393 | } | |
394 | EXPORT_SYMBOL(from_kprojid_munged); | |
395 | ||
396 | ||
22d917d8 EB |
397 | static int uid_m_show(struct seq_file *seq, void *v) |
398 | { | |
399 | struct user_namespace *ns = seq->private; | |
400 | struct uid_gid_extent *extent = v; | |
401 | struct user_namespace *lower_ns; | |
402 | uid_t lower; | |
5c1469de | 403 | |
c450f371 | 404 | lower_ns = seq_user_ns(seq); |
22d917d8 EB |
405 | if ((lower_ns == ns) && lower_ns->parent) |
406 | lower_ns = lower_ns->parent; | |
407 | ||
408 | lower = from_kuid(lower_ns, KUIDT_INIT(extent->lower_first)); | |
409 | ||
410 | seq_printf(seq, "%10u %10u %10u\n", | |
411 | extent->first, | |
412 | lower, | |
413 | extent->count); | |
414 | ||
415 | return 0; | |
5c1469de EB |
416 | } |
417 | ||
22d917d8 | 418 | static int gid_m_show(struct seq_file *seq, void *v) |
5c1469de | 419 | { |
22d917d8 EB |
420 | struct user_namespace *ns = seq->private; |
421 | struct uid_gid_extent *extent = v; | |
422 | struct user_namespace *lower_ns; | |
423 | gid_t lower; | |
5c1469de | 424 | |
c450f371 | 425 | lower_ns = seq_user_ns(seq); |
22d917d8 EB |
426 | if ((lower_ns == ns) && lower_ns->parent) |
427 | lower_ns = lower_ns->parent; | |
5c1469de | 428 | |
22d917d8 EB |
429 | lower = from_kgid(lower_ns, KGIDT_INIT(extent->lower_first)); |
430 | ||
431 | seq_printf(seq, "%10u %10u %10u\n", | |
432 | extent->first, | |
433 | lower, | |
434 | extent->count); | |
435 | ||
436 | return 0; | |
437 | } | |
438 | ||
f76d207a EB |
439 | static int projid_m_show(struct seq_file *seq, void *v) |
440 | { | |
441 | struct user_namespace *ns = seq->private; | |
442 | struct uid_gid_extent *extent = v; | |
443 | struct user_namespace *lower_ns; | |
444 | projid_t lower; | |
445 | ||
446 | lower_ns = seq_user_ns(seq); | |
447 | if ((lower_ns == ns) && lower_ns->parent) | |
448 | lower_ns = lower_ns->parent; | |
449 | ||
450 | lower = from_kprojid(lower_ns, KPROJIDT_INIT(extent->lower_first)); | |
451 | ||
452 | seq_printf(seq, "%10u %10u %10u\n", | |
453 | extent->first, | |
454 | lower, | |
455 | extent->count); | |
456 | ||
457 | return 0; | |
458 | } | |
459 | ||
22d917d8 EB |
460 | static void *m_start(struct seq_file *seq, loff_t *ppos, struct uid_gid_map *map) |
461 | { | |
462 | struct uid_gid_extent *extent = NULL; | |
463 | loff_t pos = *ppos; | |
464 | ||
465 | if (pos < map->nr_extents) | |
466 | extent = &map->extent[pos]; | |
467 | ||
468 | return extent; | |
469 | } | |
470 | ||
471 | static void *uid_m_start(struct seq_file *seq, loff_t *ppos) | |
472 | { | |
473 | struct user_namespace *ns = seq->private; | |
474 | ||
475 | return m_start(seq, ppos, &ns->uid_map); | |
476 | } | |
477 | ||
478 | static void *gid_m_start(struct seq_file *seq, loff_t *ppos) | |
479 | { | |
480 | struct user_namespace *ns = seq->private; | |
481 | ||
482 | return m_start(seq, ppos, &ns->gid_map); | |
483 | } | |
484 | ||
f76d207a EB |
485 | static void *projid_m_start(struct seq_file *seq, loff_t *ppos) |
486 | { | |
487 | struct user_namespace *ns = seq->private; | |
488 | ||
489 | return m_start(seq, ppos, &ns->projid_map); | |
490 | } | |
491 | ||
22d917d8 EB |
492 | static void *m_next(struct seq_file *seq, void *v, loff_t *pos) |
493 | { | |
494 | (*pos)++; | |
495 | return seq->op->start(seq, pos); | |
496 | } | |
497 | ||
498 | static void m_stop(struct seq_file *seq, void *v) | |
499 | { | |
500 | return; | |
501 | } | |
502 | ||
503 | struct seq_operations proc_uid_seq_operations = { | |
504 | .start = uid_m_start, | |
505 | .stop = m_stop, | |
506 | .next = m_next, | |
507 | .show = uid_m_show, | |
508 | }; | |
509 | ||
510 | struct seq_operations proc_gid_seq_operations = { | |
511 | .start = gid_m_start, | |
512 | .stop = m_stop, | |
513 | .next = m_next, | |
514 | .show = gid_m_show, | |
515 | }; | |
516 | ||
f76d207a EB |
517 | struct seq_operations proc_projid_seq_operations = { |
518 | .start = projid_m_start, | |
519 | .stop = m_stop, | |
520 | .next = m_next, | |
521 | .show = projid_m_show, | |
522 | }; | |
523 | ||
0bd14b4f EB |
524 | static bool mappings_overlap(struct uid_gid_map *new_map, struct uid_gid_extent *extent) |
525 | { | |
526 | u32 upper_first, lower_first, upper_last, lower_last; | |
527 | unsigned idx; | |
528 | ||
529 | upper_first = extent->first; | |
530 | lower_first = extent->lower_first; | |
531 | upper_last = upper_first + extent->count - 1; | |
532 | lower_last = lower_first + extent->count - 1; | |
533 | ||
534 | for (idx = 0; idx < new_map->nr_extents; idx++) { | |
535 | u32 prev_upper_first, prev_lower_first; | |
536 | u32 prev_upper_last, prev_lower_last; | |
537 | struct uid_gid_extent *prev; | |
538 | ||
539 | prev = &new_map->extent[idx]; | |
540 | ||
541 | prev_upper_first = prev->first; | |
542 | prev_lower_first = prev->lower_first; | |
543 | prev_upper_last = prev_upper_first + prev->count - 1; | |
544 | prev_lower_last = prev_lower_first + prev->count - 1; | |
545 | ||
546 | /* Does the upper range intersect a previous extent? */ | |
547 | if ((prev_upper_first <= upper_last) && | |
548 | (prev_upper_last >= upper_first)) | |
549 | return true; | |
550 | ||
551 | /* Does the lower range intersect a previous extent? */ | |
552 | if ((prev_lower_first <= lower_last) && | |
553 | (prev_lower_last >= lower_first)) | |
554 | return true; | |
555 | } | |
556 | return false; | |
557 | } | |
558 | ||
559 | ||
22d917d8 EB |
560 | static DEFINE_MUTEX(id_map_mutex); |
561 | ||
562 | static ssize_t map_write(struct file *file, const char __user *buf, | |
563 | size_t count, loff_t *ppos, | |
564 | int cap_setid, | |
565 | struct uid_gid_map *map, | |
566 | struct uid_gid_map *parent_map) | |
567 | { | |
568 | struct seq_file *seq = file->private_data; | |
569 | struct user_namespace *ns = seq->private; | |
570 | struct uid_gid_map new_map; | |
571 | unsigned idx; | |
0bd14b4f | 572 | struct uid_gid_extent *extent = NULL; |
22d917d8 EB |
573 | unsigned long page = 0; |
574 | char *kbuf, *pos, *next_line; | |
575 | ssize_t ret = -EINVAL; | |
576 | ||
577 | /* | |
578 | * The id_map_mutex serializes all writes to any given map. | |
579 | * | |
580 | * Any map is only ever written once. | |
581 | * | |
582 | * An id map fits within 1 cache line on most architectures. | |
583 | * | |
584 | * On read nothing needs to be done unless you are on an | |
585 | * architecture with a crazy cache coherency model like alpha. | |
586 | * | |
587 | * There is a one time data dependency between reading the | |
588 | * count of the extents and the values of the extents. The | |
589 | * desired behavior is to see the values of the extents that | |
590 | * were written before the count of the extents. | |
591 | * | |
592 | * To achieve this smp_wmb() is used on guarantee the write | |
593 | * order and smp_read_barrier_depends() is guaranteed that we | |
594 | * don't have crazy architectures returning stale data. | |
595 | * | |
596 | */ | |
597 | mutex_lock(&id_map_mutex); | |
598 | ||
599 | ret = -EPERM; | |
600 | /* Only allow one successful write to the map */ | |
601 | if (map->nr_extents != 0) | |
602 | goto out; | |
603 | ||
604 | /* Require the appropriate privilege CAP_SETUID or CAP_SETGID | |
605 | * over the user namespace in order to set the id mapping. | |
5c1469de | 606 | */ |
f76d207a | 607 | if (cap_valid(cap_setid) && !ns_capable(ns, cap_setid)) |
22d917d8 EB |
608 | goto out; |
609 | ||
610 | /* Get a buffer */ | |
611 | ret = -ENOMEM; | |
612 | page = __get_free_page(GFP_TEMPORARY); | |
613 | kbuf = (char *) page; | |
614 | if (!page) | |
615 | goto out; | |
616 | ||
617 | /* Only allow <= page size writes at the beginning of the file */ | |
618 | ret = -EINVAL; | |
619 | if ((*ppos != 0) || (count >= PAGE_SIZE)) | |
620 | goto out; | |
621 | ||
622 | /* Slurp in the user data */ | |
623 | ret = -EFAULT; | |
624 | if (copy_from_user(kbuf, buf, count)) | |
625 | goto out; | |
626 | kbuf[count] = '\0'; | |
627 | ||
628 | /* Parse the user data */ | |
629 | ret = -EINVAL; | |
630 | pos = kbuf; | |
631 | new_map.nr_extents = 0; | |
632 | for (;pos; pos = next_line) { | |
633 | extent = &new_map.extent[new_map.nr_extents]; | |
634 | ||
635 | /* Find the end of line and ensure I don't look past it */ | |
636 | next_line = strchr(pos, '\n'); | |
637 | if (next_line) { | |
638 | *next_line = '\0'; | |
639 | next_line++; | |
640 | if (*next_line == '\0') | |
641 | next_line = NULL; | |
5c1469de | 642 | } |
22d917d8 EB |
643 | |
644 | pos = skip_spaces(pos); | |
645 | extent->first = simple_strtoul(pos, &pos, 10); | |
646 | if (!isspace(*pos)) | |
647 | goto out; | |
648 | ||
649 | pos = skip_spaces(pos); | |
650 | extent->lower_first = simple_strtoul(pos, &pos, 10); | |
651 | if (!isspace(*pos)) | |
652 | goto out; | |
653 | ||
654 | pos = skip_spaces(pos); | |
655 | extent->count = simple_strtoul(pos, &pos, 10); | |
656 | if (*pos && !isspace(*pos)) | |
657 | goto out; | |
658 | ||
659 | /* Verify there is not trailing junk on the line */ | |
660 | pos = skip_spaces(pos); | |
661 | if (*pos != '\0') | |
662 | goto out; | |
663 | ||
664 | /* Verify we have been given valid starting values */ | |
665 | if ((extent->first == (u32) -1) || | |
666 | (extent->lower_first == (u32) -1 )) | |
667 | goto out; | |
668 | ||
669 | /* Verify count is not zero and does not cause the extent to wrap */ | |
670 | if ((extent->first + extent->count) <= extent->first) | |
671 | goto out; | |
672 | if ((extent->lower_first + extent->count) <= extent->lower_first) | |
673 | goto out; | |
674 | ||
0bd14b4f EB |
675 | /* Do the ranges in extent overlap any previous extents? */ |
676 | if (mappings_overlap(&new_map, extent)) | |
22d917d8 EB |
677 | goto out; |
678 | ||
679 | new_map.nr_extents++; | |
22d917d8 EB |
680 | |
681 | /* Fail if the file contains too many extents */ | |
682 | if ((new_map.nr_extents == UID_GID_MAP_MAX_EXTENTS) && | |
683 | (next_line != NULL)) | |
684 | goto out; | |
5c1469de | 685 | } |
22d917d8 EB |
686 | /* Be very certaint the new map actually exists */ |
687 | if (new_map.nr_extents == 0) | |
688 | goto out; | |
689 | ||
690 | ret = -EPERM; | |
691 | /* Validate the user is allowed to use user id's mapped to. */ | |
692 | if (!new_idmap_permitted(ns, cap_setid, &new_map)) | |
693 | goto out; | |
694 | ||
695 | /* Map the lower ids from the parent user namespace to the | |
696 | * kernel global id space. | |
697 | */ | |
698 | for (idx = 0; idx < new_map.nr_extents; idx++) { | |
699 | u32 lower_first; | |
700 | extent = &new_map.extent[idx]; | |
701 | ||
702 | lower_first = map_id_range_down(parent_map, | |
703 | extent->lower_first, | |
704 | extent->count); | |
705 | ||
706 | /* Fail if we can not map the specified extent to | |
707 | * the kernel global id space. | |
708 | */ | |
709 | if (lower_first == (u32) -1) | |
710 | goto out; | |
711 | ||
712 | extent->lower_first = lower_first; | |
713 | } | |
714 | ||
715 | /* Install the map */ | |
716 | memcpy(map->extent, new_map.extent, | |
717 | new_map.nr_extents*sizeof(new_map.extent[0])); | |
718 | smp_wmb(); | |
719 | map->nr_extents = new_map.nr_extents; | |
720 | ||
721 | *ppos = count; | |
722 | ret = count; | |
723 | out: | |
724 | mutex_unlock(&id_map_mutex); | |
725 | if (page) | |
726 | free_page(page); | |
727 | return ret; | |
728 | } | |
729 | ||
730 | ssize_t proc_uid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos) | |
731 | { | |
732 | struct seq_file *seq = file->private_data; | |
733 | struct user_namespace *ns = seq->private; | |
c450f371 | 734 | struct user_namespace *seq_ns = seq_user_ns(seq); |
22d917d8 EB |
735 | |
736 | if (!ns->parent) | |
737 | return -EPERM; | |
738 | ||
c450f371 EB |
739 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
740 | return -EPERM; | |
741 | ||
22d917d8 EB |
742 | return map_write(file, buf, size, ppos, CAP_SETUID, |
743 | &ns->uid_map, &ns->parent->uid_map); | |
744 | } | |
745 | ||
746 | ssize_t proc_gid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos) | |
747 | { | |
748 | struct seq_file *seq = file->private_data; | |
749 | struct user_namespace *ns = seq->private; | |
c450f371 | 750 | struct user_namespace *seq_ns = seq_user_ns(seq); |
22d917d8 EB |
751 | |
752 | if (!ns->parent) | |
753 | return -EPERM; | |
754 | ||
c450f371 EB |
755 | if ((seq_ns != ns) && (seq_ns != ns->parent)) |
756 | return -EPERM; | |
757 | ||
22d917d8 EB |
758 | return map_write(file, buf, size, ppos, CAP_SETGID, |
759 | &ns->gid_map, &ns->parent->gid_map); | |
760 | } | |
761 | ||
f76d207a EB |
762 | ssize_t proc_projid_map_write(struct file *file, const char __user *buf, size_t size, loff_t *ppos) |
763 | { | |
764 | struct seq_file *seq = file->private_data; | |
765 | struct user_namespace *ns = seq->private; | |
766 | struct user_namespace *seq_ns = seq_user_ns(seq); | |
767 | ||
768 | if (!ns->parent) | |
769 | return -EPERM; | |
770 | ||
771 | if ((seq_ns != ns) && (seq_ns != ns->parent)) | |
772 | return -EPERM; | |
773 | ||
774 | /* Anyone can set any valid project id no capability needed */ | |
775 | return map_write(file, buf, size, ppos, -1, | |
776 | &ns->projid_map, &ns->parent->projid_map); | |
777 | } | |
778 | ||
22d917d8 EB |
779 | static bool new_idmap_permitted(struct user_namespace *ns, int cap_setid, |
780 | struct uid_gid_map *new_map) | |
781 | { | |
37657da3 EB |
782 | /* Allow mapping to your own filesystem ids */ |
783 | if ((new_map->nr_extents == 1) && (new_map->extent[0].count == 1)) { | |
784 | u32 id = new_map->extent[0].lower_first; | |
785 | if (cap_setid == CAP_SETUID) { | |
786 | kuid_t uid = make_kuid(ns->parent, id); | |
787 | if (uid_eq(uid, current_fsuid())) | |
788 | return true; | |
789 | } | |
790 | else if (cap_setid == CAP_SETGID) { | |
791 | kgid_t gid = make_kgid(ns->parent, id); | |
792 | if (gid_eq(gid, current_fsgid())) | |
793 | return true; | |
794 | } | |
795 | } | |
796 | ||
f76d207a EB |
797 | /* Allow anyone to set a mapping that doesn't require privilege */ |
798 | if (!cap_valid(cap_setid)) | |
799 | return true; | |
800 | ||
22d917d8 EB |
801 | /* Allow the specified ids if we have the appropriate capability |
802 | * (CAP_SETUID or CAP_SETGID) over the parent user namespace. | |
803 | */ | |
804 | if (ns_capable(ns->parent, cap_setid)) | |
805 | return true; | |
5c1469de | 806 | |
22d917d8 | 807 | return false; |
5c1469de | 808 | } |
6164281a | 809 | |
cde1975b EB |
810 | static void *userns_get(struct task_struct *task) |
811 | { | |
812 | struct user_namespace *user_ns; | |
813 | ||
814 | rcu_read_lock(); | |
815 | user_ns = get_user_ns(__task_cred(task)->user_ns); | |
816 | rcu_read_unlock(); | |
817 | ||
818 | return user_ns; | |
819 | } | |
820 | ||
821 | static void userns_put(void *ns) | |
822 | { | |
823 | put_user_ns(ns); | |
824 | } | |
825 | ||
826 | static int userns_install(struct nsproxy *nsproxy, void *ns) | |
827 | { | |
828 | struct user_namespace *user_ns = ns; | |
829 | struct cred *cred; | |
830 | ||
831 | /* Don't allow gaining capabilities by reentering | |
832 | * the same user namespace. | |
833 | */ | |
834 | if (user_ns == current_user_ns()) | |
835 | return -EINVAL; | |
836 | ||
5155040e | 837 | /* Threaded processes may not enter a different user namespace */ |
cde1975b EB |
838 | if (atomic_read(¤t->mm->mm_users) > 1) |
839 | return -EINVAL; | |
840 | ||
e66eded8 EB |
841 | if (current->fs->users != 1) |
842 | return -EINVAL; | |
843 | ||
cde1975b EB |
844 | if (!ns_capable(user_ns, CAP_SYS_ADMIN)) |
845 | return -EPERM; | |
846 | ||
847 | cred = prepare_creds(); | |
848 | if (!cred) | |
849 | return -ENOMEM; | |
850 | ||
851 | put_user_ns(cred->user_ns); | |
852 | set_cred_user_ns(cred, get_user_ns(user_ns)); | |
853 | ||
854 | return commit_creds(cred); | |
855 | } | |
856 | ||
98f842e6 EB |
857 | static unsigned int userns_inum(void *ns) |
858 | { | |
859 | struct user_namespace *user_ns = ns; | |
860 | return user_ns->proc_inum; | |
861 | } | |
862 | ||
cde1975b EB |
863 | const struct proc_ns_operations userns_operations = { |
864 | .name = "user", | |
865 | .type = CLONE_NEWUSER, | |
866 | .get = userns_get, | |
867 | .put = userns_put, | |
868 | .install = userns_install, | |
98f842e6 | 869 | .inum = userns_inum, |
cde1975b EB |
870 | }; |
871 | ||
6164281a PE |
872 | static __init int user_namespaces_init(void) |
873 | { | |
874 | user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC); | |
875 | return 0; | |
876 | } | |
877 | module_init(user_namespaces_init); |