Skip to content

Commit 6606174

Browse files
marpombonzini
authored andcommitted
KVM: x86: Protect pmu_intel.c from Spectre-v1/L1TF attacks
This fixes Spectre-v1/L1TF vulnerabilities in intel_find_fixed_event() and intel_rdpmc_ecx_to_pmc(). kvm_rdpmc() (ancestor of intel_find_fixed_event()) and reprogram_fixed_counter() (ancestor of intel_rdpmc_ecx_to_pmc()) are exported symbols so KVM should treat them conservatively from a security perspective. 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 ea74005 commit 6606174

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
8686

8787
static unsigned intel_find_fixed_event(int idx)
8888
{
89-
if (idx >= ARRAY_SIZE(fixed_pmc_events))
89+
u32 event;
90+
size_t size = ARRAY_SIZE(fixed_pmc_events);
91+
92+
if (idx >= size)
9093
return PERF_COUNT_HW_MAX;
9194

92-
return intel_arch_events[fixed_pmc_events[idx]].event_type;
95+
event = fixed_pmc_events[array_index_nospec(idx, size)];
96+
return intel_arch_events[event].event_type;
9397
}
9498

9599
/* check if a PMC is enabled by comparing it with globl_ctrl bits. */
@@ -130,16 +134,20 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
130134
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
131135
bool fixed = idx & (1u << 30);
132136
struct kvm_pmc *counters;
137+
unsigned int num_counters;
133138

134139
idx &= ~(3u << 30);
135-
if (!fixed && idx >= pmu->nr_arch_gp_counters)
136-
return NULL;
137-
if (fixed && idx >= pmu->nr_arch_fixed_counters)
140+
if (fixed) {
141+
counters = pmu->fixed_counters;
142+
num_counters = pmu->nr_arch_fixed_counters;
143+
} else {
144+
counters = pmu->gp_counters;
145+
num_counters = pmu->nr_arch_gp_counters;
146+
}
147+
if (idx >= num_counters)
138148
return NULL;
139-
counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
140149
*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
141-
142-
return &counters[idx];
150+
return &counters[array_index_nospec(idx, num_counters)];
143151
}
144152

145153
static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)

0 commit comments

Comments
 (0)