tpm_passthrough.c 11 KB

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