From: Ian Rogers Date: Wed, 20 May 2026 19:05:34 +0000 (-0700) Subject: perf sched: Bounds check CPU in sched switch events X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=16ccbec0f3e14f6ad06af6112ea4fa5668cab46a;p=thirdparty%2Fkernel%2Fstable.git perf sched: Bounds check CPU in sched switch events Ensure CPU indexes parsed from sched switch and runtime events fit within the MAX_CPUS limit to prevent out-of-bounds indexing. Add explicit bounds checks for sample->cpu against MAX_CPUS inside process_sched_switch_event, process_sched_runtime_event, and timehist_sched_change_event. This prevents indexing beyond the boundaries of the sched->curr_pid tracking array, avoiding potential memory corruption or undefined behavior. Signed-off-by: Ian Rogers Acked-by: Namhyung Kim Cc: Adrian Hunter Cc: Albert Ou Cc: Alexander Shishkin Cc: Alexandre Ghiti Cc: Andi Kleen Cc: Andrew Jones Cc: Anup Patel Cc: Athira Rajeev Cc: Blake Jones Cc: Chen Ni Cc: Chun-Tse Shao Cc: Dapeng Mi Cc: Derek Foreman Cc: Dmitriy Vyukov Cc: Dr. David Alan Gilbert Cc: Howard Chu Cc: Hrishikesh Suresh Cc: Ingo Molnar Cc: James Clark Cc: Jiri Olsa Cc: Krzysztof Ɓopatowski Cc: Leo Yan Cc: Palmer Dabbelt Cc: Paul Walmsley Cc: Peter Zijlstra Cc: Quan Zhou Cc: Ravi Bangoria Cc: Swapnil Sapkal Cc: Thomas Falcon Cc: Tianyou Li Cc: Yujie Liu Cc: tanze Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/builtin-sched.c b/tools/perf/builtin-sched.c index d984e58c7dbf..9d73c7043182 100644 --- a/tools/perf/builtin-sched.c +++ b/tools/perf/builtin-sched.c @@ -1791,6 +1791,11 @@ static int process_sched_switch_event(const struct perf_tool *tool, u32 prev_pid = perf_sample__intval(sample, "prev_pid"), next_pid = perf_sample__intval(sample, "next_pid"); + if (this_cpu < 0 || this_cpu >= MAX_CPUS) { + pr_warning("Out-of-bound sample CPU %d. Skipping sample\n", this_cpu); + return 0; + } + if (sched->curr_pid[this_cpu] != (u32)-1) { /* * Are we trying to switch away a PID that is @@ -1813,6 +1818,11 @@ static int process_sched_runtime_event(const struct perf_tool *tool, { struct perf_sched *sched = container_of(tool, struct perf_sched, tool); + if (sample->cpu >= MAX_CPUS) { + pr_warning("Out-of-bound sample CPU %u. Skipping sample\n", sample->cpu); + return 0; + } + if (sched->tp_handler->runtime_event) return sched->tp_handler->runtime_event(sched, sample, machine); @@ -2775,6 +2785,11 @@ static int timehist_sched_change_event(const struct perf_tool *tool, int rc = 0; const char state = perf_sample__taskstate(sample, "prev_state"); + if (sample->cpu >= MAX_CPUS) { + pr_warning("Out-of-bound sample CPU %d. Skipping sample\n", sample->cpu); + return 0; + } + addr_location__init(&al); if (machine__resolve(machine, &al, sample) < 0) { pr_err("problem processing %d event. skipping it\n",