2
0

tpm_emulator.c 31 KB

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