Skip to content

Commit 5edbd2d

Browse files
Peter Zijlstragregkh
authored andcommitted
perf/x86/msr: Fix possible Spectre-v1 indexing in the MSR driver
commit 06ce6e9 upstream. > 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> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 688d5d9 commit 5edbd2d

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

arch/x86/events/msr.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <linux/perf_event.h>
2+
#include <linux/nospec.h>
23
#include <asm/intel-family.h>
34

45
enum perf_msr_id {
@@ -136,9 +137,6 @@ static int msr_event_init(struct perf_event *event)
136137
if (event->attr.type != event->pmu->type)
137138
return -ENOENT;
138139

139-
if (cfg >= PERF_MSR_EVENT_MAX)
140-
return -EINVAL;
141-
142140
/* unsupported modes and filters */
143141
if (event->attr.exclude_user ||
144142
event->attr.exclude_kernel ||
@@ -149,6 +147,11 @@ static int msr_event_init(struct perf_event *event)
149147
event->attr.sample_period) /* no sampling */
150148
return -EINVAL;
151149

150+
if (cfg >= PERF_MSR_EVENT_MAX)
151+
return -EINVAL;
152+
153+
cfg = array_index_nospec((unsigned long)cfg, PERF_MSR_EVENT_MAX);
154+
152155
if (!msr[cfg].attr)
153156
return -EINVAL;
154157

0 commit comments

Comments
 (0)