Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Yosry Ahmed <yosry@kernel.org>
To: Sean Christopherson <seanjc@google.com>
Cc: Paolo Bonzini <pbonzini@redhat.com>,
	Jim Mattson <jmattson@google.com>,
	kvm@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org, Yosry Ahmed <yosry@kernel.org>
Subject: [PATCH v2 1/4] KVM: x86: Acquire SRCU read lock in KVM_SET_SREGS[2]
Date: Wed,  2 Dec 2026 05:27:51 +0000	[thread overview]
Message-ID: <20261202052754.3170526-2-yosry@kernel.org> (raw)
In-Reply-To: <20261202052754.3170526-1-yosry@kernel.org>

From: Sean Christopherson <seanjc@google.com>

Acquire kvm->srcu across __set_sregs_common(), as it could end up
accessing SRCU-protected kvm->arch.pmu_event_filter on AMD through
svm_set_efer() -> svm_pmu_handle_nested_transition() ->
kvm_pmu_handle_event() -> pmc_is_event_allowed().

This triggers the following splat (with CONFIG_PROVE_RCU), and risks a
UAF against a concurrent KVM_SET_PMU_EVENT_FILTER:

  WARNING: suspicious RCU usage
  arch/x86/kvm/pmu.c:539 suspicious rcu_dereference_check() usage!
  ...
  pmc_is_event_allowed+0x27c/0x2b0 [kvm]
  kvm_pmu_handle_event+0x365/0x880 [kvm]
  svm_set_efer+0x16b/0x1a0 [kvm_amd]
  __set_sregs_common+0x2fd/0x6a0 [kvm]
  __set_sregs+0x37/0xd0 [kvm]
  kvm_arch_vcpu_ioctl_set_sregs+0x3e/0x50 [kvm]
  kvm_vcpu_ioctl+0x53a/0x6b0 [kvm]

As a nice side effect, this also simplifies the existing load_pdptrs()
SRCU critical section by using guard(srcu).

Fixes: a02a25a65246 ("KVM: x86/pmu: Reprogram Host/Guest-Only counters on nested transitions")
Cc: stable@vger.kernel.org
Not-yet-signed-off-by: Sean Christopherson <seanjc@google.com>
[yosry: test and write a changelog]

Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/regs.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/regs.c b/arch/x86/kvm/regs.c
index b1527d5bcccc5..82df3901bcfe4 100644
--- a/arch/x86/kvm/regs.c
+++ b/arch/x86/kvm/regs.c
@@ -571,7 +571,6 @@ static bool kvm_is_valid_sregs(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs)
 static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
 			      int *mmu_reset_needed, bool update_pdptrs)
 {
-	int idx;
 	struct desc_ptr dt;
 
 	if (!kvm_is_valid_sregs(vcpu, sregs))
@@ -583,6 +582,8 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
 	if (vcpu->arch.guest_state_protected)
 		return 0;
 
+	guard(srcu)(&vcpu->kvm->srcu);
+
 	dt.size = sregs->idt.limit;
 	dt.address = sregs->idt.base;
 	kvm_x86_call(set_idt)(vcpu, &dt);
@@ -605,13 +606,9 @@ static int __set_sregs_common(struct kvm_vcpu *vcpu, struct kvm_sregs *sregs,
 	*mmu_reset_needed |= kvm_read_cr4(vcpu) != sregs->cr4;
 	kvm_x86_call(set_cr4)(vcpu, sregs->cr4);
 
-	if (update_pdptrs) {
-		idx = srcu_read_lock(&vcpu->kvm->srcu);
-		if (is_pae_paging(vcpu)) {
-			load_pdptrs(vcpu, kvm_read_cr3(vcpu));
-			*mmu_reset_needed = 1;
-		}
-		srcu_read_unlock(&vcpu->kvm->srcu, idx);
+	if (update_pdptrs && is_pae_paging(vcpu)) {
+		load_pdptrs(vcpu, kvm_read_cr3(vcpu));
+		*mmu_reset_needed = 1;
 	}
 
 	kvm_set_segment(vcpu, &sregs->cs, VCPU_SREG_CS);
-- 
2.56.1.911.g883d8bcb20-goog


           reply	other threads:[~2026-12-02  5:28 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <20261202052754.3170526-1-yosry@kernel.org>]

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=20261202052754.3170526-2-yosry@kernel.org \
    --to=yosry@kernel.org \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pbonzini@redhat.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