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