mirror of
https://github.com/torvalds/linux.git
synced 2026-07-28 01:55:51 +02:00
KVM: selftests: Add and use kvm_free_fd() to harden against fd goofs
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>
This commit is contained in:
parent
849d65e27b
commit
3e8a0b9912
|
|
@ -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 */
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user