Skip to content

Commit a585b87

Browse files
48casean-jc
authored andcommitted
KVM: selftests: Fix signedness issue with vCPU mmap size check
Check that the return value of KVM_GET_VCPU_MMAP_SIZE is non-negative before comparing with sizeof(kvm_run). If KVM_GET_VCPU_MMAP_SIZE fails, it will return -1, and `-1 > sizeof(kvm_run)` is true, so the ASSERT passes. There are no other locations in tools/testing/selftests/kvm that make the same mistake. Signed-off-by: James Houghton <jthoughton@google.com> Link: https://lore.kernel.org/r/20250711001742.1965347-1-jthoughton@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent c17b750 commit a585b87

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

tools/testing/selftests/kvm/lib/kvm_util.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ uint32_t guest_random_seed;
2424
struct guest_random_state guest_rng;
2525
static uint32_t last_guest_seed;
2626

27-
static int vcpu_mmap_sz(void);
27+
static size_t vcpu_mmap_sz(void);
2828

2929
int __open_path_or_exit(const char *path, int flags, const char *enoent_help)
3030
{
@@ -1321,14 +1321,14 @@ void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size,
13211321
}
13221322

13231323
/* Returns the size of a vCPU's kvm_run structure. */
1324-
static int vcpu_mmap_sz(void)
1324+
static size_t vcpu_mmap_sz(void)
13251325
{
13261326
int dev_fd, ret;
13271327

13281328
dev_fd = open_kvm_dev_path_or_exit();
13291329

13301330
ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
1331-
TEST_ASSERT(ret >= sizeof(struct kvm_run),
1331+
TEST_ASSERT(ret >= 0 && ret >= sizeof(struct kvm_run),
13321332
KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
13331333

13341334
close(dev_fd);
@@ -1369,7 +1369,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
13691369
TEST_ASSERT_VM_VCPU_IOCTL(vcpu->fd >= 0, KVM_CREATE_VCPU, vcpu->fd, vm);
13701370

13711371
TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size "
1372-
"smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
1372+
"smaller than expected, vcpu_mmap_sz: %zi expected_min: %zi",
13731373
vcpu_mmap_sz(), sizeof(*vcpu->run));
13741374
vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(),
13751375
PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);

0 commit comments

Comments
 (0)