Skip to contentSkip to conversation

KVM: VMX: Fixes for faults on ENCLS emulation

PATCH Applied Sean Christopherson 2 participants 13 messages Apr 5, 2023 – Jun 3, 2023

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: VMX: Fixes for faults on ENCLS emulation

Found-by-inspection (when reviewing Binbin's patch) fixes for incorrect emulation of faults when KVMintercepts and emulates (sort of) ENCLS.

Very much compile tested only. Ideally, someone with SGX hardware can confirm that these patches are correct, e.g. my assessment that KVM needs to manually check CR0.PG is based purely of SDM pseudocode.

Sean Christopherson (2):
KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)
KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

base-commit: 27d6845d258b67f4eb3debe062b7dacc67e0c393

Signature · 1 line
2.40.0.348.gf938b09366-goog
author maintainer
#1 lore raw

PATCH 1/2 KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)

Inject a #GP when emulating/forwarding a valid ENCLS leaf if the vCPU has paging disabled, e.g. if KVM is intercepting ECREATE to enforce additional restrictions. The pseudocode in the SDM lists all #GP triggers, including CR0.PG=0, as being checked after the ENLCS-exiting checks, i.e. the VM-Exit will occur before the CPU performs the CR0.PG check.

Fixes 70210c044b4e ("KVM: VMX: Add SGX ENCLS[ECREATE] handler to enforce CPUID restrictions")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
---
 arch/x86/kvm/vmx/sgx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/sgx.c +1 -1
index aa53c98034bf..f881f6ff6408 100644
@@ -375,7 +375,7 @@ int handle_encls(struct kvm_vcpu *vcpu)
 
 	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);
-	} else if (!sgx_enabled_in_guest_bios(vcpu)) {
+	} else if (!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
 		kvm_inject_gp(vcpu, 0);
 	} else {
 		if (leaf == ECREATE)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)

Quoted 10 lines · Sean Christopherson

Inject a #GP when emulating/forwarding a valid ENCLS leaf if the vCPU has paging disabled, e.g. if KVM is intercepting ECREATE to enforce additional restrictions. The pseudocode in the SDM lists all #GP triggers, including CR0.PG=0, as being checked after the ENLCS-exiting checks, i.e. the VM-Exit will occur before the CPU performs the CR0.PG check.

Fixes 70210c044b4e ("KVM: VMX: Add SGX ENCLS[ECREATE] handler to enforce CPUID restrictions")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
Quoted 20 lines
---
 arch/x86/kvm/vmx/sgx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx/sgx.c +1 -1
index aa53c98034bf..f881f6ff6408 100644
@@ -375,7 +375,7 @@ int handle_encls(struct kvm_vcpu *vcpu)
 
 	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);
-	} else if (!sgx_enabled_in_guest_bios(vcpu)) {
+	} else if (!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
 		kvm_inject_gp(vcpu, 0);
 	} else {
 		if (leaf == ECREATE)
Signature · 1 line
2.40.0.348.gf938b09366-goog
Tested-by
#3 lore raw

Re: [PATCH 1/2] KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)

Quoted 7 lines · Kai
Quoted 6 lines · Sean Christopherson

Inject a #GP when emulating/forwarding a valid ENCLS leaf if the vCPU has paging disabled, e.g. if KVM is intercepting ECREATE to enforce additional restrictions. The pseudocode in the SDM lists all #GP triggers, including CR0.PG=0, as being checked after the ENLCS-exiting checks, i.e. the VM-Exit will occur before the CPU performs the CR0.PG check.

Quoted 8 lines
Quoted 4 lines
Fixes 70210c044b4e ("KVM: VMX: Add SGX ENCLS[ECREATE] handler to enforce CPUID restrictions")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
Quoted 1 line

Tested by running ENCLS in protected mode before enabling paging, and in my test the #GP was injected to the guest.

author maintainer
#4 lore raw

PATCH 2/2 KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

In reply to Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

Fixes 9798adbc04cf ("KVM: VMX: Frame in ENCLS handler for SGX virtualization")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
---
 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/sgx.c +9 -6
