blob: b47445022d5ce422cc576646079ce278942abc37 [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 *
Randy Dunlapd410fa42011-05-19 15:59:38 -070011 * See Documentation/security/keys-request-key.txt
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>
David Howellsb5f545c2006-01-08 01:02:47 -080019#include <asm/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
Hillf Dantonb94178b2019-09-02 13:37:29 +010074 if (!rka)
75 return;
76
David Howells3e301482005-06-23 22:00:56 -070077 seq_puts(m, "key:");
78 seq_puts(m, key->description);
David Howells63c8e452017-10-04 16:43:25 +010079 if (key_is_positive(key))
David Howells78b72802011-03-11 17:57:23 +000080 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000081}
David Howells3e301482005-06-23 22:00:56 -070082
David Howells3e301482005-06-23 22:00:56 -070083/*
David Howells973c9f42011-01-20 16:38:33 +000084 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080085 * - the key's semaphore is read-locked
86 */
87static long request_key_auth_read(const struct key *key,
88 char __user *buffer, size_t buflen)
89{
David Howells146aa8b2015-10-21 14:04:48 +010090 struct request_key_auth *rka = key->payload.data[0];
David Howellsb5f545c2006-01-08 01:02:47 -080091 size_t datalen;
92 long ret;
93
Hillf Dantonb94178b2019-09-02 13:37:29 +010094 if (!rka)
95 return -EKEYREVOKED;
96
David Howells4a38e122008-04-29 01:01:24 -070097 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080098 ret = datalen;
99
100 /* we can return the data as is */
101 if (buffer && buflen > 0) {
102 if (buflen > datalen)
103 buflen = datalen;
104
105 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
106 ret = -EFAULT;
107 }
108
109 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000110}
David Howellsb5f545c2006-01-08 01:02:47 -0800111
David Howellsb5f545c2006-01-08 01:02:47 -0800112/*
David Howells973c9f42011-01-20 16:38:33 +0000113 * Handle revocation of an authorisation token key.
114 *
115 * Called with the key sem write-locked.
David Howells04c567d2006-06-22 14:47:18 -0700116 */
117static void request_key_auth_revoke(struct key *key)
118{
David Howells146aa8b2015-10-21 14:04:48 +0100119 struct request_key_auth *rka = key->payload.data[0];
David Howells04c567d2006-06-22 14:47:18 -0700120
121 kenter("{%d}", key->serial);
122
David Howellsd84f4f92008-11-14 10:39:23 +1100123 if (rka->cred) {
124 put_cred(rka->cred);
125 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700126 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000127}
David Howells04c567d2006-06-22 14:47:18 -0700128
David Howells04c567d2006-06-22 14:47:18 -0700129/*
David Howells973c9f42011-01-20 16:38:33 +0000130 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700131 */
132static void request_key_auth_destroy(struct key *key)
133{
David Howells146aa8b2015-10-21 14:04:48 +0100134 struct request_key_auth *rka = key->payload.data[0];
David Howells3e301482005-06-23 22:00:56 -0700135
136 kenter("{%d}", key->serial);
137
David Howellsd84f4f92008-11-14 10:39:23 +1100138 if (rka->cred) {
139 put_cred(rka->cred);
140 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700141 }
142
David Howells3e301482005-06-23 22:00:56 -0700143 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100144 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700145 kfree(rka->callout_info);
David Howells74fd92c2005-10-07 15:01:09 +0100146 kfree(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000147}
David Howells3e301482005-06-23 22:00:56 -0700148
David Howells3e301482005-06-23 22:00:56 -0700149/*
David Howells973c9f42011-01-20 16:38:33 +0000150 * Create an authorisation token for /sbin/request-key or whoever to gain
151 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700152 */
David Howells4a38e122008-04-29 01:01:24 -0700153struct key *request_key_auth_new(struct key *target, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100154 size_t callout_len, struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700155{
David Howellsb5f545c2006-01-08 01:02:47 -0800156 struct request_key_auth *rka, *irka;
David Howellsd84f4f92008-11-14 10:39:23 +1100157 const struct cred *cred = current->cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800158 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700159 char desc[20];
160 int ret;
161
162 kenter("%d,", target->serial);
163
David Howellsb5f545c2006-01-08 01:02:47 -0800164 /* allocate a auth record */
165 rka = kmalloc(sizeof(*rka), GFP_KERNEL);
166 if (!rka) {
167 kleave(" = -ENOMEM");
168 return ERR_PTR(-ENOMEM);
David Howells3e301482005-06-23 22:00:56 -0700169 }
David Howells4a38e122008-04-29 01:01:24 -0700170 rka->callout_info = kmalloc(callout_len, GFP_KERNEL);
David Howells76181c12007-10-16 23:29:46 -0700171 if (!rka->callout_info) {
172 kleave(" = -ENOMEM");
173 kfree(rka);
174 return ERR_PTR(-ENOMEM);
175 }
David Howells3e301482005-06-23 22:00:56 -0700176
David Howellsb5f545c2006-01-08 01:02:47 -0800177 /* see if the calling process is already servicing the key request of
178 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100179 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800180 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100181 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700182
183 /* if the auth key has been revoked, then the key we're
184 * servicing is already instantiated */
David Howellsd84f4f92008-11-14 10:39:23 +1100185 if (test_bit(KEY_FLAG_REVOKED, &cred->request_key_auth->flags))
David Howells04c567d2006-06-22 14:47:18 -0700186 goto auth_key_revoked;
187
David Howells146aa8b2015-10-21 14:04:48 +0100188 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100189 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800190 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700191
David Howellsd84f4f92008-11-14 10:39:23 +1100192 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800193 }
194 else {
195 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100196 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800197 rka->pid = current->pid;
198 }
199
200 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100201 rka->dest_keyring = key_get(dest_keyring);
David Howells4a38e122008-04-29 01:01:24 -0700202 memcpy(rka->callout_info, callout_info, callout_len);
203 rka->callout_len = callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -0800204
David Howells3e301482005-06-23 22:00:56 -0700205 /* allocate the auth key */
206 sprintf(desc, "%x", target->serial);
207
David Howellsb5f545c2006-01-08 01:02:47 -0800208 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100209 cred->fsuid, cred->fsgid, cred,
David Howellsb5f545c2006-01-08 01:02:47 -0800210 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
David Howells5ac7eac2016-04-06 16:14:24 +0100211 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800212 if (IS_ERR(authkey)) {
213 ret = PTR_ERR(authkey);
214 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -0700215 }
216
David Howellsd84f4f92008-11-14 10:39:23 +1100217 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800218 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
219 if (ret < 0)
220 goto error_inst;
David Howells3e301482005-06-23 22:00:56 -0700221
David Howellsd84f4f92008-11-14 10:39:23 +1100222 kleave(" = {%d,%d}", authkey->serial, atomic_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800223 return authkey;
224
David Howells04c567d2006-06-22 14:47:18 -0700225auth_key_revoked:
David Howellsd84f4f92008-11-14 10:39:23 +1100226 up_read(&cred->request_key_auth->sem);
David Howells76181c12007-10-16 23:29:46 -0700227 kfree(rka->callout_info);
David Howells04c567d2006-06-22 14:47:18 -0700228 kfree(rka);
229 kleave("= -EKEYREVOKED");
230 return ERR_PTR(-EKEYREVOKED);
231
David Howellsb5f545c2006-01-08 01:02:47 -0800232error_inst:
233 key_revoke(authkey);
234 key_put(authkey);
235error_alloc:
236 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100237 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700238 kfree(rka->callout_info);
David Howellsb5f545c2006-01-08 01:02:47 -0800239 kfree(rka);
240 kleave("= %d", ret);
241 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000242}
David Howells3e301482005-06-23 22:00:56 -0700243
David Howells3e301482005-06-23 22:00:56 -0700244/*
David Howells973c9f42011-01-20 16:38:33 +0000245 * Search the current process's keyrings for the authorisation key for
246 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700247 */
248struct key *key_get_instantiation_authkey(key_serial_t target_id)
249{
David Howellsd0a059c2013-09-24 10:35:16 +0100250 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100251 struct keyring_search_context ctx = {
252 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100253 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100254 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100255 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100256 .match_data.raw_data = description,
257 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells054f6182014-12-01 22:52:50 +0000258 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
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 Biggersdc070cd2019-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 Howells4bdf0bc2013-09-24 10:35:15 +0100265 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700266
David Howellsb5f545c2006-01-08 01:02:47 -0800267 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800268 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100269 if (authkey == ERR_PTR(-EAGAIN))
270 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800271 goto error;
272 }
David Howells3e301482005-06-23 22:00:56 -0700273
David Howellsb5f545c2006-01-08 01:02:47 -0800274 authkey = key_ref_to_ptr(authkey_ref);
275 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
276 key_put(authkey);
277 authkey = ERR_PTR(-EKEYREVOKED);
278 }
David Howells3e301482005-06-23 22:00:56 -0700279
David Howellsb5f545c2006-01-08 01:02:47 -0800280error:
281 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000282}