hw/nvme: fix memory leak in nvme_dsm
[qemu/ar7.git] / backends / tpm / tpm_passthrough.c
blob179697a3a94011307ff2dc98634ddd685c2c75a9
1 /*
2 * passthrough TPM driver
4 * Copyright (c) 2010 - 2013 IBM Corporation
5 * Authors:
6 * Stefan Berger <stefanb@us.ibm.com>
8 * Copyright (C) 2011 IAIK, Graz University of Technology
9 * Author: Andreas Niederl
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.1 of the License, or (at your option) any later version.
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.
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/>
25 #include "qemu/osdep.h"
26 #include "qemu/error-report.h"
27 #include "qemu/module.h"
28 #include "qemu/sockets.h"
29 #include "sysemu/tpm_backend.h"
30 #include "sysemu/tpm_util.h"
31 #include "tpm_int.h"
32 #include "qapi/clone-visitor.h"
33 #include "qapi/qapi-visit-tpm.h"
34 #include "trace.h"
35 #include "qom/object.h"
37 #define TYPE_TPM_PASSTHROUGH "tpm-passthrough"
38 OBJECT_DECLARE_SIMPLE_TYPE(TPMPassthruState, TPM_PASSTHROUGH)
40 /* data structures */
41 struct TPMPassthruState {
42 TPMBackend parent;
44 TPMPassthroughOptions *options;
45 const char *tpm_dev;
46 int tpm_fd;
47 bool tpm_executing;
48 bool tpm_op_canceled;
49 int cancel_fd;
51 TPMVersion tpm_version;
52 size_t tpm_buffersize;
56 #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0"
58 /* functions */
60 static void tpm_passthrough_cancel_cmd(TPMBackend *tb);
62 static int tpm_passthrough_unix_read(int fd, uint8_t *buf, uint32_t len)
64 int ret;
65 reread:
66 ret = read(fd, buf, len);
67 if (ret < 0) {
68 if (errno != EINTR && errno != EAGAIN) {
69 return -1;
71 goto reread;
73 return ret;
76 static 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)
81 ssize_t ret;
82 bool is_selftest;
84 /* FIXME: protect shared variables or use other sync mechanism */
85 tpm_pt->tpm_op_canceled = false;
86 tpm_pt->tpm_executing = true;
87 *selftest_done = false;
89 is_selftest = tpm_util_is_selftest(in, in_len);
91 ret = qemu_write_full(tpm_pt->tpm_fd, in, in_len);
92 if (ret != in_len) {
93 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
94 error_setg_errno(errp, errno, "tpm_passthrough: error while "
95 "transmitting data to TPM");
97 goto err_exit;
100 tpm_pt->tpm_executing = false;
102 ret = tpm_passthrough_unix_read(tpm_pt->tpm_fd, out, out_len);
103 if (ret < 0) {
104 if (!tpm_pt->tpm_op_canceled || errno != ECANCELED) {
105 error_setg_errno(errp, errno, "tpm_passthrough: error while "
106 "reading data from TPM");
108 } else if (ret < sizeof(struct tpm_resp_hdr) ||
109 tpm_cmd_get_size(out) != ret) {
110 ret = -1;
111 error_setg_errno(errp, errno, "tpm_passthrough: received invalid "
112 "response packet from TPM");
115 if (is_selftest && (ret >= sizeof(struct tpm_resp_hdr))) {
116 *selftest_done = tpm_cmd_get_errcode(out) == 0;
119 err_exit:
120 if (ret < 0) {
121 tpm_util_write_fatal_error_response(out, out_len);
124 tpm_pt->tpm_executing = false;
127 static void tpm_passthrough_handle_request(TPMBackend *tb, TPMBackendCmd *cmd,
128 Error **errp)
130 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
132 trace_tpm_passthrough_handle_request(cmd);
134 tpm_passthrough_unix_tx_bufs(tpm_pt, cmd->in, cmd->in_len,
135 cmd->out, cmd->out_len, &cmd->selftest_done,
136 errp);
139 static void tpm_passthrough_reset(TPMBackend *tb)
141 trace_tpm_passthrough_reset();
143 tpm_passthrough_cancel_cmd(tb);
146 static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb)
148 return false;
151 static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb,
152 uint8_t locty)
154 /* only a TPM 2.0 will support this */
155 return 0;
158 static void tpm_passthrough_cancel_cmd(TPMBackend *tb)
160 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
161 int n;
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.
169 if (tpm_pt->tpm_executing) {
170 if (tpm_pt->cancel_fd >= 0) {
171 tpm_pt->tpm_op_canceled = true;
172 n = write(tpm_pt->cancel_fd, "-", 1);
173 if (n != 1) {
174 error_report("Canceling TPM command failed: %s",
175 strerror(errno));
177 } else {
178 error_report("Cannot cancel TPM command due to missing "
179 "TPM sysfs cancel entry");
184 static TPMVersion tpm_passthrough_get_tpm_version(TPMBackend *tb)
186 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
188 return tpm_pt->tpm_version;
191 static size_t tpm_passthrough_get_buffer_size(TPMBackend *tb)
193 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
194 int ret;
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;
201 return tpm_pt->tpm_buffersize;
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.
208 * From /dev/tpm0 create /sys/class/tpm/tpm0/device/cancel
209 * before 4.0: /sys/class/misc/tpm0/device/cancel
211 static int tpm_passthrough_open_sysfs_cancel(TPMPassthruState *tpm_pt)
213 int fd = -1;
214 char *dev;
215 char path[PATH_MAX];
217 if (tpm_pt->options->cancel_path) {
218 fd = qemu_open_old(tpm_pt->options->cancel_path, O_WRONLY);
219 if (fd < 0) {
220 error_report("tpm_passthrough: Could not open TPM cancel path: %s",
221 strerror(errno));
223 return fd;
226 dev = strrchr(tpm_pt->tpm_dev, '/');
227 if (!dev) {
228 error_report("tpm_passthrough: Bad TPM device path %s",
229 tpm_pt->tpm_dev);
230 return -1;
233 dev++;
234 if (snprintf(path, sizeof(path), "/sys/class/tpm/%s/device/cancel",
235 dev) < sizeof(path)) {
236 fd = qemu_open_old(path, O_WRONLY);
237 if (fd < 0) {
238 if (snprintf(path, sizeof(path), "/sys/class/misc/%s/device/cancel",
239 dev) < sizeof(path)) {
240 fd = qemu_open_old(path, O_WRONLY);
245 if (fd < 0) {
246 error_report("tpm_passthrough: Could not guess TPM cancel path");
247 } else {
248 tpm_pt->options->cancel_path = g_strdup(path);
251 return fd;
254 static int
255 tpm_passthrough_handle_device_opts(TPMPassthruState *tpm_pt, QemuOpts *opts)
257 const char *value;
259 value = qemu_opt_get(opts, "cancel-path");
260 if (value) {
261 tpm_pt->options->cancel_path = g_strdup(value);
264 value = qemu_opt_get(opts, "path");
265 if (value) {
266 tpm_pt->options->path = g_strdup(value);
269 tpm_pt->tpm_dev = value ? value : TPM_PASSTHROUGH_DEFAULT_DEVICE;
270 tpm_pt->tpm_fd = qemu_open_old(tpm_pt->tpm_dev, O_RDWR);
271 if (tpm_pt->tpm_fd < 0) {
272 error_report("Cannot access TPM device using '%s': %s",
273 tpm_pt->tpm_dev, strerror(errno));
274 return -1;
277 if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) {
278 error_report("'%s' is not a TPM device.",
279 tpm_pt->tpm_dev);
280 return -1;
283 tpm_pt->cancel_fd = tpm_passthrough_open_sysfs_cancel(tpm_pt);
284 if (tpm_pt->cancel_fd < 0) {
285 return -1;
288 return 0;
291 static TPMBackend *tpm_passthrough_create(QemuOpts *opts)
293 Object *obj = object_new(TYPE_TPM_PASSTHROUGH);
295 if (tpm_passthrough_handle_device_opts(TPM_PASSTHROUGH(obj), opts)) {
296 object_unref(obj);
297 return NULL;
300 return TPM_BACKEND(obj);
303 static int tpm_passthrough_startup_tpm(TPMBackend *tb, size_t buffersize)
305 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb);
307 if (buffersize && buffersize < tpm_pt->tpm_buffersize) {
308 error_report("Requested buffer size of %zu is smaller than host TPM's "
309 "fixed buffer size of %zu",
310 buffersize, tpm_pt->tpm_buffersize);
311 return -1;
314 return 0;
317 static TpmTypeOptions *tpm_passthrough_get_tpm_options(TPMBackend *tb)
319 TpmTypeOptions *options = g_new0(TpmTypeOptions, 1);
321 options->type = TPM_TYPE_PASSTHROUGH;
322 options->u.passthrough.data = QAPI_CLONE(TPMPassthroughOptions,
323 TPM_PASSTHROUGH(tb)->options);
325 return options;
328 static const QemuOptDesc tpm_passthrough_cmdline_opts[] = {
329 TPM_STANDARD_CMDLINE_OPTS,
331 .name = "cancel-path",
332 .type = QEMU_OPT_STRING,
333 .help = "Sysfs file entry for canceling TPM commands",
336 .name = "path",
337 .type = QEMU_OPT_STRING,
338 .help = "Path to TPM device on the host",
340 { /* end of list */ },
343 static void tpm_passthrough_inst_init(Object *obj)
345 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
347 tpm_pt->options = g_new0(TPMPassthroughOptions, 1);
348 tpm_pt->tpm_fd = -1;
349 tpm_pt->cancel_fd = -1;
352 static void tpm_passthrough_inst_finalize(Object *obj)
354 TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(obj);
356 tpm_passthrough_cancel_cmd(TPM_BACKEND(obj));
358 if (tpm_pt->tpm_fd >= 0) {
359 qemu_close(tpm_pt->tpm_fd);
361 if (tpm_pt->cancel_fd >= 0) {
362 qemu_close(tpm_pt->cancel_fd);
364 qapi_free_TPMPassthroughOptions(tpm_pt->options);
367 static void tpm_passthrough_class_init(ObjectClass *klass, void *data)
369 TPMBackendClass *tbc = TPM_BACKEND_CLASS(klass);
371 tbc->type = TPM_TYPE_PASSTHROUGH;
372 tbc->opts = tpm_passthrough_cmdline_opts;
373 tbc->desc = "Passthrough TPM backend driver";
374 tbc->create = tpm_passthrough_create;
375 tbc->startup_tpm = tpm_passthrough_startup_tpm;
376 tbc->reset = tpm_passthrough_reset;
377 tbc->cancel_cmd = tpm_passthrough_cancel_cmd;
378 tbc->get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag;
379 tbc->reset_tpm_established_flag =
380 tpm_passthrough_reset_tpm_established_flag;
381 tbc->get_tpm_version = tpm_passthrough_get_tpm_version;
382 tbc->get_buffer_size = tpm_passthrough_get_buffer_size;
383 tbc->get_tpm_options = tpm_passthrough_get_tpm_options;
384 tbc->handle_request = tpm_passthrough_handle_request;
387 static const TypeInfo tpm_passthrough_info = {
388 .name = TYPE_TPM_PASSTHROUGH,
389 .parent = TYPE_TPM_BACKEND,
390 .instance_size = sizeof(TPMPassthruState),
391 .class_init = tpm_passthrough_class_init,
392 .instance_init = tpm_passthrough_inst_init,
393 .instance_finalize = tpm_passthrough_inst_finalize,
396 static void tpm_passthrough_register(void)
398 type_register_static(&tpm_passthrough_info);
401 type_init(tpm_passthrough_register)