1 /* 2 * QEMU SEV support 3 * 4 * Copyright Advanced Micro Devices 2016-2018 5 * 6 * Author: 7 * Brijesh Singh <brijesh.singh@amd.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 * 12 */ 13 14 #include "qemu/osdep.h" 15 16 #include <linux/kvm.h> 17 #include <linux/psp-sev.h> 18 19 #include <sys/ioctl.h> 20 21 #include "qapi/error.h" 22 #include "qom/object_interfaces.h" 23 #include "qemu/base64.h" 24 #include "qemu/module.h" 25 #include "qemu/uuid.h" 26 #include "crypto/hash.h" 27 #include "sysemu/kvm.h" 28 #include "sev.h" 29 #include "sysemu/sysemu.h" 30 #include "sysemu/runstate.h" 31 #include "trace.h" 32 #include "migration/blocker.h" 33 #include "qom/object.h" 34 #include "monitor/monitor.h" 35 #include "monitor/hmp-target.h" 36 #include "qapi/qapi-commands-misc-target.h" 37 #include "exec/confidential-guest-support.h" 38 #include "hw/i386/pc.h" 39 #include "exec/address-spaces.h" 40 41 #define TYPE_SEV_GUEST "sev-guest" 42 OBJECT_DECLARE_SIMPLE_TYPE(SevGuestState, SEV_GUEST) 43 44 45 /** 46 * SevGuestState: 47 * 48 * The SevGuestState object is used for creating and managing a SEV 49 * guest. 50 * 51 * # $QEMU \ 52 * -object sev-guest,id=sev0 \ 53 * -machine ...,memory-encryption=sev0 54 */ 55 struct SevGuestState { 56 ConfidentialGuestSupport parent_obj; 57 58 /* configuration parameters */ 59 char *sev_device; 60 uint32_t policy; 61 char *dh_cert_file; 62 char *session_file; 63 uint32_t cbitpos; 64 uint32_t reduced_phys_bits; 65 bool kernel_hashes; 66 67 /* runtime state */ 68 uint32_t handle; 69 uint8_t api_major; 70 uint8_t api_minor; 71 uint8_t build_id; 72 int sev_fd; 73 SevState state; 74 gchar *measurement; 75 76 uint32_t reset_cs; 77 uint32_t reset_ip; 78 bool reset_data_valid; 79 }; 80 81 #define DEFAULT_GUEST_POLICY 0x1 /* disable debug */ 82 #define DEFAULT_SEV_DEVICE "/dev/sev" 83 84 #define SEV_INFO_BLOCK_GUID "00f771de-1a7e-4fcb-890e-68c77e2fb44e" 85 typedef struct __attribute__((__packed__)) SevInfoBlock { 86 /* SEV-ES Reset Vector Address */ 87 uint32_t reset_addr; 88 } SevInfoBlock; 89 90 #define SEV_HASH_TABLE_RV_GUID "7255371f-3a3b-4b04-927b-1da6efa8d454" 91 typedef struct QEMU_PACKED SevHashTableDescriptor { 92 /* SEV hash table area guest address */ 93 uint32_t base; 94 /* SEV hash table area size (in bytes) */ 95 uint32_t size; 96 } SevHashTableDescriptor; 97 98 /* hard code sha256 digest size */ 99 #define HASH_SIZE 32 100 101 typedef struct QEMU_PACKED SevHashTableEntry { 102 QemuUUID guid; 103 uint16_t len; 104 uint8_t hash[HASH_SIZE]; 105 } SevHashTableEntry; 106 107 typedef struct QEMU_PACKED SevHashTable { 108 QemuUUID guid; 109 uint16_t len; 110 SevHashTableEntry cmdline; 111 SevHashTableEntry initrd; 112 SevHashTableEntry kernel; 113 } SevHashTable; 114 115 /* 116 * Data encrypted by sev_encrypt_flash() must be padded to a multiple of 117 * 16 bytes. 118 */ 119 typedef struct QEMU_PACKED PaddedSevHashTable { 120 SevHashTable ht; 121 uint8_t padding[ROUND_UP(sizeof(SevHashTable), 16) - sizeof(SevHashTable)]; 122 } PaddedSevHashTable; 123 124 QEMU_BUILD_BUG_ON(sizeof(PaddedSevHashTable) % 16 != 0); 125 126 static SevGuestState *sev_guest; 127 static Error *sev_mig_blocker; 128 129 static const char *const sev_fw_errlist[] = { 130 [SEV_RET_SUCCESS] = "", 131 [SEV_RET_INVALID_PLATFORM_STATE] = "Platform state is invalid", 132 [SEV_RET_INVALID_GUEST_STATE] = "Guest state is invalid", 133 [SEV_RET_INAVLID_CONFIG] = "Platform configuration is invalid", 134 [SEV_RET_INVALID_LEN] = "Buffer too small", 135 [SEV_RET_ALREADY_OWNED] = "Platform is already owned", 136 [SEV_RET_INVALID_CERTIFICATE] = "Certificate is invalid", 137 [SEV_RET_POLICY_FAILURE] = "Policy is not allowed", 138 [SEV_RET_INACTIVE] = "Guest is not active", 139 [SEV_RET_INVALID_ADDRESS] = "Invalid address", 140 [SEV_RET_BAD_SIGNATURE] = "Bad signature", 141 [SEV_RET_BAD_MEASUREMENT] = "Bad measurement", 142 [SEV_RET_ASID_OWNED] = "ASID is already owned", 143 [SEV_RET_INVALID_ASID] = "Invalid ASID", 144 [SEV_RET_WBINVD_REQUIRED] = "WBINVD is required", 145 [SEV_RET_DFFLUSH_REQUIRED] = "DF_FLUSH is required", 146 [SEV_RET_INVALID_GUEST] = "Guest handle is invalid", 147 [SEV_RET_INVALID_COMMAND] = "Invalid command", 148 [SEV_RET_ACTIVE] = "Guest is active", 149 [SEV_RET_HWSEV_RET_PLATFORM] = "Hardware error", 150 [SEV_RET_HWSEV_RET_UNSAFE] = "Hardware unsafe", 151 [SEV_RET_UNSUPPORTED] = "Feature not supported", 152 [SEV_RET_INVALID_PARAM] = "Invalid parameter", 153 [SEV_RET_RESOURCE_LIMIT] = "Required firmware resource depleted", 154 [SEV_RET_SECURE_DATA_INVALID] = "Part-specific integrity check failure", 155 }; 156 157 #define SEV_FW_MAX_ERROR ARRAY_SIZE(sev_fw_errlist) 158 159 static int 160 sev_ioctl(int fd, int cmd, void *data, int *error) 161 { 162 int r; 163 struct kvm_sev_cmd input; 164 165 memset(&input, 0x0, sizeof(input)); 166 167 input.id = cmd; 168 input.sev_fd = fd; 169 input.data = (__u64)(unsigned long)data; 170 171 r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, &input); 172 173 if (error) { 174 *error = input.error; 175 } 176 177 return r; 178 } 179 180 static int 181 sev_platform_ioctl(int fd, int cmd, void *data, int *error) 182 { 183 int r; 184 struct sev_issue_cmd arg; 185 186 arg.cmd = cmd; 187 arg.data = (unsigned long)data; 188 r = ioctl(fd, SEV_ISSUE_CMD, &arg); 189 if (error) { 190 *error = arg.error; 191 } 192 193 return r; 194 } 195 196 static const char * 197 fw_error_to_str(int code) 198 { 199 if (code < 0 || code >= SEV_FW_MAX_ERROR) { 200 return "unknown error"; 201 } 202 203 return sev_fw_errlist[code]; 204 } 205 206 static bool 207 sev_check_state(const SevGuestState *sev, SevState state) 208 { 209 assert(sev); 210 return sev->state == state ? true : false; 211 } 212 213 static void 214 sev_set_guest_state(SevGuestState *sev, SevState new_state) 215 { 216 assert(new_state < SEV_STATE__MAX); 217 assert(sev); 218 219 trace_kvm_sev_change_state(SevState_str(sev->state), 220 SevState_str(new_state)); 221 sev->state = new_state; 222 } 223 224 static void 225 sev_ram_block_added(RAMBlockNotifier *n, void *host, size_t size, 226 size_t max_size) 227 { 228 int r; 229 struct kvm_enc_region range; 230 ram_addr_t offset; 231 MemoryRegion *mr; 232 233 /* 234 * The RAM device presents a memory region that should be treated 235 * as IO region and should not be pinned. 236 */ 237 mr = memory_region_from_host(host, &offset); 238 if (mr && memory_region_is_ram_device(mr)) { 239 return; 240 } 241 242 range.addr = (__u64)(unsigned long)host; 243 range.size = max_size; 244 245 trace_kvm_memcrypt_register_region(host, max_size); 246 r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_REG_REGION, &range); 247 if (r) { 248 error_report("%s: failed to register region (%p+%#zx) error '%s'", 249 __func__, host, max_size, strerror(errno)); 250 exit(1); 251 } 252 } 253 254 static void 255 sev_ram_block_removed(RAMBlockNotifier *n, void *host, size_t size, 256 size_t max_size) 257 { 258 int r; 259 struct kvm_enc_region range; 260 ram_addr_t offset; 261 MemoryRegion *mr; 262 263 /* 264 * The RAM device presents a memory region that should be treated 265 * as IO region and should not have been pinned. 266 */ 267 mr = memory_region_from_host(host, &offset); 268 if (mr && memory_region_is_ram_device(mr)) { 269 return; 270 } 271 272 range.addr = (__u64)(unsigned long)host; 273 range.size = max_size; 274 275 trace_kvm_memcrypt_unregister_region(host, max_size); 276 r = kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_UNREG_REGION, &range); 277 if (r) { 278 error_report("%s: failed to unregister region (%p+%#zx)", 279 __func__, host, max_size); 280 } 281 } 282 283 static struct RAMBlockNotifier sev_ram_notifier = { 284 .ram_block_added = sev_ram_block_added, 285 .ram_block_removed = sev_ram_block_removed, 286 }; 287 288 static void 289 sev_guest_finalize(Object *obj) 290 { 291 } 292 293 static char * 294 sev_guest_get_session_file(Object *obj, Error **errp) 295 { 296 SevGuestState *s = SEV_GUEST(obj); 297 298 return s->session_file ? g_strdup(s->session_file) : NULL; 299 } 300 301 static void 302 sev_guest_set_session_file(Object *obj, const char *value, Error **errp) 303 { 304 SevGuestState *s = SEV_GUEST(obj); 305 306 s->session_file = g_strdup(value); 307 } 308 309 static char * 310 sev_guest_get_dh_cert_file(Object *obj, Error **errp) 311 { 312 SevGuestState *s = SEV_GUEST(obj); 313 314 return g_strdup(s->dh_cert_file); 315 } 316 317 static void 318 sev_guest_set_dh_cert_file(Object *obj, const char *value, Error **errp) 319 { 320 SevGuestState *s = SEV_GUEST(obj); 321 322 s->dh_cert_file = g_strdup(value); 323 } 324 325 static char * 326 sev_guest_get_sev_device(Object *obj, Error **errp) 327 { 328 SevGuestState *sev = SEV_GUEST(obj); 329 330 return g_strdup(sev->sev_device); 331 } 332 333 static void 334 sev_guest_set_sev_device(Object *obj, const char *value, Error **errp) 335 { 336 SevGuestState *sev = SEV_GUEST(obj); 337 338 sev->sev_device = g_strdup(value); 339 } 340 341 static bool sev_guest_get_kernel_hashes(Object *obj, Error **errp) 342 { 343 SevGuestState *sev = SEV_GUEST(obj); 344 345 return sev->kernel_hashes; 346 } 347 348 static void sev_guest_set_kernel_hashes(Object *obj, bool value, Error **errp) 349 { 350 SevGuestState *sev = SEV_GUEST(obj); 351 352 sev->kernel_hashes = value; 353 } 354 355 static void 356 sev_guest_class_init(ObjectClass *oc, void *data) 357 { 358 object_class_property_add_str(oc, "sev-device", 359 sev_guest_get_sev_device, 360 sev_guest_set_sev_device); 361 object_class_property_set_description(oc, "sev-device", 362 "SEV device to use"); 363 object_class_property_add_str(oc, "dh-cert-file", 364 sev_guest_get_dh_cert_file, 365 sev_guest_set_dh_cert_file); 366 object_class_property_set_description(oc, "dh-cert-file", 367 "guest owners DH certificate (encoded with base64)"); 368 object_class_property_add_str(oc, "session-file", 369 sev_guest_get_session_file, 370 sev_guest_set_session_file); 371 object_class_property_set_description(oc, "session-file", 372 "guest owners session parameters (encoded with base64)"); 373 object_class_property_add_bool(oc, "kernel-hashes", 374 sev_guest_get_kernel_hashes, 375 sev_guest_set_kernel_hashes); 376 object_class_property_set_description(oc, "kernel-hashes", 377 "add kernel hashes to guest firmware for measured Linux boot"); 378 } 379 380 static void 381 sev_guest_instance_init(Object *obj) 382 { 383 SevGuestState *sev = SEV_GUEST(obj); 384 385 sev->sev_device = g_strdup(DEFAULT_SEV_DEVICE); 386 sev->policy = DEFAULT_GUEST_POLICY; 387 object_property_add_uint32_ptr(obj, "policy", &sev->policy, 388 OBJ_PROP_FLAG_READWRITE); 389 object_property_add_uint32_ptr(obj, "handle", &sev->handle, 390 OBJ_PROP_FLAG_READWRITE); 391 object_property_add_uint32_ptr(obj, "cbitpos", &sev->cbitpos, 392 OBJ_PROP_FLAG_READWRITE); 393 object_property_add_uint32_ptr(obj, "reduced-phys-bits", 394 &sev->reduced_phys_bits, 395 OBJ_PROP_FLAG_READWRITE); 396 } 397 398 /* sev guest info */ 399 static const TypeInfo sev_guest_info = { 400 .parent = TYPE_CONFIDENTIAL_GUEST_SUPPORT, 401 .name = TYPE_SEV_GUEST, 402 .instance_size = sizeof(SevGuestState), 403 .instance_finalize = sev_guest_finalize, 404 .class_init = sev_guest_class_init, 405 .instance_init = sev_guest_instance_init, 406 .interfaces = (InterfaceInfo[]) { 407 { TYPE_USER_CREATABLE }, 408 { } 409 } 410 }; 411 412 bool 413 sev_enabled(void) 414 { 415 return !!sev_guest; 416 } 417 418 bool 419 sev_es_enabled(void) 420 { 421 return sev_enabled() && (sev_guest->policy & SEV_POLICY_ES); 422 } 423 424 uint32_t 425 sev_get_cbit_position(void) 426 { 427 return sev_guest ? sev_guest->cbitpos : 0; 428 } 429 430 uint32_t 431 sev_get_reduced_phys_bits(void) 432 { 433 return sev_guest ? sev_guest->reduced_phys_bits : 0; 434 } 435 436 static SevInfo *sev_get_info(void) 437 { 438 SevInfo *info; 439 440 info = g_new0(SevInfo, 1); 441 info->enabled = sev_enabled(); 442 443 if (info->enabled) { 444 info->api_major = sev_guest->api_major; 445 info->api_minor = sev_guest->api_minor; 446 info->build_id = sev_guest->build_id; 447 info->policy = sev_guest->policy; 448 info->state = sev_guest->state; 449 info->handle = sev_guest->handle; 450 } 451 452 return info; 453 } 454 455 SevInfo *qmp_query_sev(Error **errp) 456 { 457 SevInfo *info; 458 459 info = sev_get_info(); 460 if (!info) { 461 error_setg(errp, "SEV feature is not available"); 462 return NULL; 463 } 464 465 return info; 466 } 467 468 void hmp_info_sev(Monitor *mon, const QDict *qdict) 469 { 470 SevInfo *info = sev_get_info(); 471 472 if (info && info->enabled) { 473 monitor_printf(mon, "handle: %d\n", info->handle); 474 monitor_printf(mon, "state: %s\n", SevState_str(info->state)); 475 monitor_printf(mon, "build: %d\n", info->build_id); 476 monitor_printf(mon, "api version: %d.%d\n", 477 info->api_major, info->api_minor); 478 monitor_printf(mon, "debug: %s\n", 479 info->policy & SEV_POLICY_NODBG ? "off" : "on"); 480 monitor_printf(mon, "key-sharing: %s\n", 481 info->policy & SEV_POLICY_NOKS ? "off" : "on"); 482 } else { 483 monitor_printf(mon, "SEV is not enabled\n"); 484 } 485 486 qapi_free_SevInfo(info); 487 } 488 489 static int 490 sev_get_pdh_info(int fd, guchar **pdh, size_t *pdh_len, guchar **cert_chain, 491 size_t *cert_chain_len, Error **errp) 492 { 493 guchar *pdh_data = NULL; 494 guchar *cert_chain_data = NULL; 495 struct sev_user_data_pdh_cert_export export = {}; 496 int err, r; 497 498 /* query the certificate length */ 499 r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err); 500 if (r < 0) { 501 if (err != SEV_RET_INVALID_LEN) { 502 error_setg(errp, "SEV: Failed to export PDH cert" 503 " ret=%d fw_err=%d (%s)", 504 r, err, fw_error_to_str(err)); 505 return 1; 506 } 507 } 508 509 pdh_data = g_new(guchar, export.pdh_cert_len); 510 cert_chain_data = g_new(guchar, export.cert_chain_len); 511 export.pdh_cert_address = (unsigned long)pdh_data; 512 export.cert_chain_address = (unsigned long)cert_chain_data; 513 514 r = sev_platform_ioctl(fd, SEV_PDH_CERT_EXPORT, &export, &err); 515 if (r < 0) { 516 error_setg(errp, "SEV: Failed to export PDH cert ret=%d fw_err=%d (%s)", 517 r, err, fw_error_to_str(err)); 518 goto e_free; 519 } 520 521 *pdh = pdh_data; 522 *pdh_len = export.pdh_cert_len; 523 *cert_chain = cert_chain_data; 524 *cert_chain_len = export.cert_chain_len; 525 return 0; 526 527 e_free: 528 g_free(pdh_data); 529 g_free(cert_chain_data); 530 return 1; 531 } 532 533 static int sev_get_cpu0_id(int fd, guchar **id, size_t *id_len, Error **errp) 534 { 535 guchar *id_data; 536 struct sev_user_data_get_id2 get_id2 = {}; 537 int err, r; 538 539 /* query the ID length */ 540 r = sev_platform_ioctl(fd, SEV_GET_ID2, &get_id2, &err); 541 if (r < 0 && err != SEV_RET_INVALID_LEN) { 542 error_setg(errp, "SEV: Failed to get ID ret=%d fw_err=%d (%s)", 543 r, err, fw_error_to_str(err)); 544 return 1; 545 } 546 547 id_data = g_new(guchar, get_id2.length); 548 get_id2.address = (unsigned long)id_data; 549 550 r = sev_platform_ioctl(fd, SEV_GET_ID2, &get_id2, &err); 551 if (r < 0) { 552 error_setg(errp, "SEV: Failed to get ID ret=%d fw_err=%d (%s)", 553 r, err, fw_error_to_str(err)); 554 goto err; 555 } 556 557 *id = id_data; 558 *id_len = get_id2.length; 559 return 0; 560 561 err: 562 g_free(id_data); 563 return 1; 564 } 565 566 static SevCapability *sev_get_capabilities(Error **errp) 567 { 568 SevCapability *cap = NULL; 569 guchar *pdh_data = NULL; 570 guchar *cert_chain_data = NULL; 571 guchar *cpu0_id_data = NULL; 572 size_t pdh_len = 0, cert_chain_len = 0, cpu0_id_len = 0; 573 uint32_t ebx; 574 int fd; 575 576 if (!kvm_enabled()) { 577 error_setg(errp, "KVM not enabled"); 578 return NULL; 579 } 580 if (kvm_vm_ioctl(kvm_state, KVM_MEMORY_ENCRYPT_OP, NULL) < 0) { 581 error_setg(errp, "SEV is not enabled in KVM"); 582 return NULL; 583 } 584 585 fd = open(DEFAULT_SEV_DEVICE, O_RDWR); 586 if (fd < 0) { 587 error_setg_errno(errp, errno, "SEV: Failed to open %s", 588 DEFAULT_SEV_DEVICE); 589 return NULL; 590 } 591 592 if (sev_get_pdh_info(fd, &pdh_data, &pdh_len, 593 &cert_chain_data, &cert_chain_len, errp)) { 594 goto out; 595 } 596 597 if (sev_get_cpu0_id(fd, &cpu0_id_data, &cpu0_id_len, errp)) { 598 goto out; 599 } 600 601 cap = g_new0(SevCapability, 1); 602 cap->pdh = g_base64_encode(pdh_data, pdh_len); 603 cap->cert_chain = g_base64_encode(cert_chain_data, cert_chain_len); 604 cap->cpu0_id = g_base64_encode(cpu0_id_data, cpu0_id_len); 605 606 host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL); 607 cap->cbitpos = ebx & 0x3f; 608 609 /* 610 * When SEV feature is enabled, we loose one bit in guest physical 611 * addressing. 612 */ 613 cap->reduced_phys_bits = 1; 614 615 out: 616 g_free(cpu0_id_data); 617 g_free(pdh_data); 618 g_free(cert_chain_data); 619 close(fd); 620 return cap; 621 } 622 623 SevCapability *qmp_query_sev_capabilities(Error **errp) 624 { 625 return sev_get_capabilities(errp); 626 } 627 628 static SevAttestationReport *sev_get_attestation_report(const char *mnonce, 629 Error **errp) 630 { 631 struct kvm_sev_attestation_report input = {}; 632 SevAttestationReport *report = NULL; 633 SevGuestState *sev = sev_guest; 634 g_autofree guchar *data = NULL; 635 g_autofree guchar *buf = NULL; 636 gsize len; 637 int err = 0, ret; 638 639 if (!sev_enabled()) { 640 error_setg(errp, "SEV is not enabled"); 641 return NULL; 642 } 643 644 /* lets decode the mnonce string */ 645 buf = g_base64_decode(mnonce, &len); 646 if (!buf) { 647 error_setg(errp, "SEV: failed to decode mnonce input"); 648 return NULL; 649 } 650 651 /* verify the input mnonce length */ 652 if (len != sizeof(input.mnonce)) { 653 error_setg(errp, "SEV: mnonce must be %zu bytes (got %" G_GSIZE_FORMAT ")", 654 sizeof(input.mnonce), len); 655 return NULL; 656 } 657 658 /* Query the report length */ 659 ret = sev_ioctl(sev->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT, 660 &input, &err); 661 if (ret < 0) { 662 if (err != SEV_RET_INVALID_LEN) { 663 error_setg(errp, "SEV: Failed to query the attestation report" 664 " length ret=%d fw_err=%d (%s)", 665 ret, err, fw_error_to_str(err)); 666 return NULL; 667 } 668 } 669 670 data = g_malloc(input.len); 671 input.uaddr = (unsigned long)data; 672 memcpy(input.mnonce, buf, sizeof(input.mnonce)); 673 674 /* Query the report */ 675 ret = sev_ioctl(sev->sev_fd, KVM_SEV_GET_ATTESTATION_REPORT, 676 &input, &err); 677 if (ret) { 678 error_setg_errno(errp, errno, "SEV: Failed to get attestation report" 679 " ret=%d fw_err=%d (%s)", ret, err, fw_error_to_str(err)); 680 return NULL; 681 } 682 683 report = g_new0(SevAttestationReport, 1); 684 report->data = g_base64_encode(data, input.len); 685 686 trace_kvm_sev_attestation_report(mnonce, report->data); 687 688 return report; 689 } 690 691 SevAttestationReport *qmp_query_sev_attestation_report(const char *mnonce, 692 Error **errp) 693 { 694 return sev_get_attestation_report(mnonce, errp); 695 } 696 697 static int 698 sev_read_file_base64(const char *filename, guchar **data, gsize *len) 699 { 700 gsize sz; 701 g_autofree gchar *base64 = NULL; 702 GError *error = NULL; 703 704 if (!g_file_get_contents(filename, &base64, &sz, &error)) { 705 error_report("SEV: Failed to read '%s' (%s)", filename, error->message); 706 g_error_free(error); 707 return -1; 708 } 709 710 *data = g_base64_decode(base64, len); 711 return 0; 712 } 713 714 static int 715 sev_launch_start(SevGuestState *sev) 716 { 717 gsize sz; 718 int ret = 1; 719 int fw_error, rc; 720 struct kvm_sev_launch_start start = { 721 .handle = sev->handle, .policy = sev->policy 722 }; 723 guchar *session = NULL, *dh_cert = NULL; 724 725 if (sev->session_file) { 726 if (sev_read_file_base64(sev->session_file, &session, &sz) < 0) { 727 goto out; 728 } 729 start.session_uaddr = (unsigned long)session; 730 start.session_len = sz; 731 } 732 733 if (sev->dh_cert_file) { 734 if (sev_read_file_base64(sev->dh_cert_file, &dh_cert, &sz) < 0) { 735 goto out; 736 } 737 start.dh_uaddr = (unsigned long)dh_cert; 738 start.dh_len = sz; 739 } 740 741 trace_kvm_sev_launch_start(start.policy, session, dh_cert); 742 rc = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_START, &start, &fw_error); 743 if (rc < 0) { 744 error_report("%s: LAUNCH_START ret=%d fw_error=%d '%s'", 745 __func__, ret, fw_error, fw_error_to_str(fw_error)); 746 goto out; 747 } 748 749 sev_set_guest_state(sev, SEV_STATE_LAUNCH_UPDATE); 750 sev->handle = start.handle; 751 ret = 0; 752 753 out: 754 g_free(session); 755 g_free(dh_cert); 756 return ret; 757 } 758 759 static int 760 sev_launch_update_data(SevGuestState *sev, uint8_t *addr, uint64_t len) 761 { 762 int ret, fw_error; 763 struct kvm_sev_launch_update_data update; 764 765 if (!addr || !len) { 766 return 1; 767 } 768 769 update.uaddr = (__u64)(unsigned long)addr; 770 update.len = len; 771 trace_kvm_sev_launch_update_data(addr, len); 772 ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_UPDATE_DATA, 773 &update, &fw_error); 774 if (ret) { 775 error_report("%s: LAUNCH_UPDATE ret=%d fw_error=%d '%s'", 776 __func__, ret, fw_error, fw_error_to_str(fw_error)); 777 } 778 779 return ret; 780 } 781 782 static int 783 sev_launch_update_vmsa(SevGuestState *sev) 784 { 785 int ret, fw_error; 786 787 ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_UPDATE_VMSA, NULL, &fw_error); 788 if (ret) { 789 error_report("%s: LAUNCH_UPDATE_VMSA ret=%d fw_error=%d '%s'", 790 __func__, ret, fw_error, fw_error_to_str(fw_error)); 791 } 792 793 return ret; 794 } 795 796 static void 797 sev_launch_get_measure(Notifier *notifier, void *unused) 798 { 799 SevGuestState *sev = sev_guest; 800 int ret, error; 801 g_autofree guchar *data = NULL; 802 struct kvm_sev_launch_measure measurement = {}; 803 804 if (!sev_check_state(sev, SEV_STATE_LAUNCH_UPDATE)) { 805 return; 806 } 807 808 if (sev_es_enabled()) { 809 /* measure all the VM save areas before getting launch_measure */ 810 ret = sev_launch_update_vmsa(sev); 811 if (ret) { 812 exit(1); 813 } 814 } 815 816 /* query the measurement blob length */ 817 ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_MEASURE, 818 &measurement, &error); 819 if (!measurement.len) { 820 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'", 821 __func__, ret, error, fw_error_to_str(errno)); 822 return; 823 } 824 825 data = g_new0(guchar, measurement.len); 826 measurement.uaddr = (unsigned long)data; 827 828 /* get the measurement blob */ 829 ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_MEASURE, 830 &measurement, &error); 831 if (ret) { 832 error_report("%s: LAUNCH_MEASURE ret=%d fw_error=%d '%s'", 833 __func__, ret, error, fw_error_to_str(errno)); 834 return; 835 } 836 837 sev_set_guest_state(sev, SEV_STATE_LAUNCH_SECRET); 838 839 /* encode the measurement value and emit the event */ 840 sev->measurement = g_base64_encode(data, measurement.len); 841 trace_kvm_sev_launch_measurement(sev->measurement); 842 } 843 844 static char *sev_get_launch_measurement(void) 845 { 846 if (sev_guest && 847 sev_guest->state >= SEV_STATE_LAUNCH_SECRET) { 848 return g_strdup(sev_guest->measurement); 849 } 850 851 return NULL; 852 } 853 854 SevLaunchMeasureInfo *qmp_query_sev_launch_measure(Error **errp) 855 { 856 char *data; 857 SevLaunchMeasureInfo *info; 858 859 data = sev_get_launch_measurement(); 860 if (!data) { 861 error_setg(errp, "SEV launch measurement is not available"); 862 return NULL; 863 } 864 865 info = g_malloc0(sizeof(*info)); 866 info->data = data; 867 868 return info; 869 } 870 871 static Notifier sev_machine_done_notify = { 872 .notify = sev_launch_get_measure, 873 }; 874 875 static void 876 sev_launch_finish(SevGuestState *sev) 877 { 878 int ret, error; 879 880 trace_kvm_sev_launch_finish(); 881 ret = sev_ioctl(sev->sev_fd, KVM_SEV_LAUNCH_FINISH, 0, &error); 882 if (ret) { 883 error_report("%s: LAUNCH_FINISH ret=%d fw_error=%d '%s'", 884 __func__, ret, error, fw_error_to_str(error)); 885 exit(1); 886 } 887 888 sev_set_guest_state(sev, SEV_STATE_RUNNING); 889 890 /* add migration blocker */ 891 error_setg(&sev_mig_blocker, 892 "SEV: Migration is not implemented"); 893 migrate_add_blocker(sev_mig_blocker, &error_fatal); 894 } 895 896 static void 897 sev_vm_state_change(void *opaque, bool running, RunState state) 898 { 899 SevGuestState *sev = opaque; 900 901 if (running) { 902 if (!sev_check_state(sev, SEV_STATE_RUNNING)) { 903 sev_launch_finish(sev); 904 } 905 } 906 } 907 908 int sev_kvm_init(ConfidentialGuestSupport *cgs, Error **errp) 909 { 910 SevGuestState *sev 911 = (SevGuestState *)object_dynamic_cast(OBJECT(cgs), TYPE_SEV_GUEST); 912 char *devname; 913 int ret, fw_error, cmd; 914 uint32_t ebx; 915 uint32_t host_cbitpos; 916 struct sev_user_data_status status = {}; 917 918 if (!sev) { 919 return 0; 920 } 921 922 ret = ram_block_discard_disable(true); 923 if (ret) { 924 error_report("%s: cannot disable RAM discard", __func__); 925 return -1; 926 } 927 928 sev_guest = sev; 929 sev->state = SEV_STATE_UNINIT; 930 931 host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL); 932 host_cbitpos = ebx & 0x3f; 933 934 if (host_cbitpos != sev->cbitpos) { 935 error_setg(errp, "%s: cbitpos check failed, host '%d' requested '%d'", 936 __func__, host_cbitpos, sev->cbitpos); 937 goto err; 938 } 939 940 if (sev->reduced_phys_bits < 1) { 941 error_setg(errp, "%s: reduced_phys_bits check failed, it should be >=1," 942 " requested '%d'", __func__, sev->reduced_phys_bits); 943 goto err; 944 } 945 946 devname = object_property_get_str(OBJECT(sev), "sev-device", NULL); 947 sev->sev_fd = open(devname, O_RDWR); 948 if (sev->sev_fd < 0) { 949 error_setg(errp, "%s: Failed to open %s '%s'", __func__, 950 devname, strerror(errno)); 951 g_free(devname); 952 goto err; 953 } 954 g_free(devname); 955 956 ret = sev_platform_ioctl(sev->sev_fd, SEV_PLATFORM_STATUS, &status, 957 &fw_error); 958 if (ret) { 959 error_setg(errp, "%s: failed to get platform status ret=%d " 960 "fw_error='%d: %s'", __func__, ret, fw_error, 961 fw_error_to_str(fw_error)); 962 goto err; 963 } 964 sev->build_id = status.build; 965 sev->api_major = status.api_major; 966 sev->api_minor = status.api_minor; 967 968 if (sev_es_enabled()) { 969 if (!kvm_kernel_irqchip_allowed()) { 970 error_report("%s: SEV-ES guests require in-kernel irqchip support", 971 __func__); 972 goto err; 973 } 974 975 if (!(status.flags & SEV_STATUS_FLAGS_CONFIG_ES)) { 976 error_report("%s: guest policy requires SEV-ES, but " 977 "host SEV-ES support unavailable", 978 __func__); 979 goto err; 980 } 981 cmd = KVM_SEV_ES_INIT; 982 } else { 983 cmd = KVM_SEV_INIT; 984 } 985 986 trace_kvm_sev_init(); 987 ret = sev_ioctl(sev->sev_fd, cmd, NULL, &fw_error); 988 if (ret) { 989 error_setg(errp, "%s: failed to initialize ret=%d fw_error=%d '%s'", 990 __func__, ret, fw_error, fw_error_to_str(fw_error)); 991 goto err; 992 } 993 994 ret = sev_launch_start(sev); 995 if (ret) { 996 error_setg(errp, "%s: failed to create encryption context", __func__); 997 goto err; 998 } 999 1000 ram_block_notifier_add(&sev_ram_notifier); 1001 qemu_add_machine_init_done_notifier(&sev_machine_done_notify); 1002 qemu_add_vm_change_state_handler(sev_vm_state_change, sev); 1003 1004 cgs->ready = true; 1005 1006 return 0; 1007 err: 1008 sev_guest = NULL; 1009 ram_block_discard_disable(false); 1010 return -1; 1011 } 1012 1013 int 1014 sev_encrypt_flash(uint8_t *ptr, uint64_t len, Error **errp) 1015 { 1016 if (!sev_guest) { 1017 return 0; 1018 } 1019 1020 /* if SEV is in update state then encrypt the data else do nothing */ 1021 if (sev_check_state(sev_guest, SEV_STATE_LAUNCH_UPDATE)) { 1022 int ret = sev_launch_update_data(sev_guest, ptr, len); 1023 if (ret < 0) { 1024 error_setg(errp, "SEV: Failed to encrypt pflash rom"); 1025 return ret; 1026 } 1027 } 1028 1029 return 0; 1030 } 1031 1032 int sev_inject_launch_secret(const char *packet_hdr, const char *secret, 1033 uint64_t gpa, Error **errp) 1034 { 1035 struct kvm_sev_launch_secret input; 1036 g_autofree guchar *data = NULL, *hdr = NULL; 1037 int error, ret = 1; 1038 void *hva; 1039 gsize hdr_sz = 0, data_sz = 0; 1040 MemoryRegion *mr = NULL; 1041 1042 if (!sev_guest) { 1043 error_setg(errp, "SEV not enabled for guest"); 1044 return 1; 1045 } 1046 1047 /* secret can be injected only in this state */ 1048 if (!sev_check_state(sev_guest, SEV_STATE_LAUNCH_SECRET)) { 1049 error_setg(errp, "SEV: Not in correct state. (LSECRET) %x", 1050 sev_guest->state); 1051 return 1; 1052 } 1053 1054 hdr = g_base64_decode(packet_hdr, &hdr_sz); 1055 if (!hdr || !hdr_sz) { 1056 error_setg(errp, "SEV: Failed to decode sequence header"); 1057 return 1; 1058 } 1059 1060 data = g_base64_decode(secret, &data_sz); 1061 if (!data || !data_sz) { 1062 error_setg(errp, "SEV: Failed to decode data"); 1063 return 1; 1064 } 1065 1066 hva = gpa2hva(&mr, gpa, data_sz, errp); 1067 if (!hva) { 1068 error_prepend(errp, "SEV: Failed to calculate guest address: "); 1069 return 1; 1070 } 1071 1072 input.hdr_uaddr = (uint64_t)(unsigned long)hdr; 1073 input.hdr_len = hdr_sz; 1074 1075 input.trans_uaddr = (uint64_t)(unsigned long)data; 1076 input.trans_len = data_sz; 1077 1078 input.guest_uaddr = (uint64_t)(unsigned long)hva; 1079 input.guest_len = data_sz; 1080 1081 trace_kvm_sev_launch_secret(gpa, input.guest_uaddr, 1082 input.trans_uaddr, input.trans_len); 1083 1084 ret = sev_ioctl(sev_guest->sev_fd, KVM_SEV_LAUNCH_SECRET, 1085 &input, &error); 1086 if (ret) { 1087 error_setg(errp, "SEV: failed to inject secret ret=%d fw_error=%d '%s'", 1088 ret, error, fw_error_to_str(error)); 1089 return ret; 1090 } 1091 1092 return 0; 1093 } 1094 1095 #define SEV_SECRET_GUID "4c2eb361-7d9b-4cc3-8081-127c90d3d294" 1096 struct sev_secret_area { 1097 uint32_t base; 1098 uint32_t size; 1099 }; 1100 1101 void qmp_sev_inject_launch_secret(const char *packet_hdr, 1102 const char *secret, 1103 bool has_gpa, uint64_t gpa, 1104 Error **errp) 1105 { 1106 if (!sev_enabled()) { 1107 error_setg(errp, "SEV not enabled for guest"); 1108 return; 1109 } 1110 if (!has_gpa) { 1111 uint8_t *data; 1112 struct sev_secret_area *area; 1113 1114 if (!pc_system_ovmf_table_find(SEV_SECRET_GUID, &data, NULL)) { 1115 error_setg(errp, "SEV: no secret area found in OVMF," 1116 " gpa must be specified."); 1117 return; 1118 } 1119 area = (struct sev_secret_area *)data; 1120 gpa = area->base; 1121 } 1122 1123 sev_inject_launch_secret(packet_hdr, secret, gpa, errp); 1124 } 1125 1126 static int 1127 sev_es_parse_reset_block(SevInfoBlock *info, uint32_t *addr) 1128 { 1129 if (!info->reset_addr) { 1130 error_report("SEV-ES reset address is zero"); 1131 return 1; 1132 } 1133 1134 *addr = info->reset_addr; 1135 1136 return 0; 1137 } 1138 1139 static int 1140 sev_es_find_reset_vector(void *flash_ptr, uint64_t flash_size, 1141 uint32_t *addr) 1142 { 1143 QemuUUID info_guid, *guid; 1144 SevInfoBlock *info; 1145 uint8_t *data; 1146 uint16_t *len; 1147 1148 /* 1149 * Initialize the address to zero. An address of zero with a successful 1150 * return code indicates that SEV-ES is not active. 1151 */ 1152 *addr = 0; 1153 1154 /* 1155 * Extract the AP reset vector for SEV-ES guests by locating the SEV GUID. 1156 * The SEV GUID is located on its own (original implementation) or within 1157 * the Firmware GUID Table (new implementation), either of which are 1158 * located 32 bytes from the end of the flash. 1159 * 1160 * Check the Firmware GUID Table first. 1161 */ 1162 if (pc_system_ovmf_table_find(SEV_INFO_BLOCK_GUID, &data, NULL)) { 1163 return sev_es_parse_reset_block((SevInfoBlock *)data, addr); 1164 } 1165 1166 /* 1167 * SEV info block not found in the Firmware GUID Table (or there isn't 1168 * a Firmware GUID Table), fall back to the original implementation. 1169 */ 1170 data = flash_ptr + flash_size - 0x20; 1171 1172 qemu_uuid_parse(SEV_INFO_BLOCK_GUID, &info_guid); 1173 info_guid = qemu_uuid_bswap(info_guid); /* GUIDs are LE */ 1174 1175 guid = (QemuUUID *)(data - sizeof(info_guid)); 1176 if (!qemu_uuid_is_equal(guid, &info_guid)) { 1177 error_report("SEV information block/Firmware GUID Table block not found in pflash rom"); 1178 return 1; 1179 } 1180 1181 len = (uint16_t *)((uint8_t *)guid - sizeof(*len)); 1182 info = (SevInfoBlock *)(data - le16_to_cpu(*len)); 1183 1184 return sev_es_parse_reset_block(info, addr); 1185 } 1186 1187 void sev_es_set_reset_vector(CPUState *cpu) 1188 { 1189 X86CPU *x86; 1190 CPUX86State *env; 1191 1192 /* Only update if we have valid reset information */ 1193 if (!sev_guest || !sev_guest->reset_data_valid) { 1194 return; 1195 } 1196 1197 /* Do not update the BSP reset state */ 1198 if (cpu->cpu_index == 0) { 1199 return; 1200 } 1201 1202 x86 = X86_CPU(cpu); 1203 env = &x86->env; 1204 1205 cpu_x86_load_seg_cache(env, R_CS, 0xf000, sev_guest->reset_cs, 0xffff, 1206 DESC_P_MASK | DESC_S_MASK | DESC_CS_MASK | 1207 DESC_R_MASK | DESC_A_MASK); 1208 1209 env->eip = sev_guest->reset_ip; 1210 } 1211 1212 int sev_es_save_reset_vector(void *flash_ptr, uint64_t flash_size) 1213 { 1214 CPUState *cpu; 1215 uint32_t addr; 1216 int ret; 1217 1218 if (!sev_es_enabled()) { 1219 return 0; 1220 } 1221 1222 addr = 0; 1223 ret = sev_es_find_reset_vector(flash_ptr, flash_size, 1224 &addr); 1225 if (ret) { 1226 return ret; 1227 } 1228 1229 if (addr) { 1230 sev_guest->reset_cs = addr & 0xffff0000; 1231 sev_guest->reset_ip = addr & 0x0000ffff; 1232 sev_guest->reset_data_valid = true; 1233 1234 CPU_FOREACH(cpu) { 1235 sev_es_set_reset_vector(cpu); 1236 } 1237 } 1238 1239 return 0; 1240 } 1241 1242 static const QemuUUID sev_hash_table_header_guid = { 1243 .data = UUID_LE(0x9438d606, 0x4f22, 0x4cc9, 0xb4, 0x79, 0xa7, 0x93, 1244 0xd4, 0x11, 0xfd, 0x21) 1245 }; 1246 1247 static const QemuUUID sev_kernel_entry_guid = { 1248 .data = UUID_LE(0x4de79437, 0xabd2, 0x427f, 0xb8, 0x35, 0xd5, 0xb1, 1249 0x72, 0xd2, 0x04, 0x5b) 1250 }; 1251 static const QemuUUID sev_initrd_entry_guid = { 1252 .data = UUID_LE(0x44baf731, 0x3a2f, 0x4bd7, 0x9a, 0xf1, 0x41, 0xe2, 1253 0x91, 0x69, 0x78, 0x1d) 1254 }; 1255 static const QemuUUID sev_cmdline_entry_guid = { 1256 .data = UUID_LE(0x97d02dd8, 0xbd20, 0x4c94, 0xaa, 0x78, 0xe7, 0x71, 1257 0x4d, 0x36, 0xab, 0x2a) 1258 }; 1259 1260 /* 1261 * Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page 1262 * which is included in SEV's initial memory measurement. 1263 */ 1264 bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp) 1265 { 1266 uint8_t *data; 1267 SevHashTableDescriptor *area; 1268 SevHashTable *ht; 1269 PaddedSevHashTable *padded_ht; 1270 uint8_t cmdline_hash[HASH_SIZE]; 1271 uint8_t initrd_hash[HASH_SIZE]; 1272 uint8_t kernel_hash[HASH_SIZE]; 1273 uint8_t *hashp; 1274 size_t hash_len = HASH_SIZE; 1275 hwaddr mapped_len = sizeof(*padded_ht); 1276 MemTxAttrs attrs = { 0 }; 1277 bool ret = true; 1278 1279 /* 1280 * Only add the kernel hashes if the sev-guest configuration explicitly 1281 * stated kernel-hashes=on. 1282 */ 1283 if (!sev_guest->kernel_hashes) { 1284 return false; 1285 } 1286 1287 if (!pc_system_ovmf_table_find(SEV_HASH_TABLE_RV_GUID, &data, NULL)) { 1288 error_setg(errp, "SEV: kernel specified but guest firmware " 1289 "has no hashes table GUID"); 1290 return false; 1291 } 1292 area = (SevHashTableDescriptor *)data; 1293 if (!area->base || area->size < sizeof(PaddedSevHashTable)) { 1294 error_setg(errp, "SEV: guest firmware hashes table area is invalid " 1295 "(base=0x%x size=0x%x)", area->base, area->size); 1296 return false; 1297 } 1298 1299 /* 1300 * Calculate hash of kernel command-line with the terminating null byte. If 1301 * the user doesn't supply a command-line via -append, the 1-byte "\0" will 1302 * be used. 1303 */ 1304 hashp = cmdline_hash; 1305 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA256, ctx->cmdline_data, 1306 ctx->cmdline_size, &hashp, &hash_len, errp) < 0) { 1307 return false; 1308 } 1309 assert(hash_len == HASH_SIZE); 1310 1311 /* 1312 * Calculate hash of initrd. If the user doesn't supply an initrd via 1313 * -initrd, an empty buffer will be used (ctx->initrd_size == 0). 1314 */ 1315 hashp = initrd_hash; 1316 if (qcrypto_hash_bytes(QCRYPTO_HASH_ALG_SHA256, ctx->initrd_data, 1317 ctx->initrd_size, &hashp, &hash_len, errp) < 0) { 1318 return false; 1319 } 1320 assert(hash_len == HASH_SIZE); 1321 1322 /* Calculate hash of the kernel */ 1323 hashp = kernel_hash; 1324 struct iovec iov[2] = { 1325 { .iov_base = ctx->setup_data, .iov_len = ctx->setup_size }, 1326 { .iov_base = ctx->kernel_data, .iov_len = ctx->kernel_size } 1327 }; 1328 if (qcrypto_hash_bytesv(QCRYPTO_HASH_ALG_SHA256, iov, ARRAY_SIZE(iov), 1329 &hashp, &hash_len, errp) < 0) { 1330 return false; 1331 } 1332 assert(hash_len == HASH_SIZE); 1333 1334 /* 1335 * Populate the hashes table in the guest's memory at the OVMF-designated 1336 * area for the SEV hashes table 1337 */ 1338 padded_ht = address_space_map(&address_space_memory, area->base, 1339 &mapped_len, true, attrs); 1340 if (!padded_ht || mapped_len != sizeof(*padded_ht)) { 1341 error_setg(errp, "SEV: cannot map hashes table guest memory area"); 1342 return false; 1343 } 1344 ht = &padded_ht->ht; 1345 1346 ht->guid = sev_hash_table_header_guid; 1347 ht->len = sizeof(*ht); 1348 1349 ht->cmdline.guid = sev_cmdline_entry_guid; 1350 ht->cmdline.len = sizeof(ht->cmdline); 1351 memcpy(ht->cmdline.hash, cmdline_hash, sizeof(ht->cmdline.hash)); 1352 1353 ht->initrd.guid = sev_initrd_entry_guid; 1354 ht->initrd.len = sizeof(ht->initrd); 1355 memcpy(ht->initrd.hash, initrd_hash, sizeof(ht->initrd.hash)); 1356 1357 ht->kernel.guid = sev_kernel_entry_guid; 1358 ht->kernel.len = sizeof(ht->kernel); 1359 memcpy(ht->kernel.hash, kernel_hash, sizeof(ht->kernel.hash)); 1360 1361 /* zero the excess data so the measurement can be reliably calculated */ 1362 memset(padded_ht->padding, 0, sizeof(padded_ht->padding)); 1363 1364 if (sev_encrypt_flash((uint8_t *)padded_ht, sizeof(*padded_ht), errp) < 0) { 1365 ret = false; 1366 } 1367 1368 address_space_unmap(&address_space_memory, padded_ht, 1369 mapped_len, true, mapped_len); 1370 1371 return ret; 1372 } 1373 1374 static void 1375 sev_register_types(void) 1376 { 1377 type_register_static(&sev_guest_info); 1378 } 1379 1380 type_init(sev_register_types); 1381