2
0

tpm_emulator.c 32 KB

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