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 --- 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