tpm2-space.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (C) 2016 Intel Corporation
  3. *
  4. * Authors:
  5. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * This file contains TPM2 protocol implementations of the commands
  10. * used by the kernel internally.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; version 2
  15. * of the License.
  16. */
  17. #include <linux/gfp.h>
  18. #include <asm/unaligned.h>
  19. #include "tpm.h"
  20. enum tpm2_handle_types {
  21. TPM2_HT_HMAC_SESSION = 0x02000000,
  22. TPM2_HT_POLICY_SESSION = 0x03000000,
  23. TPM2_HT_TRANSIENT = 0x80000000,
  24. };
  25. struct tpm2_context {
  26. __be64 sequence;
  27. __be32 saved_handle;
  28. __be32 hierarchy;
  29. __be16 blob_size;
  30. } __packed;
  31. static void tpm2_flush_sessions(struct tpm_chip *chip, struct tpm_space *space)
  32. {
  33. int i;
  34. for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
  35. if (space->session_tbl[i])
  36. tpm2_flush_context_cmd(chip, space->session_tbl[i],
  37. TPM_TRANSMIT_UNLOCKED);
  38. }
  39. }
  40. int tpm2_init_space(struct tpm_space *space)
  41. {
  42. space->context_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  43. if (!space->context_buf)
  44. return -ENOMEM;
  45. space->session_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  46. if (space->session_buf == NULL) {
  47. kfree(space->context_buf);
  48. return -ENOMEM;
  49. }
  50. return 0;
  51. }
  52. void tpm2_del_space(struct tpm_chip *chip, struct tpm_space *space)
  53. {
  54. mutex_lock(&chip->tpm_mutex);
  55. tpm2_flush_sessions(chip, space);
  56. mutex_unlock(&chip->tpm_mutex);
  57. kfree(space->context_buf);
  58. kfree(space->session_buf);
  59. }
  60. static int tpm2_load_context(struct tpm_chip *chip, u8 *buf,
  61. unsigned int *offset, u32 *handle)
  62. {
  63. struct tpm_buf tbuf;
  64. struct tpm2_context *ctx;
  65. unsigned int body_size;
  66. int rc;
  67. rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_LOAD);
  68. if (rc)
  69. return rc;
  70. ctx = (struct tpm2_context *)&buf[*offset];
  71. body_size = sizeof(*ctx) + be16_to_cpu(ctx->blob_size);
  72. tpm_buf_append(&tbuf, &buf[*offset], body_size);
  73. rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 4,
  74. TPM_TRANSMIT_UNLOCKED, NULL);
  75. if (rc < 0) {
  76. dev_warn(&chip->dev, "%s: failed with a system error %d\n",
  77. __func__, rc);
  78. tpm_buf_destroy(&tbuf);
  79. return -EFAULT;
  80. } else if (tpm2_rc_value(rc) == TPM2_RC_HANDLE ||
  81. rc == TPM2_RC_REFERENCE_H0) {
  82. /*
  83. * TPM_RC_HANDLE means that the session context can't
  84. * be loaded because of an internal counter mismatch
  85. * that makes the TPM think there might have been a
  86. * replay. This might happen if the context was saved
  87. * and loaded outside the space.
  88. *
  89. * TPM_RC_REFERENCE_H0 means the session has been
  90. * flushed outside the space
  91. */
  92. rc = -ENOENT;
  93. tpm_buf_destroy(&tbuf);
  94. } else if (rc > 0) {
  95. dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
  96. __func__, rc);
  97. tpm_buf_destroy(&tbuf);
  98. return -EFAULT;
  99. }
  100. *handle = be32_to_cpup((__be32 *)&tbuf.data[TPM_HEADER_SIZE]);
  101. *offset += body_size;
  102. tpm_buf_destroy(&tbuf);
  103. return 0;
  104. }
  105. static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
  106. unsigned int buf_size, unsigned int *offset)
  107. {
  108. struct tpm_buf tbuf;
  109. unsigned int body_size;
  110. int rc;
  111. rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_SAVE);
  112. if (rc)
  113. return rc;
  114. tpm_buf_append_u32(&tbuf, handle);
  115. rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0,
  116. TPM_TRANSMIT_UNLOCKED, NULL);
  117. if (rc < 0) {
  118. dev_warn(&chip->dev, "%s: failed with a system error %d\n",
  119. __func__, rc);
  120. tpm_buf_destroy(&tbuf);
  121. return -EFAULT;
  122. } else if (tpm2_rc_value(rc) == TPM2_RC_REFERENCE_H0) {
  123. tpm_buf_destroy(&tbuf);
  124. return -ENOENT;
  125. } else if (rc) {
  126. dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
  127. __func__, rc);
  128. tpm_buf_destroy(&tbuf);
  129. return -EFAULT;
  130. }
  131. body_size = tpm_buf_length(&tbuf) - TPM_HEADER_SIZE;
  132. if ((*offset + body_size) > buf_size) {
  133. dev_warn(&chip->dev, "%s: out of backing storage\n", __func__);
  134. tpm_buf_destroy(&tbuf);
  135. return -ENOMEM;
  136. }
  137. memcpy(&buf[*offset], &tbuf.data[TPM_HEADER_SIZE], body_size);
  138. *offset += body_size;
  139. tpm_buf_destroy(&tbuf);
  140. return 0;
  141. }
  142. static void tpm2_flush_space(struct tpm_chip *chip)
  143. {
  144. struct tpm_space *space = &chip->work_space;
  145. int i;
  146. for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
  147. if (space->context_tbl[i] && ~space->context_tbl[i])
  148. tpm2_flush_context_cmd(chip, space->context_tbl[i],
  149. TPM_TRANSMIT_UNLOCKED);
  150. tpm2_flush_sessions(chip, space);
  151. }
  152. static int tpm2_load_space(struct tpm_chip *chip)
  153. {
  154. struct tpm_space *space = &chip->work_space;
  155. unsigned int offset;
  156. int i;
  157. int rc;
  158. for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  159. if (!space->context_tbl[i])
  160. continue;
  161. /* sanity check, should never happen */
  162. if (~space->context_tbl[i]) {
  163. dev_err(&chip->dev, "context table is inconsistent");
  164. return -EFAULT;
  165. }
  166. rc = tpm2_load_context(chip, space->context_buf, &offset,
  167. &space->context_tbl[i]);
  168. if (rc)
  169. return rc;
  170. }
  171. for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
  172. u32 handle;
  173. if (!space->session_tbl[i])
  174. continue;
  175. rc = tpm2_load_context(chip, space->session_buf,
  176. &offset, &handle);
  177. if (rc == -ENOENT) {
  178. /* load failed, just forget session */
  179. space->session_tbl[i] = 0;
  180. } else if (rc) {
  181. tpm2_flush_space(chip);
  182. return rc;
  183. }
  184. if (handle != space->session_tbl[i]) {
  185. dev_warn(&chip->dev, "session restored to wrong handle\n");
  186. tpm2_flush_space(chip);
  187. return -EFAULT;
  188. }
  189. }
  190. return 0;
  191. }
  192. static bool tpm2_map_to_phandle(struct tpm_space *space, void *handle)
  193. {
  194. u32 vhandle = be32_to_cpup((__be32 *)handle);
  195. u32 phandle;
  196. int i;
  197. i = 0xFFFFFF - (vhandle & 0xFFFFFF);
  198. if (i >= ARRAY_SIZE(space->context_tbl) || !space->context_tbl[i])
  199. return false;
  200. phandle = space->context_tbl[i];
  201. *((__be32 *)handle) = cpu_to_be32(phandle);
  202. return true;
  203. }
  204. static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd)
  205. {
  206. struct tpm_space *space = &chip->work_space;
  207. unsigned int nr_handles;
  208. u32 attrs;
  209. u32 *handle;
  210. int i;
  211. i = tpm2_find_cc(chip, cc);
  212. if (i < 0)
  213. return -EINVAL;
  214. attrs = chip->cc_attrs_tbl[i];
  215. nr_handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
  216. handle = (u32 *)&cmd[TPM_HEADER_SIZE];
  217. for (i = 0; i < nr_handles; i++, handle++) {
  218. if ((be32_to_cpu(*handle) & 0xFF000000) == TPM2_HT_TRANSIENT) {
  219. if (!tpm2_map_to_phandle(space, handle))
  220. return -EINVAL;
  221. }
  222. }
  223. return 0;
  224. }
  225. int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
  226. u8 *cmd)
  227. {
  228. int rc;
  229. if (!space)
  230. return 0;
  231. memcpy(&chip->work_space.context_tbl, &space->context_tbl,
  232. sizeof(space->context_tbl));
  233. memcpy(&chip->work_space.session_tbl, &space->session_tbl,
  234. sizeof(space->session_tbl));
  235. memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
  236. memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
  237. rc = tpm2_load_space(chip);
  238. if (rc) {
  239. tpm2_flush_space(chip);
  240. return rc;
  241. }
  242. rc = tpm2_map_command(chip, cc, cmd);
  243. if (rc) {
  244. tpm2_flush_space(chip);
  245. return rc;
  246. }
  247. return 0;
  248. }
  249. static bool tpm2_add_session(struct tpm_chip *chip, u32 handle)
  250. {
  251. struct tpm_space *space = &chip->work_space;
  252. int i;
  253. for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++)
  254. if (space->session_tbl[i] == 0)
  255. break;
  256. if (i == ARRAY_SIZE(space->session_tbl))
  257. return false;
  258. space->session_tbl[i] = handle;
  259. return true;
  260. }
  261. static u32 tpm2_map_to_vhandle(struct tpm_space *space, u32 phandle, bool alloc)
  262. {
  263. int i;
  264. for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  265. if (alloc) {
  266. if (!space->context_tbl[i]) {
  267. space->context_tbl[i] = phandle;
  268. break;
  269. }
  270. } else if (space->context_tbl[i] == phandle)
  271. break;
  272. }
  273. if (i == ARRAY_SIZE(space->context_tbl))
  274. return 0;
  275. return TPM2_HT_TRANSIENT | (0xFFFFFF - i);
  276. }
  277. static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
  278. size_t len)
  279. {
  280. struct tpm_space *space = &chip->work_space;
  281. struct tpm_output_header *header = (void *)rsp;
  282. u32 phandle;
  283. u32 phandle_type;
  284. u32 vhandle;
  285. u32 attrs;
  286. int i;
  287. if (be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS)
  288. return 0;
  289. i = tpm2_find_cc(chip, cc);
  290. /* sanity check, should never happen */
  291. if (i < 0)
  292. return -EFAULT;
  293. attrs = chip->cc_attrs_tbl[i];
  294. if (!((attrs >> TPM2_CC_ATTR_RHANDLE) & 1))
  295. return 0;
  296. phandle = be32_to_cpup((__be32 *)&rsp[TPM_HEADER_SIZE]);
  297. phandle_type = phandle & 0xFF000000;
  298. switch (phandle_type) {
  299. case TPM2_HT_TRANSIENT:
  300. vhandle = tpm2_map_to_vhandle(space, phandle, true);
  301. if (!vhandle)
  302. goto out_no_slots;
  303. *(__be32 *)&rsp[TPM_HEADER_SIZE] = cpu_to_be32(vhandle);
  304. break;
  305. case TPM2_HT_HMAC_SESSION:
  306. case TPM2_HT_POLICY_SESSION:
  307. if (!tpm2_add_session(chip, phandle))
  308. goto out_no_slots;
  309. break;
  310. default:
  311. dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
  312. __func__, phandle);
  313. break;
  314. };
  315. return 0;
  316. out_no_slots:
  317. tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_UNLOCKED);
  318. dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__,
  319. phandle);
  320. return -ENOMEM;
  321. }
  322. struct tpm2_cap_handles {
  323. u8 more_data;
  324. __be32 capability;
  325. __be32 count;
  326. __be32 handles[];
  327. } __packed;
  328. static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
  329. size_t len)
  330. {
  331. struct tpm_space *space = &chip->work_space;
  332. struct tpm_output_header *header = (void *)rsp;
  333. struct tpm2_cap_handles *data;
  334. u32 phandle;
  335. u32 phandle_type;
  336. u32 vhandle;
  337. int i;
  338. int j;
  339. if (cc != TPM2_CC_GET_CAPABILITY ||
  340. be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS) {
  341. return 0;
  342. }
  343. if (len < TPM_HEADER_SIZE + 9)
  344. return -EFAULT;
  345. data = (void *)&rsp[TPM_HEADER_SIZE];
  346. if (be32_to_cpu(data->capability) != TPM2_CAP_HANDLES)
  347. return 0;
  348. if (len != TPM_HEADER_SIZE + 9 + 4 * be32_to_cpu(data->count))
  349. return -EFAULT;
  350. for (i = 0, j = 0; i < be32_to_cpu(data->count); i++) {
  351. phandle = be32_to_cpup((__be32 *)&data->handles[i]);
  352. phandle_type = phandle & 0xFF000000;
  353. switch (phandle_type) {
  354. case TPM2_HT_TRANSIENT:
  355. vhandle = tpm2_map_to_vhandle(space, phandle, false);
  356. if (!vhandle)
  357. break;
  358. data->handles[j] = cpu_to_be32(vhandle);
  359. j++;
  360. break;
  361. default:
  362. data->handles[j] = cpu_to_be32(phandle);
  363. j++;
  364. break;
  365. }
  366. }
  367. header->length = cpu_to_be32(TPM_HEADER_SIZE + 9 + 4 * j);
  368. data->count = cpu_to_be32(j);
  369. return 0;
  370. }
  371. static int tpm2_save_space(struct tpm_chip *chip)
  372. {
  373. struct tpm_space *space = &chip->work_space;
  374. unsigned int offset;
  375. int i;
  376. int rc;
  377. for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  378. if (!(space->context_tbl[i] && ~space->context_tbl[i]))
  379. continue;
  380. rc = tpm2_save_context(chip, space->context_tbl[i],
  381. space->context_buf, PAGE_SIZE,
  382. &offset);
  383. if (rc == -ENOENT) {
  384. space->context_tbl[i] = 0;
  385. continue;
  386. } else if (rc)
  387. return rc;
  388. tpm2_flush_context_cmd(chip, space->context_tbl[i],
  389. TPM_TRANSMIT_UNLOCKED);
  390. space->context_tbl[i] = ~0;
  391. }
  392. for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
  393. if (!space->session_tbl[i])
  394. continue;
  395. rc = tpm2_save_context(chip, space->session_tbl[i],
  396. space->session_buf, PAGE_SIZE,
  397. &offset);
  398. if (rc == -ENOENT) {
  399. /* handle error saving session, just forget it */
  400. space->session_tbl[i] = 0;
  401. } else if (rc < 0) {
  402. tpm2_flush_space(chip);
  403. return rc;
  404. }
  405. }
  406. return 0;
  407. }
  408. int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
  409. u32 cc, u8 *buf, size_t *bufsiz)
  410. {
  411. struct tpm_output_header *header = (void *)buf;
  412. int rc;
  413. if (!space)
  414. return 0;
  415. rc = tpm2_map_response_header(chip, cc, buf, *bufsiz);
  416. if (rc) {
  417. tpm2_flush_space(chip);
  418. return rc;
  419. }
  420. rc = tpm2_map_response_body(chip, cc, buf, *bufsiz);
  421. if (rc) {
  422. tpm2_flush_space(chip);
  423. return rc;
  424. }
  425. rc = tpm2_save_space(chip);
  426. if (rc) {
  427. tpm2_flush_space(chip);
  428. return rc;
  429. }
  430. *bufsiz = be32_to_cpu(header->length);
  431. memcpy(&space->context_tbl, &chip->work_space.context_tbl,
  432. sizeof(space->context_tbl));
  433. memcpy(&space->session_tbl, &chip->work_space.session_tbl,
  434. sizeof(space->session_tbl));
  435. memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
  436. memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
  437. return 0;
  438. }