]> git.proxmox.com Git - mirror_qemu.git/blob - hw/tpm/tpm_passthrough.c
Merge remote-tracking branch 'remotes/kevin/tags/for-upstream' into staging
[mirror_qemu.git] / hw / tpm / tpm_passthrough.c
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
25 #include <dirent.h>
26
27 #include "qemu-common.h"
28 #include "qapi/error.h"
29 #include "qemu/sockets.h"
30 #include "sysemu/tpm_backend.h"
31 #include "tpm_int.h"
32 #include "hw/hw.h"
33 #include "hw/i386/pc.h"
34 #include "sysemu/tpm_backend_int.h"
35 #include "tpm_tis.h"
36
37 #define DEBUG_TPM 0
38
39 #define DPRINTF(fmt, ...) do { \
40 if (DEBUG_TPM) { \
41 fprintf(stderr, fmt, ## __VA_ARGS__); \
42 } \
43 } while (0);
44
45 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
46 #define TPM_PASSTHROUGH(obj) \
47 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
48
49 static const TPMDriverOps tpm_passthrough_driver;
50
51 /* data structures */
52 typedef struct TPMPassthruThreadParams {
53 TPMState *tpm_state;
54
55 TPMRecvDataCB *recv_data_callback;
56 TPMBackend *tb;
57 } TPMPassthruThreadParams;
58
59 struct TPMPassthruState {
60 TPMBackend parent;
61
62 TPMBackendThread tbt;
63
64 TPMPassthruThreadParams tpm_thread_params;
65
66 char *tpm_dev;
67 int tpm_fd;
68 bool tpm_executing;
69 bool tpm_op_canceled;
70 int cancel_fd;
71 bool had_startup_error;
72 };
73
74 typedef struct TPMPassthruState TPMPassthruState;
75
76 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
77
78 /* functions */
79
80 static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
81
82 static int tpm_passthrough_unix_write(int fd, const uint8_t *buf, uint32_t len)
83 {
84 return send_all(fd, buf, len);
85 }
86
87 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
88 {
89 return recv_all(fd, buf, len, true);
90 }
91
92 static uint32_t tpm_passthrough_get_size_from_buffer(const uint8_t *buf)
93 {
94 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)buf;
95
96 return be32_to_cpu(resp->len);
97 }
98
99 /*
100 * Write an error message in the given output buffer.
101 */
102 static void tpm_write_fatal_error_response(uint8_t *out, uint32_t out_len)
103 {
104 if (out_len >= sizeof(struct tpm_resp_hdr)) {
105 struct tpm_resp_hdr *resp = (struct tpm_resp_hdr *)out;
106
107 resp->tag = cpu_to_be16(TPM_TAG_RSP_COMMAND);
108 resp->len = cpu_to_be32(sizeof(struct tpm_resp_hdr));
109 resp->errcode = cpu_to_be32(TPM_FAIL);
110 }
111 }
112
113 static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len)
114 {
115 struct tpm_req_hdr *hdr = (struct tpm_req_hdr *)in;
116
117 if (in_len >= sizeof(*hdr)) {
118 return (be32_to_cpu(hdr->ordinal) == TPM_ORD_ContinueSelfTest);
119 }
120
121 return false;
122 }
123
124 static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
125 const uint8_t *in, uint32_t in_len,
126 uint8_t *out, uint32_t out_len,
127 bool *selftest_done)
128 {
129 int ret;
130 bool is_selftest;
131 const struct tpm_resp_hdr *hdr;
132
133 tpm_pt->tpm_op_canceled = false;
134 tpm_pt->tpm_executing = true;
135 *selftest_done = false;
136
137 is_selftest = tpm_passthrough_is_selftest(in, in_len);
138
139 ret = tpm_passthrough_unix_write(tpm_pt->tpm_fd, in, in_len);
140 if (ret != in_len) {
141 if (!tpm_pt->tpm_op_canceled ||
142 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
143 error_report("tpm_passthrough: error while transmitting data "
144 "to TPM: %s (%i)",
145 strerror(errno), errno);
146 }
147 goto err_exit;
148 }
149
150 tpm_pt->tpm_executing = false;
151
152 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
153 if (ret < 0) {
154 if (!tpm_pt->tpm_op_canceled ||
155 (tpm_pt->tpm_op_canceled && errno != ECANCELED)) {
156 error_report("tpm_passthrough: error while reading data from "
157 "TPM: %s (%i)",
158 strerror(errno), errno);
159 }
160 } else if (ret < sizeof(struct tpm_resp_hdr) ||
161 tpm_passthrough_get_size_from_buffer(out) != ret) {
162 ret = -1;
163 error_report("tpm_passthrough: received invalid response "
164 "packet from TPM");
165 }
166
167 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
168 hdr = (struct tpm_resp_hdr *)out;
169 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
170 }
171
172 err_exit:
173 if (ret < 0) {
174 tpm_write_fatal_error_response(out, out_len);
175 }
176
177 tpm_pt->tpm_executing = false;
178
179 return ret;
180 }
181
182 static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt,
183 const TPMLocality *locty_data,
184 bool *selftest_done)
185 {
186 return tpm_passthrough_unix_tx_bufs(tpm_pt,
187 locty_data->w_buffer.buffer,
188 locty_data->w_offset,
189 locty_data->r_buffer.buffer,
190 locty_data->r_buffer.size,
191 selftest_done);
192 }
193
194 static void tpm_passthrough_worker_thread(gpointer data,
195 gpointer user_data)
196 {
197 TPMPassthruThreadParams *thr_parms = user_data;
198 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(thr_parms->tb);
199 TPMBackendCmd cmd = (TPMBackendCmd)data;
200 bool selftest_done = false;
201
202 DPRINTF("tpm_passthrough: processing command type %d\n", cmd);
203
204 switch (cmd) {
205 case TPM_BACKEND_CMD_PROCESS_CMD:
206 tpm_passthrough_unix_transfer(tpm_pt,
207 thr_parms->tpm_state->locty_data,
208 &selftest_done);
209
210 thr_parms->recv_data_callback(thr_parms->tpm_state,
211 thr_parms->tpm_state->locty_number,
212 selftest_done);
213 break;
214 case TPM_BACKEND_CMD_INIT:
215 case TPM_BACKEND_CMD_END:
216 case TPM_BACKEND_CMD_TPM_RESET:
217 /* nothing to do */
218 break;
219 }
220 }
221
222 /*
223 * Start the TPM (thread). If it had been started before, then terminate
224 * and start it again.
225 */
226 static int tpm_passthrough_startup_tpm(TPMBackend *tb)
227 {
228 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
229
230 /* terminate a running TPM */
231 tpm_backend_thread_end(&tpm_pt->tbt);
232
233 tpm_backend_thread_create(&tpm_pt->tbt,
234 tpm_passthrough_worker_thread,
235 &tpm_pt->tpm_thread_params);
236
237 return 0;
238 }
239
240 static void tpm_passthrough_reset(TPMBackend *tb)
241 {
242 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
243
244 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
245
246 tpm_passthrough_cancel_cmd(tb);
247
248 tpm_backend_thread_end(&tpm_pt->tbt);
249
250 tpm_pt->had_startup_error = false;
251 }
252
253 static int tpm_passthrough_init(TPMBackend *tb, TPMState *s,
254 TPMRecvDataCB *recv_data_cb)
255 {
256 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
257
258 tpm_pt->tpm_thread_params.tpm_state = s;
259 tpm_pt->tpm_thread_params.recv_data_callback = recv_data_cb;
260 tpm_pt->tpm_thread_params.tb = tb;
261
262 return 0;
263 }
264
265 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
266 {
267 return false;
268 }
269
270 static bool tpm_passthrough_get_startup_error(TPMBackend *tb)
271 {
272 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
273
274 return tpm_pt->had_startup_error;
275 }
276
277 static size_t tpm_passthrough_realloc_buffer(TPMSizedBuffer *sb)
278 {
279 size_t wanted_size = 4096; /* Linux tpm.c buffer size */
280
281 if (sb->size != wanted_size) {
282 sb->buffer = g_realloc(sb->buffer, wanted_size);
283 sb->size = wanted_size;
284 }
285 return sb->size;
286 }
287
288 static void tpm_passthrough_deliver_request(TPMBackend *tb)
289 {
290 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
291
292 tpm_backend_thread_deliver_request(&tpm_pt->tbt);
293 }
294
295 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
296 {
297 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
298 int n;
299
300 /*
301 * As of Linux 3.7 the tpm_tis driver does not properly cancel
302 * commands on all TPM manufacturers' TPMs.
303 * Only cancel if we're busy so we don't cancel someone else's
304 * command, e.g., a command executed on the host.
305 */
306 if (tpm_pt->tpm_executing) {
307 if (tpm_pt->cancel_fd >= 0) {
308 n = write(tpm_pt->cancel_fd, "-", 1);
309 if (n != 1) {
310 error_report("Canceling TPM command failed: %s",
311 strerror(errno));
312 } else {
313 tpm_pt->tpm_op_canceled = true;
314 }
315 } else {
316 error_report("Cannot cancel TPM command due to missing "
317 "TPM sysfs cancel entry");
318 }
319 }
320 }
321
322 static const char *tpm_passthrough_create_desc(void)
323 {
324 return "Passthrough TPM backend driver";
325 }
326
327 /*
328 * A basic test of a TPM device. We expect a well formatted response header
329 * (error response is fine) within one second.
330 */
331 static int tpm_passthrough_test_tpmdev(int fd)
332 {
333 struct tpm_req_hdr req = {
334 .tag = cpu_to_be16(TPM_TAG_RQU_COMMAND),
335 .len = cpu_to_be32(sizeof(req)),
336 .ordinal = cpu_to_be32(TPM_ORD_GetTicks),
337 };
338 struct tpm_resp_hdr *resp;
339 fd_set readfds;
340 int n;
341 struct timeval tv = {
342 .tv_sec = 1,
343 .tv_usec = 0,
344 };
345 unsigned char buf[1024];
346
347 n = write(fd, &req, sizeof(req));
348 if (n < 0) {
349 return errno;
350 }
351 if (n != sizeof(req)) {
352 return EFAULT;
353 }
354
355 FD_ZERO(&readfds);
356 FD_SET(fd, &readfds);
357
358 /* wait for a second */
359 n = select(fd + 1, &readfds, NULL, NULL, &tv);
360 if (n != 1) {
361 return errno;
362 }
363
364 n = read(fd, &buf, sizeof(buf));
365 if (n < sizeof(struct tpm_resp_hdr)) {
366 return EFAULT;
367 }
368
369 resp = (struct tpm_resp_hdr *)buf;
370 /* check the header */
371 if (be16_to_cpu(resp->tag) != TPM_TAG_RSP_COMMAND ||
372 be32_to_cpu(resp->len) != n) {
373 return EBADMSG;
374 }
375
376 return 0;
377 }
378
379 /*
380 * Unless path or file descriptor set has been provided by user,
381 * determine the sysfs cancel file following kernel documentation
382 * in Documentation/ABI/stable/sysfs-class-tpm.
383 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
384 */
385 static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb)
386 {
387 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
388 int fd = -1;
389 char *dev;
390 char path[PATH_MAX];
391
392 if (tb->cancel_path) {
393 fd = qemu_open(tb->cancel_path, O_WRONLY);
394 if (fd < 0) {
395 error_report("Could not open TPM cancel path : %s",
396 strerror(errno));
397 }
398 return fd;
399 }
400
401 dev = strrchr(tpm_pt->tpm_dev, '/');
402 if (dev) {
403 dev++;
404 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
405 dev) < sizeof(path)) {
406 fd = qemu_open(path, O_WRONLY);
407 if (fd >= 0) {
408 tb->cancel_path = g_strdup(path);
409 } else {
410 error_report("tpm_passthrough: Could not open TPM cancel "
411 "path %s : %s", path, strerror(errno));
412 }
413 }
414 } else {
415 error_report("tpm_passthrough: Bad TPM device path %s",
416 tpm_pt->tpm_dev);
417 }
418
419 return fd;
420 }
421
422 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
423 {
424 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
425 const char *value;
426
427 value = qemu_opt_get(opts, "cancel-path");
428 tb->cancel_path = g_strdup(value);
429
430 value = qemu_opt_get(opts, "path");
431 if (!value) {
432 value = TPM_PASSTHROUGH_DEFAULT_DEVICE;
433 }
434
435 tpm_pt->tpm_dev = g_strdup(value);
436
437 tb->path = g_strdup(tpm_pt->tpm_dev);
438
439 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
440 if (tpm_pt->tpm_fd < 0) {
441 error_report("Cannot access TPM device using '%s': %s",
442 tpm_pt->tpm_dev, strerror(errno));
443 goto err_free_parameters;
444 }
445
446 if (tpm_passthrough_test_tpmdev(tpm_pt->tpm_fd)) {
447 error_report("'%s' is not a TPM device.",
448 tpm_pt->tpm_dev);
449 goto err_close_tpmdev;
450 }
451
452 return 0;
453
454 err_close_tpmdev:
455 qemu_close(tpm_pt->tpm_fd);
456 tpm_pt->tpm_fd = -1;
457
458 err_free_parameters:
459 g_free(tb->path);
460 tb->path = NULL;
461
462 g_free(tpm_pt->tpm_dev);
463 tpm_pt->tpm_dev = NULL;
464
465 return 1;
466 }
467
468 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
469 {
470 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
471 TPMBackend *tb = TPM_BACKEND(obj);
472 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
473
474 tb->id = g_strdup(id);
475 /* let frontend set the fe_model to proper value */
476 tb->fe_model = -1;
477
478 tb->ops = &tpm_passthrough_driver;
479
480 if (tpm_passthrough_handle_device_opts(opts, tb)) {
481 goto err_exit;
482 }
483
484 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tb);
485 if (tpm_pt->cancel_fd < 0) {
486 goto err_exit;
487 }
488
489 return tb;
490
491 err_exit:
492 g_free(tb->id);
493
494 return NULL;
495 }
496
497 static void tpm_passthrough_destroy(TPMBackend *tb)
498 {
499 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
500
501 tpm_passthrough_cancel_cmd(tb);
502
503 tpm_backend_thread_end(&tpm_pt->tbt);
504
505 qemu_close(tpm_pt->tpm_fd);
506 qemu_close(tpm_pt->cancel_fd);
507
508 g_free(tb->id);
509 g_free(tb->path);
510 g_free(tb->cancel_path);
511 g_free(tpm_pt->tpm_dev);
512 }
513
514 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
515 TPM_STANDARD_CMDLINE_OPTS,
516 {
517 .name = "cancel-path",
518 .type = QEMU_OPT_STRING,
519 .help = "Sysfs file entry for canceling TPM commands",
520 },
521 {
522 .name = "path",
523 .type = QEMU_OPT_STRING,
524 .help = "Path to TPM device on the host",
525 },
526 { /* end of list */ },
527 };
528
529 static const TPMDriverOps tpm_passthrough_driver = {
530 .type = TPM_TYPE_PASSTHROUGH,
531 .opts = tpm_passthrough_cmdline_opts,
532 .desc = tpm_passthrough_create_desc,
533 .create = tpm_passthrough_create,
534 .destroy = tpm_passthrough_destroy,
535 .init = tpm_passthrough_init,
536 .startup_tpm = tpm_passthrough_startup_tpm,
537 .realloc_buffer = tpm_passthrough_realloc_buffer,
538 .reset = tpm_passthrough_reset,
539 .had_startup_error = tpm_passthrough_get_startup_error,
540 .deliver_request = tpm_passthrough_deliver_request,
541 .cancel_cmd = tpm_passthrough_cancel_cmd,
542 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
543 };
544
545 static void tpm_passthrough_inst_init(Object *obj)
546 {
547 }
548
549 static void tpm_passthrough_inst_finalize(Object *obj)
550 {
551 }
552
553 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
554 {
555 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
556
557 tbc->ops = &tpm_passthrough_driver;
558 }
559
560 static const TypeInfo tpm_passthrough_info = {
561 .name = TYPE_TPM_PASSTHROUGH,
562 .parent = TYPE_TPM_BACKEND,
563 .instance_size = sizeof(TPMPassthruState),
564 .class_init = tpm_passthrough_class_init,
565 .instance_init = tpm_passthrough_inst_init,
566 .instance_finalize = tpm_passthrough_inst_finalize,
567 };
568
569 static void tpm_passthrough_register(void)
570 {
571 type_register_static(&tpm_passthrough_info);
572 tpm_register_driver(&tpm_passthrough_driver);
573 }
574
575 type_init(tpm_passthrough_register)