kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests
@ 2020-01-16  0:54 Krish Sadhukhan
  2020-01-16  0:54 ` [PATCH 1/2 " Krish Sadhukhan
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Krish Sadhukhan @ 2020-01-16  0:54 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini, jmattson

v2 -> v3:
	The following changes have been made to patch# 1:
	  i) Commit message body mentions the reason for doing this check in
	     software.
	  i) The CONFIG_X86_64 guard in nested_vmx_check_guest_state() is
	     removed.
	  ii) The data type of the parameter to kvm_dr7_valid() is
	      'unsigned long'.


[PATCH 1/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests
[PATCH 2/2 v3] kvm-unit-test: nVMX: Test GUEST_DR7 on vmentry of nested guests

 arch/x86/kvm/vmx/nested.c | 4 ++++
 arch/x86/kvm/x86.c        | 2 +-
 arch/x86/kvm/x86.h        | 6 ++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

Krish Sadhukhan (1):
      nVMX: Check GUEST_DR7 on vmentry of nested guests

 x86/vmx_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

Krish Sadhukhan (1):
      nVMX: Test GUEST_DR7 on vmentry of nested guests


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests
  2020-01-16  0:54 [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests Krish Sadhukhan
@ 2020-01-16  0:54 ` Krish Sadhukhan
  2020-01-16  0:54 ` [PATCH 2/2 v3] kvm-unit-test: nVMX: Test " Krish Sadhukhan
  2020-01-19  9:13 ` [PATCH 0/2 v3] KVM: nVMX: Check " Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Krish Sadhukhan @ 2020-01-16  0:54 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini, jmattson

According to section "Checks on Guest Control Registers, Debug Registers, and
and MSRs" in Intel SDM vol 3C, the following checks are performed on vmentry
of nested guests:

    If the "load debug controls" VM-entry control is 1, bits 63:32 in the DR7
    field must be 0.

In KVM, GUEST_DR7 is set prior to the vmcs02 VM-entry by kvm_set_dr() and the
latter synthesizes a #GP if any bit in the high dword in the former is set.
Hence this field needs to be checked in software.

Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
Reviewed-by: Karl Heubaum <karl.heubaum@oracle.com>
---
 arch/x86/kvm/vmx/nested.c | 4 ++++
 arch/x86/kvm/x86.c        | 2 +-
 arch/x86/kvm/x86.h        | 6 ++++++
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 4aea7d304beb..a5ee49ab75d5 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -2899,6 +2899,10 @@ static int nested_vmx_check_guest_state(struct kvm_vcpu *vcpu,
 	    CC(!nested_guest_cr4_valid(vcpu, vmcs12->guest_cr4)))
 		return -EINVAL;
 
+	if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_DEBUG_CONTROLS) &&
+	    CC(!kvm_dr7_valid(vmcs12->guest_dr7)))
+		return -EINVAL;
+
 	if ((vmcs12->vm_entry_controls & VM_ENTRY_LOAD_IA32_PAT) &&
 	    CC(!kvm_pat_valid(vmcs12->guest_ia32_pat)))
 		return -EINVAL;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index cf917139de6b..220f20a2f9c3 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -1064,7 +1064,7 @@ static int __kvm_set_dr(struct kvm_vcpu *vcpu, int dr, unsigned long val)
 	case 5:
 		/* fall through */
 	default: /* 7 */
-		if (val & 0xffffffff00000000ULL)
+		if (!kvm_dr7_valid(val))
 			return -1; /* #GP */
 		vcpu->arch.dr7 = (val & DR7_VOLATILE) | DR7_FIXED_1;
 		kvm_update_dr7(vcpu);
diff --git a/arch/x86/kvm/x86.h b/arch/x86/kvm/x86.h
index 29391af8871d..db42692bb209 100644
--- a/arch/x86/kvm/x86.h
+++ b/arch/x86/kvm/x86.h
@@ -369,6 +369,12 @@ static inline bool kvm_pat_valid(u64 data)
 	return (data | ((data & 0x0202020202020202ull) << 1)) == data;
 }
 
