public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
Cc: pbonzini@redhat.com, tglx@linutronix.de, mingo@redhat.com,
	bp@alien8.de,  dave.hansen@linux.intel.com, x86@kernel.org,
	kvm@vger.kernel.org
Subject: Re: [PATCH] KVM: x86: Fix potential NULL dereference in amd_pmu_refresh()
Date: Tue, 25 Nov 2025 07:31:23 -0800	[thread overview]
Message-ID: <aSXLyxvBvXvEhRpm@google.com> (raw)
In-Reply-To: <20251125152013.433803-1-chelsyratnawat2001@gmail.com>

On Tue, Nov 25, 2025, Chelsy Ratnawat wrote:
> kvm_find_cpuid_entry_index() can return NULL if the guest CPUID
> entry is missing, but amd_pmu_refresh() was dereferencing the pointer
> without checking. This could cause a kernel crash.
> 
> Add a NULL check and fallback to AMD64_NUM_COUNTERS_CORE if the
> entry is missing.
> 
> Signed-off-by: Chelsy Ratnawat <chelsyratnawat2001@gmail.com>
> ---
>  arch/x86/kvm/svm/pmu.c | 10 ++++++++--
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
> index bc062285fbf5..aa8313fa98c9 100644
> --- a/arch/x86/kvm/svm/pmu.c
> +++ b/arch/x86/kvm/svm/pmu.c
> @@ -178,6 +178,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
>  {
>  	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
>  	union cpuid_0x80000022_ebx ebx;
> +	struct kvm_cpuid_entry2 *entry;
>  
>  	pmu->version = 1;
>  	if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFMON_V2)) {
> @@ -188,8 +189,13 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
>  		 */
>  		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
>  			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
> -		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;

Heh, me thinks you didn't read the comment above the BUILD_BUG_ON():

		/*
		 * Note, PERFMON_V2 is also in 0x80000022.0x0, i.e. the guest
		 * CPUID entry is guaranteed to be non-NULL.
		 */
		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);

Yes, it's weird and confusing to subtly rely on entry 0x80000022 being non-NULL,
but doing so means KVM doesn't have to provide arbitrary fallback logic for a
scenario that can't happen.

      reply	other threads:[~2025-11-25 15:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-25 15:20 [PATCH] KVM: x86: Fix potential NULL dereference in amd_pmu_refresh() Chelsy Ratnawat
2025-11-25 15:31 ` Sean Christopherson [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=aSXLyxvBvXvEhRpm@google.com \
    --to=seanjc@google.com \
    --cc=bp@alien8.de \
    --cc=chelsyratnawat2001@gmail.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@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