Skip to contentSkip to conversation

x86: nVMX: test APIC reg features

PATCH New Paolo Bonzini 1 participant 7 messages Apr 5, 2019 KVM

lore
Share & tools
lore

Paolo Bonzini

author maintainer
#0 lore raw

cover x86: nVMX: test APIC reg features

This patch series adds test for nested "APIC-register virtualization" and nested "virtualize x2APIC mode".

Paolo

Marc Orr (6):
Test nested APIC-register virtualization Test odd/incorrect APIC-register virt configs
apic_reg_virt_test: Make APIC-access addr 4k page fix vmx_apic_reg_virt for older platforms Test VMX's virtualize x2APIC mode w/ nested Add leak scenario to virt_x2apic_mode_test

 lib/x86/apic-defs.h |    1 +
 lib/x86/apic.h      |   28 +
 lib/x86/processor.h |   15 +
 lib/x86/vm.c        |   83 +++
 lib/x86/vm.h        |    3 +
 x86/vmexit.c        |    5 -
 x86/vmx.c           |   75 ++-
 x86/vmx.h           |    1 +
 x86/vmx_tests.c     | 1427 +++++++++++++++++++++++++++++++++++++++++++++++++++
 9 files changed, 1614 insertions(+), 24 deletions(-)
Signature · 1 line
1.8.3.1
author maintainer
#1 lore raw

PATCH 1/6 Test nested APIC-register virtualization

From: Marc Orr <[email protected]>

This patch adds a test to validate the APIC-register virtualization execution control with nested virtualization. Specific test cases
include: APIC-access virtualization, APIC-access virtualization with Use TPR Shadow, and APIC-register virtualization.

---
 lib/x86/apic.h  |   6 +
 x86/vmx_tests.c | 442 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 448 insertions(+)
lib/x86/apic.h +6 -0
index be8e96c..a6a7a6a 100644
@@ -56,4 +56,10 @@ int enable_x2apic(void);
 void disable_apic(void);
 void reset_apic(void);
 
+/* Converts byte-addressable APIC register offset to 4-byte offset. */
+static inline u32 apic_reg_index(u32 reg)
+{
+	return reg >> 2;
+}
+
 #endif
x86/vmx_tests.c +442 -0

454 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#2 lore raw

PATCH 2/6 Test odd/incorrect APIC-register virt configs

In reply to Paolo Bonzini

From: Marc Orr <[email protected]>

This patch extends apic_reg_virt_test to test some odd/incorrect VMCS configurations, where writing the APIC registers behaves oddly (e.g., writes the APIC access page rather than the APIC virtualization page) or simply fails VM entry due to invalid VMCS controls.

---
 x86/vmx.c       |  75 +++++++++++----
 x86/vmx.h       |   1 +
 x86/vmx_tests.c | 282 +++++++++++++++++++++++++++++++++++++++++++++++---------
 3 files changed, 295 insertions(+), 63 deletions(-)
x86/vmx.c +56 -19
index 6ba56bc..f125043 100644
@@ -1763,6 +1763,30 @@ void test_set_guest(test_guest_func func)
 	v2_guest_main = func;
 }
 
