From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796407302; b=Zg0OzAruM0brX6O3k+xNMle9VoLSM2eHdcj9RhxsYffj4V8OeervBp5vfi8oQ3q9laky +b890DV6fogapxe5FQ912Lxcqiw1FZaItJx9aCA1YAg0ru2kXkvkDXFrHwMDmFCH77Dom J/F+KAtM2yXJtkyv4jRtOzXJJYtChE9ZOU5CxqNehzLcgLyDscWHJLUnU21ve9+sd2KbD OHzbOiMwRC6QsAVct8bKXl8wR3+8UGmuRoC512oLJCCY4LsxHsmF+btnUNXF5irBF7HJ6 HIPUl+KVhMn0PBSclQO1LsGIU0xfCFbde1SDBaGyZy3cT26RWpDji55LX05SvbJ3nPQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796407302; h=DMARC-Filter:DKIM-Signature:Received:X-MC-Unique: X-Mimecast-MFC-AGG-ID:Received:Received:From:To:Cc:Subject:Date: Message-ID:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding:X-Scanned-By; bh=utQ34zibKWriAlievXGScCc4j0/sMPNOi+VdbdVq/Gk=; b=FRBQOQ+9gQMxwYPwbb5gH8rk4xqM/MZ7zHniFhvD41K6wKCln4vKkXaIGU3ka9y0EkI6 LwfuCSHUG1nhab/8QO35yaxFaBr8g71nIFlX6w+VfsworUEJE9mSXaVEdyYeX2oKTuGUl 5mdES8Ia937bCpnzS/rR1fMa+Xy+BWGKoeMiAEH60kP5250CfxYTaX+vvT4z8fzCJd1Nk XDN4UPfV7DmJ9+BD4/lNQb9MvIKzPte529PV0Enn0/7Jmeb3Zm+YI50LcrIems3EZMWIb 5DApCGdM6upGIRv/7/hPZ+4m8gW8m0q/Bqd36VwXLbaTZytFRxNTwBHLmhPr0rHbSFA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=W44XdCJf; arc=none smtp.remote-ip=170.10.129.124 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 831E9606A32D for ; Fri, 4 Dec 2026 18:01:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 831E9606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=W44XdCJf DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1796407301; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=utQ34zibKWriAlievXGScCc4j0/sMPNOi+VdbdVq/Gk=; b=W44XdCJfFtrcpG18uWLET7DgfvjuUOymlrjF1ztzWe2ZG2zASkS/uFUSiDYye31IEGw0X5 wByfEnfKCkGegQ2+YmhALl3k3UHaqZaXJyeXjLA7TrHQdd0BgJth9vTSv2EFQ5tyPRkfhq jO7P6ZlxF7SdRyyQ0vy2XgbP2HLTOr0= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS with 8BITMIME (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-75-Nk_7D8aTPmWy7kAVDoiZSw-1; Fri, 4 Dec 2026 18:01:38 +0000 X-MC-Unique: Nk_7D8aTPmWy7kAVDoiZSw-1 X-Mimecast-MFC-AGG-ID: Nk_7D8aTPmWy7kAVDoiZSw_1796407297 Received: from mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.12]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2884E18B6277; Fri, 4 Dec 2026 18:01:37 +0000 (UTC) Received: from pbonzini-thinkpadx1carbon6th.rmtit.csb (headnet04.pony-001.prod.iad2.dc.redhat.com [10.2.32.116]) by mx-prod-int-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 11EE91955E80; Fri, 4 Dec 2026 18:01:34 +0000 (UTC) From: Paolo Bonzini To: linux-kernel@vger.kernel.org, kvm@vger.kernel.org Cc: Sean Christopherson , stable@vger.kernel.org, Stefan Teodorescu , Sagi Shahar 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 Message-ID: <20261204180059.2165490-18-pbonzini@redhat.com> In-Reply-To: <20261204180059.2165490-1-pbonzini@redhat.com> References: <20261204180059.2165490-1-pbonzini@redhat.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.12 From: Sean Christopherson [ 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: ? __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 Cc: Sagi Shahar Signed-off-by: Sean Christopherson Link: https://patch.msgid.link/20261119173451.67434-1-seanjc@google.com Signed-off-by: Paolo Bonzini Signed-off-by: Paolo Bonzini --- 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