+static inline bool kvm_dr7_valid(unsigned long data)
+{
+	/* Bits [63:32] are reserved */
+	return !(data >> 32);
+}
+
 void kvm_load_guest_xsave_state(struct kvm_vcpu *vcpu);
 void kvm_load_host_xsave_state(struct kvm_vcpu *vcpu);
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2 v3] kvm-unit-test: nVMX: Test GUEST_DR7 on vmentry of nested guests
  2020-01-16  0:54 [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests Krish Sadhukhan
  2020-01-16  0:54 ` [PATCH 1/2 " Krish Sadhukhan
@ 2020-01-16  0:54 ` Krish Sadhukhan
  2020-01-19  9:13 ` [PATCH 0/2 v3] KVM: nVMX: Check " Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Krish Sadhukhan @ 2020-01-16  0:54 UTC (permalink / raw)
  To: kvm; +Cc: pbonzini, jmattson

According to section "Checks on Guest Control Registers, Debug Registers, and
and MSRs" in Intel SDM vol 3C, the following checks are performed on vmentry
of nested guests:

   If the "load debug controls" VM-entry control is 1,

      - bits 63:32 in the DR7 field must be 0.

Signed-off-by: Krish Sadhukhan <krish.sadhukhan@oracle.com>
Reviewed-by: Karl Heubaum <karl.heubaum@oracle.com>
Co-developed-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Jim Mattson <jmattson@google.com>
---
 x86/vmx_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)

diff --git a/x86/vmx_tests.c b/x86/vmx_tests.c
index fce773c..b773872 100644
--- a/x86/vmx_tests.c
+++ b/x86/vmx_tests.c
@@ -7442,6 +7442,49 @@ static void vmx_host_state_area_test(void)
 	test_load_host_perf_global_ctrl();
 }
 
+/*
+ * If the "load debug controls" VM-entry control is 1, bits 63:32 in
+ * the DR7 field must be 0.
+ *
+ * [Intel SDM]
+ */
+static void test_guest_dr7(void)
+{
+	u32 ent_saved = vmcs_read(ENT_CONTROLS);
+	u64 dr7_saved = vmcs_read(GUEST_DR7);
+	u64 val;
+	int i;
+
+	if (ctrl_enter_rev.set & ENT_LOAD_DBGCTLS) {
+		vmcs_clear_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS);
+		for (i = 0; i < 64; i++) {
+			val = 1ull << i;
+			vmcs_write(GUEST_DR7, val);
+			enter_guest();
+			report_guest_state_test("ENT_LOAD_DBGCTLS disabled",
+						VMX_VMCALL, val, "GUEST_DR7");
+		}
+	}
+	if (ctrl_enter_rev.clr & ENT_LOAD_DBGCTLS) {
+		vmcs_set_bits(ENT_CONTROLS, ENT_LOAD_DBGCTLS);
+		for (i = 0; i < 64; i++) {
+			val = 1ull << i;
+			vmcs_write(GUEST_DR7, val);
+			if (i < 32)
+				enter_guest();
+			else
+				enter_guest_with_invalid_guest_state();
+			report_guest_state_test("ENT_LOAD_DBGCTLS enabled",
+						i < 32 ? VMX_VMCALL :
+						VMX_ENTRY_FAILURE |
+						VMX_FAIL_STATE,
+						val, "GUEST_DR7");
+		}
+	}
+	vmcs_write(GUEST_DR7, dr7_saved);
+	vmcs_write(ENT_CONTROLS, ent_saved);
+}
+
 /*
  *  If the "load IA32_PAT" VM-entry control is 1, the value of the field
  *  for the IA32_PAT MSR must be one that could be written by WRMSR
@@ -7480,6 +7523,7 @@ static void vmx_guest_state_area_test(void)
 	test_canonical(GUEST_SYSENTER_ESP, "GUEST_SYSENTER_ESP", false);
 	test_canonical(GUEST_SYSENTER_EIP, "GUEST_SYSENTER_EIP", false);
 
+	test_guest_dr7();
 	test_load_guest_pat();
 	test_guest_efer();
 	test_load_guest_perf_global_ctrl();
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests
  2020-01-16  0:54 [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests Krish Sadhukhan
  2020-01-16  0:54 ` [PATCH 1/2 " Krish Sadhukhan
  2020-01-16  0:54 ` [PATCH 2/2 v3] kvm-unit-test: nVMX: Test " Krish Sadhukhan
@ 2020-01-19  9:13 ` Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2020-01-19  9:13 UTC (permalink / raw)
  To: Krish Sadhukhan, kvm; +Cc: jmattson

On 16/01/20 01:54, Krish Sadhukhan wrote:
> v2 -> v3:
> 	The following changes have been made to patch# 1:
> 	  i) Commit message body mentions the reason for doing this check in
> 	     software.
> 	  i) The CONFIG_X86_64 guard in nested_vmx_check_guest_state() is
> 	     removed.
> 	  ii) The data type of the parameter to kvm_dr7_valid() is
> 	      'unsigned long'.
> 
> 
> [PATCH 1/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests
> [PATCH 2/2 v3] kvm-unit-test: nVMX: Test GUEST_DR7 on vmentry of nested guests
> 
>  arch/x86/kvm/vmx/nested.c | 4 ++++
>  arch/x86/kvm/x86.c        | 2 +-
>  arch/x86/kvm/x86.h        | 6 ++++++
>  3 files changed, 11 insertions(+), 1 deletion(-)
> 
> Krish Sadhukhan (1):
>       nVMX: Check GUEST_DR7 on vmentry of nested guests
> 
>  x86/vmx_tests.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 44 insertions(+)
> 
> Krish Sadhukhan (1):
>       nVMX: Test GUEST_DR7 on vmentry of nested guests
> 

Queued, thanks.

Paolo


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-01-19  9:13 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-16  0:54 [PATCH 0/2 v3] KVM: nVMX: Check GUEST_DR7 on vmentry of nested guests Krish Sadhukhan
2020-01-16  0:54 ` [PATCH 1/2 " Krish Sadhukhan
2020-01-16  0:54 ` [PATCH 2/2 v3] kvm-unit-test: nVMX: Test " Krish Sadhukhan
2020-01-19  9:13 ` [PATCH 0/2 v3] KVM: nVMX: Check " Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).