+static void check_for_guest_termination(void)
+{
+	if (is_hypercall()) {
+		int ret;
+
+		ret = handle_hypercall();
+		switch (ret) {
+		case VMX_TEST_VMEXIT:
+			guest_finished = 1;
+			break;
+		case VMX_TEST_VMABORT:
+			continue_abort();
+			break;
+		case VMX_TEST_VMSKIP:
+			continue_skip();
+			break;
+		default:
+			printf("ERROR : Invalid handle_hypercall return %d.\n",
+			       ret);
+			abort();
+		}
+	}
+}
+
 /*
  * Enters the guest (or launches it for the first time). Error to call once the
  * guest has returned (i.e., run past the end of its guest() function). Also
@@ -1785,26 +1809,39 @@ void enter_guest(void)
 
 	launched = 1;
 
-	if (is_hypercall()) {
-		int ret;
+	check_for_guest_termination();
+}
 
-		ret = handle_hypercall();
-		switch (ret) {
-		case VMX_TEST_VMEXIT:
-			guest_finished = 1;
-			break;
-		case VMX_TEST_VMABORT:
-			continue_abort();
-			break;
-		case VMX_TEST_VMSKIP:
-			continue_skip();
-			break;
-		default:
-			printf("ERROR : Invalid handle_hypercall return %d.\n",
-			       ret);
-			abort();
-		}
-	}
+void enter_guest_with_bad_controls(void)
+{
+	struct vmentry_failure failure;
+	bool ok;
+
+	TEST_ASSERT_MSG(v2_guest_main,
+			"Never called test_set_guest_func!");
+
+	TEST_ASSERT_MSG(!guest_finished,
+			"Called enter_guest() after guest returned.");
+
+	ok = vmx_enter_guest(&failure);
+	report_xfail("vmlaunch fails, as expected",
+		     true, ok);
+	report("failure occurred early", failure.early);
+	report("FLAGS set correctly",
+	       (failure.flags & VMX_ENTRY_FLAGS) == X86_EFLAGS_ZF);
+	report("VM-Inst Error # is %d (VM entry with invalid control field(s))",
+	       vmcs_read(VMX_INST_ERROR) == VMXERR_ENTRY_INVALID_CONTROL_FIELD,
+	       VMXERR_ENTRY_INVALID_CONTROL_FIELD);
+
+	/*
+	 * This if statement shouldn't fire, as the entire premise of this
+	 * function is that VM entry is expected to fail, rather than succeed
+	 * and execute to termination. However, if the VM entry does
+	 * unexpectedly succeed, it's nice to check whether the guest has
+	 * terminated, to reduce the number of error messages.
+	 */
+	if (ok)
+		check_for_guest_termination();
 }
 
 extern struct vmx_test vmx_tests[];
x86/vmx.h +1 -0
index bcfaa79..d740a66 100644
@@ -812,6 +812,7 @@ bool ept_execute_only_supported(void);
 bool ept_ad_bits_supported(void);
 
 void enter_guest(void);
+void enter_guest_with_bad_controls(void);
 
 typedef void (*test_guest_func)(void);
 typedef void (*test_teardown_func)(void *data);
x86/vmx_tests.c +238 -44

508 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#3 lore raw

PATCH 3/6 apic_reg_virt_test: Make APIC-access addr 4k page

In reply to Paolo Bonzini

From: Marc Orr <[email protected]>

This patch introduces split_large_page(), which is used to force the APIC-access address to be a 4k page. Otherwise, the apic_reg_virt_test fails on upstream.

---
 lib/x86/processor.h | 10 +++++++
 lib/x86/vm.c        | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/x86/vm.h        |  3 ++
 x86/vmx_tests.c     |  6 ++++
 4 files changed, 102 insertions(+)
lib/x86/processor.h +10 -0
index 916e67d..6f77148 100644
@@ -36,6 +36,7 @@
 #define X86_CR4_PSE    0x00000010
 #define X86_CR4_PAE    0x00000020
 #define X86_CR4_MCE    0x00000040
+#define X86_CR4_PGE    0x00000080
 #define X86_CR4_PCE    0x00000100
 #define X86_CR4_UMIP   0x00000800
 #define X86_CR4_VMXE   0x00002000
@@ -477,4 +478,13 @@ static inline void set_bit(int bit, u8 *addr)
 			     : "+m" (*addr) : "Ir" (bit) : "cc", "memory");
 }
 
+static inline void flush_tlb(void)
+{
+	ulong cr4;
+
+	cr4 = read_cr4();
+	write_cr4(cr4 ^ X86_CR4_PGE);
+	write_cr4(cr4);
+}
+
 #endif
lib/x86/vm.c +83 -0

86 lines not on this page · raw message

lib/x86/vm.h +3 -0
index 729f172..8750a1e 100644
@@ -42,4 +42,7 @@ static inline void *current_page_table(void)
 {
 	return phys_to_virt(read_cr3());
 }
+
+void split_large_page(unsigned long *ptep, int level);
+void force_4k_page(void *addr);
 #endif
x86/vmx_tests.c +6 -0
index 73aca95..092e70e 100644
@@ -5673,7 +5673,13 @@ static void apic_reg_virt_test(void)
 
 	test_set_guest(apic_reg_virt_guest);
 
+	/*
+	 * From the SDM: The 1-setting of the "virtualize APIC accesses"
+	 * VM-execution is guaranteed to apply only if translations to the
+	 * APIC-access address use a 4-KByte page.
+	 */
 	apic_access_address = alloc_page();
+	force_4k_page(apic_access_address);
 	vmcs_write(APIC_ACCS_ADDR, virt_to_phys(apic_access_address));
 
 	virtual_apic_page = alloc_page();
