1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2010 IBM Corporation
4 * Copyright (c) 2019-2021, Linaro Limited
6 * See Documentation/security/keys/trusted-encrypted.rst
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_tpm.h>
14 #include <linux/capability.h>
15 #include <linux/err.h>
16 #include <linux/init.h>
17 #include <linux/key-type.h>
18 #include <linux/module.h>
19 #include <linux/parser.h>
20 #include <linux/random.h>
21 #include <linux/rcupdate.h>
22 #include <linux/slab.h>
23 #include <linux/static_call.h>
24 #include <linux/string.h>
25 #include <linux/uaccess.h>
27 static char *trusted_rng = "default";
28 module_param_named(rng, trusted_rng, charp, 0);
29 MODULE_PARM_DESC(rng, "Select trusted key RNG");
31 static char *trusted_key_source;
32 module_param_named(source, trusted_key_source, charp, 0);
33 MODULE_PARM_DESC(source, "Select trusted keys source (tpm, tee or caam)");
35 static const struct trusted_key_source trusted_key_sources[] = {
36 #if defined(CONFIG_TRUSTED_KEYS_TPM)
37 { "tpm", &trusted_key_tpm_ops },
39 #if defined(CONFIG_TRUSTED_KEYS_TEE)
40 { "tee", &trusted_key_tee_ops },
42 #if defined(CONFIG_TRUSTED_KEYS_CAAM)
43 { "caam", &trusted_key_caam_ops },
47 DEFINE_STATIC_CALL_NULL(trusted_key_init, *trusted_key_sources[0].ops->init);
48 DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
49 DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
50 *trusted_key_sources[0].ops->unseal);
51 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
52 *trusted_key_sources[0].ops->get_random);
53 DEFINE_STATIC_CALL_NULL(trusted_key_exit, *trusted_key_sources[0].ops->exit);
54 static unsigned char migratable;
58 Opt_new, Opt_load, Opt_update,
61 static const match_table_t key_tokens = {
64 {Opt_update, "update"},
69 * datablob_parse - parse the keyctl data and fill in the
72 * On success returns 0, otherwise -EINVAL.
74 static int datablob_parse(char **datablob, struct trusted_key_payload *p)
76 substring_t args[MAX_OPT_ARGS];
83 c = strsep(datablob, " \t");
86 key_cmd = match_token(c, key_tokens, args);
89 /* first argument is key size */
90 c = strsep(datablob, " \t");
93 ret = kstrtol(c, 10, &keylen);
94 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
100 /* first argument is sealed blob */
101 c = strsep(datablob, " \t");
104 p->blob_len = strlen(c) / 2;
105 if (p->blob_len > MAX_BLOB_SIZE)
107 ret = hex2bin(p->blob, c, p->blob_len);
121 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
123 struct trusted_key_payload *p = NULL;
126 ret = key_payload_reserve(key, sizeof(*p));
129 p = kzalloc(sizeof(*p), GFP_KERNEL);
133 p->migratable = migratable;
139 * trusted_instantiate - create a new trusted key
141 * Unseal an existing trusted blob or, for a new key, get a
142 * random key, then seal and create a trusted key-type key,
143 * adding it to the specified keyring.
145 * On success, return 0. Otherwise return errno.
147 static int trusted_instantiate(struct key *key,
148 struct key_preparsed_payload *prep)
150 struct trusted_key_payload *payload = NULL;
151 size_t datalen = prep->datalen;
152 char *datablob, *orig_datablob;
157 if (datalen <= 0 || datalen > 32767 || !prep->data)
160 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
163 memcpy(datablob, prep->data, datalen);
164 datablob[datalen] = '\0';
166 payload = trusted_payload_alloc(key);
172 key_cmd = datablob_parse(&datablob, payload);
178 dump_payload(payload);
182 ret = static_call(trusted_key_unseal)(payload, datablob);
183 dump_payload(payload);
185 pr_info("key_unseal failed (%d)\n", ret);
188 key_len = payload->key_len;
189 ret = static_call(trusted_key_get_random)(payload->key,
194 if (ret != key_len) {
195 pr_info("key_create failed (%d)\n", ret);
200 ret = static_call(trusted_key_seal)(payload, datablob);
202 pr_info("key_seal failed (%d)\n", ret);
208 kfree_sensitive(orig_datablob);
210 rcu_assign_keypointer(key, payload);
212 kfree_sensitive(payload);
216 static void trusted_rcu_free(struct rcu_head *rcu)
218 struct trusted_key_payload *p;
220 p = container_of(rcu, struct trusted_key_payload, rcu);
225 * trusted_update - reseal an existing key with new PCR values
227 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
229 struct trusted_key_payload *p;
230 struct trusted_key_payload *new_p;
231 size_t datalen = prep->datalen;
232 char *datablob, *orig_datablob;
235 if (key_is_negative(key))
237 p = key->payload.data[0];
240 if (datalen <= 0 || datalen > 32767 || !prep->data)
243 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
247 new_p = trusted_payload_alloc(key);
253 memcpy(datablob, prep->data, datalen);
254 datablob[datalen] = '\0';
255 ret = datablob_parse(&datablob, new_p);
256 if (ret != Opt_update) {
258 kfree_sensitive(new_p);
262 /* copy old key values, and reseal with new pcrs */
263 new_p->migratable = p->migratable;
264 new_p->key_len = p->key_len;
265 memcpy(new_p->key, p->key, p->key_len);
269 ret = static_call(trusted_key_seal)(new_p, datablob);
271 pr_info("key_seal failed (%d)\n", ret);
272 kfree_sensitive(new_p);
276 rcu_assign_keypointer(key, new_p);
277 call_rcu(&p->rcu, trusted_rcu_free);
279 kfree_sensitive(orig_datablob);
284 * trusted_read - copy the sealed blob data to userspace in hex.
285 * On success, return to userspace the trusted key datablob size.
287 static long trusted_read(const struct key *key, char *buffer,
290 const struct trusted_key_payload *p;
294 p = dereference_key_locked(key);
298 if (buffer && buflen >= 2 * p->blob_len) {
300 for (i = 0; i < p->blob_len; i++)
301 bufp = hex_byte_pack(bufp, p->blob[i]);
303 return 2 * p->blob_len;
307 * trusted_destroy - clear and free the key's payload
309 static void trusted_destroy(struct key *key)
311 kfree_sensitive(key->payload.data[0]);
314 struct key_type key_type_trusted = {
316 .instantiate = trusted_instantiate,
317 .update = trusted_update,
318 .destroy = trusted_destroy,
319 .describe = user_describe,
320 .read = trusted_read,
322 EXPORT_SYMBOL_GPL(key_type_trusted);
324 static int kernel_get_random(unsigned char *key, size_t key_len)
326 return get_random_bytes_wait(key, key_len) ?: key_len;
329 static int __init init_trusted(void)
331 int (*get_random)(unsigned char *key, size_t key_len);
334 for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
335 if (trusted_key_source &&
336 strncmp(trusted_key_source, trusted_key_sources[i].name,
337 strlen(trusted_key_sources[i].name)))
341 * We always support trusted.rng="kernel" and "default" as
342 * well as trusted.rng=$trusted.source if the trust source
343 * defines its own get_random callback.
345 get_random = trusted_key_sources[i].ops->get_random;
346 if (trusted_rng && strcmp(trusted_rng, "default")) {
347 if (!strcmp(trusted_rng, "kernel")) {
348 get_random = kernel_get_random;
349 } else if (strcmp(trusted_rng, trusted_key_sources[i].name) ||
351 pr_warn("Unsupported RNG. Supported: kernel");
353 pr_cont(", %s", trusted_key_sources[i].name);
354 pr_cont(", default\n");
360 get_random = kernel_get_random;
362 static_call_update(trusted_key_init,
363 trusted_key_sources[i].ops->init);
364 static_call_update(trusted_key_seal,
365 trusted_key_sources[i].ops->seal);
366 static_call_update(trusted_key_unseal,
367 trusted_key_sources[i].ops->unseal);
368 static_call_update(trusted_key_get_random,
370 static_call_update(trusted_key_exit,
371 trusted_key_sources[i].ops->exit);
372 migratable = trusted_key_sources[i].ops->migratable;
374 ret = static_call(trusted_key_init)();
380 * encrypted_keys.ko depends on successful load of this module even if
381 * trusted key implementation is not found.
389 static void __exit cleanup_trusted(void)
391 static_call_cond(trusted_key_exit)();
394 late_initcall(init_trusted);
395 module_exit(cleanup_trusted);
397 MODULE_LICENSE("GPL");