blob: ecba39c93fd91f887026c3f9c8f6bc3270fc9225 [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
Hillf Dantond41a3ef2019-09-02 13:37:29 +010069 if (!rka)
70 return;
71
David Howells3e301482005-06-23 22:00:56 -070072 seq_puts(m, "key:");
73 seq_puts(m, key->description);
David Howells363b02d2017-10-04 16:43:25 +010074 if (key_is_positive(key))
David Howells78b72802011-03-11 17:57:23 +000075 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000076}
David Howells3e301482005-06-23 22:00:56 -070077
David Howells3e301482005-06-23 22:00:56 -070078/*
David Howells973c9f42011-01-20 16:38:33 +000079 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080080 * - the key's semaphore is read-locked
81 */
82static long request_key_auth_read(const struct key *key,
83 char __user *buffer, size_t buflen)
84{
David Howellse59428f2019-06-19 16:10:15 +010085 struct request_key_auth *rka = dereference_key_locked(key);
David Howellsb5f545c2006-01-08 01:02:47 -080086 size_t datalen;
87 long ret;
88
Hillf Dantond41a3ef2019-09-02 13:37:29 +010089 if (!rka)
90 return -EKEYREVOKED;
91
David Howells4a38e122008-04-29 01:01:24 -070092 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080093 ret = datalen;
94
95 /* we can return the data as is */
96 if (buffer && buflen > 0) {
97 if (buflen > datalen)
98 buflen = datalen;
99
100 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
101 ret = -EFAULT;
102 }
103
104 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000105}
David Howellsb5f545c2006-01-08 01:02:47 -0800106
Eric Biggers44d81432017-09-21 13:57:40 -0700107static void free_request_key_auth(struct request_key_auth *rka)
108{
109 if (!rka)
110 return;
111 key_put(rka->target_key);
112 key_put(rka->dest_keyring);
113 if (rka->cred)
114 put_cred(rka->cred);
115 kfree(rka->callout_info);
116 kfree(rka);
117}
118
David Howells04c567d2006-06-22 14:47:18 -0700119/*
David Howellse59428f2019-06-19 16:10:15 +0100120 * Dispose of the request_key_auth record under RCU conditions
121 */
122static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
123{
124 struct request_key_auth *rka =
125 container_of(rcu, struct request_key_auth, rcu);
126
127 free_request_key_auth(rka);
128}
129
130/*
131 * Handle revocation of an authorisation token key.
132 *
133 * Called with the key sem write-locked.
134 */
135static void request_key_auth_revoke(struct key *key)
136{
137 struct request_key_auth *rka = dereference_key_locked(key);
138
139 kenter("{%d}", key->serial);
140 rcu_assign_keypointer(key, NULL);
141 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
142}
143
144/*
David Howells973c9f42011-01-20 16:38:33 +0000145 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700146 */
147static void request_key_auth_destroy(struct key *key)
148{
David Howellse59428f2019-06-19 16:10:15 +0100149 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
David Howells3e301482005-06-23 22:00:56 -0700150
151 kenter("{%d}", key->serial);
David Howellse59428f2019-06-19 16:10:15 +0100152 if (rka) {
153 rcu_assign_keypointer(key, NULL);
154 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
155 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000156}
David Howells3e301482005-06-23 22:00:56 -0700157
David Howells3e301482005-06-23 22:00:56 -0700158/*
David Howells973c9f42011-01-20 16:38:33 +0000159 * Create an authorisation token for /sbin/request-key or whoever to gain
160 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700161 */
David Howells822ad642019-02-14 16:20:25 +0000162struct key *request_key_auth_new(struct key *target, const char *op,
163 const void *callout_info, size_t callout_len,
164 struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700165{
David Howellsb5f545c2006-01-08 01:02:47 -0800166 struct request_key_auth *rka, *irka;
David Howells7936d162019-05-22 14:09:29 +0100167 const struct cred *cred = current_cred();
David Howellsb5f545c2006-01-08 01:02:47 -0800168 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700169 char desc[20];
Eric Biggers44d81432017-09-21 13:57:40 -0700170 int ret = -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -0700171
172 kenter("%d,", target->serial);
173
David Howellsb5f545c2006-01-08 01:02:47 -0800174 /* allocate a auth record */
Eric Biggers44d81432017-09-21 13:57:40 -0700175 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
176 if (!rka)
177 goto error;
Eric Biggerse007ce92017-09-21 13:57:42 -0700178 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700179 if (!rka->callout_info)
180 goto error_free_rka;
Eric Biggerse007ce92017-09-21 13:57:42 -0700181 rka->callout_len = callout_len;
David Howells822ad642019-02-14 16:20:25 +0000182 strlcpy(rka->op, op, sizeof(rka->op));
David Howells3e301482005-06-23 22:00:56 -0700183
David Howellsb5f545c2006-01-08 01:02:47 -0800184 /* see if the calling process is already servicing the key request of
185 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100186 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800187 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100188 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700189
190 /* if the auth key has been revoked, then the key we're
191 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700192 if (test_bit(KEY_FLAG_REVOKED,
193 &cred->request_key_auth->flags)) {
194 up_read(&cred->request_key_auth->sem);
195 ret = -EKEYREVOKED;
196 goto error_free_rka;
197 }
David Howells04c567d2006-06-22 14:47:18 -0700198
David Howells146aa8b2015-10-21 14:04:48 +0100199 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100200 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800201 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700202
David Howellsd84f4f92008-11-14 10:39:23 +1100203 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800204 }
205 else {
206 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100207 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800208 rka->pid = current->pid;
209 }
210
211 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100212 rka->dest_keyring = key_get(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800213
David Howells3e301482005-06-23 22:00:56 -0700214 /* allocate the auth key */
215 sprintf(desc, "%x", target->serial);
216
David Howellsb5f545c2006-01-08 01:02:47 -0800217 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100218 cred->fsuid, cred->fsgid, cred,
Linus Torvalds028db3e2019-07-10 18:43:43 -0700219 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
220 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800221 if (IS_ERR(authkey)) {
222 ret = PTR_ERR(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700223 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700224 }
225
David Howellsd84f4f92008-11-14 10:39:23 +1100226 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800227 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
228 if (ret < 0)
Eric Biggers44d81432017-09-21 13:57:40 -0700229 goto error_put_authkey;
David Howells3e301482005-06-23 22:00:56 -0700230
Elena Reshetovafff29292017-03-31 15:20:48 +0300231 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800232 return authkey;
233
Eric Biggers44d81432017-09-21 13:57:40 -0700234error_put_authkey:
David Howellsb5f545c2006-01-08 01:02:47 -0800235 key_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700236error_free_rka:
237 free_request_key_auth(rka);
238error:
David Howellsb5f545c2006-01-08 01:02:47 -0800239 kleave("= %d", ret);
240 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000241}
David Howells3e301482005-06-23 22:00:56 -0700242
David Howells3e301482005-06-23 22:00:56 -0700243/*
David Howells973c9f42011-01-20 16:38:33 +0000244 * Search the current process's keyrings for the authorisation key for
245 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700246 */
247struct key *key_get_instantiation_authkey(key_serial_t target_id)
248{
David Howellsd0a059c2013-09-24 10:35:16 +0100249 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100250 struct keyring_search_context ctx = {
251 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100252 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100253 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100254 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100255 .match_data.raw_data = description,
256 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100257 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
258 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100259 };
David Howellsb5f545c2006-01-08 01:02:47 -0800260 struct key *authkey;
261 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700262
Eric Biggersede0fa982019-02-22 15:36:18 +0000263 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
David Howellsd0a059c2013-09-24 10:35:16 +0100264
David Howellse59428f2019-06-19 16:10:15 +0100265 rcu_read_lock();
266 authkey_ref = search_process_keyrings_rcu(&ctx);
267 rcu_read_unlock();
David Howells3e301482005-06-23 22:00:56 -0700268
David Howellsb5f545c2006-01-08 01:02:47 -0800269 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800270 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100271 if (authkey == ERR_PTR(-EAGAIN))
272 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800273 goto error;
274 }
David Howells3e301482005-06-23 22:00:56 -0700275
David Howellsb5f545c2006-01-08 01:02:47 -0800276 authkey = key_ref_to_ptr(authkey_ref);
277 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
278 key_put(authkey);
279 authkey = ERR_PTR(-EKEYREVOKED);
280 }
David Howells3e301482005-06-23 22:00:56 -0700281
David Howellsb5f545c2006-01-08 01:02:47 -0800282error:
283 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000284}