Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp1118920ybl; Wed, 11 Dec 2019 12:50:09 -0800 (PST) X-Google-Smtp-Source: APXvYqw2L0akIPs95raTYswAFwl/9EpYcfpWZawzHwUhSzJuZJqKIOaVFr2AJfB5pMAiKwELssn/ X-Received: by 2002:a05:6808:aac:: with SMTP id r12mr4334562oij.59.1576097409807; Wed, 11 Dec 2019 12:50:09 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576097409; cv=none; d=google.com; s=arc-20160816; b=h1MLUhP9IOnp7moIJ/8UblzslvulPXQjeSky3a4DgTmc2uIInKKEZmDC0dCCDMq6lq G1BHDcf5BtJ2L1A+MIrLIIE566v2DE0MceAcRPD+0mgdX6x0Xnx9NyOcuO+ZSeQTqrTc gAjhkFxe5PTeKF2T5VojzIwBM5bBUOmxZrt1dmMZFa8o75FhrDiAV2y3nkNvYhnxNqee mDclpau64q+M5PJfZW4fafZe38lNPsJI9KJBpM5j8rKw5mx9uN8OENKFkz74c72f8eu5 3BzXndWtFONgCGYAsHeBQ8J99RTN+HN8a/+AZPOeewwaizw2eXyZAupARM+cffVj86kK wdjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=q2WftxLFBC38ZplqpzPFiWQPxTVG5vmfpl6ZFdDx0Ls=; b=Mqs6uvL2mt8RQrDlGE81J04+XDrwXg1l03UwbaO7MJ3XdrkEKhHbwBv0R+LyknhMgf gijtb19Lx22hqiytbMpyPhXOtNG8/WT6MgFP4v9IPlYduJrMgjZMuv0vjkg8DcMLmEi2 h6oT/MkDX5YcRQ9TFIEjQUB0ukZiY4W6xtKRPGkiCiPAYm1eB2mtdcsGQhO3JUm9n4Fd RODRoV73uORaNYDpvHfiI3I7zH60nfePSZMHJMZ2o+YSM2w1i/7yjwRR4svGs4hxDvRL onspUr1t7qSrxOoelhzeNlofmfRr0Y9z+ULioQVA53nqsRy8e714ZyWMZDQZWW8BWdHf UDIw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=n38F3O5W; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w129si1901563oig.83.2019.12.11.12.49.58; Wed, 11 Dec 2019 12:50:09 -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=@google.com header.s=20161025 header.b=n38F3O5W; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727327AbfLKUtB (ORCPT + 99 others); Wed, 11 Dec 2019 15:49:01 -0500 Received: from mail-pg1-f201.google.com ([209.85.215.201]:34326 "EHLO mail-pg1-f201.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727312AbfLKUs6 (ORCPT ); Wed, 11 Dec 2019 15:48:58 -0500 Received: by mail-pg1-f201.google.com with SMTP id w9so56239pgl.1 for ; Wed, 11 Dec 2019 12:48:58 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=q2WftxLFBC38ZplqpzPFiWQPxTVG5vmfpl6ZFdDx0Ls=; b=n38F3O5Wj72FHpKAB1iuO1qaxLkiOywJqGKJuKRPX9DRIUOVnbjea5yRxkMvMW08fi YLWDubVosTKLT9/ex20DfZ4MzGfCk7Ut5ki6Ovf9RDL/Wfj/XCVCedl/6l+bai2LKPa6 IHIUk/Y2ejoWqHqahSGxr7E7SCWFzR0tCFExxFpjsAsQ/b+Gkw1gSe1t0XrBruX7JSx2 0nLnl2vLR/4nil+bA1itCNMwDQuI5QCy8+TPojba4ykkfKBRH08TTHAYGSwtYZUbweAF bUw02KELO0yzVboH3k6uwcX///DkmaUdzbbSR57n3esKFCDEKpED2d9tImt571EN+0in o3mQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=q2WftxLFBC38ZplqpzPFiWQPxTVG5vmfpl6ZFdDx0Ls=; b=SRYy1Grpm512lpRMearCehTzmO/d4Kp+deIaPKWSZaegTeELN76+/3or6VDBE2Bp5/ mjdOkn6kvh3g5DVV4N9R4Phj5Rpx3YeVRi56WR9knpVsGqs6/RwMpIVW9ZPfbbtYqhl+ +OPTD9NAJHNwWSPI963criqPqnRNjNgWdrV8b6yBEphYrZTPpOlUFON4fgazaqya+NHV HplDkE88hcPpwCCGZRpJaIzpNiKYvGl0g9NrUZ5cJ2r6eegAHEQSN4kgnyNIgDGJHOOQ ZzXWbmMcD/g2d20JH7+tJJFfyx75CGCAFBqM15HMRLniEPI3xQ/sKCFBKHAop6pCsAOQ Ff/Q== X-Gm-Message-State: APjAAAVU5yUOR+zHPW1TShTwTFC6Wx5GR8u5tlSEBfNunNCxE/SWjU2J R5sH9H3sxiAruPapT6QAHbhQ6sWQ9uZz X-Received: by 2002:a63:31cf:: with SMTP id x198mr6253789pgx.272.1576097337682; Wed, 11 Dec 2019 12:48:57 -0800 (PST) Date: Wed, 11 Dec 2019 12:47:48 -0800 In-Reply-To: <20191211204753.242298-1-pomonis@google.com> Message-Id: <20191211204753.242298-9-pomonis@google.com> Mime-Version: 1.0 References: <20191211204753.242298-1-pomonis@google.com> X-Mailer: git-send-email 2.24.0.525.g8f36a354ae-goog Subject: [PATCH v2 08/13] KVM: x86: Protect MSR-based index computations in pmu.h from Spectre-v1/L1TF attacks From: Marios Pomonis To: Paolo Bonzini , rkrcmar@redhat.com, Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel Cc: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "H. Peter Anvin" , x86@kernel.org, kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Nick Finco , Andrew Honig , Marios Pomonis , stable@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This fixes a Spectre-v1/L1TF vulnerability in the get_gp_pmc() and get_fixed_pmc() functions. They both contain index computations based on the (attacker-controlled) MSR number. Fixes: commit 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 --- arch/x86/kvm/pmu.h | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h index 7ebb62326c14..13332984b6d5 100644 --- a/arch/x86/kvm/pmu.h +++ b/arch/x86/kvm/pmu.h @@ -2,6 +2,8 @@ #ifndef __KVM_X86_PMU_H #define __KVM_X86_PMU_H +#include + #define vcpu_to_pmu(vcpu) (&(vcpu)->arch.pmu) #define pmu_to_vcpu(pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu)) #define pmc_to_pmu(pmc) (&(pmc)->vcpu->arch.pmu) @@ -102,8 +104,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu, static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr, u32 base) { - if (msr >= base && msr < base + pmu->nr_arch_gp_counters) - return &pmu->gp_counters[msr - base]; + if (msr >= base && msr < base + pmu->nr_arch_gp_counters) { + u32 index = array_index_nospec(msr - base, + pmu->nr_arch_gp_counters); + + return &pmu->gp_counters[index]; + } return NULL; } @@ -113,8 +119,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr) { int base = MSR_CORE_PERF_FIXED_CTR0; - if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) - return &pmu->fixed_counters[msr - base]; + if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) { + u32 index = array_index_nospec(msr - base, + pmu->nr_arch_fixed_counters); + + return &pmu->fixed_counters[index]; + } return NULL; } -- 2.24.0.525.g8f36a354ae-goog