tpm_emulator.c 31 KB

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