[PATCH] selftests: kvm: Check calloc() return in vcpu_get_reg_list()

Chaithanya Lagisetty posted 1 patch 3 weeks, 4 days ago
tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
1 file changed, 1 insertion(+)
[PATCH] selftests: kvm: Check calloc() return in vcpu_get_reg_list()
Posted by Chaithanya Lagisetty 3 weeks, 4 days ago
vcpu_get_reg_list() allocates reg_list with calloc() and immediately
dereferences it via reg_list->n without checking for allocation failure,
unlike every other allocation in this file which is guarded by
TEST_ASSERT(). If calloc() returns NULL the test crashes with a NULL
pointer dereference instead of a clean failure message.

Add the missing TEST_ASSERT() check.

Fixes: fd02029a9e01 ("KVM: selftests: Add aarch64 get-reg-list test")
Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index 9ddc047d5c27..d15f39900ed7 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -1727,6 +1727,7 @@ struct kvm_reg_list *vcpu_get_reg_list(struct kvm_vcpu *vcpu)
 	TEST_ASSERT(ret == -1 && errno == E2BIG, "KVM_GET_REG_LIST n=0");
 
 	reg_list = calloc(1, sizeof(*reg_list) + reg_list_n.n * sizeof(__u64));
+	TEST_ASSERT(reg_list, "Failed to allocate reg_list");
 	reg_list->n = reg_list_n.n;
 	vcpu_ioctl(vcpu, KVM_GET_REG_LIST, reg_list);
 	return reg_list;
-- 
2.43.0
Re: [PATCH] selftests: kvm: Check calloc() return in vcpu_get_reg_list()
Posted by Sean Christopherson 4 days, 22 hours ago
On Tue, 01 Sep 2026 09:08:38 +0000, Chaithanya Lagisetty wrote:
> vcpu_get_reg_list() allocates reg_list with calloc() and immediately
> dereferences it via reg_list->n without checking for allocation failure,
> unlike every other allocation in this file which is guarded by
> TEST_ASSERT(). If calloc() returns NULL the test crashes with a NULL
> pointer dereference instead of a clean failure message.
> 
> Add the missing TEST_ASSERT() check.
> 
> [...]

Applied to kvm-x86 selftests, thanks!

[1/1] selftests: kvm: Check calloc() return in vcpu_get_reg_list()
      https://github.com/kvm-x86/linux/commit/b57917fc2b26

--
https://github.com/kvm-x86/linux/tree/next