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_tpm.h>
12 #include <linux/capability.h>
13 #include <linux/err.h>
14 #include <linux/init.h>
15 #include <linux/key-type.h>
16 #include <linux/module.h>
17 #include <linux/parser.h>
18 #include <linux/rcupdate.h>
19 #include <linux/slab.h>
20 #include <linux/static_call.h>
21 #include <linux/string.h>
22 #include <linux/uaccess.h>
24 static char *trusted_key_source;
25 module_param_named(source, trusted_key_source, charp, 0);
26 MODULE_PARM_DESC(source, "Select trusted keys source (tpm or tee)");
28 static const struct trusted_key_source trusted_key_sources[] = {
29 #if defined(CONFIG_TCG_TPM)
30 { "tpm", &trusted_key_tpm_ops },
34 DEFINE_STATIC_CALL_NULL(trusted_key_init, *trusted_key_sources[0].ops->init);
35 DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal);
36 DEFINE_STATIC_CALL_NULL(trusted_key_unseal,
37 *trusted_key_sources[0].ops->unseal);
38 DEFINE_STATIC_CALL_NULL(trusted_key_get_random,
39 *trusted_key_sources[0].ops->get_random);
40 DEFINE_STATIC_CALL_NULL(trusted_key_exit, *trusted_key_sources[0].ops->exit);
41 static unsigned char migratable;
45 Opt_new, Opt_load, Opt_update,
48 static const match_table_t key_tokens = {
51 {Opt_update, "update"},
56 * datablob_parse - parse the keyctl data and fill in the
59 * On success returns 0, otherwise -EINVAL.
61 static int datablob_parse(char *datablob, struct trusted_key_payload *p)
63 substring_t args[MAX_OPT_ARGS];
70 c = strsep(&datablob, " \t");
73 key_cmd = match_token(c, key_tokens, args);
76 /* first argument is key size */
77 c = strsep(&datablob, " \t");
80 ret = kstrtol(c, 10, &keylen);
81 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE)
87 /* first argument is sealed blob */
88 c = strsep(&datablob, " \t");
91 p->blob_len = strlen(c) / 2;
92 if (p->blob_len > MAX_BLOB_SIZE)
94 ret = hex2bin(p->blob, c, p->blob_len);
108 static struct trusted_key_payload *trusted_payload_alloc(struct key *key)
110 struct trusted_key_payload *p = NULL;
113 ret = key_payload_reserve(key, sizeof(*p));
116 p = kzalloc(sizeof(*p), GFP_KERNEL);
118 p->migratable = migratable;
124 * trusted_instantiate - create a new trusted key
126 * Unseal an existing trusted blob or, for a new key, get a
127 * random key, then seal and create a trusted key-type key,
128 * adding it to the specified keyring.
130 * On success, return 0. Otherwise return errno.
132 static int trusted_instantiate(struct key *key,
133 struct key_preparsed_payload *prep)
135 struct trusted_key_payload *payload = NULL;
136 size_t datalen = prep->datalen;
142 if (datalen <= 0 || datalen > 32767 || !prep->data)
145 datablob = kmalloc(datalen + 1, GFP_KERNEL);
148 memcpy(datablob, prep->data, datalen);
149 datablob[datalen] = '\0';
151 payload = trusted_payload_alloc(key);
157 key_cmd = datablob_parse(datablob, payload);
163 dump_payload(payload);
167 ret = static_call(trusted_key_unseal)(payload, datablob);
168 dump_payload(payload);
170 pr_info("key_unseal failed (%d)\n", ret);
173 key_len = payload->key_len;
174 ret = static_call(trusted_key_get_random)(payload->key,
179 if (ret != key_len) {
180 pr_info("key_create failed (%d)\n", ret);
185 ret = static_call(trusted_key_seal)(payload, datablob);
187 pr_info("key_seal failed (%d)\n", ret);
193 kfree_sensitive(datablob);
195 rcu_assign_keypointer(key, payload);
197 kfree_sensitive(payload);
201 static void trusted_rcu_free(struct rcu_head *rcu)
203 struct trusted_key_payload *p;
205 p = container_of(rcu, struct trusted_key_payload, rcu);
210 * trusted_update - reseal an existing key with new PCR values
212 static int trusted_update(struct key *key, struct key_preparsed_payload *prep)
214 struct trusted_key_payload *p;
215 struct trusted_key_payload *new_p;
216 size_t datalen = prep->datalen;
220 if (key_is_negative(key))
222 p = key->payload.data[0];
225 if (datalen <= 0 || datalen > 32767 || !prep->data)
228 datablob = kmalloc(datalen + 1, GFP_KERNEL);
232 new_p = trusted_payload_alloc(key);
238 memcpy(datablob, prep->data, datalen);
239 datablob[datalen] = '\0';
240 ret = datablob_parse(datablob, new_p);
241 if (ret != Opt_update) {
243 kfree_sensitive(new_p);
247 /* copy old key values, and reseal with new pcrs */
248 new_p->migratable = p->migratable;
249 new_p->key_len = p->key_len;
250 memcpy(new_p->key, p->key, p->key_len);
254 ret = static_call(trusted_key_seal)(new_p, datablob);
256 pr_info("key_seal failed (%d)\n", ret);
257 kfree_sensitive(new_p);
261 rcu_assign_keypointer(key, new_p);
262 call_rcu(&p->rcu, trusted_rcu_free);
264 kfree_sensitive(datablob);
269 * trusted_read - copy the sealed blob data to userspace in hex.
270 * On success, return to userspace the trusted key datablob size.
272 static long trusted_read(const struct key *key, char *buffer,
275 const struct trusted_key_payload *p;
279 p = dereference_key_locked(key);
283 if (buffer && buflen >= 2 * p->blob_len) {
285 for (i = 0; i < p->blob_len; i++)
286 bufp = hex_byte_pack(bufp, p->blob[i]);
288 return 2 * p->blob_len;
292 * trusted_destroy - clear and free the key's payload
294 static void trusted_destroy(struct key *key)
296 kfree_sensitive(key->payload.data[0]);
299 struct key_type key_type_trusted = {
301 .instantiate = trusted_instantiate,
302 .update = trusted_update,
303 .destroy = trusted_destroy,
304 .describe = user_describe,
305 .read = trusted_read,
307 EXPORT_SYMBOL_GPL(key_type_trusted);
309 static int __init init_trusted(void)
313 for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) {
314 if (trusted_key_source &&
315 strncmp(trusted_key_source, trusted_key_sources[i].name,
316 strlen(trusted_key_sources[i].name)))
319 static_call_update(trusted_key_init,
320 trusted_key_sources[i].ops->init);
321 static_call_update(trusted_key_seal,
322 trusted_key_sources[i].ops->seal);
323 static_call_update(trusted_key_unseal,
324 trusted_key_sources[i].ops->unseal);
325 static_call_update(trusted_key_get_random,
326 trusted_key_sources[i].ops->get_random);
327 static_call_update(trusted_key_exit,
328 trusted_key_sources[i].ops->exit);
329 migratable = trusted_key_sources[i].ops->migratable;
331 ret = static_call(trusted_key_init)();
337 * encrypted_keys.ko depends on successful load of this module even if
338 * trusted key implementation is not found.
346 static void __exit cleanup_trusted(void)
348 static_call(trusted_key_exit)();
351 late_initcall(init_trusted);
352 module_exit(cleanup_trusted);
354 MODULE_LICENSE("GPL");