Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
torvalds
GitHub Repository: torvalds/linux
Path: blob/master/security/keys/trusted-keys/trusted_core.c
50682 views
1
// SPDX-License-Identifier: GPL-2.0-only
2
/*
3
* Copyright (C) 2010 IBM Corporation
4
* Copyright (c) 2019-2021, Linaro Limited
5
*
6
* See Documentation/security/keys/trusted-encrypted.rst
7
*/
8
9
#include <keys/user-type.h>
10
#include <keys/trusted-type.h>
11
#include <keys/trusted_tee.h>
12
#include <keys/trusted_caam.h>
13
#include <keys/trusted_dcp.h>
14
#include <keys/trusted_tpm.h>
15
#include <keys/trusted_pkwm.h>
16
#include <linux/capability.h>
17
#include <linux/err.h>
18
#include <linux/init.h>
19
#include <linux/key-type.h>
20
#include <linux/module.h>
21
#include <linux/parser.h>
22
#include <linux/random.h>
23
#include <linux/rcupdate.h>
24
#include <linux/slab.h>
25
#include <linux/static_call.h>
26
#include <linux/string.h>
27
#include <linux/uaccess.h>
28
29
static char *trusted_rng = "default";
30
module_param_named(rng, trusted_rng, charp, 0);
31
MODULE_PARM_DESC(rng, "Select trusted key RNG");
32
33
static char *trusted_key_source;
34
module_param_named(source, trusted_key_source, charp, 0);
35
MODULE_PARM_DESC(source, "Select trusted keys source (tpm, tee, caam, dcp or pkwm)");
36
37
static const struct trusted_key_source trusted_key_sources[] = {
38
#if defined(CONFIG_TRUSTED_KEYS_TPM)
39
{ "tpm", &trusted_key_tpm_ops },
40
#endif
41
#if defined(CONFIG_TRUSTED_KEYS_TEE)
42
{ "tee", &trusted_key_tee_ops },
43
#endif
44
#if defined(CONFIG_TRUSTED_KEYS_CAAM)
45
{ "caam", &trusted_key_caam_ops },
46
#endif
47
#if defined(CONFIG_TRUSTED_KEYS_DCP)
48
{ "dcp", &dcp_trusted_key_ops },
49
#endif
50
#if defined(CONFIG_TRUSTED_KEYS_PKWM)
51
{ "pkwm", &pkwm_trusted_key_ops },
52
#endif
53
};
54
55
DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
56
DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
57
*trusted_key_sources[0].ops->unseal);
58
DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
59
*trusted_key_sources[0].ops->get_random);
60
static void (*trusted_key_exit)(void);
61
static unsigned char migratable;
62
63
enum {
64
Opt_err,
65
Opt_new, Opt_load, Opt_update,
66
};
67
68
static const match_table_t key_tokens = {
69
{Opt_new, "new"},
70
{Opt_load, "load"},
71
{Opt_update, "update"},
72
{Opt_err, NULL}
73
};
74
75
/*
76
* datablob_parse - parse the keyctl data and fill in the
77
* payload structure
78
*
79
* On success returns 0, otherwise -EINVAL.
80
*/
81
static int datablob_parse(char **datablob, struct trusted_key_payload *p)
82
{
83
substring_t args[MAX_OPT_ARGS];
84
long keylen;
85
int ret = -EINVAL;
86
int key_cmd;
87
char *c;
88
89
/* main command */
90
c = strsep(datablob, " \t");
91
if (!c)
92
return -EINVAL;
93
key_cmd = match_token(c, key_tokens, args);
94
switch (key_cmd) {
95
case Opt_new:
96
/* first argument is key size */
97
c = strsep(datablob, " \t");
98
if (!c)
99
return -EINVAL;
100
ret = kstrtol(c, 10, &keylen);
101
if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
102
return -EINVAL;
103
p->key_len = keylen;
104
ret = Opt_new;
105
break;
106
case Opt_load:
107
/* first argument is sealed blob */
108
c = strsep(datablob, " \t");
109
if (!c)
110
return -EINVAL;
111
p->blob_len = strlen(c) / 2;
112
if (p->blob_len > MAX_BLOB_SIZE)
113
return -EINVAL;
114
ret = hex2bin(p->blob, c, p->blob_len);
115
if (ret < 0)
116
return -EINVAL;
117
ret = Opt_load;
118
break;
119
case Opt_update:
120
ret = Opt_update;
121
break;
122
case Opt_err:
123
return -EINVAL;
124
}
125
return ret;
126
}
127
128
static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
129
{
130
struct trusted_key_payload *p = NULL;
131
int ret;
132
133
ret = key_payload_reserve(key, sizeof(*p));
134
if (ret < 0)
135
goto err;
136
p = kzalloc(sizeof(*p), GFP_KERNEL);
137
if (!p)
138
goto err;
139
140
p->migratable = migratable;
141
err:
142
return p;
143
}
144
145
/*
146
* trusted_instantiate - create a new trusted key
147
*
148
* Unseal an existing trusted blob or, for a new key, get a
149
* random key, then seal and create a trusted key-type key,
150
* adding it to the specified keyring.
151
*
152
* On success, return 0. Otherwise return errno.
153
*/
154
static int trusted_instantiate(struct key *key,
155
struct key_preparsed_payload *prep)
156
{
157
struct trusted_key_payload *payload = NULL;
158
size_t datalen = prep->datalen;
159
char *datablob, *orig_datablob;
160
int ret = 0;
161
int key_cmd;
162
size_t key_len;
163
164
if (datalen == 0 || datalen > 32767 || !prep->data)
165
return -EINVAL;
166
167
orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
168
if (!datablob)
169
return -ENOMEM;
170
memcpy(datablob, prep->data, datalen);
171
datablob[datalen] = '\0';
172
173
payload = trusted_payload_alloc(key);
174
if (!payload) {
175
ret = -ENOMEM;
176
goto out;
177
}
178
179
key_cmd = datablob_parse(&datablob, payload);
180
if (key_cmd < 0) {
181
ret = key_cmd;
182
goto out;
183
}
184
185
dump_payload(payload);
186
187
switch (key_cmd) {
188
case Opt_load:
189
ret = static_call(trusted_key_unseal)(payload, datablob);
190
dump_payload(payload);
191
if (ret < 0)
192
pr_info("key_unseal failed (%d)\n", ret);
193
break;
194
case Opt_new:
195
key_len = payload->key_len;
196
ret = static_call(trusted_key_get_random)(payload->key,
197
key_len);
198
if (ret < 0)
199
goto out;
200
201
if (ret != key_len) {
202
pr_info("key_create failed (%d)\n", ret);
203
ret = -EIO;
204
goto out;
205
}
206
207
ret = static_call(trusted_key_seal)(payload, datablob);
208
if (ret < 0)
209
pr_info("key_seal failed (%d)\n", ret);
210
break;
211
default:
212
ret = -EINVAL;
213
}
214
out:
215
kfree_sensitive(orig_datablob);
216
if (!ret)
217
rcu_assign_keypointer(key, payload);
218
else
219
kfree_sensitive(payload);
220
return ret;
221
}
222
223
static void trusted_rcu_free(struct rcu_head *rcu)
224
{
225
struct trusted_key_payload *p;
226
227
p = container_of(rcu, struct trusted_key_payload, rcu);
228
kfree_sensitive(p);
229
}
230
231
/*
232
* trusted_update - reseal an existing key with new PCR values
233
*/
234
static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
235
{
236
struct trusted_key_payload *p;
237
struct trusted_key_payload *new_p;
238
size_t datalen = prep->datalen;
239
char *datablob, *orig_datablob;
240
int ret = 0;
241
242
if (key_is_negative(key))
243
return -ENOKEY;
244
p = key->payload.data[0];
245
if (!p->migratable)
246
return -EPERM;
247
if (datalen == 0 || datalen > 32767 || !prep->data)
248
return -EINVAL;
249
250
orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
251
if (!datablob)
252
return -ENOMEM;
253
254
new_p = trusted_payload_alloc(key);
255
if (!new_p) {
256
ret = -ENOMEM;
257
goto out;
258
}
259
260
memcpy(datablob, prep->data, datalen);
261
datablob[datalen] = '\0';
262
ret = datablob_parse(&datablob, new_p);
263
if (ret != Opt_update) {
264
ret = -EINVAL;
265
kfree_sensitive(new_p);
266
goto out;
267
}
268
269
/* copy old key values, and reseal with new pcrs */
270
new_p->migratable = p->migratable;
271
new_p->key_len = p->key_len;
272
memcpy(new_p->key, p->key, p->key_len);
273
dump_payload(p);
274
dump_payload(new_p);
275
276
ret = static_call(trusted_key_seal)(new_p, datablob);
277
if (ret < 0) {
278
pr_info("key_seal failed (%d)\n", ret);
279
kfree_sensitive(new_p);
280
goto out;
281
}
282
283
rcu_assign_keypointer(key, new_p);
284
call_rcu(&p->rcu, trusted_rcu_free);
285
out:
286
kfree_sensitive(orig_datablob);
287
return ret;
288
}
289
290
/*
291
* trusted_read - copy the sealed blob data to userspace in hex.
292
* On success, return to userspace the trusted key datablob size.
293
*/
294
static long trusted_read(const struct key *key, char *buffer,
295
size_t buflen)
296
{
297
const struct trusted_key_payload *p;
298
char *bufp;
299
int i;
300
301
p = dereference_key_locked(key);
302
if (!p)
303
return -EINVAL;
304
305
if (buffer && buflen >= 2 * p->blob_len) {
306
bufp = buffer;
307
for (i = 0; i < p->blob_len; i++)
308
bufp = hex_byte_pack(bufp, p->blob[i]);
309
}
310
return 2 * p->blob_len;
311
}
312
313
/*
314
* trusted_destroy - clear and free the key's payload
315
*/
316
static void trusted_destroy(struct key *key)
317
{
318
kfree_sensitive(key->payload.data[0]);
319
}
320
321
struct key_type key_type_trusted = {
322
.name = "trusted",
323
.instantiate = trusted_instantiate,
324
.update = trusted_update,
325
.destroy = trusted_destroy,
326
.describe = user_describe,
327
.read = trusted_read,
328
};
329
EXPORT_SYMBOL_GPL(key_type_trusted);
330
331
static int kernel_get_random(unsigned char *key, size_t key_len)
332
{
333
return get_random_bytes_wait(key, key_len) ?: key_len;
334
}
335
336
static int __init init_trusted(void)
337
{
338
int (*get_random)(unsigned char *key, size_t key_len);
339
int i, ret = 0;
340
341
for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
342
if (trusted_key_source &&
343
strncmp(trusted_key_source, trusted_key_sources[i].name,
344
strlen(trusted_key_sources[i].name)))
345
continue;
346
347
/*
348
* We always support trusted.rng="kernel" and "default" as
349
* well as trusted.rng=$trusted.source if the trust source
350
* defines its own get_random callback.
351
*/
352
get_random = trusted_key_sources[i].ops->get_random;
353
if (trusted_rng && strcmp(trusted_rng, "default")) {
354
if (!strcmp(trusted_rng, "kernel")) {
355
get_random = kernel_get_random;
356
} else if (strcmp(trusted_rng, trusted_key_sources[i].name) ||
357
!get_random) {
358
pr_warn("Unsupported RNG. Supported: kernel");
359
if (get_random)
360
pr_cont(", %s", trusted_key_sources[i].name);
361
pr_cont(", default\n");
362
return -EINVAL;
363
}
364
}
365
366
if (!get_random)
367
get_random = kernel_get_random;
368
369
ret = trusted_key_sources[i].ops->init();
370
if (!ret) {
371
static_call_update(trusted_key_seal, trusted_key_sources[i].ops->seal);
372
static_call_update(trusted_key_unseal, trusted_key_sources[i].ops->unseal);
373
static_call_update(trusted_key_get_random, get_random);
374
375
trusted_key_exit = trusted_key_sources[i].ops->exit;
376
migratable = trusted_key_sources[i].ops->migratable;
377
}
378
379
if (!ret || ret != -ENODEV)
380
break;
381
}
382
383
/*
384
* encrypted_keys.ko depends on successful load of this module even if
385
* trusted key implementation is not found.
386
*/
387
if (ret == -ENODEV)
388
return 0;
389
390
return ret;
391
}
392
393
static void __exit cleanup_trusted(void)
394
{
395
if (trusted_key_exit)
396
(*trusted_key_exit)();
397
}
398
399
late_initcall(init_trusted);
400
module_exit(cleanup_trusted);
401
402
MODULE_DESCRIPTION("Trusted Key type");
403
MODULE_LICENSE("GPL");
404
405