blob: 0e04f72ef2d4339c8ac50691d0171e138dc11df2 [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 Howellsd84f4f92008-11-14 10:39:23 +110086 cred = get_current_cred();
87 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
David Howells7e047ef2006-06-26 00:24:50 -070088 KEY_ALLOC_QUOTA_OVERRUN, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +110089 put_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -080090 if (IS_ERR(keyring)) {
91 ret = PTR_ERR(keyring);
92 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -070093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
David Howellsb5f545c2006-01-08 01:02:47 -080095 /* attach the auth key to the session keyring */
96 ret = __key_link(keyring, authkey);
97 if (ret < 0)
98 goto error_link;
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 /* record the UID and GID */
David Howells86a264a2008-11-14 10:39:18 +1100101 sprintf(uid_str, "%d", cred->fsuid);
102 sprintf(gid_str, "%d", cred->fsgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 /* we say which key is under construction */
105 sprintf(key_str, "%d", key->serial);
106
107 /* we specify the process's default keyrings */
108 sprintf(keyring_str[0], "%d",
David Howellsd84f4f92008-11-14 10:39:23 +1100109 cred->thread_keyring ? cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 prkey = 0;
David Howellsbb952bb2008-11-14 10:39:20 +1100112 if (cred->tgcred->process_keyring)
113 prkey = cred->tgcred->process_keyring->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
David Howellsbb952bb2008-11-14 10:39:20 +1100115 if (cred->tgcred->session_keyring)
116 sskey = rcu_dereference(cred->tgcred->session_keyring)->serial;
117 else
David Howells86a264a2008-11-14 10:39:18 +1100118 sskey = cred->user->session_keyring->serial;
David Howells3e301482005-06-23 22:00:56 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 sprintf(keyring_str[2], "%d", sskey);
121
122 /* set up a minimal environment */
123 i = 0;
124 envp[i++] = "HOME=/";
125 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
126 envp[i] = NULL;
127
128 /* set up the argument list */
129 i = 0;
130 argv[i++] = "/sbin/request-key";
131 argv[i++] = (char *) op;
132 argv[i++] = key_str;
133 argv[i++] = uid_str;
134 argv[i++] = gid_str;
135 argv[i++] = keyring_str[0];
136 argv[i++] = keyring_str[1];
137 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 argv[i] = NULL;
139
140 /* do it */
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700141 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
142 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700143 kdebug("usermode -> 0x%x", ret);
144 if (ret >= 0) {
145 /* ret is the exit/wait code */
146 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
147 key_validate(key) < 0)
148 ret = -ENOKEY;
149 else
150 /* ignore any errors from userspace if the key was
151 * instantiated */
152 ret = 0;
153 }
David Howells3e301482005-06-23 22:00:56 -0700154
David Howellsb5f545c2006-01-08 01:02:47 -0800155error_link:
156 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700157
David Howellsb5f545c2006-01-08 01:02:47 -0800158error_alloc:
David Howells76181c12007-10-16 23:29:46 -0700159 complete_request_key(cons, ret);
David Howellsd84f4f92008-11-14 10:39:23 +1100160 kleave(" = %d", ret);
David Howells3e301482005-06-23 22:00:56 -0700161 return ret;
David Howells76181c12007-10-16 23:29:46 -0700162}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
David Howells76181c12007-10-16 23:29:46 -0700165 * call out to userspace for key construction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 * - we ignore program failure and go on key status instead
167 */
David Howells4a38e122008-04-29 01:01:24 -0700168static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100169 size_t callout_len, void *aux,
170 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
David Howells76181c12007-10-16 23:29:46 -0700172 struct key_construction *cons;
David Howellsb5f545c2006-01-08 01:02:47 -0800173 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700174 struct key *authkey;
175 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
David Howells4a38e122008-04-29 01:01:24 -0700177 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700178
David Howells76181c12007-10-16 23:29:46 -0700179 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
180 if (!cons)
181 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
David Howellsb5f545c2006-01-08 01:02:47 -0800183 /* allocate an authorisation key */
David Howells8bbf49762008-11-14 10:39:14 +1100184 authkey = request_key_auth_new(key, callout_info, callout_len,
185 dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800186 if (IS_ERR(authkey)) {
David Howells76181c12007-10-16 23:29:46 -0700187 kfree(cons);
David Howellsb5f545c2006-01-08 01:02:47 -0800188 ret = PTR_ERR(authkey);
189 authkey = NULL;
David Howells76181c12007-10-16 23:29:46 -0700190 } else {
191 cons->authkey = key_get(authkey);
192 cons->key = key_get(key);
193
194 /* make the call */
195 actor = call_sbin_request_key;
196 if (key->type->request_key)
197 actor = key->type->request_key;
198
199 ret = actor(cons, "create", aux);
200
201 /* check that the actor called complete_request_key() prior to
202 * returning an error */
203 WARN_ON(ret < 0 &&
204 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
205 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800206 }
207
David Howells76181c12007-10-16 23:29:46 -0700208 kleave(" = %d", ret);
209 return ret;
210}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
David Howells8bbf49762008-11-14 10:39:14 +1100213 * get the appropriate destination keyring for the request
214 * - we return whatever keyring we select with an extra reference upon it which
215 * the caller must release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 */
David Howells8bbf49762008-11-14 10:39:14 +1100217static void construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700218{
David Howells8bbf49762008-11-14 10:39:14 +1100219 struct request_key_auth *rka;
David Howellsbb952bb2008-11-14 10:39:20 +1100220 const struct cred *cred = current_cred();
David Howells8bbf49762008-11-14 10:39:14 +1100221 struct key *dest_keyring = *_dest_keyring, *authkey;
David Howells3e301482005-06-23 22:00:56 -0700222
David Howells8bbf49762008-11-14 10:39:14 +1100223 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700224
225 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100226 if (dest_keyring) {
227 /* the caller supplied one */
228 key_get(dest_keyring);
229 } else {
230 /* use a default keyring; falling through the cases until we
231 * find one that we actually have */
David Howellsbb952bb2008-11-14 10:39:20 +1100232 switch (cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700233 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100234 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100235 if (cred->request_key_auth) {
236 authkey = cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100237 down_read(&authkey->sem);
238 rka = authkey->payload.data;
239 if (!test_bit(KEY_FLAG_REVOKED,
240 &authkey->flags))
241 dest_keyring =
242 key_get(rka->dest_keyring);
243 up_read(&authkey->sem);
244 if (dest_keyring)
245 break;
246 }
247
David Howells3e301482005-06-23 22:00:56 -0700248 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100249 dest_keyring = key_get(cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700250 if (dest_keyring)
251 break;
252
253 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100254 dest_keyring = key_get(cred->tgcred->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700255 if (dest_keyring)
256 break;
257
258 case KEY_REQKEY_DEFL_SESSION_KEYRING:
259 rcu_read_lock();
260 dest_keyring = key_get(
David Howellsbb952bb2008-11-14 10:39:20 +1100261 rcu_dereference(cred->tgcred->session_keyring));
David Howells3e301482005-06-23 22:00:56 -0700262 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700263
264 if (dest_keyring)
265 break;
266
267 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100268 dest_keyring =
David Howellsbb952bb2008-11-14 10:39:20 +1100269 key_get(cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700270 break;
271
272 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100273 dest_keyring = key_get(cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700274 break;
275
276 case KEY_REQKEY_DEFL_GROUP_KEYRING:
277 default:
278 BUG();
279 }
280 }
281
David Howells8bbf49762008-11-14 10:39:14 +1100282 *_dest_keyring = dest_keyring;
283 kleave(" [dk %d]", key_serial(dest_keyring));
284 return;
David Howells76181c12007-10-16 23:29:46 -0700285}
David Howells3e301482005-06-23 22:00:56 -0700286
David Howells76181c12007-10-16 23:29:46 -0700287/*
288 * allocate a new key in under-construction state and attempt to link it in to
289 * the requested place
290 * - may return a key that's already under construction instead
291 */
292static int construct_alloc_key(struct key_type *type,
293 const char *description,
294 struct key *dest_keyring,
295 unsigned long flags,
296 struct key_user *user,
297 struct key **_key)
298{
David Howellsd84f4f92008-11-14 10:39:23 +1100299 const struct cred *cred = current_cred();
David Howells76181c12007-10-16 23:29:46 -0700300 struct key *key;
301 key_ref_t key_ref;
David Howells3e301482005-06-23 22:00:56 -0700302
David Howells76181c12007-10-16 23:29:46 -0700303 kenter("%s,%s,,,", type->name, description);
304
305 mutex_lock(&user->cons_lock);
306
David Howellsd84f4f92008-11-14 10:39:23 +1100307 key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
308 KEY_POS_ALL, flags);
David Howells76181c12007-10-16 23:29:46 -0700309 if (IS_ERR(key))
310 goto alloc_failed;
311
312 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
313
David Howells8bbf49762008-11-14 10:39:14 +1100314 down_write(&dest_keyring->sem);
David Howells76181c12007-10-16 23:29:46 -0700315
316 /* attach the key to the destination keyring under lock, but we do need
317 * to do another check just in case someone beat us to it whilst we
318 * waited for locks */
319 mutex_lock(&key_construction_mutex);
320
David Howellsd84f4f92008-11-14 10:39:23 +1100321 key_ref = search_process_keyrings(type, description, type->match, cred);
David Howells76181c12007-10-16 23:29:46 -0700322 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 Howellsd84f4f92008-11-14 10:39:23 +1100366 kenter("");
367
David Howells47d804b2008-11-14 10:39:11 +1100368 user = key_user_lookup(current_fsuid());
David Howells76181c12007-10-16 23:29:46 -0700369 if (!user)
370 return ERR_PTR(-ENOMEM);
371
David Howells8bbf49762008-11-14 10:39:14 +1100372 construct_get_dest_keyring(&dest_keyring);
373
David Howells76181c12007-10-16 23:29:46 -0700374 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
375 &key);
376 key_user_put(user);
377
378 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100379 ret = construct_key(key, callout_info, callout_len, aux,
380 dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100381 if (ret < 0) {
382 kdebug("cons failed");
David Howells76181c12007-10-16 23:29:46 -0700383 goto construction_failed;
David Howellsd84f4f92008-11-14 10:39:23 +1100384 }
David Howells76181c12007-10-16 23:29:46 -0700385 }
386
David Howells8bbf49762008-11-14 10:39:14 +1100387 key_put(dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100388 kleave(" = key %d", key_serial(key));
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 Howellsd84f4f92008-11-14 10:39:23 +1100395 kleave(" = %d", ret);
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{
David Howellsd84f4f92008-11-14 10:39:23 +1100414 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100416 key_ref_t key_ref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
David Howells4a38e122008-04-29 01:01:24 -0700418 kenter("%s,%s,%p,%zu,%p,%p,%lx",
419 type->name, description, callout_info, callout_len, aux,
David Howells4e54f082006-06-29 02:24:28 -0700420 dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 /* search all the process keyrings for a key */
David Howells664cceb2005-09-28 17:03:15 +0100423 key_ref = search_process_keyrings(type, description, type->match,
David Howellsd84f4f92008-11-14 10:39:23 +1100424 cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
David Howells664cceb2005-09-28 17:03:15 +0100426 if (!IS_ERR(key_ref)) {
427 key = key_ref_to_ptr(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700428 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800429 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700430 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* the search failed, but the keyrings were searchable, so we
432 * should consult userspace if we can */
433 key = ERR_PTR(-ENOKEY);
434 if (!callout_info)
435 goto error;
436
David Howells76181c12007-10-16 23:29:46 -0700437 key = construct_key_and_link(type, description, callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700438 callout_len, aux, dest_keyring,
439 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
David Howells3e301482005-06-23 22:00:56 -0700442error:
443 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 return key;
David Howells76181c12007-10-16 23:29:46 -0700445}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
David Howells76181c12007-10-16 23:29:46 -0700447/*
448 * wait for construction of a key to complete
449 */
450int wait_for_key_construction(struct key *key, bool intr)
451{
452 int ret;
David Howells3e301482005-06-23 22:00:56 -0700453
David Howells76181c12007-10-16 23:29:46 -0700454 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
455 intr ? key_wait_bit_intr : key_wait_bit,
456 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
457 if (ret < 0)
458 return ret;
459 return key_validate(key);
460}
461EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700462
David Howells3e301482005-06-23 22:00:56 -0700463/*
464 * request a key
465 * - search the process's keyrings
466 * - check the list of keys being created or updated
467 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700468 * - waits uninterruptible for creation to complete
David Howells3e301482005-06-23 22:00:56 -0700469 */
470struct key *request_key(struct key_type *type,
471 const char *description,
472 const char *callout_info)
473{
David Howells76181c12007-10-16 23:29:46 -0700474 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700475 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700476 int ret;
David Howells3e301482005-06-23 22:00:56 -0700477
David Howells4a38e122008-04-29 01:01:24 -0700478 if (callout_info)
479 callout_len = strlen(callout_info);
480 key = request_key_and_link(type, description, callout_info, callout_len,
481 NULL, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700482 if (!IS_ERR(key)) {
483 ret = wait_for_key_construction(key, false);
484 if (ret < 0) {
485 key_put(key);
486 return ERR_PTR(ret);
487 }
488 }
489 return key;
490}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700492
David Howells4e54f082006-06-29 02:24:28 -0700493/*
494 * request a key with auxiliary data for the upcaller
495 * - search the process's keyrings
496 * - check the list of keys being created or updated
497 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700498 * - waits uninterruptible for creation to complete
David Howells4e54f082006-06-29 02:24:28 -0700499 */
500struct key *request_key_with_auxdata(struct key_type *type,
501 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700502 const void *callout_info,
503 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700504 void *aux)
505{
David Howells76181c12007-10-16 23:29:46 -0700506 struct key *key;
507 int ret;
508
David Howells4a38e122008-04-29 01:01:24 -0700509 key = request_key_and_link(type, description, callout_info, callout_len,
510 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700511 if (!IS_ERR(key)) {
512 ret = wait_for_key_construction(key, false);
513 if (ret < 0) {
514 key_put(key);
515 return ERR_PTR(ret);
516 }
517 }
518 return key;
519}
520EXPORT_SYMBOL(request_key_with_auxdata);
521
522/*
523 * request a key (allow async construction)
524 * - search the process's keyrings
525 * - check the list of keys being created or updated
526 * - call out to userspace for a key if supplementary info was provided
527 */
528struct key *request_key_async(struct key_type *type,
529 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700530 const void *callout_info,
531 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700532{
David Howells4a38e122008-04-29 01:01:24 -0700533 return request_key_and_link(type, description, callout_info,
534 callout_len, NULL, NULL,
535 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700536}
537EXPORT_SYMBOL(request_key_async);
538
539/*
540 * request a key with auxiliary data for the upcaller (allow async construction)
541 * - search the process's keyrings
542 * - check the list of keys being created or updated
543 * - call out to userspace for a key if supplementary info was provided
544 */
545struct key *request_key_async_with_auxdata(struct key_type *type,
546 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700547 const void *callout_info,
548 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700549 void *aux)
550{
David Howells4a38e122008-04-29 01:01:24 -0700551 return request_key_and_link(type, description, callout_info,
552 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700553}
554EXPORT_SYMBOL(request_key_async_with_auxdata);