blob: e45b5cf3b97fdae76128baa2445a305488078b3b [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 Howells146aa8b2015-10-21 14:04:48 +010057 key->payload.data[0] = (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 Howells822ad642019-02-14 16:20:25 +000067 struct request_key_auth *rka = get_request_key_auth(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 Howells822ad642019-02-14 16:20:25 +000082 struct request_key_auth *rka = get_request_key_auth(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
David Howellsb5f545c2006-01-08 01:02:47 -0800101/*
David Howells973c9f42011-01-20 16:38:33 +0000102 * Handle revocation of an authorisation token key.
103 *
104 * Called with the key sem write-locked.
David Howells04c567d2006-06-22 14:47:18 -0700105 */
106static void request_key_auth_revoke(struct key *key)
107{
David Howells822ad642019-02-14 16:20:25 +0000108 struct request_key_auth *rka = get_request_key_auth(key);
David Howells04c567d2006-06-22 14:47:18 -0700109
110 kenter("{%d}", key->serial);
111
David Howellsd84f4f92008-11-14 10:39:23 +1100112 if (rka->cred) {
113 put_cred(rka->cred);
114 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700115 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000116}
David Howells04c567d2006-06-22 14:47:18 -0700117
Eric Biggers44d81432017-09-21 13:57:40 -0700118static void free_request_key_auth(struct request_key_auth *rka)
119{
120 if (!rka)
121 return;
122 key_put(rka->target_key);
123 key_put(rka->dest_keyring);
124 if (rka->cred)
125 put_cred(rka->cred);
126 kfree(rka->callout_info);
127 kfree(rka);
128}
129
David Howells04c567d2006-06-22 14:47:18 -0700130/*
David Howells973c9f42011-01-20 16:38:33 +0000131 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700132 */
133static void request_key_auth_destroy(struct key *key)
134{
David Howells822ad642019-02-14 16:20:25 +0000135 struct request_key_auth *rka = get_request_key_auth(key);
David Howells3e301482005-06-23 22:00:56 -0700136
137 kenter("{%d}", key->serial);
138
Eric Biggers44d81432017-09-21 13:57:40 -0700139 free_request_key_auth(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000140}
David Howells3e301482005-06-23 22:00:56 -0700141
David Howells3e301482005-06-23 22:00:56 -0700142/*
David Howells973c9f42011-01-20 16:38:33 +0000143 * Create an authorisation token for /sbin/request-key or whoever to gain
144 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700145 */
David Howells822ad642019-02-14 16:20:25 +0000146struct key *request_key_auth_new(struct key *target, const char *op,
147 const void *callout_info, size_t callout_len,
148 struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700149{
David Howellsb5f545c2006-01-08 01:02:47 -0800150 struct request_key_auth *rka, *irka;
David Howellsd84f4f92008-11-14 10:39:23 +1100151 const struct cred *cred = current->cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800152 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700153 char desc[20];
Eric Biggers44d81432017-09-21 13:57:40 -0700154 int ret = -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -0700155
156 kenter("%d,", target->serial);
157
David Howellsb5f545c2006-01-08 01:02:47 -0800158 /* allocate a auth record */
Eric Biggers44d81432017-09-21 13:57:40 -0700159 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
160 if (!rka)
161 goto error;
Eric Biggerse007ce92017-09-21 13:57:42 -0700162 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700163 if (!rka->callout_info)
164 goto error_free_rka;
Eric Biggerse007ce92017-09-21 13:57:42 -0700165 rka->callout_len = callout_len;
David Howells822ad642019-02-14 16:20:25 +0000166 strlcpy(rka->op, op, sizeof(rka->op));
David Howells3e301482005-06-23 22:00:56 -0700167
David Howellsb5f545c2006-01-08 01:02:47 -0800168 /* see if the calling process is already servicing the key request of
169 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100170 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800171 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100172 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700173
174 /* if the auth key has been revoked, then the key we're
175 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700176 if (test_bit(KEY_FLAG_REVOKED,
177 &cred->request_key_auth->flags)) {
178 up_read(&cred->request_key_auth->sem);
179 ret = -EKEYREVOKED;
180 goto error_free_rka;
181 }
David Howells04c567d2006-06-22 14:47:18 -0700182
David Howells146aa8b2015-10-21 14:04:48 +0100183 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100184 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800185 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700186
David Howellsd84f4f92008-11-14 10:39:23 +1100187 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800188 }
189 else {
190 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100191 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800192 rka->pid = current->pid;
193 }
194
195 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100196 rka->dest_keyring = key_get(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800197
David Howells3e301482005-06-23 22:00:56 -0700198 /* allocate the auth key */
199 sprintf(desc, "%x", target->serial);
200
David Howellsb5f545c2006-01-08 01:02:47 -0800201 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100202 cred->fsuid, cred->fsgid, cred,
David Howellsb5f545c2006-01-08 01:02:47 -0800203 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
David Howells5ac7eac2016-04-06 16:14:24 +0100204 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800205 if (IS_ERR(authkey)) {
206 ret = PTR_ERR(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700207 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700208 }
209
David Howellsd84f4f92008-11-14 10:39:23 +1100210 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800211 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
212 if (ret < 0)
Eric Biggers44d81432017-09-21 13:57:40 -0700213 goto error_put_authkey;
David Howells3e301482005-06-23 22:00:56 -0700214
Elena Reshetovafff29292017-03-31 15:20:48 +0300215 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800216 return authkey;
217
Eric Biggers44d81432017-09-21 13:57:40 -0700218error_put_authkey:
David Howellsb5f545c2006-01-08 01:02:47 -0800219 key_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700220error_free_rka:
221 free_request_key_auth(rka);
222error:
David Howellsb5f545c2006-01-08 01:02:47 -0800223 kleave("= %d", ret);
224 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000225}
David Howells3e301482005-06-23 22:00:56 -0700226
David Howells3e301482005-06-23 22:00:56 -0700227/*
David Howells973c9f42011-01-20 16:38:33 +0000228 * Search the current process's keyrings for the authorisation key for
229 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700230 */
231struct key *key_get_instantiation_authkey(key_serial_t target_id)
232{
David Howellsd0a059c2013-09-24 10:35:16 +0100233 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100234 struct keyring_search_context ctx = {
235 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100236 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100237 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100238 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100239 .match_data.raw_data = description,
240 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells054f6182014-12-01 22:52:50 +0000241 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100242 };
David Howellsb5f545c2006-01-08 01:02:47 -0800243 struct key *authkey;
244 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700245
Eric Biggersede0fa982019-02-22 15:36:18 +0000246 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
David Howellsd0a059c2013-09-24 10:35:16 +0100247
David Howells4bdf0bc2013-09-24 10:35:15 +0100248 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700249
David Howellsb5f545c2006-01-08 01:02:47 -0800250 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800251 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100252 if (authkey == ERR_PTR(-EAGAIN))
253 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800254 goto error;
255 }
David Howells3e301482005-06-23 22:00:56 -0700256
David Howellsb5f545c2006-01-08 01:02:47 -0800257 authkey = key_ref_to_ptr(authkey_ref);
258 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
259 key_put(authkey);
260 authkey = ERR_PTR(-EKEYREVOKED);
261 }
David Howells3e301482005-06-23 22:00:56 -0700262
David Howellsb5f545c2006-01-08 01:02:47 -0800263error:
264 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000265}