Skip to contentSkip to conversation

KVM: svm: Avoid NULL pointer dereference in svm_hv_update_vp_id()

PATCH In review Vitaly Kuznetsov 2 participants 2 messages Jun 14, 2021

Subsystems (4)
lore
Share & tools
lore

Vitaly Kuznetsov

author maintainer
#0 lore raw

PATCH 1/1 KVM: svm: Avoid NULL pointer dereference in svm_hv_update_vp_id()

Hyper-V context is allocated dynamically when Hyper-V features are enabled on a vCPU but svm_hv_update_vp_id() is called unconditionally from svm_vcpu_run(), this leads to dereferencing to_hv_vcpu(vcpu) which can be NULL. Use kvm_hv_get_vpindex() wrapper to avoid the problem.

Fixes 4ba0d72aaa32 ("KVM: SVM: hyper-v: Direct Virtual Flush support")

---
- The patch introducing the issue is currently in kvm/queue.

---
 arch/x86/kvm/svm/svm_onhyperv.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm_onhyperv.h +3 -2
index ce23149670ea..9b9a55abc29f 100644
@@ -99,9 +99,10 @@ static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
 {
 	struct hv_enlightenments *hve =
 		(struct hv_enlightenments *)vmcb->control.reserved_sw;
+	u32 vp_index = kvm_hv_get_vpindex(vcpu);
 
-	if (hve->hv_vp_id != to_hv_vcpu(vcpu)->vp_index) {
-		hve->hv_vp_id = to_hv_vcpu(vcpu)->vp_index;
+	if (hve->hv_vp_id != vp_index) {
+		hve->hv_vp_id = vp_index;
 		vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
 	}
 }
Signature · 1 line
2.31.1
maintainer
#1 lore raw
Quoted 21 lines · Vitaly Kuznetsov

Hyper-V context is allocated dynamically when Hyper-V features are enabled on a vCPU but svm_hv_update_vp_id() is called unconditionally from svm_vcpu_run(), this leads to dereferencing to_hv_vcpu(vcpu) which can be NULL. Use kvm_hv_get_vpindex() wrapper to avoid the problem.

Fixes 4ba0d72aaa32 ("KVM: SVM: hyper-v: Direct Virtual Flush support")

---
- The patch introducing the issue is currently in kvm/queue.

---
  arch/x86/kvm/svm/svm_onhyperv.h | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/svm/svm_onhyperv.h +0 -0
index ce23149670ea..9b9a55abc29f 100644
@@ -99,9 +99,10 @@ static inline void svm_hv_update_vp_id(struct vmcb *vmcb,
  {
  	struct hv_enlightenments *hve =

arch/x86/kvm/svm/svm_onhyperv.h static inline void svm_hv_update_vp_id(struct vmcb *vmcb, · line 108

Quoted 10 lines
arch/x86/kvm/svm/svm_onhyperv.h +3 -2
  		(struct hv_enlightenments *)vmcb->control.reserved_sw;
+	u32 vp_index = kvm_hv_get_vpindex(vcpu);
 
-	if (hve->hv_vp_id != to_hv_vcpu(vcpu)->vp_index) {
-		hve->hv_vp_id = to_hv_vcpu(vcpu)->vp_index;
+	if (hve->hv_vp_id != vp_index) {
+		hve->hv_vp_id = vp_index;
  		vmcb_mark_dirty(vmcb, VMCB_HV_NESTED_ENLIGHTENMENTS);
  	}
  }

Squashed, thanks.

Paolo