kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check
@ 2025-07-11  0:17 James Houghton
  2025-07-11  0:29 ` James Houghton
  2025-08-19 23:12 ` Sean Christopherson
  0 siblings, 2 replies; 3+ messages in thread
From: James Houghton @ 2025-07-11  0:17 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson
  Cc: James Houghton, Venkatesh Srinivas, kvm, linux-kernel

Check that the return value of KVM_GET_VCPU_MMAP_SIZE is non-negative
before comparing with sizeof(vcpu_run). If KVM_GET_VCPU_MMAP_SIZE fails,
it will return -1, and `-1 > sizeof(vcpu_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>
---
 tools/testing/selftests/kvm/lib/kvm_util.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c
index a055343a7bf75..7f870c99e64e0 100644
--- a/tools/testing/selftests/kvm/lib/kvm_util.c
+++ b/tools/testing/selftests/kvm/lib/kvm_util.c
@@ -24,7 +24,7 @@ uint32_t guest_random_seed;
 struct guest_random_state guest_rng;
 static uint32_t last_guest_seed;
 
-static int vcpu_mmap_sz(void);
+static size_t vcpu_mmap_sz(void);
 
 int open_path_or_exit(const char *path, int flags)
 {
@@ -1307,14 +1307,14 @@ void vm_guest_mem_fallocate(struct kvm_vm *vm, uint64_t base, uint64_t size,
 }
 
 /* Returns the size of a vCPU's kvm_run structure. */
-static int vcpu_mmap_sz(void)
+static size_t vcpu_mmap_sz(void)
 {
 	int dev_fd, ret;
 
 	dev_fd = open_kvm_dev_path_or_exit();
 
 	ret = ioctl(dev_fd, KVM_GET_VCPU_MMAP_SIZE, NULL);
-	TEST_ASSERT(ret >= sizeof(struct kvm_run),
+	TEST_ASSERT(ret >= 0 && ret >= sizeof(struct kvm_run),
 		    KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
 
 	close(dev_fd);
@@ -1355,7 +1355,7 @@ struct kvm_vcpu *__vm_vcpu_add(struct kvm_vm *vm, uint32_t vcpu_id)
 	TEST_ASSERT_VM_VCPU_IOCTL(vcpu->fd >= 0, KVM_CREATE_VCPU, vcpu->fd, vm);
 
 	TEST_ASSERT(vcpu_mmap_sz() >= sizeof(*vcpu->run), "vcpu mmap size "
-		"smaller than expected, vcpu_mmap_sz: %i expected_min: %zi",
+		"smaller than expected, vcpu_mmap_sz: %zi expected_min: %zi",
 		vcpu_mmap_sz(), sizeof(*vcpu->run));
 	vcpu->run = (struct kvm_run *) mmap(NULL, vcpu_mmap_sz(),
 		PROT_READ | PROT_WRITE, MAP_SHARED, vcpu->fd, 0);
-- 
2.50.0.727.gbf7dc18ff4-goog


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check
  2025-07-11  0:17 [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check James Houghton
@ 2025-07-11  0:29 ` James Houghton
  2025-08-19 23:12 ` Sean Christopherson
  1 sibling, 0 replies; 3+ messages in thread
From: James Houghton @ 2025-07-11  0:29 UTC (permalink / raw)
  To: Paolo Bonzini, Sean Christopherson; +Cc: Venkatesh Srinivas, kvm, linux-kernel

On Thu, Jul 10, 2025 at 5:17 PM James Houghton <jthoughton@google.com> wrote:
>
> Check that the return value of KVM_GET_VCPU_MMAP_SIZE is non-negative
> before comparing with sizeof(vcpu_run). If KVM_GET_VCPU_MMAP_SIZE fails,
> it will return -1, and `-1 > sizeof(vcpu_run)` is true, so the ASSERT
> passes.

:%s/vcpu_run/kvm_run/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check
  2025-07-11  0:17 [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check James Houghton
  2025-07-11  0:29 ` James Houghton
@ 2025-08-19 23:12 ` Sean Christopherson
  1 sibling, 0 replies; 3+ messages in thread
From: Sean Christopherson @ 2025-08-19 23:12 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini, James Houghton
  Cc: Venkatesh Srinivas, kvm, linux-kernel

On Fri, 11 Jul 2025 00:17:42 +0000, James Houghton wrote:
> Check that the return value of KVM_GET_VCPU_MMAP_SIZE is non-negative
> before comparing with sizeof(vcpu_run). If KVM_GET_VCPU_MMAP_SIZE fails,
> it will return -1, and `-1 > sizeof(vcpu_run)` is true, so the ASSERT
> passes.
> 
> There are no other locations in tools/testing/selftests/kvm that make
> the same mistake.
> 
> [...]

Applied to kvm-x86 selftests, thanks!

[1/1] KVM: selftests: Fix signedness issue with vCPU mmap size check
      https://github.com/kvm-x86/linux/commit/a585b8761451

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-08-19 23:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-11  0:17 [PATCH] KVM: selftests: Fix signedness issue with vCPU mmap size check James Houghton
2025-07-11  0:29 ` James Houghton
2025-08-19 23:12 ` Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).