Skip to content

Commit 06ce6e9

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
perf/x86/msr: Fix possible Spectre-v1 indexing in the MSR driver
> 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 <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Signed-off-by: Ingo Molnar <[email protected]>
1 parent 46b1b57 commit 06ce6e9

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

arch/x86/events/msr.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/perf_event.h>
3+
#include <linux/nospec.h>
34
#include <asm/intel-family.h>
45

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

161-
if (cfg >= PERF_MSR_EVENT_MAX)
162-
return -EINVAL;
163-
164162
/* unsupported modes and filters */
165163
if (event->attr.exclude_user ||
166164
event->attr.exclude_kernel ||
@@ -171,6 +169,11 @@ static int msr_event_init(struct perf_event *event)
171169
event->attr.sample_period) /* no sampling */
172170
return -EINVAL;
173171

172+
if (cfg >= PERF_MSR_EVENT_MAX)
173+
return -EINVAL;
174+
175+
cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
176+
174177
if (!msr[cfg].attr)
175178
return -EINVAL;
176179

0 commit comments

Comments
 (0)