linux/arch/x86/kvm
Sean Christopherson 0cb2af2ea6 KVM: x86: Fix shadow paging use-after-free due to unexpected GFN
The shadow MMU computes GFNs for direct shadow pages using sp->gfn plus
the SPTE index. This assumption breaks for shadow paging if the guest
page tables are modified between VM entries (similar to commit
aad885e774, "KVM: x86/mmu: Drop/zap existing present SPTE even
when creating an MMIO SPTE", 2026-03-27).  The flow is as follows:

- a PDE is installed for a 2MB mapping, and a page in that area is
  accessed.  KVM creates a kvm_mmu_page consisting of 512 4KB pages;
  the kvm_mmu_page is marked by FNAME(fetch) as direct-mapped because
  the guest's mapping is a huge page (and thus contiguous).

- the PDE mapping is changed from outside the guest.

- the guest accesses another page in the same 2MB area.  KVM installs
  a new leaf SPTE and rmap entry; the SPTE uses the "correct" GFN
  (i.e. based on the new mapping, as changed in the previous step) but
  that GFN is outside of the [sp->gfn, sp->gfn + 511] range; therefore
  the rmap entry cannot be found and removed when the kvm_mmu_page
  is zapped.

- the memslot that covers the first 2MB mapping is deleted, and the
  kvm_mmu_page for the now-invalid GPA is zapped.  However, rmap_remove()
  only looks at the [sp->gfn, sp->gfn + 511] range established in step 1,
  and fails to find the rmap entry that was recorded by step 3.

- any operation that causes an rmap walk for the same page accessed
  by step 3 then walks a stale rmap and dereferences a freed kvm_mmu_page.
  This includes dirty logging or MMU notifier invalidations (e.g., from
  MADV_DONTNEED).

The underlying issue is that KVM's walking of shadow PTEs assumes that
if a SPTE is present when KVM wants to install a non-leaf SPTE, then the
existing kvm_mmu_page must be for the correct gfn.  Because the only way
for the gfn to be wrong is if KVM messed up and failed to zap a SPTE...
which shouldn't happen, but *actually* only happens in response to a
guest write.

That bug dates back literally forever, as even the first version of KVM
assumes that the GFN matches and walks into the "wrong" shadow page.
However, that was only an imprecision until 2032a93d66 ("KVM: MMU:
Don't allocate gfns page for direct mmu pages") came along.

Fix it by checking for a target gfn mismatch and zapping the existing
SPTE.  That way the old SP and rmap entries are gone, KVM installs
the rmap in the right location, and everyone is happy.

Fixes: 2032a93d66 ("KVM: MMU: Don't allocate gfns page for direct mmu pages")
Fixes: 6aa8b732ca ("kvm: userspace interface")
Reported-by: Alexander Bulekov <bkov@amazon.com>
Reported-by: Fred Griffoul <fgriffo@amazon.co.uk>
Cc: stable@vger.kernel.org
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20260503201029.106481-1-pbonzini@redhat.com/
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
2026-05-03 22:32:53 +02:00
..
mmu KVM: x86: Fix shadow paging use-after-free due to unexpected GFN 2026-05-03 22:32:53 +02:00
svm KVM: x86: use inlines instead of macros for is_sev_*guest 2026-04-13 19:00:47 +02:00
vmx KVM: x86: Fix misleading variable names and add more comments for PIR=>IRR flow 2026-05-03 22:32:41 +02:00
.gitignore
cpuid.c KVM x86 misc changes for 7.1 2026-04-13 11:51:34 +02:00
cpuid.h KVM: x86: Harden against unexpected adjustments to kvm_cpu_caps 2026-01-30 13:28:29 -08:00
debugfs.c
emulate.c KVM nested SVM changes for 7.1 (with one common x86 fix) 2026-04-13 13:01:50 +02:00
fpu.h KVM: x86: Add AVX support to the emulator's register fetch and writeback 2025-11-19 14:30:54 -08:00
hyperv.c KVM: x86: check for nEPT/nNPT in slow flush hypercalls 2026-05-03 22:17:30 +02:00
hyperv.h KVM: nSVM: Raise #UD if unhandled VMMCALL isn't intercepted by L1 2026-03-04 16:08:55 -08:00
i8254.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
i8254.h KVM: x86: Move IRQ mask notifier infrastructure to I/O APIC emulation 2025-06-20 13:52:52 -07:00
i8259.c treewide: Replace kmalloc with kmalloc_obj for non-scalar types 2026-02-21 01:02:28 -08:00
ioapic.c KVM: x86: Fix SRCU list traversal in kvm_fire_mask_notifiers() 2026-03-11 18:41:11 +01:00
ioapic.h KVM: x86: Bury ioapic.h definitions behind CONFIG_KVM_IOAPIC 2026-01-12 09:31:41 -08:00
irq.c Merge tag 'kvm-x86-apic-6.20' of https://github.com/kvm-x86/linux into HEAD 2026-02-11 12:45:32 -05:00
irq.h KVM: x86: Move kvm_irq_delivery_to_apic() from irq.c to lapic.c 2025-09-10 12:05:09 -07:00
Kconfig KVM: remove CONFIG_KVM_GENERIC_MMU_NOTIFIER 2026-02-28 15:31:35 +01:00
kvm_cache_regs.h KVM: VMX: Make CR4.CET a guest owned bit 2025-09-23 10:03:09 -07:00
kvm_emulate.h KVM: SVM: Properly check RAX in the emulator for SVM instructions 2026-04-03 16:08:00 -07:00
kvm_onhyperv.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
kvm_onhyperv.h
kvm-asm-offsets.c
lapic.c KVM: x86: Fix misleading variable names and add more comments for PIR=>IRR flow 2026-05-03 22:32:41 +02:00
lapic.h Merge tag 'kvm-x86-apic-6.20' of https://github.com/kvm-x86/linux into HEAD 2026-02-11 12:45:32 -05:00
Makefile KVM: x86: Enforce use of EXPORT_SYMBOL_FOR_KVM_INTERNAL 2026-01-12 09:31:20 -08:00
mmu.h KVM TDX changes for 6.19: 2025-11-26 09:36:37 +01:00
mtrr.c KVM: x86: drop x86.h include from cpuid.h 2024-11-01 09:22:23 -07:00
pmu.c KVM: x86/pmu: annotate struct kvm_x86_pmu_event_filter with __counted_by() 2026-03-02 14:31:28 -08:00
pmu.h KVM: x86/pmu: Disallow emulation in the fastpath if mediated PMCs are active 2026-01-08 11:52:12 -08:00
reverse_cpuid.h KVM: x86: Advertise AVX10_VNNI_INT CPUID to userspace 2026-01-23 10:00:02 -08:00
smm.c KVM: x86: Export KVM-internal symbols for sub-modules only 2025-09-30 13:40:02 -04:00
smm.h KVM: x86: Save and reload SSP to/from SMRAM 2025-09-23 09:11:22 -07:00
trace.h KVM: SVM: Treat exit_code as an unsigned 64-bit value through all of KVM 2026-01-13 17:37:03 -08:00
tss.h
x86.c KVM SVM changes for 7.1 2026-04-13 19:00:43 +02:00
x86.h KVM nested SVM changes for 7.1 (with one common x86 fix) 2026-04-13 13:01:50 +02:00
xen.c Convert 'alloc_obj' family to use the new default GFP_KERNEL argument 2026-02-21 17:09:51 -08:00
xen.h KVM: x86: Update Xen TSC leaves during CPUID emulation 2025-02-25 07:09:55 -08:00