blob: f656e9c069e3ec31f64d8a4a2ec9d9add862a4cb [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 Howells93b4a442010-04-23 13:18:00 -040071 struct key *key = cons->key, *authkey = cons->authkey, *keyring,
72 *session;
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 Howellsd84f4f92008-11-14 10:39:23 +110087 cred = get_current_cred();
88 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
David Howells7e047ef2006-06-26 00:24:50 -070089 KEY_ALLOC_QUOTA_OVERRUN, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +110090 put_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -080091 if (IS_ERR(keyring)) {
92 ret = PTR_ERR(keyring);
93 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -070094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
David Howellsb5f545c2006-01-08 01:02:47 -080096 /* attach the auth key to the session keyring */
David Howells896903c2010-04-30 14:32:23 +010097 ret = key_link(keyring, authkey);
David Howellsb5f545c2006-01-08 01:02:47 -080098 if (ret < 0)
99 goto error_link;
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* record the UID and GID */
David Howells86a264a2008-11-14 10:39:18 +1100102 sprintf(uid_str, "%d", cred->fsuid);
103 sprintf(gid_str, "%d", cred->fsgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 /* we say which key is under construction */
106 sprintf(key_str, "%d", key->serial);
107
108 /* we specify the process's default keyrings */
109 sprintf(keyring_str[0], "%d",
David Howellsd84f4f92008-11-14 10:39:23 +1100110 cred->thread_keyring ? cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 prkey = 0;
David Howellsbb952bb2008-11-14 10:39:20 +1100113 if (cred->tgcred->process_keyring)
114 prkey = cred->tgcred->process_keyring->serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
David Howells93b4a442010-04-23 13:18:00 -0400116 rcu_read_lock();
117 session = rcu_dereference(cred->tgcred->session_keyring);
118 if (!session)
119 session = cred->user->session_keyring;
120 sskey = session->serial;
121 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 sprintf(keyring_str[2], "%d", sskey);
124
125 /* set up a minimal environment */
126 i = 0;
127 envp[i++] = "HOME=/";
128 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
129 envp[i] = NULL;
130
131 /* set up the argument list */
132 i = 0;
133 argv[i++] = "/sbin/request-key";
134 argv[i++] = (char *) op;
135 argv[i++] = key_str;
136 argv[i++] = uid_str;
137 argv[i++] = gid_str;
138 argv[i++] = keyring_str[0];
139 argv[i++] = keyring_str[1];
140 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 argv[i] = NULL;
142
143 /* do it */
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700144 ret = call_usermodehelper_keys(argv[0], argv, envp, keyring,
145 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700146 kdebug("usermode -> 0x%x", ret);
147 if (ret >= 0) {
148 /* ret is the exit/wait code */
149 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
150 key_validate(key) < 0)
151 ret = -ENOKEY;
152 else
153 /* ignore any errors from userspace if the key was
154 * instantiated */
155 ret = 0;
156 }
David Howells3e301482005-06-23 22:00:56 -0700157
David Howellsb5f545c2006-01-08 01:02:47 -0800158error_link:
159 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700160
David Howellsb5f545c2006-01-08 01:02:47 -0800161error_alloc:
David Howells76181c12007-10-16 23:29:46 -0700162 complete_request_key(cons, ret);
David Howellsd84f4f92008-11-14 10:39:23 +1100163 kleave(" = %d", ret);
David Howells3e301482005-06-23 22:00:56 -0700164 return ret;
David Howells76181c12007-10-16 23:29:46 -0700165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
David Howells76181c12007-10-16 23:29:46 -0700168 * call out to userspace for key construction
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 * - we ignore program failure and go on key status instead
170 */
David Howells4a38e122008-04-29 01:01:24 -0700171static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100172 size_t callout_len, void *aux,
173 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
David Howells76181c12007-10-16 23:29:46 -0700175 struct key_construction *cons;
David Howellsb5f545c2006-01-08 01:02:47 -0800176 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700177 struct key *authkey;
178 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
David Howells4a38e122008-04-29 01:01:24 -0700180 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700181
David Howells76181c12007-10-16 23:29:46 -0700182 cons = kmalloc(sizeof(*cons), GFP_KERNEL);
183 if (!cons)
184 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howellsb5f545c2006-01-08 01:02:47 -0800186 /* allocate an authorisation key */
David Howells8bbf49762008-11-14 10:39:14 +1100187 authkey = request_key_auth_new(key, callout_info, callout_len,
188 dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800189 if (IS_ERR(authkey)) {
David Howells76181c12007-10-16 23:29:46 -0700190 kfree(cons);
David Howellsb5f545c2006-01-08 01:02:47 -0800191 ret = PTR_ERR(authkey);
192 authkey = NULL;
David Howells76181c12007-10-16 23:29:46 -0700193 } else {
194 cons->authkey = key_get(authkey);
195 cons->key = key_get(key);
196
197 /* make the call */
198 actor = call_sbin_request_key;
199 if (key->type->request_key)
200 actor = key->type->request_key;
201
202 ret = actor(cons, "create", aux);
203
204 /* check that the actor called complete_request_key() prior to
205 * returning an error */
206 WARN_ON(ret < 0 &&
207 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
208 key_put(authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800209 }
210
David Howells76181c12007-10-16 23:29:46 -0700211 kleave(" = %d", ret);
212 return ret;
213}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/*
David Howells8bbf49762008-11-14 10:39:14 +1100216 * get the appropriate destination keyring for the request
217 * - we return whatever keyring we select with an extra reference upon it which
218 * the caller must release
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 */
David Howells8bbf49762008-11-14 10:39:14 +1100220static void construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700221{
David Howells8bbf49762008-11-14 10:39:14 +1100222 struct request_key_auth *rka;
David Howellsbb952bb2008-11-14 10:39:20 +1100223 const struct cred *cred = current_cred();
David Howells8bbf49762008-11-14 10:39:14 +1100224 struct key *dest_keyring = *_dest_keyring, *authkey;
David Howells3e301482005-06-23 22:00:56 -0700225
David Howells8bbf49762008-11-14 10:39:14 +1100226 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700227
228 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100229 if (dest_keyring) {
230 /* the caller supplied one */
231 key_get(dest_keyring);
232 } else {
233 /* use a default keyring; falling through the cases until we
234 * find one that we actually have */
David Howellsbb952bb2008-11-14 10:39:20 +1100235 switch (cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700236 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100237 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100238 if (cred->request_key_auth) {
239 authkey = cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100240 down_read(&authkey->sem);
241 rka = authkey->payload.data;
242 if (!test_bit(KEY_FLAG_REVOKED,
243 &authkey->flags))
244 dest_keyring =
245 key_get(rka->dest_keyring);
246 up_read(&authkey->sem);
247 if (dest_keyring)
248 break;
249 }
250
David Howells3e301482005-06-23 22:00:56 -0700251 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100252 dest_keyring = key_get(cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700253 if (dest_keyring)
254 break;
255
256 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100257 dest_keyring = key_get(cred->tgcred->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700258 if (dest_keyring)
259 break;
260
261 case KEY_REQKEY_DEFL_SESSION_KEYRING:
262 rcu_read_lock();
263 dest_keyring = key_get(
David Howellsbb952bb2008-11-14 10:39:20 +1100264 rcu_dereference(cred->tgcred->session_keyring));
David Howells3e301482005-06-23 22:00:56 -0700265 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700266
267 if (dest_keyring)
268 break;
269
270 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100271 dest_keyring =
David Howellsbb952bb2008-11-14 10:39:20 +1100272 key_get(cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700273 break;
274
275 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100276 dest_keyring = key_get(cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700277 break;
278
279 case KEY_REQKEY_DEFL_GROUP_KEYRING:
280 default:
281 BUG();
282 }
283 }
284
David Howells8bbf49762008-11-14 10:39:14 +1100285 *_dest_keyring = dest_keyring;
286 kleave(" [dk %d]", key_serial(dest_keyring));
287 return;
David Howells76181c12007-10-16 23:29:46 -0700288}
David Howells3e301482005-06-23 22:00:56 -0700289
David Howells76181c12007-10-16 23:29:46 -0700290/*
291 * allocate a new key in under-construction state and attempt to link it in to
292 * the requested place
293 * - may return a key that's already under construction instead
294 */
295static int construct_alloc_key(struct key_type *type,
296 const char *description,
297 struct key *dest_keyring,
298 unsigned long flags,
299 struct key_user *user,
300 struct key **_key)
301{
David Howellsf70e2e02010-04-30 14:32:39 +0100302 struct keyring_list *prealloc;
David Howellsd84f4f92008-11-14 10:39:23 +1100303 const struct cred *cred = current_cred();
David Howells76181c12007-10-16 23:29:46 -0700304 struct key *key;
305 key_ref_t key_ref;
David Howells2b9e4682010-04-30 14:32:34 +0100306 int ret;
David Howells3e301482005-06-23 22:00:56 -0700307
David Howells76181c12007-10-16 23:29:46 -0700308 kenter("%s,%s,,,", type->name, description);
309
David Howellsf70e2e02010-04-30 14:32:39 +0100310 *_key = NULL;
David Howells76181c12007-10-16 23:29:46 -0700311 mutex_lock(&user->cons_lock);
312
David Howellsd84f4f92008-11-14 10:39:23 +1100313 key = key_alloc(type, description, cred->fsuid, cred->fsgid, cred,
314 KEY_POS_ALL, flags);
David Howells76181c12007-10-16 23:29:46 -0700315 if (IS_ERR(key))
316 goto alloc_failed;
317
318 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
319
David Howellsf70e2e02010-04-30 14:32:39 +0100320 if (dest_keyring) {
321 ret = __key_link_begin(dest_keyring, type, description,
322 &prealloc);
323 if (ret < 0)
324 goto link_prealloc_failed;
325 }
David Howells76181c12007-10-16 23:29:46 -0700326
327 /* attach the key to the destination keyring under lock, but we do need
328 * to do another check just in case someone beat us to it whilst we
329 * waited for locks */
330 mutex_lock(&key_construction_mutex);
331
David Howellsd84f4f92008-11-14 10:39:23 +1100332 key_ref = search_process_keyrings(type, description, type->match, cred);
David Howells76181c12007-10-16 23:29:46 -0700333 if (!IS_ERR(key_ref))
334 goto key_already_present;
335
David Howells34574dd2009-04-09 17:14:05 +0100336 if (dest_keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100337 __key_link(dest_keyring, key, &prealloc);
David Howells76181c12007-10-16 23:29:46 -0700338
339 mutex_unlock(&key_construction_mutex);
David Howells34574dd2009-04-09 17:14:05 +0100340 if (dest_keyring)
David Howellsf70e2e02010-04-30 14:32:39 +0100341 __key_link_end(dest_keyring, type, prealloc);
David Howells76181c12007-10-16 23:29:46 -0700342 mutex_unlock(&user->cons_lock);
343 *_key = key;
344 kleave(" = 0 [%d]", key_serial(key));
345 return 0;
346
David Howells2b9e4682010-04-30 14:32:34 +0100347 /* the key is now present - we tell the caller that we found it by
348 * returning -EINPROGRESS */
David Howells76181c12007-10-16 23:29:46 -0700349key_already_present:
David Howellsf70e2e02010-04-30 14:32:39 +0100350 key_put(key);
David Howells76181c12007-10-16 23:29:46 -0700351 mutex_unlock(&key_construction_mutex);
David Howellsf70e2e02010-04-30 14:32:39 +0100352 key = key_ref_to_ptr(key_ref);
David Howells03449cd2010-04-27 13:13:08 -0700353 if (dest_keyring) {
David Howellsf70e2e02010-04-30 14:32:39 +0100354 ret = __key_link_check_live_key(dest_keyring, key);
355 if (ret == 0)
356 __key_link(dest_keyring, key, &prealloc);
357 __key_link_end(dest_keyring, type, prealloc);
358 if (ret < 0)
359 goto link_check_failed;
David Howells03449cd2010-04-27 13:13:08 -0700360 }
David Howells76181c12007-10-16 23:29:46 -0700361 mutex_unlock(&user->cons_lock);
David Howellsf70e2e02010-04-30 14:32:39 +0100362 *_key = key;
David Howells76181c12007-10-16 23:29:46 -0700363 kleave(" = -EINPROGRESS [%d]", key_serial(key));
364 return -EINPROGRESS;
365
David Howellsf70e2e02010-04-30 14:32:39 +0100366link_check_failed:
367 mutex_unlock(&user->cons_lock);
368 key_put(key);
369 kleave(" = %d [linkcheck]", ret);
370 return ret;
371
372link_prealloc_failed:
373 up_write(&dest_keyring->sem);
374 mutex_unlock(&user->cons_lock);
375 kleave(" = %d [prelink]", ret);
376 return ret;
377
David Howells76181c12007-10-16 23:29:46 -0700378alloc_failed:
379 mutex_unlock(&user->cons_lock);
David Howells76181c12007-10-16 23:29:46 -0700380 kleave(" = %ld", PTR_ERR(key));
381 return PTR_ERR(key);
382}
383
384/*
385 * commence key construction
386 */
387static struct key *construct_key_and_link(struct key_type *type,
388 const char *description,
389 const char *callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700390 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700391 void *aux,
392 struct key *dest_keyring,
393 unsigned long flags)
394{
395 struct key_user *user;
396 struct key *key;
397 int ret;
398
David Howellsd84f4f92008-11-14 10:39:23 +1100399 kenter("");
400
Serge E. Hallyn1d1e9752009-02-26 18:27:38 -0600401 user = key_user_lookup(current_fsuid(), current_user_ns());
David Howells76181c12007-10-16 23:29:46 -0700402 if (!user)
403 return ERR_PTR(-ENOMEM);
404
David Howells8bbf49762008-11-14 10:39:14 +1100405 construct_get_dest_keyring(&dest_keyring);
406
David Howells76181c12007-10-16 23:29:46 -0700407 ret = construct_alloc_key(type, description, dest_keyring, flags, user,
408 &key);
409 key_user_put(user);
410
411 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100412 ret = construct_key(key, callout_info, callout_len, aux,
413 dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100414 if (ret < 0) {
415 kdebug("cons failed");
David Howells76181c12007-10-16 23:29:46 -0700416 goto construction_failed;
David Howellsd84f4f92008-11-14 10:39:23 +1100417 }
David Howells2b9e4682010-04-30 14:32:34 +0100418 } else if (ret == -EINPROGRESS) {
419 ret = 0;
420 } else {
421 key = ERR_PTR(ret);
David Howells76181c12007-10-16 23:29:46 -0700422 }
423
David Howells8bbf49762008-11-14 10:39:14 +1100424 key_put(dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100425 kleave(" = key %d", key_serial(key));
David Howells76181c12007-10-16 23:29:46 -0700426 return key;
427
428construction_failed:
429 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
430 key_put(key);
David Howells8bbf49762008-11-14 10:39:14 +1100431 key_put(dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100432 kleave(" = %d", ret);
David Howells76181c12007-10-16 23:29:46 -0700433 return ERR_PTR(ret);
434}
435
David Howells3e301482005-06-23 22:00:56 -0700436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * request a key
438 * - search the process's keyrings
439 * - check the list of keys being created or updated
David Howells3e301482005-06-23 22:00:56 -0700440 * - call out to userspace for a key if supplementary info was provided
441 * - cache the key in an appropriate keyring
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 */
David Howells3e301482005-06-23 22:00:56 -0700443struct key *request_key_and_link(struct key_type *type,
444 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700445 const void *callout_info,
446 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700447 void *aux,
David Howells7e047ef2006-06-26 00:24:50 -0700448 struct key *dest_keyring,
449 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
David Howellsd84f4f92008-11-14 10:39:23 +1100451 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100453 key_ref_t key_ref;
David Howells2b9e4682010-04-30 14:32:34 +0100454 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
David Howells4a38e122008-04-29 01:01:24 -0700456 kenter("%s,%s,%p,%zu,%p,%p,%lx",
457 type->name, description, callout_info, callout_len, aux,
David Howells4e54f082006-06-29 02:24:28 -0700458 dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 /* search all the process keyrings for a key */
David Howells664cceb2005-09-28 17:03:15 +0100461 key_ref = search_process_keyrings(type, description, type->match,
David Howellsd84f4f92008-11-14 10:39:23 +1100462 cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
David Howells664cceb2005-09-28 17:03:15 +0100464 if (!IS_ERR(key_ref)) {
465 key = key_ref_to_ptr(key_ref);
David Howells03449cd2010-04-27 13:13:08 -0700466 if (dest_keyring) {
467 construct_get_dest_keyring(&dest_keyring);
David Howells2b9e4682010-04-30 14:32:34 +0100468 ret = key_link(dest_keyring, key);
David Howells03449cd2010-04-27 13:13:08 -0700469 key_put(dest_keyring);
David Howells2b9e4682010-04-30 14:32:34 +0100470 if (ret < 0) {
471 key_put(key);
472 key = ERR_PTR(ret);
473 goto error;
474 }
David Howells03449cd2010-04-27 13:13:08 -0700475 }
David Howells76181c12007-10-16 23:29:46 -0700476 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800477 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700478 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* the search failed, but the keyrings were searchable, so we
480 * should consult userspace if we can */
481 key = ERR_PTR(-ENOKEY);
482 if (!callout_info)
483 goto error;
484
David Howells76181c12007-10-16 23:29:46 -0700485 key = construct_key_and_link(type, description, callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700486 callout_len, aux, dest_keyring,
487 flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
David Howells3e301482005-06-23 22:00:56 -0700490error:
491 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return key;
David Howells76181c12007-10-16 23:29:46 -0700493}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
David Howells76181c12007-10-16 23:29:46 -0700495/*
496 * wait for construction of a key to complete
497 */
498int wait_for_key_construction(struct key *key, bool intr)
499{
500 int ret;
David Howells3e301482005-06-23 22:00:56 -0700501
David Howells76181c12007-10-16 23:29:46 -0700502 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
503 intr ? key_wait_bit_intr : key_wait_bit,
504 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
505 if (ret < 0)
506 return ret;
507 return key_validate(key);
508}
509EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700510
David Howells3e301482005-06-23 22:00:56 -0700511/*
512 * request a key
513 * - search the process's keyrings
514 * - check the list of keys being created or updated
515 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700516 * - waits uninterruptible for creation to complete
David Howells3e301482005-06-23 22:00:56 -0700517 */
518struct key *request_key(struct key_type *type,
519 const char *description,
520 const char *callout_info)
521{
David Howells76181c12007-10-16 23:29:46 -0700522 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700523 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700524 int ret;
David Howells3e301482005-06-23 22:00:56 -0700525
David Howells4a38e122008-04-29 01:01:24 -0700526 if (callout_info)
527 callout_len = strlen(callout_info);
528 key = request_key_and_link(type, description, callout_info, callout_len,
529 NULL, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700530 if (!IS_ERR(key)) {
531 ret = wait_for_key_construction(key, false);
532 if (ret < 0) {
533 key_put(key);
534 return ERR_PTR(ret);
535 }
536 }
537 return key;
538}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700540
David Howells4e54f082006-06-29 02:24:28 -0700541/*
542 * request a key with auxiliary data for the upcaller
543 * - search the process's keyrings
544 * - check the list of keys being created or updated
545 * - call out to userspace for a key if supplementary info was provided
David Howells76181c12007-10-16 23:29:46 -0700546 * - waits uninterruptible for creation to complete
David Howells4e54f082006-06-29 02:24:28 -0700547 */
548struct key *request_key_with_auxdata(struct key_type *type,
549 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700550 const void *callout_info,
551 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700552 void *aux)
553{
David Howells76181c12007-10-16 23:29:46 -0700554 struct key *key;
555 int ret;
556
David Howells4a38e122008-04-29 01:01:24 -0700557 key = request_key_and_link(type, description, callout_info, callout_len,
558 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700559 if (!IS_ERR(key)) {
560 ret = wait_for_key_construction(key, false);
561 if (ret < 0) {
562 key_put(key);
563 return ERR_PTR(ret);
564 }
565 }
566 return key;
567}
568EXPORT_SYMBOL(request_key_with_auxdata);
569
570/*
571 * request a key (allow async construction)
572 * - search the process's keyrings
573 * - check the list of keys being created or updated
574 * - call out to userspace for a key if supplementary info was provided
575 */
576struct key *request_key_async(struct key_type *type,
577 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700578 const void *callout_info,
579 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700580{
David Howells4a38e122008-04-29 01:01:24 -0700581 return request_key_and_link(type, description, callout_info,
582 callout_len, NULL, NULL,
583 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700584}
585EXPORT_SYMBOL(request_key_async);
586
587/*
588 * request a key with auxiliary data for the upcaller (allow async construction)
589 * - search the process's keyrings
590 * - check the list of keys being created or updated
591 * - call out to userspace for a key if supplementary info was provided
592 */
593struct key *request_key_async_with_auxdata(struct key_type *type,
594 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700595 const void *callout_info,
596 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700597 void *aux)
598{
David Howells4a38e122008-04-29 01:01:24 -0700599 return request_key_and_link(type, description, callout_info,
600 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700601}
602EXPORT_SYMBOL(request_key_async_with_auxdata);