trusted_core.c 9.2 KB

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