Signature · 1 line
1.8.3.1
author maintainer
#4 lore raw

PATCH 4/6 fix vmx_apic_reg_virt for older platforms

In reply to Paolo Bonzini

From: Marc Orr <[email protected]>

This test was failing because "Use TPR shadow" virtualization behaves differently across platforms. For example, on Sandy Bridge the upper three bytes of the VTPR are cleared upon VM entry, whereas they are left as is on Skylake.

This difference in behavior is consistent with the SDM, which according to Volume 3, Section 26.2.1.1 VM-Execution Control Fields, says:
... bytes 3:1 of VTPR may be cleared (behavior may be implementation-specific). ...

---
 x86/vmx_tests.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
x86/vmx_tests.c +9 -2
index 092e70e..41c763d 100644
@@ -5405,6 +5405,7 @@ struct apic_reg_virt_guest_args {
 	u32 reg;
 	u32 val;
 	bool check_rd;
+	u32 (*virt_fn)(u32);
 } apic_reg_virt_guest_args;
 
 static void apic_reg_virt_guest(void)
@@ -5418,6 +5419,7 @@ static void apic_reg_virt_guest(void)
 		u32 reg = args->reg;
 		u32 val = args->val;
 		bool check_rd = args->check_rd;
+		u32 (*virt_fn)(u32) = args->virt_fn;
 
 		if (op == TERMINATE)
 			break;
@@ -5425,9 +5427,13 @@ static void apic_reg_virt_guest(void)
 		if (op == APIC_OP_XAPIC_RD) {
 			u32 ret = vmx_xapic_read(apic_access_address, reg);
 
-			if (check_rd)
+			if (check_rd) {
+				u32 want = virt_fn(val);
+				u32 got = virt_fn(ret);
+
 				report("read 0x%x, expected 0x%x.",
-				       ret == val, ret, val);
+				       got == want, got, want);
+			}
 		} else if (op == APIC_OP_XAPIC_WR) {
 			vmx_xapic_write(apic_access_address, reg, val);
 		}
@@ -5457,6 +5463,7 @@ static void test_xapic_rd(
 	args->reg = reg;
 	args->val = val;
 	args->check_rd = exit_reason_want == VMX_VMCALL;
+	args->virt_fn = expectation->virt_fn;
 
 	/* Setup virtual APIC page */
 	if (!expectation->virtualize_apic_accesses) {
Signature · 1 line
1.8.3.1
author maintainer
#5 lore raw

PATCH 5/6 Test VMX's virtualize x2APIC mode w/ nested

In reply to Paolo Bonzini

From: Marc Orr <[email protected]>

This patch extends x86/vmx_tests.c to test enabling virtualize x2APIC mode for nested VMX. The basic premises of the test is to pass values between L1 and L2 via the virtual APIC page. Emphasis is placed on validating that L2 can never read/write L0's APIC registers, which would be disastrous.

Note, this test was used to detect and fix the issue described in the KVM patch titled "KVM: x86: nVMX: fix x2APIC VTPR read intercept".

---
 lib/x86/apic-defs.h |   1 +
 lib/x86/apic.h      |  22 ++
 x86/vmx_tests.c     | 755 +++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 774 insertions(+), 4 deletions(-)
lib/x86/apic-defs.h +1 -0
index a7bc6a0..e7c3e92 100644
@@ -56,6 +56,7 @@
 #define		APIC_ESR_SENDILL	0x00020
 #define		APIC_ESR_RECVILL	0x00040
 #define		APIC_ESR_ILLREGA	0x00080
+#define	APIC_CMCI	0x2F0
 #define	APIC_ICR	0x300
 #define		APIC_DEST_SELF		0x40000
 #define		APIC_DEST_ALLINC	0x80000
lib/x86/apic.h +22 -0
index a6a7a6a..651124d 100644
@@ -62,4 +62,26 @@ static inline u32 apic_reg_index(u32 reg)
 	return reg >> 2;
 }
 
+static inline u32 x2apic_msr(u32 reg)
+{
+	return APIC_BASE_MSR + (reg >> 4);
+}
+
+static inline bool x2apic_reg_reserved(u32 reg)
+{
+	switch (reg) {
+	case 0x000 ... 0x010:
+	case 0x040 ... 0x070:
+	case 0x0c0:
+	case 0x0e0:
+	case 0x290 ... 0x2e0:
+	case 0x310:
+	case 0x3a0 ... 0x3d0:
+	case 0x3f0:
+		return true;
+	default:
+		return false;
+	}
+}
+
 #endif
x86/vmx_tests.c +751 -4

792 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author maintainer
#6 lore raw

PATCH 6/6 Add leak scenario to virt_x2apic_mode_test

In reply to Paolo Bonzini

From: Marc Orr <[email protected]>

While working on virt_x2apic_mode_test, we noticed that KVM allows for a buggy or malicious L1 to get at L0's x2APIC MSRs via nested. The issue is in KVM's nested_vmx_prepare_msr_bitmap() function. Specifically, an L1 can execute the following sequence:

1. WRMSR(IA32_SPEC_CTRL, 1), which causes the spec_ctrl variable, in nested_vmx_prepare_msr_bitmap() to become true.
2. Clear "virtualize x2APIC mode"
3. Set "APIC-register virualization"

Then, KVM will copy L1's MSR bitmap for the x2APIC MSR range into L0, and run L2 with "Virtualize x2APIC mode" disabled, which gives L2 unfettered access to L0's x2APIC msrs.

Thus, this patch extends virt_x2apic_mode_test with a test case for this scenario.

Note, this patch was used to discover and fix the issue described in the KVM patch titled "KVM: x86: nVMX: close leak of L0's x2APIC MSRs".

---
 lib/x86/processor.h |  5 +++++
 x86/vmexit.c        |  5 -----
 x86/vmx_tests.c     | 31 +++++++++++++++++++++++++++++++
 3 files changed, 36 insertions(+), 5 deletions(-)
lib/x86/processor.h +5 -0
index 6f77148..a4ef315 100644
@@ -487,4 +487,9 @@ static inline void flush_tlb(void)
 	write_cr4(cr4);
 }
 
