Skip to contentSkip to conversation

KVM: nSVM: Check reserved values for 'Type' and invalid vectors in EVENTINJ

PATCH Superseded Krish Sadhukhan 2 participants 4 messages Nov 30, 2020 – Dec 2, 2020

Subsystems (3)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover KVM: nSVM: Check reserved values for 'Type' and invalid vectors in EVENTINJ

v2 -> v3:
1. Rebased to kvm.git's nested-svm branch.
2. Simplied bracing in patch# 1.

According to sections "Canonicalization and Consistency Checks" and "Event Injection" in APM vol 2,

VMRUN exits with VMEXIT_INVALID error code if either:
- Reserved values of TYPE have been specified, or
- TYPE = 3 (exception) has been specified with a vector that does not correspond to an exception (this includes vector 2, which is an NMI, not an exception).

Patch# 1 adds these checks to KVM.
Patch# 2 adds tests for these checks.

[PATCH 1/2 v3] KVM: nSVM: Check reserved values for 'Type' and invalid [PATCH 2/2 v3] nSVM: Test reserved values for 'Type' and invalid vectors in

 arch/x86/include/asm/svm.h |  4 ++++
 arch/x86/kvm/svm/nested.c  | 14 ++++++++++++++
 2 files changed, 18 insertions(+)

Krish Sadhukhan (1):
KVM: nSVM: Check reserved values for 'Type' and invalid vectors in EVENTINJ

 x86/svm_tests.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)

Krish Sadhukhan (1):
nSVM: Test reserved values for 'Type' and invalid vectors in EVENTINJ

#1 lore raw

PATCH v3 1/2 KVM: nSVM: Check reserved values for 'Type' and invalid vectors in EVENTINJ

According to sections "Canonicalization and Consistency Checks" and "Event Injection" in APM vol 2

VMRUN exits with VMEXIT_INVALID error code if either:
- Reserved values of TYPE have been specified, or
- TYPE = 3 (exception) has been specified with a vector that does not correspond to an exception (this includes vector 2, which is an NMI, not an exception).

---
 arch/x86/include/asm/svm.h |  4 ++++
 arch/x86/kvm/svm/nested.c  | 14 ++++++++++++++
 2 files changed, 18 insertions(+)
arch/x86/include/asm/svm.h +4 -0
index 71d630bb5e08..d676f140cd19 100644
@@ -341,9 +341,13 @@ struct vmcb {
 #define SVM_EVTINJ_TYPE_MASK (7 << SVM_EVTINJ_TYPE_SHIFT)
 
 #define SVM_EVTINJ_TYPE_INTR (0 << SVM_EVTINJ_TYPE_SHIFT)
+#define SVM_EVTINJ_TYPE_RESV1 (1 << SVM_EVTINJ_TYPE_SHIFT)
 #define SVM_EVTINJ_TYPE_NMI (2 << SVM_EVTINJ_TYPE_SHIFT)
 #define SVM_EVTINJ_TYPE_EXEPT (3 << SVM_EVTINJ_TYPE_SHIFT)
 #define SVM_EVTINJ_TYPE_SOFT (4 << SVM_EVTINJ_TYPE_SHIFT)
+#define SVM_EVTINJ_TYPE_RESV5 (5 << SVM_EVTINJ_TYPE_SHIFT)
+#define SVM_EVTINJ_TYPE_RESV6 (6 << SVM_EVTINJ_TYPE_SHIFT)
+#define SVM_EVTINJ_TYPE_RESV7 (7 << SVM_EVTINJ_TYPE_SHIFT)
 
 #define SVM_EVTINJ_VALID (1 << 31)
 #define SVM_EVTINJ_VALID_ERR (1 << 11)
arch/x86/kvm/svm/nested.c +14 -0
index 05d564c8d034..2599b32ea7be 100644
@@ -212,6 +212,9 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
 
 static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
 {
+	u32 type, vector;
+	bool valid;
+
 	if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
 		return false;
 
@@ -222,6 +225,17 @@ static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
 	    !npt_enabled)
 		return false;
 
+	valid = control->event_inj & SVM_EVTINJ_VALID;
+	type = control->event_inj & SVM_EVTINJ_TYPE_MASK;
+	if (valid && type == SVM_EVTINJ_TYPE_RESV1 ||
+	    type >= SVM_EVTINJ_TYPE_RESV5)
+		return false;
+
+	vector = control->event_inj & SVM_EVTINJ_VEC_MASK;
+	if (valid && type == SVM_EVTINJ_TYPE_EXEPT &&
+	    vector == NMI_VECTOR || (vector > 31 && vector < 256))
+		return false;
+
 	return true;
 }
 
Signature · 1 line
2.27.0
#2 lore raw

arch/x86/kvm/svm/nested.c static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu) · line 215

Quoted 9 lines · Krish Sadhukhan
arch/x86/kvm/svm/nested.c +1 -0
index 05d564c8d034..2599b32ea7be 100644
@@ -212,6 +212,9 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
 
 static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
 {
+	u32 type, vector;

Making vector a u8 would be more intuitive, since its mask is 0xff.

arch/x86/kvm/svm/nested.c static bool nested_vmcb_check_controls(struct vmcb_control_area *control) · line 231

Quoted 13 lines
arch/x86/kvm/svm/nested.c +6 -0
+	bool valid;
+
 	if ((vmcb_is_intercept(control, INTERCEPT_VMRUN)) == 0)
 		return false;
 
@@ -222,6 +225,17 @@ static bool nested_vmcb_check_controls(struct vmcb_control_area *control)
 	    !npt_enabled)
 		return false;
 
