blob: 7495a93b4b9024dad78d526d17feb7d07f93016f [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 Dunlapd410fa4e2011-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 Howellscf7f6012012-09-13 13:06:29 +010023static int request_key_auth_instantiate(struct key *,
24 struct key_preparsed_payload *);
David Howells3e301482005-06-23 22:00:56 -070025static void request_key_auth_describe(const struct key *, struct seq_file *);
David Howells04c567d2006-06-22 14:47:18 -070026static void request_key_auth_revoke(struct key *);
David Howells3e301482005-06-23 22:00:56 -070027static void request_key_auth_destroy(struct key *);
David Howellsb5f545c2006-01-08 01:02:47 -080028static long request_key_auth_read(const struct key *, char __user *, size_t);
David Howells3e301482005-06-23 22:00:56 -070029
30/*
David Howells973c9f42011-01-20 16:38:33 +000031 * The request-key authorisation key type definition.
David Howells3e301482005-06-23 22:00:56 -070032 */
33struct key_type key_type_request_key_auth = {
34 .name = ".request_key_auth",
35 .def_datalen = sizeof(struct request_key_auth),
36 .instantiate = request_key_auth_instantiate,
37 .describe = request_key_auth_describe,
David Howells04c567d2006-06-22 14:47:18 -070038 .revoke = request_key_auth_revoke,
David Howells3e301482005-06-23 22:00:56 -070039 .destroy = request_key_auth_destroy,
David Howellsb5f545c2006-01-08 01:02:47 -080040 .read = request_key_auth_read,
David Howells3e301482005-06-23 22:00:56 -070041};
42
David Howells3e301482005-06-23 22:00:56 -070043/*
David Howells973c9f42011-01-20 16:38:33 +000044 * Instantiate a request-key authorisation key.
David Howells3e301482005-06-23 22:00:56 -070045 */
46static int request_key_auth_instantiate(struct key *key,
David Howellscf7f6012012-09-13 13:06:29 +010047 struct key_preparsed_payload *prep)
David Howells3e301482005-06-23 22:00:56 -070048{
David Howellscf7f6012012-09-13 13:06:29 +010049 key->payload.data = (struct request_key_auth *)prep->data;
David Howellsb5f545c2006-01-08 01:02:47 -080050 return 0;
David Howellsa8b17ed2011-01-20 16:38:27 +000051}
David Howells3e301482005-06-23 22:00:56 -070052
David Howells3e301482005-06-23 22:00:56 -070053/*
David Howells973c9f42011-01-20 16:38:33 +000054 * Describe an authorisation token.
David Howells3e301482005-06-23 22:00:56 -070055 */
56static void request_key_auth_describe(const struct key *key,
57 struct seq_file *m)
58{
59 struct request_key_auth *rka = key->payload.data;
60
61 seq_puts(m, "key:");
62 seq_puts(m, key->description);
David Howells78b72802011-03-11 17:57:23 +000063 if (key_is_instantiated(key))
64 seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
David Howellsa8b17ed2011-01-20 16:38:27 +000065}
David Howells3e301482005-06-23 22:00:56 -070066
David Howells3e301482005-06-23 22:00:56 -070067/*
David Howells973c9f42011-01-20 16:38:33 +000068 * Read the callout_info data (retrieves the callout information).
David Howellsb5f545c2006-01-08 01:02:47 -080069 * - the key's semaphore is read-locked
70 */
71static long request_key_auth_read(const struct key *key,
72 char __user *buffer, size_t buflen)
73{
74 struct request_key_auth *rka = key->payload.data;
75 size_t datalen;
76 long ret;
77
David Howells4a38e122008-04-29 01:01:24 -070078 datalen = rka->callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -080079 ret = datalen;
80
81 /* we can return the data as is */
82 if (buffer && buflen > 0) {
83 if (buflen > datalen)
84 buflen = datalen;
85
86 if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
87 ret = -EFAULT;
88 }
89
90 return ret;
David Howellsa8b17ed2011-01-20 16:38:27 +000091}
David Howellsb5f545c2006-01-08 01:02:47 -080092
David Howellsb5f545c2006-01-08 01:02:47 -080093/*
David Howells973c9f42011-01-20 16:38:33 +000094 * Handle revocation of an authorisation token key.
95 *
96 * Called with the key sem write-locked.
David Howells04c567d2006-06-22 14:47:18 -070097 */
98static void request_key_auth_revoke(struct key *key)
99{
100 struct request_key_auth *rka = key->payload.data;
101
102 kenter("{%d}", key->serial);
103
David Howellsd84f4f92008-11-14 10:39:23 +1100104 if (rka->cred) {
105 put_cred(rka->cred);
106 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700107 }
David Howellsa8b17ed2011-01-20 16:38:27 +0000108}
David Howells04c567d2006-06-22 14:47:18 -0700109
David Howells04c567d2006-06-22 14:47:18 -0700110/*
David Howells973c9f42011-01-20 16:38:33 +0000111 * Destroy an instantiation authorisation token key.
David Howells3e301482005-06-23 22:00:56 -0700112 */
113static void request_key_auth_destroy(struct key *key)
114{
115 struct request_key_auth *rka = key->payload.data;
116
117 kenter("{%d}", key->serial);
118
David Howellsd84f4f92008-11-14 10:39:23 +1100119 if (rka->cred) {
120 put_cred(rka->cred);
121 rka->cred = NULL;
David Howells04c567d2006-06-22 14:47:18 -0700122 }
123
David Howells3e301482005-06-23 22:00:56 -0700124 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100125 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700126 kfree(rka->callout_info);
David Howells74fd92c2005-10-07 15:01:09 +0100127 kfree(rka);
David Howellsa8b17ed2011-01-20 16:38:27 +0000128}
David Howells3e301482005-06-23 22:00:56 -0700129
David Howells3e301482005-06-23 22:00:56 -0700130/*
David Howells973c9f42011-01-20 16:38:33 +0000131 * Create an authorisation token for /sbin/request-key or whoever to gain
132 * access to the caller's security data.
David Howells3e301482005-06-23 22:00:56 -0700133 */
David Howells4a38e122008-04-29 01:01:24 -0700134struct key *request_key_auth_new(struct key *target, const void *callout_info,
David Howells8bbf49762008-11-14 10:39:14 +1100135 size_t callout_len, struct key *dest_keyring)
David Howells3e301482005-06-23 22:00:56 -0700136{
David Howellsb5f545c2006-01-08 01:02:47 -0800137 struct request_key_auth *rka, *irka;
David Howellsd84f4f92008-11-14 10:39:23 +1100138 const struct cred *cred = current->cred;
David Howellsb5f545c2006-01-08 01:02:47 -0800139 struct key *authkey = NULL;
David Howells3e301482005-06-23 22:00:56 -0700140 char desc[20];
141 int ret;
142
143 kenter("%d,", target->serial);
144
David Howellsb5f545c2006-01-08 01:02:47 -0800145 /* allocate a auth record */
146 rka = kmalloc(sizeof(*rka), GFP_KERNEL);
147 if (!rka) {
148 kleave(" = -ENOMEM");
149 return ERR_PTR(-ENOMEM);
David Howells3e301482005-06-23 22:00:56 -0700150 }
David Howells4a38e122008-04-29 01:01:24 -0700151 rka->callout_info = kmalloc(callout_len, GFP_KERNEL);
David Howells76181c12007-10-16 23:29:46 -0700152 if (!rka->callout_info) {
153 kleave(" = -ENOMEM");
154 kfree(rka);
155 return ERR_PTR(-ENOMEM);
156 }
David Howells3e301482005-06-23 22:00:56 -0700157
David Howellsb5f545c2006-01-08 01:02:47 -0800158 /* see if the calling process is already servicing the key request of
159 * another process */
David Howellsd84f4f92008-11-14 10:39:23 +1100160 if (cred->request_key_auth) {
David Howellsb5f545c2006-01-08 01:02:47 -0800161 /* it is - use that instantiation context here too */
David Howellsd84f4f92008-11-14 10:39:23 +1100162 down_read(&cred->request_key_auth->sem);
David Howells04c567d2006-06-22 14:47:18 -0700163
164 /* if the auth key has been revoked, then the key we're
165 * servicing is already instantiated */
David Howellsd84f4f92008-11-14 10:39:23 +1100166 if (test_bit(KEY_FLAG_REVOKED, &cred->request_key_auth->flags))
David Howells04c567d2006-06-22 14:47:18 -0700167 goto auth_key_revoked;
168
David Howellsd84f4f92008-11-14 10:39:23 +1100169 irka = cred->request_key_auth->payload.data;
170 rka->cred = get_cred(irka->cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800171 rka->pid = irka->pid;
David Howells04c567d2006-06-22 14:47:18 -0700172
David Howellsd84f4f92008-11-14 10:39:23 +1100173 up_read(&cred->request_key_auth->sem);
David Howellsb5f545c2006-01-08 01:02:47 -0800174 }
175 else {
176 /* it isn't - use this process as the context */
David Howellsd84f4f92008-11-14 10:39:23 +1100177 rka->cred = get_cred(cred);
David Howellsb5f545c2006-01-08 01:02:47 -0800178 rka->pid = current->pid;
179 }
180
181 rka->target_key = key_get(target);
David Howells8bbf49762008-11-14 10:39:14 +1100182 rka->dest_keyring = key_get(dest_keyring);
David Howells4a38e122008-04-29 01:01:24 -0700183 memcpy(rka->callout_info, callout_info, callout_len);
184 rka->callout_len = callout_len;
David Howellsb5f545c2006-01-08 01:02:47 -0800185
David Howells3e301482005-06-23 22:00:56 -0700186 /* allocate the auth key */
187 sprintf(desc, "%x", target->serial);
188
David Howellsb5f545c2006-01-08 01:02:47 -0800189 authkey = key_alloc(&key_type_request_key_auth, desc,
David Howellsd84f4f92008-11-14 10:39:23 +1100190 cred->fsuid, cred->fsgid, cred,
David Howellsb5f545c2006-01-08 01:02:47 -0800191 KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
David Howells7e047ef2006-06-26 00:24:50 -0700192 KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA);
David Howellsb5f545c2006-01-08 01:02:47 -0800193 if (IS_ERR(authkey)) {
194 ret = PTR_ERR(authkey);
195 goto error_alloc;
David Howells3e301482005-06-23 22:00:56 -0700196 }
197
David Howellsd84f4f92008-11-14 10:39:23 +1100198 /* construct the auth key */
David Howellsb5f545c2006-01-08 01:02:47 -0800199 ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
200 if (ret < 0)
201 goto error_inst;
David Howells3e301482005-06-23 22:00:56 -0700202
David Howellsd84f4f92008-11-14 10:39:23 +1100203 kleave(" = {%d,%d}", authkey->serial, atomic_read(&authkey->usage));
David Howellsb5f545c2006-01-08 01:02:47 -0800204 return authkey;
205
David Howells04c567d2006-06-22 14:47:18 -0700206auth_key_revoked:
David Howellsd84f4f92008-11-14 10:39:23 +1100207 up_read(&cred->request_key_auth->sem);
David Howells76181c12007-10-16 23:29:46 -0700208 kfree(rka->callout_info);
David Howells04c567d2006-06-22 14:47:18 -0700209 kfree(rka);
210 kleave("= -EKEYREVOKED");
211 return ERR_PTR(-EKEYREVOKED);
212
David Howellsb5f545c2006-01-08 01:02:47 -0800213error_inst:
214 key_revoke(authkey);
215 key_put(authkey);
216error_alloc:
217 key_put(rka->target_key);
David Howells8bbf49762008-11-14 10:39:14 +1100218 key_put(rka->dest_keyring);
David Howells76181c12007-10-16 23:29:46 -0700219 kfree(rka->callout_info);
David Howellsb5f545c2006-01-08 01:02:47 -0800220 kfree(rka);
221 kleave("= %d", ret);
222 return ERR_PTR(ret);
David Howellsa8b17ed2011-01-20 16:38:27 +0000223}
David Howells3e301482005-06-23 22:00:56 -0700224
David Howells3e301482005-06-23 22:00:56 -0700225/*
David Howells973c9f42011-01-20 16:38:33 +0000226 * Search the current process's keyrings for the authorisation key for
227 * instantiation of a key.
David Howells3e301482005-06-23 22:00:56 -0700228 */
229struct key *key_get_instantiation_authkey(key_serial_t target_id)
230{
David Howellsd0a059c2013-09-24 10:35:16 +0100231 char description[16];
David Howells4bdf0bc2013-09-24 10:35:15 +0100232 struct keyring_search_context ctx = {
233 .index_key.type = &key_type_request_key_auth,
David Howellsd0a059c2013-09-24 10:35:16 +0100234 .index_key.description = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100235 .cred = current_cred(),
David Howellsd0a059c2013-09-24 10:35:16 +0100236 .match = user_match,
237 .match_data = description,
David Howells4bdf0bc2013-09-24 10:35:15 +0100238 .flags = KEYRING_SEARCH_LOOKUP_DIRECT,
239 };
David Howellsb5f545c2006-01-08 01:02:47 -0800240 struct key *authkey;
241 key_ref_t authkey_ref;
David Howells3e301482005-06-23 22:00:56 -0700242
David Howellsd0a059c2013-09-24 10:35:16 +0100243 sprintf(description, "%x", target_id);
244
David Howells4bdf0bc2013-09-24 10:35:15 +0100245 authkey_ref = search_process_keyrings(&ctx);
David Howells3e301482005-06-23 22:00:56 -0700246
David Howellsb5f545c2006-01-08 01:02:47 -0800247 if (IS_ERR(authkey_ref)) {
David Howellse231c2e2008-02-07 00:15:26 -0800248 authkey = ERR_CAST(authkey_ref);
David Howells4d674312011-06-13 22:33:52 +0100249 if (authkey == ERR_PTR(-EAGAIN))
250 authkey = ERR_PTR(-ENOKEY);
David Howellsb5f545c2006-01-08 01:02:47 -0800251 goto error;
252 }
David Howells3e301482005-06-23 22:00:56 -0700253
David Howellsb5f545c2006-01-08 01:02:47 -0800254 authkey = key_ref_to_ptr(authkey_ref);
255 if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
256 key_put(authkey);
257 authkey = ERR_PTR(-EKEYREVOKED);
258 }
David Howells3e301482005-06-23 22:00:56 -0700259
David Howellsb5f545c2006-01-08 01:02:47 -0800260error:
261 return authkey;
David Howellsa8b17ed2011-01-20 16:38:27 +0000262}