Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH] KVM: nSVM: Reject EVENTINJ of #BR/#OF into a 64-bit L2
@ 2026-11-18 13:37 Manuel Andreas
  2026-11-18 13:55 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Manuel Andreas @ 2026-11-18 13:37 UTC (permalink / raw)
  To: kvm; +Cc: seanjc, pbonzini, Manuel Andreas

nested_svm_event_inj_valid_exept() treats BR_VECTOR and OF_VECTOR as
unconditionally valid for TYPE=3 (exception) injection.  However, the APM
(Vol 2, 15.20) states that injecting an event that is impossible for the
guest mode, e.g. #BR into a guest in 64-bit mode, makes VMRUN fail with
VMEXIT_INVALID.  BOUND and INTO are both invalid in 64-bit mode.

Because the check depends on both the control area (EVENTINJ) and the save
area (EFER/CR0/CS), add it to nested_svm_check_cached_vmcb12(), which covers
both nested VMRUN and RSM, rather than to nested_vmcb_check_controls().

Fixes: 7e79f71bca5c ("KVM: nSVM: Add missing consistency check for EVENTINJ")
Signed-off-by: Manuel Andreas <manuel.andreas@tum.de>
---
 arch/x86/kvm/svm/nested.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 1cecb82702e2..c69d334e3c3c 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -378,6 +378,8 @@ static bool nested_svm_event_inj_valid_exept(struct kvm_vcpu *vcpu, u8 vector)
  * set and:
  * - The type of event_inj is not one of the defined values.
  * - The type is SVM_EVTINJ_TYPE_EXEPT, but the vector is not a valid exception.
+ * - The event is impossible in the guest's mode. However this depends on
+ *   save state, see nested_svm_check_event_inj_mode().
  */
 static bool nested_svm_check_event_inj(struct kvm_vcpu *vcpu, u32 event_inj)
 {
@@ -398,6 +400,31 @@ static bool nested_svm_check_event_inj(struct kvm_vcpu *vcpu, u32 event_inj)
 	return true;
 }
 
+/*
+ * #BR and #OF are invalid in 64-bit mode, but not in compatibility mode,
+ * hence the CS.L check. VMRUN ignores EFER.LMA and derives
+ * long mode from EFER.LME, CR0.PG and CR4.PAE, so do the same.
+ */
+static bool nested_svm_check_event_inj_mode(u32 event_inj,
+					    struct vmcb_save_area_cached *save)
+{
+	u32 type = event_inj & SVM_EVTINJ_TYPE_MASK;
+	u8 vector = event_inj & SVM_EVTINJ_VEC_MASK;
+
+	if (!(event_inj & SVM_EVTINJ_VALID))
+		return true;
+
+	if (!((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG) &&
+	     (save->cr4 & X86_CR4_PAE) && (save->cs.attrib & SVM_SELECTOR_L_MASK)))
+		return true;
+
+	if (type == SVM_EVTINJ_TYPE_EXEPT &&
+	    (vector == BR_VECTOR || vector == OF_VECTOR))
+		return false;
+
+	return true;
+}
+
 static bool nested_vmcb_check_controls(struct kvm_vcpu *vcpu,
 				       struct vmcb_ctrl_area_cached *control)
 {
@@ -488,6 +515,11 @@ int nested_svm_check_cached_vmcb12(struct kvm_vcpu *vcpu)
 	    !nested_vmcb_check_controls(vcpu, &svm->nested.ctl))
 		return -EINVAL;
 
+	/* This check requires both save and control state. */
+	if (CC(!nested_svm_check_event_inj_mode(svm->nested.ctl.event_inj,
+						&svm->nested.save)))
+		return -EINVAL;
+
 	return 0;
 }
 

base-commit: cbe54fc3462b3949ab27b9ae81fb9ddb509040c7
-- 
2.56.0


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

* Re: [PATCH] KVM: nSVM: Reject EVENTINJ of #BR/#OF into a 64-bit L2
  2026-11-18 13:37 [PATCH] KVM: nSVM: Reject EVENTINJ of #BR/#OF into a 64-bit L2 Manuel Andreas
@ 2026-11-18 13:55 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-18 13:55 UTC (permalink / raw)
  To: Manuel Andreas, kvm