index f881f6ff6408..1c092ab89c33 100644
@@ -350,11 +350,12 @@ static int handle_encls_einit(struct kvm_vcpu *vcpu)
 
 static inline bool encls_leaf_enabled_in_guest(struct kvm_vcpu *vcpu, u32 leaf)
 {
-	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX))
-		return false;
-
+	/*
+	 * ENCLS #UDs if SGX1 isn't supported, i.e. this point will be reached
+	 * if and only if the SGX1 leafs are enabled.
+	 */
 	if (leaf >= ECREATE && leaf <= ETRACK)
-		return guest_cpuid_has(vcpu, X86_FEATURE_SGX1);
+		return true;
 
 	if (leaf >= EAUG && leaf <= EMODT)
 		return guest_cpuid_has(vcpu, X86_FEATURE_SGX2);
@@ -373,9 +374,11 @@ int handle_encls(struct kvm_vcpu *vcpu)
 {
 	u32 leaf = (u32)kvm_rax_read(vcpu);
 
-	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
+	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
+	    !guest_cpuid_has(vcpu, X86_FEATURE_SGX1)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);
-	} else if (!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
+	} else if (!encls_leaf_enabled_in_guest(vcpu, leaf) ||
+		   !sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
 		kvm_inject_gp(vcpu, 0);
 	} else {
 		if (leaf == ECREATE)
Signature · 1 line
2.40.0.348.gf938b09366-goog
#5 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

If I recall correctly, this is an erratum which can clear SGX1 in CPUID while the SGX flag is still in CPUID?

But I am not sure whether this part is relevant to this patch? Because SDM already says ENCLS causes #UD if SGX1 isn't present. This patch changes "unsupported leaf" from causing #UD to causing #GP, which is also documented in SDM.

Quoted 14 lines
Fixes 9798adbc04cf ("KVM: VMX: Frame in ENCLS handler for SGX virtualization")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
---
 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/sgx.c +0 -0
index f881f6ff6408..1c092ab89c33 100644

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 354

Quoted 8 lines
arch/x86/kvm/vmx/sgx.c +2 -3
@@ -350,11 +350,12 @@ static int handle_encls_einit(struct kvm_vcpu *vcpu)
 
 static inline bool encls_leaf_enabled_in_guest(struct kvm_vcpu *vcpu, u32 leaf)
 {
-	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX))
-		return false;
-
+	/*
+	 * ENCLS #UDs if SGX1 isn't supported, i.e. this point will be reached

Why #UDs instead of #UD? Is #UD a verb?

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 356

Quoted 2 lines
arch/x86/kvm/vmx/sgx.c +2 -0
+	 * if and only if the SGX1 leafs are enabled.
+	 */

Is it better to move "ENCLS #UDs if SGX1 isn't supported" part to ...

Quoted 7 lines
arch/x86/kvm/vmx/sgx.c +1 -1
 	if (leaf >= ECREATE && leaf <= ETRACK)
-		return guest_cpuid_has(vcpu, X86_FEATURE_SGX1);
+		return true;
 
 	if (leaf >= EAUG && leaf <= EMODT)
 		return guest_cpuid_has(vcpu, X86_FEATURE_SGX2);

arch/x86/kvm/vmx/sgx.c int handle_encls(struct kvm_vcpu *vcpu) · line 379

Quoted 7 lines
arch/x86/kvm/vmx/sgx.c +2 -1
@@ -373,9 +374,11 @@ int handle_encls(struct kvm_vcpu *vcpu)
 {
 	u32 leaf = (u32)kvm_rax_read(vcpu);
 
-	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
+	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
+	    !guest_cpuid_has(vcpu, X86_FEATURE_SGX1)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);

... above here, where the actual code reside?

