Skip to content

perf tools: Fix OOB reads, NULL deref, and resource leaks

Arnaldo Carvalho de Melo linux-perf-users lkml sashiko-reviews 2 versions In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest4 patches 2 repliesIn review
  2. v14 patches 2 repliesSuperseded

Changes in v2 from the cover letter

- Patch 4: fix off-by-one in non-compact path — max_cpu.cpu is a 0-based index, needs + 1 when used as cpus_nr (count). Reported by sashiko-bot.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4perf tools: Guard test_bit from out-of-bounds sample CPUtools/perf/builtin-annotate.c tools/perf/builtin-diff.c tools/perf/builtin-report.c +1 more+10 -51 reply
2/4perf sched: Fix thread reference leak in latency_switch_eventtools/perf/builtin-sched.c+1 -10 replies
3/4perf sched: Fix NULL dereference in latency_runtime_eventtools/perf/builtin-sched.c+3 -10 replies
4/4perf sched: Fix comp_cpus heap overflow with cross-machine recordingstools/perf/builtin-sched.c+2 -40 replies

Reviews trailers per patch

None of the 4 patches has a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchNamhyung Kim
SeriesA-b
1/4
2/4
3/4
4/4

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCHES v2 0/4] perf tools: Fix OOB reads, NULL deref, and resource leaks

Hi,

Four pre-existing bugs found by sashiko-bot during AI-assisted review of the perf-data-validation hardening series. All are independent of that series -- they are latent bugs in surrounding code exposed during review.

1. test_bit(sample->cpu, cpu_bitmap) reads out of bounds when PERF_SAMPLE_CPU is absent (sample->cpu == (u32)-1) in annotate, diff, report, and sched timehist.

2. Thread reference leak in perf sched latency_switch_event() -- one error path does 'return -1' instead of 'goto out_put'.

3. NULL pointer dereference in perf sched latency_runtime_event() -- thread_atoms_search() called before the NULL check on the thread returned by machine__findnew_thread().

4. Heap buffer overflow in perf sched map --compact mode -- comp_cpus allocated based on host CPU count but indexed using a MAX_CPUS-sized bitmap, overflowing when analyzing recordings from machines with more CPUs than the host. Also fixes the sizeof mismatch from the int-to-struct perf_cpu type change, removes the now-dead sysconf(_SC_NPROCESSORS_CONF) initialization, and fixes an off-by-one in the non-compact path where max_cpu (0-based index) was used as cpus_nr (count) without adding 1.

All four require crafted or unusual perf.data inputs to trigger. Verified with gcc and clang builds, checkpatch, and perf test.

Changes in v2:
- Patch 4: fix off-by-one in non-compact path — max_cpu.cpu is a 0-based index, needs + 1 when used as cpus_nr (count). Reported by sashiko-bot.

Arnaldo Carvalho de Melo (4):
perf tools: Guard test_bit from out-of-bounds sample CPU perf sched: Fix thread reference leak in latency_switch_event perf sched: Fix NULL dereference in latency_runtime_event perf sched: Fix comp_cpus heap overflow with cross-machine recordings

 tools/perf/builtin-annotate.c |  3 ++-
 tools/perf/builtin-diff.c     |  3 ++-
 tools/perf/builtin-report.c   |  3 ++-
 tools/perf/builtin-sched.c    | 18 ++++++++++--------
 4 files changed, 16 insertions(+), 11 deletions(-)

Developed with AI assistance (Claude/sashiko), tagged in commits.

Thanks a lot,

- Arnaldo