blob: 0488b0af5bd64bed73dacebe8bf48a3f24075e8a [file] [log] [blame]
David Howells76181c12007-10-16 23:29:46 -07001/* Request a key from userspace
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells76181c12007-10-16 23:29:46 -07003 * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * Written by David Howells (dhowells@redhat.com)
5 *
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.
David Howellsf1a9bad2005-10-07 15:04:52 +010010 *
11 * See Documentation/keys-request-key.txt
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/kmod.h>
17#include <linux/err.h>
David Howells3e301482005-06-23 22:00:56 -070018#include <linux/keyctl.h>
Robert P. J. Dayfdb89bc2008-04-29 01:01:32 -070019#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "internal.h"
21
David Howellse9e349b2008-11-14 10:39:13 +110022#define key_negative_timeout 60 /* default timeout on a negative key's existence */
23
David Howells76181c12007-10-16 23:29:46 -070024/*
25 * wait_on_bit() sleep function for uninterruptible waiting
26 */
27static int key_wait_bit(void *flags)
28{
29 schedule();
30 return 0;
31}
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Howells76181c12007-10-16 23:29:46 -070033/*
34 * wait_on_bit() sleep function for interruptible waiting
35 */
36static int key_wait_bit_intr(void *flags)
37{
38 schedule();
39 return signal_pending(current) ? -ERESTARTSYS : 0;
40}
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
David Howells76181c12007-10-16 23:29:46 -070042/*
43 * call to complete the construction of a key
44 */
45void complete_request_key(struct key_construction *cons, int error)
46{
47 kenter("{%d,%d},%d", cons->key->serial, cons->authkey->serial, error);
48
49 if (error < 0)
50 key_negate_and_link(cons->key, key_negative_timeout, NULL,
51 cons->authkey);
52 else
53 key_revoke(cons->authkey);
54
55 key_put(cons->key);
56 key_put(cons->authkey);
57 kfree(cons);
58}
59EXPORT_SYMBOL(complete_request_key);
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
62 * request userspace finish the construction of a key
David Howellsb5f545c2006-01-08 01:02:47 -080063 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 */
David Howells76181c12007-10-16 23:29:46 -070065static int call_sbin_request_key(struct key_construction *cons,
David Howells4e54f082006-06-29 02:24:28 -070066 const char *op,
67 void *aux)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
69 struct task_struct *tsk = current;
David Howells86a264a2008-11-14 10:39:18 +110070 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 key_serial_t prkey, sskey;
David Howells76181c12007-10-16 23:29:46 -070072 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
David Howellsb5f545c2006-01-08 01:02:47 -080073 char *argv[9], *envp[3], uid_str[12], gid_str[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 char key_str[12], keyring_str[3][12];
David Howellsb5f545c2006-01-08 01:02:47 -080075 char desc[20];
David Howells3e301482005-06-23 22:00:56 -070076 int ret, i;
77
David Howellsb5f545c2006-01-08 01:02:47 -080078 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
David Howells3e301482005-06-23 22:00:56 -070079
David Howells8bbf49762008-11-14 10:39:14 +110080 ret = install_user_keyrings();
81 if (ret < 0)
82 goto error_alloc;
83
David Howellsb5f545c2006-01-08 01:02:47 -080084 /* allocate a new session keyring */
85 sprintf(desc, "_req.%u", key->serial);
86
David Howells47d804b2008-11-14 10:39:11 +110087 keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
David Howells7e047ef2006-06-26 00:24:50 -070088 KEY_ALLOC_QUOTA_OVERRUN, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -080089 if (IS_ERR(keyring)) {
90 ret = PTR_ERR(keyring);
91 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -070092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
David Howellsb5f545c2006-01-08 01:02:47 -080094 /* attach the auth key to the session keyring */
95 ret = __key_link(keyring, authkey);
96 if (ret < 0)
97 goto error_link;
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 /* record the UID and GID */
David Howells86a264a2008-11-14 10:39:18 +1100100 sprintf(uid_str, "%d", cred->fsuid);
101 sprintf(gid_str, "%d", cred->fsgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /* we say which key is under construction */
104 sprintf(key_str, "%d", key->serial);
105
106 /* we specify the process's default keyrings */
107 sprintf(keyring_str[0], "%d",
David Howells86a264a2008-11-14 10:39:18 +1100108 cred->thread_keyring ?
109 cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 prkey = 0;
112 if (tsk->signal->process_keyring)
113 prkey = tsk->signal->process_keyring->serial;
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 sprintf(keyring_str[1], "%d", prkey);
David Howells3e301482005-06-23 22:00:56 -0700116
117 if (tsk->signal->session_keyring) {
118 rcu_read_lock();
119 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
120 rcu_read_unlock();
David Howells76181c12007-10-16 23:29:46 -0700121 } else {
David Howells86a264a2008-11-14 10:39:18 +1100122 sskey = cred->user->session_keyring->serial;
David Howells3e301482005-06-23 22:00:56 -0700123 }
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 sprintf(keyring_str[2], "%d", sskey);
126
127 /* set up a minimal environment */
128 i = 0;
129 envp[i++] = "HOME=/";
130 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
131 envp[i] = NULL;
132
133 /* set up the argument list */
134 i = 0;
135 argv[i++] = "/sbin/request-key";
136 argv[i++] = (char *) op;
137 argv[i++] = key_str;
138 argv[i++] = uid_str;
139 argv[i++] = gid_str;
140 argv[i++] = keyring_str[0];
141 argv[i++] = keyring_str[1];
142 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 argv[i] = NULL;
144
145 /* do it */
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700146 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
147 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700148 kdebug("usermode -> 0x%x", ret);
149 if (ret >= 0) {
150 /* ret is the exit/wait code */
151 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
152 key_validate(key) < 0)
153 ret = -ENOKEY;
154 else
155 /* ignore any errors from userspace if the key was
156 * instantiated */
157 ret = 0;
158 }
David Howells3e301482005-06-23 22:00:56 -0700159
David Howellsb5f545c2006-01-08 01:02:47 -0800160error_link:
161 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700162
David Howellsb5f545c2006-01-08 01:02:47 -0800163error_alloc:
David Howells3e301482005-06-23 22:00:56 -0700164 kleave(" = %d", ret);
David Howells76181c12007-10-16 23:29:46 -0700165 complete_request_key(cons, ret);
David Howells3e301482005-06-23 22:00:56 -0700166 return ret;
David Howells76181c12007-10-16 23:29:46 -0700167}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
David Howells76181c12007-10-16 23:29:46 -0700170 * call out to userspace for key construction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * - we ignore program failure and go on key status instead
172 */
David Howells4a38e122008-04-29 01:01:24 -0700173static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100174 size_t callout_len, void *aux,
175 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
David Howells76181c12007-10-16 23:29:46 -0700177 struct key_construction *cons;
David Howellsb5f545c2006-01-08 01:02:47 -0800178 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700179 struct key *authkey;
180 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David Howells4a38e122008-04-29 01:01:24 -0700182 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700183
David Howells76181c12007-10-16 23:29:46 -0700184 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
185 if (!cons)
186 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
David Howellsb5f545c2006-01-08 01:02:47 -0800188 /* allocate an authorisation key */
David Howells8bbf49762008-11-14 10:39:14 +1100189 authkey = request_key_auth_new(key, callout_info, callout_len,
190 dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800191 if (IS_ERR(authkey)) {
David Howells76181c12007-10-16 23:29:46 -0700192 kfree(cons);
David Howellsb5f545c2006-01-08 01:02:47 -0800193 ret = PTR_ERR(authkey);
194 authkey = NULL;
David Howells76181c12007-10-16 23:29:46 -0700195 } else {
196 cons->authkey = key_get(authkey);
197 cons->key = key_get(key);
198
199 /* make the call */
200 actor = call_sbin_request_key;
201 if (key->type->request_key)
202 actor = key->type->request_key;
203
204 ret = actor(cons, "create", aux);
205
206 /* check that the actor called complete_request_key() prior to
207 * returning an error */
208 WARN_ON(ret < 0 &&
209 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
210 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800211 }
212
David Howells76181c12007-10-16 23:29:46 -0700213 kleave(" = %d", ret);
214 return ret;
215}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217/*
David Howells8bbf49762008-11-14 10:39:14 +1100218 * get the appropriate destination keyring for the request
219 * - we return whatever keyring we select with an extra reference upon it which
220 * the caller must release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
David Howells8bbf49762008-11-14 10:39:14 +1100222static void construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700223{
David Howells8bbf49762008-11-14 10:39:14 +1100224 struct request_key_auth *rka;
David Howells3e301482005-06-23 22:00:56 -0700225 struct task_struct *tsk = current;
David Howells8bbf49762008-11-14 10:39:14 +1100226 struct key *dest_keyring = *_dest_keyring, *authkey;
David Howells3e301482005-06-23 22:00:56 -0700227
David Howells8bbf49762008-11-14 10:39:14 +1100228 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700229
230 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100231 if (dest_keyring) {
232 /* the caller supplied one */
233 key_get(dest_keyring);
234 } else {
235 /* use a default keyring; falling through the cases until we
236 * find one that we actually have */
David Howellsb6dff3e2008-11-14 10:39:16 +1100237 switch (tsk->cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700238 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100239 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100240 if (tsk->cred->request_key_auth) {
241 authkey = tsk->cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100242 down_read(&authkey->sem);
243 rka = authkey->payload.data;
244 if (!test_bit(KEY_FLAG_REVOKED,
245 &authkey->flags))
246 dest_keyring =
247 key_get(rka->dest_keyring);
248 up_read(&authkey->sem);
249 if (dest_keyring)
250 break;
251 }
252
David Howells3e301482005-06-23 22:00:56 -0700253 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100254 dest_keyring = key_get(tsk->cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700255 if (dest_keyring)
256 break;
257
258 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +1100259 dest_keyring = key_get(tsk->signal->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700260 if (dest_keyring)
261 break;
262
263 case KEY_REQKEY_DEFL_SESSION_KEYRING:
264 rcu_read_lock();
265 dest_keyring = key_get(
266 rcu_dereference(tsk->signal->session_keyring));
267 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700268
269 if (dest_keyring)
270 break;
271
272 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100273 dest_keyring =
274 key_get(tsk->cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700275 break;
276
277 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100278 dest_keyring = key_get(tsk->cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700279 break;
280
281 case KEY_REQKEY_DEFL_GROUP_KEYRING:
282 default:
283 BUG();
284 }
285 }
286
David Howells8bbf49762008-11-14 10:39:14 +1100287 *_dest_keyring = dest_keyring;
288 kleave(" [dk %d]", key_serial(dest_keyring));
289 return;
David Howells76181c12007-10-16 23:29:46 -0700290}
David Howells3e301482005-06-23 22:00:56 -0700291
David Howells76181c12007-10-16 23:29:46 -0700292/*
293 * allocate a new key in under-construction state and attempt to link it in to
294 * the requested place
295 * - may return a key that's already under construction instead
296 */
297static int construct_alloc_key(struct key_type *type,
298 const char *description,
299 struct key *dest_keyring,
300 unsigned long flags,
301 struct key_user *user,
302 struct key **_key)
303{
304 struct key *key;
305 key_ref_t key_ref;
David Howells3e301482005-06-23 22:00:56 -0700306
David Howells76181c12007-10-16 23:29:46 -0700307 kenter("%s,%s,,,", type->name, description);
308
309 mutex_lock(&user->cons_lock);
310
311 key = key_alloc(type, description,
David Howells47d804b2008-11-14 10:39:11 +1100312 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
David Howells76181c12007-10-16 23:29:46 -0700313 flags);
314 if (IS_ERR(key))
315 goto alloc_failed;
316
317 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
318
David Howells8bbf49762008-11-14 10:39:14 +1100319 down_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700320
321 /* attach the key to the destination keyring under lock, but we do need
322 * to do another check just in case someone beat us to it whilst we
323 * waited for locks */
324 mutex_lock(&key_construction_mutex);
325
326 key_ref = search_process_keyrings(type, description, type->match,
327 current);
328 if (!IS_ERR(key_ref))
329 goto key_already_present;
330
David Howells8bbf49762008-11-14 10:39:14 +1100331 __key_link(dest_keyring, key);
David Howells76181c12007-10-16 23:29:46 -0700332
333 mutex_unlock(&key_construction_mutex);
David Howells8bbf49762008-11-14 10:39:14 +1100334 up_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700335 mutex_unlock(&user->cons_lock);
336 *_key = key;
337 kleave(" = 0 [%d]", key_serial(key));
338 return 0;
339
340key_already_present:
341 mutex_unlock(&key_construction_mutex);
342 if (dest_keyring)
343 up_write(&dest_keyring->sem);
344 mutex_unlock(&user->cons_lock);
345 key_put(key);
346 *_key = key = key_ref_to_ptr(key_ref);
347 kleave(" = -EINPROGRESS [%d]", key_serial(key));
348 return -EINPROGRESS;
349
350alloc_failed:
351 mutex_unlock(&user->cons_lock);
352 *_key = NULL;
353 kleave(" = %ld", PTR_ERR(key));
354 return PTR_ERR(key);
355}
356
357/*
358 * commence key construction
359 */
360static struct key *construct_key_and_link(struct key_type *type,
361 const char *description,
362 const char *callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700363 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700364 void *aux,
365 struct key *dest_keyring,
366 unsigned long flags)
367{
368 struct key_user *user;
369 struct key *key;
370 int ret;
371
David Howells47d804b2008-11-14 10:39:11 +1100372 user = key_user_lookup(current_fsuid());
David Howells76181c12007-10-16 23:29:46 -0700373 if (!user)
374 return ERR_PTR(-ENOMEM);
375
David Howells8bbf49762008-11-14 10:39:14 +1100376 construct_get_dest_keyring(&dest_keyring);
377
David Howells76181c12007-10-16 23:29:46 -0700378 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
379 &key);
380 key_user_put(user);
381
382 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100383 ret = construct_key(key, callout_info, callout_len, aux,
384 dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700385 if (ret < 0)
386 goto construction_failed;
387 }
388
David Howells8bbf49762008-11-14 10:39:14 +1100389 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700390 return key;
391
392construction_failed:
393 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
394 key_put(key);
David Howells8bbf49762008-11-14 10:39:14 +1100395 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700396 return ERR_PTR(ret);
397}
398
David Howells3e301482005-06-23 22:00:56 -0700399/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 * request a key
401 * - search the process's keyrings
402 * - check the list of keys being created or updated
David Howells3e301482005-06-23 22:00:56 -0700403 * - call out to userspace for a key if supplementary info was provided
404 * - cache the key in an appropriate keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 */
David Howells3e301482005-06-23 22:00:56 -0700406struct key *request_key_and_link(struct key_type *type,
407 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700408 const void *callout_info,
409 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700410 void *aux,
David Howells7e047ef2006-06-26 00:24:50 -0700411 struct key *dest_keyring,
412 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100415 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
David Howells4a38e122008-04-29 01:01:24 -0700417 kenter("%s,%s,%p,%zu,%p,%p,%lx",
418 type->name, description, callout_info, callout_len, aux,
David Howells4e54f082006-06-29 02:24:28 -0700419 dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /* search all the process keyrings for a key */
David Howells664cceb2005-09-28 17:03:15 +0100422 key_ref = search_process_keyrings(type, description, type->match,
423 current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
David Howells664cceb2005-09-28 17:03:15 +0100425 if (!IS_ERR(key_ref)) {
426 key = key_ref_to_ptr(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700427 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800428 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700429 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 /* the search failed, but the keyrings were searchable, so we
431 * should consult userspace if we can */
432 key = ERR_PTR(-ENOKEY);
433 if (!callout_info)
434 goto error;
435
David Howells76181c12007-10-16 23:29:46 -0700436 key = construct_key_and_link(type, description, callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700437 callout_len, aux, dest_keyring,
438 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440
David Howells3e301482005-06-23 22:00:56 -0700441error:
442 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return key;
David Howells76181c12007-10-16 23:29:46 -0700444}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
David Howells76181c12007-10-16 23:29:46 -0700446/*
447 * wait for construction of a key to complete
448 */
449int wait_for_key_construction(struct key *key, bool intr)
450{
451 int ret;
David Howells3e301482005-06-23 22:00:56 -0700452
David Howells76181c12007-10-16 23:29:46 -0700453 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
454 intr ? key_wait_bit_intr : key_wait_bit,
455 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
456 if (ret < 0)
457 return ret;
458 return key_validate(key);
459}
460EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700461
David Howells3e301482005-06-23 22:00:56 -0700462/*
463 * request a key
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
David Howells76181c12007-10-16 23:29:46 -0700467 * - waits uninterruptible for creation to complete
David Howells3e301482005-06-23 22:00:56 -0700468 */
469struct key *request_key(struct key_type *type,
470 const char *description,
471 const char *callout_info)
472{
David Howells76181c12007-10-16 23:29:46 -0700473 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700474 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700475 int ret;
David Howells3e301482005-06-23 22:00:56 -0700476
David Howells4a38e122008-04-29 01:01:24 -0700477 if (callout_info)
478 callout_len = strlen(callout_info);
479 key = request_key_and_link(type, description, callout_info, callout_len,
480 NULL, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700481 if (!IS_ERR(key)) {
482 ret = wait_for_key_construction(key, false);
483 if (ret < 0) {
484 key_put(key);
485 return ERR_PTR(ret);
486 }
487 }
488 return key;
489}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700491
David Howells4e54f082006-06-29 02:24:28 -0700492/*
493 * request a key with auxiliary data for the upcaller
494 * - search the process's keyrings
495 * - check the list of keys being created or updated
496 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700497 * - waits uninterruptible for creation to complete
David Howells4e54f082006-06-29 02:24:28 -0700498 */
499struct key *request_key_with_auxdata(struct key_type *type,
500 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700501 const void *callout_info,
502 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700503 void *aux)
504{
David Howells76181c12007-10-16 23:29:46 -0700505 struct key *key;
506 int ret;
507
David Howells4a38e122008-04-29 01:01:24 -0700508 key = request_key_and_link(type, description, callout_info, callout_len,
509 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700510 if (!IS_ERR(key)) {
511 ret = wait_for_key_construction(key, false);
512 if (ret < 0) {
513 key_put(key);
514 return ERR_PTR(ret);
515 }
516 }
517 return key;
518}
519EXPORT_SYMBOL(request_key_with_auxdata);
520
521/*
522 * request a key (allow async construction)
523 * - search the process's keyrings
524 * - check the list of keys being created or updated
525 * - call out to userspace for a key if supplementary info was provided
526 */
527struct key *request_key_async(struct key_type *type,
528 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700529 const void *callout_info,
530 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700531{
David Howells4a38e122008-04-29 01:01:24 -0700532 return request_key_and_link(type, description, callout_info,
533 callout_len, NULL, NULL,
534 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700535}
536EXPORT_SYMBOL(request_key_async);
537
538/*
539 * request a key with auxiliary data for the upcaller (allow async construction)
540 * - search the process's keyrings
541 * - check the list of keys being created or updated
542 * - call out to userspace for a key if supplementary info was provided
543 */
544struct key *request_key_async_with_auxdata(struct key_type *type,
545 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700546 const void *callout_info,
547 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700548 void *aux)
549{
David Howells4a38e122008-04-29 01:01:24 -0700550 return request_key_and_link(type, description, callout_info,
551 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700552}
553EXPORT_SYMBOL(request_key_async_with_auxdata);