Skip to content

perf tools: Assorted fixes

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

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest11 patches 5 repliesIn review
  2. v411 patches 2 repliesSuperseded
  3. v311 patches 12 repliesSuperseded
Earlier versions · 2
VersionPostedPatchesRepliesStatus
v211 patches0 repliesSuperseded
v111 patches0 repliesSuperseded

Changes in v5 from the cover letter

- Patch 4 (bitmap_free): also reset nbits to 0 in mmap__munmap() — without this, a reused struct mmap retains the old positive nbits while bits is NULL, and bitmap_scnprintf() dereferences the NULL pointer when perf runs with -vv.
(Found by sashiko-bot lore review)
- Patch 10 (build_id__snprintf): null-terminate bf before the loop — when bf_size == 1, the offs + 1 < bf_size guard skips the loop entirely, leaving bf[0] uninitialized.
(Found by sashiko-bot lore review)
- Collected Reviewed-by tags from Ian Rogers' v3 reviews

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/11perf tools: Fix get_max_num() size_t underflow on empty sysfs filetools/perf/util/cpumap.c+6 -00 replies
2/11perf tools: Use scnprintf() in cpu_map__snprint() to prevent overflowtools/perf/util/cpumap.c+9 -90 replies
3/11perf tools: Use perf_env__get_cpu_topology() in machine__resolve()tools/perf/util/event.c+13 -21 reply
4/11perf tools: NULL bitmap pointers after bitmap_free()tools/perf/builtin-record.c tools/perf/util/mmap.c+3 -00 replies
5/11perf sched: Bounds-check prio before test_bit() in timehisttools/perf/builtin-sched.c+3 -11 reply
6/11perf sched: Fix idle-hist callchain display using wrong rb_first varianttools/perf/builtin-sched.c+2 -10 replies
7/11perf tools: Add O_CLOEXEC to open() calls in DSO and ELF codetools/perf/util/dso.c tools/perf/util/symbol-elf.c+12 -120 replies
8/11perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name()tools/perf/util/bpf-event.c+6 -50 replies
9/11perf hists: Fix snprintf() in hists__scnprintf_title() UID filter pathtools/perf/util/hist.c+4 -30 replies
10/11perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()tools/perf/util/build-id.c tools/perf/util/hwmon_pmu.c+11 -82 replies
11/11libperf: Document code simplification case for widening struct perf_cputools/lib/perf/TODO+8 -01 reply

Reviews trailers per patch

All 11 patches have a Reviewed-by or Acked-by.

PatchIan Rogers
1/11R-b
2/11R-b
3/11R-b
4/11R-b
5/11R-b
6/11A-b
7/11R-b
8/11R-b
9/11R-b
10/11R-b
11/11A-b

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

Read the v5 thread

Cover letter [PATCHES v5 00/11] perf tools: Assorted fixes

From: Arnaldo Carvalho de Melo <[email protected]>

Hi,

Sixth batch of pre-existing bug fixes found by sashiko-bot AI review during the perf-data-validation hardening series. All bugs are latent in existing code — none were introduced by the hardening patches.

Three broad categories:

1. snprintf() accumulation overflows (patches 2, 8, 9, 10):

Several functions accumulate formatted output via ret += snprintf(). snprintf() returns the would-have-been-written count, so on truncation ret overshoots the buffer size and the next 'size - ret' underflows to a huge unsigned value, disabling bounds checking. Switched to scnprintf() which returns actual bytes written.

Affected: cpu_map__snprint(), snprintf_hex(), synthesize_bpf_prog_name(), hists__scnprintf_title(), build_id__snprintf(), hwmon_pmu__describe_items().

2. Missing safety checks on untrusted data (patches 1, 3, 5, 6):

- get_max_num(): size_t underflow on empty sysfs file causes heap over-read.
- machine__resolve(): unguarded env->cpu[] access with untrusted CPU index — switched to perf_env__get_cpu_topology() accessor, added bounds check before int16_t truncation.
- timehist: test_bit(prio, ...) without bounds check on untrusted tracepoint priority.
- idle-hist: rb_first_cached() on a tree populated with plain rb_insert_color() — rb_leftmost never set, callchains silently dropped.

3. Resource hygiene (patches 4, 7):

- bitmap_free() without NULLing the pointer (2 call sites).
- O_CLOEXEC missing from open() calls in DSO and ELF code (12 call sites across 2 files).

