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 const struct cred *cred = current_cred();
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", cred->fsuid);
100 sprintf(gid_str, "%d", cred->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 cred->thread_keyring ?
108 cred->thread_keyring->serial : 0);
111 if (cred->tgcred->process_keyring)
112 prkey = cred->tgcred->process_keyring->serial;
114 if (cred->tgcred->session_keyring)
115 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
117 sskey = cred->user->session_keyring->serial;
119 sprintf(keyring_str[2], "%d", sskey);
121 /* set up a minimal environment */
123 envp[i++] = "HOME=/";
124 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
127 /* set up the argument list */
129 argv[i++] = "/sbin/request-key";
130 argv[i++] = (char *) op;
134 argv[i++] = keyring_str[0];
135 argv[i++] = keyring_str[1];
136 argv[i++] = keyring_str[2];
140 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
142 kdebug("usermode -> 0x%x", ret);
144 /* ret is the exit/wait code */
145 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
146 key_validate(key) < 0)
149 /* ignore any errors from userspace if the key was
158 kleave(" = %d", ret);
159 complete_request_key(cons, ret);
164 * call out to userspace for key construction
165 * - we ignore program failure and go on key status instead
167 static int construct_key(struct key *key, const void *callout_info,
168 size_t callout_len, void *aux,
169 struct key *dest_keyring)
171 struct key_construction *cons;
172 request_key_actor_t actor;
176 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
178 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
182 /* allocate an authorisation key */
183 authkey = request_key_auth_new(key, callout_info, callout_len,
185 if (IS_ERR(authkey)) {
187 ret = PTR_ERR(authkey);
190 cons->authkey = key_get(authkey);
191 cons->key = key_get(key);
194 actor = call_sbin_request_key;
195 if (key->type->request_key)
196 actor = key->type->request_key;
198 ret = actor(cons, "create", aux);
200 /* check that the actor called complete_request_key() prior to
201 * returning an error */
203 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
207 kleave(" = %d", ret);
212 * get the appropriate destination keyring for the request
213 * - we return whatever keyring we select with an extra reference upon it which
214 * the caller must release
216 static void construct_get_dest_keyring(struct key **_dest_keyring)
218 struct request_key_auth *rka;
219 const struct cred *cred = current_cred();
220 struct key *dest_keyring = *_dest_keyring, *authkey;
222 kenter("%p", dest_keyring);
224 /* find the appropriate keyring */
226 /* the caller supplied one */
227 key_get(dest_keyring);
229 /* use a default keyring; falling through the cases until we
230 * find one that we actually have */
231 switch (cred->jit_keyring) {
232 case KEY_REQKEY_DEFL_DEFAULT:
233 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
234 if (cred->request_key_auth) {
235 authkey = cred->request_key_auth;
236 down_read(&authkey->sem);
237 rka = authkey->payload.data;
238 if (!test_bit(KEY_FLAG_REVOKED,
241 key_get(rka->dest_keyring);
242 up_read(&authkey->sem);
247 case KEY_REQKEY_DEFL_THREAD_KEYRING:
248 dest_keyring = key_get(cred->thread_keyring);
252 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
253 dest_keyring = key_get(cred->tgcred->process_keyring);
257 case KEY_REQKEY_DEFL_SESSION_KEYRING:
259 dest_keyring = key_get(
260 rcu_dereference(cred->tgcred->session_keyring));
266 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
268 key_get(cred->user->session_keyring);
271 case KEY_REQKEY_DEFL_USER_KEYRING:
272 dest_keyring = key_get(cred->user->uid_keyring);
275 case KEY_REQKEY_DEFL_GROUP_KEYRING:
281 *_dest_keyring = dest_keyring;
282 kleave(" [dk %d]", key_serial(dest_keyring));
287 * allocate a new key in under-construction state and attempt to link it in to
288 * the requested place
289 * - may return a key that's already under construction instead
291 static int construct_alloc_key(struct key_type *type,
292 const char *description,
293 struct key *dest_keyring,
295 struct key_user *user,
301 kenter("%s,%s,,,", type->name, description);
303 mutex_lock(&user->cons_lock);
305 key = key_alloc(type, description,
306 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
311 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
313 down_write(&dest_keyring->sem);
315 /* attach the key to the destination keyring under lock, but we do need
316 * to do another check just in case someone beat us to it whilst we
317 * waited for locks */
318 mutex_lock(&key_construction_mutex);
320 key_ref = search_process_keyrings(type, description, type->match,
322 if (!IS_ERR(key_ref))
323 goto key_already_present;
325 __key_link(dest_keyring, key);
327 mutex_unlock(&key_construction_mutex);
328 up_write(&dest_keyring->sem);
329 mutex_unlock(&user->cons_lock);
331 kleave(" = 0 [%d]", key_serial(key));
335 mutex_unlock(&key_construction_mutex);
337 up_write(&dest_keyring->sem);
338 mutex_unlock(&user->cons_lock);
340 *_key = key = key_ref_to_ptr(key_ref);
341 kleave(" = -EINPROGRESS [%d]", key_serial(key));
345 mutex_unlock(&user->cons_lock);
347 kleave(" = %ld", PTR_ERR(key));
352 * commence key construction
354 static struct key *construct_key_and_link(struct key_type *type,
355 const char *description,
356 const char *callout_info,
359 struct key *dest_keyring,
362 struct key_user *user;
366 user = key_user_lookup(current_fsuid());
368 return ERR_PTR(-ENOMEM);
370 construct_get_dest_keyring(&dest_keyring);
372 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
377 ret = construct_key(key, callout_info, callout_len, aux,
380 goto construction_failed;
383 key_put(dest_keyring);
387 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
389 key_put(dest_keyring);
395 * - search the process's keyrings
396 * - check the list of keys being created or updated
397 * - call out to userspace for a key if supplementary info was provided
398 * - cache the key in an appropriate keyring
400 struct key *request_key_and_link(struct key_type *type,
401 const char *description,
402 const void *callout_info,
405 struct key *dest_keyring,
411 kenter("%s,%s,%p,%zu,%p,%p,%lx",
412 type->name, description, callout_info, callout_len, aux,
413 dest_keyring, flags);
415 /* search all the process keyrings for a key */
416 key_ref = search_process_keyrings(type, description, type->match,
419 if (!IS_ERR(key_ref)) {
420 key = key_ref_to_ptr(key_ref);
421 } else if (PTR_ERR(key_ref) != -EAGAIN) {
422 key = ERR_CAST(key_ref);
424 /* the search failed, but the keyrings were searchable, so we
425 * should consult userspace if we can */
426 key = ERR_PTR(-ENOKEY);
430 key = construct_key_and_link(type, description, callout_info,
431 callout_len, aux, dest_keyring,
436 kleave(" = %p", key);
441 * wait for construction of a key to complete
443 int wait_for_key_construction(struct key *key, bool intr)
447 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
448 intr ? key_wait_bit_intr : key_wait_bit,
449 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
452 return key_validate(key);
454 EXPORT_SYMBOL(wait_for_key_construction);
458 * - search the process's keyrings
459 * - check the list of keys being created or updated
460 * - call out to userspace for a key if supplementary info was provided
461 * - waits uninterruptible for creation to complete
463 struct key *request_key(struct key_type *type,
464 const char *description,
465 const char *callout_info)
468 size_t callout_len = 0;
472 callout_len = strlen(callout_info);
473 key = request_key_and_link(type, description, callout_info, callout_len,
474 NULL, NULL, KEY_ALLOC_IN_QUOTA);
476 ret = wait_for_key_construction(key, false);
484 EXPORT_SYMBOL(request_key);
487 * request a key with auxiliary data for the upcaller
488 * - search the process's keyrings
489 * - check the list of keys being created or updated
490 * - call out to userspace for a key if supplementary info was provided
491 * - waits uninterruptible for creation to complete
493 struct key *request_key_with_auxdata(struct key_type *type,
494 const char *description,
495 const void *callout_info,
502 key = request_key_and_link(type, description, callout_info, callout_len,
503 aux, NULL, KEY_ALLOC_IN_QUOTA);
505 ret = wait_for_key_construction(key, false);
513 EXPORT_SYMBOL(request_key_with_auxdata);
516 * request a key (allow async construction)
517 * - search the process's keyrings
518 * - check the list of keys being created or updated
519 * - call out to userspace for a key if supplementary info was provided
521 struct key *request_key_async(struct key_type *type,
522 const char *description,
523 const void *callout_info,
526 return request_key_and_link(type, description, callout_info,
527 callout_len, NULL, NULL,
530 EXPORT_SYMBOL(request_key_async);
533 * request a key with auxiliary data for the upcaller (allow async construction)
534 * - search the process's keyrings
535 * - check the list of keys being created or updated
536 * - call out to userspace for a key if supplementary info was provided
538 struct key *request_key_async_with_auxdata(struct key_type *type,
539 const char *description,
540 const void *callout_info,
544 return request_key_and_link(type, description, callout_info,
545 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
547 EXPORT_SYMBOL(request_key_async_with_auxdata);