+	valid = control->event_inj & SVM_EVTINJ_VALID;
+	type = control->event_inj & SVM_EVTINJ_TYPE_MASK;
+	if (valid && type == SVM_EVTINJ_TYPE_RESV1 ||
+	    type >= SVM_EVTINJ_TYPE_RESV5)

You were a little too aggressive in removing parantheses. :-) This breaks the build with CONFIG_KVM_WERROR=y due to not wrapping the chain of ANDs that are separated by ORs.

  /arch/x86/kvm/svm/nested.c: In function ‘nested_vmcb_check_controls’:
  /arch/x86/kvm/svm/nested.c:230:12: error: suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]
    230 |  if (valid && type == SVM_EVTINJ_TYPE_RESV1 ||
  /arch/x86/kvm/svm/nested.c:235:45: error: suggest parentheses around ‘&&’ within ‘||’ [-Werror=parentheses]
    235 |  if (valid && type == SVM_EVTINJ_TYPE_EXEPT &&

arch/x86/kvm/svm/nested.c static bool nested_vmcb_check_controls(struct vmcb_control_area *control) · line 236

Quoted 5 lines
arch/x86/kvm/svm/nested.c +5 -0
+		return false;
+
+	vector = control->event_inj & SVM_EVTINJ_VEC_MASK;
+	if (valid && type == SVM_EVTINJ_TYPE_EXEPT &&
+	    vector == NMI_VECTOR || (vector > 31 && vector < 256))

I'm pretty sure this is straight up wrong due to removing paranetheses. I assume the check is supposed to reject valid exceptions with (vector == 2 || vector > 31). As is, it rejects valid exceptions with vector ==2, OR any vector greater than 31. If for some reason this should reject any vector > 31, it'd be better to through that into a separate if statement.

Also, the "< 256" check is pointless, it will always be true since vector is an 8-bit value.

Quoted 8 lines
arch/x86/kvm/svm/nested.c +2 -0
+		return false;
+
 	return true;
 }
 
Signature · 1 line
2.27.0
#3 lore raw

PATCH v3 2/2 nSVM: Test reserved values for 'Type' and invalid vectors in EVENTINJ

In reply to Krish Sadhukhan

According to sections "Canonicalization and Consistency Checks" and "Event Injection" in APM vol 2

VMRUN exits with VMEXIT_INVALID error code if either:
- Reserved values of TYPE have been specified, or
- TYPE = 3 (exception) has been specified with a vector that does not correspond to an exception (this includes vector 2, which is an NMI, not an exception).

Existing tests already cover part of the second rule. This patch covers the the first rule and the missing pieces of the second rule.

---
 x86/svm_tests.c | 38 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 38 insertions(+)
x86/svm_tests.c +38 -0
index f78c9e4..b9be522 100644
@@ -2132,6 +2132,43 @@ static void test_dr(void)
 	vmcb->save.dr7 = dr_saved;
 }
 
+static void test_event_inject(void)
+{
+	u32 i;
+	u32 event_inj_saved = vmcb->control.event_inj;
+
+	handle_exception(DE_VECTOR, my_isr);
+
+	report (svm_vmrun() == SVM_EXIT_VMMCALL && count_exc == 0, "Test "
+	    "No EVENTINJ");
+
+	/*
+	 * Reserved values for 'Type' in EVENTINJ causes VMEXIT_INVALID.
+	 */
+	for (i = 1; i < 8; i++) {
+		if (i != 1 && i < 5)
+			continue;
+		vmcb->control.event_inj = DE_VECTOR |
+		    i << SVM_EVTINJ_TYPE_SHIFT | SVM_EVTINJ_VALID;
+		report(svm_vmrun() == SVM_EXIT_ERR && count_exc == 0,
+		    "Test invalid TYPE (%x) in EVENTINJ", i);
+	}
+
+	/*
+	 * Invalid vector number for event type 'exception' in EVENTINJ
+	 * causes VMEXIT_INVALID.
+	 */
+	for (i = 32; i < 256; i += 4) {
+		vmcb->control.event_inj = i | SVM_EVTINJ_TYPE_EXEPT |
+		    SVM_EVTINJ_VALID;
+		report(svm_vmrun() == SVM_EXIT_ERR && count_exc == 0,
+		    "Test invalid vector (%u) in EVENTINJ for event type "
+		    "\'exception\'", i);
+	}
+
+	vmcb->control.event_inj = event_inj_saved;
+}
+
 static void svm_guest_state_test(void)
 {
 	test_set_guest(basic_guest_main);
@@ -2141,6 +2178,7 @@ static void svm_guest_state_test(void)
 	test_cr3();
 	test_cr4();
 	test_dr();
+	test_event_inject();
 }
 
 struct svm_test svm_tests[] = {
Signature · 1 line
2.18.4