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 cred = get_current_cred();
87 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
88 KEY_ALLOC_QUOTA_OVERRUN, NULL);
90 if (IS_ERR(keyring)) {
91 ret = PTR_ERR(keyring);
95 /* attach the auth key to the session keyring */
96 ret = __key_link(keyring, authkey);
100 /* record the UID and GID */
101 sprintf(uid_str, "%d", cred->fsuid);
102 sprintf(gid_str, "%d", cred->fsgid);
104 /* we say which key is under construction */
105 sprintf(key_str, "%d", key->serial);
107 /* we specify the process's default keyrings */
108 sprintf(keyring_str[0], "%d",
109 cred->thread_keyring ? cred->thread_keyring->serial : 0);
112 if (cred->tgcred->process_keyring)
113 prkey = cred->tgcred->process_keyring->serial;
115 if (cred->tgcred->session_keyring)
116 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
118 sskey = cred->user->session_keyring->serial;
120 sprintf(keyring_str[2], "%d", sskey);
122 /* set up a minimal environment */
124 envp[i++] = "HOME=/";
125 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
128 /* set up the argument list */
130 argv[i++] = "/sbin/request-key";
131 argv[i++] = (char *) op;
135 argv[i++] = keyring_str[0];
136 argv[i++] = keyring_str[1];
137 argv[i++] = keyring_str[2];
141 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
143 kdebug("usermode -> 0x%x", ret);
145 /* ret is the exit/wait code */
146 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
147 key_validate(key) < 0)
150 /* ignore any errors from userspace if the key was
159 complete_request_key(cons, ret);
160 kleave(" = %d", ret);
165 * call out to userspace for key construction
166 * - we ignore program failure and go on key status instead
168 static int construct_key(struct key *key, const void *callout_info,
169 size_t callout_len, void *aux,
170 struct key *dest_keyring)
172 struct key_construction *cons;
173 request_key_actor_t actor;
177 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
179 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
183 /* allocate an authorisation key */
184 authkey = request_key_auth_new(key, callout_info, callout_len,
186 if (IS_ERR(authkey)) {
188 ret = PTR_ERR(authkey);
191 cons->authkey = key_get(authkey);
192 cons->key = key_get(key);
195 actor = call_sbin_request_key;
196 if (key->type->request_key)
197 actor = key->type->request_key;
199 ret = actor(cons, "create", aux);
201 /* check that the actor called complete_request_key() prior to
202 * returning an error */
204 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
208 kleave(" = %d", ret);
213 * get the appropriate destination keyring for the request
214 * - we return whatever keyring we select with an extra reference upon it which
215 * the caller must release
217 static void construct_get_dest_keyring(struct key **_dest_keyring)
219 struct request_key_auth *rka;
220 const struct cred *cred = current_cred();
221 struct key *dest_keyring = *_dest_keyring, *authkey;
223 kenter("%p", dest_keyring);
225 /* find the appropriate keyring */
227 /* the caller supplied one */
228 key_get(dest_keyring);
230 /* use a default keyring; falling through the cases until we
231 * find one that we actually have */
232 switch (cred->jit_keyring) {
233 case KEY_REQKEY_DEFL_DEFAULT:
234 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
235 if (cred->request_key_auth) {
236 authkey = cred->request_key_auth;
237 down_read(&authkey->sem);
238 rka = authkey->payload.data;
239 if (!test_bit(KEY_FLAG_REVOKED,
242 key_get(rka->dest_keyring);
243 up_read(&authkey->sem);
248 case KEY_REQKEY_DEFL_THREAD_KEYRING:
249 dest_keyring = key_get(cred->thread_keyring);
253 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
254 dest_keyring = key_get(cred->tgcred->process_keyring);
258 case KEY_REQKEY_DEFL_SESSION_KEYRING:
260 dest_keyring = key_get(
261 rcu_dereference(cred->tgcred->session_keyring));
267 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
269 key_get(cred->user->session_keyring);
272 case KEY_REQKEY_DEFL_USER_KEYRING:
273 dest_keyring = key_get(cred->user->uid_keyring);
276 case KEY_REQKEY_DEFL_GROUP_KEYRING:
282 *_dest_keyring = dest_keyring;
283 kleave(" [dk %d]", key_serial(dest_keyring));
288 * allocate a new key in under-construction state and attempt to link it in to
289 * the requested place
290 * - may return a key that's already under construction instead
292 static int construct_alloc_key(struct key_type *type,
293 const char *description,
294 struct key *dest_keyring,
296 struct key_user *user,
299 const struct cred *cred = current_cred();
303 kenter("%s,%s,,,", type->name, description);
305 mutex_lock(&user->cons_lock);
307 key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
312 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
315 down_write(&dest_keyring->sem);
317 /* attach the key to the destination keyring under lock, but we do need
318 * to do another check just in case someone beat us to it whilst we
319 * waited for locks */
320 mutex_lock(&key_construction_mutex);
322 key_ref = search_process_keyrings(type, description, type->match, cred);
323 if (!IS_ERR(key_ref))
324 goto key_already_present;
327 __key_link(dest_keyring, key);
329 mutex_unlock(&key_construction_mutex);
331 up_write(&dest_keyring->sem);
332 mutex_unlock(&user->cons_lock);
334 kleave(" = 0 [%d]", key_serial(key));
338 mutex_unlock(&key_construction_mutex);
340 up_write(&dest_keyring->sem);
341 mutex_unlock(&user->cons_lock);
343 *_key = key = key_ref_to_ptr(key_ref);
344 kleave(" = -EINPROGRESS [%d]", key_serial(key));
348 mutex_unlock(&user->cons_lock);
350 kleave(" = %ld", PTR_ERR(key));
355 * commence key construction
357 static struct key *construct_key_and_link(struct key_type *type,
358 const char *description,
359 const char *callout_info,
362 struct key *dest_keyring,
365 struct key_user *user;
371 user = key_user_lookup(current_fsuid(), current_user_ns());
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 kdebug("cons failed");
386 goto construction_failed;
390 key_put(dest_keyring);
391 kleave(" = key %d", key_serial(key));
395 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
397 key_put(dest_keyring);
398 kleave(" = %d", ret);
404 * - search the process's keyrings
405 * - check the list of keys being created or updated
406 * - call out to userspace for a key if supplementary info was provided
407 * - cache the key in an appropriate keyring
409 struct key *request_key_and_link(struct key_type *type,
410 const char *description,
411 const void *callout_info,
414 struct key *dest_keyring,
417 const struct cred *cred = current_cred();
421 kenter("%s,%s,%p,%zu,%p,%p,%lx",
422 type->name, description, callout_info, callout_len, aux,
423 dest_keyring, flags);
425 /* search all the process keyrings for a key */
426 key_ref = search_process_keyrings(type, description, type->match,
429 if (!IS_ERR(key_ref)) {
430 key = key_ref_to_ptr(key_ref);
431 } else if (PTR_ERR(key_ref) != -EAGAIN) {
432 key = ERR_CAST(key_ref);
434 /* the search failed, but the keyrings were searchable, so we
435 * should consult userspace if we can */
436 key = ERR_PTR(-ENOKEY);
440 key = construct_key_and_link(type, description, callout_info,
441 callout_len, aux, dest_keyring,
446 kleave(" = %p", key);
451 * wait for construction of a key to complete
453 int wait_for_key_construction(struct key *key, bool intr)
457 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
458 intr ? key_wait_bit_intr : key_wait_bit,
459 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
462 return key_validate(key);
464 EXPORT_SYMBOL(wait_for_key_construction);
468 * - search the process's keyrings
469 * - check the list of keys being created or updated
470 * - call out to userspace for a key if supplementary info was provided
471 * - waits uninterruptible for creation to complete
473 struct key *request_key(struct key_type *type,
474 const char *description,
475 const char *callout_info)
478 size_t callout_len = 0;
482 callout_len = strlen(callout_info);
483 key = request_key_and_link(type, description, callout_info, callout_len,
484 NULL, NULL, KEY_ALLOC_IN_QUOTA);
486 ret = wait_for_key_construction(key, false);
494 EXPORT_SYMBOL(request_key);
497 * request a key with auxiliary data for the upcaller
498 * - search the process's keyrings
499 * - check the list of keys being created or updated
500 * - call out to userspace for a key if supplementary info was provided
501 * - waits uninterruptible for creation to complete
503 struct key *request_key_with_auxdata(struct key_type *type,
504 const char *description,
505 const void *callout_info,
512 key = request_key_and_link(type, description, callout_info, callout_len,
513 aux, NULL, KEY_ALLOC_IN_QUOTA);
515 ret = wait_for_key_construction(key, false);
523 EXPORT_SYMBOL(request_key_with_auxdata);
526 * request a key (allow async construction)
527 * - search the process's keyrings
528 * - check the list of keys being created or updated
529 * - call out to userspace for a key if supplementary info was provided
531 struct key *request_key_async(struct key_type *type,
532 const char *description,
533 const void *callout_info,
536 return request_key_and_link(type, description, callout_info,
537 callout_len, NULL, NULL,
540 EXPORT_SYMBOL(request_key_async);
543 * request a key with auxiliary data for the upcaller (allow async construction)
544 * - search the process's keyrings
545 * - check the list of keys being created or updated
546 * - call out to userspace for a key if supplementary info was provided
548 struct key *request_key_async_with_auxdata(struct key_type *type,
549 const char *description,
550 const void *callout_info,
554 return request_key_and_link(type, description, callout_info,
555 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
557 EXPORT_SYMBOL(request_key_async_with_auxdata);