Commit 15c1ef2a authored by Sean Christopherson's avatar Sean Christopherson Committed by Quanxian Wang
Browse files

KVM: x86: Add BUILD_BUG_ON() to detect bad usage of "scattered" flags

mainline inclusion
from mainline-v6.2-rc1
commit c4690d01
category: feature
bugzilla: https://gitee.com/openeuler/intel-kernel/issues/I8GYV5
CVE: N/A
Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=c4690d016182d271a862767145db8b2bc792f4a8



-------------------------------------

Intel-SIG: commit c4690d01 ("KVM: x86: Add BUILD_BUG_ON() to detect bad usage of "scattered" flags")

-------------------------------------

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: default avatarSean Christopherson <seanjc@google.com>
Message-Id: <20221125125845.1182922-2-jiaxi.chen@linux.intel.com>
Signed-off-by: default avatarPaolo Bonzini <pbonzini@redhat.com>
Signed-off-by: default avatarQuanxian Wang <quanxian.wang@intel.com>
parent 3ca7635e
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -58,7 +58,13 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted)
}

#define F feature_bit
#define SF(name) (boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0)

/* Scattered Flag - For features that are scattered by cpufeatures.h. */
#define SF(name)						\
({								\
	BUILD_BUG_ON(X86_FEATURE_##name >= MAX_CPU_FEATURES);	\
	(boot_cpu_has(X86_FEATURE_##name) ? F(name) : 0);	\
})

static inline struct kvm_cpuid_entry2 *cpuid_entry2_find(
	struct kvm_cpuid_entry2 *entries, int nent, u32 function, u32 index)