i386/sev: Support measured direct kernel boot on SNP
v3 cover i386/sev: Support measured direct kernel boot on SNP
(This RFC patch series is based on AMD's RFC upmv10-snpv3 tree [1]. So far that RFC tree was not submitted for review in qemu-devel.)
In order to enable measured direct kernel boot on SNP, QEMU needs to fill the hashes page when kernel-hashes=on. This relies on several changes to the SNP metadata published by OVMF (See [2] for proposed OVMF patches).
Patch 1 pulls the 'kernel-hashes' property from the SEV guest settings to the common settings to make it available for both SEV and SNP.
Patch 2 and 3 refactor the code in target/i386/sev.c to allow easier change in patch 4.
Patch 4 adds the hashes table for SNP guests (or validates the page as a zero page if kernel-hashes=off).
This patch series is also available at [3].
[1] https://github.com/mdroth/qemu/commits/upmv10-snpv3
[2] https://edk2.groups.io/g/devel/message/100625
[3] https://github.com/confidential-containers-demo/qemu/tree/snp-kernel-hashes-v3
v3:
* Separate refactoring to their own patches.
* Use a new SNP metadata entry which indicates kernel hashes page; during pre-validation, either encrypt the page with the kernel hashes or mark it as zero (thanks Tom).
v2: https://lore.kernel.org/qemu-devel/[email protected]/
v2 changes:
* Rebase on top of upmv10-snpv3 which includes kernel-hashes.
v1: https://lore.kernel.org/qemu-devel/20220329064038.96006-1-dovmurik%40linux.ibm.com/
Dov Murik (4):
qapi, i386: Move kernel-hashes to SevCommonProperties i386/sev: Extract build_kernel_loader_hashes i386/sev: Reorder struct declarations i386/sev: Allow measured direct kernel boot on SNP
qapi/qom.json | 12 +-- include/hw/i386/pc.h | 2 + target/i386/sev.c | 238 +++++++++++++++++++++++++------------------ 3 files changed, 146 insertions(+), 106 deletions(-)
Signature · 1 line
2.25.1
RFC PATCH v3 1/4 qapi, i386: Move kernel-hashes to SevCommonProperties
In order to enable kernel-hashes for SNP, pull it from SevGuestProperties to its parent SevCommonProperties so it will be available for both SEV and SNP.
--- qapi/qom.json | 12 ++++++------ target/i386/sev.c | 44 ++++++++++++++++++-------------------------- 2 files changed, 24 insertions(+), 32 deletions(-)
qapi/qom.json +6 -6
@@ -836,6 +836,10 @@ # @reduced-phys-bits: number of bits in physical addresses that become # unavailable when SEV is enabled # +# @kernel-hashes: if true, add hashes of kernel/initrd/cmdline to a +# designated guest firmware page for measured boot +# with -kernel (default: false) (since 6.2) +# # @upm-mode: configure Unmapped Private Memory mode # # @discard: configure how discarding is handled for memory after
@@ -848,6 +852,7 @@ 'data': { '*sev-device': 'str', '*cbitpos': 'uint32', 'reduced-phys-bits': 'uint32', + '*kernel-hashes': 'bool', '*upm-mode': 'bool', '*discard': 'str' } }
@@ -864,10 +869,6 @@ # # @handle: SEV firmware handle (default: 0) # -# @kernel-hashes: if true, add hashes of kernel/initrd/cmdline to a -# designated guest firmware page for measured boot -# with -kernel (default: false) (since 6.2) -# # Since: 2.12 ## { 'struct': 'SevGuestProperties',
@@ -875,8 +876,7 @@ 'data': { '*dh-cert-file': 'str', '*session-file': 'str', '*policy': 'uint32', - '*handle': 'uint32', - '*kernel-hashes': 'bool' } } + '*handle': 'uint32' } } ## # @SevSnpGuestProperties:
target/i386/sev.c +18 -26
90 lines not on this page · raw message
Signature · 1 line
2.25.1
RFC PATCH v3 2/4 i386/sev: Extract build_kernel_loader_hashes
In reply to Dov MurikExtract the building of the kernel hashes table out from sev_add_kernel_loader_hashes() to allow building it in other memory areas (for SNP support).
No functional change intended.
--- target/i386/sev.c | 110 ++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 48 deletions(-)
target/i386/sev.c +62 -48
@@ -2065,50 +2065,16 @@ static const QemuUUID sev_cmdline_entry_guid = { 0x4d, 0x36, 0xab, 0x2a) }; -/* -* Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page -* which is included in SEV's initial memory measurement. -*/ -bool sev_add_kernel_loader_hashes (SevKernelLoaderContext *ctx,Error **errp) +static bool build_kernel_loader_hashes (PaddedSevHashTable *padded_ht, +SevKernelLoaderContext *ctx, +Error **errp) { -uint8_t *data; -SevHashTableDescriptor *area;SevHashTable *ht; -PaddedSevHashTable *padded_ht;uint8_t cmdline_hash[HASH_SIZE ];uint8_t initrd_hash[HASH_SIZE ];uint8_t kernel_hash[HASH_SIZE ];uint8_t *hashp;size_t hash_len =HASH_SIZE ; -hwaddr mapped_len =sizeof (*padded_ht); -MemTxAttrs attrs = {0 }; -bool ret = true; -SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); - -/* -* Only add the kernel hashes if the sev-guest configuration explicitly -* stated kernel-hashes=on. -*/ -if (!sev_common->kernel_hashes) { -return false; - } - -if (!pc_system_ovmf_table_find (SEV_HASH_TABLE_RV_GUID , &data,NULL )) { -error_setg (errp,"SEV: kernel specified but guest firmware " -"has no hashes table GUID" ); -return false; - } - -if (sev_snp_enabled ()) { -return false; - } - - area = (SevHashTableDescriptor *)data; -if (!area->base || area->size <sizeof (PaddedSevHashTable)) { -error_setg (errp,"SEV: guest firmware hashes table area is invalid " -"(base=0x%x size=0x%x)" , area->base, area->size); -return false; - }/* * Calculate hash of kernel command-line with the terminating null byte. If
@@ -2145,16 +2111,6 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp) }assert (hash_len ==HASH_SIZE ); -/* -* Populate the hashes table in the guest's memory at the OVMF-designated -* area for the SEV hashes table -*/ - padded_ht =address_space_map (&address_space_memory, area->base, - &mapped_len, true, attrs); -if (!padded_ht || mapped_len !=sizeof (*padded_ht)) { -error_setg (errp,"SEV: cannot map hashes table guest memory area" ); -return false; - } ht = &padded_ht->ht; ht->guid = sev_hash_table_header_guid;
@@ -2175,7 +2131,65 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)/* zero the excess data so the measurement can be reliably calculated */ memset (padded_ht->padding,0 ,sizeof (padded_ht->padding)); -if (sev_encrypt_flash (area->base, (uint8_t *)padded_ht,sizeof (*padded_ht), errp) <0 ) { +return true; +} + +/* +* Add the hashes of the linux kernel/initrd/cmdline to an encrypted guest page +* which is included in SEV's initial memory measurement. +*/ +bool sev_add_kernel_loader_hashes (SevKernelLoaderContext *ctx,Error **errp) +{ +uint8_t *data; +SevHashTableDescriptor *area; +PaddedSevHashTable *padded_ht; +hwaddr mapped_len =sizeof (*padded_ht); +MemTxAttrs attrs = {0 }; +bool ret = true; +SevCommonState *sev_common =SEV_COMMON (MACHINE (qdev_get_machine ())->cgs); + +/* +* Only add the kernel hashes if the sev-guest configuration explicitly +* stated kernel-hashes=on. +*/ +if (!sev_common->kernel_hashes) { +return false; + } + +if (!pc_system_ovmf_table_find (SEV_HASH_TABLE_RV_GUID , &data,NULL )) { +error_setg (errp,"SEV: kernel specified but guest firmware " +"has no hashes table GUID" ); +return false; + } + +if (sev_snp_enabled ()) { +return false; + } + + area = (SevHashTableDescriptor *)data; +if (!area->base || area->size <sizeof (PaddedSevHashTable)) { +error_setg (errp,"SEV: guest firmware hashes table area is invalid " +"(base=0x%x size=0x%x)" , area->base, area->size); +return false; + } + +/* +* Populate the hashes table in the guest's memory at the OVMF-designated +* area for the SEV hashes table +*/ + padded_ht =address_space_map (&address_space_memory, area->base, + &mapped_len, true, attrs); +if (!padded_ht || mapped_len !=sizeof (*padded_ht)) { +error_setg (errp,"SEV: cannot map hashes table guest memory area" ); +return false; + } + +if (build_kernel_loader_hashes (padded_ht, ctx, errp)) { +if (sev_encrypt_flash (area->base, (uint8_t *)padded_ht, +sizeof (*padded_ht), errp) <0 ) { + ret = false; + } + }else { ret = false; }
Signature · 1 line
2.25.1
RFC PATCH v3 3/4 i386/sev: Reorder struct declarations
In reply to Dov MurikMove the declaration of PaddedSevHashTable before SevSnpGuest so we can add a new such field to the latter.
No functional change intended.
--- target/i386/sev.c | 56 +++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 28 deletions(-)
target/i386/sev.c +28 -28
@@ -40,6 +40,34 @@#include "exec/address-spaces.h" #include "exec/ramblock.h" +/* hard code sha256 digest size */ +#define HASH_SIZE 32 + +typedef struct QEMU_PACKED SevHashTableEntry { +QemuUUID guid; +uint16_t len; +uint8_t hash[HASH_SIZE ]; +} SevHashTableEntry; + +typedef struct QEMU_PACKED SevHashTable { +QemuUUID guid; +uint16_t len; +SevHashTableEntry cmdline; +SevHashTableEntry initrd; +SevHashTableEntry kernel; +} SevHashTable; + +/* +* Data encrypted by sev_encrypt_flash() must be padded to a multiple of +* 16 bytes. +*/ +typedef struct QEMU_PACKED PaddedSevHashTable { +SevHashTable ht; +uint8_t padding[ROUND_UP (sizeof (SevHashTable),16 ) -sizeof (SevHashTable)]; +} PaddedSevHashTable; + +QEMU_BUILD_BUG_ON (sizeof (PaddedSevHashTable) %16 !=0 ); +#define TYPE_SEV_COMMON "sev-common" OBJECT_DECLARE_SIMPLE_TYPE(SevCommonState, SEV_COMMON)#define TYPE_SEV_GUEST "sev-guest"
@@ -123,34 +151,6 @@ typedef struct QEMU_PACKED SevHashTableDescriptor {uint32_t size; } SevHashTableDescriptor; -/* hard code sha256 digest size */ -#define HASH_SIZE 32 - -typedef struct QEMU_PACKED SevHashTableEntry { -QemuUUID guid; -uint16_t len; -uint8_t hash[HASH_SIZE ]; -} SevHashTableEntry; - -typedef struct QEMU_PACKED SevHashTable { -QemuUUID guid; -uint16_t len; -SevHashTableEntry cmdline; -SevHashTableEntry initrd; -SevHashTableEntry kernel; -} SevHashTable; - -/* -* Data encrypted by sev_encrypt_flash() must be padded to a multiple of -* 16 bytes. -*/ -typedef struct QEMU_PACKED PaddedSevHashTable { -SevHashTable ht; -uint8_t padding[ROUND_UP (sizeof (SevHashTable),16 ) -sizeof (SevHashTable)]; -} PaddedSevHashTable; - -QEMU_BUILD_BUG_ON (sizeof (PaddedSevHashTable) %16 !=0 ); -static Error *sev_mig_blocker;static const char *const sev_fw_errlist[] = {
Signature · 1 line
2.25.1
RFC PATCH v3 4/4 i386/sev: Allow measured direct kernel boot on SNP
In reply to Dov MurikIn SNP, the hashes page designated with a specific metadata entry published in AmdSev OVMF.
Therefore, if the user enabled kernel hashes (for measured direct boot), QEMU should prepare the content of hashes table, and during the processing of the metadata entry it copy the content into the designated page and encrypt it.
Note that in SNP (unlike SEV and SEV-ES) the measurements is done in whole 4KB pages. Therefore QEMU zeros the whole page that includes the hashes table, and fills in the kernel hashes area in that page, and then encrypts the whole page. The rest of the page is reserved for SEV launch secrets which are not usable anyway on SNP.
If the user disabled kernel hashes, QEMU pre-validates the kernel hashes page as a zero page.
--- include/hw/i386/pc.h | 2 ++ target/i386/sev.c | 40 ++++++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-)
include/hw/i386/pc.h +2 -0
@@ -163,6 +163,8 @@ typedef enum {SEV_DESC_TYPE_SNP_SECRETS ,/* The section contains address that can be used as a CPUID page */ SEV_DESC_TYPE_CPUID , +/* The section contains the region for kernel hashes for measured direct boot */ +SEV_DESC_TYPE_SNP_KERNEL_HASHES =0x10 , } ovmf_sev_metadata_desc_type;
target/i386/sev.c +36 -4
@@ -131,6 +131,9 @@ struct SevSnpGuestState {struct kvm_snp_init kvm_init_conf;struct kvm_sev_snp_launch_start kvm_start_conf;struct kvm_sev_snp_launch_finish kvm_finish_conf; + +uint32_t kernel_hashes_offset; +PaddedSevHashTable *kernel_hashes_data; };#define DEFAULT_GUEST_POLICY 0x1/* disable debug */
@@ -1529,6 +1532,23 @@ snp_launch_update_cpuid(SevSnpGuestState *sev_snp, uint32_t cpuid_addr,return 0 ; } +static int +snp_launch_update_kernel_hashes (SevSnpGuestState *sev_snp,uint32_t addr, +void *hva,uint32_t len) +{ +int type =KVM_SEV_SNP_PAGE_TYPE_ZERO ; +if (sev_snp->sev_common.kernel_hashes) { +assert (sev_snp->kernel_hashes_data); +assert ((sev_snp->kernel_hashes_offset + +sizeof (*sev_snp->kernel_hashes_data)) <= len); +memset (hva,0 , len); +memcpy (hva + sev_snp->kernel_hashes_offset, sev_snp->kernel_hashes_data, +sizeof (*sev_snp->kernel_hashes_data)); + type =KVM_SEV_SNP_PAGE_TYPE_NORMAL ; + } +return sev_snp_launch_update (sev_snp, addr, hva, len, type); +} +static int snp_metadata_desc_to_page_type (int desc_type) {
@@ -1537,6 +1557,7 @@ snp_metadata_desc_to_page_type(int desc_type)case SEV_DESC_TYPE_SNP_SEC_MEM :return KVM_SEV_SNP_PAGE_TYPE_ZERO ;case SEV_DESC_TYPE_SNP_SECRETS :return KVM_SEV_SNP_PAGE_TYPE_SECRETS ;case SEV_DESC_TYPE_CPUID :return KVM_SEV_SNP_PAGE_TYPE_CPUID ; +case SEV_DESC_TYPE_SNP_KERNEL_HASHES :return KVM_SEV_SNP_PAGE_TYPE_NORMAL ;default :return -1 ; } }
@@ -1568,6 +1589,9 @@ snp_populate_metadata_pages(SevSnpGuestState *sev_snp,if (type ==KVM_SEV_SNP_PAGE_TYPE_CPUID ) { ret =snp_launch_update_cpuid (sev_snp, desc->base, hva, desc->len); + }else if (desc->type ==SEV_DESC_TYPE_SNP_KERNEL_HASHES ) { + ret =snp_launch_update_kernel_hashes (sev_snp, desc->base, hva, + desc->len); }else { ret =sev_snp_launch_update (sev_snp, desc->base, hva, desc->len, type);
@@ -2162,10 +2186,6 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)return false; } -if (sev_snp_enabled ()) { -return false; - } - area = (SevHashTableDescriptor *)data;if (!area->base || area->size <sizeof (PaddedSevHashTable)) { error_setg(errp, "SEV: guest firmware hashes table area is invalid "
@@ -2173,6 +2193,18 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)return false; } +if (sev_snp_enabled ()) { +/* +* SNP: Populate the hashes table in an area that later in +* snp_launch_update_kernel_hashes() will be copied to the guest memory +* and encrypted. +*/ +SevSnpGuestState *sev_snp_guest =SEV_SNP_GUEST (sev_common); + sev_snp_guest->kernel_hashes_offset = area->base & ~TARGET_PAGE_MASK ; + sev_snp_guest->kernel_hashes_data =g_new0 (PaddedSevHashTable,1 ); +return build_kernel_loader_hashes (sev_snp_guest->kernel_hashes_data, ctx, errp); + } +/* * Populate the hashes table in the guest's memory at the OVMF-designated * area for the SEV hashes table
Signature · 1 line
2.25.1