]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
perf sched: Bounds check CPU in sched switch events
authorIan Rogers <irogers@google.com>
Wed, 20 May 2026 19:05:34 +0000 (12:05 -0700)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 May 2026 19:39:41 +0000 (16:39 -0300)
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 <irogers@google.com>
Acked-by: Namhyung Kim <namhyung@kernel.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Albert Ou <aou@eecs.berkeley.edu>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Alexandre Ghiti <alex@ghiti.fr>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Andrew Jones <ajones@ventanamicro.com>
Cc: Anup Patel <anup@brainfault.org>
Cc: Athira Rajeev <atrajeev@linux.ibm.com>
Cc: Blake Jones <blakejones@google.com>
Cc: Chen Ni <nichen@iscas.ac.cn>
Cc: Chun-Tse Shao <ctshao@google.com>
Cc: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Derek Foreman <derek.foreman@collabora.com>
Cc: Dmitriy Vyukov <dvyukov@google.com>
Cc: Dr. David Alan Gilbert <linux@treblig.org>
Cc: Howard Chu <howardchu95@gmail.com>
Cc: Hrishikesh Suresh <hrishikesh123s@gmail.com>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Krzysztof Ɓopatowski <krzysztof.m.lopatowski@gmail.com>
Cc: Leo Yan <leo.yan@arm.com>
Cc: Palmer Dabbelt <palmer@dabbelt.com>
Cc: Paul Walmsley <pjw@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Quan Zhou <zhouquan@iscas.ac.cn>
Cc: Ravi Bangoria <ravi.bangoria@amd.com>
Cc: Swapnil Sapkal <swapnil.sapkal@amd.com>
Cc: Thomas Falcon <thomas.falcon@intel.com>
Cc: Tianyou Li <tianyou.li@intel.com>
Cc: Yujie Liu <yujie.liu@intel.com>
Cc: tanze <tanze@kylinos.cn>
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
tools/perf/builtin-sched.c

index d984e58c7dbf494381f31330ff9d64dd15e1bbc8..9d73c704318294f5926a8e391a0af9825678e8a4 100644 (file)
@@ -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",