tpm_passthrough.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * passthrough TPM driver
  3. *
  4. * Copyright (c) 2010 - 2013 IBM Corporation
  5. * Authors:
  6. * Stefan Berger <stefanb@us.ibm.com>
  7. *
  8. * Copyright (C) 2011 IAIK, Graz University of Technology
  9. * Author: Andreas Niederl
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, see <http://www.gnu.org/licenses/>
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu-common.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/module.h"
  28. #include "qemu/sockets.h"
  29. #include "sysemu/tpm_backend.h"
  30. #include "tpm_int.h"
  31. #include "qapi/clone-visitor.h"
  32. #include "qapi/qapi-visit-tpm.h"
  33. #include "tpm_util.h"
  34. #include "trace.h"
  35. #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
  36. #define TPM_PASSTHROUGH(obj) \
  37. OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
  38. /* data structures */
  39. struct TPMPassthruState {
  40. TPMBackend parent;
  41. TPMPassthroughOptions *options;
  42. const char *tpm_dev;
  43. int tpm_fd;
  44. bool tpm_executing;
  45. bool tpm_op_canceled;
  46. int cancel_fd;
  47. TPMVersion tpm_version;
  48. size_t tpm_buffersize;
  49. };
  50. typedef struct TPMPassthruState TPMPassthruState;
  51. #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
  52. /* functions */
  53. static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
  54. static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
  55. {
  56. int ret;
  57. reread:
  58. ret = read(fd, buf, len);
  59. if (ret < 0) {
  60. if (errno != EINTR && errno != EAGAIN) {
  61. return -1;
  62. }
  63. goto reread;
  64. }
  65. return ret;
  66. }
  67. static void tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
  68. const uint8_t *in, uint32_t in_len,
  69. uint8_t *out, uint32_t out_len,
  70. bool *selftest_done, Error **errp)
  71. {
  72. ssize_t ret;
  73. bool is_selftest;
  74. /* FIXME: protect shared variables or use other sync mechanism */
  75. tpm_pt->tpm_op_canceled = false;
  76. tpm_pt->tpm_executing = true;
  77. *selftest_done = false;
  78. is_selftest = tpm_util_is_selftest(in, in_len);
  79. ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
  80. if (ret != in_len) {
  81. if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
  82. error_setg_errno(errp, errno, "tpm_passthrough: error while "
  83. "transmitting data to TPM");
  84. }
  85. goto err_exit;
  86. }
  87. tpm_pt->tpm_executing = false;
  88. ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
  89. if (ret < 0) {
  90. if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
  91. error_setg_errno(errp, errno, "tpm_passthrough: error while "
  92. "reading data from TPM");
  93. }
  94. } else if (ret < sizeof(struct tpm_resp_hdr) ||
  95. tpm_cmd_get_size(out) != ret) {
  96. ret = -1;
  97. error_setg_errno(errp, errno, "tpm_passthrough: received invalid "
  98. "response packet from TPM");
  99. }
  100. if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
  101. *selftest_done = tpm_cmd_get_errcode(out) == 0;
  102. }
  103. err_exit:
  104. if (ret < 0) {
  105. tpm_util_write_fatal_error_response(out, out_len);
  106. }
  107. tpm_pt->tpm_executing = false;
  108. }
  109. static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
  110. Error **errp)
  111. {
  112. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
  113. trace_tpm_passthrough_handle_request(cmd);
  114. tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
  115. cmd->out, cmd->out_len, &cmd->selftest_done,
  116. errp);
  117. }
  118. static void tpm_passthrough_reset(TPMBackend *tb)
  119. {
  120. trace_tpm_passthrough_reset();
  121. tpm_passthrough_cancel_cmd(tb);
  122. }
  123. static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
  124. {
  125. return false;
  126. }
  127. static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
  128. uint8_t locty)
  129. {
  130. /* only a TPM 2.0 will support this */
  131. return 0;
  132. }
  133. static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
  134. {
  135. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
  136. int n;
  137. /*
  138. * As of Linux 3.7 the tpm_tis driver does not properly cancel
  139. * commands on all TPM manufacturers' TPMs.
  140. * Only cancel if we're busy so we don't cancel someone else's
  141. * command, e.g., a command executed on the host.
  142. */
  143. if (tpm_pt->tpm_executing) {
  144. if (tpm_pt->cancel_fd >= 0) {
  145. tpm_pt->tpm_op_canceled = true;
  146. n = write(tpm_pt->cancel_fd, "-", 1);
  147. if (n != 1) {
  148. error_report("Canceling TPM command failed: %s",
  149. strerror(errno));
  150. }
  151. } else {
  152. error_report("Cannot cancel TPM command due to missing "
  153. "TPM sysfs cancel entry");
  154. }
  155. }
  156. }
  157. static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
  158. {
  159. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
  160. return tpm_pt->tpm_version;
  161. }
  162. static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)
  163. {
  164. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
  165. int ret;
  166. ret = tpm_util_get_buffer_size(tpm_pt->tpm_fd, tpm_pt->tpm_version,
  167. &tpm_pt->tpm_buffersize);
  168. if (ret < 0) {
  169. tpm_pt->tpm_buffersize = 4096;
  170. }
  171. return tpm_pt->tpm_buffersize;
  172. }
  173. /*
  174. * Unless path or file descriptor set has been provided by user,
  175. * determine the sysfs cancel file following kernel documentation
  176. * in Documentation/ABI/stable/sysfs-class-tpm.
  177. * From /dev/tpm0 create /sys/class/tpm/tpm0/device/cancel
  178. * before 4.0: /sys/class/misc/tpm0/device/cancel
  179. */
  180. static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
  181. {
  182. int fd = -1;
  183. char *dev;
  184. char path[PATH_MAX];
  185. if (tpm_pt->options->cancel_path) {
  186. fd = qemu_open(tpm_pt->options->cancel_path, O_WRONLY);
  187. if (fd < 0) {
  188. error_report("tpm_passthrough: Could not open TPM cancel path: %s",
  189. strerror(errno));
  190. }
  191. return fd;
  192. }
  193. dev = strrchr(tpm_pt->tpm_dev, '/');
  194. if (!dev) {
  195. error_report("tpm_passthrough: Bad TPM device path %s",
  196. tpm_pt->tpm_dev);
  197. return -1;
  198. }
  199. dev++;
  200. if (snprintf(path, sizeof(path), "/sys/class/tpm/%s/device/cancel",
  201. dev) < sizeof(path)) {
  202. fd = qemu_open(path, O_WRONLY);
  203. if (fd < 0) {
  204. if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
  205. dev) < sizeof(path)) {
  206. fd = qemu_open(path, O_WRONLY);
  207. }
  208. }
  209. }
  210. if (fd < 0) {
  211. error_report("tpm_passthrough: Could not guess TPM cancel path");
  212. } else {
  213. tpm_pt->options->cancel_path = g_strdup(path);
  214. }
  215. return fd;
  216. }
  217. static int
  218. tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
  219. {
  220. const char *value;
  221. value = qemu_opt_get(opts, "cancel-path");
  222. if (value) {
  223. tpm_pt->options->cancel_path = g_strdup(value);
  224. tpm_pt->options->has_cancel_path = true;
  225. }
  226. value = qemu_opt_get(opts, "path");
  227. if (value) {
  228. tpm_pt->options->has_path = true;
  229. tpm_pt->options->path = g_strdup(value);
  230. }
  231. tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
  232. tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
  233. if (tpm_pt->tpm_fd < 0) {
  234. error_report("Cannot access TPM device using '%s': %s",
  235. tpm_pt->tpm_dev, strerror(errno));
  236. return -1;
  237. }
  238. if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
  239. error_report("'%s' is not a TPM device.",
  240. tpm_pt->tpm_dev);
  241. return -1;
  242. }
  243. tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
  244. if (tpm_pt->cancel_fd < 0) {
  245. return -1;
  246. }
  247. return 0;
  248. }
  249. static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
  250. {
  251. Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
  252. if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
  253. object_unref(obj);
  254. return NULL;
  255. }
  256. return TPM_BACKEND(obj);
  257. }
  258. static int tpm_passthrough_startup_tpm(TPMBackend *tb, size_t buffersize)
  259. {
  260. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
  261. if (buffersize && buffersize < tpm_pt->tpm_buffersize) {
  262. error_report("Requested buffer size of %zu is smaller than host TPM's "
  263. "fixed buffer size of %zu",
  264. buffersize, tpm_pt->tpm_buffersize);
  265. return -1;
  266. }
  267. return 0;
  268. }
  269. static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
  270. {
  271. TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
  272. options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
  273. options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
  274. TPM_PASSTHROUGH(tb)->options);
  275. return options;
  276. }
  277. static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
  278. TPM_STANDARD_CMDLINE_OPTS,
  279. {
  280. .name = "cancel-path",
  281. .type = QEMU_OPT_STRING,
  282. .help = "Sysfs file entry for canceling TPM commands",
  283. },
  284. {
  285. .name = "path",
  286. .type = QEMU_OPT_STRING,
  287. .help = "Path to TPM device on the host",
  288. },
  289. { /* end of list */ },
  290. };
  291. static void tpm_passthrough_inst_init(Object *obj)
  292. {
  293. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
  294. tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
  295. tpm_pt->tpm_fd = -1;
  296. tpm_pt->cancel_fd = -1;
  297. }
  298. static void tpm_passthrough_inst_finalize(Object *obj)
  299. {
  300. TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
  301. tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
  302. if (tpm_pt->tpm_fd >= 0) {
  303. qemu_close(tpm_pt->tpm_fd);
  304. }
  305. if (tpm_pt->cancel_fd >= 0) {
  306. qemu_close(tpm_pt->cancel_fd);
  307. }
  308. qapi_free_TPMPassthroughOptions(tpm_pt->options);
  309. }
  310. static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
  311. {
  312. TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
  313. tbc->type = TPM_TYPE_PASSTHROUGH;
  314. tbc->opts = tpm_passthrough_cmdline_opts;
  315. tbc->desc = "Passthrough TPM backend driver";
  316. tbc->create = tpm_passthrough_create;
  317. tbc->startup_tpm = tpm_passthrough_startup_tpm;
  318. tbc->reset = tpm_passthrough_reset;
  319. tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
  320. tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
  321. tbc->reset_tpm_established_flag =
  322. tpm_passthrough_reset_tpm_established_flag;
  323. tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
  324. tbc->get_buffer_size = tpm_passthrough_get_buffer_size;
  325. tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
  326. tbc->handle_request = tpm_passthrough_handle_request;
  327. }
  328. static const TypeInfo tpm_passthrough_info = {
  329. .name = TYPE_TPM_PASSTHROUGH,
  330. .parent = TYPE_TPM_BACKEND,
  331. .instance_size = sizeof(TPMPassthruState),
  332. .class_init = tpm_passthrough_class_init,
  333. .instance_init = tpm_passthrough_inst_init,
  334. .instance_finalize = tpm_passthrough_inst_finalize,
  335. };
  336. static void tpm_passthrough_register(void)
  337. {
  338. type_register_static(&tpm_passthrough_info);
  339. }
  340. type_init(tpm_passthrough_register)