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_dcp.h>
14 #include <keys/trusted_tpm.h>
15 #include <linux/capability.h>
16 #include <linux/err.h>
17 #include <linux/init.h>
18 #include <linux/key-type.h>
19 #include <linux/module.h>
20 #include <linux/parser.h>
21 #include <linux/random.h>
22 #include <linux/rcupdate.h>
23 #include <linux/slab.h>
24 #include <linux/static_call.h>
25 #include <linux/string.h>
26 #include <linux/uaccess.h>
28 static char *trusted_rng
= "default";
29 module_param_named(rng
, trusted_rng
, charp
, 0);
30 MODULE_PARM_DESC(rng
, "Select trusted key RNG");
32 static char *trusted_key_source
;
33 module_param_named(source
, trusted_key_source
, charp
, 0);
34 MODULE_PARM_DESC(source
, "Select trusted keys source (tpm, tee, caam or dcp)");
36 static const struct trusted_key_source trusted_key_sources
[] = {
37 #if defined(CONFIG_TRUSTED_KEYS_TPM)
38 { "tpm", &trusted_key_tpm_ops
},
40 #if defined(CONFIG_TRUSTED_KEYS_TEE)
41 { "tee", &trusted_key_tee_ops
},
43 #if defined(CONFIG_TRUSTED_KEYS_CAAM)
44 { "caam", &trusted_key_caam_ops
},
46 #if defined(CONFIG_TRUSTED_KEYS_DCP)
47 { "dcp", &dcp_trusted_key_ops
},
51 DEFINE_STATIC_CALL_NULL(trusted_key_seal
, *trusted_key_sources
[0].ops
->seal
);
52 DEFINE_STATIC_CALL_NULL(trusted_key_unseal
,
53 *trusted_key_sources
[0].ops
->unseal
);
54 DEFINE_STATIC_CALL_NULL(trusted_key_get_random
,
55 *trusted_key_sources
[0].ops
->get_random
);
56 static void (*trusted_key_exit
)(void);
57 static unsigned char migratable
;
61 Opt_new
, Opt_load
, Opt_update
,
64 static const match_table_t key_tokens
= {
67 {Opt_update
, "update"},
72 * datablob_parse - parse the keyctl data and fill in the
75 * On success returns 0, otherwise -EINVAL.
77 static int datablob_parse(char **datablob
, struct trusted_key_payload
*p
)
79 substring_t args
[MAX_OPT_ARGS
];
86 c
= strsep(datablob
, " \t");
89 key_cmd
= match_token(c
, key_tokens
, args
);
92 /* first argument is key size */
93 c
= strsep(datablob
, " \t");
96 ret
= kstrtol(c
, 10, &keylen
);
97 if (ret
< 0 || keylen
< MIN_KEY_SIZE
|| keylen
> MAX_KEY_SIZE
)
103 /* first argument is sealed blob */
104 c
= strsep(datablob
, " \t");
107 p
->blob_len
= strlen(c
) / 2;
108 if (p
->blob_len
> MAX_BLOB_SIZE
)
110 ret
= hex2bin(p
->blob
, c
, p
->blob_len
);
124 static struct trusted_key_payload
*trusted_payload_alloc(struct key
*key
)
126 struct trusted_key_payload
*p
= NULL
;
129 ret
= key_payload_reserve(key
, sizeof(*p
));
132 p
= kzalloc(sizeof(*p
), GFP_KERNEL
);
136 p
->migratable
= migratable
;
142 * trusted_instantiate - create a new trusted key
144 * Unseal an existing trusted blob or, for a new key, get a
145 * random key, then seal and create a trusted key-type key,
146 * adding it to the specified keyring.
148 * On success, return 0. Otherwise return errno.
150 static int trusted_instantiate(struct key
*key
,
151 struct key_preparsed_payload
*prep
)
153 struct trusted_key_payload
*payload
= NULL
;
154 size_t datalen
= prep
->datalen
;
155 char *datablob
, *orig_datablob
;
160 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
163 orig_datablob
= datablob
= kmalloc(datalen
+ 1, GFP_KERNEL
);
166 memcpy(datablob
, prep
->data
, datalen
);
167 datablob
[datalen
] = '\0';
169 payload
= trusted_payload_alloc(key
);
175 key_cmd
= datablob_parse(&datablob
, payload
);
181 dump_payload(payload
);
185 ret
= static_call(trusted_key_unseal
)(payload
, datablob
);
186 dump_payload(payload
);
188 pr_info("key_unseal failed (%d)\n", ret
);
191 key_len
= payload
->key_len
;
192 ret
= static_call(trusted_key_get_random
)(payload
->key
,
197 if (ret
!= key_len
) {
198 pr_info("key_create failed (%d)\n", ret
);
203 ret
= static_call(trusted_key_seal
)(payload
, datablob
);
205 pr_info("key_seal failed (%d)\n", ret
);
211 kfree_sensitive(orig_datablob
);
213 rcu_assign_keypointer(key
, payload
);
215 kfree_sensitive(payload
);
219 static void trusted_rcu_free(struct rcu_head
*rcu
)
221 struct trusted_key_payload
*p
;
223 p
= container_of(rcu
, struct trusted_key_payload
, rcu
);
228 * trusted_update - reseal an existing key with new PCR values
230 static int trusted_update(struct key
*key
, struct key_preparsed_payload
*prep
)
232 struct trusted_key_payload
*p
;
233 struct trusted_key_payload
*new_p
;
234 size_t datalen
= prep
->datalen
;
235 char *datablob
, *orig_datablob
;
238 if (key_is_negative(key
))
240 p
= key
->payload
.data
[0];
243 if (datalen
<= 0 || datalen
> 32767 || !prep
->data
)
246 orig_datablob
= datablob
= kmalloc(datalen
+ 1, GFP_KERNEL
);
250 new_p
= trusted_payload_alloc(key
);
256 memcpy(datablob
, prep
->data
, datalen
);
257 datablob
[datalen
] = '\0';
258 ret
= datablob_parse(&datablob
, new_p
);
259 if (ret
!= Opt_update
) {
261 kfree_sensitive(new_p
);
265 /* copy old key values, and reseal with new pcrs */
266 new_p
->migratable
= p
->migratable
;
267 new_p
->key_len
= p
->key_len
;
268 memcpy(new_p
->key
, p
->key
, p
->key_len
);
272 ret
= static_call(trusted_key_seal
)(new_p
, datablob
);
274 pr_info("key_seal failed (%d)\n", ret
);
275 kfree_sensitive(new_p
);
279 rcu_assign_keypointer(key
, new_p
);
280 call_rcu(&p
->rcu
, trusted_rcu_free
);
282 kfree_sensitive(orig_datablob
);
287 * trusted_read - copy the sealed blob data to userspace in hex.
288 * On success, return to userspace the trusted key datablob size.
290 static long trusted_read(const struct key
*key
, char *buffer
,
293 const struct trusted_key_payload
*p
;
297 p
= dereference_key_locked(key
);
301 if (buffer
&& buflen
>= 2 * p
->blob_len
) {
303 for (i
= 0; i
< p
->blob_len
; i
++)
304 bufp
= hex_byte_pack(bufp
, p
->blob
[i
]);
306 return 2 * p
->blob_len
;
310 * trusted_destroy - clear and free the key's payload
312 static void trusted_destroy(struct key
*key
)
314 kfree_sensitive(key
->payload
.data
[0]);
317 struct key_type key_type_trusted
= {
319 .instantiate
= trusted_instantiate
,
320 .update
= trusted_update
,
321 .destroy
= trusted_destroy
,
322 .describe
= user_describe
,
323 .read
= trusted_read
,
325 EXPORT_SYMBOL_GPL(key_type_trusted
);
327 static int kernel_get_random(unsigned char *key
, size_t key_len
)
329 return get_random_bytes_wait(key
, key_len
) ?: key_len
;
332 static int __init
init_trusted(void)
334 int (*get_random
)(unsigned char *key
, size_t key_len
);
337 for (i
= 0; i
< ARRAY_SIZE(trusted_key_sources
); i
++) {
338 if (trusted_key_source
&&
339 strncmp(trusted_key_source
, trusted_key_sources
[i
].name
,
340 strlen(trusted_key_sources
[i
].name
)))
344 * We always support trusted.rng="kernel" and "default" as
345 * well as trusted.rng=$trusted.source if the trust source
346 * defines its own get_random callback.
348 get_random
= trusted_key_sources
[i
].ops
->get_random
;
349 if (trusted_rng
&& strcmp(trusted_rng
, "default")) {
350 if (!strcmp(trusted_rng
, "kernel")) {
351 get_random
= kernel_get_random
;
352 } else if (strcmp(trusted_rng
, trusted_key_sources
[i
].name
) ||
354 pr_warn("Unsupported RNG. Supported: kernel");
356 pr_cont(", %s", trusted_key_sources
[i
].name
);
357 pr_cont(", default\n");
363 get_random
= kernel_get_random
;
365 ret
= trusted_key_sources
[i
].ops
->init();
367 static_call_update(trusted_key_seal
, trusted_key_sources
[i
].ops
->seal
);
368 static_call_update(trusted_key_unseal
, trusted_key_sources
[i
].ops
->unseal
);
369 static_call_update(trusted_key_get_random
, get_random
);
371 trusted_key_exit
= trusted_key_sources
[i
].ops
->exit
;
372 migratable
= trusted_key_sources
[i
].ops
->migratable
;
375 if (!ret
|| ret
!= -ENODEV
)
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 if (trusted_key_exit
)
392 (*trusted_key_exit
)();
395 late_initcall(init_trusted
);
396 module_exit(cleanup_trusted
);
398 MODULE_DESCRIPTION("Trusted Key type");
399 MODULE_LICENSE("GPL");