Skip to content

Commit e1779c2

Browse files
Maxim Levitskybonzini
authored andcommitted
KVM: x86: nSVM: fix potential NULL derefernce on nested migration
Turns out that due to review feedback and/or rebases I accidentally moved the call to nested_svm_load_cr3 to be too early, before the NPT is enabled, which is very wrong to do. KVM can't even access guest memory at that point as nested NPT is needed for that, and of course it won't initialize the walk_mmu, which is main issue the patch was addressing. Fix this for real. Fixes: 232f75d ("KVM: nSVM: call nested_svm_load_cr3 on nested state load") Cc: stable@vger.kernel.org Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com> Message-Id: <20220207155447.840194-3-mlevitsk@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent c53bbe2 commit e1779c2

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

arch/x86/kvm/svm/nested.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,18 +1457,6 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
14571457
!__nested_vmcb_check_save(vcpu, &save_cached))
14581458
goto out_free;
14591459

1460-
/*
1461-
* While the nested guest CR3 is already checked and set by
1462-
* KVM_SET_SREGS, it was set when nested state was yet loaded,
1463-
* thus MMU might not be initialized correctly.
1464-
* Set it again to fix this.
1465-
*/
1466-
1467-
ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1468-
nested_npt_enabled(svm), false);
1469-
if (WARN_ON_ONCE(ret))
1470-
goto out_free;
1471-
14721460

14731461
/*
14741462
* All checks done, we can enter guest mode. Userspace provides
@@ -1494,6 +1482,20 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
14941482

14951483
svm_switch_vmcb(svm, &svm->nested.vmcb02);
14961484
nested_vmcb02_prepare_control(svm);
1485+
1486+
/*
1487+
* While the nested guest CR3 is already checked and set by
1488+
* KVM_SET_SREGS, it was set when nested state was yet loaded,
1489+
* thus MMU might not be initialized correctly.
1490+
* Set it again to fix this.
1491+
*/
1492+
1493+
ret = nested_svm_load_cr3(&svm->vcpu, vcpu->arch.cr3,
1494+
nested_npt_enabled(svm), false);
1495+
if (WARN_ON_ONCE(ret))
1496+
goto out_free;
1497+
1498+
14971499
kvm_make_request(KVM_REQ_GET_NESTED_STATE_PAGES, vcpu);
14981500
ret = 0;
14991501
out_free:

0 commit comments

Comments
 (0)