blob: e73ec040e2509494d3d123ad8c47419b747a33f7 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
David Howells973c9f42011-01-20 16:38:33 +00002/* Request key authorisation token key definition.
David Howells3e301482005-06-23 22:00:56 -07003 *
4 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
Kees Cook3db38ed2017-05-13 04:51:52 -07007 * See Documentation/security/keys/request-key.rst
David Howells3e301482005-06-23 22:00:56 -07008 */
9
David Howells3e301482005-06-23 22:00:56 -070010#include <linux/sched.h>
11#include <linux/err.h>
12#include <linux/seq_file.h>
Robert P. J. Dayfdb89bc2008-04-29 01:01:32 -070013#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080014#include <linux/uaccess.h>
David Howells3e301482005-06-23 22:00:56 -070015#include "internal.h"
David Howells822ad642019-02-14 16:20:25 +000016#include <keys/request_key_auth-type.h>
David Howells3e301482005-06-23 22:00:56 -070017
David Howellsf1dcde92014-07-18 18:56:36 +010018static int request_key_auth_preparse(struct key_preparsed_payload *);
19static void request_key_auth_free_preparse(struct key_preparsed_payload *);
David Howellscf7f6012012-09-13 13:06:29 +010020static int request_key_auth_instantiate(struct key *,
21 struct key_preparsed_payload *);
David Howells3e301482005-06-23 22:00:56 -070022static void request_key_auth_describe(const struct key *, struct seq_file *);
David Howells04c567d2006-06-22 14:47:18 -070023static void request_key_auth_revoke(struct key *);
David Howells3e301482005-06-23 22:00:56 -070024static void request_key_auth_destroy(struct key *);
David Howellsb5f545c2006-01-08 01:02:47 -080025static long request_key_auth_read(const struct key *, char __user *, size_t);
David Howells3e301482005-06-23 22:00:56 -070026
27/*
David Howells973c9f42011-01-20 16:38:33 +000028 * The request-key authorisation key type definition.
David Howells3e301482005-06-23 22:00:56 -070029 */
30struct key_type key_type_request_key_auth = {
31 .name = ".request_key_auth",
32 .def_datalen = sizeof(struct request_key_auth),
David Howellsf1dcde92014-07-18 18:56:36 +010033 .preparse = request_key_auth_preparse,
34 .free_preparse = request_key_auth_free_preparse,
David Howells3e301482005-06-23 22:00:56 -070035 .instantiate = request_key_auth_instantiate,
36 .describe = request_key_auth_describe,
David Howells04c567d2006-06-22 14:47:18 -070037 .revoke = request_key_auth_revoke,
David Howells3e301482005-06-23 22:00:56 -070038 .destroy = request_key_auth_destroy,
David Howellsb5f545c2006-01-08 01:02:47 -080039 .read = request_key_auth_read,
David Howells3e301482005-06-23 22:00:56 -070040};
41
David Howells8da79b62014-09-16 17:07:07 +010042static int request_key_auth_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010043{
44 return 0;
45}
46
David Howells8da79b62014-09-16 17:07:07 +010047static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010048{
49}
50
David Howells3e301482005-06-23 22:00:56 -070051/*
David Howells973c9f42011-01-20 16:38:33 +000052 * Instantiate a request-key authorisation key.
David Howells3e301482005-06-23 22:00:56 -070053 */
54static int request_key_auth_instantiate(struct key *key,
David Howellscf7f6012012-09-13 13:06:29 +010055 struct key_preparsed_payload *prep)
David Howells3e301482005-06-23 22:00:56 -070056{
David Howellse59428f2019-06-19 16:10:15 +010057 rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
David Howellsb5f545c2006-01-08 01:02:47 -080058 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000059}
David Howells3e301482005-06-23 22:00:56 -070060
David Howells3e301482005-06-23 22:00:56 -070061/*
David Howells973c9f42011-01-20 16:38:33 +000062 * Describe an authorisation token.
David Howells3e301482005-06-23 22:00:56 -070063 */
64static void request_key_auth_describe(const struct key *key,
65 struct seq_file *m)
66{
David Howellse59428f2019-06-19 16:10:15 +010067 struct request_key_auth *rka = dereference_key_rcu(key);
David Howells3e301482005-06-23 22:00:56 -070068
69 seq_puts(m, "key:");
70 seq_puts(m, key->description);
David Howells363b02d2017-10-04 16:43:25 +010071 if (key_is_positive(key))
David Howells78b72802011-03-11 17:57:23 +000072 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000073}
David Howells3e301482005-06-23 22:00:56 -070074
David Howells3e301482005-06-23 22:00:56 -070075/*
David Howells973c9f42011-01-20 16:38:33 +000076 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080077 * - the key's semaphore is read-locked
78 */
79static long request_key_auth_read(const struct key *key,
80 char __user *buffer, size_t buflen)
81{
David Howellse59428f2019-06-19 16:10:15 +010082 struct request_key_auth *rka = dereference_key_locked(key);
David Howellsb5f545c2006-01-08 01:02:47 -080083 size_t datalen;
84 long ret;
85
David Howells4a38e122008-04-29 01:01:24 -070086 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080087 ret = datalen;
88
89 /* we can return the data as is */
90 if (buffer && buflen > 0) {
91 if (buflen > datalen)
92 buflen = datalen;
93
94 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
95 ret = -EFAULT;
96 }
97
98 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +000099}
David Howellsb5f545c2006-01-08 01:02:47 -0800100
Eric Biggers44d81432017-09-21 13:57:40 -0700101static void free_request_key_auth(struct request_key_auth *rka)
102{
103 if (!rka)
104 return;
105 key_put(rka->target_key);
106 key_put(rka->dest_keyring);
107 if (rka->cred)
108 put_cred(rka->cred);
109 kfree(rka->callout_info);
110 kfree(rka);
111}
112
David Howells04c567d2006-06-22 14:47:18 -0700113/*
David Howellse59428f2019-06-19 16:10:15 +0100114 * Dispose of the request_key_auth record under RCU conditions
115 */
116static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
117{
118 struct request_key_auth *rka =
119 container_of(rcu, struct request_key_auth, rcu);
120
121 free_request_key_auth(rka);
122}
123
124/*
125 * Handle revocation of an authorisation token key.
126 *
127 * Called with the key sem write-locked.
128 */
129static void request_key_auth_revoke(struct key *key)
130{
131 struct request_key_auth *rka = dereference_key_locked(key);
132
133 kenter("{%d}", key->serial);
134 rcu_assign_keypointer(key, NULL);
135 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
136}
137
138/*
David Howells973c9f42011-01-20 16:38:33 +0000139 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700140 */
141static void request_key_auth_destroy(struct key *key)
142{
David Howellse59428f2019-06-19 16:10:15 +0100143 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
David Howells3e301482005-06-23 22:00:56 -0700144
145 kenter("{%d}", key->serial);
David Howellse59428f2019-06-19 16:10:15 +0100146 if (rka) {
147 rcu_assign_keypointer(key, NULL);
148 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
149 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000150}
David Howells3e301482005-06-23 22:00:56 -0700151
David Howells3e301482005-06-23 22:00:56 -0700152/*
David Howells973c9f42011-01-20 16:38:33 +0000153 * Create an authorisation token for /sbin/request-key or whoever to gain
154 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700155 */
David Howells822ad642019-02-14 16:20:25 +0000156struct key *request_key_auth_new(struct key *target, const char *op,
157 const void *callout_info, size_t callout_len,
158 struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700159{
David Howellsb5f545c2006-01-08 01:02:47 -0800160 struct request_key_auth *rka, *irka;
David Howells7936d162019-05-22 14:09:29 +0100161 const struct cred *cred = current_cred();
David Howellsb5f545c2006-01-08 01:02:47 -0800162 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700163 char desc[20];
Eric Biggers44d81432017-09-21 13:57:40 -0700164 int ret = -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -0700165
166 kenter("%d,", target->serial);
167
David Howellsb5f545c2006-01-08 01:02:47 -0800168 /* allocate a auth record */
Eric Biggers44d81432017-09-21 13:57:40 -0700169 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
170 if (!rka)
171 goto error;
Eric Biggerse007ce92017-09-21 13:57:42 -0700172 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700173 if (!rka->callout_info)
174 goto error_free_rka;
Eric Biggerse007ce92017-09-21 13:57:42 -0700175 rka->callout_len = callout_len;
David Howells822ad642019-02-14 16:20:25 +0000176 strlcpy(rka->op, op, sizeof(rka->op));
David Howells3e301482005-06-23 22:00:56 -0700177
David Howellsb5f545c2006-01-08 01:02:47 -0800178 /* see if the calling process is already servicing the key request of
179 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100180 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800181 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100182 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700183
184 /* if the auth key has been revoked, then the key we're
185 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700186 if (test_bit(KEY_FLAG_REVOKED,
187 &cred->request_key_auth->flags)) {
188 up_read(&cred->request_key_auth->sem);
189 ret = -EKEYREVOKED;
190 goto error_free_rka;
191 }
David Howells04c567d2006-06-22 14:47:18 -0700192
David Howells146aa8b2015-10-21 14:04:48 +0100193 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100194 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800195 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700196
David Howellsd84f4f92008-11-14 10:39:23 +1100197 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800198 }
199 else {
200 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100201 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800202 rka->pid = current->pid;
203 }
204
205 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100206 rka->dest_keyring = key_get(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800207
David Howells3e301482005-06-23 22:00:56 -0700208 /* allocate the auth key */
209 sprintf(desc, "%x", target->serial);
210
David Howellsb5f545c2006-01-08 01:02:47 -0800211 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100212 cred->fsuid, cred->fsgid, cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700213 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
214 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800215 if (IS_ERR(authkey)) {
216 ret = PTR_ERR(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700217 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700218 }
219
David Howellsd84f4f92008-11-14 10:39:23 +1100220 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800221 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
222 if (ret < 0)
Eric Biggers44d81432017-09-21 13:57:40 -0700223 goto error_put_authkey;
David Howells3e301482005-06-23 22:00:56 -0700224
Elena Reshetovafff29292017-03-31 15:20:48 +0300225 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800226 return authkey;
227
Eric Biggers44d81432017-09-21 13:57:40 -0700228error_put_authkey:
David Howellsb5f545c2006-01-08 01:02:47 -0800229 key_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700230error_free_rka:
231 free_request_key_auth(rka);
232error:
David Howellsb5f545c2006-01-08 01:02:47 -0800233 kleave("= %d", ret);
234 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000235}
David Howells3e301482005-06-23 22:00:56 -0700236
David Howells3e301482005-06-23 22:00:56 -0700237/*
David Howells973c9f42011-01-20 16:38:33 +0000238 * Search the current process's keyrings for the authorisation key for
239 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700240 */
241struct key *key_get_instantiation_authkey(key_serial_t target_id)
242{
David Howellsd0a059c2013-09-24 10:35:16 +0100243 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100244 struct keyring_search_context ctx = {
245 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100246 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100247 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100248 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100249 .match_data.raw_data = description,
250 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100251 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
252 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100253 };
David Howellsb5f545c2006-01-08 01:02:47 -0800254 struct key *authkey;
255 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700256
Eric Biggersede0fa982019-02-22 15:36:18 +0000257 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
David Howellsd0a059c2013-09-24 10:35:16 +0100258
David Howellse59428f2019-06-19 16:10:15 +0100259 rcu_read_lock();
260 authkey_ref = search_process_keyrings_rcu(&ctx);
261 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700262
David Howellsb5f545c2006-01-08 01:02:47 -0800263 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800264 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100265 if (authkey == ERR_PTR(-EAGAIN))
266 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800267 goto error;
268 }
David Howells3e301482005-06-23 22:00:56 -0700269
David Howellsb5f545c2006-01-08 01:02:47 -0800270 authkey = key_ref_to_ptr(authkey_ref);
271 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
272 key_put(authkey);
273 authkey = ERR_PTR(-EKEYREVOKED);
274 }
David Howells3e301482005-06-23 22:00:56 -0700275
David Howellsb5f545c2006-01-08 01:02:47 -0800276error:
277 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000278}