Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3701488ybv; Mon, 10 Feb 2020 04:59:57 -0800 (PST) X-Google-Smtp-Source: APXvYqzJRFTJTawHWg4n9jQNk/GObaXQvqj4qvD6jgDJb8vxz/T5Uz/DvOm1kTieZ1QB0INv5UXw X-Received: by 2002:a9d:6c01:: with SMTP id f1mr911567otq.133.1581339597346; Mon, 10 Feb 2020 04:59:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581339597; cv=none; d=google.com; s=arc-20160816; b=kd/8XNnz37u+7UhxSGR4QgjXat9JzLJ9R410G1IEF+YpHUi35n1fKjCaHmWdpqJjYa xT4gIeu2+pYy2RZowP1CsilVHE+gxzF3wgl6Wqw5lkYg9IcvQq8+B0IRMBamVoDwkPQn TlIt9c8lO9jFOtSEvOp8cqR/OlSgG1prvxNIfQuxlB39C95STF9o4t96QRWlrGJPWXJU 8V7tHxAHFZws4vMYsRTQsMEIOXx/eAEGrqwfjhcRQEUG2attXXDcAz9bKskcxLDjG5pb TuV1yK3vb9hc8r6xq/vlp+lmjUvVOA6LX/g+2vx8T8vybW5KDi0Ig3hQOtUyGxg5JDbO khHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=JgmB4HvSqtJBgurQahPbBT4ioT0AS2iQmA7FHt5gMrg=; b=CIR77uViVoRfvp9jU+LvysTxLokUkY9iMITafIZhRn98v7/TysQSKlXv6yJVgY+0Xg 3VQA+OoObZR9wN3oF/zVShVfT5XvFsoP7Mb8A2MFBfY4PQZ3C320i57c1ap599oUtqsy 6vtKzKj4U94id2zbqZx3dWu/kO6J6XGOBoK/0HYiQWMmR7VJnDbltAUr+8hvz5LsClRT OOktAIt63DhkY2xzH1gbWa/BDLVEHxhWGqMToK/Y6N0hsAWe+1QjTM2a01tQ8kc5oQol /mWecWqPWYVK+wO55SLEBebO2zKGIb6bhNMppJoyfBd4NlcUM1rjzPiDlYWJzHwCwlR5 dAWg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wMN4pInG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9si155384otp.57.2020.02.10.04.59.46; Mon, 10 Feb 2020 04:59:57 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=wMN4pInG; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1730168AbgBJM7m (ORCPT + 99 others); Mon, 10 Feb 2020 07:59:42 -0500 Received: from mail.kernel.org ([198.145.29.99]:42636 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729896AbgBJMlH (ORCPT ); Mon, 10 Feb 2020 07:41:07 -0500 Received: from localhost (unknown [209.37.97.194]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 472392051A; Mon, 10 Feb 2020 12:41:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581338467; bh=MkI4Fn1ZQS+DuXc6JBCeYTPbMyJ9HcFdg6Ig2ScxoZs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=wMN4pInGEIfBwrjcA9Ph5aX0KiMhfuuSbTEW8daBvI4uGnPF752RLA8BNRK16EfEE tswfTkvW2ltBT2ypqToBjeGmcNcJHipNp8iuHbva0fi2JO2j0bMNx/Mup7VjGULNDN cDTAV9TV0u2d0V6e+SgCemELdQVWCHv0fZaF3E4I= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Nick Finco , Marios Pomonis , Andrew Honig , Jim Mattson , Paolo Bonzini Subject: [PATCH 5.5 229/367] KVM: x86: Protect pmu_intel.c from Spectre-v1/L1TF attacks Date: Mon, 10 Feb 2020 04:32:22 -0800 Message-Id: <20200210122445.484838003@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200210122423.695146547@linuxfoundation.org> References: <20200210122423.695146547@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Marios Pomonis commit 66061740f1a487f4ed54fde75e724709f805da53 upstream. 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 Signed-off-by: Marios Pomonis Reviewed-by: Andrew Honig Cc: stable@vger.kernel.org Reviewed-by: Jim Mattson Signed-off-by: Paolo Bonzini Signed-off-by: Greg Kroah-Hartman --- arch/x86/kvm/vmx/pmu_intel.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -86,10 +86,14 @@ static unsigned intel_find_arch_event(st 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. */ @@ -130,16 +134,20 @@ static struct kvm_pmc *intel_rdpmc_ecx_t 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)