2
0

tpm_emulator.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Emulator TPM driver
  3. *
  4. * Copyright (c) 2017 Intel Corporation
  5. * Author: Amarnath Valluri <amarnath.valluri@intel.com>
  6. *
  7. * Copyright (c) 2010 - 2013, 2018 IBM Corporation
  8. * Authors:
  9. * Stefan Berger <stefanb@us.ibm.com>
  10. *
  11. * Copyright (C) 2011 IAIK, Graz University of Technology
  12. * Author: Andreas Niederl
  13. *
  14. * This library is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU Lesser General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2 of the License, or (at your option) any later version.
  18. *
  19. * This library is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * Lesser General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Lesser General Public
  25. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. #include "qemu/osdep.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/module.h"
  31. #include "qemu/sockets.h"
  32. #include "io/channel-socket.h"
  33. #include "sysemu/tpm_backend.h"
  34. #include "tpm_int.h"
  35. #include "tpm_util.h"
  36. #include "tpm_ioctl.h"
  37. #include "migration/blocker.h"
  38. #include "migration/vmstate.h"
  39. #include "qapi/error.h"
  40. #include "qapi/clone-visitor.h"
  41. #include "qapi/qapi-visit-tpm.h"
  42. #include "chardev/char-fe.h"
  43. #include "trace.h"
  44. #define TYPE_TPM_EMULATOR "tpm-emulator"
  45. #define TPM_EMULATOR(obj) \
  46. OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
  47. #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
  48. /* data structures */
  49. /* blobs from the TPM; part of VM state when migrating */
  50. typedef struct TPMBlobBuffers {
  51. uint32_t permanent_flags;
  52. TPMSizedBuffer permanent;
  53. uint32_t volatil_flags;
  54. TPMSizedBuffer volatil;
  55. uint32_t savestate_flags;
  56. TPMSizedBuffer savestate;
  57. } TPMBlobBuffers;
  58. typedef struct TPMEmulator {
  59. TPMBackend parent;
  60. TPMEmulatorOptions *options;
  61. CharBackend ctrl_chr;
  62. QIOChannel *data_ioc;
  63. TPMVersion tpm_version;
  64. ptm_cap caps; /* capabilities of the TPM */
  65. uint8_t cur_locty_number; /* last set locality */
  66. Error *migration_blocker;
  67. QemuMutex mutex;
  68. unsigned int established_flag:1;
  69. unsigned int established_flag_cached:1;
  70. TPMBlobBuffers state_blobs;
  71. } TPMEmulator;
  72. struct tpm_error {
  73. uint32_t tpm_result;
  74. const char *string;
  75. };
  76. static const struct tpm_error tpm_errors[] = {
  77. /* TPM 1.2 error codes */
  78. { TPM_BAD_PARAMETER , "a parameter is bad" },
  79. { TPM_FAIL , "operation failed" },
  80. { TPM_KEYNOTFOUND , "key could not be found" },
  81. { TPM_BAD_PARAM_SIZE , "bad parameter size"},
  82. { TPM_ENCRYPT_ERROR , "encryption error" },
  83. { TPM_DECRYPT_ERROR , "decryption error" },
  84. { TPM_BAD_KEY_PROPERTY, "bad key property" },
  85. { TPM_BAD_MODE , "bad (encryption) mode" },
  86. { TPM_BAD_VERSION , "bad version identifier" },
  87. { TPM_BAD_LOCALITY , "bad locality" },
  88. /* TPM 2 error codes */
  89. { TPM_RC_FAILURE , "operation failed" },
  90. { TPM_RC_LOCALITY , "bad locality" },
  91. { TPM_RC_INSUFFICIENT, "insufficient amount of data" },
  92. };
  93. static const char *tpm_emulator_strerror(uint32_t tpm_result)
  94. {
  95. size_t i;
  96. for (i = 0; i < ARRAY_SIZE(tpm_errors); i++) {
  97. if (tpm_errors[i].tpm_result == tpm_result) {
  98. return tpm_errors[i].string;
  99. }
  100. }
  101. return "";
  102. }
  103. static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
  104. size_t msg_len_in, size_t msg_len_out)
  105. {
  106. CharBackend *dev = &tpm->ctrl_chr;
  107. uint32_t cmd_no = cpu_to_be32(cmd);
  108. ssize_t n = sizeof(uint32_t) + msg_len_in;
  109. uint8_t *buf = NULL;
  110. int ret = -1;
  111. qemu_mutex_lock(&tpm->mutex);
  112. buf = g_alloca(n);
  113. memcpy(buf, &cmd_no, sizeof(cmd_no));
  114. memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
  115. n = qemu_chr_fe_write_all(dev, buf, n);
  116. if (n <= 0) {
  117. goto end;
  118. }
  119. if (msg_len_out != 0) {
  120. n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
  121. if (n <= 0) {
  122. goto end;
  123. }
  124. }
  125. ret = 0;
  126. end:
  127. qemu_mutex_unlock(&tpm->mutex);
  128. return ret;
  129. }
  130. static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
  131. const uint8_t *in, uint32_t in_len,
  132. uint8_t *out, uint32_t out_len,
  133. bool *selftest_done,
  134. Error **err)
  135. {
  136. ssize_t ret;
  137. bool is_selftest = false;
  138. if (selftest_done) {
  139. *selftest_done = false;
  140. is_selftest = tpm_util_is_selftest(in, in_len);
  141. }
  142. ret = qio_channel_write_all(tpm_emu->data_ioc, (char *)in, in_len, err);
  143. if (ret != 0) {
  144. return -1;
  145. }
  146. ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out,
  147. sizeof(struct tpm_resp_hdr), err);
  148. if (ret != 0) {
  149. return -1;
  150. }
  151. ret = qio_channel_read_all(tpm_emu->data_ioc,
  152. (char *)out + sizeof(struct tpm_resp_hdr),
  153. tpm_cmd_get_size(out) - sizeof(struct tpm_resp_hdr), err);
  154. if (ret != 0) {
  155. return -1;
  156. }
  157. if (is_selftest) {
  158. *selftest_done = tpm_cmd_get_errcode(out) == 0;
  159. }
  160. return 0;
  161. }
  162. static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
  163. Error **errp)
  164. {
  165. ptm_loc loc;
  166. if (tpm_emu->cur_locty_number == locty_number) {
  167. return 0;
  168. }
  169. trace_tpm_emulator_set_locality(locty_number);
  170. memset(&loc, 0, sizeof(loc));
  171. loc.u.req.loc = locty_number;
  172. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_LOCALITY, &loc,
  173. sizeof(loc), sizeof(loc)) < 0) {
  174. error_setg(errp, "tpm-emulator: could not set locality : %s",
  175. strerror(errno));
  176. return -1;
  177. }
  178. loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
  179. if (loc.u.resp.tpm_result != 0) {
  180. error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
  181. loc.u.resp.tpm_result);
  182. return -1;
  183. }
  184. tpm_emu->cur_locty_number = locty_number;
  185. return 0;
  186. }
  187. static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
  188. Error **errp)
  189. {
  190. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  191. trace_tpm_emulator_handle_request();
  192. if (tpm_emulator_set_locality(tpm_emu, cmd->locty, errp) < 0 ||
  193. tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
  194. cmd->out, cmd->out_len,
  195. &cmd->selftest_done, errp) < 0) {
  196. tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
  197. }
  198. }
  199. static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
  200. {
  201. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_CAPABILITY,
  202. &tpm_emu->caps, 0, sizeof(tpm_emu->caps)) < 0) {
  203. error_report("tpm-emulator: probing failed : %s", strerror(errno));
  204. return -1;
  205. }
  206. tpm_emu->caps = be64_to_cpu(tpm_emu->caps);
  207. trace_tpm_emulator_probe_caps(tpm_emu->caps);
  208. return 0;
  209. }
  210. static int tpm_emulator_check_caps(TPMEmulator *tpm_emu)
  211. {
  212. ptm_cap caps = 0;
  213. const char *tpm = NULL;
  214. /* check for min. required capabilities */
  215. switch (tpm_emu->tpm_version) {
  216. case TPM_VERSION_1_2:
  217. caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
  218. PTM_CAP_SET_LOCALITY | PTM_CAP_SET_DATAFD | PTM_CAP_STOP |
  219. PTM_CAP_SET_BUFFERSIZE;
  220. tpm = "1.2";
  221. break;
  222. case TPM_VERSION_2_0:
  223. caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
  224. PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED |
  225. PTM_CAP_SET_DATAFD | PTM_CAP_STOP | PTM_CAP_SET_BUFFERSIZE;
  226. tpm = "2";
  227. break;
  228. case TPM_VERSION_UNSPEC:
  229. error_report("tpm-emulator: TPM version has not been set");
  230. return -1;
  231. }
  232. if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
  233. error_report("tpm-emulator: TPM does not implement minimum set of "
  234. "required capabilities for TPM %s (0x%x)", tpm, (int)caps);
  235. return -1;
  236. }
  237. return 0;
  238. }
  239. static int tpm_emulator_stop_tpm(TPMBackend *tb)
  240. {
  241. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  242. ptm_res res;
  243. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_STOP, &res, 0, sizeof(res)) < 0) {
  244. error_report("tpm-emulator: Could not stop TPM: %s",
  245. strerror(errno));
  246. return -1;
  247. }
  248. res = be32_to_cpu(res);
  249. if (res) {
  250. error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x %s", res,
  251. tpm_emulator_strerror(res));
  252. return -1;
  253. }
  254. return 0;
  255. }
  256. static int tpm_emulator_set_buffer_size(TPMBackend *tb,
  257. size_t wanted_size,
  258. size_t *actual_size)
  259. {
  260. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  261. ptm_setbuffersize psbs;
  262. if (tpm_emulator_stop_tpm(tb) < 0) {
  263. return -1;
  264. }
  265. psbs.u.req.buffersize = cpu_to_be32(wanted_size);
  266. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_BUFFERSIZE, &psbs,
  267. sizeof(psbs.u.req), sizeof(psbs.u.resp)) < 0) {
  268. error_report("tpm-emulator: Could not set buffer size: %s",
  269. strerror(errno));
  270. return -1;
  271. }
  272. psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result);
  273. if (psbs.u.resp.tpm_result != 0) {
  274. error_report("tpm-emulator: TPM result for set buffer size : 0x%x %s",
  275. psbs.u.resp.tpm_result,
  276. tpm_emulator_strerror(psbs.u.resp.tpm_result));
  277. return -1;
  278. }
  279. if (actual_size) {
  280. *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
  281. }
  282. trace_tpm_emulator_set_buffer_size(
  283. be32_to_cpu(psbs.u.resp.buffersize),
  284. be32_to_cpu(psbs.u.resp.minsize),
  285. be32_to_cpu(psbs.u.resp.maxsize));
  286. return 0;
  287. }
  288. static int tpm_emulator_startup_tpm_resume(TPMBackend *tb, size_t buffersize,
  289. bool is_resume)
  290. {
  291. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  292. ptm_init init = {
  293. .u.req.init_flags = 0,
  294. };
  295. ptm_res res;
  296. trace_tpm_emulator_startup_tpm_resume(is_resume, buffersize);
  297. if (buffersize != 0 &&
  298. tpm_emulator_set_buffer_size(tb, buffersize, NULL) < 0) {
  299. goto err_exit;
  300. }
  301. if (is_resume) {
  302. init.u.req.init_flags |= cpu_to_be32(PTM_INIT_FLAG_DELETE_VOLATILE);
  303. }
  304. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_INIT, &init, sizeof(init),
  305. sizeof(init)) < 0) {
  306. error_report("tpm-emulator: could not send INIT: %s",
  307. strerror(errno));
  308. goto err_exit;
  309. }
  310. res = be32_to_cpu(init.u.resp.tpm_result);
  311. if (res) {
  312. error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x %s", res,
  313. tpm_emulator_strerror(res));
  314. goto err_exit;
  315. }
  316. return 0;
  317. err_exit:
  318. return -1;
  319. }
  320. static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
  321. {
  322. return tpm_emulator_startup_tpm_resume(tb, buffersize, false);
  323. }
  324. static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
  325. {
  326. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  327. ptm_est est;
  328. if (tpm_emu->established_flag_cached) {
  329. return tpm_emu->established_flag;
  330. }
  331. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
  332. 0, sizeof(est)) < 0) {
  333. error_report("tpm-emulator: Could not get the TPM established flag: %s",
  334. strerror(errno));
  335. return false;
  336. }
  337. trace_tpm_emulator_get_tpm_established_flag(est.u.resp.bit);
  338. tpm_emu->established_flag_cached = 1;
  339. tpm_emu->established_flag = (est.u.resp.bit != 0);
  340. return tpm_emu->established_flag;
  341. }
  342. static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
  343. uint8_t locty)
  344. {
  345. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  346. ptm_reset_est reset_est;
  347. ptm_res res;
  348. /* only a TPM 2.0 will support this */
  349. if (tpm_emu->tpm_version != TPM_VERSION_2_0) {
  350. return 0;
  351. }
  352. reset_est.u.req.loc = tpm_emu->cur_locty_number;
  353. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_RESET_TPMESTABLISHED,
  354. &reset_est, sizeof(reset_est),
  355. sizeof(reset_est)) < 0) {
  356. error_report("tpm-emulator: Could not reset the establishment bit: %s",
  357. strerror(errno));
  358. return -1;
  359. }
  360. res = be32_to_cpu(reset_est.u.resp.tpm_result);
  361. if (res) {
  362. error_report(
  363. "tpm-emulator: TPM result for rest established flag: 0x%x %s",
  364. res, tpm_emulator_strerror(res));
  365. return -1;
  366. }
  367. tpm_emu->established_flag_cached = 0;
  368. return 0;
  369. }
  370. static void tpm_emulator_cancel_cmd(TPMBackend *tb)
  371. {
  372. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  373. ptm_res res;
  374. if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, PTM_CAP_CANCEL_TPM_CMD)) {
  375. trace_tpm_emulator_cancel_cmd_not_supt();
  376. return;
  377. }
  378. /* FIXME: make the function non-blocking, or it may block a VCPU */
  379. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_CANCEL_TPM_CMD, &res, 0,
  380. sizeof(res)) < 0) {
  381. error_report("tpm-emulator: Could not cancel command: %s",
  382. strerror(errno));
  383. } else if (res != 0) {
  384. error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
  385. be32_to_cpu(res));
  386. }
  387. }
  388. static TPMVersion tpm_emulator_get_tpm_version(TPMBackend *tb)
  389. {
  390. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  391. return tpm_emu->tpm_version;
  392. }
  393. static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)
  394. {
  395. size_t actual_size;
  396. if (tpm_emulator_set_buffer_size(tb, 0, &actual_size) < 0) {
  397. return 4096;
  398. }
  399. return actual_size;
  400. }
  401. static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
  402. {
  403. Error *err = NULL;
  404. ptm_cap caps = PTM_CAP_GET_STATEBLOB | PTM_CAP_SET_STATEBLOB |
  405. PTM_CAP_STOP;
  406. if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
  407. error_setg(&tpm_emu->migration_blocker,
  408. "Migration disabled: TPM emulator does not support "
  409. "migration");
  410. migrate_add_blocker(tpm_emu->migration_blocker, &err);
  411. if (err) {
  412. error_report_err(err);
  413. error_free(tpm_emu->migration_blocker);
  414. tpm_emu->migration_blocker = NULL;
  415. return -1;
  416. }
  417. }
  418. return 0;
  419. }
  420. static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
  421. {
  422. ptm_res res;
  423. Error *err = NULL;
  424. int fds[2] = { -1, -1 };
  425. if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
  426. error_report("tpm-emulator: Failed to create socketpair");
  427. return -1;
  428. }
  429. qemu_chr_fe_set_msgfds(&tpm_emu->ctrl_chr, fds + 1, 1);
  430. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_DATAFD, &res, 0,
  431. sizeof(res)) < 0 || res != 0) {
  432. error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
  433. strerror(errno));
  434. goto err_exit;
  435. }
  436. tpm_emu->data_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fds[0], &err));
  437. if (err) {
  438. error_prepend(&err, "tpm-emulator: Failed to create io channel: ");
  439. error_report_err(err);
  440. goto err_exit;
  441. }
  442. closesocket(fds[1]);
  443. return 0;
  444. err_exit:
  445. closesocket(fds[0]);
  446. closesocket(fds[1]);
  447. return -1;
  448. }
  449. static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
  450. {
  451. const char *value;
  452. value = qemu_opt_get(opts, "chardev");
  453. if (value) {
  454. Error *err = NULL;
  455. Chardev *dev = qemu_chr_find(value);
  456. if (!dev) {
  457. error_report("tpm-emulator: tpm chardev '%s' not found.", value);
  458. goto err;
  459. }
  460. if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
  461. error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
  462. value);
  463. error_report_err(err);
  464. goto err;
  465. }
  466. tpm_emu->options->chardev = g_strdup(value);
  467. }
  468. if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
  469. goto err;
  470. }
  471. /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
  472. * by passthrough driver, which not yet using GIOChannel.
  473. */
  474. if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
  475. &tpm_emu->tpm_version)) {
  476. error_report("'%s' is not emulating TPM device. Error: %s",
  477. tpm_emu->options->chardev, strerror(errno));
  478. goto err;
  479. }
  480. switch (tpm_emu->tpm_version) {
  481. case TPM_VERSION_1_2:
  482. trace_tpm_emulator_handle_device_opts_tpm12();
  483. break;
  484. case TPM_VERSION_2_0:
  485. trace_tpm_emulator_handle_device_opts_tpm2();
  486. break;
  487. default:
  488. trace_tpm_emulator_handle_device_opts_unspec();
  489. }
  490. if (tpm_emulator_probe_caps(tpm_emu) ||
  491. tpm_emulator_check_caps(tpm_emu)) {
  492. goto err;
  493. }
  494. return tpm_emulator_block_migration(tpm_emu);
  495. err:
  496. trace_tpm_emulator_handle_device_opts_startup_error();
  497. return -1;
  498. }
  499. static TPMBackend *tpm_emulator_create(QemuOpts *opts)
  500. {
  501. TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
  502. if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
  503. object_unref(OBJECT(tb));
  504. return NULL;
  505. }
  506. return tb;
  507. }
  508. static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
  509. {
  510. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  511. TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
  512. options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
  513. options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
  514. return options;
  515. }
  516. static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
  517. TPM_STANDARD_CMDLINE_OPTS,
  518. {
  519. .name = "chardev",
  520. .type = QEMU_OPT_STRING,
  521. .help = "Character device to use for out-of-band control messages",
  522. },
  523. { /* end of list */ },
  524. };
  525. /*
  526. * Transfer a TPM state blob from the TPM into a provided buffer.
  527. *
  528. * @tpm_emu: TPMEmulator
  529. * @type: the type of blob to transfer
  530. * @tsb: the TPMSizeBuffer to fill with the blob
  531. * @flags: the flags to return to the caller
  532. */
  533. static int tpm_emulator_get_state_blob(TPMEmulator *tpm_emu,
  534. uint8_t type,
  535. TPMSizedBuffer *tsb,
  536. uint32_t *flags)
  537. {
  538. ptm_getstate pgs;
  539. ptm_res res;
  540. ssize_t n;
  541. uint32_t totlength, length;
  542. tpm_sized_buffer_reset(tsb);
  543. pgs.u.req.state_flags = cpu_to_be32(PTM_STATE_FLAG_DECRYPTED);
  544. pgs.u.req.type = cpu_to_be32(type);
  545. pgs.u.req.offset = 0;
  546. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_STATEBLOB,
  547. &pgs, sizeof(pgs.u.req),
  548. offsetof(ptm_getstate, u.resp.data)) < 0) {
  549. error_report("tpm-emulator: could not get state blob type %d : %s",
  550. type, strerror(errno));
  551. return -1;
  552. }
  553. res = be32_to_cpu(pgs.u.resp.tpm_result);
  554. if (res != 0 && (res & 0x800) == 0) {
  555. error_report("tpm-emulator: Getting the stateblob (type %d) failed "
  556. "with a TPM error 0x%x %s", type, res,
  557. tpm_emulator_strerror(res));
  558. return -1;
  559. }
  560. totlength = be32_to_cpu(pgs.u.resp.totlength);
  561. length = be32_to_cpu(pgs.u.resp.length);
  562. if (totlength != length) {
  563. error_report("tpm-emulator: Expecting to read %u bytes "
  564. "but would get %u", totlength, length);
  565. return -1;
  566. }
  567. *flags = be32_to_cpu(pgs.u.resp.state_flags);
  568. if (totlength > 0) {
  569. tsb->buffer = g_try_malloc(totlength);
  570. if (!tsb->buffer) {
  571. error_report("tpm-emulator: Out of memory allocating %u bytes",
  572. totlength);
  573. return -1;
  574. }
  575. n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr, tsb->buffer, totlength);
  576. if (n != totlength) {
  577. error_report("tpm-emulator: Could not read stateblob (type %d); "
  578. "expected %u bytes, got %zd",
  579. type, totlength, n);
  580. return -1;
  581. }
  582. }
  583. tsb->size = totlength;
  584. trace_tpm_emulator_get_state_blob(type, tsb->size, *flags);
  585. return 0;
  586. }
  587. static int tpm_emulator_get_state_blobs(TPMEmulator *tpm_emu)
  588. {
  589. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  590. if (tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
  591. &state_blobs->permanent,
  592. &state_blobs->permanent_flags) < 0 ||
  593. tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
  594. &state_blobs->volatil,
  595. &state_blobs->volatil_flags) < 0 ||
  596. tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
  597. &state_blobs->savestate,
  598. &state_blobs->savestate_flags) < 0) {
  599. goto err_exit;
  600. }
  601. return 0;
  602. err_exit:
  603. tpm_sized_buffer_reset(&state_blobs->volatil);
  604. tpm_sized_buffer_reset(&state_blobs->permanent);
  605. tpm_sized_buffer_reset(&state_blobs->savestate);
  606. return -1;
  607. }
  608. /*
  609. * Transfer a TPM state blob to the TPM emulator.
  610. *
  611. * @tpm_emu: TPMEmulator
  612. * @type: the type of TPM state blob to transfer
  613. * @tsb: TPMSizedBuffer containing the TPM state blob
  614. * @flags: Flags describing the (encryption) state of the TPM state blob
  615. */
  616. static int tpm_emulator_set_state_blob(TPMEmulator *tpm_emu,
  617. uint32_t type,
  618. TPMSizedBuffer *tsb,
  619. uint32_t flags)
  620. {
  621. ssize_t n;
  622. ptm_setstate pss;
  623. ptm_res tpm_result;
  624. if (tsb->size == 0) {
  625. return 0;
  626. }
  627. pss = (ptm_setstate) {
  628. .u.req.state_flags = cpu_to_be32(flags),
  629. .u.req.type = cpu_to_be32(type),
  630. .u.req.length = cpu_to_be32(tsb->size),
  631. };
  632. /* write the header only */
  633. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_STATEBLOB, &pss,
  634. offsetof(ptm_setstate, u.req.data), 0) < 0) {
  635. error_report("tpm-emulator: could not set state blob type %d : %s",
  636. type, strerror(errno));
  637. return -1;
  638. }
  639. /* now the body */
  640. n = qemu_chr_fe_write_all(&tpm_emu->ctrl_chr, tsb->buffer, tsb->size);
  641. if (n != tsb->size) {
  642. error_report("tpm-emulator: Writing the stateblob (type %d) "
  643. "failed; could not write %u bytes, but only %zd",
  644. type, tsb->size, n);
  645. return -1;
  646. }
  647. /* now get the result */
  648. n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr,
  649. (uint8_t *)&pss, sizeof(pss.u.resp));
  650. if (n != sizeof(pss.u.resp)) {
  651. error_report("tpm-emulator: Reading response from writing stateblob "
  652. "(type %d) failed; expected %zu bytes, got %zd", type,
  653. sizeof(pss.u.resp), n);
  654. return -1;
  655. }
  656. tpm_result = be32_to_cpu(pss.u.resp.tpm_result);
  657. if (tpm_result != 0) {
  658. error_report("tpm-emulator: Setting the stateblob (type %d) failed "
  659. "with a TPM error 0x%x %s", type, tpm_result,
  660. tpm_emulator_strerror(tpm_result));
  661. return -1;
  662. }
  663. trace_tpm_emulator_set_state_blob(type, tsb->size, flags);
  664. return 0;
  665. }
  666. /*
  667. * Set all the TPM state blobs.
  668. *
  669. * Returns a negative errno code in case of error.
  670. */
  671. static int tpm_emulator_set_state_blobs(TPMBackend *tb)
  672. {
  673. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  674. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  675. trace_tpm_emulator_set_state_blobs();
  676. if (tpm_emulator_stop_tpm(tb) < 0) {
  677. trace_tpm_emulator_set_state_blobs_error("Could not stop TPM");
  678. return -EIO;
  679. }
  680. if (tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
  681. &state_blobs->permanent,
  682. state_blobs->permanent_flags) < 0 ||
  683. tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
  684. &state_blobs->volatil,
  685. state_blobs->volatil_flags) < 0 ||
  686. tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
  687. &state_blobs->savestate,
  688. state_blobs->savestate_flags) < 0) {
  689. return -EIO;
  690. }
  691. trace_tpm_emulator_set_state_blobs_done();
  692. return 0;
  693. }
  694. static int tpm_emulator_pre_save(void *opaque)
  695. {
  696. TPMBackend *tb = opaque;
  697. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  698. trace_tpm_emulator_pre_save();
  699. tpm_backend_finish_sync(tb);
  700. /* get the state blobs from the TPM */
  701. return tpm_emulator_get_state_blobs(tpm_emu);
  702. }
  703. /*
  704. * Load the TPM state blobs into the TPM.
  705. *
  706. * Returns negative errno codes in case of error.
  707. */
  708. static int tpm_emulator_post_load(void *opaque, int version_id)
  709. {
  710. TPMBackend *tb = opaque;
  711. int ret;
  712. ret = tpm_emulator_set_state_blobs(tb);
  713. if (ret < 0) {
  714. return ret;
  715. }
  716. if (tpm_emulator_startup_tpm_resume(tb, 0, true) < 0) {
  717. return -EIO;
  718. }
  719. return 0;
  720. }
  721. static const VMStateDescription vmstate_tpm_emulator = {
  722. .name = "tpm-emulator",
  723. .version_id = 0,
  724. .pre_save = tpm_emulator_pre_save,
  725. .post_load = tpm_emulator_post_load,
  726. .fields = (VMStateField[]) {
  727. VMSTATE_UINT32(state_blobs.permanent_flags, TPMEmulator),
  728. VMSTATE_UINT32(state_blobs.permanent.size, TPMEmulator),
  729. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.permanent.buffer,
  730. TPMEmulator, 0, 0,
  731. state_blobs.permanent.size),
  732. VMSTATE_UINT32(state_blobs.volatil_flags, TPMEmulator),
  733. VMSTATE_UINT32(state_blobs.volatil.size, TPMEmulator),
  734. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.volatil.buffer,
  735. TPMEmulator, 0, 0,
  736. state_blobs.volatil.size),
  737. VMSTATE_UINT32(state_blobs.savestate_flags, TPMEmulator),
  738. VMSTATE_UINT32(state_blobs.savestate.size, TPMEmulator),
  739. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.savestate.buffer,
  740. TPMEmulator, 0, 0,
  741. state_blobs.savestate.size),
  742. VMSTATE_END_OF_LIST()
  743. }
  744. };
  745. static void tpm_emulator_inst_init(Object *obj)
  746. {
  747. TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
  748. trace_tpm_emulator_inst_init();
  749. tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
  750. tpm_emu->cur_locty_number = ~0;
  751. qemu_mutex_init(&tpm_emu->mutex);
  752. vmstate_register(NULL, -1, &vmstate_tpm_emulator, obj);
  753. }
  754. /*
  755. * Gracefully shut down the external TPM
  756. */
  757. static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
  758. {
  759. ptm_res res;
  760. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
  761. error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
  762. strerror(errno));
  763. } else if (res != 0) {
  764. error_report("tpm-emulator: TPM result for shutdown: 0x%x %s",
  765. be32_to_cpu(res), tpm_emulator_strerror(be32_to_cpu(res)));
  766. }
  767. }
  768. static void tpm_emulator_inst_finalize(Object *obj)
  769. {
  770. TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
  771. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  772. tpm_emulator_shutdown(tpm_emu);
  773. object_unref(OBJECT(tpm_emu->data_ioc));
  774. qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
  775. qapi_free_TPMEmulatorOptions(tpm_emu->options);
  776. if (tpm_emu->migration_blocker) {
  777. migrate_del_blocker(tpm_emu->migration_blocker);
  778. error_free(tpm_emu->migration_blocker);
  779. }
  780. tpm_sized_buffer_reset(&state_blobs->volatil);
  781. tpm_sized_buffer_reset(&state_blobs->permanent);
  782. tpm_sized_buffer_reset(&state_blobs->savestate);
  783. qemu_mutex_destroy(&tpm_emu->mutex);
  784. vmstate_unregister(NULL, &vmstate_tpm_emulator, obj);
  785. }
  786. static void tpm_emulator_class_init(ObjectClass *klass, void *data)
  787. {
  788. TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
  789. tbc->type = TPM_TYPE_EMULATOR;
  790. tbc->opts = tpm_emulator_cmdline_opts;
  791. tbc->desc = "TPM emulator backend driver";
  792. tbc->create = tpm_emulator_create;
  793. tbc->startup_tpm = tpm_emulator_startup_tpm;
  794. tbc->cancel_cmd = tpm_emulator_cancel_cmd;
  795. tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
  796. tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
  797. tbc->get_tpm_version = tpm_emulator_get_tpm_version;
  798. tbc->get_buffer_size = tpm_emulator_get_buffer_size;
  799. tbc->get_tpm_options = tpm_emulator_get_tpm_options;
  800. tbc->handle_request = tpm_emulator_handle_request;
  801. }
  802. static const TypeInfo tpm_emulator_info = {
  803. .name = TYPE_TPM_EMULATOR,
  804. .parent = TYPE_TPM_BACKEND,
  805. .instance_size = sizeof(TPMEmulator),
  806. .class_init = tpm_emulator_class_init,
  807. .instance_init = tpm_emulator_inst_init,
  808. .instance_finalize = tpm_emulator_inst_finalize,
  809. };
  810. static void tpm_emulator_register(void)
  811. {
  812. type_register_static(&tpm_emulator_info);
  813. }
  814. type_init(tpm_emulator_register)