Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: Binbin Wu <binbin.wu@linux.intel.com>
Cc: pbonzini@redhat.com, kvm@vger.kernel.org,
	rick.p.edgecombe@intel.com,  xiaoyao.li@intel.com,
	chao.gao@intel.com, kai.huang@intel.com
Subject: Re: [RFC PATCH 01/27] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf
Date: Mon, 8 Jun 2026 07:19:16 -0700	[thread overview]
Message-ID: <aibPZMfxabh6meOk@google.com> (raw)
In-Reply-To: <4e5b6b9c-4f7d-4d62-874a-4bcb745e9a47@linux.intel.com>

On Mon, Jun 08, 2026, Binbin Wu wrote:
> 
> 
> On 4/17/2026 3:35 PM, Binbin Wu wrote:
> > Guard the use of cpuid_func_emulated() with a check that the CPUID
> > sub-leaf index is 0, as cpuid_func_emulated() unconditionally returns
> > emulated features for index 0 and does not account for indexed leaves.
> > 
> > Without the guard, when iterating over reverse_cpuid[] entries that
> > share the same CPUID function but have a non-zero index, e.g.
> > CPUID_7_1_ECX (function=7, index=1), the emulated features for index 0
> > are incorrectly OR'd into the wrong capability word.  For example,
> > RDPID (CPUID.7.0:ECX[22]) gets erroneously applied to CPUID_7_1_ECX,
> > which would allow userspace to set bit 22 of CPUID.7.1:ECX in the vCPU's
> > capabilities.
> > 
> > This is currently benign as the affected bits in the non-zero index
> > words happen to not correspond to meaningful features, but it could
> > cause problems as new features are defined in those positions.
> > 
> 
> Hi Sean/Paolo,
> 
> While the bug is currently benign, I still think it's worth fixing.
> 
> There could be two options:
> - Considering cpuid_func_emulated() probably would not be extended
>   in the future, still keep this simple implementation, but add some
>   comments to cpuid_func_emulated() to call out only subleaf 0 is emulated
>   as suggested by Xiaoyao.
>   https://lore.kernel.org/kvm/fa47d520-1de9-4ddc-94ca-6bb53ac30f10@intel.com/
> - Add the index input parameter to cpuid_func_emulated().

Definitely handle this in cpuid_func_emulated().

diff --git arch/x86/kvm/cpuid.c arch/x86/kvm/cpuid.c
index db8be9173bd0..737ca3e7bda2 100644
--- arch/x86/kvm/cpuid.c
+++ arch/x86/kvm/cpuid.c
@@ -400,7 +400,7 @@ void kvm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
                if (!entry)
                        continue;
 
-               cpuid_func_emulated(&emulated, cpuid.function, true);
+               cpuid_func_emulated(&emulated, cpuid.function, cpuid.index, true);
 
                /*
                 * A vCPU has a feature if it's supported by KVM and is enabled
@@ -1369,11 +1369,15 @@ static struct kvm_cpuid_entry2 *do_host_cpuid(struct kvm_cpuid_array *array,
        return entry;
 }
 
-static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func,
+static int cpuid_func_emulated(struct kvm_cpuid_entry2 *entry, u32 func, u32 index,
                               bool include_partially_emulated)
 {
        memset(entry, 0, sizeof(*entry));
 
+       /* KVM doesn't currently emulate any non-zero indices. */
+       if (cpuid_function_is_indexed(func) && index)
+               return 0;
+
        entry->function = func;
        entry->index = 0;
        entry->flags = 0;
@@ -1411,7 +1415,7 @@ static int __do_cpuid_func_emulated(struct kvm_cpuid_array *array, u32 func)
        if (array->nent >= array->maxnent)
                return -E2BIG;
 
-       array->nent += cpuid_func_emulated(&array->entries[array->nent], func, false);
+       array->nent += cpuid_func_emulated(&array->entries[array->nent], func, 0, false);
        return 0;
 }
 


  reply	other threads:[~2026-06-08 14:19 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-17  7:35 [RFC PATCH 00/27] KVM: x86: Add a paranoid mode for CPUID verification Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 01/27] KVM: x86: Fix emulated CPUID features being applied to wrong sub-leaf Binbin Wu
