[PATCH 6.1.y 24/24] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ
From: Paolo Bonzini
Date: Fri Dec 04 2026 - 13:14:17 EST
From: Sean Christopherson <seanjc@xxxxxxxxxx>
[ Upstream commit 55abfb5917d39e9511ca89d37f86cdc3a1d1797b ]
Verify the target pCPU is valid before sending an IPI to trigger CPU=>CPU
posted IRQ processing, as TDX invalidates vcpu->cpu when disassociating a
pCPU from a vCPU. Ensure the relevant reads and writes are performed once,
to prevent vcpu->cpu from being re-read after it's been validated.
If a TDX vCPU's task is migrated immediately after a VM-Exit, and the CPU
sending the IPI is sufficiently delayed, KVM will attempt to send an IPI to
pCPU '-1', generate an out-of-bounds read, and ultimately crash.
BUG: unable to handle page fault for address: ffffffff88021be8
#PF: supervisor read access in kernel mode
#PF: error_code(0x0000) - not-present page
PGD 10a0815067 P4D 10a0815067 PUD 0
Oops: 0000 [#1] SMP NOPTI
CPU: 0 PID: 173535 Comm: repro Tainted: G O 6.6.103-smp-1301.106.448.13 #1
Hardware name: Google Izumi/Izumi, BIOS 0.20260728.0-16 07/28/2026
RIP: 0010:memcpy+0xc/0x20
Call Trace:
<TASK>
? __x2apic_send_IPI_mask+0x71/0x160
tdx_deliver_interrupt+0x75/0xe0 [kvm_intel]
__apic_accept_irq+0xdf/0x240 [kvm]
kvm_irq_delivery_to_apic+0x1df/0x2e0 [kvm]
kvm_apic_send_ipi+0xa3/0x100 [kvm]
kvm_x2apic_icr_write+0x47/0xc0 [kvm]
__kvm_set_msr+0xa0/0x1c0 [kvm]
kvm_set_msr_ignored_check+0x18/0xf0 [kvm]
kvm_emulate_wrmsr+0x74/0x180 [kvm]
tdx_handle_exit+0x238/0x13a0 [kvm_intel]
Note, using the "mask" variant to send the IPI is stupid and inefficient,
and actively contributed to making the bug worse. Because TDX requires
x2APIC, if KVM had used the more obvious send_IPI(), then attempting to
send an IPI to '-1' would have morphed into a broadcast the IRQ to all CPUs
instead of an out-of-bounds read. That wart will be fixed shortly.
Note #2, the flaw has existed since commit 705699a13994 ("KVM: nVMX: Enable
nested posted interrupt processing"), but was unreachable until TDX came
along and invalidated the CPU of a live vCPU.
Note #3, __kvm_vcpu_kick() and kvm_make_vcpu_request() already guard
against an invalid pCPU.
Note #4, tdx_vcpu_free() checks vcpu->cpu without using READ_ONCE(), but
that's a-ok since the vCPU is unreachable, i.e. vcpu->cpu can't change.
Note #5, invalidating vcpu->cpu probably means TDX is triggering a VM-wide
blast of IPIs to do KVM_REQ_GLOBAL_CLOCK_UPDATE => KVM_REQ_CLOCK_UPDATE,
because kvm_arch_vcpu_load() thinks the vCPU has never been loaded on any
CPU. That too will be fixed in the near future.
Note #6, related to note #5, invoking kvm_x86_call(vcpu_load)(vcpu, cpu) in
the middle of kvm_arch_vcpu_load() changes vcpu->cpu to -1 for TDX vCPUs,
i.e. fixing that would fix #5 as well (although it's simpler to just make
the request during vCPU creation).
Note #7, the only reason reading vcpu->cpu multiple times when loading a
vCPU isn't completely broken is because loading a vCPU is mutually
exclusive with disassociating a vCPU from a pCPU, outside of the
aforementioned self-induced pain with .vcpu_load(), thanks to commit
15945e9ec195 ("KVM: TDX: Guard VM state transitions with "all" the locks").
Fixes: d789fa6efac9 ("KVM: TDX: Handle vCPU dissociation")
Cc: stable@xxxxxxxxxxxxxxx
Reported-by: Stefan Teodorescu <fane@xxxxxxxxxx>
Cc: Sagi Shahar <sagis@xxxxxxxxxx>
Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx>
Link: https://patch.msgid.link/20261119173451.67434-1-seanjc@xxxxxxxxxx
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kvm/vmx/vmx.c | 3 ++-
arch/x86/kvm/x86.c | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 28e0198c2024..516c4e083c50 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -4139,9 +4139,10 @@ static inline void kvm_vcpu_trigger_posted_interrupt(struct kvm_vcpu *vcpu,
* not wait for any requested interrupts in PIR, and sending a
* notification event also results in a benign, spurious event.
*/
+ int cpu = READ_ONCE(vcpu->cpu);
if (vcpu != kvm_get_running_vcpu())
- apic->send_IPI_mask(get_cpu_mask(vcpu->cpu), pi_vec);
+ apic->send_IPI_mask(get_cpu_mask(cpu), pi_vec);
return;
}
#endif
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 972d0ed30d1d..b667af4fa2a7 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4783,7 +4783,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
kvm_make_request(KVM_REQ_GLOBAL_CLOCK_UPDATE, vcpu);
if (vcpu->cpu != cpu)
kvm_make_request(KVM_REQ_MIGRATE_TIMER, vcpu);
- vcpu->cpu = cpu;
+ WRITE_ONCE(vcpu->cpu, cpu);
}
kvm_make_request(KVM_REQ_STEAL_UPDATE, vcpu);
--
2.56.1