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