trusted_core.c 9.3 KB

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