Thank you for your contribution! Sashiko AI review found no new issues
in this patch, but noticed 1 related pre-existing issue it does not
introduce:
- [High] KVM: nSVM: missing EFER.LMA normalization in
  nested_vmcb02_prepare_save() [1]
--

commit bd07241cd6a52030415a707aa3b0f77f55f74c7c
Author: Manuel Andreas <manuel.andreas@tum.de>

KVM: nSVM: Reject EVENTINJ of #BR/#OF into a 64-bit L2

This commit rejects VMCB12 EVENTINJ of #BR or #OF into a 64-bit L2 guest in
nested_svm_check_cached_vmcb12(), deriving 64-bit mode from EFER.LME,
CR0.PG, CR4.PAE, and CS.L.

> diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
> index 1cecb82702e2c..c69d334e3c3c5 100644
> --- a/arch/x86/kvm/svm/nested.c
> +++ b/arch/x86/kvm/svm/nested.c
[ ... ]
> @@ -398,6 +400,31 @@ static bool nested_svm_check_event_inj(struct kvm_vcpu *vcpu, u32 event_inj)
>  	return true;
>  }
>  
> +/*
> + * #BR and #OF are invalid in 64-bit mode, but not in compatibility mode,
> + * hence the CS.L check. VMRUN ignores EFER.LMA and derives
> + * long mode from EFER.LME, CR0.PG and CR4.PAE, so do the same.
> + */
> +static bool nested_svm_check_event_inj_mode(u32 event_inj,
> +					    struct vmcb_save_area_cached *save)
> +{
> +	u32 type = event_inj & SVM_EVTINJ_TYPE_MASK;
> +	u8 vector = event_inj & SVM_EVTINJ_VEC_MASK;
> +
> +	if (!(event_inj & SVM_EVTINJ_VALID))
> +		return true;
> +
> +	if (!((save->efer & EFER_LME) && (save->cr0 & X86_CR0_PG) &&
> +	     (save->cr4 & X86_CR4_PAE) && (save->cs.attrib & SVM_SELECTOR_L_MASK)))
> +		return true;

[Severity: High]
This problem wasn't introduced by this patch, but since VMRUN ignores
EFER.LMA, can nested_vmcb02_prepare_save() leave EFER_LMA clear in
vcpu->arch.efer [1] when entering a 64-bit L2 whose VMCB12 save area
has EFER_LME=1, X86_CR0_PG=1, and EFER_LMA=0?

arch/x86/kvm/svm/nested.c:nested_vmcb02_prepare_save() {
	...
	/* SVM ignores EFER.LMA if EFER.LME=0 (instead of failing VMRUN). */
	if (!(svm->nested.save.efer & EFER_LME))
		svm->nested.save.efer &= ~EFER_LMA;

	svm_set_efer(vcpu, svm->nested.save.efer);

	svm_set_cr0(vcpu, svm->nested.save.cr0);
	...
}

arch/x86/kvm/svm/svm.c:svm_set_cr0() {
	...
	if (vcpu->arch.efer & EFER_LME) {
		if (!is_paging(vcpu) && (cr0 & X86_CR0_PG)) {
			vcpu->arch.efer |= EFER_LMA;
	...
}

nested_vmcb02_prepare_save() only clears EFER_LMA when EFER_LME is clear,
and svm_set_cr0() only sets EFER_LMA when transitioning from !is_paging().
Because L1 already has paging enabled on VMRUN, is_paging(vcpu) is true
when svm_set_cr0() runs, leaving vcpu->arch.efer with EFER_LMA=0.

As a result, KVM miscomputes L2's long mode and MMU role state, and a
subsequent MSR_EFER write via set_efer() triggers WARN_ON_ONCE((efer &
EFER_LMA) != (vcpu->arch.efer & EFER_LMA)) and injects a spurious #GP into
L2.

[1] https://sashiko.dev/#/bug/linux-d0314b95-e275-4bc2-be19-05c208604a98

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118133741.1185904-1-manuel.andreas@tum.de?part=1

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

end of thread, other threads:[~2026-11-18 13:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-18 13:37 [PATCH] KVM: nSVM: Reject EVENTINJ of #BR/#OF into a 64-bit L2 Manuel Andreas
2026-11-18 13:55 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox