tpm2-space.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530
  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_NESTED);
  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_NESTED, 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. *handle = 0;
  93. tpm_buf_destroy(&tbuf);
  94. return -ENOENT;
  95. } else if (rc > 0) {
  96. dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
  97. __func__, rc);
  98. tpm_buf_destroy(&tbuf);
  99. return -EFAULT;
  100. }
  101. *handle = be32_to_cpup((__be32 *)&tbuf.data[TPM_HEADER_SIZE]);
  102. *offset += body_size;
  103. tpm_buf_destroy(&tbuf);
  104. return 0;
  105. }
  106. static int tpm2_save_context(struct tpm_chip *chip, u32 handle, u8 *buf,
  107. unsigned int buf_size, unsigned int *offset)
  108. {
  109. struct tpm_buf tbuf;
  110. unsigned int body_size;
  111. int rc;
  112. rc = tpm_buf_init(&tbuf, TPM2_ST_NO_SESSIONS, TPM2_CC_CONTEXT_SAVE);
  113. if (rc)
  114. return rc;
  115. tpm_buf_append_u32(&tbuf, handle);
  116. rc = tpm_transmit_cmd(chip, NULL, tbuf.data, PAGE_SIZE, 0,
  117. TPM_TRANSMIT_NESTED, NULL);
  118. if (rc < 0) {
  119. dev_warn(&chip->dev, "%s: failed with a system error %d\n",
  120. __func__, rc);
  121. tpm_buf_destroy(&tbuf);
  122. return -EFAULT;
  123. } else if (tpm2_rc_value(rc) == TPM2_RC_REFERENCE_H0) {
  124. tpm_buf_destroy(&tbuf);
  125. return -ENOENT;
  126. } else if (rc) {
  127. dev_warn(&chip->dev, "%s: failed with a TPM error 0x%04X\n",
  128. __func__, rc);
  129. tpm_buf_destroy(&tbuf);
  130. return -EFAULT;
  131. }
  132. body_size = tpm_buf_length(&tbuf) - TPM_HEADER_SIZE;
  133. if ((*offset + body_size) > buf_size) {
  134. dev_warn(&chip->dev, "%s: out of backing storage\n", __func__);
  135. tpm_buf_destroy(&tbuf);
  136. return -ENOMEM;
  137. }
  138. memcpy(&buf[*offset], &tbuf.data[TPM_HEADER_SIZE], body_size);
  139. *offset += body_size;
  140. tpm_buf_destroy(&tbuf);
  141. return 0;
  142. }
  143. static void tpm2_flush_space(struct tpm_chip *chip)
  144. {
  145. struct tpm_space *space = &chip->work_space;
  146. int i;
  147. for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++)
  148. if (space->context_tbl[i] && ~space->context_tbl[i])
  149. tpm2_flush_context_cmd(chip, space->context_tbl[i],
  150. TPM_TRANSMIT_NESTED);
  151. tpm2_flush_sessions(chip, space);
  152. }
  153. static int tpm2_load_space(struct tpm_chip *chip)
  154. {
  155. struct tpm_space *space = &chip->work_space;
  156. unsigned int offset;
  157. int i;
  158. int rc;
  159. for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  160. if (!space->context_tbl[i])
  161. continue;
  162. /* sanity check, should never happen */
  163. if (~space->context_tbl[i]) {
  164. dev_err(&chip->dev, "context table is inconsistent");
  165. return -EFAULT;
  166. }
  167. rc = tpm2_load_context(chip, space->context_buf, &offset,
  168. &space->context_tbl[i]);
  169. if (rc)
  170. return rc;
  171. }
  172. for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
  173. u32 handle;
  174. if (!space->session_tbl[i])
  175. continue;
  176. rc = tpm2_load_context(chip, space->session_buf,
  177. &offset, &handle);
  178. if (rc == -ENOENT) {
  179. /* load failed, just forget session */
  180. space->session_tbl[i] = 0;
  181. } else if (rc) {
  182. tpm2_flush_space(chip);
  183. return rc;
  184. }
  185. if (handle != space->session_tbl[i]) {
  186. dev_warn(&chip->dev, "session restored to wrong handle\n");
  187. tpm2_flush_space(chip);
  188. return -EFAULT;
  189. }
  190. }
  191. return 0;
  192. }
  193. static bool tpm2_map_to_phandle(struct tpm_space *space, void *handle)
  194. {
  195. u32 vhandle = be32_to_cpup((__be32 *)handle);
  196. u32 phandle;
  197. int i;
  198. i = 0xFFFFFF - (vhandle & 0xFFFFFF);
  199. if (i >= ARRAY_SIZE(space->context_tbl) || !space->context_tbl[i])
  200. return false;
  201. phandle = space->context_tbl[i];
  202. *((__be32 *)handle) = cpu_to_be32(phandle);
  203. return true;
  204. }
  205. static int tpm2_map_command(struct tpm_chip *chip, u32 cc, u8 *cmd)
  206. {
  207. struct tpm_space *space = &chip->work_space;
  208. unsigned int nr_handles;
  209. u32 attrs;
  210. __be32 *handle;
  211. int i;
  212. i = tpm2_find_cc(chip, cc);
  213. if (i < 0)
  214. return -EINVAL;
  215. attrs = chip->cc_attrs_tbl[i];
  216. nr_handles = (attrs >> TPM2_CC_ATTR_CHANDLES) & GENMASK(2, 0);
  217. handle = (__be32 *)&cmd[TPM_HEADER_SIZE];
  218. for (i = 0; i < nr_handles; i++, handle++) {
  219. if ((be32_to_cpu(*handle) & 0xFF000000) == TPM2_HT_TRANSIENT) {
  220. if (!tpm2_map_to_phandle(space, handle))
  221. return -EINVAL;
  222. }
  223. }
  224. return 0;
  225. }
  226. int tpm2_prepare_space(struct tpm_chip *chip, struct tpm_space *space, u32 cc,
  227. u8 *cmd)
  228. {
  229. int rc;
  230. if (!space)
  231. return 0;
  232. memcpy(&chip->work_space.context_tbl, &space->context_tbl,
  233. sizeof(space->context_tbl));
  234. memcpy(&chip->work_space.session_tbl, &space->session_tbl,
  235. sizeof(space->session_tbl));
  236. memcpy(chip->work_space.context_buf, space->context_buf, PAGE_SIZE);
  237. memcpy(chip->work_space.session_buf, space->session_buf, PAGE_SIZE);
  238. rc = tpm2_load_space(chip);
  239. if (rc) {
  240. tpm2_flush_space(chip);
  241. return rc;
  242. }
  243. rc = tpm2_map_command(chip, cc, cmd);
  244. if (rc) {
  245. tpm2_flush_space(chip);
  246. return rc;
  247. }
  248. return 0;
  249. }
  250. static bool tpm2_add_session(struct tpm_chip *chip, u32 handle)
  251. {
  252. struct tpm_space *space = &chip->work_space;
  253. int i;
  254. for (i = 0; i < ARRAY_SIZE(space->session_tbl); i++)
  255. if (space->session_tbl[i] == 0)
  256. break;
  257. if (i == ARRAY_SIZE(space->session_tbl))
  258. return false;
  259. space->session_tbl[i] = handle;
  260. return true;
  261. }
  262. static u32 tpm2_map_to_vhandle(struct tpm_space *space, u32 phandle, bool alloc)
  263. {
  264. int i;
  265. for (i = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  266. if (alloc) {
  267. if (!space->context_tbl[i]) {
  268. space->context_tbl[i] = phandle;
  269. break;
  270. }
  271. } else if (space->context_tbl[i] == phandle)
  272. break;
  273. }
  274. if (i == ARRAY_SIZE(space->context_tbl))
  275. return 0;
  276. return TPM2_HT_TRANSIENT | (0xFFFFFF - i);
  277. }
  278. static int tpm2_map_response_header(struct tpm_chip *chip, u32 cc, u8 *rsp,
  279. size_t len)
  280. {
  281. struct tpm_space *space = &chip->work_space;
  282. struct tpm_output_header *header = (void *)rsp;
  283. u32 phandle;
  284. u32 phandle_type;
  285. u32 vhandle;
  286. u32 attrs;
  287. int i;
  288. if (be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS)
  289. return 0;
  290. i = tpm2_find_cc(chip, cc);
  291. /* sanity check, should never happen */
  292. if (i < 0)
  293. return -EFAULT;
  294. attrs = chip->cc_attrs_tbl[i];
  295. if (!((attrs >> TPM2_CC_ATTR_RHANDLE) & 1))
  296. return 0;
  297. phandle = be32_to_cpup((__be32 *)&rsp[TPM_HEADER_SIZE]);
  298. phandle_type = phandle & 0xFF000000;
  299. switch (phandle_type) {
  300. case TPM2_HT_TRANSIENT:
  301. vhandle = tpm2_map_to_vhandle(space, phandle, true);
  302. if (!vhandle)
  303. goto out_no_slots;
  304. *(__be32 *)&rsp[TPM_HEADER_SIZE] = cpu_to_be32(vhandle);
  305. break;
  306. case TPM2_HT_HMAC_SESSION:
  307. case TPM2_HT_POLICY_SESSION:
  308. if (!tpm2_add_session(chip, phandle))
  309. goto out_no_slots;
  310. break;
  311. default:
  312. dev_err(&chip->dev, "%s: unknown handle 0x%08X\n",
  313. __func__, phandle);
  314. break;
  315. };
  316. return 0;
  317. out_no_slots:
  318. tpm2_flush_context_cmd(chip, phandle, TPM_TRANSMIT_NESTED);
  319. dev_warn(&chip->dev, "%s: out of slots for 0x%08X\n", __func__,
  320. phandle);
  321. return -ENOMEM;
  322. }
  323. struct tpm2_cap_handles {
  324. u8 more_data;
  325. __be32 capability;
  326. __be32 count;
  327. __be32 handles[];
  328. } __packed;
  329. static int tpm2_map_response_body(struct tpm_chip *chip, u32 cc, u8 *rsp,
  330. size_t len)
  331. {
  332. struct tpm_space *space = &chip->work_space;
  333. struct tpm_output_header *header = (void *)rsp;
  334. struct tpm2_cap_handles *data;
  335. u32 phandle;
  336. u32 phandle_type;
  337. u32 vhandle;
  338. int i;
  339. int j;
  340. if (cc != TPM2_CC_GET_CAPABILITY ||
  341. be32_to_cpu(header->return_code) != TPM2_RC_SUCCESS) {
  342. return 0;
  343. }
  344. if (len < TPM_HEADER_SIZE + 9)
  345. return -EFAULT;
  346. data = (void *)&rsp[TPM_HEADER_SIZE];
  347. if (be32_to_cpu(data->capability) != TPM2_CAP_HANDLES)
  348. return 0;
  349. if (len != TPM_HEADER_SIZE + 9 + 4 * be32_to_cpu(data->count))
  350. return -EFAULT;
  351. for (i = 0, j = 0; i < be32_to_cpu(data->count); i++) {
  352. phandle = be32_to_cpup((__be32 *)&data->handles[i]);
  353. phandle_type = phandle & 0xFF000000;
  354. switch (phandle_type) {
  355. case TPM2_HT_TRANSIENT:
  356. vhandle = tpm2_map_to_vhandle(space, phandle, false);
  357. if (!vhandle)
  358. break;
  359. data->handles[j] = cpu_to_be32(vhandle);
  360. j++;
  361. break;
  362. default:
  363. data->handles[j] = cpu_to_be32(phandle);
  364. j++;
  365. break;
  366. }
  367. }
  368. header->length = cpu_to_be32(TPM_HEADER_SIZE + 9 + 4 * j);
  369. data->count = cpu_to_be32(j);
  370. return 0;
  371. }
  372. static int tpm2_save_space(struct tpm_chip *chip)
  373. {
  374. struct tpm_space *space = &chip->work_space;
  375. unsigned int offset;
  376. int i;
  377. int rc;
  378. for (i = 0, offset = 0; i < ARRAY_SIZE(space->context_tbl); i++) {
  379. if (!(space->context_tbl[i] && ~space->context_tbl[i]))
  380. continue;
  381. rc = tpm2_save_context(chip, space->context_tbl[i],
  382. space->context_buf, PAGE_SIZE,
  383. &offset);
  384. if (rc == -ENOENT) {
  385. space->context_tbl[i] = 0;
  386. continue;
  387. } else if (rc)
  388. return rc;
  389. tpm2_flush_context_cmd(chip, space->context_tbl[i],
  390. TPM_TRANSMIT_NESTED);
  391. space->context_tbl[i] = ~0;
  392. }
  393. for (i = 0, offset = 0; i < ARRAY_SIZE(space->session_tbl); i++) {
  394. if (!space->session_tbl[i])
  395. continue;
  396. rc = tpm2_save_context(chip, space->session_tbl[i],
  397. space->session_buf, PAGE_SIZE,
  398. &offset);
  399. if (rc == -ENOENT) {
  400. /* handle error saving session, just forget it */
  401. space->session_tbl[i] = 0;
  402. } else if (rc < 0) {
  403. tpm2_flush_space(chip);
  404. return rc;
  405. }
  406. }
  407. return 0;
  408. }
  409. int tpm2_commit_space(struct tpm_chip *chip, struct tpm_space *space,
  410. u32 cc, u8 *buf, size_t *bufsiz)
  411. {
  412. struct tpm_output_header *header = (void *)buf;
  413. int rc;
  414. if (!space)
  415. return 0;
  416. rc = tpm2_map_response_header(chip, cc, buf, *bufsiz);
  417. if (rc) {
  418. tpm2_flush_space(chip);
  419. return rc;
  420. }
  421. rc = tpm2_map_response_body(chip, cc, buf, *bufsiz);
  422. if (rc) {
  423. tpm2_flush_space(chip);
  424. return rc;
  425. }
  426. rc = tpm2_save_space(chip);
  427. if (rc) {
  428. tpm2_flush_space(chip);
  429. return rc;
  430. }
  431. *bufsiz = be32_to_cpu(header->length);
  432. memcpy(&space->context_tbl, &chip->work_space.context_tbl,
  433. sizeof(space->context_tbl));
  434. memcpy(&space->session_tbl, &chip->work_space.session_tbl,
  435. sizeof(space->session_tbl));
  436. memcpy(space->context_buf, chip->work_space.context_buf, PAGE_SIZE);
  437. memcpy(space->session_buf, chip->work_space.session_buf, PAGE_SIZE);
  438. return 0;
  439. }