Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp666255ybv; Thu, 13 Feb 2020 07:26:35 -0800 (PST) X-Google-Smtp-Source: APXvYqwTrgKgKy3UsXbjFXEALIr9qpbnXVFkpcUPGNpvz3FSSWRc+N/6P+5v9VqemZIUQZ6UY7q6 X-Received: by 2002:a54:4896:: with SMTP id r22mr3305538oic.30.1581607595811; Thu, 13 Feb 2020 07:26:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581607595; cv=none; d=google.com; s=arc-20160816; b=iJ5owKeETB7r/ayK6bGu3HfvpIdq0sAbFtLh9D7z8jsYXHKLCDPkSePBeMJDttwjAb N/YhiQnEWnO6q6ZvpFfPYorg+q+lQe3rVk+s0Obt+uhJdZcmVcvVUXopIOAFvRbt3V06 wngQtdbTu6rxEpSx/NM+AzPeRKcdwBeEwz+fadef0wP/yDfPmEEkoigrSliA6EInTHkk 9w/K3ZmMWZzOtFnuQRKM5rCAdyhS+FWqtGS0SCUkiMf5xLEnSVGTBk85KgrnNlFqU5v/ tp0dRBzrZRS8UCPAMNPDlmTShugmZyGUB3v3xTeg+PUeFOQRUj3rv+kFgkzzuLnRALfE pt4g== 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=jUqaXsSnj0kbkPKRop4zD69wivJIaIj0ZAtzBNWjJNE=; b=qBC36raoOBzycIHCRgKHFRhmCKg6UZwnYbtNHSbOSJViGrkIX7DWsKyrk3+FZ0oYCe sYbs7B5YA+PSn/DjHbZU12KftTaJS/k79BTFzNKUqslq2RGSLHLrjjlFFTWXXRYaIZrq MMQemooTb2uvYg0yxzWsIlbnrtOvZ+k4RJYNAQkboLTIkz/QoTJOZpatIrB7+Un0aAUe OqWin9n5uiIjSLN0tnfx/7mlel1yZbyMq3lECbjoKSKRpnnI6BCXyMXhVOoegWUyz2yX oGNdkOrDfW3T/u7j0tNr3N7rULq/byDaoyJbgNfCACGJN7p1/LsQKx4r04oI59A6ha8o 6mKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=byVQGIR0; 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 h138si1255092oib.6.2020.02.13.07.26.23; Thu, 13 Feb 2020 07:26:35 -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=byVQGIR0; 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 S1729076AbgBMPZs (ORCPT + 99 others); Thu, 13 Feb 2020 10:25:48 -0500 Received: from mail.kernel.org ([198.145.29.99]:36772 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728589AbgBMPYG (ORCPT ); Thu, 13 Feb 2020 10:24:06 -0500 Received: from localhost (unknown [104.132.1.104]) (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 5526B24689; Thu, 13 Feb 2020 15:24:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1581607446; bh=u8zKPE2MDJBP7Xodhz8YBPY1I5BV9w4c8q36pzW/TeY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=byVQGIR0EUf3QL1/IakVT2QL55KdTLNA4OZkJRbwxI14tBNE7zbuVk/yzQysv2B5/ a2zzcYTGcJ1dYqa4t4JsMDf7D6Jo/ThLTRQWkI/XtT9/4dnWZHyX2vz+N3IQty6r7F lWDCmXs4bC94lpWKC7GODevWINx15pSRQoNB1oxg= 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 , Sasha Levin Subject: [PATCH 4.9 088/116] KVM: x86: Protect pmu_intel.c from Spectre-v1/L1TF attacks Date: Thu, 13 Feb 2020 07:20:32 -0800 Message-Id: <20200213151917.023068223@linuxfoundation.org> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200213151842.259660170@linuxfoundation.org> References: <20200213151842.259660170@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 [ 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 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: Sasha Levin --- arch/x86/kvm/pmu_intel.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/pmu_intel.c b/arch/x86/kvm/pmu_intel.c index 2729131fe9bfc..84ae4dd261caf 100644 --- a/arch/x86/kvm/pmu_intel.c +++ b/arch/x86/kvm/pmu_intel.c @@ -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,15 +135,19 @@ 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; - - return &counters[idx]; + return &counters[array_index_nospec(idx, num_counters)]; } static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) -- 2.20.1