Brad Bishop | 6e60e8b | 2018-02-01 10:27:11 -0500 | [diff] [blame] | 1 | From 8737eef18f39ed087fd911d0a0886e8174d0468c Mon Sep 17 00:00:00 2001 |
| 2 | From: Stefan Berger <stefanb@linux.vnet.ibm.com> |
| 3 | Date: Sat, 31 Dec 2016 11:23:32 -0500 |
| 4 | Subject: [PATCH 1/4] Provide support for the CUSE TPM |
| 5 | |
| 6 | Rather than integrating TPM functionality into QEMU directly |
| 7 | using the TPM emulation of libtpms, we now integrate an external |
| 8 | emulated TPM device. This device is expected to implement a Linux |
| 9 | CUSE interface (CUSE = character device in userspace). |
| 10 | |
| 11 | QEMU talks to the CUSE TPM using much functionality of the |
| 12 | passthrough driver. For example, the TPM commands and responses |
| 13 | are sent to the CUSE TPM using the read()/write() interface. |
| 14 | However, some out-of-band control needs to be done using the CUSE |
| 15 | TPM's ioctls. The CUSE TPM currently defines and implements 15 |
| 16 | different ioctls for controlling certain life-cycle aspects of |
| 17 | the emulated TPM. The ioctls can be regarded as a replacement for |
| 18 | direct function calls to a TPM emulator if the TPM were to be |
| 19 | directly integrated into QEMU. |
| 20 | |
| 21 | One of the ioctls allows to get a bitmask of supported capabilities. |
| 22 | Each returned bit indicates which capabilities have been implemented. |
| 23 | An include file defining the various ioctls is added to QEMU. |
| 24 | |
| 25 | The CUSE TPM and associated tools can be found here: |
| 26 | |
| 27 | https://github.com/stefanberger/swtpm |
| 28 | |
| 29 | (please use the latest version) |
| 30 | |
| 31 | To use the external CUSE TPM, the CUSE TPM should be started as follows: |
| 32 | |
| 33 | /usr/bin/swtpm_ioctl -s /dev/vtpm-test |
| 34 | |
| 35 | /usr/bin/swtpm_cuse -n vtpm-test |
| 36 | |
| 37 | QEMU can then be started using the following parameters: |
| 38 | |
| 39 | qemu-system-x86_64 \ |
| 40 | [...] \ |
| 41 | -tpmdev cuse-tpm,id=tpm0,cancel-path=/dev/null,path=/dev/vtpm-test \ |
| 42 | -device tpm-tis,id=tpm0,tpmdev=tpm0 \ |
| 43 | [...] |
| 44 | |
| 45 | Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com> |
| 46 | Cc: Eric Blake <eblake@redhat.com> |
| 47 | |
| 48 | Conflicts: |
| 49 | docs/qmp-commands.txt |
| 50 | |
| 51 | Patch cherry-picked from https://github.com/stefanberger/qemu-tpm, branch v2.8.0+tpm, |
| 52 | commit 27d6cd856d5a14061955df7a93ee490697a7a174. Applied cleanly except for |
| 53 | docs/qmp-commands.txt which did not exist yet in qemu 2.7. |
| 54 | |
| 55 | Upstream-Status: Pending [https://lists.nongnu.org/archive/html/qemu-devel/2016-06/msg00252.html] |
| 56 | Signed-off-by: Patrick Ohly <patrick.ohly@intel.com> |
| 57 | --- |
| 58 | hmp.c | 6 ++ |
| 59 | hw/tpm/tpm_int.h | 1 + |
| 60 | hw/tpm/tpm_ioctl.h | 215 +++++++++++++++++++++++++++++++++++++ |
| 61 | hw/tpm/tpm_passthrough.c | 274 +++++++++++++++++++++++++++++++++++++++++++++-- |
| 62 | qapi-schema.json | 18 +++- |
| 63 | qemu-options.hx | 21 +++- |
| 64 | tpm.c | 11 +- |
| 65 | 7 files changed, 529 insertions(+), 17 deletions(-) |
| 66 | create mode 100644 hw/tpm/tpm_ioctl.h |
| 67 | |
| 68 | diff --git a/hmp.c b/hmp.c |
| 69 | index cc2056e9e2..277b45ef5a 100644 |
| 70 | --- a/hmp.c |
| 71 | +++ b/hmp.c |
| 72 | @@ -883,6 +883,12 @@ void hmp_info_tpm(Monitor *mon, const QDict *qdict) |
| 73 | tpo->has_cancel_path ? ",cancel-path=" : "", |
| 74 | tpo->has_cancel_path ? tpo->cancel_path : ""); |
| 75 | break; |
| 76 | + case TPM_TYPE_OPTIONS_KIND_CUSE_TPM: |
| 77 | + tpo = ti->options->u.passthrough.data; |
| 78 | + monitor_printf(mon, "%s%s", |
| 79 | + tpo->has_path ? ",path=" : "", |
| 80 | + tpo->has_path ? tpo->path : ""); |
| 81 | + break; |
| 82 | case TPM_TYPE_OPTIONS_KIND__MAX: |
| 83 | break; |
| 84 | } |
| 85 | diff --git a/hw/tpm/tpm_int.h b/hw/tpm/tpm_int.h |
| 86 | index f2f285b3cc..6b2c9c953a 100644 |
| 87 | --- a/hw/tpm/tpm_int.h |
| 88 | +++ b/hw/tpm/tpm_int.h |
| 89 | @@ -61,6 +61,7 @@ struct tpm_resp_hdr { |
| 90 | #define TPM_TAG_RSP_AUTH1_COMMAND 0xc5 |
| 91 | #define TPM_TAG_RSP_AUTH2_COMMAND 0xc6 |
| 92 | |
| 93 | +#define TPM_SUCCESS 0 |
| 94 | #define TPM_FAIL 9 |
| 95 | |
| 96 | #define TPM_ORD_ContinueSelfTest 0x53 |
| 97 | diff --git a/hw/tpm/tpm_ioctl.h b/hw/tpm/tpm_ioctl.h |
| 98 | new file mode 100644 |
| 99 | index 0000000000..a341e15741 |
| 100 | --- /dev/null |
| 101 | +++ b/hw/tpm/tpm_ioctl.h |
| 102 | @@ -0,0 +1,215 @@ |
| 103 | +/* |
| 104 | + * tpm_ioctl.h |
| 105 | + * |
| 106 | + * (c) Copyright IBM Corporation 2014, 2015. |
| 107 | + * |
| 108 | + * This file is licensed under the terms of the 3-clause BSD license |
| 109 | + */ |
| 110 | +#ifndef _TPM_IOCTL_H_ |
| 111 | +#define _TPM_IOCTL_H_ |
| 112 | + |
| 113 | +#include <stdint.h> |
| 114 | +#include <sys/uio.h> |
| 115 | +#include <sys/types.h> |
| 116 | +#include <sys/ioctl.h> |
| 117 | + |
| 118 | +/* |
| 119 | + * Every response from a command involving a TPM command execution must hold |
| 120 | + * the ptm_res as the first element. |
| 121 | + * ptm_res corresponds to the error code of a command executed by the TPM. |
| 122 | + */ |
| 123 | + |
| 124 | +typedef uint32_t ptm_res; |
| 125 | + |
| 126 | +/* PTM_GET_TPMESTABLISHED: get the establishment bit */ |
| 127 | +struct ptm_est { |
| 128 | + union { |
| 129 | + struct { |
| 130 | + ptm_res tpm_result; |
| 131 | + unsigned char bit; /* TPM established bit */ |
| 132 | + } resp; /* response */ |
| 133 | + } u; |
| 134 | +}; |
| 135 | + |
| 136 | +/* PTM_RESET_TPMESTABLISHED: reset establishment bit */ |
| 137 | +struct ptm_reset_est { |
| 138 | + union { |
| 139 | + struct { |
| 140 | + uint8_t loc; /* locality to use */ |
| 141 | + } req; /* request */ |
| 142 | + struct { |
| 143 | + ptm_res tpm_result; |
| 144 | + } resp; /* response */ |
| 145 | + } u; |
| 146 | +}; |
| 147 | + |
| 148 | +/* PTM_INIT */ |
| 149 | +struct ptm_init { |
| 150 | + union { |
| 151 | + struct { |
| 152 | + uint32_t init_flags; /* see definitions below */ |
| 153 | + } req; /* request */ |
| 154 | + struct { |
| 155 | + ptm_res tpm_result; |
| 156 | + } resp; /* response */ |
| 157 | + } u; |
| 158 | +}; |
| 159 | + |
| 160 | +/* above init_flags */ |
| 161 | +#define PTM_INIT_FLAG_DELETE_VOLATILE (1 << 0) |
| 162 | + /* delete volatile state file after reading it */ |
| 163 | + |
| 164 | +/* PTM_SET_LOCALITY */ |
| 165 | +struct ptm_loc { |
| 166 | + union { |
| 167 | + struct { |
| 168 | + uint8_t loc; /* locality to set */ |
| 169 | + } req; /* request */ |
| 170 | + struct { |
| 171 | + ptm_res tpm_result; |
| 172 | + } resp; /* response */ |
| 173 | + } u; |
| 174 | +}; |
| 175 | + |
| 176 | +/* PTM_HASH_DATA: hash given data */ |
| 177 | +struct ptm_hdata { |
| 178 | + union { |
| 179 | + struct { |
| 180 | + uint32_t length; |
| 181 | + uint8_t data[4096]; |
| 182 | + } req; /* request */ |
| 183 | + struct { |
| 184 | + ptm_res tpm_result; |
| 185 | + } resp; /* response */ |
| 186 | + } u; |
| 187 | +}; |
| 188 | + |
| 189 | +/* |
| 190 | + * size of the TPM state blob to transfer; x86_64 can handle 8k, |
| 191 | + * ppc64le only ~7k; keep the response below a 4k page size |
| 192 | + */ |
| 193 | +#define PTM_STATE_BLOB_SIZE (3 * 1024) |
| 194 | + |
| 195 | +/* |
| 196 | + * The following is the data structure to get state blobs from the TPM. |
| 197 | + * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple reads |
| 198 | + * with this ioctl and with adjusted offset are necessary. All bytes |
| 199 | + * must be transferred and the transfer is done once the last byte has been |
| 200 | + * returned. |
| 201 | + * It is possible to use the read() interface for reading the data; however, |
| 202 | + * the first bytes of the state blob will be part of the response to the ioctl(); |
| 203 | + * a subsequent read() is only necessary if the total length (totlength) exceeds |
| 204 | + * the number of received bytes. seek() is not supported. |
| 205 | + */ |
| 206 | +struct ptm_getstate { |
| 207 | + union { |
| 208 | + struct { |
| 209 | + uint32_t state_flags; /* may be: PTM_STATE_FLAG_DECRYPTED */ |
| 210 | + uint32_t type; /* which blob to pull */ |
| 211 | + uint32_t offset; /* offset from where to read */ |
| 212 | + } req; /* request */ |
| 213 | + struct { |
| 214 | + ptm_res tpm_result; |
| 215 | + uint32_t state_flags; /* may be: PTM_STATE_FLAG_ENCRYPTED */ |
| 216 | + uint32_t totlength; /* total length that will be transferred */ |
| 217 | + uint32_t length; /* number of bytes in following buffer */ |
| 218 | + uint8_t data[PTM_STATE_BLOB_SIZE]; |
| 219 | + } resp; /* response */ |
| 220 | + } u; |
| 221 | +}; |
| 222 | + |
| 223 | +/* TPM state blob types */ |
| 224 | +#define PTM_BLOB_TYPE_PERMANENT 1 |
| 225 | +#define PTM_BLOB_TYPE_VOLATILE 2 |
| 226 | +#define PTM_BLOB_TYPE_SAVESTATE 3 |
| 227 | + |
| 228 | +/* state_flags above : */ |
| 229 | +#define PTM_STATE_FLAG_DECRYPTED 1 /* on input: get decrypted state */ |
| 230 | +#define PTM_STATE_FLAG_ENCRYPTED 2 /* on output: state is encrypted */ |
| 231 | + |
| 232 | +/* |
| 233 | + * The following is the data structure to set state blobs in the TPM. |
| 234 | + * If the size of the state blob exceeds the PTM_STATE_BLOB_SIZE, multiple |
| 235 | + * 'writes' using this ioctl are necessary. The last packet is indicated |
| 236 | + * by the length being smaller than the PTM_STATE_BLOB_SIZE. |
| 237 | + * The very first packet may have a length indicator of '0' enabling |
| 238 | + * a write() with all the bytes from a buffer. If the write() interface |
| 239 | + * is used, a final ioctl with a non-full buffer must be made to indicate |
| 240 | + * that all data were transferred (a write with 0 bytes would not work). |
| 241 | + */ |
| 242 | +struct ptm_setstate { |
| 243 | + union { |
| 244 | + struct { |
| 245 | + uint32_t state_flags; /* may be PTM_STATE_FLAG_ENCRYPTED */ |
| 246 | + uint32_t type; /* which blob to set */ |
| 247 | + uint32_t length; /* length of the data; |
| 248 | + use 0 on the first packet to |
| 249 | + transfer using write() */ |
| 250 | + uint8_t data[PTM_STATE_BLOB_SIZE]; |
| 251 | + } req; /* request */ |
| 252 | + struct { |
| 253 | + ptm_res tpm_result; |
| 254 | + } resp; /* response */ |
| 255 | + } u; |
| 256 | +}; |
| 257 | + |
| 258 | +/* |
| 259 | + * PTM_GET_CONFIG: Data structure to get runtime configuration information |
| 260 | + * such as which keys are applied. |
| 261 | + */ |
| 262 | +struct ptm_getconfig { |
| 263 | + union { |
| 264 | + struct { |
| 265 | + ptm_res tpm_result; |
| 266 | + uint32_t flags; |
| 267 | + } resp; /* response */ |
| 268 | + } u; |
| 269 | +}; |
| 270 | + |
| 271 | +#define PTM_CONFIG_FLAG_FILE_KEY 0x1 |
| 272 | +#define PTM_CONFIG_FLAG_MIGRATION_KEY 0x2 |
| 273 | + |
| 274 | + |
| 275 | +typedef uint64_t ptm_cap; |
| 276 | +typedef struct ptm_est ptm_est; |
| 277 | +typedef struct ptm_reset_est ptm_reset_est; |
| 278 | +typedef struct ptm_loc ptm_loc; |
| 279 | +typedef struct ptm_hdata ptm_hdata; |
| 280 | +typedef struct ptm_init ptm_init; |
| 281 | +typedef struct ptm_getstate ptm_getstate; |
| 282 | +typedef struct ptm_setstate ptm_setstate; |
| 283 | +typedef struct ptm_getconfig ptm_getconfig; |
| 284 | + |
| 285 | +/* capability flags returned by PTM_GET_CAPABILITY */ |
| 286 | +#define PTM_CAP_INIT (1) |
| 287 | +#define PTM_CAP_SHUTDOWN (1<<1) |
| 288 | +#define PTM_CAP_GET_TPMESTABLISHED (1<<2) |
| 289 | +#define PTM_CAP_SET_LOCALITY (1<<3) |
| 290 | +#define PTM_CAP_HASHING (1<<4) |
| 291 | +#define PTM_CAP_CANCEL_TPM_CMD (1<<5) |
| 292 | +#define PTM_CAP_STORE_VOLATILE (1<<6) |
| 293 | +#define PTM_CAP_RESET_TPMESTABLISHED (1<<7) |
| 294 | +#define PTM_CAP_GET_STATEBLOB (1<<8) |
| 295 | +#define PTM_CAP_SET_STATEBLOB (1<<9) |
| 296 | +#define PTM_CAP_STOP (1<<10) |
| 297 | +#define PTM_CAP_GET_CONFIG (1<<11) |
| 298 | + |
| 299 | +enum { |
| 300 | + PTM_GET_CAPABILITY = _IOR('P', 0, ptm_cap), |
| 301 | + PTM_INIT = _IOWR('P', 1, ptm_init), |
| 302 | + PTM_SHUTDOWN = _IOR('P', 2, ptm_res), |
| 303 | + PTM_GET_TPMESTABLISHED = _IOR('P', 3, ptm_est), |
| 304 | + PTM_SET_LOCALITY = _IOWR('P', 4, ptm_loc), |
| 305 | + PTM_HASH_START = _IOR('P', 5, ptm_res), |
| 306 | + PTM_HASH_DATA = _IOWR('P', 6, ptm_hdata), |
| 307 | + PTM_HASH_END = _IOR('P', 7, ptm_res), |
| 308 | + PTM_CANCEL_TPM_CMD = _IOR('P', 8, ptm_res), |
| 309 | + PTM_STORE_VOLATILE = _IOR('P', 9, ptm_res), |
| 310 | + PTM_RESET_TPMESTABLISHED = _IOWR('P', 10, ptm_reset_est), |
| 311 | + PTM_GET_STATEBLOB = _IOWR('P', 11, ptm_getstate), |
| 312 | + PTM_SET_STATEBLOB = _IOWR('P', 12, ptm_setstate), |
| 313 | + PTM_STOP = _IOR('P', 13, ptm_res), |
| 314 | + PTM_GET_CONFIG = _IOR('P', 14, ptm_getconfig), |
| 315 | +}; |
| 316 | + |
| 317 | +#endif /* _TPM_IOCTL_H */ |
| 318 | diff --git a/hw/tpm/tpm_passthrough.c b/hw/tpm/tpm_passthrough.c |
| 319 | index e88c0d20bc..050f2ba850 100644 |
| 320 | --- a/hw/tpm/tpm_passthrough.c |
| 321 | +++ b/hw/tpm/tpm_passthrough.c |
| 322 | @@ -33,6 +33,7 @@ |
| 323 | #include "sysemu/tpm_backend_int.h" |
| 324 | #include "tpm_tis.h" |
| 325 | #include "tpm_util.h" |
| 326 | +#include "tpm_ioctl.h" |
| 327 | |
| 328 | #define DEBUG_TPM 0 |
| 329 | |
| 330 | @@ -45,6 +46,7 @@ |
| 331 | #define TYPE_TPM_PASSTHROUGH "tpm-passthrough" |
| 332 | #define TPM_PASSTHROUGH(obj) \ |
| 333 | OBJECT_CHECK(TPMPassthruState, (obj), TYPE_TPM_PASSTHROUGH) |
| 334 | +#define TYPE_TPM_CUSE "tpm-cuse" |
| 335 | |
| 336 | static const TPMDriverOps tpm_passthrough_driver; |
| 337 | |
| 338 | @@ -71,12 +73,18 @@ struct TPMPassthruState { |
| 339 | bool had_startup_error; |
| 340 | |
| 341 | TPMVersion tpm_version; |
| 342 | + ptm_cap cuse_cap; /* capabilities of the CUSE TPM */ |
| 343 | + uint8_t cur_locty_number; /* last set locality */ |
| 344 | }; |
| 345 | |
| 346 | typedef struct TPMPassthruState TPMPassthruState; |
| 347 | |
| 348 | #define TPM_PASSTHROUGH_DEFAULT_DEVICE "/dev/tpm0" |
| 349 | |
| 350 | +#define TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt) (tpm_pt->cuse_cap != 0) |
| 351 | + |
| 352 | +#define TPM_CUSE_IMPLEMENTS_ALL(S, cap) (((S)->cuse_cap & (cap)) == (cap)) |
| 353 | + |
| 354 | /* functions */ |
| 355 | |
| 356 | static void tpm_passthrough_cancel_cmd(TPMBackend *tb); |
| 357 | @@ -148,7 +156,28 @@ static bool tpm_passthrough_is_selftest(const uint8_t *in, uint32_t in_len) |
| 358 | return false; |
| 359 | } |
| 360 | |
| 361 | +static int tpm_passthrough_set_locality(TPMPassthruState *tpm_pt, |
| 362 | + uint8_t locty_number) |
| 363 | +{ |
| 364 | + ptm_loc loc; |
| 365 | + |
| 366 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 367 | + if (tpm_pt->cur_locty_number != locty_number) { |
| 368 | + loc.u.req.loc = locty_number; |
| 369 | + if (ioctl(tpm_pt->tpm_fd, PTM_SET_LOCALITY, &loc) < 0) { |
| 370 | + error_report("tpm_cuse: could not set locality on " |
| 371 | + "CUSE TPM: %s", |
| 372 | + strerror(errno)); |
| 373 | + return -1; |
| 374 | + } |
| 375 | + tpm_pt->cur_locty_number = locty_number; |
| 376 | + } |
| 377 | + } |
| 378 | + return 0; |
| 379 | +} |
| 380 | + |
| 381 | static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, |
| 382 | + uint8_t locality_number, |
| 383 | const uint8_t *in, uint32_t in_len, |
| 384 | uint8_t *out, uint32_t out_len, |
| 385 | bool *selftest_done) |
| 386 | @@ -157,6 +186,11 @@ static int tpm_passthrough_unix_tx_bufs(TPMPassthruState *tpm_pt, |
| 387 | bool is_selftest; |
| 388 | const struct tpm_resp_hdr *hdr; |
| 389 | |
| 390 | + ret = tpm_passthrough_set_locality(tpm_pt, locality_number); |
| 391 | + if (ret < 0) { |
| 392 | + goto err_exit; |
| 393 | + } |
| 394 | + |
| 395 | tpm_pt->tpm_op_canceled = false; |
| 396 | tpm_pt->tpm_executing = true; |
| 397 | *selftest_done = false; |
| 398 | @@ -207,10 +241,12 @@ err_exit: |
| 399 | } |
| 400 | |
| 401 | static int tpm_passthrough_unix_transfer(TPMPassthruState *tpm_pt, |
| 402 | + uint8_t locality_number, |
| 403 | const TPMLocality *locty_data, |
| 404 | bool *selftest_done) |
| 405 | { |
| 406 | return tpm_passthrough_unix_tx_bufs(tpm_pt, |
| 407 | + locality_number, |
| 408 | locty_data->w_buffer.buffer, |
| 409 | locty_data->w_offset, |
| 410 | locty_data->r_buffer.buffer, |
| 411 | @@ -231,6 +267,7 @@ static void tpm_passthrough_worker_thread(gpointer data, |
| 412 | switch (cmd) { |
| 413 | case TPM_BACKEND_CMD_PROCESS_CMD: |
| 414 | tpm_passthrough_unix_transfer(tpm_pt, |
| 415 | + thr_parms->tpm_state->locty_number, |
| 416 | thr_parms->tpm_state->locty_data, |
| 417 | &selftest_done); |
| 418 | |
| 419 | @@ -247,6 +284,93 @@ static void tpm_passthrough_worker_thread(gpointer data, |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | + * Gracefully shut down the external CUSE TPM |
| 424 | + */ |
| 425 | +static void tpm_passthrough_shutdown(TPMPassthruState *tpm_pt) |
| 426 | +{ |
| 427 | + ptm_res res; |
| 428 | + |
| 429 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 430 | + if (ioctl(tpm_pt->tpm_fd, PTM_SHUTDOWN, &res) < 0) { |
| 431 | + error_report("tpm_cuse: Could not cleanly shut down " |
| 432 | + "the CUSE TPM: %s", |
| 433 | + strerror(errno)); |
| 434 | + } |
| 435 | + } |
| 436 | +} |
| 437 | + |
| 438 | +/* |
| 439 | + * Probe for the CUSE TPM by sending an ioctl() requesting its |
| 440 | + * capability flags. |
| 441 | + */ |
| 442 | +static int tpm_passthrough_cuse_probe(TPMPassthruState *tpm_pt) |
| 443 | +{ |
| 444 | + int rc = 0; |
| 445 | + |
| 446 | + if (ioctl(tpm_pt->tpm_fd, PTM_GET_CAPABILITY, &tpm_pt->cuse_cap) < 0) { |
| 447 | + error_report("Error: CUSE TPM was requested, but probing failed"); |
| 448 | + rc = -1; |
| 449 | + } |
| 450 | + |
| 451 | + return rc; |
| 452 | +} |
| 453 | + |
| 454 | +static int tpm_passthrough_cuse_check_caps(TPMPassthruState *tpm_pt) |
| 455 | +{ |
| 456 | + int rc = 0; |
| 457 | + ptm_cap caps = 0; |
| 458 | + const char *tpm = NULL; |
| 459 | + |
| 460 | + /* check for min. required capabilities */ |
| 461 | + switch (tpm_pt->tpm_version) { |
| 462 | + case TPM_VERSION_1_2: |
| 463 | + caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED | |
| 464 | + PTM_CAP_SET_LOCALITY; |
| 465 | + tpm = "1.2"; |
| 466 | + break; |
| 467 | + case TPM_VERSION_2_0: |
| 468 | + caps = PTM_CAP_INIT | PTM_CAP_SHUTDOWN | PTM_CAP_GET_TPMESTABLISHED | |
| 469 | + PTM_CAP_SET_LOCALITY | PTM_CAP_RESET_TPMESTABLISHED; |
| 470 | + tpm = "2"; |
| 471 | + break; |
| 472 | + case TPM_VERSION_UNSPEC: |
| 473 | + error_report("tpm_cuse: %s: TPM version has not been set", |
| 474 | + __func__); |
| 475 | + return -1; |
| 476 | + } |
| 477 | + |
| 478 | + if (!TPM_CUSE_IMPLEMENTS_ALL(tpm_pt, caps)) { |
| 479 | + error_report("tpm_cuse: TPM does not implement minimum set of required " |
| 480 | + "capabilities for TPM %s (0x%x)", tpm, (int)caps); |
| 481 | + rc = -1; |
| 482 | + } |
| 483 | + |
| 484 | + return rc; |
| 485 | +} |
| 486 | + |
| 487 | +/* |
| 488 | + * Initialize the external CUSE TPM |
| 489 | + */ |
| 490 | +static int tpm_passthrough_cuse_init(TPMPassthruState *tpm_pt) |
| 491 | +{ |
| 492 | + int rc = 0; |
| 493 | + ptm_init init = { |
| 494 | + .u.req.init_flags = PTM_INIT_FLAG_DELETE_VOLATILE, |
| 495 | + }; |
| 496 | + |
| 497 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 498 | + if (ioctl(tpm_pt->tpm_fd, PTM_INIT, &init) < 0) { |
| 499 | + error_report("tpm_cuse: Detected CUSE TPM but could not " |
| 500 | + "send INIT: %s", |
| 501 | + strerror(errno)); |
| 502 | + rc = -1; |
| 503 | + } |
| 504 | + } |
| 505 | + |
| 506 | + return rc; |
| 507 | +} |
| 508 | + |
| 509 | +/* |
| 510 | * Start the TPM (thread). If it had been started before, then terminate |
| 511 | * and start it again. |
| 512 | */ |
| 513 | @@ -261,6 +385,8 @@ static int tpm_passthrough_startup_tpm(TPMBackend *tb) |
| 514 | tpm_passthrough_worker_thread, |
| 515 | &tpm_pt->tpm_thread_params); |
| 516 | |
| 517 | + tpm_passthrough_cuse_init(tpm_pt); |
| 518 | + |
| 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | @@ -291,14 +417,43 @@ static int tpm_passthrough_init(TPMBackend *tb, TPMState *s, |
| 523 | |
| 524 | static bool tpm_passthrough_get_tpm_established_flag(TPMBackend *tb) |
| 525 | { |
| 526 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); |
| 527 | + ptm_est est; |
| 528 | + |
| 529 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 530 | + if (ioctl(tpm_pt->tpm_fd, PTM_GET_TPMESTABLISHED, &est) < 0) { |
| 531 | + error_report("tpm_cuse: Could not get the TPM established " |
| 532 | + "flag from the CUSE TPM: %s", |
| 533 | + strerror(errno)); |
| 534 | + return false; |
| 535 | + } |
| 536 | + return (est.u.resp.bit != 0); |
| 537 | + } |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | static int tpm_passthrough_reset_tpm_established_flag(TPMBackend *tb, |
| 542 | uint8_t locty) |
| 543 | { |
| 544 | + TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); |
| 545 | + int rc = 0; |
| 546 | + ptm_reset_est ptmreset_est; |
| 547 | + |
| 548 | /* only a TPM 2.0 will support this */ |
| 549 | - return 0; |
| 550 | + if (tpm_pt->tpm_version == TPM_VERSION_2_0) { |
| 551 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 552 | + ptmreset_est.u.req.loc = tpm_pt->cur_locty_number; |
| 553 | + |
| 554 | + if (ioctl(tpm_pt->tpm_fd, PTM_RESET_TPMESTABLISHED, |
| 555 | + &ptmreset_est) < 0) { |
| 556 | + error_report("tpm_cuse: Could not reset the establishment bit " |
| 557 | + "failed: %s", |
| 558 | + strerror(errno)); |
| 559 | + rc = -1; |
| 560 | + } |
| 561 | + } |
| 562 | + } |
| 563 | + return rc; |
| 564 | } |
| 565 | |
| 566 | static bool tpm_passthrough_get_startup_error(TPMBackend *tb) |
| 567 | @@ -329,7 +484,8 @@ static void tpm_passthrough_deliver_request(TPMBackend *tb) |
| 568 | static void tpm_passthrough_cancel_cmd(TPMBackend *tb) |
| 569 | { |
| 570 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); |
| 571 | - int n; |
| 572 | + ptm_res res; |
| 573 | + static bool error_printed; |
| 574 | |
| 575 | /* |
| 576 | * As of Linux 3.7 the tpm_tis driver does not properly cancel |
| 577 | @@ -338,17 +494,34 @@ static void tpm_passthrough_cancel_cmd(TPMBackend *tb) |
| 578 | * command, e.g., a command executed on the host. |
| 579 | */ |
| 580 | if (tpm_pt->tpm_executing) { |
| 581 | - if (tpm_pt->cancel_fd >= 0) { |
| 582 | - n = write(tpm_pt->cancel_fd, "-", 1); |
| 583 | - if (n != 1) { |
| 584 | - error_report("Canceling TPM command failed: %s", |
| 585 | - strerror(errno)); |
| 586 | - } else { |
| 587 | - tpm_pt->tpm_op_canceled = true; |
| 588 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 589 | + if (TPM_CUSE_IMPLEMENTS_ALL(tpm_pt, PTM_CAP_CANCEL_TPM_CMD)) { |
| 590 | + if (ioctl(tpm_pt->tpm_fd, PTM_CANCEL_TPM_CMD, &res) < 0) { |
| 591 | + error_report("tpm_cuse: Could not cancel command on " |
| 592 | + "CUSE TPM: %s", |
| 593 | + strerror(errno)); |
| 594 | + } else if (res != TPM_SUCCESS) { |
| 595 | + if (!error_printed) { |
| 596 | + error_report("TPM error code from command " |
| 597 | + "cancellation of CUSE TPM: 0x%x", res); |
| 598 | + error_printed = true; |
| 599 | + } |
| 600 | + } else { |
| 601 | + tpm_pt->tpm_op_canceled = true; |
| 602 | + } |
| 603 | } |
| 604 | } else { |
| 605 | - error_report("Cannot cancel TPM command due to missing " |
| 606 | - "TPM sysfs cancel entry"); |
| 607 | + if (tpm_pt->cancel_fd >= 0) { |
| 608 | + if (write(tpm_pt->cancel_fd, "-", 1) != 1) { |
| 609 | + error_report("Canceling TPM command failed: %s", |
| 610 | + strerror(errno)); |
| 611 | + } else { |
| 612 | + tpm_pt->tpm_op_canceled = true; |
| 613 | + } |
| 614 | + } else { |
| 615 | + error_report("Cannot cancel TPM command due to missing " |
| 616 | + "TPM sysfs cancel entry"); |
| 617 | + } |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | @@ -378,6 +551,11 @@ static int tpm_passthrough_open_sysfs_cancel(TPMBackend *tb) |
| 622 | char *dev; |
| 623 | char path[PATH_MAX]; |
| 624 | |
| 625 | + if (TPM_PASSTHROUGH_USES_CUSE_TPM(tpm_pt)) { |
| 626 | + /* not needed, but so we have a fd */ |
| 627 | + return qemu_open("/dev/null", O_WRONLY); |
| 628 | + } |
| 629 | + |
| 630 | if (tb->cancel_path) { |
| 631 | fd = qemu_open(tb->cancel_path, O_WRONLY); |
| 632 | if (fd < 0) { |
| 633 | @@ -412,12 +590,22 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) |
| 634 | { |
| 635 | TPMPassthruState *tpm_pt = TPM_PASSTHROUGH(tb); |
| 636 | const char *value; |
| 637 | + bool have_cuse = false; |
| 638 | + |
| 639 | + value = qemu_opt_get(opts, "type"); |
| 640 | + if (value != NULL && !strcmp("cuse-tpm", value)) { |
| 641 | + have_cuse = true; |
| 642 | + } |
| 643 | |
| 644 | value = qemu_opt_get(opts, "cancel-path"); |
| 645 | tb->cancel_path = g_strdup(value); |
| 646 | |
| 647 | value = qemu_opt_get(opts, "path"); |
| 648 | if (!value) { |
| 649 | + if (have_cuse) { |
| 650 | + error_report("Missing path to access CUSE TPM"); |
| 651 | + goto err_free_parameters; |
| 652 | + } |
| 653 | value = TPM_PASSTHROUGH_DEFAULT_DEVICE; |
| 654 | } |
| 655 | |
| 656 | @@ -432,15 +620,36 @@ static int tpm_passthrough_handle_device_opts(QemuOpts *opts, TPMBackend *tb) |
| 657 | goto err_free_parameters; |
| 658 | } |
| 659 | |
| 660 | + tpm_pt->cur_locty_number = ~0; |
| 661 | + |
| 662 | + if (have_cuse) { |
| 663 | + if (tpm_passthrough_cuse_probe(tpm_pt)) { |
| 664 | + goto err_close_tpmdev; |
| 665 | + } |
| 666 | + /* init TPM for probing */ |
| 667 | + if (tpm_passthrough_cuse_init(tpm_pt)) { |
| 668 | + goto err_close_tpmdev; |
| 669 | + } |
| 670 | + } |
| 671 | + |
| 672 | if (tpm_util_test_tpmdev(tpm_pt->tpm_fd, &tpm_pt->tpm_version)) { |
| 673 | error_report("'%s' is not a TPM device.", |
| 674 | tpm_pt->tpm_dev); |
| 675 | goto err_close_tpmdev; |
| 676 | } |
| 677 | |
| 678 | + if (have_cuse) { |
| 679 | + if (tpm_passthrough_cuse_check_caps(tpm_pt)) { |
| 680 | + goto err_close_tpmdev; |
| 681 | + } |
| 682 | + } |
| 683 | + |
| 684 | + |
| 685 | return 0; |
| 686 | |
| 687 | err_close_tpmdev: |
| 688 | + tpm_passthrough_shutdown(tpm_pt); |
| 689 | + |
| 690 | qemu_close(tpm_pt->tpm_fd); |
| 691 | tpm_pt->tpm_fd = -1; |
| 692 | |
| 693 | @@ -491,6 +700,8 @@ static void tpm_passthrough_destroy(TPMBackend *tb) |
| 694 | |
| 695 | tpm_backend_thread_end(&tpm_pt->tbt); |
| 696 | |
| 697 | + tpm_passthrough_shutdown(tpm_pt); |
| 698 | + |
| 699 | qemu_close(tpm_pt->tpm_fd); |
| 700 | qemu_close(tpm_pt->cancel_fd); |
| 701 | |
| 702 | @@ -564,3 +775,44 @@ static void tpm_passthrough_register(void) |
| 703 | } |
| 704 | |
| 705 | type_init(tpm_passthrough_register) |
| 706 | + |
| 707 | +/* CUSE TPM */ |
| 708 | +static const char *tpm_passthrough_cuse_create_desc(void) |
| 709 | +{ |
| 710 | + return "CUSE TPM backend driver"; |
| 711 | +} |
| 712 | + |
| 713 | +static const TPMDriverOps tpm_cuse_driver = { |
| 714 | + .type = TPM_TYPE_CUSE_TPM, |
| 715 | + .opts = tpm_passthrough_cmdline_opts, |
| 716 | + .desc = tpm_passthrough_cuse_create_desc, |
| 717 | + .create = tpm_passthrough_create, |
| 718 | + .destroy = tpm_passthrough_destroy, |
| 719 | + .init = tpm_passthrough_init, |
| 720 | + .startup_tpm = tpm_passthrough_startup_tpm, |
| 721 | + .realloc_buffer = tpm_passthrough_realloc_buffer, |
| 722 | + .reset = tpm_passthrough_reset, |
| 723 | + .had_startup_error = tpm_passthrough_get_startup_error, |
| 724 | + .deliver_request = tpm_passthrough_deliver_request, |
| 725 | + .cancel_cmd = tpm_passthrough_cancel_cmd, |
| 726 | + .get_tpm_established_flag = tpm_passthrough_get_tpm_established_flag, |
| 727 | + .reset_tpm_established_flag = tpm_passthrough_reset_tpm_established_flag, |
| 728 | + .get_tpm_version = tpm_passthrough_get_tpm_version, |
| 729 | +}; |
| 730 | + |
| 731 | +static const TypeInfo tpm_cuse_info = { |
| 732 | + .name = TYPE_TPM_CUSE, |
| 733 | + .parent = TYPE_TPM_BACKEND, |
| 734 | + .instance_size = sizeof(TPMPassthruState), |
| 735 | + .class_init = tpm_passthrough_class_init, |
| 736 | + .instance_init = tpm_passthrough_inst_init, |
| 737 | + .instance_finalize = tpm_passthrough_inst_finalize, |
| 738 | +}; |
| 739 | + |
| 740 | +static void tpm_cuse_register(void) |
| 741 | +{ |
| 742 | + type_register_static(&tpm_cuse_info); |
| 743 | + tpm_register_driver(&tpm_cuse_driver); |
| 744 | +} |
| 745 | + |
| 746 | +type_init(tpm_cuse_register) |
| 747 | diff --git a/qapi-schema.json b/qapi-schema.json |
| 748 | index 5658723b37..53120d0f63 100644 |
| 749 | --- a/qapi-schema.json |
| 750 | +++ b/qapi-schema.json |
| 751 | @@ -3522,10 +3522,12 @@ |
| 752 | # An enumeration of TPM types |
| 753 | # |
| 754 | # @passthrough: TPM passthrough type |
| 755 | +# @cuse-tpm: CUSE TPM type |
| 756 | +# Since: 2.6 |
| 757 | # |
| 758 | # Since: 1.5 |
| 759 | ## |
| 760 | -{ 'enum': 'TpmType', 'data': [ 'passthrough' ] } |
| 761 | +{ 'enum': 'TpmType', 'data': [ 'passthrough', 'cuse-tpm' ] } |
| 762 | |
| 763 | ## |
| 764 | # @query-tpm-types: |
| 765 | @@ -3554,6 +3556,17 @@ |
| 766 | '*cancel-path' : 'str'} } |
| 767 | |
| 768 | ## |
| 769 | +# @TPMCuseOptions: |
| 770 | +# |
| 771 | +# Information about the CUSE TPM type |
| 772 | +# |
| 773 | +# @path: string describing the path used for accessing the TPM device |
| 774 | +# |
| 775 | +# Since: 2.6 |
| 776 | +## |
| 777 | +{ 'struct': 'TPMCuseOptions', 'data': { 'path' : 'str'}} |
| 778 | + |
| 779 | +## |
| 780 | # @TpmTypeOptions: |
| 781 | # |
| 782 | # A union referencing different TPM backend types' configuration options |
| 783 | @@ -3563,7 +3576,8 @@ |
| 784 | # Since: 1.5 |
| 785 | ## |
| 786 | { 'union': 'TpmTypeOptions', |
| 787 | - 'data': { 'passthrough' : 'TPMPassthroughOptions' } } |
| 788 | + 'data': { 'passthrough' : 'TPMPassthroughOptions', |
| 789 | + 'cuse-tpm' : 'TPMCuseOptions' } } |
| 790 | |
| 791 | ## |
| 792 | # @TpmInfo: |
| 793 | diff --git a/qemu-options.hx b/qemu-options.hx |
| 794 | index a71aaf8ea8..e0f1d8e676 100644 |
| 795 | --- a/qemu-options.hx |
| 796 | +++ b/qemu-options.hx |
| 797 | @@ -2763,7 +2763,10 @@ DEF("tpmdev", HAS_ARG, QEMU_OPTION_tpmdev, \ |
| 798 | "-tpmdev passthrough,id=id[,path=path][,cancel-path=path]\n" |
| 799 | " use path to provide path to a character device; default is /dev/tpm0\n" |
| 800 | " use cancel-path to provide path to TPM's cancel sysfs entry; if\n" |
| 801 | - " not provided it will be searched for in /sys/class/misc/tpm?/device\n", |
| 802 | + " not provided it will be searched for in /sys/class/misc/tpm?/device\n" |
| 803 | + "-tpmdev cuse-tpm,id=id,path=path\n" |
| 804 | + " use path to provide path to a character device to talk to the\n" |
| 805 | + " TPM emulator providing a CUSE interface\n", |
| 806 | QEMU_ARCH_ALL) |
| 807 | STEXI |
| 808 | |
| 809 | @@ -2772,8 +2775,8 @@ The general form of a TPM device option is: |
| 810 | |
| 811 | @item -tpmdev @var{backend} ,id=@var{id} [,@var{options}] |
| 812 | @findex -tpmdev |
| 813 | -Backend type must be: |
| 814 | -@option{passthrough}. |
| 815 | +Backend type must be either one of the following: |
| 816 | +@option{passthrough}, @option{cuse-tpm}. |
| 817 | |
| 818 | The specific backend type will determine the applicable options. |
| 819 | The @code{-tpmdev} option creates the TPM backend and requires a |
| 820 | @@ -2823,6 +2826,18 @@ To create a passthrough TPM use the following two options: |
| 821 | Note that the @code{-tpmdev} id is @code{tpm0} and is referenced by |
| 822 | @code{tpmdev=tpm0} in the device option. |
| 823 | |
| 824 | +@item -tpmdev cuse-tpm, id=@var{id}, path=@var{path} |
| 825 | + |
| 826 | +(Linux-host only) Enable access to a TPM emulator with a CUSE interface. |
| 827 | + |
| 828 | +@option{path} specifies the path to the CUSE TPM character device. |
| 829 | + |
| 830 | +To create a backend device accessing the CUSE TPM emulator using /dev/vtpm |
| 831 | +use the following two options: |
| 832 | +@example |
| 833 | +-tpmdev cuse-tpm,id=tpm0,path=/dev/vtpm -device tpm-tis,tpmdev=tpm0 |
| 834 | +@end example |
| 835 | + |
| 836 | @end table |
| 837 | |
| 838 | ETEXI |
| 839 | diff --git a/tpm.c b/tpm.c |
| 840 | index 9a7c7114d3..5ec2373286 100644 |
| 841 | --- a/tpm.c |
| 842 | +++ b/tpm.c |
| 843 | @@ -25,7 +25,7 @@ static QLIST_HEAD(, TPMBackend) tpm_backends = |
| 844 | |
| 845 | |
| 846 | #define TPM_MAX_MODELS 1 |
| 847 | -#define TPM_MAX_DRIVERS 1 |
| 848 | +#define TPM_MAX_DRIVERS 2 |
| 849 | |
| 850 | static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = { |
| 851 | NULL, |
| 852 | @@ -272,6 +272,15 @@ static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv) |
| 853 | tpo->has_cancel_path = true; |
| 854 | } |
| 855 | break; |
| 856 | + case TPM_TYPE_CUSE_TPM: |
| 857 | + res->options->type = TPM_TYPE_OPTIONS_KIND_CUSE_TPM; |
| 858 | + tpo = g_new0(TPMPassthroughOptions, 1); |
| 859 | + res->options->u.passthrough.data = tpo; |
| 860 | + if (drv->path) { |
| 861 | + tpo->path = g_strdup(drv->path); |
| 862 | + tpo->has_path = true; |
| 863 | + } |
| 864 | + break; |
| 865 | case TPM_TYPE__MAX: |
| 866 | break; |
| 867 | } |
| 868 | -- |
| 869 | 2.11.0 |
| 870 | |