tpm_emulator.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 "sysemu/tpm_util.h"
  35. #include "tpm_int.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. #include "qom/object.h"
  45. #define TYPE_TPM_EMULATOR "tpm-emulator"
  46. OBJECT_DECLARE_SIMPLE_TYPE(TPMEmulator, 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. 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. };
  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 **errp)
  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, errp);
  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), errp);
  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), errp);
  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. Error *err = NULL;
  453. Chardev *dev;
  454. value = qemu_opt_get(opts, "chardev");
  455. if (!value) {
  456. error_report("tpm-emulator: parameter 'chardev' is missing");
  457. goto err;
  458. }
  459. dev = qemu_chr_find(value);
  460. if (!dev) {
  461. error_report("tpm-emulator: tpm chardev '%s' not found", value);
  462. goto err;
  463. }
  464. if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
  465. error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
  466. value);
  467. error_report_err(err);
  468. goto err;
  469. }
  470. tpm_emu->options->chardev = g_strdup(value);
  471. if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
  472. goto err;
  473. }
  474. /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
  475. * by passthrough driver, which not yet using GIOChannel.
  476. */
  477. if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
  478. &tpm_emu->tpm_version)) {
  479. error_report("'%s' is not emulating TPM device. Error: %s",
  480. tpm_emu->options->chardev, strerror(errno));
  481. goto err;
  482. }
  483. switch (tpm_emu->tpm_version) {
  484. case TPM_VERSION_1_2:
  485. trace_tpm_emulator_handle_device_opts_tpm12();
  486. break;
  487. case TPM_VERSION_2_0:
  488. trace_tpm_emulator_handle_device_opts_tpm2();
  489. break;
  490. default:
  491. trace_tpm_emulator_handle_device_opts_unspec();
  492. }
  493. if (tpm_emulator_probe_caps(tpm_emu) ||
  494. tpm_emulator_check_caps(tpm_emu)) {
  495. goto err;
  496. }
  497. return tpm_emulator_block_migration(tpm_emu);
  498. err:
  499. trace_tpm_emulator_handle_device_opts_startup_error();
  500. return -1;
  501. }
  502. static TPMBackend *tpm_emulator_create(QemuOpts *opts)
  503. {
  504. TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
  505. if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
  506. object_unref(OBJECT(tb));
  507. return NULL;
  508. }
  509. return tb;
  510. }
  511. static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
  512. {
  513. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  514. TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
  515. options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
  516. options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
  517. return options;
  518. }
  519. static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
  520. TPM_STANDARD_CMDLINE_OPTS,
  521. {
  522. .name = "chardev",
  523. .type = QEMU_OPT_STRING,
  524. .help = "Character device to use for out-of-band control messages",
  525. },
  526. { /* end of list */ },
  527. };
  528. /*
  529. * Transfer a TPM state blob from the TPM into a provided buffer.
  530. *
  531. * @tpm_emu: TPMEmulator
  532. * @type: the type of blob to transfer
  533. * @tsb: the TPMSizeBuffer to fill with the blob
  534. * @flags: the flags to return to the caller
  535. */
  536. static int tpm_emulator_get_state_blob(TPMEmulator *tpm_emu,
  537. uint8_t type,
  538. TPMSizedBuffer *tsb,
  539. uint32_t *flags)
  540. {
  541. ptm_getstate pgs;
  542. ptm_res res;
  543. ssize_t n;
  544. uint32_t totlength, length;
  545. tpm_sized_buffer_reset(tsb);
  546. pgs.u.req.state_flags = cpu_to_be32(PTM_STATE_FLAG_DECRYPTED);
  547. pgs.u.req.type = cpu_to_be32(type);
  548. pgs.u.req.offset = 0;
  549. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_STATEBLOB,
  550. &pgs, sizeof(pgs.u.req),
  551. offsetof(ptm_getstate, u.resp.data)) < 0) {
  552. error_report("tpm-emulator: could not get state blob type %d : %s",
  553. type, strerror(errno));
  554. return -1;
  555. }
  556. res = be32_to_cpu(pgs.u.resp.tpm_result);
  557. if (res != 0 && (res & 0x800) == 0) {
  558. error_report("tpm-emulator: Getting the stateblob (type %d) failed "
  559. "with a TPM error 0x%x %s", type, res,
  560. tpm_emulator_strerror(res));
  561. return -1;
  562. }
  563. totlength = be32_to_cpu(pgs.u.resp.totlength);
  564. length = be32_to_cpu(pgs.u.resp.length);
  565. if (totlength != length) {
  566. error_report("tpm-emulator: Expecting to read %u bytes "
  567. "but would get %u", totlength, length);
  568. return -1;
  569. }
  570. *flags = be32_to_cpu(pgs.u.resp.state_flags);
  571. if (totlength > 0) {
  572. tsb->buffer = g_try_malloc(totlength);
  573. if (!tsb->buffer) {
  574. error_report("tpm-emulator: Out of memory allocating %u bytes",
  575. totlength);
  576. return -1;
  577. }
  578. n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr, tsb->buffer, totlength);
  579. if (n != totlength) {
  580. error_report("tpm-emulator: Could not read stateblob (type %d); "
  581. "expected %u bytes, got %zd",
  582. type, totlength, n);
  583. return -1;
  584. }
  585. }
  586. tsb->size = totlength;
  587. trace_tpm_emulator_get_state_blob(type, tsb->size, *flags);
  588. return 0;
  589. }
  590. static int tpm_emulator_get_state_blobs(TPMEmulator *tpm_emu)
  591. {
  592. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  593. if (tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
  594. &state_blobs->permanent,
  595. &state_blobs->permanent_flags) < 0 ||
  596. tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
  597. &state_blobs->volatil,
  598. &state_blobs->volatil_flags) < 0 ||
  599. tpm_emulator_get_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
  600. &state_blobs->savestate,
  601. &state_blobs->savestate_flags) < 0) {
  602. goto err_exit;
  603. }
  604. return 0;
  605. err_exit:
  606. tpm_sized_buffer_reset(&state_blobs->volatil);
  607. tpm_sized_buffer_reset(&state_blobs->permanent);
  608. tpm_sized_buffer_reset(&state_blobs->savestate);
  609. return -1;
  610. }
  611. /*
  612. * Transfer a TPM state blob to the TPM emulator.
  613. *
  614. * @tpm_emu: TPMEmulator
  615. * @type: the type of TPM state blob to transfer
  616. * @tsb: TPMSizedBuffer containing the TPM state blob
  617. * @flags: Flags describing the (encryption) state of the TPM state blob
  618. */
  619. static int tpm_emulator_set_state_blob(TPMEmulator *tpm_emu,
  620. uint32_t type,
  621. TPMSizedBuffer *tsb,
  622. uint32_t flags)
  623. {
  624. ssize_t n;
  625. ptm_setstate pss;
  626. ptm_res tpm_result;
  627. if (tsb->size == 0) {
  628. return 0;
  629. }
  630. pss = (ptm_setstate) {
  631. .u.req.state_flags = cpu_to_be32(flags),
  632. .u.req.type = cpu_to_be32(type),
  633. .u.req.length = cpu_to_be32(tsb->size),
  634. };
  635. /* write the header only */
  636. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_STATEBLOB, &pss,
  637. offsetof(ptm_setstate, u.req.data), 0) < 0) {
  638. error_report("tpm-emulator: could not set state blob type %d : %s",
  639. type, strerror(errno));
  640. return -1;
  641. }
  642. /* now the body */
  643. n = qemu_chr_fe_write_all(&tpm_emu->ctrl_chr, tsb->buffer, tsb->size);
  644. if (n != tsb->size) {
  645. error_report("tpm-emulator: Writing the stateblob (type %d) "
  646. "failed; could not write %u bytes, but only %zd",
  647. type, tsb->size, n);
  648. return -1;
  649. }
  650. /* now get the result */
  651. n = qemu_chr_fe_read_all(&tpm_emu->ctrl_chr,
  652. (uint8_t *)&pss, sizeof(pss.u.resp));
  653. if (n != sizeof(pss.u.resp)) {
  654. error_report("tpm-emulator: Reading response from writing stateblob "
  655. "(type %d) failed; expected %zu bytes, got %zd", type,
  656. sizeof(pss.u.resp), n);
  657. return -1;
  658. }
  659. tpm_result = be32_to_cpu(pss.u.resp.tpm_result);
  660. if (tpm_result != 0) {
  661. error_report("tpm-emulator: Setting the stateblob (type %d) failed "
  662. "with a TPM error 0x%x %s", type, tpm_result,
  663. tpm_emulator_strerror(tpm_result));
  664. return -1;
  665. }
  666. trace_tpm_emulator_set_state_blob(type, tsb->size, flags);
  667. return 0;
  668. }
  669. /*
  670. * Set all the TPM state blobs.
  671. *
  672. * Returns a negative errno code in case of error.
  673. */
  674. static int tpm_emulator_set_state_blobs(TPMBackend *tb)
  675. {
  676. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  677. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  678. trace_tpm_emulator_set_state_blobs();
  679. if (tpm_emulator_stop_tpm(tb) < 0) {
  680. trace_tpm_emulator_set_state_blobs_error("Could not stop TPM");
  681. return -EIO;
  682. }
  683. if (tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_PERMANENT,
  684. &state_blobs->permanent,
  685. state_blobs->permanent_flags) < 0 ||
  686. tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_VOLATILE,
  687. &state_blobs->volatil,
  688. state_blobs->volatil_flags) < 0 ||
  689. tpm_emulator_set_state_blob(tpm_emu, PTM_BLOB_TYPE_SAVESTATE,
  690. &state_blobs->savestate,
  691. state_blobs->savestate_flags) < 0) {
  692. return -EIO;
  693. }
  694. trace_tpm_emulator_set_state_blobs_done();
  695. return 0;
  696. }
  697. static int tpm_emulator_pre_save(void *opaque)
  698. {
  699. TPMBackend *tb = opaque;
  700. TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
  701. trace_tpm_emulator_pre_save();
  702. tpm_backend_finish_sync(tb);
  703. /* get the state blobs from the TPM */
  704. return tpm_emulator_get_state_blobs(tpm_emu);
  705. }
  706. /*
  707. * Load the TPM state blobs into the TPM.
  708. *
  709. * Returns negative errno codes in case of error.
  710. */
  711. static int tpm_emulator_post_load(void *opaque, int version_id)
  712. {
  713. TPMBackend *tb = opaque;
  714. int ret;
  715. ret = tpm_emulator_set_state_blobs(tb);
  716. if (ret < 0) {
  717. return ret;
  718. }
  719. if (tpm_emulator_startup_tpm_resume(tb, 0, true) < 0) {
  720. return -EIO;
  721. }
  722. return 0;
  723. }
  724. static const VMStateDescription vmstate_tpm_emulator = {
  725. .name = "tpm-emulator",
  726. .version_id = 0,
  727. .pre_save = tpm_emulator_pre_save,
  728. .post_load = tpm_emulator_post_load,
  729. .fields = (VMStateField[]) {
  730. VMSTATE_UINT32(state_blobs.permanent_flags, TPMEmulator),
  731. VMSTATE_UINT32(state_blobs.permanent.size, TPMEmulator),
  732. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.permanent.buffer,
  733. TPMEmulator, 0, 0,
  734. state_blobs.permanent.size),
  735. VMSTATE_UINT32(state_blobs.volatil_flags, TPMEmulator),
  736. VMSTATE_UINT32(state_blobs.volatil.size, TPMEmulator),
  737. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.volatil.buffer,
  738. TPMEmulator, 0, 0,
  739. state_blobs.volatil.size),
  740. VMSTATE_UINT32(state_blobs.savestate_flags, TPMEmulator),
  741. VMSTATE_UINT32(state_blobs.savestate.size, TPMEmulator),
  742. VMSTATE_VBUFFER_ALLOC_UINT32(state_blobs.savestate.buffer,
  743. TPMEmulator, 0, 0,
  744. state_blobs.savestate.size),
  745. VMSTATE_END_OF_LIST()
  746. }
  747. };
  748. static void tpm_emulator_inst_init(Object *obj)
  749. {
  750. TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
  751. trace_tpm_emulator_inst_init();
  752. tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
  753. tpm_emu->cur_locty_number = ~0;
  754. qemu_mutex_init(&tpm_emu->mutex);
  755. vmstate_register(NULL, VMSTATE_INSTANCE_ID_ANY,
  756. &vmstate_tpm_emulator, obj);
  757. }
  758. /*
  759. * Gracefully shut down the external TPM
  760. */
  761. static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
  762. {
  763. ptm_res res;
  764. if (!tpm_emu->options->chardev) {
  765. /* was never properly initialized */
  766. return;
  767. }
  768. if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
  769. error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
  770. strerror(errno));
  771. } else if (res != 0) {
  772. error_report("tpm-emulator: TPM result for shutdown: 0x%x %s",
  773. be32_to_cpu(res), tpm_emulator_strerror(be32_to_cpu(res)));
  774. }
  775. }
  776. static void tpm_emulator_inst_finalize(Object *obj)
  777. {
  778. TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
  779. TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
  780. tpm_emulator_shutdown(tpm_emu);
  781. object_unref(OBJECT(tpm_emu->data_ioc));
  782. qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
  783. qapi_free_TPMEmulatorOptions(tpm_emu->options);
  784. if (tpm_emu->migration_blocker) {
  785. migrate_del_blocker(tpm_emu->migration_blocker);
  786. error_free(tpm_emu->migration_blocker);
  787. }
  788. tpm_sized_buffer_reset(&state_blobs->volatil);
  789. tpm_sized_buffer_reset(&state_blobs->permanent);
  790. tpm_sized_buffer_reset(&state_blobs->savestate);
  791. qemu_mutex_destroy(&tpm_emu->mutex);
  792. vmstate_unregister(NULL, &vmstate_tpm_emulator, obj);
  793. }
  794. static void tpm_emulator_class_init(ObjectClass *klass, void *data)
  795. {
  796. TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
  797. tbc->type = TPM_TYPE_EMULATOR;
  798. tbc->opts = tpm_emulator_cmdline_opts;
  799. tbc->desc = "TPM emulator backend driver";
  800. tbc->create = tpm_emulator_create;
  801. tbc->startup_tpm = tpm_emulator_startup_tpm;
  802. tbc->cancel_cmd = tpm_emulator_cancel_cmd;
  803. tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
  804. tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
  805. tbc->get_tpm_version = tpm_emulator_get_tpm_version;
  806. tbc->get_buffer_size = tpm_emulator_get_buffer_size;
  807. tbc->get_tpm_options = tpm_emulator_get_tpm_options;
  808. tbc->handle_request = tpm_emulator_handle_request;
  809. }
  810. static const TypeInfo tpm_emulator_info = {
  811. .name = TYPE_TPM_EMULATOR,
  812. .parent = TYPE_TPM_BACKEND,
  813. .instance_size = sizeof(TPMEmulator),
  814. .class_init = tpm_emulator_class_init,
  815. .instance_init = tpm_emulator_inst_init,
  816. .instance_finalize = tpm_emulator_inst_finalize,
  817. };
  818. static void tpm_emulator_register(void)
  819. {
  820. type_register_static(&tpm_emulator_info);
  821. }
  822. type_init(tpm_emulator_register)