Skip to contentSkip to conversation

riscv/kvm: fix guest vector leak on host alloc failure

PATCH In review Yufan Chen 2 participants 2 messages Mar 28

Subsystems (3)
lore
Share & tools
lore

Yufan Chen

author
#0 lore raw

PATCH 1/1 riscv/kvm: fix guest vector leak on host alloc failure

From: Yufan Chen <[email protected]>

When allocating vector context for a vCPU, guest_context.vector.datap is allocated before host_context.vector.datap. If the second allocation fails, the function returns -ENOMEM directly and leaks the guest buffer.

Switch the failure path to centralized cleanup. On host allocation failure, free guest_context.vector.datap, clear the pointer, and return -ENOMEM through a shared exit label.

---
 arch/riscv/kvm/vcpu_vector.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/riscv/kvm/vcpu_vector.c +10 -2
index 05f3cc2d8..4c2f92dce 100644
@@ -75,15 +75,23 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx)
 
 int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
 {
+	int rc = -ENOMEM;
+
 	vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
 	if (!vcpu->arch.guest_context.vector.datap)
-		return -ENOMEM;
+		goto out;
 
 	vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
 	if (!vcpu->arch.host_context.vector.datap)
-		return -ENOMEM;
+		goto free_guest_vector_datap;
 
 	return 0;
+
+free_guest_vector_datap:
+	kfree(vcpu->arch.guest_context.vector.datap);
+	vcpu->arch.guest_context.vector.datap = NULL;
+out:
+	return rc;
 }
 
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu)
Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 8 lines · Yufan Chen

From: Yufan Chen <[email protected]>

When allocating vector context for a vCPU, guest_context.vector.datap is allocated before host_context.vector.datap. If the second allocation fails, the function returns -ENOMEM directly and leaks the guest buffer.

Switch the failure path to centralized cleanup. On host allocation failure, free guest_context.vector.datap, clear the pointer, and return -ENOMEM through a shared exit label.

This is already fixed by patch "riscv: kvm: fix vector context allocation leak".

Regards,
Anup

Quoted 37 lines
---
 arch/riscv/kvm/vcpu_vector.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/riscv/kvm/vcpu_vector.c +10 -3
index 05f3cc2d8..4c2f92dce 100644
@@ -75,15 +75,23 @@ void kvm_riscv_vcpu_host_vector_restore(struct kvm_cpu_context *cntx)
 
 int kvm_riscv_vcpu_alloc_vector_context(struct kvm_vcpu *vcpu)
 {
+       int rc = -ENOMEM;
+
        vcpu->arch.guest_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!vcpu->arch.guest_context.vector.datap)
-               return -ENOMEM;
+               goto out;
 
        vcpu->arch.host_context.vector.datap = kzalloc(riscv_v_vsize, GFP_KERNEL);
        if (!vcpu->arch.host_context.vector.datap)
-               return -ENOMEM;
+               goto free_guest_vector_datap;
 
        return 0;
+
+free_guest_vector_datap:
+       kfree(vcpu->arch.guest_context.vector.datap);
+       vcpu->arch.guest_context.vector.datap = NULL;
+out:
+       return rc;
 }
 
 void kvm_riscv_vcpu_free_vector_context(struct kvm_vcpu *vcpu)
--

2.47.3