blob: 5d672f7580dd5330516dd62f7d705cac46c319fb [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 Howellscf7f6012012-09-13 13:06:29 +010062 key->payload.data = (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{
72 struct request_key_auth *rka = key->payload.data;
73
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{
87 struct request_key_auth *rka = key->payload.data;
88 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{
113 struct request_key_auth *rka = key->payload.data;
114
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
David Howells04c567d2006-06-22 14:47:18 -0700123/*
David Howells973c9f42011-01-20 16:38:33 +0000124 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700125 */
126static void request_key_auth_destroy(struct key *key)
127{
128 struct request_key_auth *rka = key->payload.data;
129
130 kenter("{%d}", key->serial);
131
David Howellsd84f4f92008-11-14 10:39:23 +1100132 if (rka->cred) {
133 put_cred(rka->cred);
134 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700135 }
136
David Howells3e301482005-06-23 22:00:56 -0700137 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100138 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700139 kfree(rka->callout_info);
David Howells74fd92c2005-10-07 15:01:09 +0100140 kfree(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000141}
David Howells3e301482005-06-23 22:00:56 -0700142
David Howells3e301482005-06-23 22:00:56 -0700143/*
David Howells973c9f42011-01-20 16:38:33 +0000144 * Create an authorisation token for /sbin/request-key or whoever to gain
145 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700146 */
David Howells4a38e122008-04-29 01:01:24 -0700147struct key *request_key_auth_new(struct key *target, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100148 size_t callout_len, 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];
154 int ret;
155
156 kenter("%d,", target->serial);
157
David Howellsb5f545c2006-01-08 01:02:47 -0800158 /* allocate a auth record */
159 rka = kmalloc(sizeof(*rka), GFP_KERNEL);
160 if (!rka) {
161 kleave(" = -ENOMEM");
162 return ERR_PTR(-ENOMEM);
David Howells3e301482005-06-23 22:00:56 -0700163 }
David Howells4a38e122008-04-29 01:01:24 -0700164 rka->callout_info = kmalloc(callout_len, GFP_KERNEL);
David Howells76181c12007-10-16 23:29:46 -0700165 if (!rka->callout_info) {
166 kleave(" = -ENOMEM");
167 kfree(rka);
168 return ERR_PTR(-ENOMEM);
169 }
David Howells3e301482005-06-23 22:00:56 -0700170
David Howellsb5f545c2006-01-08 01:02:47 -0800171 /* see if the calling process is already servicing the key request of
172 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100173 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800174 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100175 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700176
177 /* if the auth key has been revoked, then the key we're
178 * servicing is already instantiated */
David Howellsd84f4f92008-11-14 10:39:23 +1100179 if (test_bit(KEY_FLAG_REVOKED, &cred->request_key_auth->flags))
David Howells04c567d2006-06-22 14:47:18 -0700180 goto auth_key_revoked;
181
David Howellsd84f4f92008-11-14 10:39:23 +1100182 irka = cred->request_key_auth->payload.data;
183 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800184 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700185
David Howellsd84f4f92008-11-14 10:39:23 +1100186 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800187 }
188 else {
189 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100190 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800191 rka->pid = current->pid;
192 }
193
194 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100195 rka->dest_keyring = key_get(dest_keyring);
David Howells4a38e122008-04-29 01:01:24 -0700196 memcpy(rka->callout_info, callout_info, callout_len);
197 rka->callout_len = callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -0800198
David Howells3e301482005-06-23 22:00:56 -0700199 /* allocate the auth key */
200 sprintf(desc, "%x", target->serial);
201
David Howellsb5f545c2006-01-08 01:02:47 -0800202 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100203 cred->fsuid, cred->fsgid, cred,
David Howellsb5f545c2006-01-08 01:02:47 -0800204 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
David Howells7e047ef2006-06-26 00:24:50 -0700205 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA);
David Howellsb5f545c2006-01-08 01:02:47 -0800206 if (IS_ERR(authkey)) {
207 ret = PTR_ERR(authkey);
208 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -0700209 }
210
David Howellsd84f4f92008-11-14 10:39:23 +1100211 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800212 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
213 if (ret < 0)
214 goto error_inst;
David Howells3e301482005-06-23 22:00:56 -0700215
David Howellsd84f4f92008-11-14 10:39:23 +1100216 kleave(" = {%d,%d}", authkey->serial, atomic_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800217 return authkey;
218
David Howells04c567d2006-06-22 14:47:18 -0700219auth_key_revoked:
David Howellsd84f4f92008-11-14 10:39:23 +1100220 up_read(&cred->request_key_auth->sem);
David Howells76181c12007-10-16 23:29:46 -0700221 kfree(rka->callout_info);
David Howells04c567d2006-06-22 14:47:18 -0700222 kfree(rka);
223 kleave("= -EKEYREVOKED");
224 return ERR_PTR(-EKEYREVOKED);
225
David Howellsb5f545c2006-01-08 01:02:47 -0800226error_inst:
227 key_revoke(authkey);
228 key_put(authkey);
229error_alloc:
230 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100231 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700232 kfree(rka->callout_info);
David Howellsb5f545c2006-01-08 01:02:47 -0800233 kfree(rka);
234 kleave("= %d", ret);
235 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000236}
David Howells3e301482005-06-23 22:00:56 -0700237
David Howells3e301482005-06-23 22:00:56 -0700238/*
David Howells973c9f42011-01-20 16:38:33 +0000239 * Search the current process's keyrings for the authorisation key for
240 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700241 */
242struct key *key_get_instantiation_authkey(key_serial_t target_id)
243{
David Howellsd0a059c2013-09-24 10:35:16 +0100244 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100245 struct keyring_search_context ctx = {
246 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100247 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100248 .cred = current_cred(),
David Howellsc06cfb02014-09-16 17:36:06 +0100249 .match_data.cmp = key_default_cmp,
David Howells46291952014-09-16 17:36:02 +0100250 .match_data.raw_data = description,
251 .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
David Howells054f6182014-12-01 22:52:50 +0000252 .flags = KEYRING_SEARCH_DO_STATE_CHECK,
David Howells4bdf0bc2013-09-24 10:35:15 +0100253 };
David Howellsb5f545c2006-01-08 01:02:47 -0800254 struct key *authkey;
255 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700256
David Howellsd0a059c2013-09-24 10:35:16 +0100257 sprintf(description, "%x", target_id);
258
David Howells4bdf0bc2013-09-24 10:35:15 +0100259 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700260
David Howellsb5f545c2006-01-08 01:02:47 -0800261 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800262 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100263 if (authkey == ERR_PTR(-EAGAIN))
264 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800265 goto error;
266 }
David Howells3e301482005-06-23 22:00:56 -0700267
David Howellsb5f545c2006-01-08 01:02:47 -0800268 authkey = key_ref_to_ptr(authkey_ref);
269 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
270 key_put(authkey);
271 authkey = ERR_PTR(-EKEYREVOKED);
272 }
David Howells3e301482005-06-23 22:00:56 -0700273
David Howellsb5f545c2006-01-08 01:02:47 -0800274error:
275 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000276}