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