Commit | Line | Data |
---|---|---|
76181c13 | 1 | /* Request a key from userspace |
1da177e4 | 2 | * |
76181c13 | 3 | * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
f1a9badc DH |
10 | * |
11 | * See Documentation/keys-request-key.txt | |
1da177e4 LT |
12 | */ |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/sched.h> | |
16 | #include <linux/kmod.h> | |
17 | #include <linux/err.h> | |
3e30148c | 18 | #include <linux/keyctl.h> |
fdb89bce | 19 | #include <linux/slab.h> |
1da177e4 LT |
20 | #include "internal.h" |
21 | ||
76181c13 DH |
22 | /* |
23 | * wait_on_bit() sleep function for uninterruptible waiting | |
24 | */ | |
25 | static int key_wait_bit(void *flags) | |
26 | { | |
27 | schedule(); | |
28 | return 0; | |
29 | } | |
30 | ||
31 | /* | |
32 | * wait_on_bit() sleep function for interruptible waiting | |
33 | */ | |
34 | static int key_wait_bit_intr(void *flags) | |
35 | { | |
36 | schedule(); | |
37 | return signal_pending(current) ? -ERESTARTSYS : 0; | |
38 | } | |
39 | ||
40 | /* | |
41 | * call to complete the construction of a key | |
42 | */ | |
43 | void complete_request_key(struct key_construction *cons, int error) | |
44 | { | |
45 | kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error); | |
1da177e4 | 46 | |
76181c13 DH |
47 | if (error < 0) |
48 | key_negate_and_link(cons->key, key_negative_timeout, NULL, | |
49 | cons->authkey); | |
50 | else | |
51 | key_revoke(cons->authkey); | |
52 | ||
53 | key_put(cons->key); | |
54 | key_put(cons->authkey); | |
55 | kfree(cons); | |
56 | } | |
57 | EXPORT_SYMBOL(complete_request_key); | |
1da177e4 | 58 | |
1da177e4 LT |
59 | /* |
60 | * request userspace finish the construction of a key | |
b5f545c8 | 61 | * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>" |
1da177e4 | 62 | */ |
76181c13 | 63 | static int call_sbin_request_key(struct key_construction *cons, |
4e54f085 DH |
64 | const char *op, |
65 | void *aux) | |
1da177e4 LT |
66 | { |
67 | struct task_struct *tsk = current; | |
1da177e4 | 68 | key_serial_t prkey, sskey; |
76181c13 | 69 | struct key *key = cons->key, *authkey = cons->authkey, *keyring; |
b5f545c8 | 70 | char *argv[9], *envp[3], uid_str[12], gid_str[12]; |
1da177e4 | 71 | char key_str[12], keyring_str[3][12]; |
b5f545c8 | 72 | char desc[20]; |
3e30148c DH |
73 | int ret, i; |
74 | ||
b5f545c8 | 75 | kenter("{%d},{%d},%s", key->serial, authkey->serial, op); |
3e30148c | 76 | |
1f8f5cf6 DH |
77 | ret = install_user_keyrings(tsk); |
78 | if (ret < 0) | |
79 | goto error_alloc; | |
80 | ||
b5f545c8 DH |
81 | /* allocate a new session keyring */ |
82 | sprintf(desc, "_req.%u", key->serial); | |
83 | ||
7e047ef5 DH |
84 | keyring = keyring_alloc(desc, current->fsuid, current->fsgid, current, |
85 | KEY_ALLOC_QUOTA_OVERRUN, NULL); | |
b5f545c8 DH |
86 | if (IS_ERR(keyring)) { |
87 | ret = PTR_ERR(keyring); | |
88 | goto error_alloc; | |
3e30148c | 89 | } |
1da177e4 | 90 | |
b5f545c8 DH |
91 | /* attach the auth key to the session keyring */ |
92 | ret = __key_link(keyring, authkey); | |
93 | if (ret < 0) | |
94 | goto error_link; | |
95 | ||
1da177e4 LT |
96 | /* record the UID and GID */ |
97 | sprintf(uid_str, "%d", current->fsuid); | |
98 | sprintf(gid_str, "%d", current->fsgid); | |
99 | ||
100 | /* we say which key is under construction */ | |
101 | sprintf(key_str, "%d", key->serial); | |
102 | ||
103 | /* we specify the process's default keyrings */ | |
104 | sprintf(keyring_str[0], "%d", | |
105 | tsk->thread_keyring ? tsk->thread_keyring->serial : 0); | |
106 | ||
107 | prkey = 0; | |
108 | if (tsk->signal->process_keyring) | |
109 | prkey = tsk->signal->process_keyring->serial; | |
110 | ||
3e30148c | 111 | sprintf(keyring_str[1], "%d", prkey); |
1da177e4 | 112 | |
3e30148c DH |
113 | if (tsk->signal->session_keyring) { |
114 | rcu_read_lock(); | |
115 | sskey = rcu_dereference(tsk->signal->session_keyring)->serial; | |
116 | rcu_read_unlock(); | |
76181c13 | 117 | } else { |
1da177e4 | 118 | sskey = tsk->user->session_keyring->serial; |
3e30148c | 119 | } |
1da177e4 | 120 | |
1da177e4 LT |
121 | sprintf(keyring_str[2], "%d", sskey); |
122 | ||
123 | /* set up a minimal environment */ | |
124 | i = 0; | |
125 | envp[i++] = "HOME=/"; | |
126 | envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin"; | |
127 | envp[i] = NULL; | |
128 | ||
129 | /* set up the argument list */ | |
130 | i = 0; | |
131 | argv[i++] = "/sbin/request-key"; | |
132 | argv[i++] = (char *) op; | |
133 | argv[i++] = key_str; | |
134 | argv[i++] = uid_str; | |
135 | argv[i++] = gid_str; | |
136 | argv[i++] = keyring_str[0]; | |
137 | argv[i++] = keyring_str[1]; | |
138 | argv[i++] = keyring_str[2]; | |
1da177e4 LT |
139 | argv[i] = NULL; |
140 | ||
141 | /* do it */ | |
86313c48 JF |
142 | ret = call_usermodehelper_keys(argv[0], argv, envp, keyring, |
143 | UMH_WAIT_PROC); | |
76181c13 DH |
144 | kdebug("usermode -> 0x%x", ret); |
145 | if (ret >= 0) { | |
146 | /* ret is the exit/wait code */ | |
147 | if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) || | |
148 | key_validate(key) < 0) | |
149 | ret = -ENOKEY; | |
150 | else | |
151 | /* ignore any errors from userspace if the key was | |
152 | * instantiated */ | |
153 | ret = 0; | |
154 | } | |
3e30148c | 155 | |
b5f545c8 DH |
156 | error_link: |
157 | key_put(keyring); | |
3e30148c | 158 | |
b5f545c8 | 159 | error_alloc: |
3e30148c | 160 | kleave(" = %d", ret); |
76181c13 | 161 | complete_request_key(cons, ret); |
3e30148c | 162 | return ret; |
76181c13 | 163 | } |
1da177e4 | 164 | |
1da177e4 | 165 | /* |
76181c13 | 166 | * call out to userspace for key construction |
1da177e4 LT |
167 | * - we ignore program failure and go on key status instead |
168 | */ | |
4a38e122 DH |
169 | static int construct_key(struct key *key, const void *callout_info, |
170 | size_t callout_len, void *aux) | |
1da177e4 | 171 | { |
76181c13 | 172 | struct key_construction *cons; |
b5f545c8 | 173 | request_key_actor_t actor; |
76181c13 DH |
174 | struct key *authkey; |
175 | int ret; | |
1da177e4 | 176 | |
4a38e122 | 177 | kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux); |
3e30148c | 178 | |
76181c13 DH |
179 | cons = kmalloc(sizeof(*cons), GFP_KERNEL); |
180 | if (!cons) | |
181 | return -ENOMEM; | |
1da177e4 | 182 | |
b5f545c8 | 183 | /* allocate an authorisation key */ |
4a38e122 | 184 | authkey = request_key_auth_new(key, callout_info, callout_len); |
b5f545c8 | 185 | if (IS_ERR(authkey)) { |
76181c13 | 186 | kfree(cons); |
b5f545c8 DH |
187 | ret = PTR_ERR(authkey); |
188 | authkey = NULL; | |
76181c13 DH |
189 | } else { |
190 | cons->authkey = key_get(authkey); | |
191 | cons->key = key_get(key); | |
192 | ||
193 | /* make the call */ | |
194 | actor = call_sbin_request_key; | |
195 | if (key->type->request_key) | |
196 | actor = key->type->request_key; | |
197 | ||
198 | ret = actor(cons, "create", aux); | |
199 | ||
200 | /* check that the actor called complete_request_key() prior to | |
201 | * returning an error */ | |
202 | WARN_ON(ret < 0 && | |
203 | !test_bit(KEY_FLAG_REVOKED, &authkey->flags)); | |
204 | key_put(authkey); | |
1da177e4 LT |
205 | } |
206 | ||
76181c13 DH |
207 | kleave(" = %d", ret); |
208 | return ret; | |
209 | } | |
1da177e4 | 210 | |
3e30148c | 211 | /* |
76181c13 DH |
212 | * link a key to the appropriate destination keyring |
213 | * - the caller must hold a write lock on the destination keyring | |
3e30148c | 214 | */ |
76181c13 | 215 | static void construct_key_make_link(struct key *key, struct key *dest_keyring) |
3e30148c DH |
216 | { |
217 | struct task_struct *tsk = current; | |
218 | struct key *drop = NULL; | |
219 | ||
220 | kenter("{%d},%p", key->serial, dest_keyring); | |
221 | ||
222 | /* find the appropriate keyring */ | |
223 | if (!dest_keyring) { | |
224 | switch (tsk->jit_keyring) { | |
225 | case KEY_REQKEY_DEFL_DEFAULT: | |
226 | case KEY_REQKEY_DEFL_THREAD_KEYRING: | |
227 | dest_keyring = tsk->thread_keyring; | |
228 | if (dest_keyring) | |
229 | break; | |
230 | ||
231 | case KEY_REQKEY_DEFL_PROCESS_KEYRING: | |
232 | dest_keyring = tsk->signal->process_keyring; | |
233 | if (dest_keyring) | |
234 | break; | |
235 | ||
236 | case KEY_REQKEY_DEFL_SESSION_KEYRING: | |
237 | rcu_read_lock(); | |
238 | dest_keyring = key_get( | |
239 | rcu_dereference(tsk->signal->session_keyring)); | |
240 | rcu_read_unlock(); | |
241 | drop = dest_keyring; | |
242 | ||
243 | if (dest_keyring) | |
244 | break; | |
245 | ||
246 | case KEY_REQKEY_DEFL_USER_SESSION_KEYRING: | |
76181c13 | 247 | dest_keyring = tsk->user->session_keyring; |
3e30148c DH |
248 | break; |
249 | ||
250 | case KEY_REQKEY_DEFL_USER_KEYRING: | |
76181c13 | 251 | dest_keyring = tsk->user->uid_keyring; |
3e30148c DH |
252 | break; |
253 | ||
254 | case KEY_REQKEY_DEFL_GROUP_KEYRING: | |
255 | default: | |
256 | BUG(); | |
257 | } | |
258 | } | |
259 | ||
260 | /* and attach the key to it */ | |
76181c13 | 261 | __key_link(dest_keyring, key); |
3e30148c | 262 | key_put(drop); |
3e30148c | 263 | kleave(""); |
76181c13 | 264 | } |
3e30148c | 265 | |
76181c13 DH |
266 | /* |
267 | * allocate a new key in under-construction state and attempt to link it in to | |
268 | * the requested place | |
269 | * - may return a key that's already under construction instead | |
270 | */ | |
271 | static int construct_alloc_key(struct key_type *type, | |
272 | const char *description, | |
273 | struct key *dest_keyring, | |
274 | unsigned long flags, | |
275 | struct key_user *user, | |
276 | struct key **_key) | |
277 | { | |
278 | struct key *key; | |
279 | key_ref_t key_ref; | |
280 | ||
281 | kenter("%s,%s,,,", type->name, description); | |
282 | ||
283 | mutex_lock(&user->cons_lock); | |
284 | ||
285 | key = key_alloc(type, description, | |
286 | current->fsuid, current->fsgid, current, KEY_POS_ALL, | |
287 | flags); | |
288 | if (IS_ERR(key)) | |
289 | goto alloc_failed; | |
290 | ||
291 | set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags); | |
292 | ||
293 | if (dest_keyring) | |
294 | down_write(&dest_keyring->sem); | |
295 | ||
296 | /* attach the key to the destination keyring under lock, but we do need | |
297 | * to do another check just in case someone beat us to it whilst we | |
298 | * waited for locks */ | |
299 | mutex_lock(&key_construction_mutex); | |
300 | ||
301 | key_ref = search_process_keyrings(type, description, type->match, | |
302 | current); | |
303 | if (!IS_ERR(key_ref)) | |
304 | goto key_already_present; | |
305 | ||
306 | if (dest_keyring) | |
307 | construct_key_make_link(key, dest_keyring); | |
308 | ||
309 | mutex_unlock(&key_construction_mutex); | |
310 | if (dest_keyring) | |
311 | up_write(&dest_keyring->sem); | |
312 | mutex_unlock(&user->cons_lock); | |
313 | *_key = key; | |
314 | kleave(" = 0 [%d]", key_serial(key)); | |
315 | return 0; | |
316 | ||
317 | key_already_present: | |
318 | mutex_unlock(&key_construction_mutex); | |
319 | if (dest_keyring) | |
320 | up_write(&dest_keyring->sem); | |
321 | mutex_unlock(&user->cons_lock); | |
322 | key_put(key); | |
323 | *_key = key = key_ref_to_ptr(key_ref); | |
324 | kleave(" = -EINPROGRESS [%d]", key_serial(key)); | |
325 | return -EINPROGRESS; | |
326 | ||
327 | alloc_failed: | |
328 | mutex_unlock(&user->cons_lock); | |
329 | *_key = NULL; | |
330 | kleave(" = %ld", PTR_ERR(key)); | |
331 | return PTR_ERR(key); | |
332 | } | |
333 | ||
334 | /* | |
335 | * commence key construction | |
336 | */ | |
337 | static struct key *construct_key_and_link(struct key_type *type, | |
338 | const char *description, | |
339 | const char *callout_info, | |
4a38e122 | 340 | size_t callout_len, |
76181c13 DH |
341 | void *aux, |
342 | struct key *dest_keyring, | |
343 | unsigned long flags) | |
344 | { | |
345 | struct key_user *user; | |
346 | struct key *key; | |
347 | int ret; | |
348 | ||
349 | user = key_user_lookup(current->fsuid); | |
350 | if (!user) | |
351 | return ERR_PTR(-ENOMEM); | |
352 | ||
353 | ret = construct_alloc_key(type, description, dest_keyring, flags, user, | |
354 | &key); | |
355 | key_user_put(user); | |
356 | ||
357 | if (ret == 0) { | |
4a38e122 | 358 | ret = construct_key(key, callout_info, callout_len, aux); |
76181c13 DH |
359 | if (ret < 0) |
360 | goto construction_failed; | |
361 | } | |
362 | ||
363 | return key; | |
364 | ||
365 | construction_failed: | |
366 | key_negate_and_link(key, key_negative_timeout, NULL, NULL); | |
367 | key_put(key); | |
368 | return ERR_PTR(ret); | |
369 | } | |
3e30148c | 370 | |
1da177e4 LT |
371 | /* |
372 | * request a key | |
373 | * - search the process's keyrings | |
374 | * - check the list of keys being created or updated | |
3e30148c DH |
375 | * - call out to userspace for a key if supplementary info was provided |
376 | * - cache the key in an appropriate keyring | |
1da177e4 | 377 | */ |
3e30148c DH |
378 | struct key *request_key_and_link(struct key_type *type, |
379 | const char *description, | |
4a38e122 DH |
380 | const void *callout_info, |
381 | size_t callout_len, | |
4e54f085 | 382 | void *aux, |
7e047ef5 DH |
383 | struct key *dest_keyring, |
384 | unsigned long flags) | |
1da177e4 | 385 | { |
1da177e4 | 386 | struct key *key; |
664cceb0 | 387 | key_ref_t key_ref; |
1da177e4 | 388 | |
4a38e122 DH |
389 | kenter("%s,%s,%p,%zu,%p,%p,%lx", |
390 | type->name, description, callout_info, callout_len, aux, | |
4e54f085 | 391 | dest_keyring, flags); |
3e30148c | 392 | |
1da177e4 | 393 | /* search all the process keyrings for a key */ |
664cceb0 DH |
394 | key_ref = search_process_keyrings(type, description, type->match, |
395 | current); | |
1da177e4 | 396 | |
664cceb0 DH |
397 | if (!IS_ERR(key_ref)) { |
398 | key = key_ref_to_ptr(key_ref); | |
76181c13 | 399 | } else if (PTR_ERR(key_ref) != -EAGAIN) { |
e231c2ee | 400 | key = ERR_CAST(key_ref); |
76181c13 | 401 | } else { |
1da177e4 LT |
402 | /* the search failed, but the keyrings were searchable, so we |
403 | * should consult userspace if we can */ | |
404 | key = ERR_PTR(-ENOKEY); | |
405 | if (!callout_info) | |
406 | goto error; | |
407 | ||
76181c13 | 408 | key = construct_key_and_link(type, description, callout_info, |
4a38e122 DH |
409 | callout_len, aux, dest_keyring, |
410 | flags); | |
1da177e4 LT |
411 | } |
412 | ||
3e30148c DH |
413 | error: |
414 | kleave(" = %p", key); | |
1da177e4 | 415 | return key; |
76181c13 | 416 | } |
1da177e4 | 417 | |
76181c13 DH |
418 | /* |
419 | * wait for construction of a key to complete | |
420 | */ | |
421 | int wait_for_key_construction(struct key *key, bool intr) | |
422 | { | |
423 | int ret; | |
3e30148c | 424 | |
76181c13 DH |
425 | ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT, |
426 | intr ? key_wait_bit_intr : key_wait_bit, | |
427 | intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE); | |
428 | if (ret < 0) | |
429 | return ret; | |
430 | return key_validate(key); | |
431 | } | |
432 | EXPORT_SYMBOL(wait_for_key_construction); | |
3e30148c | 433 | |
3e30148c DH |
434 | /* |
435 | * request a key | |
436 | * - search the process's keyrings | |
437 | * - check the list of keys being created or updated | |
438 | * - call out to userspace for a key if supplementary info was provided | |
76181c13 | 439 | * - waits uninterruptible for creation to complete |
3e30148c DH |
440 | */ |
441 | struct key *request_key(struct key_type *type, | |
442 | const char *description, | |
443 | const char *callout_info) | |
444 | { | |
76181c13 | 445 | struct key *key; |
4a38e122 | 446 | size_t callout_len = 0; |
76181c13 DH |
447 | int ret; |
448 | ||
4a38e122 DH |
449 | if (callout_info) |
450 | callout_len = strlen(callout_info); | |
451 | key = request_key_and_link(type, description, callout_info, callout_len, | |
452 | NULL, NULL, KEY_ALLOC_IN_QUOTA); | |
76181c13 DH |
453 | if (!IS_ERR(key)) { |
454 | ret = wait_for_key_construction(key, false); | |
455 | if (ret < 0) { | |
456 | key_put(key); | |
457 | return ERR_PTR(ret); | |
458 | } | |
459 | } | |
460 | return key; | |
461 | } | |
1da177e4 | 462 | EXPORT_SYMBOL(request_key); |
4e54f085 | 463 | |
4e54f085 DH |
464 | /* |
465 | * request a key with auxiliary data for the upcaller | |
466 | * - search the process's keyrings | |
467 | * - check the list of keys being created or updated | |
468 | * - call out to userspace for a key if supplementary info was provided | |
76181c13 | 469 | * - waits uninterruptible for creation to complete |
4e54f085 DH |
470 | */ |
471 | struct key *request_key_with_auxdata(struct key_type *type, | |
472 | const char *description, | |
4a38e122 DH |
473 | const void *callout_info, |
474 | size_t callout_len, | |
4e54f085 DH |
475 | void *aux) |
476 | { | |
76181c13 DH |
477 | struct key *key; |
478 | int ret; | |
479 | ||
4a38e122 DH |
480 | key = request_key_and_link(type, description, callout_info, callout_len, |
481 | aux, NULL, KEY_ALLOC_IN_QUOTA); | |
76181c13 DH |
482 | if (!IS_ERR(key)) { |
483 | ret = wait_for_key_construction(key, false); | |
484 | if (ret < 0) { | |
485 | key_put(key); | |
486 | return ERR_PTR(ret); | |
487 | } | |
488 | } | |
489 | return key; | |
490 | } | |
491 | EXPORT_SYMBOL(request_key_with_auxdata); | |
4e54f085 | 492 | |
76181c13 DH |
493 | /* |
494 | * request a key (allow async construction) | |
495 | * - search the process's keyrings | |
496 | * - check the list of keys being created or updated | |
497 | * - call out to userspace for a key if supplementary info was provided | |
498 | */ | |
499 | struct key *request_key_async(struct key_type *type, | |
500 | const char *description, | |
4a38e122 DH |
501 | const void *callout_info, |
502 | size_t callout_len) | |
76181c13 | 503 | { |
4a38e122 DH |
504 | return request_key_and_link(type, description, callout_info, |
505 | callout_len, NULL, NULL, | |
506 | KEY_ALLOC_IN_QUOTA); | |
76181c13 DH |
507 | } |
508 | EXPORT_SYMBOL(request_key_async); | |
4e54f085 | 509 | |
76181c13 DH |
510 | /* |
511 | * request a key with auxiliary data for the upcaller (allow async construction) | |
512 | * - search the process's keyrings | |
513 | * - check the list of keys being created or updated | |
514 | * - call out to userspace for a key if supplementary info was provided | |
515 | */ | |
516 | struct key *request_key_async_with_auxdata(struct key_type *type, | |
517 | const char *description, | |
4a38e122 DH |
518 | const void *callout_info, |
519 | size_t callout_len, | |
76181c13 DH |
520 | void *aux) |
521 | { | |
4a38e122 DH |
522 | return request_key_and_link(type, description, callout_info, |
523 | callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA); | |
76181c13 DH |
524 | } |
525 | EXPORT_SYMBOL(request_key_async_with_auxdata); |