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);
124 p->migratable = migratable;
130 * trusted_instantiate - create a new trusted key
132 * Unseal an existing trusted blob or, for a new key, get a
133 * random key, then seal and create a trusted key-type key,
134 * adding it to the specified keyring.
136 * On success, return 0. Otherwise return errno.
138 static int trusted_instantiate(struct key *key,
139 struct key_preparsed_payload *prep)
141 struct trusted_key_payload *payload = NULL;
142 size_t datalen = prep->datalen;
143 char *datablob, *orig_datablob;
148 if (datalen <= 0 || datalen > 32767 || !prep->data)
151 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
154 memcpy(datablob, prep->data, datalen);
155 datablob[datalen] = '\0';
157 payload = trusted_payload_alloc(key);
163 key_cmd = datablob_parse(&datablob, payload);
169 dump_payload(payload);
173 ret = static_call(trusted_key_unseal)(payload, datablob);
174 dump_payload(payload);
176 pr_info("key_unseal failed (%d)\n", ret);
179 key_len = payload->key_len;
180 ret = static_call(trusted_key_get_random)(payload->key,
185 if (ret != key_len) {
186 pr_info("key_create failed (%d)\n", ret);
191 ret = static_call(trusted_key_seal)(payload, datablob);
193 pr_info("key_seal failed (%d)\n", ret);
199 kfree_sensitive(orig_datablob);
201 rcu_assign_keypointer(key, payload);
203 kfree_sensitive(payload);
207 static void trusted_rcu_free(struct rcu_head *rcu)
209 struct trusted_key_payload *p;
211 p = container_of(rcu, struct trusted_key_payload, rcu);
216 * trusted_update - reseal an existing key with new PCR values
218 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
220 struct trusted_key_payload *p;
221 struct trusted_key_payload *new_p;
222 size_t datalen = prep->datalen;
223 char *datablob, *orig_datablob;
226 if (key_is_negative(key))
228 p = key->payload.data[0];
231 if (datalen <= 0 || datalen > 32767 || !prep->data)
234 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL);
238 new_p = trusted_payload_alloc(key);
244 memcpy(datablob, prep->data, datalen);
245 datablob[datalen] = '\0';
246 ret = datablob_parse(&datablob, new_p);
247 if (ret != Opt_update) {
249 kfree_sensitive(new_p);
253 /* copy old key values, and reseal with new pcrs */
254 new_p->migratable = p->migratable;
255 new_p->key_len = p->key_len;
256 memcpy(new_p->key, p->key, p->key_len);
260 ret = static_call(trusted_key_seal)(new_p, datablob);
262 pr_info("key_seal failed (%d)\n", ret);
263 kfree_sensitive(new_p);
267 rcu_assign_keypointer(key, new_p);
268 call_rcu(&p->rcu, trusted_rcu_free);
270 kfree_sensitive(orig_datablob);
275 * trusted_read - copy the sealed blob data to userspace in hex.
276 * On success, return to userspace the trusted key datablob size.
278 static long trusted_read(const struct key *key, char *buffer,
281 const struct trusted_key_payload *p;
285 p = dereference_key_locked(key);
289 if (buffer && buflen >= 2 * p->blob_len) {
291 for (i = 0; i < p->blob_len; i++)
292 bufp = hex_byte_pack(bufp, p->blob[i]);
294 return 2 * p->blob_len;
298 * trusted_destroy - clear and free the key's payload
300 static void trusted_destroy(struct key *key)
302 kfree_sensitive(key->payload.data[0]);
305 struct key_type key_type_trusted = {
307 .instantiate = trusted_instantiate,
308 .update = trusted_update,
309 .destroy = trusted_destroy,
310 .describe = user_describe,
311 .read = trusted_read,
313 EXPORT_SYMBOL_GPL(key_type_trusted);
315 static int __init init_trusted(void)
319 for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
320 if (trusted_key_source &&
321 strncmp(trusted_key_source, trusted_key_sources[i].name,
322 strlen(trusted_key_sources[i].name)))
325 static_call_update(trusted_key_init,
326 trusted_key_sources[i].ops->init);
327 static_call_update(trusted_key_seal,
328 trusted_key_sources[i].ops->seal);
329 static_call_update(trusted_key_unseal,
330 trusted_key_sources[i].ops->unseal);
331 static_call_update(trusted_key_get_random,
332 trusted_key_sources[i].ops->get_random);
333 static_call_update(trusted_key_exit,
334 trusted_key_sources[i].ops->exit);
335 migratable = trusted_key_sources[i].ops->migratable;
337 ret = static_call(trusted_key_init)();
343 * encrypted_keys.ko depends on successful load of this module even if
344 * trusted key implementation is not found.
352 static void __exit cleanup_trusted(void)
354 static_call(trusted_key_exit)();
357 late_initcall(init_trusted);
358 module_exit(cleanup_trusted);
360 MODULE_LICENSE("GPL");