target/i386: do not crash if microvm guest uses SGX CPUID leaves
PATCH 1/1 target/i386: do not crash if microvm guest uses SGX CPUID leaves
sgx_epc_get_section assumes a PC platform is in use:
bool sgx_epc_get_section(int section_nr, uint64_t *addr, uint64_t *size)
{
PCMachineState *pcms = PC_MACHINE(qdev_get_machine());However, sgx_epc_get_section is called by CPUID regardless of whether SGX state has been initialized or which platform is in use. Check whether the machine has the right QOM class and if not behave as if there are no EPC sections.
Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2142
--- hw/i386/sgx.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
hw/i386/sgx.c +4 -2
@@ -266,10 +266,12 @@ void hmp_info_sgx(Monitor *mon, const QDict *qdict)bool sgx_epc_get_section (int section_nr,uint64_t *addr,uint64_t *size) { -PCMachineState *pcms =PC_MACHINE (qdev_get_machine ()); +PCMachineState *pcms = + (PCMachineState *)object_dynamic_cast (qdev_get_machine (), +TYPE_PC_MACHINE );SGXEPCDevice *epc; -if (pcms->sgx_epc.size ==0 || pcms->sgx_epc.nr_sections <= section_nr) { +if (!pcms || pcms->sgx_epc.size ==0 || pcms->sgx_epc.nr_sections <= section_nr) {return true; }
Signature · 1 line
2.45.2
Hi Paolo,
Quoted 34 lines · Paolo Bonzini
Date: Tue, 16 Jul 2024 18:55:30 +0200
From: Paolo Bonzini <[email protected]>
Subject: [PATCH] target/i386: do not crash if microvm guest uses SGX CPUID
leaves
X-Mailer: git-send-email 2.45.2
sgx_epc_get_sectionassumes a PC platform is in use:bool
sgx_epc_get_section(int section_nr, uint64_t *addr, uint64_t *size){ PCMachineState *pcms = PC_MACHINE(qdev_get_machine());However,
sgx_epc_get_sectionis called by CPUID regardless of whether SGX state has been initialized or which platform is in use. Check whether the machine has the right QOM class and if not behave as if there are no EPC sections.Fixes 1dec2e1f19f ("i386: Update SGX CPUID info according to hardware/KVM/user input", 2021-09-30)Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2142
Signed-off-by Paolo Bonzini <[email protected]>--- hw/i386/sgx.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/i386/sgx.c +0
-1@@ -266,10 +266,12 @@ void hmp_info_sgx(Monitor *mon, const QDict *qdict)bool sgx_epc_get_section (int section_nr,uint64_t *addr,uint64_t *size) { -PCMachineState *pcms =PC_MACHINE (qdev_get_machine ());
hw/i386/sgx.c void hmp_info_sgx(Monitor *mon, const QDict *qdict) · line 277
Quoted 10 lines
hw/i386/sgx.c +4
-1+PCMachineState *pcms = + (PCMachineState *)object_dynamic_cast (qdev_get_machine (), +TYPE_PC_MACHINE );SGXEPCDevice *epc; -if (pcms->sgx_epc.size ==0 || pcms->sgx_epc.nr_sections <= section_nr) { +if (!pcms || pcms->sgx_epc.size ==0 || pcms->sgx_epc.nr_sections <= section_nr) {return true; }
The above change is necessary...
...but it only avoids encoding sub-leafs CPUID.0x12.{0x2..N}, while subleafs CPUID.0x12.{0x0,0x1} still have valid SGX information.
According to the error message in qmp_query_sgx(), sgx is only supported on PC machines. So how about simply taking it a step further and masking out the entire 0x12 leaf for microvm as well?
for example:
hw/i386/sgx-stub.c +5 -0
@@ -32,6 +32,11 @@ void pc_machine_init_sgx_epc(PCMachineState *pcms)memset (&pcms->sgx_epc,0 ,sizeof (SGXEPCState)); } +bool check_sgx_support (void ) +{ +return false; +} +bool sgx_epc_get_section (int section_nr,uint64_t *addr,uint64_t *size) {return true;
hw/i386/sgx.c +8 -0
@@ -264,6 +264,14 @@ void hmp_info_sgx(Monitor *mon, const QDict *qdict) size); } +bool check_sgx_support (void ) +{ +if (!object_dynamic_cast (qdev_get_machine (),TYPE_X86_MACHINE )) { +return false; + } +return true; +} +bool sgx_epc_get_section (int section_nr,uint64_t *addr,uint64_t *size) {PCMachineState *pcms =PC_MACHINE (qdev_get_machine ());
include/hw/i386/sgx-epc.h +1 -0
@@ -58,6 +58,7 @@ typedef struct SGXEPCState {int nr_sections; } SGXEPCState; +bool check_sgx_support (void );bool sgx_epc_get_section (int section_nr,uint64_t *addr,uint64_t *size);void sgx_epc_build_srat (GArray *table_data);
target/i386/cpu.c +2 -1
@@ -6702,7 +6702,8 @@ void cpu_x86_cpuid(CPUX86State *env, uint32_t index, uint32_t count,case 0x12 :#ifndef CONFIG_USER_ONLY if (!kvm_enabled () || - !(env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_SGX )) { + !(env->features[FEAT_7_0_EBX ] &CPUID_7_0_EBX_SGX ) || + !check_sgx_support ()) { *eax = *ebx = *ecx = *edx =0 ;break ; }