]> git.proxmox.com Git - mirror_qemu.git/blame - hw/tpm/tpm_passthrough.c
tpm: Set the flags of the CMD_INIT command to 0
[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
0430891c 25#include "qemu/osdep.h"
4549a8b7 26#include "qemu-common.h"
d49b6836 27#include "qemu/error-report.h"
4549a8b7 28#include "qemu/sockets.h"
dccfcd0e 29#include "sysemu/tpm_backend.h"
4549a8b7
SB
30#include "tpm_int.h"
31#include "hw/hw.h"
f59864ba 32#include "qapi/clone-visitor.h"
56a3c24f 33#include "tpm_util.h"
4549a8b7 34
4d1ba9c4
SB
35#define DEBUG_TPM 0
36
37#define DPRINTF(fmt, ...) do { \
38 if (DEBUG_TPM) { \
39 fprintf(stderr, fmt, ## __VA_ARGS__); \
40 } \
2562755e 41} while (0)
4549a8b7 42
8f0605cc
SB
43#define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
44#define TPM_PASSTHROUGH(obj) \
45 OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH)
4549a8b7 46
8f0605cc 47/* data structures */
4549a8b7 48struct TPMPassthruState {
8f0605cc
SB
49 TPMBackend parent;
50
f59864ba
AV
51 TPMPassthroughOptions *options;
52 const char *tpm_dev;
4549a8b7 53 int tpm_fd;
92dcc234
SB
54 bool tpm_executing;
55 bool tpm_op_canceled;
56 int cancel_fd;
56a3c24f
SB
57
58 TPMVersion tpm_version;
abc5cda0 59 size_t tpm_buffersize;
4549a8b7
SB
60};
61
8f0605cc
SB
62typedef struct TPMPassthruState TPMPassthruState;
63
4549a8b7
SB
64#define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
65
92dcc234
SB
66/* functions */
67
68static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
69
4549a8b7
SB
70static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
71{
46f296cd
DB
72 int ret;
73 reread:
74 ret = read(fd, buf, len);
75 if (ret < 0) {
76 if (errno != EINTR && errno != EAGAIN) {
77 return -1;
78 }
79 goto reread;
80 }
81 return ret;
4549a8b7 82}
92dcc234 83static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt,
4549a8b7 84 const uint8_t *in, uint32_t in_len,
fd859081
SB
85 uint8_t *out, uint32_t out_len,
86 bool *selftest_done)
4549a8b7 87{
4a3d8098 88 ssize_t ret;
fd859081
SB
89 bool is_selftest;
90 const struct tpm_resp_hdr *hdr;
4549a8b7 91
21cb1e63 92 /* FIXME: protect shared variables or use other sync mechanism */
92dcc234
SB
93 tpm_pt->tpm_op_canceled = false;
94 tpm_pt->tpm_executing = true;
fd859081
SB
95 *selftest_done = false;
96
4a3d8098 97 is_selftest = tpm_util_is_selftest(in, in_len);
92dcc234 98
54aa36d5 99 ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
4549a8b7 100 if (ret != in_len) {
5f333d79 101 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
92dcc234 102 error_report("tpm_passthrough: error while transmitting data "
27215a22 103 "to TPM: %s (%i)",
92dcc234
SB
104 strerror(errno), errno);
105 }
4549a8b7
SB
106 goto err_exit;
107 }
108
92dcc234
SB
109 tpm_pt->tpm_executing = false;
110
111 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
4549a8b7 112 if (ret < 0) {
5f333d79 113 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
92dcc234 114 error_report("tpm_passthrough: error while reading data from "
27215a22 115 "TPM: %s (%i)",
92dcc234
SB
116 strerror(errno), errno);
117 }
4549a8b7 118 } else if (ret < sizeof(struct tpm_resp_hdr) ||
4a3d8098 119 be32_to_cpu(((struct tpm_resp_hdr *)out)->len) != ret) {
4549a8b7
SB
120 ret = -1;
121 error_report("tpm_passthrough: received invalid response "
27215a22 122 "packet from TPM");
4549a8b7
SB
123 }
124
fd859081
SB
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
4549a8b7
SB
130err_exit:
131 if (ret < 0) {
4a3d8098 132 tpm_util_write_fatal_error_response(out, out_len);
4549a8b7
SB
133 }
134
92dcc234
SB
135 tpm_pt->tpm_executing = false;
136
4549a8b7
SB
137 return ret;
138}
139
0e43b7e6 140static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd)
4549a8b7 141{
b19a5eea 142 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
4549a8b7 143
0e43b7e6 144 DPRINTF("tpm_passthrough: processing command %p\n", cmd);
905e78ba 145
0e43b7e6
MAL
146 tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
147 cmd->out, cmd->out_len, &cmd->selftest_done);
4549a8b7
SB
148}
149
4549a8b7
SB
150static void tpm_passthrough_reset(TPMBackend *tb)
151{
4549a8b7
SB
152 DPRINTF("tpm_passthrough: CALL TO TPM_RESET!\n");
153
92dcc234 154 tpm_passthrough_cancel_cmd(tb);
4549a8b7
SB
155}
156
157static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
158{
159 return false;
160}
161
116694c3
SB
162static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
163 uint8_t locty)
164{
165 /* only a TPM 2.0 will support this */
166 return 0;
167}
168
4549a8b7
SB
169static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
170{
8f0605cc 171 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
92dcc234
SB
172 int n;
173
174 /*
175 * As of Linux 3.7 the tpm_tis driver does not properly cancel
176 * commands on all TPM manufacturers' TPMs.
177 * Only cancel if we're busy so we don't cancel someone else's
178 * command, e.g., a command executed on the host.
179 */
180 if (tpm_pt->tpm_executing) {
181 if (tpm_pt->cancel_fd >= 0) {
21cb1e63 182 tpm_pt->tpm_op_canceled = true;
92dcc234
SB
183 n = write(tpm_pt->cancel_fd, "-", 1);
184 if (n != 1) {
27215a22 185 error_report("Canceling TPM command failed: %s",
92dcc234 186 strerror(errno));
92dcc234
SB
187 }
188 } else {
189 error_report("Cannot cancel TPM command due to missing "
190 "TPM sysfs cancel entry");
191 }
192 }
4549a8b7
SB
193}
194
116694c3
SB
195static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
196{
56a3c24f 197 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
4549a8b7 198
56a3c24f 199 return tpm_pt->tpm_version;
4549a8b7
SB
200}
201
b21e6aaf
SB
202static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)
203{
abc5cda0
SB
204 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
205 int ret;
206
207 ret = tpm_util_get_buffer_size(tpm_pt->tpm_fd, tpm_pt->tpm_version,
208 &tpm_pt->tpm_buffersize);
209 if (ret < 0) {
210 tpm_pt->tpm_buffersize = 4096;
211 }
212 return tpm_pt->tpm_buffersize;
b21e6aaf
SB
213}
214
92dcc234
SB
215/*
216 * Unless path or file descriptor set has been provided by user,
217 * determine the sysfs cancel file following kernel documentation
218 * in Documentation/ABI/stable/sysfs-class-tpm.
8e36d6ca 219 * From /dev/tpm0 create /sys/class/misc/tpm0/device/cancel
92dcc234 220 */
f59864ba 221static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
92dcc234
SB
222{
223 int fd = -1;
8e36d6ca 224 char *dev;
92dcc234 225 char path[PATH_MAX];
92dcc234 226
f59864ba
AV
227 if (tpm_pt->options->cancel_path) {
228 fd = qemu_open(tpm_pt->options->cancel_path, O_WRONLY);
92dcc234
SB
229 if (fd < 0) {
230 error_report("Could not open TPM cancel path : %s",
231 strerror(errno));
232 }
233 return fd;
234 }
235
8e36d6ca
SB
236 dev = strrchr(tpm_pt->tpm_dev, '/');
237 if (dev) {
238 dev++;
239 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
240 dev) < sizeof(path)) {
92dcc234 241 fd = qemu_open(path, O_WRONLY);
69c07db0 242 if (fd < 0) {
8e36d6ca
SB
243 error_report("tpm_passthrough: Could not open TPM cancel "
244 "path %s : %s", path, strerror(errno));
245 }
92dcc234 246 }
8e36d6ca
SB
247 } else {
248 error_report("tpm_passthrough: Bad TPM device path %s",
249 tpm_pt->tpm_dev);
92dcc234
SB
250 }
251
252 return fd;
253}
254
803de211
MAL
255static int
256tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
4549a8b7
SB
257{
258 const char *value;
259
92dcc234 260 value = qemu_opt_get(opts, "cancel-path");
f59864ba
AV
261 if (value) {
262 tpm_pt->options->cancel_path = g_strdup(value);
263 tpm_pt->options->has_cancel_path = true;
264 }
92dcc234 265
4549a8b7 266 value = qemu_opt_get(opts, "path");
f59864ba
AV
267 if (value) {
268 tpm_pt->options->has_path = true;
269 tpm_pt->options->path = g_strdup(value);
4549a8b7
SB
270 }
271
f59864ba 272 tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
8f0605cc
SB
273 tpm_pt->tpm_fd = qemu_open(tpm_pt->tpm_dev, O_RDWR);
274 if (tpm_pt->tpm_fd < 0) {
27215a22 275 error_report("Cannot access TPM device using '%s': %s",
8f0605cc 276 tpm_pt->tpm_dev, strerror(errno));
bef2ed3f 277 return -1;
4549a8b7
SB
278 }
279
56a3c24f 280 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
27215a22 281 error_report("'%s' is not a TPM device.",
8f0605cc 282 tpm_pt->tpm_dev);
bef2ed3f 283 return -1;
4549a8b7
SB
284 }
285
bef2ed3f
MAL
286 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
287 if (tpm_pt->cancel_fd < 0) {
288 return -1;
289 }
4549a8b7 290
bef2ed3f 291 return 0;
4549a8b7
SB
292}
293
9f7c0ef2 294static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
4549a8b7 295{
8f0605cc 296 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
4549a8b7 297
bef2ed3f
MAL
298 if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
299 object_unref(obj);
300 return NULL;
92dcc234
SB
301 }
302
9f7c0ef2 303 return TPM_BACKEND(obj);
4549a8b7
SB
304}
305
683c4b77
SB
306static int tpm_passthrough_startup_tpm(TPMBackend *tb, size_t buffersize)
307{
308 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
309
310 if (buffersize && buffersize < tpm_pt->tpm_buffersize) {
311 error_report("Requested buffer size of %zu is smaller than host TPM's "
312 "fixed buffer size of %zu",
313 buffersize, tpm_pt->tpm_buffersize);
314 return -1;
315 }
316
317 return 0;
318}
319
f59864ba
AV
320static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
321{
322 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
323
324 options->type = TPM_TYPE_OPTIONS_KIND_PASSTHROUGH;
325 options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
326 TPM_PASSTHROUGH(tb)->options);
327
328 return options;
329}
330
bb716238
SB
331static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
332 TPM_STANDARD_CMDLINE_OPTS,
333 {
334 .name = "cancel-path",
335 .type = QEMU_OPT_STRING,
336 .help = "Sysfs file entry for canceling TPM commands",
337 },
338 {
339 .name = "path",
340 .type = QEMU_OPT_STRING,
341 .help = "Path to TPM device on the host",
342 },
343 { /* end of list */ },
344};
345
8f0605cc
SB
346static void tpm_passthrough_inst_init(Object *obj)
347{
f35fe5cb
AV
348 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
349
f59864ba 350 tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
f35fe5cb
AV
351 tpm_pt->tpm_fd = -1;
352 tpm_pt->cancel_fd = -1;
8f0605cc
SB
353}
354
355static void tpm_passthrough_inst_finalize(Object *obj)
356{
f35fe5cb
AV
357 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
358
359 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
360
8df4d848
MAL
361 if (tpm_pt->tpm_fd >= 0) {
362 qemu_close(tpm_pt->tpm_fd);
363 }
364 if (tpm_pt->cancel_fd >= 0) {
365 qemu_close(tpm_pt->cancel_fd);
366 }
f59864ba 367 qapi_free_TPMPassthroughOptions(tpm_pt->options);
8f0605cc
SB
368}
369
370static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
371{
372 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
373
d31076ba
MAL
374 tbc->type = TPM_TYPE_PASSTHROUGH;
375 tbc->opts = tpm_passthrough_cmdline_opts;
376 tbc->desc = "Passthrough TPM backend driver";
377 tbc->create = tpm_passthrough_create;
683c4b77 378 tbc->startup_tpm = tpm_passthrough_startup_tpm;
d31076ba
MAL
379 tbc->reset = tpm_passthrough_reset;
380 tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
381 tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
382 tbc->reset_tpm_established_flag =
383 tpm_passthrough_reset_tpm_established_flag;
384 tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
b21e6aaf 385 tbc->get_buffer_size = tpm_passthrough_get_buffer_size;
d31076ba 386 tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
b19a5eea 387 tbc->handle_request = tpm_passthrough_handle_request;
8f0605cc
SB
388}
389
390static const TypeInfo tpm_passthrough_info = {
391 .name = TYPE_TPM_PASSTHROUGH,
392 .parent = TYPE_TPM_BACKEND,
393 .instance_size = sizeof(TPMPassthruState),
394 .class_init = tpm_passthrough_class_init,
395 .instance_init = tpm_passthrough_inst_init,
396 .instance_finalize = tpm_passthrough_inst_finalize,
397};
398
4549a8b7
SB
399static void tpm_passthrough_register(void)
400{
8f0605cc 401 type_register_static(&tpm_passthrough_info);
4549a8b7
SB
402}
403
404type_init(tpm_passthrough_register)