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>
23 * wait_on_bit() sleep function for uninterruptible waiting
25 static int key_wait_bit(void *flags)
32 * wait_on_bit() sleep function for interruptible waiting
34 static int key_wait_bit_intr(void *flags)
37 return signal_pending(current) ? -ERESTARTSYS : 0;
41 * call to complete the construction of a key
43 void complete_request_key(struct key_construction *cons, int error)
45 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
48 key_negate_and_link(cons->key, key_negative_timeout, NULL,
51 key_revoke(cons->authkey);
54 key_put(cons->authkey);
57 EXPORT_SYMBOL(complete_request_key);
60 * request userspace finish the construction of a key
61 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
63 static int call_sbin_request_key(struct key_construction *cons,
67 struct task_struct *tsk = current;
68 key_serial_t prkey, sskey;
69 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
70 char *argv[9], *envp[3], uid_str[12], gid_str[12];
71 char key_str[12], keyring_str[3][12];
75 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
77 /* allocate a new session keyring */
78 sprintf(desc, "_req.%u", key->serial);
80 keyring = keyring_alloc(desc, current->fsuid, current->fsgid, current,
81 KEY_ALLOC_QUOTA_OVERRUN, NULL);
82 if (IS_ERR(keyring)) {
83 ret = PTR_ERR(keyring);
87 /* attach the auth key to the session keyring */
88 ret = __key_link(keyring, authkey);
92 /* record the UID and GID */
93 sprintf(uid_str, "%d", current->fsuid);
94 sprintf(gid_str, "%d", current->fsgid);
96 /* we say which key is under construction */
97 sprintf(key_str, "%d", key->serial);
99 /* we specify the process's default keyrings */
100 sprintf(keyring_str[0], "%d",
101 tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
104 if (tsk->signal->process_keyring)
105 prkey = tsk->signal->process_keyring->serial;
107 sprintf(keyring_str[1], "%d", prkey);
109 if (tsk->signal->session_keyring) {
111 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
114 sskey = tsk->user->session_keyring->serial;
117 sprintf(keyring_str[2], "%d", sskey);
119 /* set up a minimal environment */
121 envp[i++] = "HOME=/";
122 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
125 /* set up the argument list */
127 argv[i++] = "/sbin/request-key";
128 argv[i++] = (char *) op;
132 argv[i++] = keyring_str[0];
133 argv[i++] = keyring_str[1];
134 argv[i++] = keyring_str[2];
138 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
140 kdebug("usermode -> 0x%x", ret);
142 /* ret is the exit/wait code */
143 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
144 key_validate(key) < 0)
147 /* ignore any errors from userspace if the key was
156 kleave(" = %d", ret);
157 complete_request_key(cons, ret);
162 * call out to userspace for key construction
163 * - we ignore program failure and go on key status instead
165 static int construct_key(struct key *key, const void *callout_info,
166 size_t callout_len, void *aux)
168 struct key_construction *cons;
169 request_key_actor_t actor;
173 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
175 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
179 /* allocate an authorisation key */
180 authkey = request_key_auth_new(key, callout_info, callout_len);
181 if (IS_ERR(authkey)) {
183 ret = PTR_ERR(authkey);
186 cons->authkey = key_get(authkey);
187 cons->key = key_get(key);
190 actor = call_sbin_request_key;
191 if (key->type->request_key)
192 actor = key->type->request_key;
194 ret = actor(cons, "create", aux);
196 /* check that the actor called complete_request_key() prior to
197 * returning an error */
199 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
203 kleave(" = %d", ret);
208 * link a key to the appropriate destination keyring
209 * - the caller must hold a write lock on the destination keyring
211 static void construct_key_make_link(struct key *key, struct key *dest_keyring)
213 struct task_struct *tsk = current;
214 struct key *drop = NULL;
216 kenter("{%d},%p", key->serial, dest_keyring);
218 /* find the appropriate 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;
227 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
228 dest_keyring = tsk->signal->process_keyring;
232 case KEY_REQKEY_DEFL_SESSION_KEYRING:
234 dest_keyring = key_get(
235 rcu_dereference(tsk->signal->session_keyring));
242 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
243 dest_keyring = tsk->user->session_keyring;
246 case KEY_REQKEY_DEFL_USER_KEYRING:
247 dest_keyring = tsk->user->uid_keyring;
250 case KEY_REQKEY_DEFL_GROUP_KEYRING:
256 /* and attach the key to it */
257 __key_link(dest_keyring, key);
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
267 static int construct_alloc_key(struct key_type *type,
268 const char *description,
269 struct key *dest_keyring,
271 struct key_user *user,
277 kenter("%s,%s,,,", type->name, description);
279 mutex_lock(&user->cons_lock);
281 key = key_alloc(type, description,
282 current->fsuid, current->fsgid, current, KEY_POS_ALL,
287 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
290 down_write(&dest_keyring->sem);
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);
297 key_ref = search_process_keyrings(type, description, type->match,
299 if (!IS_ERR(key_ref))
300 goto key_already_present;
303 construct_key_make_link(key, dest_keyring);
305 mutex_unlock(&key_construction_mutex);
307 up_write(&dest_keyring->sem);
308 mutex_unlock(&user->cons_lock);
310 kleave(" = 0 [%d]", key_serial(key));
314 mutex_unlock(&key_construction_mutex);
316 up_write(&dest_keyring->sem);
317 mutex_unlock(&user->cons_lock);
319 *_key = key = key_ref_to_ptr(key_ref);
320 kleave(" = -EINPROGRESS [%d]", key_serial(key));
324 mutex_unlock(&user->cons_lock);
326 kleave(" = %ld", PTR_ERR(key));
331 * commence key construction
333 static struct key *construct_key_and_link(struct key_type *type,
334 const char *description,
335 const char *callout_info,
338 struct key *dest_keyring,
341 struct key_user *user;
345 user = key_user_lookup(current->fsuid);
347 return ERR_PTR(-ENOMEM);
349 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
354 ret = construct_key(key, callout_info, callout_len, aux);
356 goto construction_failed;
362 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
369 * - search the process's keyrings
370 * - check the list of keys being created or updated
371 * - call out to userspace for a key if supplementary info was provided
372 * - cache the key in an appropriate keyring
374 struct key *request_key_and_link(struct key_type *type,
375 const char *description,
376 const void *callout_info,
379 struct key *dest_keyring,
385 kenter("%s,%s,%p,%zu,%p,%p,%lx",
386 type->name, description, callout_info, callout_len, aux,
387 dest_keyring, flags);
389 /* search all the process keyrings for a key */
390 key_ref = search_process_keyrings(type, description, type->match,
393 if (!IS_ERR(key_ref)) {
394 key = key_ref_to_ptr(key_ref);
395 } else if (PTR_ERR(key_ref) != -EAGAIN) {
396 key = ERR_CAST(key_ref);
398 /* the search failed, but the keyrings were searchable, so we
399 * should consult userspace if we can */
400 key = ERR_PTR(-ENOKEY);
404 key = construct_key_and_link(type, description, callout_info,
405 callout_len, aux, dest_keyring,
410 kleave(" = %p", key);
415 * wait for construction of a key to complete
417 int wait_for_key_construction(struct key *key, bool intr)
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);
426 return key_validate(key);
428 EXPORT_SYMBOL(wait_for_key_construction);
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
435 * - waits uninterruptible for creation to complete
437 struct key *request_key(struct key_type *type,
438 const char *description,
439 const char *callout_info)
442 size_t callout_len = 0;
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);
450 ret = wait_for_key_construction(key, false);
458 EXPORT_SYMBOL(request_key);
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
465 * - waits uninterruptible for creation to complete
467 struct key *request_key_with_auxdata(struct key_type *type,
468 const char *description,
469 const void *callout_info,
476 key = request_key_and_link(type, description, callout_info, callout_len,
477 aux, NULL, KEY_ALLOC_IN_QUOTA);
479 ret = wait_for_key_construction(key, false);
487 EXPORT_SYMBOL(request_key_with_auxdata);
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
495 struct key *request_key_async(struct key_type *type,
496 const char *description,
497 const void *callout_info,
500 return request_key_and_link(type, description, callout_info,
501 callout_len, NULL, NULL,
504 EXPORT_SYMBOL(request_key_async);
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
512 struct key *request_key_async_with_auxdata(struct key_type *type,
513 const char *description,
514 const void *callout_info,
518 return request_key_and_link(type, description, callout_info,
519 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
521 EXPORT_SYMBOL(request_key_async_with_auxdata);