History

KVM: arm64: Reject guest_memfd memslots when the VM has MTE

The user cannot use MTE on VMAs created by mapping a guest_memfd file, as arch_calc_vm_flag_bits() does not set VM_MTE_ALLOWED. When creating a guest_memfd backed memslot, kvm_arch_prepare_memory_region() rejects the memslot if MTE is enabled for the VM and if guest_memfd has been mapped in a VMA that intersects the memslot. However, the documentation for KVM_SET_USER_MEMORY_REGION2 explicitly states that the only condition for userspace_addr is for it to be a legal userspace address, but the mapping is not required to be valid nor populated at memslot creation. If userspace sets userspace_addr to an address that hasn't been mapped, or if userspace_addr belongs to a VMA that isn't backed by the guest_memfd file, or if the VMA doesn't intersect the memslot, memslot creation is successful and KVM ends up with a VM with MTE and guest_memfd-backed memslots. The same happens if the order is reversed: when userspace enables MTE, KVM does not check if memslots backed by guest_memfd are already present. Fix both issues by rejecting guest_memfd-backed memslots when MTE is enabled, and by rejecting MTE when guest_memfd-backed memslots are already present.

Alexandru Elisei committed 3 months ago

Commit 679d7201 parent e7821043 files · +29 −6Browse files

Unified · Split

3 files
  1. Documentation/virt/kvm/api.rst +6 −0
  2. arch/arm64/kvm/arm.c +19 −6
  3. arch/arm64/kvm/mmu.c +4 −0
Documentation/virt/kvm/api.rst+6−0
@@ -8414,6 +8414,12 @@ When this capability is enabled all memory in memslots must be mapped as · 8413 unmodified lines
84148414 attempts to create a memslot with an invalid mmap will result in an
84158415 -EINVAL return.
84168416
8417+``guest_memfd``, even though it is an anonymous file, is not supported with MTE.
8418+Attempting to create a memslot backed by ``guest_memfd`` when the MTE capability
8419+is enabled, or attempting to enable the MTE capability after
8420+``guest_memfd``-backed memslots have been created, will result in an -EINVAL
8421+return.
8422+
84178423 When enabled the VMM may make use of the ``KVM_ARM_MTE_COPY_TAGS`` ioctl to
84188424 perform a bulk copy of tags to/from the guest.
84198425
arch/arm64/kvm/arm.c+19−6
@@ -149,14 +149,27 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm, · 148 unmodified lines
149149 set_bit(KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER,
150150 &kvm->arch.flags);
151151 break;
152− case KVM_CAP_ARM_MTE:
153− mutex_lock(&kvm->lock);
154− if (system_supports_mte() && !kvm->created_vcpus) {
155− r = 0;
156− set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
152+ case KVM_CAP_ARM_MTE: {
153+ struct kvm_memory_slot *memslot;
154+ int bkt;
155+
156+ guard(mutex)(&kvm->lock);
157+ if (!system_supports_mte() || kvm->created_vcpus)
158+ break;
159+
160+ r = 0;
161+ guard(mutex)(&kvm->slots_lock);
162+ kvm_for_each_memslot(memslot, bkt, kvm_memslots(kvm)) {
163+ if (kvm_slot_has_gmem(memslot)) {
164+ r = -EINVAL;
165+ break;
166+ }
157167 }
158− mutex_unlock(&kvm->lock);
168+ if (r == 0)
169+ set_bit(KVM_ARCH_FLAG_MTE_ENABLED, &kvm->arch.flags);
159170 break;
171+
172+ }
160173 case KVM_CAP_ARM_SYSTEM_SUSPEND:
161174 r = 0;
162175 set_bit(KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED, &kvm->arch.flags);
arch/arm64/kvm/mmu.c+4−0
@@ -2652,6 +2652,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, · 2651 unmodified lines
26522652 if (kvm_slot_has_gmem(new) && !kvm_memslot_is_gmem_only(new))
26532653 return -EINVAL;
26542654
2655+ /* guest_memfd is incompatible with MTE. */
2656+ if (kvm_slot_has_gmem(new) && kvm_has_mte(kvm))
2657+ return -EINVAL;
2658+
26552659 hva = new->userspace_addr;
26562660 reg_end = hva + (new->npages << PAGE_SHIFT);
26572661

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t