Skip to content

Commit c3a1e11

Browse files
committed
KVM: VMX: Inject #GP, not #UD, if SGX2 ENCLS leafs are unsupported
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 <binbin.wu@linux.intel.com> Cc: Kai Huang <kai.huang@intel.com> Tested-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Link: https://lore.kernel.org/r/20230405234556.696927-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 5e50082 commit c3a1e11

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

arch/x86/kvm/vmx/sgx.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,11 +357,12 @@ static int handle_encls_einit(struct kvm_vcpu *vcpu)
357357

358358
static inline bool encls_leaf_enabled_in_guest(struct kvm_vcpu *vcpu, u32 leaf)
359359
{
360-
if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX))
361-
return false;
362-
360+
/*
361+
* ENCLS generates a #UD if SGX1 isn't supported, i.e. this point will
362+
* be reached if and only if the SGX1 leafs are enabled.
363+
*/
363364
if (leaf >= ECREATE && leaf <= ETRACK)
364-
return guest_cpuid_has(vcpu, X86_FEATURE_SGX1);
365+
return true;
365366

366367
if (leaf >= EAUG && leaf <= EMODT)
367368
return guest_cpuid_has(vcpu, X86_FEATURE_SGX2);
@@ -380,9 +381,11 @@ int handle_encls(struct kvm_vcpu *vcpu)
380381
{
381382
u32 leaf = (u32)kvm_rax_read(vcpu);
382383

383-
if (!encls_leaf_enabled_in_guest(vcpu, leaf)) {
384+
if (!enable_sgx || !guest_cpuid_has(vcpu, X86_FEATURE_SGX) ||
385+
!guest_cpuid_has(vcpu, X86_FEATURE_SGX1)) {
384386
kvm_queue_exception(vcpu, UD_VECTOR);
385-
} else if (!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
387+
} else if (!encls_leaf_enabled_in_guest(vcpu, leaf) ||
388+
!sgx_enabled_in_guest_bios(vcpu) || !is_paging(vcpu)) {
386389
kvm_inject_gp(vcpu, 0);
387390
} else {
388391
if (leaf == ECREATE)

0 commit comments

Comments
 (0)