KVM: x86: Ensure userspace can't run L2 with stale PFNs
Sean Christopherson
maintainerv2 cover KVM: x86: Ensure userspace can't run L2 with stale PFNs
Fix a class of bugs where KVM (a) lets userspace run L2 with potentially stale PFNs and (b) fails to set kvm_run appropriately and so could unintentionally cause a well-behaved VMM to run afoul of (a).
v2:
- Re-pend KVM_REQ_GET_NESTED_STATE_PAGES in the common vcpu_enter_guest() path.
- Fix the kvm_run bug(s).
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (2):
KVM: x86: Re-pend GET_NESTED_STATE_PAGES if getting said pages fails
KVM: x86: Fill kvm_run exit fields in common get_nested_state_pages()
error paths
arch/x86/kvm/svm/nested.c | 7 +------ arch/x86/kvm/vmx/nested.c | 15 +++++---------- arch/x86/kvm/x86.c | 4 ++++ 3 files changed, 10 insertions(+), 16 deletions(-)
base-commit: 70c944caf570fda2d79baa71435589a8db39f048
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Sean Christopherson
maintainerPATCH v2 1/2 KVM: x86: Re-pend GET_NESTED_STATE_PAGES if getting said pages fails
Re-pend GET_NESTED_STATE_PAGES before exiting to userspace if getting the nested pages fails in the KVM_RUN path. If userspace re-runs the vCPU, and vmcs02 holds valid PFNs from the *previous* run of L2, then KVM could re-enter L2 with stale, unpinned PFNs mapped into e.g. the vAPIC page.
Note, both SVM and VMX (as of commit 11722439fb20 ("KVM: nVMX: Ensure KVM_REQ_GET_NESTED_STATE_PAGES is cleared on VM-Exit") ensure the request is cleared on VM-Exit (including the "forced" case), i.e. there is no risk of double-mapping due to emulated VMLAUNCH/VMRESUME/VMRUN *and* the request trying to map the nested pages.
--- arch/x86/kvm/x86.c | 1 + 1 file changed, 1 insertion(+)
arch/x86/kvm/x86.c +1 -0
@@ -8024,6 +8024,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu)) {if (unlikely (!kvm_nested_call (get_nested_state_pages)(vcpu))) { +kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu); r =0 ; goto out; }
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Sean Christopherson
maintainerPATCH v2 2/2 KVM: x86: Fill kvm_run exit fields in common get_nested_state_pages() error paths
In reply to Sean ChristophersonFill kvm_run with "internal error, emulation" in the common error handling paths for getting nested state pages, as requiring each check to manually fill kvm_run is error prone and requires a non-trivial amount of copy+paste.
Specifically, both SVM and VMX fail to fill kvm_run if load_pdptrs() fails, and SVM fails to fill kvm_run if kvm_hv_verify_vp_assist() fails. If those flows fail, the *best* case scenario is that KVM will exit to userspace with KVM_EXIT_UNKNOWN. The worst case scenario is that KVM exits with a stale exit_reason and confuses userspace.
Note, SVM never exits to userspace if something goes sideways when dealing with vmcb12 assets while emulating VMRUN, i.e. lack of SVM-specific code is not a bug.
--- arch/x86/kvm/svm/nested.c | 7 +------ arch/x86/kvm/vmx/nested.c | 15 +++++---------- arch/x86/kvm/x86.c | 3 +++ 3 files changed, 9 insertions(+), 16 deletions(-)
arch/x86/kvm/svm/nested.c +1 -6
@@ -2130,13 +2130,8 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)return false; } -if (!nested_svm_merge_msrpm (vcpu)) { - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror = -KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ; +if (!nested_svm_merge_msrpm (vcpu))return false; - }if (kvm_hv_verify_vp_assist (vcpu))return false;
arch/x86/kvm/vmx/nested.c +5 -10
@@ -3465,10 +3465,6 @@ static bool nested_get_vmcs12_pages(struct kvm_vcpu *vcpu) }else {pr_debug_ratelimited ("%s: no backing for APIC-access address in vmcs12\n" , __func__); - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror = -KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ;return false; } }
@@ -3539,11 +3535,6 @@ static bool vmx_get_nested_state_pages(struct kvm_vcpu *vcpu)if (!nested_get_evmcs_page (vcpu)) {pr_debug_ratelimited ("%s: enlightened vmptrld failed\n" , __func__); - vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; - vcpu->run->internal.suberror = -KVM_INTERNAL_ERROR_EMULATION ; - vcpu->run->internal.ndata =0 ; -return false; }#endif
@@ -3915,8 +3906,12 @@ static int nested_vmx_run(struct kvm_vcpu *vcpu, bool launch) vmentry_failed: vcpu->arch.nested_run_pending =0 ; -if (status ==NVMX_VMENTRY_KVM_INTERNAL_ERROR ) +if (status ==NVMX_VMENTRY_KVM_INTERNAL_ERROR ) { + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; + vcpu->run->internal.ndata =0 ;return 0 ; + }if (status ==NVMX_VMENTRY_VMEXIT )return 1 ;WARN_ON_ONCE (status !=NVMX_VMENTRY_VMFAIL );
arch/x86/kvm/x86.c +3 -0
@@ -8024,6 +8024,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)if (kvm_check_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu)) {if (unlikely (!kvm_nested_call (get_nested_state_pages)(vcpu))) { + vcpu->run->exit_reason =KVM_EXIT_INTERNAL_ERROR ; + vcpu->run->internal.suberror =KVM_INTERNAL_ERROR_EMULATION ; + vcpu->run->internal.ndata =0 ;kvm_make_request (KVM_REQ_GET_NESTED_STATE_PAGES , vcpu); r =0 ; goto out;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
Quoted 10 lines · Sean Christopherson
Fix a class of bugs where KVM (a) lets userspace run L2 with potentially stale PFNs and (b) fails to set
kvm_runappropriately and so could unintentionally cause a well-behaved VMM to run afoul of (a).v2:
- Re-pendKVM_REQ_GET_NESTED_STATE_PAGESin the commonvcpu_enter_guest()path.
- Fix thekvm_runbug(s).[...]
Applied to kvm-x86 fixes, thanks!
[1/2] KVM: x86: Re-pend GET_NESTED_STATE_PAGES if getting said pages fails
https://github.com/kvm-x86/linux/commit/10180a277549 [2/2] KVM: x86: Fill kvm_run exit fields in common get_nested_state_pages() error paths https://github.com/kvm-x86/linux/commit/c1214f293d77