Quoted 9 lines
arch/x86/kvm/vmx/sgx.c +2 -1
-	} else if (!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
+	} else if (!encls_leaf_enabled_in_guest(vcpu, leaf) ||
+		   !sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
 		kvm_inject_gp(vcpu, 0);
 	} else {
 		if (leaf == ECREATE)
Signature · 1 line
2.40.0.348.gf938b09366-goog
author maintainer
#6 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

Quoted 9 lines · Kai
Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

If I recall correctly, this is an erratum which can clear SGX1 in CPUID while the SGX flag is still in CPUID?

Nope, not an erratum, architectural behavior.

Quoted 4 lines

But I am not sure whether this part is relevant to this patch? Because SDM already says ENCLS causes #UD if SGX1 isn't present. This patch changes "unsupported leaf" from causing #UD to causing #GP, which is also documented in SDM.

I wanted to capture why SGX1 is different and given special treatment in the SDM. I.e. to explain why SGX1 leafs are an exception to the "#GP if leaf unsupported" clause.

Quoted 14 lines
Quoted 14 lines
Fixes 9798adbc04cf ("KVM: VMX: Frame in ENCLS handler for SGX virtualization")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
---
 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/sgx.c +0 -0
index f881f6ff6408..1c092ab89c33 100644
@@ -350,11 +350,12 @@ static int handle_encls_einit(struct kvm_vcpu *vcpu)
 
Quoted 9 lines

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 354

Quoted 7 lines
arch/x86/kvm/vmx/sgx.c +2 -3
 static inline bool encls_leaf_enabled_in_guest(struct kvm_vcpu *vcpu, u32 leaf)
 {
-	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX))
-		return false;
-
+	/*
+	 * ENCLS #UDs if SGX1 isn't supported, i.e. this point will be reached

Why #UDs instead of #UD? Is #UD a verb?

Heh, it is now ;-) I can reword to something like

	/*
	 * ENCLS generates a #UD if SGX1 isn't supported ...
	 */

if my made up grammar is confusing.

Quoted 15 lines

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 356

Quoted 2 lines
arch/x86/kvm/vmx/sgx.c +2 -0
+	 * if and only if the SGX1 leafs are enabled.
+	 */

Is it better to move "ENCLS #UDs if SGX1 isn't supported" part to ...

Quoted 10 lines
arch/x86/kvm/vmx/sgx.c +1 -1
 	if (leaf >= ECREATE && leaf <= ETRACK)
-		return guest_cpuid_has(vcpu, X86_FEATURE_SGX1);
+		return true;
 
 	if (leaf >= EAUG && leaf <= EMODT)
 		return guest_cpuid_has(vcpu, X86_FEATURE_SGX2);
@@ -373,9 +374,11 @@ int handle_encls(struct kvm_vcpu *vcpu)
 {
 	u32 leaf = (u32)kvm_rax_read(vcpu);
 
Quoted 6 lines

arch/x86/kvm/vmx/sgx.c int handle_encls(struct kvm_vcpu *vcpu) · line 379

Quoted 4 lines
arch/x86/kvm/vmx/sgx.c +2 -1
-	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
+	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
+	    !guest_cpuid_has(vcpu, X86_FEATURE_SGX1)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);

... above here, where the actual code reside?

My goal was to document why encls_leaf_enabled_in_guest() unconditionally returns true for SGX1 leafs, i.e. why it doesn't query X86_FEATURE_SGX1. I'm definitely not opposed to also adding a comment here, but I do want to keep the comment in encls_leaf_enabled_in_guest().

Reviewed-by
#7 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Kai
Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

Quoted 4 lines
Quoted 2 lines

If I recall correctly, this is an erratum which can clear SGX1 in CPUID while the SGX flag is still in CPUID?

Nope, not an erratum, architectural behavior.

I found the relevant section in SDM:

All supported IA32_MCi_CTL bits for all the machine check banks must be set for Intel SGX to be available
(CPUID.SGX_Leaf.0:EAX[SGX1] == 1). Any act of clearing bits from '1 to '0 in any of the IA32_MCi_CTL register
may disable Intel SGX (set CPUID.SGX_Leaf.0:EAX[SGX1] to 0) until the next reset.

Looking at the code, it seems currently KVM won't clear SGX1 bit in CPUID when guest disables IA32_MCi_CTL (writing 0). Should we do that?

Quoted 9 lines
Quoted 5 lines

