]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: selftests: Add and use kvm_free_fd() to harden against fd goofs
authorSean Christopherson <seanjc@google.com>
Fri, 22 May 2026 17:15:35 +0000 (10:15 -0700)
committerSean Christopherson <seanjc@google.com>
Tue, 26 May 2026 19:24:13 +0000 (12:24 -0700)
Add a kvm_free_fd() macro to close and invalidate a file descriptor, and
use it through the core infrastructure to harden against goofs where a
selftest attempts to reuse a closed file descriptor.

Cc: Bibo Mao <maobibo@loongson.cn>
Cc: Fuad Tabba <tabba@google.com>
Cc: Ackerley Tng <ackerleytng@google.com>
Reviewed-by: Ackerley Tng <ackerleytng@google.com>
Link: https://patch.msgid.link/20260522171535.3525890-3-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
tools/testing/selftests/kvm/include/kvm_syscalls.h
tools/testing/selftests/kvm/lib/kvm_util.c

index 843c9904c46f64b3b059527c0e8ffcfadfb8d51d..e0ef13fcb3c7cfe435d53c540c6b3e464037b1a3 100644 (file)
@@ -79,4 +79,10 @@ __KVM_SYSCALL_DEFINE(fallocate, 4, int, fd, int, mode, loff_t, offset, loff_t, l
 __KVM_SYSCALL_DEFINE(ftruncate, 2, unsigned int, fd, off_t, length);
 __KVM_SYSCALL_DEFINE(madvise, 3, void *, addr, size_t, length, int, advice);
 
+#define kvm_free_fd(fd)                \
+do {                           \
+       kvm_close(fd);          \
+       (fd) = -1;              \
+} while (0)
+
 #endif /* SELFTEST_KVM_SYSCALLS_H */
index 464ac07e86b3f51e3141f101c32abe86f6ff871c..534bf849386c7d211229b5920e86a6ae65405fa2 100644 (file)
@@ -77,7 +77,8 @@ static ssize_t get_module_param(const char *module_name, const char *param,
        int fd, r;
 
        /* Verify KVM is loaded, to provide a more helpful SKIP message. */
-       close(open_kvm_dev_path_or_exit());
+       fd = open_kvm_dev_path_or_exit();
+       kvm_free_fd(fd);
 
        r = snprintf(path, path_size, "/sys/module/%s/parameters/%s",
                     module_name, param);
@@ -90,8 +91,7 @@ static ssize_t get_module_param(const char *module_name, const char *param,
        TEST_ASSERT(bytes_read > 0, "read(%s) returned %ld, wanted %ld bytes",
                    path, bytes_read, buffer_size);
 
-       r = close(fd);
-       TEST_ASSERT(!r, "close(%s) failed", path);
+       kvm_free_fd(fd);
        return bytes_read;
 }
 
@@ -160,7 +160,7 @@ unsigned int kvm_check_cap(long cap)
        ret = __kvm_ioctl(kvm_fd, KVM_CHECK_EXTENSION, (void *)cap);
        TEST_ASSERT(ret >= 0, KVM_IOCTL_ERROR(KVM_CHECK_EXTENSION, ret));
 
-       close(kvm_fd);
+       kvm_free_fd(kvm_fd);
 
        return (unsigned int)ret;
 }
@@ -747,8 +747,7 @@ static void kvm_stats_release(struct kvm_binary_stats *stats)
                stats->desc = NULL;
        }
 
-       kvm_close(stats->fd);
-       stats->fd = -1;
+       kvm_free_fd(stats->fd);
 }
 
 __weak void vcpu_arch_free(struct kvm_vcpu *vcpu)
@@ -777,7 +776,7 @@ static void vm_vcpu_rm(struct kvm_vm *vm, struct kvm_vcpu *vcpu)
 
        kvm_munmap(vcpu->run, vcpu_mmap_sz());
 
-       kvm_close(vcpu->fd);
+       kvm_free_fd(vcpu->fd);
        kvm_stats_release(&vcpu->stats);
 
        list_del(&vcpu->list);
@@ -793,8 +792,8 @@ void kvm_vm_release(struct kvm_vm *vmp)
        list_for_each_entry_safe(vcpu, tmp, &vmp->vcpus, list)
                vm_vcpu_rm(vmp, vcpu);
 
-       kvm_close(vmp->fd);
-       kvm_close(vmp->kvm_fd);
+       kvm_free_fd(vmp->fd);
+       kvm_free_fd(vmp->kvm_fd);
 
        /* Free cached stats metadata and close FD */
        kvm_stats_release(&vmp->stats);
@@ -815,10 +814,10 @@ static void __vm_mem_region_delete(struct kvm_vm *vm,
        if (region->fd >= 0) {
                /* There's an extra map when using shared memory. */
                kvm_munmap(region->mmap_alias, region->mmap_size);
-               close(region->fd);
+               kvm_free_fd(region->fd);
        }
        if ((int)region->region.guest_memfd >= 0)
-               close(region->region.guest_memfd);
+               kvm_free_fd(region->region.guest_memfd);
 
        free(region);
 }
@@ -1311,7 +1310,7 @@ static size_t vcpu_mmap_sz(void)
        TEST_ASSERT(ret >= 0 && ret >= sizeof(struct kvm_run),
                    KVM_IOCTL_ERROR(KVM_GET_VCPU_MMAP_SIZE, ret));
 
-       close(dev_fd);
+       kvm_free_fd(dev_fd);
 
        return ret;
 }