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