From: Paolo Bonzini <pbonzini@redhat.com>
To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org
Cc: Sean Christopherson <seanjc@google.com>,
stable@vger.kernel.org, Stefan Teodorescu <fane@google.com>,
Sagi Shahar <sagis@google.com>
Subject: [PATCH 6.18.y 17/17] KVM: VMX: Ensure pCPU is valid before sending IPI to deliver posted IRQ
Date: Fri, 4 Dec 2026 19:00:59 +0100 [thread overview]
Message-ID: <20261204180059.2165490-18-pbonzini@redhat.com> (raw)
In-Reply-To: <20261204180059.2165490-1-pbonzini@redhat.com>
From: Sean Christopherson <seanjc@google.com>
[ 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@vger.kernel.org
Reported-by: Stefan Teodorescu <fane@google.com>
Cc: Sagi Shahar <sagis@google.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
Link: https://patch.msgid.link/20261119173451.67434-1-seanjc@google.com
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
---
arch/x86/kvm/vmx/common.h | 5 +++--
arch/x86/kvm/vmx/tdx.c | 4 ++--
arch/x86/kvm/x86.c | 2 +-
3 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/arch/x86/kvm/vmx/common.h b/arch/x86/kvm/vmx/common.h
index 80c78b35d168..b1bedb2c1327 100644
--- a/arch/x86/kvm/vmx/common.h
+++ b/arch/x86/kvm/vmx/common.h
@@ -139,9 +139,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);
+ if (cpu >= 0 && vcpu != kvm_get_running_vcpu())
+ __apic_send_IPI_mask(get_cpu_mask(cpu), pi_vec);
return;
}
#endif
diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index 0a0fd87fd53e..e5182b25a856 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -278,7 +278,7 @@ static inline void tdx_disassociate_vp(struct kvm_vcpu *vcpu)
*/
smp_wmb();
- vcpu->cpu = -1;
+ WRITE_ONCE(vcpu->cpu, -1);
}
static void tdx_no_vcpus_enter_start(struct kvm *kvm)
@@ -398,7 +398,7 @@ static void tdx_flush_vp_on_cpu(struct kvm_vcpu *vcpu)
struct tdx_flush_vp_arg arg = {
.vcpu = vcpu,
};
- int cpu = vcpu->cpu;
+ int cpu = READ_ONCE(vcpu->cpu);
if (unlikely(cpu == -1))
return;
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 3ca396316d3c..0e4f636acb2f 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -4910,7 +4910,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
prev parent reply other threads:[~2026-12-04 18:01 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-04 18:00 [PATCH 6.18.y 00/17] Fixes for various (mostly x86) KVM vulnerabilities Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 01/17] KVM: x86: Grab lapic_timer in a local variable to cleanup periodic code Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 02/17] KVM: x86/mmu: Flush TLBs if child SPTE is zapped when splitting a huge SPTE Paolo Bonzini
2026-12-05 12:48 ` Sasha Levin
2026-12-04 18:00 ` [PATCH 6.18.y 03/17] KVM: x86/mmu: Propagate the return from kvm_zap_all_rmap_sptes() out of __rmap_add() Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 04/17] KVM: x86: Disable hypervisor (VMX Preemption) timer if TSC deadline wraps Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 05/17] KVM: x86: Reset local APIC timer metadata on RESET, INIT, and KVM_SET_LAPIC Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 06/17] LoongArch: KVM: Flush TLBs *before* dropping mmu_lock Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 07/17] KVM: x86/mmu: Flush all TLB entries, not a precise range, when yielding Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 08/17] KVM: x86/mmu: Flush TLBs in kvm_unmap_gfn_range() if the unmap may block/yield Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 09/17] KVM: x86/xen: Pin kvm->mm prior to borrowing mm when setting event channel Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 10/17] KVM: Fold kvm_flush_shadow_all() into kvm_mmu_notifier_release() Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 11/17] KVM: WARN if there are in-flight memslot updates when a VM's process exits Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 12/17] KVM: Set mn_active_invalidate_count when the VM's mm is released Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 13/17] KVM: Invalidate all GFN=>PFN caches when VM and/or its process is dying Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 14/17] x86/fpu: Bail early from guest XFD update if XFD isn't supported Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 15/17] x86/fpu: Clear XSTATE_BV[i] in guest XSAVE state if guest FPU isn't in-use Paolo Bonzini
2026-12-04 18:00 ` [PATCH 6.18.y 16/17] x86/fpu: Ensure guest FPU is resident in hardware when updating actual XFD MSR Paolo Bonzini
2026-12-04 18:00 ` Paolo Bonzini [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261204180059.2165490-18-pbonzini@redhat.com \
--to=pbonzini@redhat.com \
--cc=fane@google.com \
--cc=kvm@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=sagis@google.com \
--cc=seanjc@google.com \
--cc=stable@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®