]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/commitdiff
KVM: x86: Protect pmu_intel.c from Spectre-v1/L1TF attacks
authorMarios Pomonis <pomonis@google.com>
Wed, 11 Dec 2019 20:47:53 +0000 (12:47 -0800)
committerKhalid Elmously <khalid.elmously@canonical.com>
Fri, 13 Mar 2020 04:31:00 +0000 (00:31 -0400)
BugLink: https://bugs.launchpad.net/bugs/1866678
[ Upstream commit 66061740f1a487f4ed54fde75e724709f805da53 ]

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: 25462f7f5295 ("KVM: x86/vPMU: Define kvm_pmu_ops to support vPMU function dispatch")
Signed-off-by: Nick Finco <nifi@google.com>
Signed-off-by: Marios Pomonis <pomonis@google.com>
Reviewed-by: Andrew Honig <ahonig@google.com>
Cc: stable@vger.kernel.org
Reviewed-by: Jim Mattson <jmattson@google.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Kamal Mostafa <kamal@canonical.com>
Signed-off-by: Khalid Elmously <khalid.elmously@canonical.com>
arch/x86/kvm/pmu_intel.c

index c3f103e2b08e15b7286d81306cb8a5694db8e75f..2ab8c20c8bfada19011396fa1826e761b1c8986d 100644 (file)
@@ -87,10 +87,14 @@ static unsigned intel_find_arch_event(struct kvm_pmu *pmu,
 
 static unsigned intel_find_fixed_event(int idx)
 {
-       if (idx >= ARRAY_SIZE(fixed_pmc_events))
+       u32 event;
+       size_t size = ARRAY_SIZE(fixed_pmc_events);
+
+       if (idx >= size)
                return PERF_COUNT_HW_MAX;
 
-       return intel_arch_events[fixed_pmc_events[idx]].event_type;
+       event = fixed_pmc_events[array_index_nospec(idx, size)];
+       return intel_arch_events[event].event_type;
 }
 
 /* check if a PMC is enabled by comparing it with globl_ctrl bits. */
@@ -131,16 +135,20 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
        bool fixed = idx & (1u << 30);
        struct kvm_pmc *counters;
+       unsigned int num_counters;
 
        idx &= ~(3u << 30);
-       if (!fixed && idx >= pmu->nr_arch_gp_counters)
-               return NULL;
-       if (fixed && idx >= pmu->nr_arch_fixed_counters)
+       if (fixed) {
+               counters = pmu->fixed_counters;
+               num_counters = pmu->nr_arch_fixed_counters;
+       } else {
+               counters = pmu->gp_counters;
+               num_counters = pmu->nr_arch_gp_counters;
+       }
+       if (idx >= num_counters)
                return NULL;
-       counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
        *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
-
-       return &counters[idx];
+       return &counters[array_index_nospec(idx, num_counters)];
 }
 
 static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)