blob: afc304e8b61e199074da076f1a4052d7d6f03067 [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 Howells146aa8b2015-10-21 14:04:48 +010061 key->payload.data[0] = (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 Howells822ad642019-02-14 16:20:25 +000071 struct request_key_auth *rka = get_request_key_auth(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 Howells822ad642019-02-14 16:20:25 +000086 struct request_key_auth *rka = get_request_key_auth(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
David Howellsb5f545c2006-01-08 01:02:47 -0800105/*
David Howells973c9f42011-01-20 16:38:33 +0000106 * Handle revocation of an authorisation token key.
107 *
108 * Called with the key sem write-locked.
David Howells04c567d2006-06-22 14:47:18 -0700109 */
110static void request_key_auth_revoke(struct key *key)
111{
David Howells822ad642019-02-14 16:20:25 +0000112 struct request_key_auth *rka = get_request_key_auth(key);
David Howells04c567d2006-06-22 14:47:18 -0700113
114 kenter("{%d}", key->serial);
115
David Howellsd84f4f92008-11-14 10:39:23 +1100116 if (rka->cred) {
117 put_cred(rka->cred);
118 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700119 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000120}
David Howells04c567d2006-06-22 14:47:18 -0700121
Eric Biggers44d81432017-09-21 13:57:40 -0700122static void free_request_key_auth(struct request_key_auth *rka)
123{
124 if (!rka)
125 return;
126 key_put(rka->target_key);
127 key_put(rka->dest_keyring);
128 if (rka->cred)
129 put_cred(rka->cred);
130 kfree(rka->callout_info);
131 kfree(rka);
132}
133
David Howells04c567d2006-06-22 14:47:18 -0700134/*
David Howells973c9f42011-01-20 16:38:33 +0000135 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700136 */
137static void request_key_auth_destroy(struct key *key)
138{
David Howells822ad642019-02-14 16:20:25 +0000139 struct request_key_auth *rka = get_request_key_auth(key);
David Howells3e301482005-06-23 22:00:56 -0700140
141 kenter("{%d}", key->serial);
142
Eric Biggers44d81432017-09-21 13:57:40 -0700143 free_request_key_auth(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000144}
David Howells3e301482005-06-23 22:00:56 -0700145
David Howells3e301482005-06-23 22:00:56 -0700146/*
David Howells973c9f42011-01-20 16:38:33 +0000147 * Create an authorisation token for /sbin/request-key or whoever to gain
148 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700149 */
David Howells822ad642019-02-14 16:20:25 +0000150struct key *request_key_auth_new(struct key *target, const char *op,
151 const void *callout_info, size_t callout_len,
152 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;
Eric Biggerse007ce92017-09-21 13:57:42 -0700166 rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
Eric Biggers44d81432017-09-21 13:57:40 -0700167 if (!rka->callout_info)
168 goto error_free_rka;
Eric Biggerse007ce92017-09-21 13:57:42 -0700169 rka->callout_len = callout_len;
David Howells822ad642019-02-14 16:20:25 +0000170 strlcpy(rka->op, op, sizeof(rka->op));
David Howells3e301482005-06-23 22:00:56 -0700171
David Howellsb5f545c2006-01-08 01:02:47 -0800172 /* see if the calling process is already servicing the key request of
173 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100174 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800175 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100176 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700177
178 /* if the auth key has been revoked, then the key we're
179 * servicing is already instantiated */
Eric Biggers44d81432017-09-21 13:57:40 -0700180 if (test_bit(KEY_FLAG_REVOKED,
181 &cred->request_key_auth->flags)) {
182 up_read(&cred->request_key_auth->sem);
183 ret = -EKEYREVOKED;
184 goto error_free_rka;
185 }
David Howells04c567d2006-06-22 14:47:18 -0700186
David Howells146aa8b2015-10-21 14:04:48 +0100187 irka = cred->request_key_auth->payload.data[0];
David Howellsd84f4f92008-11-14 10:39:23 +1100188 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800189 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700190
David Howellsd84f4f92008-11-14 10:39:23 +1100191 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800192 }
193 else {
194 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100195 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800196 rka->pid = current->pid;
197 }
198
199 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100200 rka->dest_keyring = key_get(dest_keyring);
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_put(authkey);
Eric Biggers44d81432017-09-21 13:57:40 -0700224error_free_rka:
225 free_request_key_auth(rka);
226error:
David Howellsb5f545c2006-01-08 01:02:47 -0800227 kleave("= %d", ret);
228 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000229}
David Howells3e301482005-06-23 22:00:56 -0700230
David Howells3e301482005-06-23 22:00:56 -0700231/*
David Howells973c9f42011-01-20 16:38:33 +0000232 * Search the current process's keyrings for the authorisation key for
233 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700234 */
235struct key *key_get_instantiation_authkey(key_serial_t target_id)
236{
David Howellsd0a059c2013-09-24 10:35:16 +0100237 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100238 struct keyring_search_context ctx = {
239 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100240 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100241 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100242 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100243 .match_data.raw_data = description,
244 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells054f6182014-12-01 22:52:50 +0000245 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100246 };
David Howellsb5f545c2006-01-08 01:02:47 -0800247 struct key *authkey;
248 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700249
David Howellsd0a059c2013-09-24 10:35:16 +0100250 sprintf(description, "%x", target_id);
251
David Howells4bdf0bc2013-09-24 10:35:15 +0100252 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700253
David Howellsb5f545c2006-01-08 01:02:47 -0800254 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800255 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100256 if (authkey == ERR_PTR(-EAGAIN))
257 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800258 goto error;
259 }
David Howells3e301482005-06-23 22:00:56 -0700260
David Howellsb5f545c2006-01-08 01:02:47 -0800261 authkey = key_ref_to_ptr(authkey_ref);
262 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
263 key_put(authkey);
264 authkey = ERR_PTR(-EKEYREVOKED);
265 }
David Howells3e301482005-06-23 22:00:56 -0700266
David Howellsb5f545c2006-01-08 01:02:47 -0800267error:
268 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000269}