1 /* Request a key from userspace
3 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
11 * See Documentation/keys-request-key.txt
14 #include <linux/module.h>
15 #include <linux/sched.h>
16 #include <linux/kmod.h>
17 #include <linux/err.h>
18 #include <linux/keyctl.h>
19 #include <linux/slab.h>
22 #define key_negative_timeout 60 /* default timeout on a negative key's existence */
25 * wait_on_bit() sleep function for uninterruptible waiting
27 static int key_wait_bit(void *flags)
34 * wait_on_bit() sleep function for interruptible waiting
36 static int key_wait_bit_intr(void *flags)
39 return signal_pending(current) ? -ERESTARTSYS : 0;
43 * call to complete the construction of a key
45 void complete_request_key(struct key_construction *cons, int error)
47 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
50 key_negate_and_link(cons->key, key_negative_timeout, NULL,
53 key_revoke(cons->authkey);
56 key_put(cons->authkey);
59 EXPORT_SYMBOL(complete_request_key);
62 * request userspace finish the construction of a key
63 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
65 static int call_sbin_request_key(struct key_construction *cons,
69 struct task_struct *tsk = current;
70 key_serial_t prkey, sskey;
71 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
72 char *argv[9], *envp[3], uid_str[12], gid_str[12];
73 char key_str[12], keyring_str[3][12];
77 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
79 ret = install_user_keyrings();
83 /* allocate a new session keyring */
84 sprintf(desc, "_req.%u", key->serial);
86 keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
87 KEY_ALLOC_QUOTA_OVERRUN, NULL);
88 if (IS_ERR(keyring)) {
89 ret = PTR_ERR(keyring);
93 /* attach the auth key to the session keyring */
94 ret = __key_link(keyring, authkey);
98 /* record the UID and GID */
99 sprintf(uid_str, "%d", current_fsuid());
100 sprintf(gid_str, "%d", current_fsgid());
102 /* we say which key is under construction */
103 sprintf(key_str, "%d", key->serial);
105 /* we specify the process's default keyrings */
106 sprintf(keyring_str[0], "%d",
107 tsk->cred->thread_keyring ?
108 tsk->cred->thread_keyring->serial : 0);
111 if (tsk->signal->process_keyring)
112 prkey = tsk->signal->process_keyring->serial;
114 sprintf(keyring_str[1], "%d", prkey);
116 if (tsk->signal->session_keyring) {
118 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
121 sskey = tsk->cred->user->session_keyring->serial;
124 sprintf(keyring_str[2], "%d", sskey);
126 /* set up a minimal environment */
128 envp[i++] = "HOME=/";
129 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
132 /* set up the argument list */
134 argv[i++] = "/sbin/request-key";
135 argv[i++] = (char *) op;
139 argv[i++] = keyring_str[0];
140 argv[i++] = keyring_str[1];
141 argv[i++] = keyring_str[2];
145 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
147 kdebug("usermode -> 0x%x", ret);
149 /* ret is the exit/wait code */
150 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
151 key_validate(key) < 0)
154 /* ignore any errors from userspace if the key was
163 kleave(" = %d", ret);
164 complete_request_key(cons, ret);
169 * call out to userspace for key construction
170 * - we ignore program failure and go on key status instead
172 static int construct_key(struct key *key, const void *callout_info,
173 size_t callout_len, void *aux,
174 struct key *dest_keyring)
176 struct key_construction *cons;
177 request_key_actor_t actor;
181 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
183 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
187 /* allocate an authorisation key */
188 authkey = request_key_auth_new(key, callout_info, callout_len,
190 if (IS_ERR(authkey)) {
192 ret = PTR_ERR(authkey);
195 cons->authkey = key_get(authkey);
196 cons->key = key_get(key);
199 actor = call_sbin_request_key;
200 if (key->type->request_key)
201 actor = key->type->request_key;
203 ret = actor(cons, "create", aux);
205 /* check that the actor called complete_request_key() prior to
206 * returning an error */
208 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
212 kleave(" = %d", ret);
217 * get the appropriate destination keyring for the request
218 * - we return whatever keyring we select with an extra reference upon it which
219 * the caller must release
221 static void construct_get_dest_keyring(struct key **_dest_keyring)
223 struct request_key_auth *rka;
224 struct task_struct *tsk = current;
225 struct key *dest_keyring = *_dest_keyring, *authkey;
227 kenter("%p", dest_keyring);
229 /* find the appropriate keyring */
231 /* the caller supplied one */
232 key_get(dest_keyring);
234 /* use a default keyring; falling through the cases until we
235 * find one that we actually have */
236 switch (tsk->cred->jit_keyring) {
237 case KEY_REQKEY_DEFL_DEFAULT:
238 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
239 if (tsk->cred->request_key_auth) {
240 authkey = tsk->cred->request_key_auth;
241 down_read(&authkey->sem);
242 rka = authkey->payload.data;
243 if (!test_bit(KEY_FLAG_REVOKED,
246 key_get(rka->dest_keyring);
247 up_read(&authkey->sem);
252 case KEY_REQKEY_DEFL_THREAD_KEYRING:
253 dest_keyring = key_get(tsk->cred->thread_keyring);
257 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
258 dest_keyring = key_get(tsk->signal->process_keyring);
262 case KEY_REQKEY_DEFL_SESSION_KEYRING:
264 dest_keyring = key_get(
265 rcu_dereference(tsk->signal->session_keyring));
271 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
273 key_get(tsk->cred->user->session_keyring);
276 case KEY_REQKEY_DEFL_USER_KEYRING:
277 dest_keyring = key_get(tsk->cred->user->uid_keyring);
280 case KEY_REQKEY_DEFL_GROUP_KEYRING:
286 *_dest_keyring = dest_keyring;
287 kleave(" [dk %d]", key_serial(dest_keyring));
292 * allocate a new key in under-construction state and attempt to link it in to
293 * the requested place
294 * - may return a key that's already under construction instead
296 static int construct_alloc_key(struct key_type *type,
297 const char *description,
298 struct key *dest_keyring,
300 struct key_user *user,
306 kenter("%s,%s,,,", type->name, description);
308 mutex_lock(&user->cons_lock);
310 key = key_alloc(type, description,
311 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
316 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
318 down_write(&dest_keyring->sem);
320 /* attach the key to the destination keyring under lock, but we do need
321 * to do another check just in case someone beat us to it whilst we
322 * waited for locks */
323 mutex_lock(&key_construction_mutex);
325 key_ref = search_process_keyrings(type, description, type->match,
327 if (!IS_ERR(key_ref))
328 goto key_already_present;
330 __key_link(dest_keyring, key);
332 mutex_unlock(&key_construction_mutex);
333 up_write(&dest_keyring->sem);
334 mutex_unlock(&user->cons_lock);
336 kleave(" = 0 [%d]", key_serial(key));
340 mutex_unlock(&key_construction_mutex);
342 up_write(&dest_keyring->sem);
343 mutex_unlock(&user->cons_lock);
345 *_key = key = key_ref_to_ptr(key_ref);
346 kleave(" = -EINPROGRESS [%d]", key_serial(key));
350 mutex_unlock(&user->cons_lock);
352 kleave(" = %ld", PTR_ERR(key));
357 * commence key construction
359 static struct key *construct_key_and_link(struct key_type *type,
360 const char *description,
361 const char *callout_info,
364 struct key *dest_keyring,
367 struct key_user *user;
371 user = key_user_lookup(current_fsuid());
373 return ERR_PTR(-ENOMEM);
375 construct_get_dest_keyring(&dest_keyring);
377 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
382 ret = construct_key(key, callout_info, callout_len, aux,
385 goto construction_failed;
388 key_put(dest_keyring);
392 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
394 key_put(dest_keyring);
400 * - search the process's keyrings
401 * - check the list of keys being created or updated
402 * - call out to userspace for a key if supplementary info was provided
403 * - cache the key in an appropriate keyring
405 struct key *request_key_and_link(struct key_type *type,
406 const char *description,
407 const void *callout_info,
410 struct key *dest_keyring,
416 kenter("%s,%s,%p,%zu,%p,%p,%lx",
417 type->name, description, callout_info, callout_len, aux,
418 dest_keyring, flags);
420 /* search all the process keyrings for a key */
421 key_ref = search_process_keyrings(type, description, type->match,
424 if (!IS_ERR(key_ref)) {
425 key = key_ref_to_ptr(key_ref);
426 } else if (PTR_ERR(key_ref) != -EAGAIN) {
427 key = ERR_CAST(key_ref);
429 /* the search failed, but the keyrings were searchable, so we
430 * should consult userspace if we can */
431 key = ERR_PTR(-ENOKEY);
435 key = construct_key_and_link(type, description, callout_info,
436 callout_len, aux, dest_keyring,
441 kleave(" = %p", key);
446 * wait for construction of a key to complete
448 int wait_for_key_construction(struct key *key, bool intr)
452 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
453 intr ? key_wait_bit_intr : key_wait_bit,
454 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
457 return key_validate(key);
459 EXPORT_SYMBOL(wait_for_key_construction);
463 * - search the process's keyrings
464 * - check the list of keys being created or updated
465 * - call out to userspace for a key if supplementary info was provided
466 * - waits uninterruptible for creation to complete
468 struct key *request_key(struct key_type *type,
469 const char *description,
470 const char *callout_info)
473 size_t callout_len = 0;
477 callout_len = strlen(callout_info);
478 key = request_key_and_link(type, description, callout_info, callout_len,
479 NULL, NULL, KEY_ALLOC_IN_QUOTA);
481 ret = wait_for_key_construction(key, false);
489 EXPORT_SYMBOL(request_key);
492 * request a key with auxiliary data for the upcaller
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 * - waits uninterruptible for creation to complete
498 struct key *request_key_with_auxdata(struct key_type *type,
499 const char *description,
500 const void *callout_info,
507 key = request_key_and_link(type, description, callout_info, callout_len,
508 aux, NULL, KEY_ALLOC_IN_QUOTA);
510 ret = wait_for_key_construction(key, false);
518 EXPORT_SYMBOL(request_key_with_auxdata);
521 * request a key (allow async construction)
522 * - search the process's keyrings
523 * - check the list of keys being created or updated
524 * - call out to userspace for a key if supplementary info was provided
526 struct key *request_key_async(struct key_type *type,
527 const char *description,
528 const void *callout_info,
531 return request_key_and_link(type, description, callout_info,
532 callout_len, NULL, NULL,
535 EXPORT_SYMBOL(request_key_async);
538 * request a key with auxiliary data for the upcaller (allow async construction)
539 * - search the process's keyrings
540 * - check the list of keys being created or updated
541 * - call out to userspace for a key if supplementary info was provided
543 struct key *request_key_async_with_auxdata(struct key_type *type,
544 const char *description,
545 const void *callout_info,
549 return request_key_and_link(type, description, callout_info,
550 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
552 EXPORT_SYMBOL(request_key_async_with_auxdata);