2026-05-15  9:03   ` Xiaoyao Li
2026-05-18  8:09     ` Binbin Wu
2026-05-18 10:51       ` Xiaoyao Li
2026-06-08  1:58   ` Binbin Wu
2026-06-08 14:19     ` Sean Christopherson [this message]
2026-06-09  1:56       ` Binbin Wu
2026-06-09  3:00         ` Sean Christopherson
2026-04-17  7:35 ` [RFC PATCH 02/27] KVM: x86: Reorder the features for CPUID 7 Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 03/27] KVM: x86: Add definitions for CPUID overlays Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 04/27] KVM: x86: Extend F() and its variants " Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 05/27] KVM: x86: Extend kvm_cpu_cap_{set/clear}() to configure overlays Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 06/27] KVM: x86: Populate TDX CPUID overlay with supported feature bits Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 07/27] KVM: x86: Support KVM_GET_{SUPPORTED,EMULATED}_CPUID as VM scope ioctls Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 08/27] KVM: x86: Thread @kvm to KVM CPU capability helpers Binbin Wu
2026-04-21  6:18   ` Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 09/27] KVM: x86: Use overlays of KVM CPU capabilities Binbin Wu
2026-04-21  5:31   ` Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 10/27] KVM: x86: Use vendor-specific overlay flags instead of F_CPUID_DEFAULT Binbin Wu
2026-04-21  6:43   ` Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 11/27] KVM: SVM: Drop unnecessary clears of unsupported common x86 features Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 12/27] KVM: x86: Split KVM CPU cap leafs into two parts Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 13/27] KVM: x86: Add a helper to initialize CPUID multi-bit fields Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 14/27] KVM: x86: Add a helper to init multiple feature bits based on raw CPUID Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 15/27] KVM: x86: Add infrastructure to track CPUID entries ignored in paranoid mode Binbin Wu
2026-04-17  7:35 ` [RFC PATCH 16/27] KVM: x86: Init allowed masks for basic CPUID range " Binbin Wu
2026-04-21  6:51   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 17/27] KVM: x86: Init allowed masks for extended " Binbin Wu
2026-04-21  7:55   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 18/27] KVM: x86: Handle Centaur CPUID leafs " Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 19/27] KVM: x86: Track KVM PV CPUID features for " Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 20/27] KVM: x86: Add per-VM flag to track CPUID " Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 21/27] KVM: x86: Make kvm_vcpu_after_set_cpuid() return an error code Binbin Wu
2026-04-22  8:22   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 22/27] KVM: x86: Verify userspace CPUID inputs in paranoid mode Binbin Wu
2026-04-22  8:59   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 23/27] KVM: x86: Account for runtime CPUID features " Binbin Wu
2026-04-23  2:41   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 24/27] KVM: x86: Skip paranoid CPUID check for KVM PV leafs when base is relocated Binbin Wu
2026-04-23  3:02   ` Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 25/27] KVM: x86: Add new KVM_CAP_X86_CPUID_PARANOID Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 26/27] KVM: x86: Add a helper to query the allowed CPUID mask Binbin Wu
2026-04-17  7:36 ` [RFC PATCH 27/27] KVM: TDX: Replace hardcoded CPUID filtering with the allowed mask Binbin Wu
2026-04-23  3:25   ` Binbin Wu
2026-05-15  8:08 ` [RFC PATCH 00/27] KVM: x86: Add a paranoid mode for CPUID verification Xiaoyao Li
2026-05-15 15:45   ` Edgecombe, Rick P
2026-05-18  7:58   ` Binbin Wu
2026-05-18 14:29     ` Sean Christopherson
2026-05-18 20:55       ` Edgecombe, Rick P
2026-05-19  2:36         ` Binbin Wu
2026-05-19  3:03           ` Edgecombe, Rick P
2026-05-18  1:38 ` Xiaoyao Li
2026-05-18  7:24   ` Binbin Wu
2026-05-18  7:47     ` Xiaoyao Li

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=aibPZMfxabh6meOk@google.com \
    --to=seanjc@google.com \
    --cc=binbin.wu@linux.intel.com \
    --cc=chao.gao@intel.com \
    --cc=kai.huang@intel.com \
    --cc=kvm@vger.kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=rick.p.edgecombe@intel.com \
    --cc=xiaoyao.li@intel.com \
    /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