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