Skip to content

Commit af62fe2

Browse files
committed
KVM: Harden and prepare for modifying existing guest_memfd memslots
Unbind guest_memfd memslots if KVM commits a MOVE or FLAGS_ONLY memslot change to harden against use-after-free, and to prepare for eventually supporting dirty logging on guest_memfd memslots, at which point FLAGS_ONLY changes will be expected/supported. Add two separate WARNs, once to yell if a guest_memfd memslot is moved (which KVM is never expected to allow/support), and again if the unbind() is triggered, to help detect uAPI goofs prior to deliberately allowing FLAGS_ONLY changes. Link: https://patch.msgid.link/20251202020334.1171351-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9935df5 commit af62fe2

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

virt/kvm/kvm_main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,6 +1748,12 @@ static void kvm_commit_memory_region(struct kvm *kvm,
17481748
kvm_free_memslot(kvm, old);
17491749
break;
17501750
case KVM_MR_MOVE:
1751+
/*
1752+
* Moving a guest_memfd memslot isn't supported, and will never
1753+
* be supported.
1754+
*/
1755+
WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD);
1756+
fallthrough;
17511757
case KVM_MR_FLAGS_ONLY:
17521758
/*
17531759
* Free the dirty bitmap as needed; the below check encompasses
@@ -1756,6 +1762,15 @@ static void kvm_commit_memory_region(struct kvm *kvm,
17561762
if (old->dirty_bitmap && !new->dirty_bitmap)
17571763
kvm_destroy_dirty_bitmap(old);
17581764

1765+
/*
1766+
* Unbind the guest_memfd instance as needed; the @new slot has
1767+
* already created its own binding. TODO: Drop the WARN when
1768+
* dirty logging guest_memfd memslots is supported. Until then,
1769+
* flags-only changes on guest_memfd slots should be impossible.
1770+
*/
1771+
if (WARN_ON_ONCE(old->flags & KVM_MEM_GUEST_MEMFD))
1772+
kvm_gmem_unbind(old);
1773+
17591774
/*
17601775
* The final quirk. Free the detached, old slot, but only its
17611776
* memory, not any metadata. Metadata, including arch specific

0 commit comments

Comments
 (0)