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