Skip to content

Commit

Permalink
perf/x86/msr: Fix possible Spectre-v1 indexing in the MSR driver
Browse files Browse the repository at this point in the history
> arch/x86/events/msr.c:178 msr_event_init() warn: potential spectre issue 'msr' (local cap)

Userspace controls @attr, sanitize cfg (attr->config) before using it
to index an array.

Reported-by: Dan Carpenter <dan.carpenter@oracle.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Cc: <stable@kernel.org>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Vince Weaver <vincent.weaver@maine.edu>
Signed-off-by: Ingo Molnar <mingo@kernel.org>
  • Loading branch information
Peter Zijlstra authored and Ingo Molnar committed May 5, 2018
1 parent 46b1b57 commit 06ce6e9
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions arch/x86/events/msr.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// SPDX-License-Identifier: GPL-2.0
#include <linux/perf_event.h>
#include <linux/nospec.h>
#include <asm/intel-family.h>

enum perf_msr_id {
Expand Down Expand Up @@ -158,9 +159,6 @@ static int msr_event_init(struct perf_event *event)
if (event->attr.type != event->pmu->type)
return -ENOENT;

if (cfg >= PERF_MSR_EVENT_MAX)
return -EINVAL;

/* unsupported modes and filters */
if (event->attr.exclude_user ||
event->attr.exclude_kernel ||
Expand All @@ -171,6 +169,11 @@ static int msr_event_init(struct perf_event *event)
event->attr.sample_period) /* no sampling */
return -EINVAL;

if (cfg >= PERF_MSR_EVENT_MAX)
return -EINVAL;

cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);

if (!msr[cfg].attr)
return -EINVAL;

Expand Down

0 comments on commit 06ce6e9

Please sign in to comment.