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 /* allocate a new session keyring */
80 sprintf(desc, "_req.%u", key->serial);
82 keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
83 KEY_ALLOC_QUOTA_OVERRUN, NULL);
84 if (IS_ERR(keyring)) {
85 ret = PTR_ERR(keyring);
89 /* attach the auth key to the session keyring */
90 ret = __key_link(keyring, authkey);
94 /* record the UID and GID */
95 sprintf(uid_str, "%d", current_fsuid());
96 sprintf(gid_str, "%d", current_fsgid());
98 /* we say which key is under construction */
99 sprintf(key_str, "%d", key->serial);
101 /* we specify the process's default keyrings */
102 sprintf(keyring_str[0], "%d",
103 tsk->thread_keyring ? tsk->thread_keyring->serial : 0);
106 if (tsk->signal->process_keyring)
107 prkey = tsk->signal->process_keyring->serial;
109 sprintf(keyring_str[1], "%d", prkey);
111 if (tsk->signal->session_keyring) {
113 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
116 sskey = tsk->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)
170 struct key_construction *cons;
171 request_key_actor_t actor;
175 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
177 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
181 /* allocate an authorisation key */
182 authkey = request_key_auth_new(key, callout_info, callout_len);
183 if (IS_ERR(authkey)) {
185 ret = PTR_ERR(authkey);
188 cons->authkey = key_get(authkey);
189 cons->key = key_get(key);
192 actor = call_sbin_request_key;
193 if (key->type->request_key)
194 actor = key->type->request_key;
196 ret = actor(cons, "create", aux);
198 /* check that the actor called complete_request_key() prior to
199 * returning an error */
201 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
205 kleave(" = %d", ret);
210 * link a key to the appropriate destination keyring
211 * - the caller must hold a write lock on the destination keyring
213 static void construct_key_make_link(struct key *key, struct key *dest_keyring)
215 struct task_struct *tsk = current;
216 struct key *drop = NULL;
218 kenter("{%d},%p", key->serial, dest_keyring);
220 /* find the appropriate 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;
229 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
230 dest_keyring = tsk->signal->process_keyring;
234 case KEY_REQKEY_DEFL_SESSION_KEYRING:
236 dest_keyring = key_get(
237 rcu_dereference(tsk->signal->session_keyring));
244 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
245 dest_keyring = tsk->user->session_keyring;
248 case KEY_REQKEY_DEFL_USER_KEYRING:
249 dest_keyring = tsk->user->uid_keyring;
252 case KEY_REQKEY_DEFL_GROUP_KEYRING:
258 /* and attach the key to it */
259 __key_link(dest_keyring, key);
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
269 static int construct_alloc_key(struct key_type *type,
270 const char *description,
271 struct key *dest_keyring,
273 struct key_user *user,
279 kenter("%s,%s,,,", type->name, description);
281 mutex_lock(&user->cons_lock);
283 key = key_alloc(type, description,
284 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
289 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
292 down_write(&dest_keyring->sem);
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);
299 key_ref = search_process_keyrings(type, description, type->match,
301 if (!IS_ERR(key_ref))
302 goto key_already_present;
305 construct_key_make_link(key, dest_keyring);
307 mutex_unlock(&key_construction_mutex);
309 up_write(&dest_keyring->sem);
310 mutex_unlock(&user->cons_lock);
312 kleave(" = 0 [%d]", key_serial(key));
316 mutex_unlock(&key_construction_mutex);
318 up_write(&dest_keyring->sem);
319 mutex_unlock(&user->cons_lock);
321 *_key = key = key_ref_to_ptr(key_ref);
322 kleave(" = -EINPROGRESS [%d]", key_serial(key));
326 mutex_unlock(&user->cons_lock);
328 kleave(" = %ld", PTR_ERR(key));
333 * commence key construction
335 static struct key *construct_key_and_link(struct key_type *type,
336 const char *description,
337 const char *callout_info,
340 struct key *dest_keyring,
343 struct key_user *user;
347 user = key_user_lookup(current_fsuid());
349 return ERR_PTR(-ENOMEM);
351 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
356 ret = construct_key(key, callout_info, callout_len, aux);
358 goto construction_failed;
364 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
371 * - search the process's keyrings
372 * - check the list of keys being created or updated
373 * - call out to userspace for a key if supplementary info was provided
374 * - cache the key in an appropriate keyring
376 struct key *request_key_and_link(struct key_type *type,
377 const char *description,
378 const void *callout_info,
381 struct key *dest_keyring,
387 kenter("%s,%s,%p,%zu,%p,%p,%lx",
388 type->name, description, callout_info, callout_len, aux,
389 dest_keyring, flags);
391 /* search all the process keyrings for a key */
392 key_ref = search_process_keyrings(type, description, type->match,
395 if (!IS_ERR(key_ref)) {
396 key = key_ref_to_ptr(key_ref);
397 } else if (PTR_ERR(key_ref) != -EAGAIN) {
398 key = ERR_CAST(key_ref);
400 /* the search failed, but the keyrings were searchable, so we
401 * should consult userspace if we can */
402 key = ERR_PTR(-ENOKEY);
406 key = construct_key_and_link(type, description, callout_info,
407 callout_len, aux, dest_keyring,
412 kleave(" = %p", key);
417 * wait for construction of a key to complete
419 int wait_for_key_construction(struct key *key, bool intr)
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);
428 return key_validate(key);
430 EXPORT_SYMBOL(wait_for_key_construction);
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
437 * - waits uninterruptible for creation to complete
439 struct key *request_key(struct key_type *type,
440 const char *description,
441 const char *callout_info)
444 size_t callout_len = 0;
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);
452 ret = wait_for_key_construction(key, false);
460 EXPORT_SYMBOL(request_key);
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
467 * - waits uninterruptible for creation to complete
469 struct key *request_key_with_auxdata(struct key_type *type,
470 const char *description,
471 const void *callout_info,
478 key = request_key_and_link(type, description, callout_info, callout_len,
479 aux, NULL, KEY_ALLOC_IN_QUOTA);
481 ret = wait_for_key_construction(key, false);
489 EXPORT_SYMBOL(request_key_with_auxdata);
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
497 struct key *request_key_async(struct key_type *type,
498 const char *description,
499 const void *callout_info,
502 return request_key_and_link(type, description, callout_info,
503 callout_len, NULL, NULL,
506 EXPORT_SYMBOL(request_key_async);
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
514 struct key *request_key_async_with_auxdata(struct key_type *type,
515 const char *description,
516 const void *callout_info,
520 return request_key_and_link(type, description, callout_info,
521 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
523 EXPORT_SYMBOL(request_key_async_with_auxdata);