Skip to content

Commit 7281c8d

Browse files
Peter ZijlstraIngo Molnar
authored andcommitted
sched/core: Fix possible Spectre-v1 indexing for sched_prio_to_weight[]
> kernel/sched/core.c:6921 cpu_weight_nice_write_s64() warn: potential spectre issue 'sched_prio_to_weight' Userspace controls @nice, so sanitize the value 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: Linus Torvalds <[email protected]> Cc: Mike Galbraith <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: [email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent b5bf9a9 commit 7281c8d

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

kernel/sched/core.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sched.h"
99

1010
#include <linux/kthread.h>
11+
#include <linux/nospec.h>
1112

1213
#include <asm/switch_to.h>
1314
#include <asm/tlb.h>
@@ -6923,11 +6924,15 @@ static int cpu_weight_nice_write_s64(struct cgroup_subsys_state *css,
69236924
struct cftype *cft, s64 nice)
69246925
{
69256926
unsigned long weight;
6927+
int idx;
69266928

69276929
if (nice < MIN_NICE || nice > MAX_NICE)
69286930
return -ERANGE;
69296931

6930-
weight = sched_prio_to_weight[NICE_TO_PRIO(nice) - MAX_RT_PRIO];
6932+
idx = NICE_TO_PRIO(nice) - MAX_RT_PRIO;
6933+
idx = array_index_nospec(idx, 40);
6934+
weight = sched_prio_to_weight[idx];
6935+
69316936
return sched_group_set_shares(css_tg(css), scale_load(weight));
69326937
}
69336938
#endif

0 commit comments

Comments
 (0)