+static inline int has_spec_ctrl(void)
+{
+    return !!(cpuid_indexed(7,0).d & (1 << 26));
+}
+
 #endif
x86/vmexit.c +0 -5
index c12dd24..f094f53 100644
@@ -417,11 +417,6 @@ static void tscdeadline(void)
 	while (x == 0) barrier();
 }
 
-static int has_spec_ctrl(void)
-{
-    return !!(cpuid_indexed(7,0).d & (1 << 26));
-}
-
 static void wr_ibrs_msr(void)
 {
 	wrmsr(MSR_IA32_SPEC_CTRL, 1);
x86/vmx_tests.c +31 -0
index 12d80df..868f284 100644
@@ -6106,6 +6106,27 @@ struct virt_x2apic_mode_test_case virt_x2apic_mode_tests[] = {
 			},
 		},
 	},
+
+	/*
+	 * Disable "Virtualize x2APIC mode", disable x2APIC MSR intercepts, and
+	 * enable "APIC-register virtualization" --> L2 gets L1's x2APIC MSRs.
+	 */
+	{
+		.name = "Baseline",
+		.virt_x2apic_mode_config = {
+			.virtual_interrupt_delivery = true,
+			.use_msr_bitmaps = true,
+			.disable_x2apic_msr_intercepts = true,
+			.disable_x2apic = false,
+			.apic_reg_virt_config = {
+				.apic_register_virtualization = true,
+				.use_tpr_shadow = true,
+				.virtualize_apic_accesses = false,
+				.virtualize_x2apic_mode = false,
+				.activate_secondary_controls = true,
+			},
+		},
+	},
 };
 
 enum X2apic_op {
@@ -6423,6 +6444,16 @@ static void virt_x2apic_mode_test(void)
 	struct virt_x2apic_mode_guest_args *args = &virt_x2apic_mode_guest_args;
 
 	/*
+	 * This is to exercise an issue in KVM's logic to merge L0's and L1's
+	 * MSR bitmaps. Previously, an L1 could get at L0's x2APIC MSRs by
+	 * writing the IA32_SPEC_CTRL MSR or the IA32_PRED_CMD MSRs. KVM would
+	 * then proceed to manipulate the MSR bitmaps, as if VMCS12 had the
+	 * "Virtualize x2APIC mod" control set, even when it didn't.
+	 */
+	if (has_spec_ctrl())
+		wrmsr(MSR_IA32_SPEC_CTRL, 1);
+
+	/*
 	 * Check that VMCS12 supports:
 	 *   - "Virtual-APIC address", indicated by "use TPR shadow"
 	 *   - "MSR-bitmap address", indicated by "use MSR bitmaps"
Signature · 1 line
1.8.3.1