blob: f613987e8a63091729adf1adbdbc76b4846ac08e [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
David Howells3e301482005-06-23 22:00:56 -070014#include <linux/sched.h>
15#include <linux/err.h>
16#include <linux/seq_file.h>
Robert P. J. Dayfdb89bc2008-04-29 01:01:32 -070017#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080018#include <linux/uaccess.h>
David Howells3e301482005-06-23 22:00:56 -070019#include "internal.h"
David Howells822ad642019-02-14 16:20:25 +000020#include <keys/request_key_auth-type.h>
David Howells3e301482005-06-23 22:00:56 -070021
David Howellsf1dcde92014-07-18 18:56:36 +010022static int request_key_auth_preparse(struct key_preparsed_payload *);
23static void request_key_auth_free_preparse(struct key_preparsed_payload *);
David Howellscf7f6012012-09-13 13:06:29 +010024static int request_key_auth_instantiate(struct key *,
25 struct key_preparsed_payload *);
David Howells3e301482005-06-23 22:00:56 -070026static void request_key_auth_describe(const struct key *, struct seq_file *);
David Howells04c567d2006-06-22 14:47:18 -070027static void request_key_auth_revoke(struct key *);
David Howells3e301482005-06-23 22:00:56 -070028static void request_key_auth_destroy(struct key *);
David Howellsb5f545c2006-01-08 01:02:47 -080029static long request_key_auth_read(const struct key *, char __user *, size_t);
David Howells3e301482005-06-23 22:00:56 -070030
31/*
David Howells973c9f42011-01-20 16:38:33 +000032 * The request-key authorisation key type definition.
David Howells3e301482005-06-23 22:00:56 -070033 */
34struct key_type key_type_request_key_auth = {
35 .name = ".request_key_auth",
36 .def_datalen = sizeof(struct request_key_auth),
David Howellsf1dcde92014-07-18 18:56:36 +010037 .preparse = request_key_auth_preparse,
38 .free_preparse = request_key_auth_free_preparse,
David Howells3e301482005-06-23 22:00:56 -070039 .instantiate = request_key_auth_instantiate,
40 .describe = request_key_auth_describe,
David Howells04c567d2006-06-22 14:47:18 -070041 .revoke = request_key_auth_revoke,
David Howells3e301482005-06-23 22:00:56 -070042 .destroy = request_key_auth_destroy,
David Howellsb5f545c2006-01-08 01:02:47 -080043 .read = request_key_auth_read,
David Howells3e301482005-06-23 22:00:56 -070044};
45
David Howells8da79b62014-09-16 17:07:07 +010046static int request_key_auth_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010047{
48 return 0;
49}
50
David Howells8da79b62014-09-16 17:07:07 +010051static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
David Howellsf1dcde92014-07-18 18:56:36 +010052{
53}
54
David Howells3e301482005-06-23 22:00:56 -070055/*
David Howells973c9f42011-01-20 16:38:33 +000056 * Instantiate a request-key authorisation key.
David Howells3e301482005-06-23 22:00:56 -070057 */
58static int request_key_auth_instantiate(struct key *key,
David Howellscf7f6012012-09-13 13:06:29 +010059 struct key_preparsed_payload *prep)
David Howells3e301482005-06-23 22:00:56 -070060{
David Howellse59428f2019-06-19 16:10:15 +010061 rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
David Howellsb5f545c2006-01-08 01:02:47 -080062 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000063}
David Howells3e301482005-06-23 22:00:56 -070064
David Howells3e301482005-06-23 22:00:56 -070065/*
David Howells973c9f42011-01-20 16:38:33 +000066 * Describe an authorisation token.
David Howells3e301482005-06-23 22:00:56 -070067 */
68static void request_key_auth_describe(const struct key *key,
69 struct seq_file *m)
70{
David Howellse59428f2019-06-19 16:10:15 +010071 struct request_key_auth *rka = dereference_key_rcu(key);
David Howells3e301482005-06-23 22:00:56 -070072
73 seq_puts(m, "key:");
74 seq_puts(m, key->description);
David Howells363b02d2017-10-04 16:43:25 +010075 if (key_is_positive(key))
David Howells78b72802011-03-11 17:57:23 +000076 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000077}
David Howells3e301482005-06-23 22:00:56 -070078
David Howells3e301482005-06-23 22:00:56 -070079/*
David Howells973c9f42011-01-20 16:38:33 +000080 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080081 * - the key's semaphore is read-locked
82 */
83static long request_key_auth_read(const struct key *key,
84 char __user *buffer, size_t buflen)
85{
David Howellse59428f2019-06-19 16:10:15 +010086 struct request_key_auth *rka = dereference_key_locked(key);
David Howellsb5f545c2006-01-08 01:02:47 -080087 size_t datalen;
88 long ret;
89
David Howells4a38e122008-04-29 01:01:24 -070090 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080091 ret = datalen;
92
93 /* we can return the data as is */
94 if (buffer && buflen > 0) {
95 if (buflen > datalen)
96 buflen = datalen;
97
98 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
99 ret = -EFAULT;
100 }
101
102 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +0000103}
David Howellsb5f545c2006-01-08 01:02:47 -0800104
Eric Biggers44d81432017-09-21 13:57:40 -0700105static void free_request_key_auth(struct request_key_auth *rka)
106{
107 if (!rka)
108 return;
109 key_put(rka->target_key);
110 key_put(rka->dest_keyring);
111 if (rka->cred)
112 put_cred(rka->cred);
113 kfree(rka->callout_info);
114 kfree(rka);
115}
116
David Howells04c567d2006-06-22 14:47:18 -0700117/*
David Howellse59428f2019-06-19 16:10:15 +0100118 * Dispose of the request_key_auth record under RCU conditions
119 */
120static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
121{
122 struct request_key_auth *rka =
123 container_of(rcu, struct request_key_auth, rcu);
124
125 free_request_key_auth(rka);
126}
127
128/*
129 * Handle revocation of an authorisation token key.
130 *
131 * Called with the key sem write-locked.
132 */
133static void request_key_auth_revoke(struct key *key)
134{
135 struct request_key_auth *rka = dereference_key_locked(key);
136
137 kenter("{%d}", key->serial);
138 rcu_assign_keypointer(key, NULL);
139 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
140}
141
142/*
David Howells973c9f42011-01-20 16:38:33 +0000143 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700144 */
145static void request_key_auth_destroy(struct key *key)
146{
David Howellse59428f2019-06-19 16:10:15 +0100147 struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
David Howells3e301482005-06-23 22:00:56 -0700148
149 kenter("{%d}", key->serial);
David Howellse59428f2019-06-19 16:10:15 +0100150 if (rka) {
151 rcu_assign_keypointer(key, NULL);
152 call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
153 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000154}
David Howells3e301482005-06-23 22:00:56 -0700155
David Howells3e301482005-06-23 22:00:56 -0700156/*
David Howells973c9f42011-01-20 16:38:33 +0000157 * Create an authorisation token for /sbin/request-key or whoever to gain
158 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700159 */
David Howells822ad642019-02-14 16:20:25 +0000160struct key *request_key_auth_new(struct key *target, const char *op,
161 const void *callout_info, size_t callout_len,
162 struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700163{
David Howellsb5f545c2006-01-08 01:02:47 -0800164 struct request_key_auth *rka, *irka;
David Howells7936d162019-05-22 14:09:29 +0100165 const struct cred *cred = current_cred();
David Howellsb5f545c2006-01-08 01:02:47 -0800166 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700167 char desc[20];
Eric Biggers44d81432017-09-21 13:57:40 -0700168 int ret = -ENOMEM;
David Howells3e301482005-06-23 22:00:56 -0700169
170 kenter("%d,", target->serial);
171
David Howellsb5f545c2006-01-08 01:02:47 -0800172 /* allocate a auth record */
Eric Biggers44d81432017-09-21 13:57:40 -0700173 rka = kzalloc(sizeof(*rka), GFP_KERNEL);
174 if (!rka)
175 goto error;
Eric Biggerse007ce92017-09-21 13:57:42 -0700176 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700177 if (!rka->callout_info)
178 goto error_free_rka;
Eric Biggerse007ce92017-09-21 13:57:42 -0700179 rka->callout_len = callout_len;
David Howells822ad642019-02-14 16:20:25 +0000180 strlcpy(rka->op, op, sizeof(rka->op));
David Howells3e301482005-06-23 22:00:56 -0700181
David Howellsb5f545c2006-01-08 01:02:47 -0800182 /* see if the calling process is already servicing the key request of
183 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100184 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800185 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100186 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700187
188 /* if the auth key has been revoked, then the key we're
189 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700190 if (test_bit(KEY_FLAG_REVOKED,
191 &cred->request_key_auth->flags)) {
192 up_read(&cred->request_key_auth->sem);
193 ret = -EKEYREVOKED;
194 goto error_free_rka;
195 }
David Howells04c567d2006-06-22 14:47:18 -0700196
David Howells146aa8b2015-10-21 14:04:48 +0100197 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100198 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800199 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700200
David Howellsd84f4f92008-11-14 10:39:23 +1100201 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800202 }
203 else {
204 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100205 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800206 rka->pid = current->pid;
207 }
208
209 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100210 rka->dest_keyring = key_get(dest_keyring);
David Howellsb5f545c2006-01-08 01:02:47 -0800211
David Howells3e301482005-06-23 22:00:56 -0700212 /* allocate the auth key */
213 sprintf(desc, "%x", target->serial);
214
David Howellsb5f545c2006-01-08 01:02:47 -0800215 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100216 cred->fsuid, cred->fsgid, cred,
David Howellsf7f13942019-05-20 21:51:43 +0100217 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
David Howells5ac7eac2016-04-06 16:14:24 +0100218 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
David Howellsb5f545c2006-01-08 01:02:47 -0800219 if (IS_ERR(authkey)) {
220 ret = PTR_ERR(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700221 goto error_free_rka;
David Howells3e301482005-06-23 22:00:56 -0700222 }
223
David Howellsd84f4f92008-11-14 10:39:23 +1100224 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800225 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
226 if (ret < 0)
Eric Biggers44d81432017-09-21 13:57:40 -0700227 goto error_put_authkey;
David Howells3e301482005-06-23 22:00:56 -0700228
Elena Reshetovafff29292017-03-31 15:20:48 +0300229 kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800230 return authkey;
231
Eric Biggers44d81432017-09-21 13:57:40 -0700232error_put_authkey:
David Howellsb5f545c2006-01-08 01:02:47 -0800233 key_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700234error_free_rka:
235 free_request_key_auth(rka);
236error:
David Howellsb5f545c2006-01-08 01:02:47 -0800237 kleave("= %d", ret);
238 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000239}
David Howells3e301482005-06-23 22:00:56 -0700240
David Howells3e301482005-06-23 22:00:56 -0700241/*
David Howells973c9f42011-01-20 16:38:33 +0000242 * Search the current process's keyrings for the authorisation key for
243 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700244 */
245struct key *key_get_instantiation_authkey(key_serial_t target_id)
246{
David Howellsd0a059c2013-09-24 10:35:16 +0100247 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100248 struct keyring_search_context ctx = {
249 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100250 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100251 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100252 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100253 .match_data.raw_data = description,
254 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howellsdcf49db2019-06-26 21:02:32 +0100255 .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
256 KEYRING_SEARCH_RECURSE),
David Howells4bdf0bc2013-09-24 10:35:15 +0100257 };
David Howellsb5f545c2006-01-08 01:02:47 -0800258 struct key *authkey;
259 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700260
Eric Biggersede0fa982019-02-22 15:36:18 +0000261 ctx.index_key.desc_len = sprintf(description, "%x", target_id);
David Howellsd0a059c2013-09-24 10:35:16 +0100262
David Howellse59428f2019-06-19 16:10:15 +0100263 rcu_read_lock();
264 authkey_ref = search_process_keyrings_rcu(&ctx);
265 rcu_read_unlock();
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}