tpm-backend: Move realloc_buffer() implementation to tpm-tis model
[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 void tpm_passthrough_cancel_cmd(TPMBackend *tb)
251 {
252 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
253 int n;
254
255 /*
256 * As of Linux 3.7 the tpm_tis driver does not properly cancel
257 * commands on all TPM manufacturers' TPMs.
258 * Only cancel if we're busy so we don't cancel someone else's
259 * command, e.g., a command executed on the host.
260 */
261 if (tpm_pt->tpm_executing) {
262 if (tpm_pt->cancel_fd >= 0) {
263 n = write(tpm_pt->cancel_fd, "-", 1);
264 if (n != 1) {
265 error_report("Canceling TPM command failed: %s",
266 strerror(errno));
267 } else {
268 tpm_pt->tpm_op_canceled = true;
269 }
270 } else {
271 error_report("Cannot cancel TPM command due to missing "
272 "TPM sysfs cancel entry");
273 }
274 }
275 }
276
277 static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
278 {
279 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
280
281 return tpm_pt->tpm_version;
282 }
283
284 /*
285 * Unless path or file descriptor set has been provided by user,
286 * determine the sysfs cancel file following kernel documentation
287 * in Documentation/ABI/stable/sysfs-class-tpm.
288 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
289 */
290 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
291 {
292 int fd = -1;
293 char *dev;
294 char path[PATH_MAX];
295
296 if (tpm_pt->options->cancel_path) {
297 fd = qemu_open(tpm_pt->options->cancel_path, O_WRONLY);
298 if (fd < 0) {
299 error_report("Could not open TPM cancel path : %s",
300 strerror(errno));
301 }
302 return fd;
303 }
304
305 dev = strrchr(tpm_pt->tpm_dev, '/');
306 if (dev) {
307 dev++;
308 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
309 dev) < sizeof(path)) {
310 fd = qemu_open(path, O_WRONLY);
311 if (fd >= 0) {
312 tpm_pt->options->cancel_path = g_strdup(path);
313 } else {
314 error_report("tpm_passthrough: Could not open TPM cancel "
315 "path %s : %s", path, strerror(errno));
316 }
317 }
318 } else {
319 error_report("tpm_passthrough: Bad TPM device path %s",
320 tpm_pt->tpm_dev);
321 }
322
323 return fd;
324 }
325
326 static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb)
327 {
328 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
329 const char *value;
330
331 value = qemu_opt_get(opts, "cancel-path");
332 if (value) {
333 tpm_pt->options->cancel_path = g_strdup(value);
334 tpm_pt->options->has_cancel_path = true;
335 }
336
337 value = qemu_opt_get(opts, "path");
338 if (value) {
339 tpm_pt->options->has_path = true;
340 tpm_pt->options->path = g_strdup(value);
341 }
342
343 tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
344 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
345 if (tpm_pt->tpm_fd < 0) {
346 error_report("Cannot access TPM device using '%s': %s",
347 tpm_pt->tpm_dev, strerror(errno));
348 goto err_free_parameters;
349 }
350
351 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
352 error_report("'%s' is not a TPM device.",
353 tpm_pt->tpm_dev);
354 goto err_close_tpmdev;
355 }
356
357 return 0;
358
359 err_close_tpmdev:
360 qemu_close(tpm_pt->tpm_fd);
361 tpm_pt->tpm_fd = -1;
362
363 err_free_parameters:
364 qapi_free_TPMPassthroughOptions(tpm_pt->options);
365 tpm_pt->options = NULL;
366 tpm_pt->tpm_dev = NULL;
367
368 return 1;
369 }
370
371 static TPMBackend *tpm_passthrough_create(QemuOpts *opts, const char *id)
372 {
373 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
374 TPMBackend *tb = TPM_BACKEND(obj);
375 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
376
377 tb->id = g_strdup(id);
378
379 if (tpm_passthrough_handle_device_opts(opts, tb)) {
380 goto err_exit;
381 }
382
383 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
384 if (tpm_pt->cancel_fd < 0) {
385 goto err_exit;
386 }
387
388 return tb;
389
390 err_exit:
391 object_unref(obj);
392
393 return NULL;
394 }
395
396 static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
397 {
398 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
399
400 options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
401 options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
402 TPM_PASSTHROUGH(tb)->options);
403
404 return options;
405 }
406
407 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
408 TPM_STANDARD_CMDLINE_OPTS,
409 {
410 .name = "cancel-path",
411 .type = QEMU_OPT_STRING,
412 .help = "Sysfs file entry for canceling TPM commands",
413 },
414 {
415 .name = "path",
416 .type = QEMU_OPT_STRING,
417 .help = "Path to TPM device on the host",
418 },
419 { /* end of list */ },
420 };
421
422 static const TPMDriverOps tpm_passthrough_driver = {
423 .type = TPM_TYPE_PASSTHROUGH,
424 .opts = tpm_passthrough_cmdline_opts,
425 .desc = "Passthrough TPM backend driver",
426 .create = tpm_passthrough_create,
427 .reset = tpm_passthrough_reset,
428 .cancel_cmd = tpm_passthrough_cancel_cmd,
429 .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag,
430 .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag,
431 .get_tpm_version = tpm_passthrough_get_tpm_version,
432 .get_tpm_options = tpm_passthrough_get_tpm_options,
433 };
434
435 static void tpm_passthrough_inst_init(Object *obj)
436 {
437 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
438
439 tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
440 tpm_pt->tpm_fd = -1;
441 tpm_pt->cancel_fd = -1;
442 }
443
444 static void tpm_passthrough_inst_finalize(Object *obj)
445 {
446 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
447
448 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
449
450 qemu_close(tpm_pt->tpm_fd);
451 qemu_close(tpm_pt->cancel_fd);
452 qapi_free_TPMPassthroughOptions(tpm_pt->options);
453 }
454
455 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
456 {
457 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
458
459 tbc->ops = &tpm_passthrough_driver;
460 tbc->handle_request = tpm_passthrough_handle_request;
461 }
462
463 static const TypeInfo tpm_passthrough_info = {
464 .name = TYPE_TPM_PASSTHROUGH,
465 .parent = TYPE_TPM_BACKEND,
466 .instance_size = sizeof(TPMPassthruState),
467 .class_init = tpm_passthrough_class_init,
468 .instance_init = tpm_passthrough_inst_init,
469 .instance_finalize = tpm_passthrough_inst_finalize,
470 };
471
472 static void tpm_passthrough_register(void)
473 {
474 type_register_static(&tpm_passthrough_info);
475 tpm_register_driver(&tpm_passthrough_driver);
476 }
477
478 type_init(tpm_passthrough_register)