Skip to content

Commit 13c5183

Browse files
marpombonzini
authored andcommitted
KVM: x86: Protect MSR-based index computations in pmu.h from Spectre-v1/L1TF attacks
This fixes a Spectre-v1/L1TF vulnerability in the get_gp_pmc() and get_fixed_pmc() functions. They both contain index computations based on the (attacker-controlled) MSR number. Fixes: 25462f7 ("KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch") Signed-off-by: Nick Finco <[email protected]> Signed-off-by: Marios Pomonis <[email protected]> Reviewed-by: Andrew Honig <[email protected]> Cc: [email protected] Reviewed-by: Jim Mattson <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent 25a5ede commit 13c5183

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

arch/x86/kvm/pmu.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#ifndef __KVM_X86_PMU_H
33
#define __KVM_X86_PMU_H
44

5+
#include <linux/nospec.h>
6+
57
#define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu)
68
#define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))
79
#define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu)
@@ -102,8 +104,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
102104
static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
103105
u32 base)
104106
{
105-
if (msr >= base && msr < base + pmu->nr_arch_gp_counters)
106-
return &pmu->gp_counters[msr - base];
107+
if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
108+
u32 index = array_index_nospec(msr - base,
109+
pmu->nr_arch_gp_counters);
110+
111+
return &pmu->gp_counters[index];
112+
}
107113

108114
return NULL;
109115
}
@@ -113,8 +119,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
113119
{
114120
int base = MSR_CORE_PERF_FIXED_CTR0;
115121

116-
if (msr >= base && msr < base + pmu->nr_arch_fixed_counters)
117-
return &pmu->fixed_counters[msr - base];
122+
if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
123+
u32 index = array_index_nospec(msr - base,
124+
pmu->nr_arch_fixed_counters);
125+
126+
return &pmu->fixed_counters[index];
127+
}
118128

119129
return NULL;
120130
}

0 commit comments

Comments
 (0)