]> git.proxmox.com Git - mirror_qemu.git/blob - hw/tpm/tpm_emulator.c
35c78de5a9f801e6e3886fa5379e652b1666a4eb
[mirror_qemu.git] / hw / tpm / tpm_emulator.c
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 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 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
29 #include "qemu/osdep.h"
30 #include "qemu/error-report.h"
31 #include "qemu/sockets.h"
32 #include "io/channel-socket.h"
33 #include "sysemu/tpm_backend.h"
34 #include "tpm_int.h"
35 #include "hw/hw.h"
36 #include "tpm_util.h"
37 #include "tpm_ioctl.h"
38 #include "migration/blocker.h"
39 #include "qapi/error.h"
40 #include "qapi/clone-visitor.h"
41 #include "chardev/char-fe.h"
42
43 #include <fcntl.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <stdio.h>
47
48 #define DEBUG_TPM 0
49
50 #define DPRINTF(fmt, ...) do { \
51 if (DEBUG_TPM) { \
52 fprintf(stderr, "tpm-emulator:"fmt"\n", ## __VA_ARGS__); \
53 } \
54 } while (0)
55
56 #define TYPE_TPM_EMULATOR "tpm-emulator"
57 #define TPM_EMULATOR(obj) \
58 OBJECT_CHECK(TPMEmulator, (obj), TYPE_TPM_EMULATOR)
59
60 #define TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(S, cap) (((S)->caps & (cap)) == (cap))
61
62 /* data structures */
63 typedef struct TPMEmulator {
64 TPMBackend parent;
65
66 TPMEmulatorOptions *options;
67 CharBackend ctrl_chr;
68 QIOChannel *data_ioc;
69 TPMVersion tpm_version;
70 ptm_cap caps; /* capabilities of the TPM */
71 uint8_t cur_locty_number; /* last set locality */
72 Error *migration_blocker;
73
74 QemuMutex mutex;
75
76 unsigned int established_flag:1;
77 unsigned int established_flag_cached:1;
78 } TPMEmulator;
79
80
81 static int tpm_emulator_ctrlcmd(TPMEmulator *tpm, unsigned long cmd, void *msg,
82 size_t msg_len_in, size_t msg_len_out)
83 {
84 CharBackend *dev = &tpm->ctrl_chr;
85 uint32_t cmd_no = cpu_to_be32(cmd);
86 ssize_t n = sizeof(uint32_t) + msg_len_in;
87 uint8_t *buf = NULL;
88 int ret = -1;
89
90 qemu_mutex_lock(&tpm->mutex);
91
92 buf = g_alloca(n);
93 memcpy(buf, &cmd_no, sizeof(cmd_no));
94 memcpy(buf + sizeof(cmd_no), msg, msg_len_in);
95
96 n = qemu_chr_fe_write_all(dev, buf, n);
97 if (n <= 0) {
98 goto end;
99 }
100
101 if (msg_len_out != 0) {
102 n = qemu_chr_fe_read_all(dev, msg, msg_len_out);
103 if (n <= 0) {
104 goto end;
105 }
106 }
107
108 ret = 0;
109
110 end:
111 qemu_mutex_unlock(&tpm->mutex);
112 return ret;
113 }
114
115 static int tpm_emulator_unix_tx_bufs(TPMEmulator *tpm_emu,
116 const uint8_t *in, uint32_t in_len,
117 uint8_t *out, uint32_t out_len,
118 bool *selftest_done,
119 Error **err)
120 {
121 ssize_t ret;
122 bool is_selftest = false;
123 const struct tpm_resp_hdr *hdr = NULL;
124
125 if (selftest_done) {
126 *selftest_done = false;
127 is_selftest = tpm_util_is_selftest(in, in_len);
128 }
129
130 ret = qio_channel_write_all(tpm_emu->data_ioc, (char *)in, in_len, err);
131 if (ret != 0) {
132 return -1;
133 }
134
135 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out, sizeof(*hdr),
136 err);
137 if (ret != 0) {
138 return -1;
139 }
140
141 hdr = (struct tpm_resp_hdr *)out;
142 out += sizeof(*hdr);
143 ret = qio_channel_read_all(tpm_emu->data_ioc, (char *)out,
144 be32_to_cpu(hdr->len) - sizeof(*hdr) , err);
145 if (ret != 0) {
146 return -1;
147 }
148
149 if (is_selftest) {
150 *selftest_done = (be32_to_cpu(hdr->errcode) == 0);
151 }
152
153 return 0;
154 }
155
156 static int tpm_emulator_set_locality(TPMEmulator *tpm_emu, uint8_t locty_number,
157 Error **errp)
158 {
159 ptm_loc loc;
160
161 DPRINTF("%s : locality: 0x%x", __func__, locty_number);
162
163 if (tpm_emu->cur_locty_number == locty_number) {
164 return 0;
165 }
166
167 DPRINTF("setting locality : 0x%x", locty_number);
168 loc.u.req.loc = locty_number;
169 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_LOCALITY, &loc,
170 sizeof(loc), sizeof(loc)) < 0) {
171 error_setg(errp, "tpm-emulator: could not set locality : %s",
172 strerror(errno));
173 return -1;
174 }
175
176 loc.u.resp.tpm_result = be32_to_cpu(loc.u.resp.tpm_result);
177 if (loc.u.resp.tpm_result != 0) {
178 error_setg(errp, "tpm-emulator: TPM result for set locality : 0x%x",
179 loc.u.resp.tpm_result);
180 return -1;
181 }
182
183 tpm_emu->cur_locty_number = locty_number;
184
185 return 0;
186 }
187
188 static void tpm_emulator_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
189 {
190 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
191 Error *err = NULL;
192
193 DPRINTF("processing TPM command");
194
195 if (tpm_emulator_set_locality(tpm_emu, cmd->locty, &err) < 0) {
196 goto error;
197 }
198
199 if (tpm_emulator_unix_tx_bufs(tpm_emu, cmd->in, cmd->in_len,
200 cmd->out, cmd->out_len,
201 &cmd->selftest_done, &err) < 0) {
202 goto error;
203 }
204
205 return;
206
207 error:
208 tpm_util_write_fatal_error_response(cmd->out, cmd->out_len);
209 error_report_err(err);
210 }
211
212 static int tpm_emulator_probe_caps(TPMEmulator *tpm_emu)
213 {
214 DPRINTF("%s", __func__);
215 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_CAPABILITY,
216 &tpm_emu->caps, 0, sizeof(tpm_emu->caps)) < 0) {
217 error_report("tpm-emulator: probing failed : %s", strerror(errno));
218 return -1;
219 }
220
221 tpm_emu->caps = be64_to_cpu(tpm_emu->caps);
222
223 DPRINTF("capabilities : 0x%"PRIx64, tpm_emu->caps);
224
225 return 0;
226 }
227
228 static int tpm_emulator_check_caps(TPMEmulator *tpm_emu)
229 {
230 ptm_cap caps = 0;
231 const char *tpm = NULL;
232
233 /* check for min. required capabilities */
234 switch (tpm_emu->tpm_version) {
235 case TPM_VERSION_1_2:
236 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
237 PTM_CAP_SET_LOCALITY | PTM_CAP_SET_DATAFD | PTM_CAP_STOP |
238 PTM_CAP_SET_BUFFERSIZE;
239 tpm = "1.2";
240 break;
241 case TPM_VERSION_2_0:
242 caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED |
243 PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED |
244 PTM_CAP_SET_DATAFD | PTM_CAP_STOP | PTM_CAP_SET_BUFFERSIZE;
245 tpm = "2";
246 break;
247 case TPM_VERSION_UNSPEC:
248 error_report("tpm-emulator: TPM version has not been set");
249 return -1;
250 }
251
252 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, caps)) {
253 error_report("tpm-emulator: TPM does not implement minimum set of "
254 "required capabilities for TPM %s (0x%x)", tpm, (int)caps);
255 return -1;
256 }
257
258 return 0;
259 }
260
261 static int tpm_emulator_stop_tpm(TPMBackend *tb)
262 {
263 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
264 ptm_res res;
265
266 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_STOP, &res, 0, sizeof(res)) < 0) {
267 error_report("tpm-emulator: Could not stop TPM: %s",
268 strerror(errno));
269 return -1;
270 }
271
272 res = be32_to_cpu(res);
273 if (res) {
274 error_report("tpm-emulator: TPM result for CMD_STOP: 0x%x", res);
275 return -1;
276 }
277
278 return 0;
279 }
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
288 if (tpm_emulator_stop_tpm(tb) < 0) {
289 return -1;
290 }
291
292 psbs.u.req.buffersize = cpu_to_be32(wanted_size);
293
294 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_BUFFERSIZE, &psbs,
295 sizeof(psbs.u.req), sizeof(psbs.u.resp)) < 0) {
296 error_report("tpm-emulator: Could not set buffer size: %s",
297 strerror(errno));
298 return -1;
299 }
300
301 psbs.u.resp.tpm_result = be32_to_cpu(psbs.u.resp.tpm_result);
302 if (psbs.u.resp.tpm_result != 0) {
303 error_report("tpm-emulator: TPM result for set buffer size : 0x%x",
304 psbs.u.resp.tpm_result);
305 return -1;
306 }
307
308 if (actual_size) {
309 *actual_size = be32_to_cpu(psbs.u.resp.buffersize);
310 }
311
312 DPRINTF("buffer size: %u, min: %u, max: %u\n",
313 be32_to_cpu(psbs.u.resp.buffersize),
314 be32_to_cpu(psbs.u.resp.minsize),
315 be32_to_cpu(psbs.u.resp.maxsize));
316
317 return 0;
318 }
319
320 static int tpm_emulator_startup_tpm(TPMBackend *tb, size_t buffersize)
321 {
322 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
323 ptm_init init;
324 ptm_res res;
325
326 if (buffersize != 0 &&
327 tpm_emulator_set_buffer_size(tb, buffersize, NULL) < 0) {
328 goto err_exit;
329 }
330
331 DPRINTF("%s", __func__);
332 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_INIT, &init, sizeof(init),
333 sizeof(init)) < 0) {
334 error_report("tpm-emulator: could not send INIT: %s",
335 strerror(errno));
336 goto err_exit;
337 }
338
339 res = be32_to_cpu(init.u.resp.tpm_result);
340 if (res) {
341 error_report("tpm-emulator: TPM result for CMD_INIT: 0x%x", res);
342 goto err_exit;
343 }
344 return 0;
345
346 err_exit:
347 return -1;
348 }
349
350 static bool tpm_emulator_get_tpm_established_flag(TPMBackend *tb)
351 {
352 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
353 ptm_est est;
354
355 if (tpm_emu->established_flag_cached) {
356 return tpm_emu->established_flag;
357 }
358
359 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_GET_TPMESTABLISHED, &est,
360 0, sizeof(est)) < 0) {
361 error_report("tpm-emulator: Could not get the TPM established flag: %s",
362 strerror(errno));
363 return false;
364 }
365 DPRINTF("got established flag: %0x", est.u.resp.bit);
366
367 tpm_emu->established_flag_cached = 1;
368 tpm_emu->established_flag = (est.u.resp.bit != 0);
369
370 return tpm_emu->established_flag;
371 }
372
373 static int tpm_emulator_reset_tpm_established_flag(TPMBackend *tb,
374 uint8_t locty)
375 {
376 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
377 ptm_reset_est reset_est;
378 ptm_res res;
379
380 /* only a TPM 2.0 will support this */
381 if (tpm_emu->tpm_version != TPM_VERSION_2_0) {
382 return 0;
383 }
384
385 reset_est.u.req.loc = tpm_emu->cur_locty_number;
386 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_RESET_TPMESTABLISHED,
387 &reset_est, sizeof(reset_est),
388 sizeof(reset_est)) < 0) {
389 error_report("tpm-emulator: Could not reset the establishment bit: %s",
390 strerror(errno));
391 return -1;
392 }
393
394 res = be32_to_cpu(reset_est.u.resp.tpm_result);
395 if (res) {
396 error_report("tpm-emulator: TPM result for rest establixhed flag: 0x%x",
397 res);
398 return -1;
399 }
400
401 tpm_emu->established_flag_cached = 0;
402
403 return 0;
404 }
405
406 static void tpm_emulator_cancel_cmd(TPMBackend *tb)
407 {
408 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
409 ptm_res res;
410
411 if (!TPM_EMULATOR_IMPLEMENTS_ALL_CAPS(tpm_emu, PTM_CAP_CANCEL_TPM_CMD)) {
412 DPRINTF("Backend does not support CANCEL_TPM_CMD");
413 return;
414 }
415
416 /* FIXME: make the function non-blocking, or it may block a VCPU */
417 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_CANCEL_TPM_CMD, &res, 0,
418 sizeof(res)) < 0) {
419 error_report("tpm-emulator: Could not cancel command: %s",
420 strerror(errno));
421 } else if (res != 0) {
422 error_report("tpm-emulator: Failed to cancel TPM: 0x%x",
423 be32_to_cpu(res));
424 }
425 }
426
427 static TPMVersion tpm_emulator_get_tpm_version(TPMBackend *tb)
428 {
429 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
430
431 return tpm_emu->tpm_version;
432 }
433
434 static size_t tpm_emulator_get_buffer_size(TPMBackend *tb)
435 {
436 size_t actual_size;
437
438 if (tpm_emulator_set_buffer_size(tb, 0, &actual_size) < 0) {
439 return 4096;
440 }
441
442 return actual_size;
443 }
444
445 static int tpm_emulator_block_migration(TPMEmulator *tpm_emu)
446 {
447 Error *err = NULL;
448
449 error_setg(&tpm_emu->migration_blocker,
450 "Migration disabled: TPM emulator not yet migratable");
451 migrate_add_blocker(tpm_emu->migration_blocker, &err);
452 if (err) {
453 error_report_err(err);
454 error_free(tpm_emu->migration_blocker);
455 tpm_emu->migration_blocker = NULL;
456
457 return -1;
458 }
459
460 return 0;
461 }
462
463 static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
464 {
465 ptm_res res;
466 Error *err = NULL;
467 int fds[2] = { -1, -1 };
468
469 if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) < 0) {
470 error_report("tpm-emulator: Failed to create socketpair");
471 return -1;
472 }
473
474 qemu_chr_fe_set_msgfds(&tpm_emu->ctrl_chr, fds + 1, 1);
475
476 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SET_DATAFD, &res, 0,
477 sizeof(res)) < 0 || res != 0) {
478 error_report("tpm-emulator: Failed to send CMD_SET_DATAFD: %s",
479 strerror(errno));
480 goto err_exit;
481 }
482
483 tpm_emu->data_ioc = QIO_CHANNEL(qio_channel_socket_new_fd(fds[0], &err));
484 if (err) {
485 error_prepend(&err, "tpm-emulator: Failed to create io channel: ");
486 error_report_err(err);
487 goto err_exit;
488 }
489
490 closesocket(fds[1]);
491
492 return 0;
493
494 err_exit:
495 closesocket(fds[0]);
496 closesocket(fds[1]);
497 return -1;
498 }
499
500 static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
501 {
502 const char *value;
503
504 value = qemu_opt_get(opts, "chardev");
505 if (value) {
506 Error *err = NULL;
507 Chardev *dev = qemu_chr_find(value);
508
509 if (!dev) {
510 error_report("tpm-emulator: tpm chardev '%s' not found.", value);
511 goto err;
512 }
513
514 if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
515 error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
516 value);
517 error_report_err(err);
518 goto err;
519 }
520
521 tpm_emu->options->chardev = g_strdup(value);
522 }
523
524 if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
525 goto err;
526 }
527
528 /* FIXME: tpm_util_test_tpmdev() accepts only on socket fd, as it also used
529 * by passthrough driver, which not yet using GIOChannel.
530 */
531 if (tpm_util_test_tpmdev(QIO_CHANNEL_SOCKET(tpm_emu->data_ioc)->fd,
532 &tpm_emu->tpm_version)) {
533 error_report("'%s' is not emulating TPM device. Error: %s",
534 tpm_emu->options->chardev, strerror(errno));
535 goto err;
536 }
537
538 DPRINTF("TPM Version %s", tpm_emu->tpm_version == TPM_VERSION_1_2 ? "1.2" :
539 (tpm_emu->tpm_version == TPM_VERSION_2_0 ? "2.0" : "Unspecified"));
540
541 if (tpm_emulator_probe_caps(tpm_emu) ||
542 tpm_emulator_check_caps(tpm_emu)) {
543 goto err;
544 }
545
546 return tpm_emulator_block_migration(tpm_emu);
547
548 err:
549 DPRINTF("Startup error");
550 return -1;
551 }
552
553 static TPMBackend *tpm_emulator_create(QemuOpts *opts)
554 {
555 TPMBackend *tb = TPM_BACKEND(object_new(TYPE_TPM_EMULATOR));
556
557 if (tpm_emulator_handle_device_opts(TPM_EMULATOR(tb), opts)) {
558 object_unref(OBJECT(tb));
559 return NULL;
560 }
561
562 return tb;
563 }
564
565 static TpmTypeOptions *tpm_emulator_get_tpm_options(TPMBackend *tb)
566 {
567 TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
568 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
569
570 options->type = TPM_TYPE_OPTIONS_KIND_EMULATOR;
571 options->u.emulator.data = QAPI_CLONE(TPMEmulatorOptions, tpm_emu->options);
572
573 return options;
574 }
575
576 static const QemuOptDesc tpm_emulator_cmdline_opts[] = {
577 TPM_STANDARD_CMDLINE_OPTS,
578 {
579 .name = "chardev",
580 .type = QEMU_OPT_STRING,
581 .help = "Character device to use for out-of-band control messages",
582 },
583 { /* end of list */ },
584 };
585
586 static void tpm_emulator_inst_init(Object *obj)
587 {
588 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
589
590 DPRINTF("%s", __func__);
591 tpm_emu->options = g_new0(TPMEmulatorOptions, 1);
592 tpm_emu->cur_locty_number = ~0;
593 qemu_mutex_init(&tpm_emu->mutex);
594 }
595
596 /*
597 * Gracefully shut down the external TPM
598 */
599 static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
600 {
601 ptm_res res;
602
603 if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
604 error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
605 strerror(errno));
606 } else if (res != 0) {
607 error_report("tpm-emulator: TPM result for sutdown: 0x%x",
608 be32_to_cpu(res));
609 }
610 }
611
612 static void tpm_emulator_inst_finalize(Object *obj)
613 {
614 TPMEmulator *tpm_emu = TPM_EMULATOR(obj);
615
616 tpm_emulator_shutdown(tpm_emu);
617
618 object_unref(OBJECT(tpm_emu->data_ioc));
619
620 qemu_chr_fe_deinit(&tpm_emu->ctrl_chr, false);
621
622 qapi_free_TPMEmulatorOptions(tpm_emu->options);
623
624 if (tpm_emu->migration_blocker) {
625 migrate_del_blocker(tpm_emu->migration_blocker);
626 error_free(tpm_emu->migration_blocker);
627 }
628
629 qemu_mutex_destroy(&tpm_emu->mutex);
630 }
631
632 static void tpm_emulator_class_init(ObjectClass *klass, void *data)
633 {
634 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
635
636 tbc->type = TPM_TYPE_EMULATOR;
637 tbc->opts = tpm_emulator_cmdline_opts;
638 tbc->desc = "TPM emulator backend driver";
639 tbc->create = tpm_emulator_create;
640 tbc->startup_tpm = tpm_emulator_startup_tpm;
641 tbc->cancel_cmd = tpm_emulator_cancel_cmd;
642 tbc->get_tpm_established_flag = tpm_emulator_get_tpm_established_flag;
643 tbc->reset_tpm_established_flag = tpm_emulator_reset_tpm_established_flag;
644 tbc->get_tpm_version = tpm_emulator_get_tpm_version;
645 tbc->get_buffer_size = tpm_emulator_get_buffer_size;
646 tbc->get_tpm_options = tpm_emulator_get_tpm_options;
647
648 tbc->handle_request = tpm_emulator_handle_request;
649 }
650
651 static const TypeInfo tpm_emulator_info = {
652 .name = TYPE_TPM_EMULATOR,
653 .parent = TYPE_TPM_BACKEND,
654 .instance_size = sizeof(TPMEmulator),
655 .class_init = tpm_emulator_class_init,
656 .instance_init = tpm_emulator_inst_init,
657 .instance_finalize = tpm_emulator_inst_finalize,
658 };
659
660 static void tpm_emulator_register(void)
661 {
662 type_register_static(&tpm_emulator_info);
663 }
664
665 type_init(tpm_emulator_register)