blob: 3d12558362df24c60740dd3edf03960d463e176b [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{
David Howells86a264a2008-11-14 10:39:18 +110069 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 key_serial_t prkey, sskey;
David Howells76181c12007-10-16 23:29:46 -070071 struct key *key = cons->key, *authkey = cons->authkey, *keyring;
David Howellsb5f545c2006-01-08 01:02:47 -080072 char *argv[9], *envp[3], uid_str[12], gid_str[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 char key_str[12], keyring_str[3][12];
David Howellsb5f545c2006-01-08 01:02:47 -080074 char desc[20];
David Howells3e301482005-06-23 22:00:56 -070075 int ret, i;
76
David Howellsb5f545c2006-01-08 01:02:47 -080077 kenter("{%d},{%d},%s", key->serial, authkey->serial, op);
David Howells3e301482005-06-23 22:00:56 -070078
David Howells8bbf49762008-11-14 10:39:14 +110079 ret = install_user_keyrings();
80 if (ret < 0)
81 goto error_alloc;
82
David Howellsb5f545c2006-01-08 01:02:47 -080083 /* allocate a new session keyring */
84 sprintf(desc, "_req.%u", key->serial);
85
David Howells47d804b2008-11-14 10:39:11 +110086 keyring = keyring_alloc(desc, current_fsuid(), current_fsgid(), current,
David Howells7e047ef2006-06-26 00:24:50 -070087 KEY_ALLOC_QUOTA_OVERRUN, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -080088 if (IS_ERR(keyring)) {
89 ret = PTR_ERR(keyring);
90 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -070091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
David Howellsb5f545c2006-01-08 01:02:47 -080093 /* attach the auth key to the session keyring */
94 ret = __key_link(keyring, authkey);
95 if (ret < 0)
96 goto error_link;
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 /* record the UID and GID */
David Howells86a264a2008-11-14 10:39:18 +110099 sprintf(uid_str, "%d", cred->fsuid);
100 sprintf(gid_str, "%d", cred->fsgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* we say which key is under construction */
103 sprintf(key_str, "%d", key->serial);
104
105 /* we specify the process's default keyrings */
106 sprintf(keyring_str[0], "%d",
David Howells86a264a2008-11-14 10:39:18 +1100107 cred->thread_keyring ?
108 cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 prkey = 0;
David Howellsbb952bb2008-11-14 10:39:20 +1100111 if (cred->tgcred->process_keyring)
112 prkey = cred->tgcred->process_keyring->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
David Howellsbb952bb2008-11-14 10:39:20 +1100114 if (cred->tgcred->session_keyring)
115 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
116 else
David Howells86a264a2008-11-14 10:39:18 +1100117 sskey = cred->user->session_keyring->serial;
David Howells3e301482005-06-23 22:00:56 -0700118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 sprintf(keyring_str[2], "%d", sskey);
120
121 /* set up a minimal environment */
122 i = 0;
123 envp[i++] = "HOME=/";
124 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
125 envp[i] = NULL;
126
127 /* set up the argument list */
128 i = 0;
129 argv[i++] = "/sbin/request-key";
130 argv[i++] = (char *) op;
131 argv[i++] = key_str;
132 argv[i++] = uid_str;
133 argv[i++] = gid_str;
134 argv[i++] = keyring_str[0];
135 argv[i++] = keyring_str[1];
136 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 argv[i] = NULL;
138
139 /* do it */
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700140 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
141 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700142 kdebug("usermode -> 0x%x", ret);
143 if (ret >= 0) {
144 /* ret is the exit/wait code */
145 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
146 key_validate(key) < 0)
147 ret = -ENOKEY;
148 else
149 /* ignore any errors from userspace if the key was
150 * instantiated */
151 ret = 0;
152 }
David Howells3e301482005-06-23 22:00:56 -0700153
David Howellsb5f545c2006-01-08 01:02:47 -0800154error_link:
155 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700156
David Howellsb5f545c2006-01-08 01:02:47 -0800157error_alloc:
David Howells3e301482005-06-23 22:00:56 -0700158 kleave(" = %d", ret);
David Howells76181c12007-10-16 23:29:46 -0700159 complete_request_key(cons, ret);
David Howells3e301482005-06-23 22:00:56 -0700160 return ret;
David Howells76181c12007-10-16 23:29:46 -0700161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/*
David Howells76181c12007-10-16 23:29:46 -0700164 * call out to userspace for key construction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 * - we ignore program failure and go on key status instead
166 */
David Howells4a38e122008-04-29 01:01:24 -0700167static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100168 size_t callout_len, void *aux,
169 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
David Howells76181c12007-10-16 23:29:46 -0700171 struct key_construction *cons;
David Howellsb5f545c2006-01-08 01:02:47 -0800172 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700173 struct key *authkey;
174 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
David Howells4a38e122008-04-29 01:01:24 -0700176 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700177
David Howells76181c12007-10-16 23:29:46 -0700178 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
179 if (!cons)
180 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
David Howellsb5f545c2006-01-08 01:02:47 -0800182 /* allocate an authorisation key */
David Howells8bbf49762008-11-14 10:39:14 +1100183 authkey = request_key_auth_new(key, callout_info, callout_len,
184 dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800185 if (IS_ERR(authkey)) {
David Howells76181c12007-10-16 23:29:46 -0700186 kfree(cons);
David Howellsb5f545c2006-01-08 01:02:47 -0800187 ret = PTR_ERR(authkey);
188 authkey = NULL;
David Howells76181c12007-10-16 23:29:46 -0700189 } else {
190 cons->authkey = key_get(authkey);
191 cons->key = key_get(key);
192
193 /* make the call */
194 actor = call_sbin_request_key;
195 if (key->type->request_key)
196 actor = key->type->request_key;
197
198 ret = actor(cons, "create", aux);
199
200 /* check that the actor called complete_request_key() prior to
201 * returning an error */
202 WARN_ON(ret < 0 &&
203 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
204 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800205 }
206
David Howells76181c12007-10-16 23:29:46 -0700207 kleave(" = %d", ret);
208 return ret;
209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
David Howells8bbf49762008-11-14 10:39:14 +1100212 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 */
David Howells8bbf49762008-11-14 10:39:14 +1100216static void construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700217{
David Howells8bbf49762008-11-14 10:39:14 +1100218 struct request_key_auth *rka;
David Howellsbb952bb2008-11-14 10:39:20 +1100219 const struct cred *cred = current_cred();
David Howells8bbf49762008-11-14 10:39:14 +1100220 struct key *dest_keyring = *_dest_keyring, *authkey;
David Howells3e301482005-06-23 22:00:56 -0700221
David Howells8bbf49762008-11-14 10:39:14 +1100222 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700223
224 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100225 if (dest_keyring) {
226 /* the caller supplied one */
227 key_get(dest_keyring);
228 } else {
229 /* use a default keyring; falling through the cases until we
230 * find one that we actually have */
David Howellsbb952bb2008-11-14 10:39:20 +1100231 switch (cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700232 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100233 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100234 if (cred->request_key_auth) {
235 authkey = cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100236 down_read(&authkey->sem);
237 rka = authkey->payload.data;
238 if (!test_bit(KEY_FLAG_REVOKED,
239 &authkey->flags))
240 dest_keyring =
241 key_get(rka->dest_keyring);
242 up_read(&authkey->sem);
243 if (dest_keyring)
244 break;
245 }
246
David Howells3e301482005-06-23 22:00:56 -0700247 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100248 dest_keyring = key_get(cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700249 if (dest_keyring)
250 break;
251
252 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100253 dest_keyring = key_get(cred->tgcred->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700254 if (dest_keyring)
255 break;
256
257 case KEY_REQKEY_DEFL_SESSION_KEYRING:
258 rcu_read_lock();
259 dest_keyring = key_get(
David Howellsbb952bb2008-11-14 10:39:20 +1100260 rcu_dereference(cred->tgcred->session_keyring));
David Howells3e301482005-06-23 22:00:56 -0700261 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700262
263 if (dest_keyring)
264 break;
265
266 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100267 dest_keyring =
David Howellsbb952bb2008-11-14 10:39:20 +1100268 key_get(cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700269 break;
270
271 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100272 dest_keyring = key_get(cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700273 break;
274
275 case KEY_REQKEY_DEFL_GROUP_KEYRING:
276 default:
277 BUG();
278 }
279 }
280
David Howells8bbf49762008-11-14 10:39:14 +1100281 *_dest_keyring = dest_keyring;
282 kleave(" [dk %d]", key_serial(dest_keyring));
283 return;
David Howells76181c12007-10-16 23:29:46 -0700284}
David Howells3e301482005-06-23 22:00:56 -0700285
David Howells76181c12007-10-16 23:29:46 -0700286/*
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
290 */
291static int construct_alloc_key(struct key_type *type,
292 const char *description,
293 struct key *dest_keyring,
294 unsigned long flags,
295 struct key_user *user,
296 struct key **_key)
297{
298 struct key *key;
299 key_ref_t key_ref;
David Howells3e301482005-06-23 22:00:56 -0700300
David Howells76181c12007-10-16 23:29:46 -0700301 kenter("%s,%s,,,", type->name, description);
302
303 mutex_lock(&user->cons_lock);
304
305 key = key_alloc(type, description,
David Howells47d804b2008-11-14 10:39:11 +1100306 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
David Howells76181c12007-10-16 23:29:46 -0700307 flags);
308 if (IS_ERR(key))
309 goto alloc_failed;
310
311 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
312
David Howells8bbf49762008-11-14 10:39:14 +1100313 down_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700314
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);
319
320 key_ref = search_process_keyrings(type, description, type->match,
321 current);
322 if (!IS_ERR(key_ref))
323 goto key_already_present;
324
David Howells8bbf49762008-11-14 10:39:14 +1100325 __key_link(dest_keyring, key);
David Howells76181c12007-10-16 23:29:46 -0700326
327 mutex_unlock(&key_construction_mutex);
David Howells8bbf49762008-11-14 10:39:14 +1100328 up_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700329 mutex_unlock(&user->cons_lock);
330 *_key = key;
331 kleave(" = 0 [%d]", key_serial(key));
332 return 0;
333
334key_already_present:
335 mutex_unlock(&key_construction_mutex);
336 if (dest_keyring)
337 up_write(&dest_keyring->sem);
338 mutex_unlock(&user->cons_lock);
339 key_put(key);
340 *_key = key = key_ref_to_ptr(key_ref);
341 kleave(" = -EINPROGRESS [%d]", key_serial(key));
342 return -EINPROGRESS;
343
344alloc_failed:
345 mutex_unlock(&user->cons_lock);
346 *_key = NULL;
347 kleave(" = %ld", PTR_ERR(key));
348 return PTR_ERR(key);
349}
350
351/*
352 * commence key construction
353 */
354static struct key *construct_key_and_link(struct key_type *type,
355 const char *description,
356 const char *callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700357 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700358 void *aux,
359 struct key *dest_keyring,
360 unsigned long flags)
361{
362 struct key_user *user;
363 struct key *key;
364 int ret;
365
David Howells47d804b2008-11-14 10:39:11 +1100366 user = key_user_lookup(current_fsuid());
David Howells76181c12007-10-16 23:29:46 -0700367 if (!user)
368 return ERR_PTR(-ENOMEM);
369
David Howells8bbf49762008-11-14 10:39:14 +1100370 construct_get_dest_keyring(&dest_keyring);
371
David Howells76181c12007-10-16 23:29:46 -0700372 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
373 &key);
374 key_user_put(user);
375
376 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100377 ret = construct_key(key, callout_info, callout_len, aux,
378 dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700379 if (ret < 0)
380 goto construction_failed;
381 }
382
David Howells8bbf49762008-11-14 10:39:14 +1100383 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700384 return key;
385
386construction_failed:
387 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
388 key_put(key);
David Howells8bbf49762008-11-14 10:39:14 +1100389 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700390 return ERR_PTR(ret);
391}
392
David Howells3e301482005-06-23 22:00:56 -0700393/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * request a key
395 * - search the process's keyrings
396 * - check the list of keys being created or updated
David Howells3e301482005-06-23 22:00:56 -0700397 * - call out to userspace for a key if supplementary info was provided
398 * - cache the key in an appropriate keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 */
David Howells3e301482005-06-23 22:00:56 -0700400struct key *request_key_and_link(struct key_type *type,
401 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700402 const void *callout_info,
403 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700404 void *aux,
David Howells7e047ef2006-06-26 00:24:50 -0700405 struct key *dest_keyring,
406 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100409 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
David Howells4a38e122008-04-29 01:01:24 -0700411 kenter("%s,%s,%p,%zu,%p,%p,%lx",
412 type->name, description, callout_info, callout_len, aux,
David Howells4e54f082006-06-29 02:24:28 -0700413 dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* search all the process keyrings for a key */
David Howells664cceb2005-09-28 17:03:15 +0100416 key_ref = search_process_keyrings(type, description, type->match,
417 current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
David Howells664cceb2005-09-28 17:03:15 +0100419 if (!IS_ERR(key_ref)) {
420 key = key_ref_to_ptr(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700421 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800422 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700423 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /* the search failed, but the keyrings were searchable, so we
425 * should consult userspace if we can */
426 key = ERR_PTR(-ENOKEY);
427 if (!callout_info)
428 goto error;
429
David Howells76181c12007-10-16 23:29:46 -0700430 key = construct_key_and_link(type, description, callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700431 callout_len, aux, dest_keyring,
432 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
434
David Howells3e301482005-06-23 22:00:56 -0700435error:
436 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 return key;
David Howells76181c12007-10-16 23:29:46 -0700438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
David Howells76181c12007-10-16 23:29:46 -0700440/*
441 * wait for construction of a key to complete
442 */
443int wait_for_key_construction(struct key *key, bool intr)
444{
445 int ret;
David Howells3e301482005-06-23 22:00:56 -0700446
David Howells76181c12007-10-16 23:29:46 -0700447 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);
450 if (ret < 0)
451 return ret;
452 return key_validate(key);
453}
454EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700455
David Howells3e301482005-06-23 22:00:56 -0700456/*
457 * request a key
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
David Howells76181c12007-10-16 23:29:46 -0700461 * - waits uninterruptible for creation to complete
David Howells3e301482005-06-23 22:00:56 -0700462 */
463struct key *request_key(struct key_type *type,
464 const char *description,
465 const char *callout_info)
466{
David Howells76181c12007-10-16 23:29:46 -0700467 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700468 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700469 int ret;
David Howells3e301482005-06-23 22:00:56 -0700470
David Howells4a38e122008-04-29 01:01:24 -0700471 if (callout_info)
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);
David Howells76181c12007-10-16 23:29:46 -0700475 if (!IS_ERR(key)) {
476 ret = wait_for_key_construction(key, false);
477 if (ret < 0) {
478 key_put(key);
479 return ERR_PTR(ret);
480 }
481 }
482 return key;
483}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700485
David Howells4e54f082006-06-29 02:24:28 -0700486/*
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
David Howells76181c12007-10-16 23:29:46 -0700491 * - waits uninterruptible for creation to complete
David Howells4e54f082006-06-29 02:24:28 -0700492 */
493struct key *request_key_with_auxdata(struct key_type *type,
494 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700495 const void *callout_info,
496 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700497 void *aux)
498{
David Howells76181c12007-10-16 23:29:46 -0700499 struct key *key;
500 int ret;
501
David Howells4a38e122008-04-29 01:01:24 -0700502 key = request_key_and_link(type, description, callout_info, callout_len,
503 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700504 if (!IS_ERR(key)) {
505 ret = wait_for_key_construction(key, false);
506 if (ret < 0) {
507 key_put(key);
508 return ERR_PTR(ret);
509 }
510 }
511 return key;
512}
513EXPORT_SYMBOL(request_key_with_auxdata);
514
515/*
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
520 */
521struct key *request_key_async(struct key_type *type,
522 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700523 const void *callout_info,
524 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700525{
David Howells4a38e122008-04-29 01:01:24 -0700526 return request_key_and_link(type, description, callout_info,
527 callout_len, NULL, NULL,
528 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700529}
530EXPORT_SYMBOL(request_key_async);
531
532/*
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
537 */
538struct key *request_key_async_with_auxdata(struct key_type *type,
539 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700540 const void *callout_info,
541 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700542 void *aux)
543{
David Howells4a38e122008-04-29 01:01:24 -0700544 return request_key_and_link(type, description, callout_info,
545 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700546}
547EXPORT_SYMBOL(request_key_async_with_auxdata);