1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2010 IBM Corporation 4 * Copyright (c) 2019-2021, Linaro Limited 5 * 6 * See Documentation/security/keys/trusted-encrypted.rst 7 */ 8 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 <keys/trusted_pkwm.h> 16 #include <linux/capability.h> 17 #include <linux/err.h> 18 #include <linux/hex.h> 19 #include <linux/init.h> 20 #include <linux/key-type.h> 21 #include <linux/module.h> 22 #include <linux/parser.h> 23 #include <linux/random.h> 24 #include <linux/rcupdate.h> 25 #include <linux/slab.h> 26 #include <linux/static_call.h> 27 #include <linux/string.h> 28 #include <linux/uaccess.h> 29 30 static char *trusted_rng = "default"; 31 module_param_named(rng, trusted_rng, charp, 0); 32 MODULE_PARM_DESC(rng, "Select trusted key RNG"); 33 34 #ifdef CONFIG_TRUSTED_KEYS_DEBUG 35 bool trusted_debug; 36 module_param_named(debug, trusted_debug, bool, 0); 37 MODULE_PARM_DESC(debug, "Enable trusted keys debug traces (default: 0)"); 38 #endif 39 40 static char *trusted_key_source; 41 module_param_named(source, trusted_key_source, charp, 0); 42 MODULE_PARM_DESC(source, "Select trusted keys source (tpm, tee, caam, dcp or pkwm)"); 43 44 static const struct trusted_key_source trusted_key_sources[] = { 45 #if defined(CONFIG_TRUSTED_KEYS_TPM) 46 { "tpm", &trusted_key_tpm_ops }, 47 #endif 48 #if defined(CONFIG_TRUSTED_KEYS_TEE) 49 { "tee", &trusted_key_tee_ops }, 50 #endif 51 #if defined(CONFIG_TRUSTED_KEYS_CAAM) 52 { "caam", &trusted_key_caam_ops }, 53 #endif 54 #if defined(CONFIG_TRUSTED_KEYS_DCP) 55 { "dcp", &dcp_trusted_key_ops }, 56 #endif 57 #if defined(CONFIG_TRUSTED_KEYS_PKWM) 58 { "pkwm", &pkwm_trusted_key_ops }, 59 #endif 60 }; 61 62 DEFINE_STATIC_CALL_NULL(trusted_key_seal, *trusted_key_sources[0].ops->seal); 63 DEFINE_STATIC_CALL_NULL(trusted_key_unseal, 64 *trusted_key_sources[0].ops->unseal); 65 DEFINE_STATIC_CALL_NULL(trusted_key_get_random, 66 *trusted_key_sources[0].ops->get_random); 67 static void (*trusted_key_exit)(void); 68 static unsigned char migratable __ro_after_init; 69 70 enum { 71 Opt_err, 72 Opt_new, Opt_load, Opt_update, 73 }; 74 75 static const match_table_t key_tokens = { 76 {Opt_new, "new"}, 77 {Opt_load, "load"}, 78 {Opt_update, "update"}, 79 {Opt_err, NULL} 80 }; 81 82 /* 83 * datablob_parse - parse the keyctl data and fill in the 84 * payload structure 85 * 86 * On success returns 0, otherwise -EINVAL. 87 */ 88 static int datablob_parse(char **datablob, struct trusted_key_payload *p) 89 { 90 substring_t args[MAX_OPT_ARGS]; 91 long keylen; 92 int ret = -EINVAL; 93 int key_cmd; 94 char *c; 95 96 /* main command */ 97 c = strsep(datablob, " \t"); 98 if (!c) 99 return -EINVAL; 100 key_cmd = match_token(c, key_tokens, args); 101 switch (key_cmd) { 102 case Opt_new: 103 /* first argument is key size */ 104 c = strsep(datablob, " \t"); 105 if (!c) 106 return -EINVAL; 107 ret = kstrtol(c, 10, &keylen); 108 if (ret < 0 || keylen < MIN_KEY_SIZE || keylen > MAX_KEY_SIZE) 109 return -EINVAL; 110 p->key_len = keylen; 111 ret = Opt_new; 112 break; 113 case Opt_load: 114 /* first argument is sealed blob */ 115 c = strsep(datablob, " \t"); 116 if (!c) 117 return -EINVAL; 118 p->blob_len = strlen(c) / 2; 119 if (p->blob_len > MAX_BLOB_SIZE) 120 return -EINVAL; 121 ret = hex2bin(p->blob, c, p->blob_len); 122 if (ret < 0) 123 return -EINVAL; 124 ret = Opt_load; 125 break; 126 case Opt_update: 127 ret = Opt_update; 128 break; 129 case Opt_err: 130 return -EINVAL; 131 } 132 return ret; 133 } 134 135 static struct trusted_key_payload *trusted_payload_alloc(struct key *key) 136 { 137 struct trusted_key_payload *p = NULL; 138 int ret; 139 140 ret = key_payload_reserve(key, sizeof(*p)); 141 if (ret < 0) 142 goto err; 143 p = kzalloc_obj(*p); 144 if (!p) 145 goto err; 146 147 p->migratable = migratable; 148 err: 149 return p; 150 } 151 152 /* 153 * trusted_instantiate - create a new trusted key 154 * 155 * Unseal an existing trusted blob or, for a new key, get a 156 * random key, then seal and create a trusted key-type key, 157 * adding it to the specified keyring. 158 * 159 * On success, return 0. Otherwise return errno. 160 */ 161 static int trusted_instantiate(struct key *key, 162 struct key_preparsed_payload *prep) 163 { 164 struct trusted_key_payload *payload = NULL; 165 size_t datalen = prep->datalen; 166 char *datablob, *orig_datablob; 167 int ret = 0; 168 int key_cmd; 169 size_t key_len; 170 171 if (datalen == 0 || datalen > 32767 || !prep->data) 172 return -EINVAL; 173 174 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL); 175 if (!datablob) 176 return -ENOMEM; 177 memcpy(datablob, prep->data, datalen); 178 datablob[datalen] = '\0'; 179 180 payload = trusted_payload_alloc(key); 181 if (!payload) { 182 ret = -ENOMEM; 183 goto out; 184 } 185 186 key_cmd = datablob_parse(&datablob, payload); 187 if (key_cmd < 0) { 188 ret = key_cmd; 189 goto out; 190 } 191 192 dump_payload(payload); 193 194 switch (key_cmd) { 195 case Opt_load: 196 ret = static_call(trusted_key_unseal)(payload, datablob); 197 dump_payload(payload); 198 if (ret < 0) 199 pr_info("key_unseal failed (%d)\n", ret); 200 break; 201 case Opt_new: 202 key_len = payload->key_len; 203 ret = static_call(trusted_key_get_random)(payload->key, 204 key_len); 205 if (ret < 0) 206 goto out; 207 208 if (ret != key_len) { 209 pr_info("key_create failed (%d)\n", ret); 210 ret = -EIO; 211 goto out; 212 } 213 214 ret = static_call(trusted_key_seal)(payload, datablob); 215 if (ret < 0) 216 pr_info("key_seal failed (%d)\n", ret); 217 break; 218 default: 219 ret = -EINVAL; 220 } 221 out: 222 kfree_sensitive(orig_datablob); 223 if (!ret) 224 rcu_assign_keypointer(key, payload); 225 else 226 kfree_sensitive(payload); 227 return ret; 228 } 229 230 static void trusted_rcu_free(struct rcu_head *rcu) 231 { 232 struct trusted_key_payload *p; 233 234 p = container_of(rcu, struct trusted_key_payload, rcu); 235 kfree_sensitive(p); 236 } 237 238 /* 239 * trusted_update - reseal an existing key with new PCR values 240 */ 241 static int trusted_update(struct key *key, struct key_preparsed_payload *prep) 242 { 243 struct trusted_key_payload *p; 244 struct trusted_key_payload *new_p; 245 size_t datalen = prep->datalen; 246 char *datablob, *orig_datablob; 247 int ret = 0; 248 249 if (key_is_negative(key)) 250 return -ENOKEY; 251 p = key->payload.data[0]; 252 if (!p->migratable) 253 return -EPERM; 254 if (datalen == 0 || datalen > 32767 || !prep->data) 255 return -EINVAL; 256 257 orig_datablob = datablob = kmalloc(datalen + 1, GFP_KERNEL); 258 if (!datablob) 259 return -ENOMEM; 260 261 new_p = trusted_payload_alloc(key); 262 if (!new_p) { 263 ret = -ENOMEM; 264 goto out; 265 } 266 267 memcpy(datablob, prep->data, datalen); 268 datablob[datalen] = '\0'; 269 ret = datablob_parse(&datablob, new_p); 270 if (ret != Opt_update) { 271 ret = -EINVAL; 272 kfree_sensitive(new_p); 273 goto out; 274 } 275 276 /* copy old key values, and reseal with new pcrs */ 277 new_p->migratable = p->migratable; 278 new_p->key_len = p->key_len; 279 memcpy(new_p->key, p->key, p->key_len); 280 dump_payload(p); 281 dump_payload(new_p); 282 283 ret = static_call(trusted_key_seal)(new_p, datablob); 284 if (ret < 0) { 285 pr_info("key_seal failed (%d)\n", ret); 286 kfree_sensitive(new_p); 287 goto out; 288 } 289 290 rcu_assign_keypointer(key, new_p); 291 call_rcu(&p->rcu, trusted_rcu_free); 292 out: 293 kfree_sensitive(orig_datablob); 294 return ret; 295 } 296 297 /* 298 * trusted_read - copy the sealed blob data to userspace in hex. 299 * On success, return to userspace the trusted key datablob size. 300 */ 301 static long trusted_read(const struct key *key, char *buffer, 302 size_t buflen) 303 { 304 const struct trusted_key_payload *p; 305 char *bufp; 306 int i; 307 308 p = dereference_key_locked(key); 309 if (!p) 310 return -EINVAL; 311 312 if (buffer && buflen >= 2 * p->blob_len) { 313 bufp = buffer; 314 for (i = 0; i < p->blob_len; i++) 315 bufp = hex_byte_pack(bufp, p->blob[i]); 316 } 317 return 2 * p->blob_len; 318 } 319 320 /* 321 * trusted_destroy - clear and free the key's payload 322 */ 323 static void trusted_destroy(struct key *key) 324 { 325 kfree_sensitive(key->payload.data[0]); 326 } 327 328 struct key_type key_type_trusted = { 329 .name = "trusted", 330 .instantiate = trusted_instantiate, 331 .update = trusted_update, 332 .destroy = trusted_destroy, 333 .describe = user_describe, 334 .read = trusted_read, 335 }; 336 EXPORT_SYMBOL_GPL(key_type_trusted); 337 338 static int kernel_get_random(unsigned char *key, size_t key_len) 339 { 340 return get_random_bytes_wait(key, key_len) ?: key_len; 341 } 342 343 static int __init init_trusted(void) 344 { 345 int (*get_random)(unsigned char *key, size_t key_len); 346 int i, ret = 0; 347 348 for (i = 0; i < ARRAY_SIZE(trusted_key_sources); i++) { 349 if (trusted_key_source && 350 strncmp(trusted_key_source, trusted_key_sources[i].name, 351 strlen(trusted_key_sources[i].name))) 352 continue; 353 354 /* 355 * We always support trusted.rng="kernel" and "default" as 356 * well as trusted.rng=$trusted.source if the trust source 357 * defines its own get_random callback. 358 */ 359 get_random = trusted_key_sources[i].ops->get_random; 360 if (trusted_rng && strcmp(trusted_rng, "default")) { 361 if (!strcmp(trusted_rng, "kernel")) { 362 get_random = kernel_get_random; 363 } else if (strcmp(trusted_rng, trusted_key_sources[i].name) || 364 !get_random) { 365 pr_warn("Unsupported RNG. Supported: kernel"); 366 if (get_random) 367 pr_cont(", %s", trusted_key_sources[i].name); 368 pr_cont(", default\n"); 369 return -EINVAL; 370 } 371 } 372 373 if (!get_random) 374 get_random = kernel_get_random; 375 376 ret = trusted_key_sources[i].ops->init(); 377 if (!ret) { 378 static_call_update(trusted_key_seal, trusted_key_sources[i].ops->seal); 379 static_call_update(trusted_key_unseal, trusted_key_sources[i].ops->unseal); 380 static_call_update(trusted_key_get_random, get_random); 381 382 trusted_key_exit = trusted_key_sources[i].ops->exit; 383 migratable = trusted_key_sources[i].ops->migratable; 384 } 385 386 if (!ret || ret != -ENODEV) 387 break; 388 } 389 390 /* 391 * encrypted_keys.ko depends on successful load of this module even if 392 * trusted key implementation is not found. 393 */ 394 if (ret == -ENODEV) 395 return 0; 396 397 return ret; 398 } 399 400 static void __exit cleanup_trusted(void) 401 { 402 if (trusted_key_exit) 403 (*trusted_key_exit)(); 404 } 405 406 late_initcall(init_trusted); 407 module_exit(cleanup_trusted); 408 409 MODULE_DESCRIPTION("Trusted Key type"); 410 MODULE_LICENSE("GPL"); 411