Skip to content

Commit

Permalink
KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported
Browse files Browse the repository at this point in the history
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: 9798adb ("KVM: VMX: Frame in ENCLS handler for SGX virtualization")
Cc: Binbin Wu <[email protected]>
Cc: Kai Huang <[email protected]>
Tested-by: Kai Huang <[email protected]>
Reviewed-by: Kai Huang <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Sean Christopherson <[email protected]>
  • Loading branch information
sean-jc committed Jun 2, 2023
1 parent 5e50082 commit c3a1e11
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions arch/x86/kvm/vmx/sgx.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,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 generates a #UD 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);
Expand All @@ -380,9 +381,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)
Expand Down

0 comments on commit c3a1e11

Please sign in to comment.