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_tpm.h>
13 #include <linux/capability.h>
14 #include <linux/err.h>
15 #include <linux/init.h>
16 #include <linux/key-type.h>
17 #include <linux/module.h>
18 #include <linux/parser.h>
19 #include <linux/rcupdate.h>
20 #include <linux/slab.h>
21 #include <linux/static_call.h>
22 #include <linux/string.h>
23 #include <linux/uaccess.h>
25 static char *trusted_key_source;
26 module_param_named(source, trusted_key_source, charp, 0);
27 MODULE_PARM_DESC(source, "Select trusted keys source (tpm or tee)");
29 static const struct trusted_key_source trusted_key_sources[] = {
30 #if defined(CONFIG_TCG_TPM)
31 { "tpm", &trusted_key_tpm_ops },
33 #if defined(CONFIG_TEE)
34 { "tee", &trusted_key_tee_ops },
38 DEFINE_STATIC_CALL_NULL(trusted_key_init, *trusted_key_sources[0].ops->init);
39 DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
40 DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
41 *trusted_key_sources[0].ops->unseal);
42 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
43 *trusted_key_sources[0].ops->get_random);
44 DEFINE_STATIC_CALL_NULL(trusted_key_exit, *trusted_key_sources[0].ops->exit);
45 static unsigned char migratable;
49 Opt_new, Opt_load, Opt_update,
52 static const match_table_t key_tokens = {
55 {Opt_update, "update"},
60 * datablob_parse - parse the keyctl data and fill in the
63 * On success returns 0, otherwise -EINVAL.
65 static int datablob_parse(char *datablob, struct trusted_key_payload *p)
67 substring_t args[MAX_OPT_ARGS];
74 c = strsep(&datablob, " \t");
77 key_cmd = match_token(c, key_tokens, args);
80 /* first argument is key size */
81 c = strsep(&datablob, " \t");
84 ret = kstrtol(c, 10, &keylen);
85 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
91 /* first argument is sealed blob */
92 c = strsep(&datablob, " \t");
95 p->blob_len = strlen(c) / 2;
96 if (p->blob_len > MAX_BLOB_SIZE)
98 ret = hex2bin(p->blob, c, p->blob_len);
112 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
114 struct trusted_key_payload *p = NULL;
117 ret = key_payload_reserve(key, sizeof(*p));
120 p = kzalloc(sizeof(*p), GFP_KERNEL);
122 p->migratable = migratable;
128 * trusted_instantiate - create a new trusted key
130 * Unseal an existing trusted blob or, for a new key, get a
131 * random key, then seal and create a trusted key-type key,
132 * adding it to the specified keyring.
134 * On success, return 0. Otherwise return errno.
136 static int trusted_instantiate(struct key *key,
137 struct key_preparsed_payload *prep)
139 struct trusted_key_payload *payload = NULL;
140 size_t datalen = prep->datalen;
146 if (datalen <= 0 || datalen > 32767 || !prep->data)
149 datablob = kmalloc(datalen + 1, GFP_KERNEL);
152 memcpy(datablob, prep->data, datalen);
153 datablob[datalen] = '\0';
155 payload = trusted_payload_alloc(key);
161 key_cmd = datablob_parse(datablob, payload);
167 dump_payload(payload);
171 ret = static_call(trusted_key_unseal)(payload, datablob);
172 dump_payload(payload);
174 pr_info("key_unseal failed (%d)\n", ret);
177 key_len = payload->key_len;
178 ret = static_call(trusted_key_get_random)(payload->key,
183 if (ret != key_len) {
184 pr_info("key_create failed (%d)\n", ret);
189 ret = static_call(trusted_key_seal)(payload, datablob);
191 pr_info("key_seal failed (%d)\n", ret);
197 kfree_sensitive(datablob);
199 rcu_assign_keypointer(key, payload);
201 kfree_sensitive(payload);
205 static void trusted_rcu_free(struct rcu_head *rcu)
207 struct trusted_key_payload *p;
209 p = container_of(rcu, struct trusted_key_payload, rcu);
214 * trusted_update - reseal an existing key with new PCR values
216 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
218 struct trusted_key_payload *p;
219 struct trusted_key_payload *new_p;
220 size_t datalen = prep->datalen;
224 if (key_is_negative(key))
226 p = key->payload.data[0];
229 if (datalen <= 0 || datalen > 32767 || !prep->data)
232 datablob = kmalloc(datalen + 1, GFP_KERNEL);
236 new_p = trusted_payload_alloc(key);
242 memcpy(datablob, prep->data, datalen);
243 datablob[datalen] = '\0';
244 ret = datablob_parse(datablob, new_p);
245 if (ret != Opt_update) {
247 kfree_sensitive(new_p);
251 /* copy old key values, and reseal with new pcrs */
252 new_p->migratable = p->migratable;
253 new_p->key_len = p->key_len;
254 memcpy(new_p->key, p->key, p->key_len);
258 ret = static_call(trusted_key_seal)(new_p, datablob);
260 pr_info("key_seal failed (%d)\n", ret);
261 kfree_sensitive(new_p);
265 rcu_assign_keypointer(key, new_p);
266 call_rcu(&p->rcu, trusted_rcu_free);
268 kfree_sensitive(datablob);
273 * trusted_read - copy the sealed blob data to userspace in hex.
274 * On success, return to userspace the trusted key datablob size.
276 static long trusted_read(const struct key *key, char *buffer,
279 const struct trusted_key_payload *p;
283 p = dereference_key_locked(key);
287 if (buffer && buflen >= 2 * p->blob_len) {
289 for (i = 0; i < p->blob_len; i++)
290 bufp = hex_byte_pack(bufp, p->blob[i]);
292 return 2 * p->blob_len;
296 * trusted_destroy - clear and free the key's payload
298 static void trusted_destroy(struct key *key)
300 kfree_sensitive(key->payload.data[0]);
303 struct key_type key_type_trusted = {
305 .instantiate = trusted_instantiate,
306 .update = trusted_update,
307 .destroy = trusted_destroy,
308 .describe = user_describe,
309 .read = trusted_read,
311 EXPORT_SYMBOL_GPL(key_type_trusted);
313 static int __init init_trusted(void)
317 for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
318 if (trusted_key_source &&
319 strncmp(trusted_key_source, trusted_key_sources[i].name,
320 strlen(trusted_key_sources[i].name)))
323 static_call_update(trusted_key_init,
324 trusted_key_sources[i].ops->init);
325 static_call_update(trusted_key_seal,
326 trusted_key_sources[i].ops->seal);
327 static_call_update(trusted_key_unseal,
328 trusted_key_sources[i].ops->unseal);
329 static_call_update(trusted_key_get_random,
330 trusted_key_sources[i].ops->get_random);
331 static_call_update(trusted_key_exit,
332 trusted_key_sources[i].ops->exit);
333 migratable = trusted_key_sources[i].ops->migratable;
335 ret = static_call(trusted_key_init)();
341 * encrypted_keys.ko depends on successful load of this module even if
342 * trusted key implementation is not found.
350 static void __exit cleanup_trusted(void)
352 static_call(trusted_key_exit)();
355 late_initcall(init_trusted);
356 module_exit(cleanup_trusted);
358 MODULE_LICENSE("GPL");