Skip to content

Commit ce02876

Browse files
marpomgregkh
authored andcommitted
KVM: x86: Protect pmu_intel.c from Spectre-v1/L1TF attacks
[ Upstream commit 6606174 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent a31a66f commit ce02876

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

arch/x86/kvm/pmu_intel.c

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

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

93-
return intel_arch_events[fixed_pmc_events[idx]].event_type;
96+
event = fixed_pmc_events[array_index_nospec(idx, size)];
97+
return intel_arch_events[event].event_type;
9498
}
9599

96100
/* check if a PMC is enabled by comparing it with globl_ctrl bits. */
@@ -131,15 +135,19 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
131135
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
132136
bool fixed = idx & (1u << 30);
133137
struct kvm_pmc *counters;
138+
unsigned int num_counters;
134139

135140
idx &= ~(3u << 30);
136-
if (!fixed && idx >= pmu->nr_arch_gp_counters)
137-
return NULL;
138-
if (fixed && idx >= pmu->nr_arch_fixed_counters)
141+
if (fixed) {
142+
counters = pmu->fixed_counters;
143+
num_counters = pmu->nr_arch_fixed_counters;
144+
} else {
145+
counters = pmu->gp_counters;
146+
num_counters = pmu->nr_arch_gp_counters;
147+
}
148+
if (idx >= num_counters)
139149
return NULL;
140-
counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
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)