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

Message ID 20230405234556.696927-2-seanjc@google.com
State New
Headers
Series KVM: VMX: Fixes for faults on ENCLS emulation |

Commit Message

Sean Christopherson April 5, 2023, 11:45 p.m. UTC
  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 <binbin.wu@linux.intel.com>
Cc: Kai Huang <kai.huang@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/vmx/sgx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Kai Huang April 6, 2023, 8:54 a.m. UTC | #1
On Wed, 2023-04-05 at 16:45 -0700, Sean Christopherson wrote:
> 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 <binbin.wu@linux.intel.com>
> Cc: Kai Huang <kai.huang@intel.com>
> Signed-off-by: Sean Christopherson <seanjc@google.com>

Reviewed-by: Kai Huang <kai.huang@intel.com>

> ---
>  arch/x86/kvm/vmx/sgx.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kvm/vmx/sgx.c b/arch/x86/kvm/vmx/sgx.c
> index aa53c98034bf..f881f6ff6408 100644
> --- a/arch/x86/kvm/vmx/sgx.c
> +++ b/arch/x86/kvm/vmx/sgx.c
> @@ -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)
> -- 
> 2.40.0.348.gf938b09366-goog
>
  
Kai Huang April 21, 2023, 9:18 a.m. UTC | #2
On Thu, 2023-04-06 at 08:54 +0000, Huang, Kai wrote:
> On Wed, 2023-04-05 at 16:45 -0700, Sean Christopherson wrote:
> > 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 <binbin.wu@linux.intel.com>
> > Cc: Kai Huang <kai.huang@intel.com>
> > Signed-off-by: Sean Christopherson <seanjc@google.com>
> 
> Reviewed-by: Kai Huang <kai.huang@intel.com>
> 
> > 

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

Tested-by: Kai Huang <kai.huang@intel.com>
  

Patch

diff --git a/arch/x86/kvm/vmx/sgx.c b/arch/x86/kvm/vmx/sgx.c
index aa53c98034bf..f881f6ff6408 100644
--- a/arch/x86/kvm/vmx/sgx.c
+++ b/arch/x86/kvm/vmx/sgx.c
@@ -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)