KVM: x86: avoid out of bounds indices for fixed performance counters
Paolo Bonzini
maintainerPATCH 1/1 KVM: x86: avoid out of bounds indices for fixed performance counters
Because IceLake has 4 fixed performance counters but KVM only supports 3, it is possible for reprogram_fixed_counters to pass to reprogram_fixed_counter an index that is out of bounds for the fixed_pmc_events array.
Ultimately intel_find_fixed_event, which is the only place that uses fixed_pmc_events, handles this correctly because it checks against the size of fixed_pmc_events anyway. Every other place operates on the fixed_counters[] array which is sized according to INTEL_PMC_MAX_FIXED. However, it is cleaner if the unsupported performance counters are culled early on
in reprogram_fixed_counters.
--- arch/x86/kvm/vmx/pmu_intel.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +4 -3
@@ -91,7 +91,7 @@ static unsigned intel_find_fixed_event(int idx)u32 event;size_t size =ARRAY_SIZE (fixed_pmc_events); -if (idx >= size) +if (WARN_ON_ONCE (idx >= size))return PERF_COUNT_HW_MAX ; event = fixed_pmc_events[array_index_nospec (idx, size)];
@@ -500,8 +500,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->nr_arch_fixed_counters =0 ; }else { pmu->nr_arch_fixed_counters = -min_t (int, edx.split.num_counters_fixed, - x86_pmu.num_counters_fixed); +min3 (ARRAY_SIZE (fixed_pmc_events), + (size_t ) edx.split.num_counters_fixed, + (size_t ) x86_pmu.num_counters_fixed); edx.split.bit_width_fixed =min_t (int, edx.split.bit_width_fixed, x86_pmu.bit_width_fixed); pmu->counter_bitmask[KVM_PMC_FIXED] =
Signature · 1 line
2.31.1
Quoted 2 lines · Paolo Bonzini
Because IceLake has 4 fixed performance counters but KVM only supports 3, it is possible for
reprogram_fixed_countersto pass to
Emm, it's possible for a unwise user space or smart syzkaller.
Quoted 10 lines
reprogram_fixed_counteran index that is out of bounds for thefixed_pmc_eventsarray.Ultimately
intel_find_fixed_event, which is the only place that usesfixed_pmc_events, handles this correctly because it checks against the size offixed_pmc_eventsanyway. Every other place operates on the fixed_counters[] array which is sized according toINTEL_PMC_MAX_FIXED. However, it is cleaner if the unsupported performance counters are culled early on
inreprogram_fixed_counters.
How about introducing a static "struct x86_pmu_capability" variable [1] so that we can
(1) setup num_counters_fixed just once in the kvm_init_pmu_capability(), and
(2) avoid repeated calls to perf_get_x86_pmu_capability() ;
?
[1] https://lore.kernel.org/kvm/[email protected]/
By the way, do you need a re-based version of the guest PBES feature ?
Thanks,
Like Xu
Quoted 31 lines
Signed-off-by Paolo Bonzini <[email protected]>--- arch/x86/kvm/vmx/pmu_intel.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/pmu_intel.c +4
-3@@ -91,7 +91,7 @@ static unsigned intel_find_fixed_event(int idx)u32 event;size_t size =ARRAY_SIZE (fixed_pmc_events); -if (idx >= size) +if (WARN_ON_ONCE (idx >= size))return PERF_COUNT_HW_MAX ; event = fixed_pmc_events[array_index_nospec (idx, size)];@@ -500,8 +500,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->nr_arch_fixed_counters =0 ; }else { pmu->nr_arch_fixed_counters = -min_t (int, edx.split.num_counters_fixed, - x86_pmu.num_counters_fixed); +min3 (ARRAY_SIZE (fixed_pmc_events), + (size_t ) edx.split.num_counters_fixed, + (size_t ) x86_pmu.num_counters_fixed); edx.split.bit_width_fixed =min_t (int, edx.split.bit_width_fixed, x86_pmu.bit_width_fixed); pmu->counter_bitmask[KVM_PMC_FIXED] =
Paolo Bonzini
maintainerQuoted 5 lines · Like Xu
How about introducing a static "struct
x86_pmu_capability" variable [1] so that we can(1) setup
num_counters_fixedjust once in thekvm_init_pmu_capability(), and
(2) avoid repeated calls toperf_get_x86_pmu_capability();
Yes, that works.
Quoted 2 lines
[1] https://lore.kernel.org/kvm/[email protected]/
By the way, do you need a re-based version of the guest PBES feature ?
Sure, please send one.
Paolo