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