blob: 7a0c6b666ff03a5e59e979a70fd14ed808b46861 [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 *
Kees Cook3db38ed2017-05-13 04:51:52 -070011 * See Documentation/security/keys/request-key.rst
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
13
Paul Gortmaker876979c2018-12-09 15:36:29 -050014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#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"
David Howells822ad642019-02-14 16:20:25 +000021#include <keys/request_key_auth-type.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
David Howellse9e349b2008-11-14 10:39:13 +110023#define key_negative_timeout 60 /* default timeout on a negative key's existence */
24
David Howells973c9f42011-01-20 16:38:33 +000025/**
26 * complete_request_key - Complete the construction of a key.
David Howells822ad642019-02-14 16:20:25 +000027 * @auth_key: The authorisation key.
David Howells973c9f42011-01-20 16:38:33 +000028 * @error: The success or failute of the construction.
29 *
30 * Complete the attempt to construct a key. The key will be negated
31 * if an error is indicated. The authorisation key will be revoked
32 * unconditionally.
David Howells76181c12007-10-16 23:29:46 -070033 */
David Howells822ad642019-02-14 16:20:25 +000034void complete_request_key(struct key *authkey, int error)
David Howells76181c12007-10-16 23:29:46 -070035{
David Howells822ad642019-02-14 16:20:25 +000036 struct request_key_auth *rka = get_request_key_auth(authkey);
37 struct key *key = rka->target_key;
38
39 kenter("%d{%d},%d", authkey->serial, key->serial, error);
David Howells76181c12007-10-16 23:29:46 -070040
41 if (error < 0)
David Howells822ad642019-02-14 16:20:25 +000042 key_negate_and_link(key, key_negative_timeout, NULL, authkey);
David Howells76181c12007-10-16 23:29:46 -070043 else
David Howells822ad642019-02-14 16:20:25 +000044 key_revoke(authkey);
David Howells76181c12007-10-16 23:29:46 -070045}
46EXPORT_SYMBOL(complete_request_key);
47
David Howells973c9f42011-01-20 16:38:33 +000048/*
49 * Initialise a usermode helper that is going to have a specific session
50 * keyring.
51 *
52 * This is called in context of freshly forked kthread before kernel_execve(),
53 * so we can simply install the desired session_keyring at this point.
54 */
David Howells87966992011-06-17 11:25:59 +010055static int umh_keys_init(struct subprocess_info *info, struct cred *cred)
Oleg Nesterov685bfd22010-05-26 14:43:00 -070056{
Oleg Nesterov685bfd22010-05-26 14:43:00 -070057 struct key *keyring = info->data;
David Howells973c9f42011-01-20 16:38:33 +000058
Oleg Nesterov685bfd22010-05-26 14:43:00 -070059 return install_session_keyring_to_cred(cred, keyring);
60}
61
David Howells973c9f42011-01-20 16:38:33 +000062/*
63 * Clean up a usermode helper with session keyring.
64 */
Oleg Nesterov685bfd22010-05-26 14:43:00 -070065static void umh_keys_cleanup(struct subprocess_info *info)
66{
67 struct key *keyring = info->data;
68 key_put(keyring);
69}
70
David Howells973c9f42011-01-20 16:38:33 +000071/*
72 * Call a usermode helper with a specific session keyring.
73 */
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +010074static int call_usermodehelper_keys(const char *path, char **argv, char **envp,
Oleg Nesterov9d944ef2012-03-23 15:02:48 -070075 struct key *session_keyring, int wait)
Oleg Nesterov685bfd22010-05-26 14:43:00 -070076{
Lucas De Marchi93997f62013-04-30 15:28:05 -070077 struct subprocess_info *info;
78
79 info = call_usermodehelper_setup(path, argv, envp, GFP_KERNEL,
80 umh_keys_init, umh_keys_cleanup,
81 session_keyring);
82 if (!info)
83 return -ENOMEM;
84
85 key_get(session_keyring);
86 return call_usermodehelper_exec(info, wait);
Oleg Nesterov685bfd22010-05-26 14:43:00 -070087}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
David Howells973c9f42011-01-20 16:38:33 +000090 * Request userspace finish the construction of a key
David Howellsb5f545c2006-01-08 01:02:47 -080091 * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
David Howells822ad642019-02-14 16:20:25 +000093static int call_sbin_request_key(struct key *authkey, void *aux)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +010095 static char const request_key[] = "/sbin/request-key";
David Howells822ad642019-02-14 16:20:25 +000096 struct request_key_auth *rka = get_request_key_auth(authkey);
David Howells86a264a2008-11-14 10:39:18 +110097 const struct cred *cred = current_cred();
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 key_serial_t prkey, sskey;
David Howells822ad642019-02-14 16:20:25 +000099 struct key *key = rka->target_key, *keyring, *session;
David Howellsb5f545c2006-01-08 01:02:47 -0800100 char *argv[9], *envp[3], uid_str[12], gid_str[12];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 char key_str[12], keyring_str[3][12];
David Howellsb5f545c2006-01-08 01:02:47 -0800102 char desc[20];
David Howells3e301482005-06-23 22:00:56 -0700103 int ret, i;
104
David Howells822ad642019-02-14 16:20:25 +0000105 kenter("{%d},{%d},%s", key->serial, authkey->serial, rka->op);
David Howells3e301482005-06-23 22:00:56 -0700106
David Howells8bbf49762008-11-14 10:39:14 +1100107 ret = install_user_keyrings();
108 if (ret < 0)
109 goto error_alloc;
110
David Howellsb5f545c2006-01-08 01:02:47 -0800111 /* allocate a new session keyring */
112 sprintf(desc, "_req.%u", key->serial);
113
David Howellsd84f4f92008-11-14 10:39:23 +1100114 cred = get_current_cred();
115 keyring = keyring_alloc(desc, cred->fsuid, cred->fsgid, cred,
David Howells96b5c8f2012-10-02 19:24:56 +0100116 KEY_POS_ALL | KEY_USR_VIEW | KEY_USR_READ,
David Howells5ac7eac2016-04-06 16:14:24 +0100117 KEY_ALLOC_QUOTA_OVERRUN, NULL, NULL);
David Howellsd84f4f92008-11-14 10:39:23 +1100118 put_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800119 if (IS_ERR(keyring)) {
120 ret = PTR_ERR(keyring);
121 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -0700122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
David Howellsb5f545c2006-01-08 01:02:47 -0800124 /* attach the auth key to the session keyring */
David Howells896903c2010-04-30 14:32:23 +0100125 ret = key_link(keyring, authkey);
David Howellsb5f545c2006-01-08 01:02:47 -0800126 if (ret < 0)
127 goto error_link;
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 /* record the UID and GID */
Eric W. Biederman9a56c2d2012-02-08 07:53:04 -0800130 sprintf(uid_str, "%d", from_kuid(&init_user_ns, cred->fsuid));
131 sprintf(gid_str, "%d", from_kgid(&init_user_ns, cred->fsgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 /* we say which key is under construction */
134 sprintf(key_str, "%d", key->serial);
135
136 /* we specify the process's default keyrings */
137 sprintf(keyring_str[0], "%d",
David Howellsd84f4f92008-11-14 10:39:23 +1100138 cred->thread_keyring ? cred->thread_keyring->serial : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
140 prkey = 0;
David Howells3a505972012-10-02 19:24:29 +0100141 if (cred->process_keyring)
142 prkey = cred->process_keyring->serial;
Justin P. Mattock5ad18a02010-06-30 10:39:11 +0100143 sprintf(keyring_str[1], "%d", prkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
David Howells93b4a442010-04-23 13:18:00 -0400145 rcu_read_lock();
David Howells3a505972012-10-02 19:24:29 +0100146 session = rcu_dereference(cred->session_keyring);
David Howells93b4a442010-04-23 13:18:00 -0400147 if (!session)
148 session = cred->user->session_keyring;
149 sskey = session->serial;
150 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sprintf(keyring_str[2], "%d", sskey);
153
154 /* set up a minimal environment */
155 i = 0;
156 envp[i++] = "HOME=/";
157 envp[i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
158 envp[i] = NULL;
159
160 /* set up the argument list */
161 i = 0;
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100162 argv[i++] = (char *)request_key;
David Howells822ad642019-02-14 16:20:25 +0000163 argv[i++] = (char *)rka->op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 argv[i++] = key_str;
165 argv[i++] = uid_str;
166 argv[i++] = gid_str;
167 argv[i++] = keyring_str[0];
168 argv[i++] = keyring_str[1];
169 argv[i++] = keyring_str[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 argv[i] = NULL;
171
172 /* do it */
Greg Kroah-Hartman377e7a22016-12-11 18:00:43 +0100173 ret = call_usermodehelper_keys(request_key, argv, envp, keyring,
Jeremy Fitzhardinge86313c42007-07-17 18:37:03 -0700174 UMH_WAIT_PROC);
David Howells76181c12007-10-16 23:29:46 -0700175 kdebug("usermode -> 0x%x", ret);
176 if (ret >= 0) {
177 /* ret is the exit/wait code */
178 if (test_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags) ||
179 key_validate(key) < 0)
180 ret = -ENOKEY;
181 else
182 /* ignore any errors from userspace if the key was
183 * instantiated */
184 ret = 0;
185 }
David Howells3e301482005-06-23 22:00:56 -0700186
David Howellsb5f545c2006-01-08 01:02:47 -0800187error_link:
188 key_put(keyring);
David Howells3e301482005-06-23 22:00:56 -0700189
David Howellsb5f545c2006-01-08 01:02:47 -0800190error_alloc:
David Howells822ad642019-02-14 16:20:25 +0000191 complete_request_key(authkey, ret);
David Howellsd84f4f92008-11-14 10:39:23 +1100192 kleave(" = %d", ret);
David Howells3e301482005-06-23 22:00:56 -0700193 return ret;
David Howells76181c12007-10-16 23:29:46 -0700194}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196/*
David Howells973c9f42011-01-20 16:38:33 +0000197 * Call out to userspace for key construction.
198 *
199 * Program failure is ignored in favour of key status.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 */
David Howells4a38e122008-04-29 01:01:24 -0700201static int construct_key(struct key *key, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100202 size_t callout_len, void *aux,
203 struct key *dest_keyring)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
David Howellsb5f545c2006-01-08 01:02:47 -0800205 request_key_actor_t actor;
David Howells76181c12007-10-16 23:29:46 -0700206 struct key *authkey;
207 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
David Howells4a38e122008-04-29 01:01:24 -0700209 kenter("%d,%p,%zu,%p", key->serial, callout_info, callout_len, aux);
David Howells3e301482005-06-23 22:00:56 -0700210
David Howellsb5f545c2006-01-08 01:02:47 -0800211 /* allocate an authorisation key */
David Howells822ad642019-02-14 16:20:25 +0000212 authkey = request_key_auth_new(key, "create", callout_info, callout_len,
David Howells8bbf49762008-11-14 10:39:14 +1100213 dest_keyring);
David Howells822ad642019-02-14 16:20:25 +0000214 if (IS_ERR(authkey))
215 return PTR_ERR(authkey);
David Howells76181c12007-10-16 23:29:46 -0700216
David Howells822ad642019-02-14 16:20:25 +0000217 /* Make the call */
218 actor = call_sbin_request_key;
219 if (key->type->request_key)
220 actor = key->type->request_key;
David Howells76181c12007-10-16 23:29:46 -0700221
David Howells822ad642019-02-14 16:20:25 +0000222 ret = actor(authkey, aux);
David Howells76181c12007-10-16 23:29:46 -0700223
David Howells822ad642019-02-14 16:20:25 +0000224 /* check that the actor called complete_request_key() prior to
225 * returning an error */
226 WARN_ON(ret < 0 &&
227 !test_bit(KEY_FLAG_REVOKED, &authkey->flags));
David Howellsb5f545c2006-01-08 01:02:47 -0800228
David Howells822ad642019-02-14 16:20:25 +0000229 key_put(authkey);
David Howells76181c12007-10-16 23:29:46 -0700230 kleave(" = %d", ret);
231 return ret;
232}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234/*
David Howells973c9f42011-01-20 16:38:33 +0000235 * Get the appropriate destination keyring for the request.
236 *
237 * The keyring selected is returned with an extra reference upon it which the
238 * caller must release.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 */
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000240static int construct_get_dest_keyring(struct key **_dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700241{
David Howells8bbf49762008-11-14 10:39:14 +1100242 struct request_key_auth *rka;
David Howellsbb952bb2008-11-14 10:39:20 +1100243 const struct cred *cred = current_cred();
David Howells8bbf49762008-11-14 10:39:14 +1100244 struct key *dest_keyring = *_dest_keyring, *authkey;
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000245 int ret;
David Howells3e301482005-06-23 22:00:56 -0700246
David Howells8bbf49762008-11-14 10:39:14 +1100247 kenter("%p", dest_keyring);
David Howells3e301482005-06-23 22:00:56 -0700248
249 /* find the appropriate keyring */
David Howells8bbf49762008-11-14 10:39:14 +1100250 if (dest_keyring) {
251 /* the caller supplied one */
252 key_get(dest_keyring);
253 } else {
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000254 bool do_perm_check = true;
255
David Howells8bbf49762008-11-14 10:39:14 +1100256 /* use a default keyring; falling through the cases until we
257 * find one that we actually have */
David Howellsbb952bb2008-11-14 10:39:20 +1100258 switch (cred->jit_keyring) {
David Howells3e301482005-06-23 22:00:56 -0700259 case KEY_REQKEY_DEFL_DEFAULT:
David Howells8bbf49762008-11-14 10:39:14 +1100260 case KEY_REQKEY_DEFL_REQUESTOR_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100261 if (cred->request_key_auth) {
262 authkey = cred->request_key_auth;
David Howells8bbf49762008-11-14 10:39:14 +1100263 down_read(&authkey->sem);
David Howells822ad642019-02-14 16:20:25 +0000264 rka = get_request_key_auth(authkey);
David Howells8bbf49762008-11-14 10:39:14 +1100265 if (!test_bit(KEY_FLAG_REVOKED,
266 &authkey->flags))
267 dest_keyring =
268 key_get(rka->dest_keyring);
269 up_read(&authkey->sem);
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000270 if (dest_keyring) {
271 do_perm_check = false;
David Howells8bbf49762008-11-14 10:39:14 +1100272 break;
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000273 }
David Howells8bbf49762008-11-14 10:39:14 +1100274 }
275
David Howells3e301482005-06-23 22:00:56 -0700276 case KEY_REQKEY_DEFL_THREAD_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100277 dest_keyring = key_get(cred->thread_keyring);
David Howells3e301482005-06-23 22:00:56 -0700278 if (dest_keyring)
279 break;
280
281 case KEY_REQKEY_DEFL_PROCESS_KEYRING:
David Howells3a505972012-10-02 19:24:29 +0100282 dest_keyring = key_get(cred->process_keyring);
David Howells3e301482005-06-23 22:00:56 -0700283 if (dest_keyring)
284 break;
285
286 case KEY_REQKEY_DEFL_SESSION_KEYRING:
287 rcu_read_lock();
288 dest_keyring = key_get(
David Howells3a505972012-10-02 19:24:29 +0100289 rcu_dereference(cred->session_keyring));
David Howells3e301482005-06-23 22:00:56 -0700290 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700291
292 if (dest_keyring)
293 break;
294
295 case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
David Howellsb6dff3e2008-11-14 10:39:16 +1100296 dest_keyring =
David Howellsbb952bb2008-11-14 10:39:20 +1100297 key_get(cred->user->session_keyring);
David Howells3e301482005-06-23 22:00:56 -0700298 break;
299
300 case KEY_REQKEY_DEFL_USER_KEYRING:
David Howellsbb952bb2008-11-14 10:39:20 +1100301 dest_keyring = key_get(cred->user->uid_keyring);
David Howells3e301482005-06-23 22:00:56 -0700302 break;
303
304 case KEY_REQKEY_DEFL_GROUP_KEYRING:
305 default:
306 BUG();
307 }
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000308
309 /*
310 * Require Write permission on the keyring. This is essential
311 * because the default keyring may be the session keyring, and
312 * joining a keyring only requires Search permission.
313 *
314 * However, this check is skipped for the "requestor keyring" so
315 * that /sbin/request-key can itself use request_key() to add
316 * keys to the original requestor's destination keyring.
317 */
318 if (dest_keyring && do_perm_check) {
319 ret = key_permission(make_key_ref(dest_keyring, 1),
320 KEY_NEED_WRITE);
321 if (ret) {
322 key_put(dest_keyring);
323 return ret;
324 }
325 }
David Howells3e301482005-06-23 22:00:56 -0700326 }
327
David Howells8bbf49762008-11-14 10:39:14 +1100328 *_dest_keyring = dest_keyring;
329 kleave(" [dk %d]", key_serial(dest_keyring));
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000330 return 0;
David Howells76181c12007-10-16 23:29:46 -0700331}
David Howells3e301482005-06-23 22:00:56 -0700332
David Howells76181c12007-10-16 23:29:46 -0700333/*
David Howells973c9f42011-01-20 16:38:33 +0000334 * Allocate a new key in under-construction state and attempt to link it in to
335 * the requested keyring.
336 *
337 * May return a key that's already under construction instead if there was a
338 * race between two thread calling request_key().
David Howells76181c12007-10-16 23:29:46 -0700339 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100340static int construct_alloc_key(struct keyring_search_context *ctx,
David Howells76181c12007-10-16 23:29:46 -0700341 struct key *dest_keyring,
342 unsigned long flags,
343 struct key_user *user,
344 struct key **_key)
345{
David Howellsb2a4df22013-09-24 10:35:18 +0100346 struct assoc_array_edit *edit;
David Howells76181c12007-10-16 23:29:46 -0700347 struct key *key;
David Howells96b5c8f2012-10-02 19:24:56 +0100348 key_perm_t perm;
David Howells76181c12007-10-16 23:29:46 -0700349 key_ref_t key_ref;
David Howells2b9e4682010-04-30 14:32:34 +0100350 int ret;
David Howells3e301482005-06-23 22:00:56 -0700351
David Howells4bdf0bc2013-09-24 10:35:15 +0100352 kenter("%s,%s,,,",
353 ctx->index_key.type->name, ctx->index_key.description);
David Howells76181c12007-10-16 23:29:46 -0700354
David Howellsf70e2e02010-04-30 14:32:39 +0100355 *_key = NULL;
David Howells76181c12007-10-16 23:29:46 -0700356 mutex_lock(&user->cons_lock);
357
David Howells96b5c8f2012-10-02 19:24:56 +0100358 perm = KEY_POS_VIEW | KEY_POS_SEARCH | KEY_POS_LINK | KEY_POS_SETATTR;
359 perm |= KEY_USR_VIEW;
David Howells4bdf0bc2013-09-24 10:35:15 +0100360 if (ctx->index_key.type->read)
David Howells96b5c8f2012-10-02 19:24:56 +0100361 perm |= KEY_POS_READ;
David Howells4bdf0bc2013-09-24 10:35:15 +0100362 if (ctx->index_key.type == &key_type_keyring ||
363 ctx->index_key.type->update)
David Howells96b5c8f2012-10-02 19:24:56 +0100364 perm |= KEY_POS_WRITE;
365
David Howells4bdf0bc2013-09-24 10:35:15 +0100366 key = key_alloc(ctx->index_key.type, ctx->index_key.description,
367 ctx->cred->fsuid, ctx->cred->fsgid, ctx->cred,
David Howells5ac7eac2016-04-06 16:14:24 +0100368 perm, flags, NULL);
David Howells76181c12007-10-16 23:29:46 -0700369 if (IS_ERR(key))
370 goto alloc_failed;
371
372 set_bit(KEY_FLAG_USER_CONSTRUCT, &key->flags);
373
David Howellsf70e2e02010-04-30 14:32:39 +0100374 if (dest_keyring) {
David Howellsb2a4df22013-09-24 10:35:18 +0100375 ret = __key_link_begin(dest_keyring, &ctx->index_key, &edit);
David Howellsf70e2e02010-04-30 14:32:39 +0100376 if (ret < 0)
377 goto link_prealloc_failed;
378 }
David Howells76181c12007-10-16 23:29:46 -0700379
380 /* attach the key to the destination keyring under lock, but we do need
381 * to do another check just in case someone beat us to it whilst we
382 * waited for locks */
383 mutex_lock(&key_construction_mutex);
384
David Howells4bdf0bc2013-09-24 10:35:15 +0100385 key_ref = search_process_keyrings(ctx);
David Howells76181c12007-10-16 23:29:46 -0700386 if (!IS_ERR(key_ref))
387 goto key_already_present;
388
David Howells34574dd2009-04-09 17:14:05 +0100389 if (dest_keyring)
David Howellsb2a4df22013-09-24 10:35:18 +0100390 __key_link(key, &edit);
David Howells76181c12007-10-16 23:29:46 -0700391
392 mutex_unlock(&key_construction_mutex);
David Howells34574dd2009-04-09 17:14:05 +0100393 if (dest_keyring)
David Howellsb2a4df22013-09-24 10:35:18 +0100394 __key_link_end(dest_keyring, &ctx->index_key, edit);
David Howells76181c12007-10-16 23:29:46 -0700395 mutex_unlock(&user->cons_lock);
396 *_key = key;
397 kleave(" = 0 [%d]", key_serial(key));
398 return 0;
399
David Howells2b9e4682010-04-30 14:32:34 +0100400 /* the key is now present - we tell the caller that we found it by
401 * returning -EINPROGRESS */
David Howells76181c12007-10-16 23:29:46 -0700402key_already_present:
David Howellsf70e2e02010-04-30 14:32:39 +0100403 key_put(key);
David Howells76181c12007-10-16 23:29:46 -0700404 mutex_unlock(&key_construction_mutex);
David Howellsf70e2e02010-04-30 14:32:39 +0100405 key = key_ref_to_ptr(key_ref);
David Howells03449cd2010-04-27 13:13:08 -0700406 if (dest_keyring) {
David Howellsf70e2e02010-04-30 14:32:39 +0100407 ret = __key_link_check_live_key(dest_keyring, key);
408 if (ret == 0)
David Howellsb2a4df22013-09-24 10:35:18 +0100409 __key_link(key, &edit);
410 __key_link_end(dest_keyring, &ctx->index_key, edit);
David Howellsf70e2e02010-04-30 14:32:39 +0100411 if (ret < 0)
412 goto link_check_failed;
David Howells03449cd2010-04-27 13:13:08 -0700413 }
David Howells76181c12007-10-16 23:29:46 -0700414 mutex_unlock(&user->cons_lock);
David Howellsf70e2e02010-04-30 14:32:39 +0100415 *_key = key;
David Howells76181c12007-10-16 23:29:46 -0700416 kleave(" = -EINPROGRESS [%d]", key_serial(key));
417 return -EINPROGRESS;
418
David Howellsf70e2e02010-04-30 14:32:39 +0100419link_check_failed:
420 mutex_unlock(&user->cons_lock);
421 key_put(key);
422 kleave(" = %d [linkcheck]", ret);
423 return ret;
424
425link_prealloc_failed:
David Howellsf70e2e02010-04-30 14:32:39 +0100426 mutex_unlock(&user->cons_lock);
David Jefferyd0709f12015-02-12 16:45:31 +0000427 key_put(key);
David Howellsf70e2e02010-04-30 14:32:39 +0100428 kleave(" = %d [prelink]", ret);
429 return ret;
430
David Howells76181c12007-10-16 23:29:46 -0700431alloc_failed:
432 mutex_unlock(&user->cons_lock);
David Howells76181c12007-10-16 23:29:46 -0700433 kleave(" = %ld", PTR_ERR(key));
434 return PTR_ERR(key);
435}
436
437/*
David Howells973c9f42011-01-20 16:38:33 +0000438 * Commence key construction.
David Howells76181c12007-10-16 23:29:46 -0700439 */
David Howells4bdf0bc2013-09-24 10:35:15 +0100440static struct key *construct_key_and_link(struct keyring_search_context *ctx,
David Howells76181c12007-10-16 23:29:46 -0700441 const char *callout_info,
David Howells4a38e122008-04-29 01:01:24 -0700442 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700443 void *aux,
444 struct key *dest_keyring,
445 unsigned long flags)
446{
447 struct key_user *user;
448 struct key *key;
449 int ret;
450
David Howellsd84f4f92008-11-14 10:39:23 +1100451 kenter("");
452
David Howells911b79c2015-10-19 11:20:28 +0100453 if (ctx->index_key.type == &key_type_keyring)
454 return ERR_PTR(-EPERM);
David Howells965475a2016-06-14 10:29:44 +0100455
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000456 ret = construct_get_dest_keyring(&dest_keyring);
457 if (ret)
458 goto error;
David Howells76181c12007-10-16 23:29:46 -0700459
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000460 user = key_user_lookup(current_fsuid());
461 if (!user) {
462 ret = -ENOMEM;
463 goto error_put_dest_keyring;
464 }
David Howells8bbf49762008-11-14 10:39:14 +1100465
David Howells4bdf0bc2013-09-24 10:35:15 +0100466 ret = construct_alloc_key(ctx, dest_keyring, flags, user, &key);
David Howells76181c12007-10-16 23:29:46 -0700467 key_user_put(user);
468
469 if (ret == 0) {
David Howells8bbf49762008-11-14 10:39:14 +1100470 ret = construct_key(key, callout_info, callout_len, aux,
471 dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100472 if (ret < 0) {
473 kdebug("cons failed");
David Howells76181c12007-10-16 23:29:46 -0700474 goto construction_failed;
David Howellsd84f4f92008-11-14 10:39:23 +1100475 }
David Howells2b9e4682010-04-30 14:32:34 +0100476 } else if (ret == -EINPROGRESS) {
477 ret = 0;
478 } else {
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000479 goto error_put_dest_keyring;
David Howells76181c12007-10-16 23:29:46 -0700480 }
481
David Howells8bbf49762008-11-14 10:39:14 +1100482 key_put(dest_keyring);
David Howellsd84f4f92008-11-14 10:39:23 +1100483 kleave(" = key %d", key_serial(key));
David Howells76181c12007-10-16 23:29:46 -0700484 return key;
485
486construction_failed:
487 key_negate_and_link(key, key_negative_timeout, NULL, NULL);
488 key_put(key);
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000489error_put_dest_keyring:
David Howells8bbf49762008-11-14 10:39:14 +1100490 key_put(dest_keyring);
Eric Biggers4dca6ea2017-12-08 15:13:27 +0000491error:
David Howellsd84f4f92008-11-14 10:39:23 +1100492 kleave(" = %d", ret);
David Howells76181c12007-10-16 23:29:46 -0700493 return ERR_PTR(ret);
494}
495
David Howells973c9f42011-01-20 16:38:33 +0000496/**
497 * request_key_and_link - Request a key and cache it in a keyring.
498 * @type: The type of key we want.
499 * @description: The searchable description of the key.
500 * @callout_info: The data to pass to the instantiation upcall (or NULL).
501 * @callout_len: The length of callout_info.
502 * @aux: Auxiliary data for the upcall.
503 * @dest_keyring: Where to cache the key.
504 * @flags: Flags to key_alloc().
505 *
506 * A key matching the specified criteria is searched for in the process's
507 * keyrings and returned with its usage count incremented if found. Otherwise,
508 * if callout_info is not NULL, a key will be allocated and some service
509 * (probably in userspace) will be asked to instantiate it.
510 *
511 * If successfully found or created, the key will be linked to the destination
512 * keyring if one is provided.
513 *
514 * Returns a pointer to the key if successful; -EACCES, -ENOKEY, -EKEYREVOKED
515 * or -EKEYEXPIRED if an inaccessible, negative, revoked or expired key was
516 * found; -ENOKEY if no key was found and no @callout_info was given; -EDQUOT
517 * if insufficient key quota was available to create a new key; or -ENOMEM if
518 * insufficient memory was available.
519 *
520 * If the returned key was created, then it may still be under construction,
521 * and wait_for_key_construction() should be used to wait for that to complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
David Howells3e301482005-06-23 22:00:56 -0700523struct key *request_key_and_link(struct key_type *type,
524 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700525 const void *callout_info,
526 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700527 void *aux,
David Howells7e047ef2006-06-26 00:24:50 -0700528 struct key *dest_keyring,
529 unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
David Howells4bdf0bc2013-09-24 10:35:15 +0100531 struct keyring_search_context ctx = {
532 .index_key.type = type,
533 .index_key.description = description,
Eric Biggersede0fa982019-02-22 15:36:18 +0000534 .index_key.desc_len = strlen(description),
David Howells4bdf0bc2013-09-24 10:35:15 +0100535 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100536 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100537 .match_data.raw_data = description,
538 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells0b0a8412014-12-01 22:52:53 +0000539 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
540 KEYRING_SEARCH_SKIP_EXPIRED),
David Howells4bdf0bc2013-09-24 10:35:15 +0100541 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 struct key *key;
David Howells664cceb2005-09-28 17:03:15 +0100543 key_ref_t key_ref;
David Howells2b9e4682010-04-30 14:32:34 +0100544 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
David Howells4a38e122008-04-29 01:01:24 -0700546 kenter("%s,%s,%p,%zu,%p,%p,%lx",
David Howells4bdf0bc2013-09-24 10:35:15 +0100547 ctx.index_key.type->name, ctx.index_key.description,
548 callout_info, callout_len, aux, dest_keyring, flags);
David Howells3e301482005-06-23 22:00:56 -0700549
David Howells46291952014-09-16 17:36:02 +0100550 if (type->match_preparse) {
551 ret = type->match_preparse(&ctx.match_data);
552 if (ret < 0) {
553 key = ERR_PTR(ret);
554 goto error;
555 }
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 /* search all the process keyrings for a key */
David Howells4bdf0bc2013-09-24 10:35:15 +0100559 key_ref = search_process_keyrings(&ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
David Howells664cceb2005-09-28 17:03:15 +0100561 if (!IS_ERR(key_ref)) {
562 key = key_ref_to_ptr(key_ref);
David Howells03449cd2010-04-27 13:13:08 -0700563 if (dest_keyring) {
David Howells2b9e4682010-04-30 14:32:34 +0100564 ret = key_link(dest_keyring, key);
David Howells2b9e4682010-04-30 14:32:34 +0100565 if (ret < 0) {
566 key_put(key);
567 key = ERR_PTR(ret);
David Howells46291952014-09-16 17:36:02 +0100568 goto error_free;
David Howells2b9e4682010-04-30 14:32:34 +0100569 }
David Howells03449cd2010-04-27 13:13:08 -0700570 }
David Howells76181c12007-10-16 23:29:46 -0700571 } else if (PTR_ERR(key_ref) != -EAGAIN) {
David Howellse231c2e2008-02-07 00:15:26 -0800572 key = ERR_CAST(key_ref);
David Howells76181c12007-10-16 23:29:46 -0700573 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* the search failed, but the keyrings were searchable, so we
575 * should consult userspace if we can */
576 key = ERR_PTR(-ENOKEY);
577 if (!callout_info)
David Howells46291952014-09-16 17:36:02 +0100578 goto error_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
David Howells4bdf0bc2013-09-24 10:35:15 +0100580 key = construct_key_and_link(&ctx, callout_info, callout_len,
581 aux, dest_keyring, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
583
David Howells46291952014-09-16 17:36:02 +0100584error_free:
585 if (type->match_free)
586 type->match_free(&ctx.match_data);
David Howells3e301482005-06-23 22:00:56 -0700587error:
588 kleave(" = %p", key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 return key;
David Howells76181c12007-10-16 23:29:46 -0700590}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
David Howells973c9f42011-01-20 16:38:33 +0000592/**
593 * wait_for_key_construction - Wait for construction of a key to complete
594 * @key: The key being waited for.
595 * @intr: Whether to wait interruptibly.
596 *
597 * Wait for a key to finish being constructed.
598 *
599 * Returns 0 if successful; -ERESTARTSYS if the wait was interrupted; -ENOKEY
600 * if the key was negated; or -EKEYREVOKED or -EKEYEXPIRED if the key was
601 * revoked or expired.
David Howells76181c12007-10-16 23:29:46 -0700602 */
603int wait_for_key_construction(struct key *key, bool intr)
604{
605 int ret;
David Howells3e301482005-06-23 22:00:56 -0700606
David Howells76181c12007-10-16 23:29:46 -0700607 ret = wait_on_bit(&key->flags, KEY_FLAG_USER_CONSTRUCT,
David Howells76181c12007-10-16 23:29:46 -0700608 intr ? TASK_INTERRUPTIBLE : TASK_UNINTERRUPTIBLE);
NeilBrown74316202014-07-07 15:16:04 +1000609 if (ret)
610 return -ERESTARTSYS;
David Howells363b02d2017-10-04 16:43:25 +0100611 ret = key_read_state(key);
612 if (ret < 0)
613 return ret;
David Howells76181c12007-10-16 23:29:46 -0700614 return key_validate(key);
615}
616EXPORT_SYMBOL(wait_for_key_construction);
David Howells3e301482005-06-23 22:00:56 -0700617
David Howells973c9f42011-01-20 16:38:33 +0000618/**
619 * request_key - Request a key and wait for construction
620 * @type: Type of key.
621 * @description: The searchable description of the key.
622 * @callout_info: The data to pass to the instantiation upcall (or NULL).
623 *
624 * As for request_key_and_link() except that it does not add the returned key
625 * to a keyring if found, new keys are always allocated in the user's quota,
626 * the callout_info must be a NUL-terminated string and no auxiliary data can
627 * be passed.
628 *
629 * Furthermore, it then works as wait_for_key_construction() to wait for the
630 * completion of keys undergoing construction with a non-interruptible wait.
David Howells3e301482005-06-23 22:00:56 -0700631 */
632struct key *request_key(struct key_type *type,
633 const char *description,
634 const char *callout_info)
635{
David Howells76181c12007-10-16 23:29:46 -0700636 struct key *key;
David Howells4a38e122008-04-29 01:01:24 -0700637 size_t callout_len = 0;
David Howells76181c12007-10-16 23:29:46 -0700638 int ret;
David Howells3e301482005-06-23 22:00:56 -0700639
David Howells4a38e122008-04-29 01:01:24 -0700640 if (callout_info)
641 callout_len = strlen(callout_info);
642 key = request_key_and_link(type, description, callout_info, callout_len,
643 NULL, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700644 if (!IS_ERR(key)) {
645 ret = wait_for_key_construction(key, false);
646 if (ret < 0) {
647 key_put(key);
648 return ERR_PTR(ret);
649 }
650 }
651 return key;
652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653EXPORT_SYMBOL(request_key);
David Howells4e54f082006-06-29 02:24:28 -0700654
David Howells973c9f42011-01-20 16:38:33 +0000655/**
656 * request_key_with_auxdata - Request a key with auxiliary data for the upcaller
657 * @type: The type of key we want.
658 * @description: The searchable description of the key.
659 * @callout_info: The data to pass to the instantiation upcall (or NULL).
660 * @callout_len: The length of callout_info.
661 * @aux: Auxiliary data for the upcall.
662 *
663 * As for request_key_and_link() except that it does not add the returned key
664 * to a keyring if found and new keys are always allocated in the user's quota.
665 *
666 * Furthermore, it then works as wait_for_key_construction() to wait for the
667 * completion of keys undergoing construction with a non-interruptible wait.
David Howells4e54f082006-06-29 02:24:28 -0700668 */
669struct key *request_key_with_auxdata(struct key_type *type,
670 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700671 const void *callout_info,
672 size_t callout_len,
David Howells4e54f082006-06-29 02:24:28 -0700673 void *aux)
674{
David Howells76181c12007-10-16 23:29:46 -0700675 struct key *key;
676 int ret;
677
David Howells4a38e122008-04-29 01:01:24 -0700678 key = request_key_and_link(type, description, callout_info, callout_len,
679 aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700680 if (!IS_ERR(key)) {
681 ret = wait_for_key_construction(key, false);
682 if (ret < 0) {
683 key_put(key);
684 return ERR_PTR(ret);
685 }
686 }
687 return key;
688}
689EXPORT_SYMBOL(request_key_with_auxdata);
690
691/*
David Howells973c9f42011-01-20 16:38:33 +0000692 * request_key_async - Request a key (allow async construction)
693 * @type: Type of key.
694 * @description: The searchable description of the key.
695 * @callout_info: The data to pass to the instantiation upcall (or NULL).
696 * @callout_len: The length of callout_info.
697 *
698 * As for request_key_and_link() except that it does not add the returned key
699 * to a keyring if found, new keys are always allocated in the user's quota and
700 * no auxiliary data can be passed.
701 *
702 * The caller should call wait_for_key_construction() to wait for the
703 * completion of the returned key if it is still undergoing construction.
David Howells76181c12007-10-16 23:29:46 -0700704 */
705struct key *request_key_async(struct key_type *type,
706 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700707 const void *callout_info,
708 size_t callout_len)
David Howells76181c12007-10-16 23:29:46 -0700709{
David Howells4a38e122008-04-29 01:01:24 -0700710 return request_key_and_link(type, description, callout_info,
711 callout_len, NULL, NULL,
712 KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700713}
714EXPORT_SYMBOL(request_key_async);
715
716/*
717 * request a key with auxiliary data for the upcaller (allow async construction)
David Howells973c9f42011-01-20 16:38:33 +0000718 * @type: Type of key.
719 * @description: The searchable description of the key.
720 * @callout_info: The data to pass to the instantiation upcall (or NULL).
721 * @callout_len: The length of callout_info.
722 * @aux: Auxiliary data for the upcall.
723 *
724 * As for request_key_and_link() except that it does not add the returned key
725 * to a keyring if found and new keys are always allocated in the user's quota.
726 *
727 * The caller should call wait_for_key_construction() to wait for the
728 * completion of the returned key if it is still undergoing construction.
David Howells76181c12007-10-16 23:29:46 -0700729 */
730struct key *request_key_async_with_auxdata(struct key_type *type,
731 const char *description,
David Howells4a38e122008-04-29 01:01:24 -0700732 const void *callout_info,
733 size_t callout_len,
David Howells76181c12007-10-16 23:29:46 -0700734 void *aux)
735{
David Howells4a38e122008-04-29 01:01:24 -0700736 return request_key_and_link(type, description, callout_info,
737 callout_len, aux, NULL, KEY_ALLOC_IN_QUOTA);
David Howells76181c12007-10-16 23:29:46 -0700738}
739EXPORT_SYMBOL(request_key_async_with_auxdata);