blob: 3e9b9eb1dd28b44de4bee2f7206ed560055c76b4 [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;
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 Howells47d804b2008-11-14 10:39:11 +110099 sprintf(uid_str, "%d", current_fsuid());
100 sprintf(gid_str, "%d", current_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 Howellsb6dff3e2008-11-14 10:39:16 +1100107 tsk->cred->thread_keyring ?
108 tsk->cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 prkey = 0;
111 if (tsk->signal->process_keyring)
112 prkey = tsk->signal->process_keyring->serial;
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 sprintf(keyring_str[1], "%d", prkey);
David Howells3e301482005-06-23 22:00:56 -0700115
116 if (tsk->signal->session_keyring) {
117 rcu_read_lock();
118 sskey = rcu_dereference(tsk->signal->session_keyring)->serial;
119 rcu_read_unlock();
David Howells76181c12007-10-16 23:29:46 -0700120 } else {
David Howellsb6dff3e2008-11-14 10:39:16 +1100121 sskey = tsk->cred->user->session_keyring->serial;
David Howells3e301482005-06-23 22:00:56 -0700122 }
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 sprintf(keyring_str[2], "%d", sskey);
125
126 /* set up a minimal environment */
127 i = 0;
128 envp[i++] = "HOME=/";
129 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
130 envp[i] = NULL;
131
132 /* set up the argument list */
133 i = 0;
134 argv[i++] = "/sbin/request-key";
135 argv[i++] = (char *) op;
136 argv[i++] = key_str;
137 argv[i++] = uid_str;
138 argv[i++] = gid_str;
139 argv[i++] = keyring_str[0];
140 argv[i++] = keyring_str[1];
141 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 argv[i] = NULL;
143
144 /* do it */
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700145 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
146 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700147 kdebug("usermode -> 0x%x", ret);
148 if (ret >= 0) {
149 /* ret is the exit/wait code */
150 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
151 key_validate(key) < 0)
152 ret = -ENOKEY;
153 else
154 /* ignore any errors from userspace if the key was
155 * instantiated */
156 ret = 0;
157 }
David Howells3e301482005-06-23 22:00:56 -0700158
David Howellsb5f545c2006-01-08 01:02:47 -0800159error_link:
160 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700161
David Howellsb5f545c2006-01-08 01:02:47 -0800162error_alloc:
David Howells3e301482005-06-23 22:00:56 -0700163 kleave(" = %d", ret);
David Howells76181c12007-10-16 23:29:46 -0700164 complete_request_key(cons, ret);
David Howells3e301482005-06-23 22:00:56 -0700165 return ret;
David Howells76181c12007-10-16 23:29:46 -0700166}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
David Howells76181c12007-10-16 23:29:46 -0700169 * call out to userspace for key construction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * - we ignore program failure and go on key status instead
171 */
David Howells4a38e122008-04-29 01:01:24 -0700172static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100173 size_t callout_len, void *aux,
174 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
David Howells76181c12007-10-16 23:29:46 -0700176 struct key_construction *cons;
David Howellsb5f545c2006-01-08 01:02:47 -0800177 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700178 struct key *authkey;
179 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
David Howells4a38e122008-04-29 01:01:24 -0700181 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700182
David Howells76181c12007-10-16 23:29:46 -0700183 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
184 if (!cons)
185 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howellsb5f545c2006-01-08 01:02:47 -0800187 /* allocate an authorisation key */
David Howells8bbf49762008-11-14 10:39:14 +1100188 authkey = request_key_auth_new(key, callout_info, callout_len,
189 dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800190 if (IS_ERR(authkey)) {
David Howells76181c12007-10-16 23:29:46 -0700191 kfree(cons);
David Howellsb5f545c2006-01-08 01:02:47 -0800192 ret = PTR_ERR(authkey);
193 authkey = NULL;
David Howells76181c12007-10-16 23:29:46 -0700194 } else {
195 cons->authkey = key_get(authkey);
196 cons->key = key_get(key);
197
198 /* make the call */
199 actor = call_sbin_request_key;
200 if (key->type->request_key)
201 actor = key->type->request_key;
202
203 ret = actor(cons, "create", aux);
204
205 /* check that the actor called complete_request_key() prior to
206 * returning an error */
207 WARN_ON(ret < 0 &&
208 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
209 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800210 }
211
David Howells76181c12007-10-16 23:29:46 -0700212 kleave(" = %d", ret);
213 return ret;
214}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
David Howells8bbf49762008-11-14 10:39:14 +1100217 * get the appropriate destination keyring for the request
218 * - we return whatever keyring we select with an extra reference upon it which
219 * the caller must release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 */
David Howells8bbf49762008-11-14 10:39:14 +1100221static void construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700222{
David Howells8bbf49762008-11-14 10:39:14 +1100223 struct request_key_auth *rka;
David Howells3e301482005-06-23 22:00:56 -0700224 struct task_struct *tsk = current;
David Howells8bbf49762008-11-14 10:39:14 +1100225 struct key *dest_keyring = *_dest_keyring, *authkey;
David Howells3e301482005-06-23 22:00:56 -0700226
David Howells8bbf49762008-11-14 10:39:14 +1100227 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700228
229 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100230 if (dest_keyring) {
231 /* the caller supplied one */
232 key_get(dest_keyring);
233 } else {
234 /* use a default keyring; falling through the cases until we
235 * find one that we actually have */
David Howellsb6dff3e2008-11-14 10:39:16 +1100236 switch (tsk->cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700237 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100238 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100239 if (tsk->cred->request_key_auth) {
240 authkey = tsk->cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100241 down_read(&authkey->sem);
242 rka = authkey->payload.data;
243 if (!test_bit(KEY_FLAG_REVOKED,
244 &authkey->flags))
245 dest_keyring =
246 key_get(rka->dest_keyring);
247 up_read(&authkey->sem);
248 if (dest_keyring)
249 break;
250 }
251
David Howells3e301482005-06-23 22:00:56 -0700252 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100253 dest_keyring = key_get(tsk->cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700254 if (dest_keyring)
255 break;
256
257 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howells8bbf49762008-11-14 10:39:14 +1100258 dest_keyring = key_get(tsk->signal->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700259 if (dest_keyring)
260 break;
261
262 case KEY_REQKEY_DEFL_SESSION_KEYRING:
263 rcu_read_lock();
264 dest_keyring = key_get(
265 rcu_dereference(tsk->signal->session_keyring));
266 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700267
268 if (dest_keyring)
269 break;
270
271 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100272 dest_keyring =
273 key_get(tsk->cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700274 break;
275
276 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100277 dest_keyring = key_get(tsk->cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700278 break;
279
280 case KEY_REQKEY_DEFL_GROUP_KEYRING:
281 default:
282 BUG();
283 }
284 }
285
David Howells8bbf49762008-11-14 10:39:14 +1100286 *_dest_keyring = dest_keyring;
287 kleave(" [dk %d]", key_serial(dest_keyring));
288 return;
David Howells76181c12007-10-16 23:29:46 -0700289}
David Howells3e301482005-06-23 22:00:56 -0700290
David Howells76181c12007-10-16 23:29:46 -0700291/*
292 * allocate a new key in under-construction state and attempt to link it in to
293 * the requested place
294 * - may return a key that's already under construction instead
295 */
296static int construct_alloc_key(struct key_type *type,
297 const char *description,
298 struct key *dest_keyring,
299 unsigned long flags,
300 struct key_user *user,
301 struct key **_key)
302{
303 struct key *key;
304 key_ref_t key_ref;
David Howells3e301482005-06-23 22:00:56 -0700305
David Howells76181c12007-10-16 23:29:46 -0700306 kenter("%s,%s,,,", type->name, description);
307
308 mutex_lock(&user->cons_lock);
309
310 key = key_alloc(type, description,
David Howells47d804b2008-11-14 10:39:11 +1100311 current_fsuid(), current_fsgid(), current, KEY_POS_ALL,
David Howells76181c12007-10-16 23:29:46 -0700312 flags);
313 if (IS_ERR(key))
314 goto alloc_failed;
315
316 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
317
David Howells8bbf49762008-11-14 10:39:14 +1100318 down_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700319
320 /* attach the key to the destination keyring under lock, but we do need
321 * to do another check just in case someone beat us to it whilst we
322 * waited for locks */
323 mutex_lock(&key_construction_mutex);
324
325 key_ref = search_process_keyrings(type, description, type->match,
326 current);
327 if (!IS_ERR(key_ref))
328 goto key_already_present;
329
David Howells8bbf49762008-11-14 10:39:14 +1100330 __key_link(dest_keyring, key);
David Howells76181c12007-10-16 23:29:46 -0700331
332 mutex_unlock(&key_construction_mutex);
David Howells8bbf49762008-11-14 10:39:14 +1100333 up_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700334 mutex_unlock(&user->cons_lock);
335 *_key = key;
336 kleave(" = 0 [%d]", key_serial(key));
337 return 0;
338
339key_already_present:
340 mutex_unlock(&key_construction_mutex);
341 if (dest_keyring)
342 up_write(&dest_keyring->sem);
343 mutex_unlock(&user->cons_lock);
344 key_put(key);
345 *_key = key = key_ref_to_ptr(key_ref);
346 kleave(" = -EINPROGRESS [%d]", key_serial(key));
347 return -EINPROGRESS;
348
349alloc_failed:
350 mutex_unlock(&user->cons_lock);
351 *_key = NULL;
352 kleave(" = %ld", PTR_ERR(key));
353 return PTR_ERR(key);
354}
355
356/*
357 * commence key construction
358 */
359static struct key *construct_key_and_link(struct key_type *type,
360 const char *description,
361 const char *callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700362 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700363 void *aux,
364 struct key *dest_keyring,
365 unsigned long flags)
366{
367 struct key_user *user;
368 struct key *key;
369 int ret;
370
David Howells47d804b2008-11-14 10:39:11 +1100371 user = key_user_lookup(current_fsuid());
David Howells76181c12007-10-16 23:29:46 -0700372 if (!user)
373 return ERR_PTR(-ENOMEM);
374
David Howells8bbf49762008-11-14 10:39:14 +1100375 construct_get_dest_keyring(&dest_keyring);
376
David Howells76181c12007-10-16 23:29:46 -0700377 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
378 &key);
379 key_user_put(user);
380
381 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100382 ret = construct_key(key, callout_info, callout_len, aux,
383 dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700384 if (ret < 0)
385 goto construction_failed;
386 }
387
David Howells8bbf49762008-11-14 10:39:14 +1100388 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700389 return key;
390
391construction_failed:
392 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
393 key_put(key);
David Howells8bbf49762008-11-14 10:39:14 +1100394 key_put(dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700395 return ERR_PTR(ret);
396}
397
David Howells3e301482005-06-23 22:00:56 -0700398/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 * request a key
400 * - search the process's keyrings
401 * - check the list of keys being created or updated
David Howells3e301482005-06-23 22:00:56 -0700402 * - call out to userspace for a key if supplementary info was provided
403 * - cache the key in an appropriate keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 */
David Howells3e301482005-06-23 22:00:56 -0700405struct key *request_key_and_link(struct key_type *type,
406 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700407 const void *callout_info,
408 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700409 void *aux,
David Howells7e047ef2006-06-26 00:24:50 -0700410 struct key *dest_keyring,
411 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100414 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
David Howells4a38e122008-04-29 01:01:24 -0700416 kenter("%s,%s,%p,%zu,%p,%p,%lx",
417 type->name, description, callout_info, callout_len, aux,
David Howells4e54f082006-06-29 02:24:28 -0700418 dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* search all the process keyrings for a key */
David Howells664cceb2005-09-28 17:03:15 +0100421 key_ref = search_process_keyrings(type, description, type->match,
422 current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David Howells664cceb2005-09-28 17:03:15 +0100424 if (!IS_ERR(key_ref)) {
425 key = key_ref_to_ptr(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700426 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800427 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700428 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* the search failed, but the keyrings were searchable, so we
430 * should consult userspace if we can */
431 key = ERR_PTR(-ENOKEY);
432 if (!callout_info)
433 goto error;
434
David Howells76181c12007-10-16 23:29:46 -0700435 key = construct_key_and_link(type, description, callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700436 callout_len, aux, dest_keyring,
437 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
David Howells3e301482005-06-23 22:00:56 -0700440error:
441 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return key;
David Howells76181c12007-10-16 23:29:46 -0700443}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells76181c12007-10-16 23:29:46 -0700445/*
446 * wait for construction of a key to complete
447 */
448int wait_for_key_construction(struct key *key, bool intr)
449{
450 int ret;
David Howells3e301482005-06-23 22:00:56 -0700451
David Howells76181c12007-10-16 23:29:46 -0700452 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
453 intr ? key_wait_bit_intr : key_wait_bit,
454 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
455 if (ret < 0)
456 return ret;
457 return key_validate(key);
458}
459EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700460
David Howells3e301482005-06-23 22:00:56 -0700461/*
462 * request a key
463 * - search the process's keyrings
464 * - check the list of keys being created or updated
465 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700466 * - waits uninterruptible for creation to complete
David Howells3e301482005-06-23 22:00:56 -0700467 */
468struct key *request_key(struct key_type *type,
469 const char *description,
470 const char *callout_info)
471{
David Howells76181c12007-10-16 23:29:46 -0700472 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700473 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700474 int ret;
David Howells3e301482005-06-23 22:00:56 -0700475
David Howells4a38e122008-04-29 01:01:24 -0700476 if (callout_info)
477 callout_len = strlen(callout_info);
478 key = request_key_and_link(type, description, callout_info, callout_len,
479 NULL, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700480 if (!IS_ERR(key)) {
481 ret = wait_for_key_construction(key, false);
482 if (ret < 0) {
483 key_put(key);
484 return ERR_PTR(ret);
485 }
486 }
487 return key;
488}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700490
David Howells4e54f082006-06-29 02:24:28 -0700491/*
492 * request a key with auxiliary data for the upcaller
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
David Howells76181c12007-10-16 23:29:46 -0700496 * - waits uninterruptible for creation to complete
David Howells4e54f082006-06-29 02:24:28 -0700497 */
498struct key *request_key_with_auxdata(struct key_type *type,
499 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700500 const void *callout_info,
501 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700502 void *aux)
503{
David Howells76181c12007-10-16 23:29:46 -0700504 struct key *key;
505 int ret;
506
David Howells4a38e122008-04-29 01:01:24 -0700507 key = request_key_and_link(type, description, callout_info, callout_len,
508 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700509 if (!IS_ERR(key)) {
510 ret = wait_for_key_construction(key, false);
511 if (ret < 0) {
512 key_put(key);
513 return ERR_PTR(ret);
514 }
515 }
516 return key;
517}
518EXPORT_SYMBOL(request_key_with_auxdata);
519
520/*
521 * request a key (allow async construction)
522 * - search the process's keyrings
523 * - check the list of keys being created or updated
524 * - call out to userspace for a key if supplementary info was provided
525 */
526struct key *request_key_async(struct key_type *type,
527 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700528 const void *callout_info,
529 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700530{
David Howells4a38e122008-04-29 01:01:24 -0700531 return request_key_and_link(type, description, callout_info,
532 callout_len, NULL, NULL,
533 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700534}
535EXPORT_SYMBOL(request_key_async);
536
537/*
538 * request a key with auxiliary data for the upcaller (allow async construction)
539 * - search the process's keyrings
540 * - check the list of keys being created or updated
541 * - call out to userspace for a key if supplementary info was provided
542 */
543struct key *request_key_async_with_auxdata(struct key_type *type,
544 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700545 const void *callout_info,
546 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700547 void *aux)
548{
David Howells4a38e122008-04-29 01:01:24 -0700549 return request_key_and_link(type, description, callout_info,
550 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700551}
552EXPORT_SYMBOL(request_key_async_with_auxdata);