[PATCH gmem FIXUP] selftests/kvm: guestmem: check fstat results on guestmem fd
From: Paolo Bonzini
Date: Thu Oct 12 2023 - 10:08:18 EST
Do a basic sanity cheeck for the st_size and st_ino fields of
a guestmem file descriptor. The test would fail for example
if guestmem.c used the innocuous-sounding function
anon_inode_getfile() instead of the correct one,
anon_inode_getfile_secure().
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
.../testing/selftests/kvm/guest_memfd_test.c | 26 +++++++++++++++++++
1 file changed, 26 insertions(+)
diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c
index 75073645aaa1..9805b0f8f26a 100644
--- a/tools/testing/selftests/kvm/guest_memfd_test.c
+++ b/tools/testing/selftests/kvm/guest_memfd_test.c
@@ -91,6 +91,31 @@ static void test_fallocate(int fd, size_t page_size, size_t total_size)
TEST_ASSERT(!ret, "fallocate to restore punched hole should succeed");
}
+static void test_create_guest_memfd_multiple(struct kvm_vm *vm)
+{
+ int fd1, fd2, ret;
+ struct stat st1, st2;
+
+ fd1 = __vm_create_guest_memfd(vm, 4096, 0);
+ TEST_ASSERT(fd1 != -1, "memfd creation should succeed");
+
+ ret = fstat(fd1, &st1);
+ TEST_ASSERT(ret != -1, "memfd fstat should succeed");
+ TEST_ASSERT(st1.st_size == 4096, "memfd st_size should match requested size");
+
+ fd2 = __vm_create_guest_memfd(vm, 8192, 0);
+ TEST_ASSERT(fd2 != -1, "memfd creation should succeed");
+
+ ret = fstat(fd2, &st2);
+ TEST_ASSERT(ret != -1, "memfd fstat should succeed");
+ TEST_ASSERT(st2.st_size == 8192, "second memfd st_size should match requested size");
+
+ ret = fstat(fd1, &st1);
+ TEST_ASSERT(ret != -1, "memfd fstat should succeed");
+ TEST_ASSERT(st1.st_size == 4096, "first memfd st_size should still match requested size");
+ TEST_ASSERT(st1.st_ino != st2.st_ino, "different memfd should have different inode numbers");
+}
+
static void test_create_guest_memfd_invalid(struct kvm_vm *vm)
{
uint64_t valid_flags = 0;
@@ -153,6 +178,7 @@ int main(int argc, char *argv[])
vm = vm_create_barebones();
test_create_guest_memfd_invalid(vm);
+ test_create_guest_memfd_multiple(vm);
fd = vm_create_guest_memfd(vm, total_size, 0);
--
2.39.1