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