Skip to content

Commit c4690d0

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Add BUILD_BUG_ON() to detect bad usage of "scattered" flags
Add a compile-time assert in the SF() macro to detect improper usage, i.e. to detect passing in an X86_FEATURE_* flag that isn't actually scattered by the kernel. Upcoming feature flags will be 100% KVM-only and will have X86_FEATURE_* macros that point at a kvm_only_cpuid_leafs word, not a kernel-defined word. Using SF() and thus boot_cpu_has() for such feature flags would access memory beyond x86_capability[NCAPINTS] and at best incorrectly hide a feature, and at worst leak kernel state to userspace. Signed-off-by: Sean Christopherson <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 7927e27 commit c4690d0

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

arch/x86/kvm/cpuid.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,13 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted)
6565
#define KVM_X86_FEATURE_AMD_PSFD (13*32+28) /* Predictive Store Forwarding Disable */
6666

6767
#define F feature_bit
68-
#define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)
68+
69+
/* Scattered Flag - For features that are scattered by cpufeatures.h. */
70+
#define SF(name) \
71+
({ \
72+
BUILD_BUG_ON(X86_FEATURE_##name >= MAX_CPU_FEATURES); \
73+
(boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0); \
74+
})
6975

7076
/*
7177
* Magic value used by KVM when querying userspace-provided CPUID entries and

0 commit comments

Comments
 (0)