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