But I am not sure whether this part is relevant to this patch? Because SDM already says ENCLS causes #UD if SGX1 isn't present. This patch changes "unsupported leaf" from causing #UD to causing #GP, which is also documented in SDM.

I wanted to capture why SGX1 is different and given special treatment in the SDM. I.e. to explain why SGX1 leafs are an exception to the "#GP if leaf unsupported" clause.

OK.

Quoted 55 lines
Quoted 15 lines
Quoted 15 lines
Fixes 9798adbc04cf ("KVM: VMX: Frame in ENCLS handler for SGX virtualization")
Cc Binbin Wu <[email protected]>
Cc Kai Huang <[email protected]>
---
 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/sgx.c +0 -0
index f881f6ff6408..1c092ab89c33 100644
@@ -350,11 +350,12 @@ static int handle_encls_einit(struct kvm_vcpu *vcpu)
 
Quoted 9 lines

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 354

Quoted 7 lines
arch/x86/kvm/vmx/sgx.c +2 -3
 static inline bool encls_leaf_enabled_in_guest(struct kvm_vcpu *vcpu, u32 leaf)
 {
-	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX))
-		return false;
-
+	/*
+	 * ENCLS #UDs if SGX1 isn't supported, i.e. this point will be reached

Why #UDs instead of #UD? Is #UD a verb?

Heh, it is now ;-) I can reword to something like

	/*
	 * ENCLS generates a #UD if SGX1 isn't supported ...
	 */

if my made up grammar is confusing.

Quoted 15 lines

arch/x86/kvm/vmx/sgx.c static int handle_encls_einit(struct kvm_vcpu *vcpu) · line 356

Quoted 2 lines
arch/x86/kvm/vmx/sgx.c +2 -0
+	 * if and only if the SGX1 leafs are enabled.
+	 */

Is it better to move "ENCLS #UDs if SGX1 isn't supported" part to ...

Quoted 10 lines
arch/x86/kvm/vmx/sgx.c +1 -1
 	if (leaf >= ECREATE && leaf <= ETRACK)
-		return guest_cpuid_has(vcpu, X86_FEATURE_SGX1);
+		return true;
 
 	if (leaf >= EAUG && leaf <= EMODT)
 		return guest_cpuid_has(vcpu, X86_FEATURE_SGX2);
