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