riscv/kvm: fix guest vector leak on host alloc failure
Subsystems (3)
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
@@ -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
Anup Patel
maintainerQuoted 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
-ENOMEMdirectly 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
-ENOMEMthrough a shared exit label.Signed-off-by Yufan Chen <[email protected]>
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@@ -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