Patch 11 expands the libperf ABI TODO with the code simplification argument for widening struct perf_cpu.cpu — the int16_t forces truncation checks at every boundary where wider CPU indices are narrowed.

Arnaldo Carvalho de Melo (11):
perf tools: Fix get_max_num() size_t underflow on empty sysfs file perf tools: Use scnprintf() in cpu_map__snprint() to prevent overflow perf tools: Use perf_env__get_cpu_topology() in machine__resolve() perf tools: NULL bitmap pointers after bitmap_free()
perf sched: Bounds-check prio before test_bit() in timehist perf sched: Fix idle-hist callchain display using wrong rb_first variant perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name() perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
libperf: Document code simplification case for widening struct perf_cpu

 tools/lib/perf/TODO          |  8 ++++++++
 tools/perf/builtin-record.c  |  1 +
 tools/perf/builtin-sched.c   |  7 +++++--
 tools/perf/util/bpf-event.c  | 11 ++++++-----
 tools/perf/util/build-id.c   |  7 +++++--
 tools/perf/util/cpumap.c     | 24 +++++++++++++++---------
 tools/perf/util/dso.c        |  4 ++--
 tools/perf/util/event.c      | 15 +++++++++++++--
 tools/perf/util/hist.c       |  7 ++++---
 tools/perf/util/hwmon_pmu.c  | 12 ++++++------
 tools/perf/util/mmap.c       |  2 ++
 tools/perf/util/symbol-elf.c | 20 ++++++++++----------
 12 files changed, 77 insertions(+), 41 deletions(-)

Changes since v4:
- Patch 4 (bitmap_free): also reset nbits to 0 in mmap__munmap() — without this, a reused struct mmap retains the old positive nbits while bits is NULL, and bitmap_scnprintf() dereferences the NULL pointer when perf runs with -vv.
(Found by sashiko-bot lore review)
- Patch 10 (build_id__snprintf): null-terminate bf before the loop — when bf_size == 1, the offs + 1 < bf_size guard skips the loop entirely, leaving bf[0] uninitialized.
(Found by sashiko-bot lore review)
- Collected Reviewed-by tags from Ian Rogers' v3 reviews

    (patches 1, 2, 4, 5, 7, 8, 9).

- Reordered Reviewed-by tags before Cc lines in commit trailers.

Changes since v3:
- Patch 3 (machine__resolve): expanded comment explaining why the outer al->cpu < nr_cpus_avail check is needed — the int16_t cast to struct perf_cpu silently truncates e.g. 65536 to 0, bypassing the accessor's internal bounds check.
(Ian Rogers review)
- Patch 10 (build_id__snprintf): fixed loop termination — after switching to scnprintf(), offs never reaches bf_size, so the loop spun doing zero-byte writes. Changed condition to offs + 1 < bf_size.
(Found by sashiko-bot, confirmed by Ian Rogers)
- Patch 11 (TODO wording): fixed "wrap to small positive numbers" to "wrap to negative numbers (two's complement)". (Found by sashiko-bot, confirmed by Ian Rogers)

Changes since v2:
- Dropped mbind patch (was v2 patch 4): the original code was correct — get_nodes() does --maxnode before computing BITS_TO_LONGS, so bitmap_zalloc(node_index + 1) with maxnode = node_index + 2 already match. The commit message misstated the kernel-side semantics.
- Split libperf ABI TODO hunk out of prio patch into standalone

    patch 11.

- Patch 3 (machine__resolve): bounds-check al->cpu against env->nr_cpus_avail before truncating to int16_t struct perf_cpu. (Found by sashiko-bot lore review)
- Patch 4 (was v2 patch 5, bitmap_free): reworded from "Three call sites" to "Two call sites" — removed dead store from memory_node__delete_nodes() where NULLing a pointer right before freeing the containing struct was useless. (Found by sashiko-bot lore review)

Changes since v1:
- Patch 5 (was v1 patch 6): fix prio bounds-check logic — the

    v1 condition (prio < 0 || prio >= MAX_PRIO || !test_bit(...))
    incorrectly skipped events with unknown priority (prio == -1).
    Changed to (prio >= 0 && (prio >= MAX_PRIO || !test_bit(...)))
    to preserve the original pass-through for events without
    priority info.
    (Found by sashiko-bot lore review)

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

Thanks,

- Arnaldo