@@ -373,9 +374,11 @@ int handle_encls(struct kvm_vcpu *vcpu)
 {
 	u32 leaf = (u32)kvm_rax_read(vcpu);
 
Quoted 6 lines

arch/x86/kvm/vmx/sgx.c int handle_encls(struct kvm_vcpu *vcpu) · line 379

Quoted 4 lines
arch/x86/kvm/vmx/sgx.c +2 -1
-	if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
+	if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
+	    !guest_cpuid_has(vcpu, X86_FEATURE_SGX1)) {
 		kvm_queue_exception(vcpu, UD_VECTOR);

... above here, where the actual code reside?

Quoted 4 lines

My goal was to document why encls_leaf_enabled_in_guest() unconditionally returns true for SGX1 leafs, i.e. why it doesn't query X86_FEATURE_SGX1. I'm definitely not opposed to also adding a comment here, but I do want to keep the comment in encls_leaf_enabled_in_guest().

Sure.

Anyway,

author maintainer
#8 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

Quoted 16 lines · Kai Huang
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Kai
Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

Quoted 4 lines
Quoted 2 lines

If I recall correctly, this is an erratum which can clear SGX1 in CPUID while the SGX flag is still in CPUID?

Nope, not an erratum, architectural behavior.

I found the relevant section in SDM:

Quoted 7 lines

All supported IA32_MCi_CTL bits for all the machine check banks must be set for Intel SGX to be available (CPUID.SGX_Leaf.0:EAX[SGX1] == 1). Any act of clearing bits from '1 to '0 in any of the IA32_MCi_CTL register may disable Intel SGX (set CPUID.SGX_Leaf.0:EAX[SGX1] to 0) until the next reset.

Looking at the code, it seems currently KVM won't clear SGX1 bit in CPUID when guest disables IA32_MCi_CTL (writing 0). Should we do that?

No, the behavior isn't strictly required: clearing bits *may* disable Intel SGX. And there is zero benefit to the guest, the behavior exists in bare metal purely to allow the CPU to provide security guarantees. On the flip side, emulating the disabling of SGX without causing problems, e.g. when userspace sets MSRs, would be quite tricky.

#9 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

Quoted 22 lines · Sean Christopherson
Quoted 21 lines · Kai Huang
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Kai
Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

Quoted 4 lines
Quoted 2 lines

If I recall correctly, this is an erratum which can clear SGX1 in CPUID while the SGX flag is still in CPUID?

Nope, not an erratum, architectural behavior.

I found the relevant section in SDM:

All supported IA32_MCi_CTL bits for all the machine check banks must be set for Intel SGX to be available (CPUID.SGX_Leaf.0:EAX[SGX1] == 1). Any act of clearing bits from '1 to '0 in any of the IA32_MCi_CTL register may disable Intel SGX (set CPUID.SGX_Leaf.0:EAX[SGX1] to 0) until the next reset.

Quoted 8 lines
Quoted 2 lines

Looking at the code, it seems currently KVM won't clear SGX1 bit in CPUID when guest disables IA32_MCi_CTL (writing 0). Should we do that?

No, the behavior isn't strictly required: clearing bits *may* disable Intel SGX. And there is zero benefit to the guest, the behavior exists in bare metal purely to allow the CPU to provide security guarantees. On the flip side, emulating the disabling of SGX without causing problems, e.g. when userspace sets MSRs, would be quite tricky.

Yeah my thinking too. Agreed.

Tested-by
#10 lore raw

Re: [PATCH 2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

In reply to Huang, Kai
Quoted 9 lines · Kai
Quoted 8 lines · Sean Christopherson
Quoted 7 lines · Kai
Quoted 5 lines · Sean Christopherson

Per Intel's SDM, unsupported ENCLS leafs result in a #GP, not a #UD. SGX1 is a special snowflake as the SGX1 flag is used by the CPU as a "soft" disable, e.g. if software disables machine check reporting, i.e. having SGX but not SGX1 is effectively "SGX completely unsupported" and and thus #UDs.

[...]

Quoted 4 lines

Tested by disabling SGX2 in the guest and running SGX2 leaf inside, and in my testing the #GP is injected to the guest due to leaf not enabled.

#11 lore raw
In reply to Sean Christopherson
Quoted 6 lines · Sean Christopherson

Found-by-inspection (when reviewing Binbin's patch) fixes for incorrect emulation of faults when KVMintercepts and emulates (sort of) ENCLS.

Very much compile tested only. Ideally, someone with SGX hardware can confirm that these patches are correct, e.g. my assessment that KVM needs to manually check CR0.PG is based purely of SDM pseudocode.

Thanks for the patches. I don't have a "ready" SGX environment at hand, but I'll try to test or ask someone else to test after Easter holiday.

Quoted 13 lines

Sean Christopherson (2):
KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)
KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported

 arch/x86/kvm/vmx/sgx.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

base-commit: 27d6845d258b67f4eb3debe062b7dacc67e0c393

Signature · 1 line
2.40.0.348.gf938b09366-goog
author maintainer Applied
#12 lore raw
In reply to Sean Christopherson
Quoted 8 lines · Sean Christopherson

Found-by-inspection (when reviewing Binbin's patch) fixes for incorrect emulation of faults when KVMintercepts and emulates (sort of) ENCLS.

Very much compile tested only. Ideally, someone with SGX hardware can confirm that these patches are correct, e.g. my assessment that KVM needs to manually check CR0.PG is based purely of SDM pseudocode.

[...]

Applied to kvm-x86 vmx, thanks!

[1/2] KVM: VMX: Inject #GP on ENCLS if vCPU has paging disabled (CR0.PG==0)

      https://github.com/kvm-x86/linux/commit/5e50082c8c21
[2/2] KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported
      https://github.com/kvm-x86/linux/commit/c3a1e119a343

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes