blob: 69d6b3b35470ce50b76bc4ca3302d9714cba1fa4 [file] [log] [blame]
David Howells973c9f42011-01-20 16:38:33 +00001/* Request key authorisation token key definition.
David Howells3e301482005-06-23 22:00:56 -07002 *
3 * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
4 * 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
David Howells3e301482005-06-23 22:00:56 -070012 */
13
14#include <linux/module.h>
15#include <linux/sched.h>
16#include <linux/err.h>
17#include <linux/seq_file.h>
Robert P. J. Dayfdb89bc2008-04-29 01:01:32 -070018#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080019#include <linux/uaccess.h>
David Howells3e301482005-06-23 22:00:56 -070020#include "internal.h"
David Howellsd0a059c2013-09-24 10:35:16 +010021#include <keys/user-type.h>
David Howells3e301482005-06-23 22:00:56 -070022
David Howellsf1dcde92014-07-18 18:56:36 +010023static int request_key_auth_preparse(struct key_preparsed_payload *);
24static void request_key_auth_free_preparse(struct key_preparsed_payload *);
David Howellscf7f6012012-09-13 13:06:29 +010025static int request_key_auth_instantiate(struct key *,
26 struct key_preparsed_payload *);
David Howells3e301482005-06-23 22:00:56 -070027static void request_key_auth_describe(const struct key *, struct seq_file *);
David Howells04c567d2006-06-22 14:47:18 -070028static void request_key_auth_revoke(struct key *);
David Howells3e301482005-06-23 22:00:56 -070029static void request_key_auth_destroy(struct key *);
David Howellsb5f545c2006-01-08 01:02:47 -080030static long request_key_auth_read(const struct key *, char __user *, size_t);
David Howells3e301482005-06-23 22:00:56 -070031
32/*
David Howells973c9f42011-01-20 16:38:33 +000033 * The request-key authorisation key type definition.
David Howells3e301482005-06-23 22:00:56 -070034 */
35struct key_type key_type_request_key_auth = {
36 .name = ".request_key_auth",
37 .def_datalen = sizeof(struct request_key_auth),
David Howellsf1dcde92014-07-18 18:56:36 +010038 .preparse = request_key_auth_preparse,
39 .free_preparse = request_key_auth_free_preparse,
David Howells3e301482005-06-23 22:00:56 -070040 .instantiate = request_key_auth_instantiate,
41 .describe = request_key_auth_describe,
David Howells04c567d2006-06-22 14:47:18 -070042 .revoke = request_key_auth_revoke,
David Howells3e301482005-06-23 22:00:56 -070043 .destroy = request_key_auth_destroy,
David Howellsb5f545c2006-01-08 01:02:47 -080044 .read = request_key_auth_read,
David Howells3e301482005-06-23 22:00:56 -070045};
46
David Howells8da79b62014-09-16 17:07:07 +010047static int request_key_auth_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010048{
49 return 0;
50}
51
David Howells8da79b62014-09-16 17:07:07 +010052static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010053{
54}
55
David Howells3e301482005-06-23 22:00:56 -070056/*
David Howells973c9f42011-01-20 16:38:33 +000057 * Instantiate a request-key authorisation key.
David Howells3e301482005-06-23 22:00:56 -070058 */
59static int request_key_auth_instantiate(struct key *key,
David Howellscf7f6012012-09-13 13:06:29 +010060 struct key_preparsed_payload *prep)
David Howells3e301482005-06-23 22:00:56 -070061{
David Howells146aa8b2015-10-21 14:04:48 +010062 key->payload.data[0] = (struct request_key_auth *)prep->data;
David Howellsb5f545c2006-01-08 01:02:47 -080063 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000064}
David Howells3e301482005-06-23 22:00:56 -070065
David Howells3e301482005-06-23 22:00:56 -070066/*
David Howells973c9f42011-01-20 16:38:33 +000067 * Describe an authorisation token.
David Howells3e301482005-06-23 22:00:56 -070068 */
69static void request_key_auth_describe(const struct key *key,
70 struct seq_file *m)
71{
David Howells146aa8b2015-10-21 14:04:48 +010072 struct request_key_auth *rka = key->payload.data[0];
David Howells3e301482005-06-23 22:00:56 -070073
74 seq_puts(m, "key:");
75 seq_puts(m, key->description);
David Howells78b72802011-03-11 17:57:23 +000076 if (key_is_instantiated(key))
77 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000078}
David Howells3e301482005-06-23 22:00:56 -070079
David Howells3e301482005-06-23 22:00:56 -070080/*
David Howells973c9f42011-01-20 16:38:33 +000081 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080082 * - the key's semaphore is read-locked
83 */
84static long request_key_auth_read(const struct key *key,
85 char __user *buffer, size_t buflen)
86{
David Howells146aa8b2015-10-21 14:04:48 +010087 struct request_key_auth *rka = key->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -080088 size_t datalen;
89 long ret;
90
David Howells4a38e122008-04-29 01:01:24 -070091 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080092 ret = datalen;
93
94 /* we can return the data as is */
95 if (buffer && buflen > 0) {
96 if (buflen > datalen)
97 buflen = datalen;
98
99 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
100 ret = -EFAULT;
101 }
102
103 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000104}
David Howellsb5f545c2006-01-08 01:02:47 -0800105
David Howellsb5f545c2006-01-08 01:02:47 -0800106/*
David Howells973c9f42011-01-20 16:38:33 +0000107 * Handle revocation of an authorisation token key.
108 *
109 * Called with the key sem write-locked.
David Howells04c567d2006-06-22 14:47:18 -0700110 */
111static void request_key_auth_revoke(struct key *key)
112{
David Howells146aa8b2015-10-21 14:04:48 +0100113 struct request_key_auth *rka = key->payload.data[0];
David Howells04c567d2006-06-22 14:47:18 -0700114
115 kenter("{%d}", key->serial);
116
David Howellsd84f4f92008-11-14 10:39:23 +1100117 if (rka->cred) {
118 put_cred(rka->cred);
119 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700120 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000121}
David Howells04c567d2006-06-22 14:47:18 -0700122
Eric Biggers44d81432017-09-21 13:57:40 -0700123static void free_request_key_auth(struct request_key_auth *rka)
124{
125 if (!rka)
126 return;
127 key_put(rka->target_key);
128 key_put(rka->dest_keyring);
129 if (rka->cred)
130 put_cred(rka->cred);
131 kfree(rka->callout_info);
132 kfree(rka);
133}
134
David Howells04c567d2006-06-22 14:47:18 -0700135/*
David Howells973c9f42011-01-20 16:38:33 +0000136 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700137 */
138static void request_key_auth_destroy(struct key *key)
139{
David Howells146aa8b2015-10-21 14:04:48 +0100140 struct request_key_auth *rka = key->payload.data[0];
David Howells3e301482005-06-23 22:00:56 -0700141
142 kenter("{%d}", key->serial);
143
Eric Biggers44d81432017-09-21 13:57:40 -0700144 free_request_key_auth(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000145}
David Howells3e301482005-06-23 22:00:56 -0700146
David Howells3e301482005-06-23 22:00:56 -0700147/*
David Howells973c9f42011-01-20 16:38:33 +0000148 * Create an authorisation token for /sbin/request-key or whoever to gain
149 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700150 */
David Howells4a38e122008-04-29 01:01:24 -0700151struct key *request_key_auth_new(struct key *target, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100152 size_t callout_len, struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700153{
David Howellsb5f545c2006-01-08 01:02:47 -0800154 struct request_key_auth *rka, *irka;
David Howellsd84f4f92008-11-14 10:39:23 +1100155 const struct cred *cred = current->cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800156 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700157 char desc[20];
Eric Biggers44d81432017-09-21 13:57:40 -0700158 int ret = -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -0700159
160 kenter("%d,", target->serial);
161
David Howellsb5f545c2006-01-08 01:02:47 -0800162 /* allocate a auth record */
Eric Biggers44d81432017-09-21 13:57:40 -0700163 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
164 if (!rka)
165 goto error;
David Howells4a38e122008-04-29 01:01:24 -0700166 rka->callout_info = kmalloc(callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700167 if (!rka->callout_info)
168 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700169
David Howellsb5f545c2006-01-08 01:02:47 -0800170 /* see if the calling process is already servicing the key request of
171 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100172 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800173 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100174 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700175
176 /* if the auth key has been revoked, then the key we're
177 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700178 if (test_bit(KEY_FLAG_REVOKED,
179 &cred->request_key_auth->flags)) {
180 up_read(&cred->request_key_auth->sem);
181 ret = -EKEYREVOKED;
182 goto error_free_rka;
183 }
David Howells04c567d2006-06-22 14:47:18 -0700184
David Howells146aa8b2015-10-21 14:04:48 +0100185 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100186 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800187 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700188
David Howellsd84f4f92008-11-14 10:39:23 +1100189 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800190 }
191 else {
192 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100193 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800194 rka->pid = current->pid;
195 }
196
197 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100198 rka->dest_keyring = key_get(dest_keyring);
David Howells4a38e122008-04-29 01:01:24 -0700199 memcpy(rka->callout_info, callout_info, callout_len);
200 rka->callout_len = callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -0800201
David Howells3e301482005-06-23 22:00:56 -0700202 /* allocate the auth key */
203 sprintf(desc, "%x", target->serial);
204
David Howellsb5f545c2006-01-08 01:02:47 -0800205 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100206 cred->fsuid, cred->fsgid, cred,
David Howellsb5f545c2006-01-08 01:02:47 -0800207 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
David Howells5ac7eac2016-04-06 16:14:24 +0100208 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800209 if (IS_ERR(authkey)) {
210 ret = PTR_ERR(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700211 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700212 }
213
David Howellsd84f4f92008-11-14 10:39:23 +1100214 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800215 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
216 if (ret < 0)
Eric Biggers44d81432017-09-21 13:57:40 -0700217 goto error_put_authkey;
David Howells3e301482005-06-23 22:00:56 -0700218
Elena Reshetovafff29292017-03-31 15:20:48 +0300219 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800220 return authkey;
221
Eric Biggers44d81432017-09-21 13:57:40 -0700222error_put_authkey:
David Howellsb5f545c2006-01-08 01:02:47 -0800223 key_revoke(authkey);
224 key_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700225error_free_rka:
226 free_request_key_auth(rka);
227error:
David Howellsb5f545c2006-01-08 01:02:47 -0800228 kleave("= %d", ret);
229 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000230}
David Howells3e301482005-06-23 22:00:56 -0700231
David Howells3e301482005-06-23 22:00:56 -0700232/*
David Howells973c9f42011-01-20 16:38:33 +0000233 * Search the current process's keyrings for the authorisation key for
234 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700235 */
236struct key *key_get_instantiation_authkey(key_serial_t target_id)
237{
David Howellsd0a059c2013-09-24 10:35:16 +0100238 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100239 struct keyring_search_context ctx = {
240 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100241 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100242 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100243 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100244 .match_data.raw_data = description,
245 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells054f6182014-12-01 22:52:50 +0000246 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100247 };
David Howellsb5f545c2006-01-08 01:02:47 -0800248 struct key *authkey;
249 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700250
David Howellsd0a059c2013-09-24 10:35:16 +0100251 sprintf(description, "%x", target_id);
252
David Howells4bdf0bc2013-09-24 10:35:15 +0100253 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700254
David Howellsb5f545c2006-01-08 01:02:47 -0800255 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800256 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100257 if (authkey == ERR_PTR(-EAGAIN))
258 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800259 goto error;
260 }
David Howells3e301482005-06-23 22:00:56 -0700261
David Howellsb5f545c2006-01-08 01:02:47 -0800262 authkey = key_ref_to_ptr(authkey_ref);
263 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
264 key_put(authkey);
265 authkey = ERR_PTR(-EKEYREVOKED);
266 }
David Howells3e301482005-06-23 22:00:56 -0700267
David Howellsb5f545c2006-01-08 01:02:47 -0800268error:
269 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000270}