Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

KVM: selftests: Fix mostly theoretical leak of VM's binary stats FD

When allocating and freeing a VM's cached binary stats info, check for a
NULL descriptor, not a '0' file descriptor, as '0' is a legal FD. E.g. in
the unlikely scenario the kernel installs the stats FD at entry '0',
selftests would reallocate on the next __vm_get_stat() and/or fail to free
the stats in kvm_vm_free().

Fixes: 83f6e109f562 ("KVM: selftests: Cache binary stats metadata for duration of test")
Link: https://lore.kernel.org/r/20250111005049.1247555-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>

+2 -2
+2 -2
tools/testing/selftests/kvm/lib/kvm_util.c
··· 749 749 return; 750 750 751 751 /* Free cached stats metadata and close FD */ 752 - if (vmp->stats_fd) { 752 + if (vmp->stats_desc) { 753 753 free(vmp->stats_desc); 754 754 close(vmp->stats_fd); 755 755 } ··· 2218 2218 size_t size_desc; 2219 2219 int i; 2220 2220 2221 - if (!vm->stats_fd) { 2221 + if (!vm->stats_desc) { 2222 2222 vm->stats_fd = vm_get_stats_fd(vm); 2223 2223 read_stats_header(vm->stats_fd, &vm->stats_header); 2224 2224 vm->stats_desc = read_stats_descriptors(vm->stats_fd,