From: Sean Christopherson Check that a valid fd provided to mmap() must be accompanied by MAP_SHARED. With an invalid fd (usually used for anonymous mappings), there are no constraints on mmap() flags. Add this check to make sure that when a guest_memfd is used as region->fd, the flag provided to mmap() will include MAP_SHARED. Signed-off-by: Sean Christopherson [Rephrase assertion message.] Signed-off-by: Ackerley Tng --- tools/testing/selftests/kvm/lib/kvm_util.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index cb73566fdf153..8603bd5c705ed 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1057,6 +1057,9 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backing_src_type src_type, region->fd = kvm_memfd_alloc(region->mmap_size, src_type == VM_MEM_SRC_SHARED_HUGETLB); + TEST_ASSERT(region->fd == -1 || backing_src_is_shared(src_type), + "A valid fd provided to mmap() must be accompanied by MAP_SHARED."); + mmap_offset = flags & KVM_MEM_GUEST_MEMFD ? gmem_offset : 0; region->mmap_start = __kvm_mmap(region->mmap_size, PROT_READ | PROT_WRITE, vm_mem_backing_src_alias(src_type)->flag, -- 2.51.0.858.gf9c4a03a3a-goog