Skip to contentSkip to conversation

target/i386/cpu: don't assert on out-of-range CPUID 0x1f subleaf

PATCH In review Ewan Hai-oc 2 participants 3 messages Jun 9 – Jul 17

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 target/i386/cpu: don't assert on out-of-range CPUID 0x1f subleaf

encode_topo_cpuid1f() asserts count <= CPU_TOPOLOGY_LEVEL_SOCKET, but count is the subleaf index passed straight from the guest's ECX; cpu_x86_cpuid() does not clamp it for leaf 0x1f. On a TCG vCPU exposing 0x1f, any CPUID(0x1f) with ECX > 5 aborts QEMU. SeaBIOS already trips this during POST (its cpuid() helper leaves a stale ECX from the EFLAGS.ID probe), and guests reach it too,the kernel walks 0x1f subleaves in a loop and /dev/cpu/*/cpuid forwards arbitrary subleaves.

Per the SDM, CPUID never faults and a subleaf past the last topology level returns the invalid encoding (EAX=EBX=0, ECX level type 0, EDX the x2APIC ID), exactly as the legacy 0xB leaf already does. Return that instead of asserting. KVM is unaffected.

Affected models: SapphireRapids (v4+), GraniteRapids (v3+), SierraForest (v3+), ClearwaterForest (v3+), DiamondRapids and YongFeng (v3+).

Fixes 822bce9f58df ("i386/cpu: Decouple CPUID[0x1F] subleaf with specific topology level")
---
 target/i386/cpu.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
target/i386/cpu.c +8 -1
index 8929a75c7c..e035e4cf45 100644
@@ -427,7 +427,14 @@ static void encode_topo_cpuid1f(CPUX86State *env, uint32_t count,
     unsigned long level, base_level, next_level;
     uint32_t num_threads_next_level, offset_next_level;
 
-    assert(count <= CPU_TOPOLOGY_LEVEL_SOCKET);
+    /* Out-of-range subleaf: SDM mandates the invalid encoding, no fault. */
+    if (count > CPU_TOPOLOGY_LEVEL_SOCKET) {
+        *eax = 0;
+        *ebx = 0;
+        *ecx = count & 0xff;
+        *edx = cpu->apic_id;
+        return;
+    }
 
     /*
      * Find the No.(count + 1) topology level in avail_cpu_topo bitmap.

base-commit: cc329c491768b2d91eb0b0984f3baa0bf805776d

Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Ewan Hai-oc
Quoted 19 lines · Ewan Hai-oc

Date: Tue, 9 Jun 2026 17:00:46 +0800
From: Ewan Hai-oc <[email protected]>
Subject: [PATCH] target/i386/cpu: don't assert on out-of-range CPUID 0x1f
subleaf
X-Mailer: git-send-email 2.34.1

encode_topo_cpuid1f() asserts count <= CPU_TOPOLOGY_LEVEL_SOCKET, but count is the subleaf index passed straight from the guest's ECX; cpu_x86_cpuid() does not clamp it for leaf 0x1f. On a TCG vCPU exposing 0x1f, any CPUID(0x1f) with ECX > 5 aborts QEMU. SeaBIOS already trips this during POST (its cpuid() helper leaves a stale ECX from the EFLAGS.ID probe), and guests reach it too,the kernel walks 0x1f subleaves in a loop and /dev/cpu/*/cpuid forwards arbitrary subleaves.

Per the SDM, CPUID never faults and a subleaf past the last topology level returns the invalid encoding (EAX=EBX=0, ECX level type 0, EDX the x2APIC ID), exactly as the legacy 0xB leaf already does. Return that instead of asserting. KVM is unaffected.

Quoted 8 lines

Affected models: SapphireRapids (v4+), GraniteRapids (v3+), SierraForest (v3+), ClearwaterForest (v3+), DiamondRapids and YongFeng (v3+).

Fixes 822bce9f58df ("i386/cpu: Decouple CPUID[0x1F] subleaf with specific topology level")
---
 target/i386/cpu.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

(cc stable list)

Yes, good catch!

On a TCG vCPU exposing 0x1f, requesting an invalid subleaf triggers this abort, e.g.:

cpuid -1 -l 0x1f -s 8

Thanks for the fix,

Regards,
Zhao