[gmem,FIXUP] selftests/kvm: guestmem: check fstat results on guestmem fd

Message ID 20231012140715.2445237-1-pbonzini@redhat.com
State New
Headers
Series [gmem,FIXUP] selftests/kvm: guestmem: check fstat results on guestmem fd |

Commit Message

Paolo Bonzini Oct. 12, 2023, 2:07 p.m. UTC
  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@redhat.com>
---
 .../testing/selftests/kvm/guest_memfd_test.c  | 26 +++++++++++++++++++
 1 file changed, 26 insertions(+)
  

Comments

Sean Christopherson Oct. 18, 2023, 2:36 p.m. UTC | #1
On Thu, 12 Oct 2023 10:07:15 -0400, Paolo Bonzini wrote:
> 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().
> 
> 
> [...]

Applied to kvm-x86 guest_memfd, thanks!

[1/1] selftests/kvm: guestmem: check fstat results on guestmem fd
      https://github.com/kvm-x86/linux/commit/911b515af3ec

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

Patch

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);