AMD SEV patches
The following changes since commit 0055ecca84cb948c935224b4f7ca1ceb26209790:
Merge tag 'vfio-fixes-20211117.0' of git://github.com/awilliam/qemu-vfio into staging (2021-11-18 09:39:47 +0100)
are available in the Git repository at:
https://gitlab.com/berrange/qemu tags/sev-hashes-pull-request
for you to fetch changes up to 58603ba2680fa35eade630e4b040e96953a11021:
target/i386/sev: Replace qemu_map_ram_ptr with address_space_map (2021-11-18 13:28:32 +0000)
----------------------------------------------------------------
Add property for requesting AMD SEV measured kernel launch
- The 'sev-guest' object gains a boolean 'kernel-hashes' property which must be enabled to request a measured kernel launch.
----------------------------------------------------------------
Dov Murik (6):
qapi/qom,target/i386: sev-guest: Introduce kernel-hashes=on|off option target/i386/sev: Add kernel hashes only if sev-guest.kernel-hashes=on target/i386/sev: Rephrase error message when no hashes table in guest
firmware
target/i386/sev: Fail when invalid hashes table area detected target/i386/sev: Perform padding calculations at compile-time target/i386/sev: Replace qemu_map_ram_ptr with address_space_map
qapi/qom.json | 7 ++++- qemu-options.hx | 6 +++- target/i386/sev.c | 79 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 15 deletions(-)
Signature · 1 line
2.31.1
[PULL 1/6] qapi/qom,target/i386: sev-guest: Introduce kernel-hashes=on|off option
From: Dov Murik <[email protected]>
Introduce new boolean 'kernel-hashes' option on the sev-guest object. It will be used to to decide whether to add the hashes of kernel/initrd/cmdline to SEV guest memory when booting with -kernel. The default value is 'off'.
--- qapi/qom.json | 7 ++++++- qemu-options.hx | 6 +++++- target/i386/sev.c | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-)
qapi/qom.json +6 -1
@@ -769,6 +769,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) +# # Since: 2.12 ## { 'struct': 'SevGuestProperties',
@@ -778,7 +782,8 @@ '*policy': 'uint32', '*handle': 'uint32', '*cbitpos': 'uint32', - 'reduced-phys-bits': 'uint32' } } + 'reduced-phys-bits': 'uint32', + '*kernel-hashes': 'bool' } } ## # @ObjectType:
qemu-options.hx +5 -1
@@ -5189,7 +5189,7 @@ SRST -object secret,id=sec0,keyid=secmaster0,format=base64,\\ data=$SECRET,iv=$(<iv.b64) - ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file]`` + ``-object sev-guest,id=id,cbitpos=cbitpos,reduced-phys-bits=val,[sev-device=string,policy=policy,handle=handle,dh-cert-file=file,session-file=file,kernel-hashes=on|off]`` Create a Secure Encrypted Virtualization (SEV) guest object, which can be used to provide the guest memory encryption support on AMD processors.
@@ -5229,6 +5229,10 @@ SRST session with the guest owner to negotiate keys used for attestation. The file must be encoded in base64. + The ``kernel-hashes`` adds the hashes of given kernel/initrd/ + cmdline to a designated guest firmware page for measured Linux + boot with -kernel. The default is off. (Since 6.2) + e.g to launch a SEV guest .. parsed-literal::
target/i386/sev.c +20 -0
@@ -62,6 +62,7 @@ struct SevGuestState {char *session_file;uint32_t cbitpos;uint32_t reduced_phys_bits; +bool kernel_hashes;/* runtime state */ uint32_t handle;
@@ -327,6 +328,20 @@ sev_guest_set_sev_device(Object *obj, const char *value, Error **errp) sev->sev_device =g_strdup (value); } +static bool sev_guest_get_kernel_hashes (Object *obj,Error **errp) +{ +SevGuestState *sev =SEV_GUEST (obj); + +return sev->kernel_hashes; +} + +static void sev_guest_set_kernel_hashes (Object *obj,bool value,Error **errp) +{ +SevGuestState *sev =SEV_GUEST (obj); + + sev->kernel_hashes = value; +} +static void sev_guest_class_init (ObjectClass *oc,void *data) {
@@ -345,6 +360,11 @@ sev_guest_class_init(ObjectClass *oc, void *data) sev_guest_set_session_file);object_class_property_set_description (oc,"session-file" ,"guest owners session parameters (encoded with base64)" ); +object_class_property_add_bool (oc,"kernel-hashes" , + sev_guest_get_kernel_hashes, + sev_guest_set_kernel_hashes); +object_class_property_set_description (oc,"kernel-hashes" , +"add kernel hashes to guest firmware for measured Linux boot" ); } static void
Signature · 1 line
2.31.1
[PULL 2/6] target/i386/sev: Add kernel hashes only if sev-guest.kernel-hashes=on
In reply to Daniel P. BerrangéFrom: Dov Murik <[email protected]>
Commit cff03145ed3c ("sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux boot", 2021-09-30) introduced measured direct boot with -kernel, using an OVMF-designated hashes table which QEMU fills.
However, if OVMF doesn't designate such an area, QEMU would completely abort the VM launch. This breaks launching with -kernel using older OVMF images which don't publish the SEV_HASH_TABLE_RV_GUID.
Fix that so QEMU will only look for the hashes table if the sev-guest kernel-hashes option is set to on. Otherwise, QEMU won't look for the designated area in OVMF and won't fill that area.
To enable addition of kernel hashes, launch the guest with:
-object sev-guest,...,kernel-hashes=on
--- target/i386/sev.c | 8 ++++++++ 1 file changed, 8 insertions(+)
target/i386/sev.c +8 -0
@@ -1223,6 +1223,14 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)size_t hash_len =HASH_SIZE ;int aligned_len; +/* +* Only add the kernel hashes if the sev-guest configuration explicitly +* stated kernel-hashes=on. +*/ +if (!sev_guest->kernel_hashes) { +return false; + } +if (!pc_system_ovmf_table_find (SEV_HASH_TABLE_RV_GUID , &data,NULL )) {error_setg (errp,"SEV: kernel specified but OVMF has no hash table guid" );return false;
Signature · 1 line
2.31.1
[PULL 3/6] target/i386/sev: Rephrase error message when no hashes table in guest firmware
In reply to Daniel P. BerrangéFrom: Dov Murik <[email protected]>
--- target/i386/sev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/sev.c +2 -1
@@ -1232,7 +1232,8 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp) }if (!pc_system_ovmf_table_find (SEV_HASH_TABLE_RV_GUID , &data,NULL )) { -error_setg (errp,"SEV: kernel specified but OVMF has no hash table guid" ); +error_setg (errp,"SEV: kernel specified but guest firmware " +"has no hashes table GUID" );return false; } area = (SevHashTableDescriptor *)data;
Signature · 1 line
2.31.1
[PULL 4/6] target/i386/sev: Fail when invalid hashes table area detected
In reply to Daniel P. BerrangéFrom: Dov Murik <[email protected]>
Commit cff03145ed3c ("sev/i386: Introduce sev_add_kernel_loader_hashes for measured linux boot", 2021-09-30) introduced measured direct boot with -kernel, using an OVMF-designated hashes table which QEMU fills.
However, no checks are performed on the validity of the hashes area designated by OVMF. Specifically, if OVMF publishes the SEV_HASH_TABLE_RV_GUID entry but it is filled with zeroes, this will cause QEMU to write the hashes entries over the first page of the guest's memory (GPA 0).
Add validity checks to the published area. If the hashes table area's base address is zero, or its size is too small to fit the aligned hashes table, display an error and stop the guest launch. In such case, the following error will be displayed:
qemu-system-x86_64: SEV: guest firmware hashes table area is invalid (base=0x0 size=0x0)
--- target/i386/sev.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
target/i386/sev.c +6 -2
@@ -1221,7 +1221,7 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)uint8_t kernel_hash[HASH_SIZE ];uint8_t *hashp;size_t hash_len =HASH_SIZE ; -int aligned_len; +int aligned_len =ROUND_UP (sizeof (SevHashTable),16 );/* * Only add the kernel hashes if the sev-guest configuration explicitly
@@ -1237,6 +1237,11 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)return false; } area = (SevHashTableDescriptor *)data; +if (!area->base || area->size < aligned_len) { +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
@@ -1295,7 +1300,6 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)memcpy (ht->kernel.hash, kernel_hash,sizeof (ht->kernel.hash));/* When calling sev_encrypt_flash, the length has to be 16 byte aligned */ - aligned_len =ROUND_UP (ht->len,16 );if (aligned_len != ht->len) {/* zero the excess data so the measurement can be reliably calculated */ memset (ht->padding,0 , aligned_len - ht->len);
Signature · 1 line
2.31.1
[PULL 5/6] target/i386/sev: Perform padding calculations at compile-time
In reply to Daniel P. BerrangéFrom: Dov Murik <[email protected]>
In sev_add_kernel_loader_hashes, the sizes of structs are known at compile-time, so calculate needed padding at compile-time.
No functional change intended.
--- target/i386/sev.c | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-)
target/i386/sev.c +18 -10
@@ -110,9 +110,19 @@ typedef struct QEMU_PACKED SevHashTable {SevHashTableEntry cmdline;SevHashTableEntry initrd;SevHashTableEntry kernel; -uint8_t padding[]; } 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 SevGuestState *sev_guest;static Error *sev_mig_blocker;
@@ -1216,12 +1226,12 @@ bool sev_add_kernel_loader_hashes(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 ; -int aligned_len =ROUND_UP (sizeof (SevHashTable),16 );/* * Only add the kernel hashes if the sev-guest configuration explicitly
@@ -1237,7 +1247,7 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)return false; } area = (SevHashTableDescriptor *)data; -if (!area->base || area->size < aligned_len) { +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;
@@ -1282,7 +1292,8 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)* Populate the hashes table in the guest's memory at the OVMF-designated * area for the SEV hashes table */ - ht =qemu_map_ram_ptr (NULL , area->base); + padded_ht =qemu_map_ram_ptr (NULL , area->base); + ht = &padded_ht->ht; ht->guid = sev_hash_table_header_guid; ht->len =sizeof (*ht);
@@ -1299,13 +1310,10 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp) ht->kernel.len =sizeof (ht->kernel);memcpy (ht->kernel.hash, kernel_hash,sizeof (ht->kernel.hash)); -/* When calling sev_encrypt_flash, the length has to be 16 byte aligned */ -if (aligned_len != ht->len) { -/* zero the excess data so the measurement can be reliably calculated */ -memset (ht->padding,0 , aligned_len - ht->len); - } +/* zero the excess data so the measurement can be reliably calculated */ +memset (padded_ht->padding,0 ,sizeof (padded_ht->padding)); -if (sev_encrypt_flash ((uint8_t *)ht, aligned_len, errp) <0 ) { +if (sev_encrypt_flash ((uint8_t *)padded_ht,sizeof (*padded_ht), errp) <0 ) {return false; }
Signature · 1 line
2.31.1
[PULL 6/6] target/i386/sev: Replace qemu_map_ram_ptr with address_space_map
In reply to Daniel P. BerrangéFrom: Dov Murik <[email protected]>
Use address_space_map/unmap and check for errors.
--- target/i386/sev.c | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-)
target/i386/sev.c +15 -3
@@ -37,6 +37,7 @@#include "qapi/qmp/qerror.h" #include "exec/confidential-guest-support.h" #include "hw/i386/pc.h" +#include "exec/address-spaces.h" #define TYPE_SEV_GUEST "sev-guest"OBJECT_DECLARE_SIMPLE_TYPE (SevGuestState,SEV_GUEST )
@@ -1232,6 +1233,9 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)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;/* * Only add the kernel hashes if the sev-guest configuration explicitly
@@ -1292,7 +1296,12 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)* Populate the hashes table in the guest's memory at the OVMF-designated * area for the SEV hashes table */ - padded_ht =qemu_map_ram_ptr (NULL , area->base); + 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;
@@ -1314,10 +1323,13 @@ bool sev_add_kernel_loader_hashes(SevKernelLoaderContext *ctx, Error **errp)memset (padded_ht->padding,0 ,sizeof (padded_ht->padding));if (sev_encrypt_flash ((uint8_t *)padded_ht,sizeof (*padded_ht), errp) <0 ) { -return false; + ret = false; } -return true; +address_space_unmap (&address_space_memory, padded_ht, + mapped_len, true, mapped_len); + +return ret; } static void
Signature · 1 line
2.31.1
Richard Henderson
AppliedQuoted 29 lines · Daniel P. Berrangé
The following changes since commit
0055ecca84cb948c935224b4f7ca1ceb26209790:Merge tag 'vfio-fixes-20211117.0' of git://github.com/awilliam/qemu-vfio into staging (2021-11-18 09:39:47 +0100)are available in the Git repository at:
https://gitlab.com/berrange/qemu tags/sev-hashes-pull-request
for you to fetch changes up to
58603ba2680fa35eade630e4b040e96953a11021:target/i386/sev: Replace qemu_map_ram_ptr with address_space_map (2021-11-18 13:28:32 +0000)----------------------------------------------------------------
Add property for requesting AMD SEV measured kernel launch- The 'sev-guest' object gains a boolean 'kernel-hashes' property which must be enabled to request a measured kernel launch.
----------------------------------------------------------------
Dov Murik (6):
qapi/qom,target/i386: sev-guest: Introduce kernel-hashes=on|off option target/i386/sev: Add kernel hashes only if sev-guest.kernel-hashes=on target/i386/sev: Rephrase error message when no hashes table in guest
firmware
target/i386/sev: Fail when invalid hashes table area detected target/i386/sev: Perform padding calculations at compile-time target/i386/sev: Replaceqemu_map_ram_ptrwithaddress_space_map
Quoted 4 lines
qapi/qom.json | 7 ++++- qemu-options.hx | 6 +++- target/i386/sev.c | 79 +++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 77 insertions(+), 15 deletions(-)
Applied, thanks.
r~