perf tools: Assorted fixes
Arnaldo Carvalho de Melo
maintainerv4 cover perf tools: Assorted fixes
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 | 4 ++-- 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 | 1 + tools/perf/util/symbol-elf.c | 20 ++++++++++---------- 12 files changed, 73 insertions(+), 41 deletions(-)
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
Arnaldo Carvalho de Melo
maintainerPATCH v4 1/11 perf tools: Fix get_max_num() size_t underflow on empty sysfs file
From: Arnaldo Carvalho de Melo <[email protected]>
get_max_num() reads a sysfs file (cpu/possible, cpu/present, or node/possible) and scans backward from the end to find the last number. If the file is empty, filename__read_str() returns num == 0. The loop while (--num) decrements the size_t from 0 to SIZE_MAX, reading backward across the heap until a comma or hyphen is found or unmapped memory is hit.
Add an early return for empty files before the backward scan.
--- tools/perf/util/cpumap.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/perf/util/cpumap.c +6 -0
@@ -448,6 +448,12 @@ static int get_max_num(char *path, int *max) buf[num] ='\0' ; +/* empty file — nothing to parse */ +if (num ==0 ) { + err =-1 ; + goto out; + } +/* start on the right, to find highest node num */ while (--num) {if ((buf[num] ==',' ) || (buf[num] =='-' )) {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 01/11] perf tools: Fix get_max_num() size_t underflow on empty sysfs file
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Out-of-bounds write (buffer underflow) when reading empty files due to unvalidated length after string read operations. --
commit a32722a23e97db7d655860207d1ea4758fadcf7b
Author: Arnaldo Carvalho de Melo <[email protected]>
perf tools: Fix get_max_num() size_t underflow on empty sysfs file
This commit prevents a size_t underflow in get_max_num() by adding an early return when filename__read_str() yields an empty file with a length of 0. This protects the subsequent code from scanning backward out of bounds.
Quoted 5 lines
tools/perf/util/cpumap.c +0
-0
tools/perf/util/cpumap.c static int get_max_num(char *path, int *max) · line 456
Quoted 9 lines
tools/perf/util/cpumap.c +6
-0@@ -448,6 +448,12 @@ static int get_max_num(char *path, int *max) buf[num] ='\0' ; +/* empty file — nothing to parse */ +if (num ==0 ) { + err =-1 ; + goto out; + } +
[Severity: High]
This is a pre-existing issue, but since this patch successfully prevents an underflow with empty files, could the exact same bug exist in other places that read system files?
For instance, in tools/perf/util/pmu.c:pmu_id():
if (filename__read_str(path, &str, &len) < 0)
...
str[len - 1] = 0; /* remove line feed */If len is 0 due to an empty file, does len - 1 underflow to SIZE_MAX and cause a 1-byte out-of-bounds write that corrupts heap chunk metadata?
A similar pattern appears in tools/perf/util/thread.c:thread__set_comm_from_proc():
if (procfs__read_str(path, &comm, &sz) < 0)
...
comm[sz - 1] = '\0';Could this also result in the same buffer underwrite if the /proc/.../comm file happens to be empty?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Arnaldo Carvalho de Melo
maintainerPATCH v4 2/11 perf tools: Use scnprintf() in cpu_map__snprint() to prevent overflow
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
cpu_map__snprint() accumulates snprintf() return values in ret. snprintf() returns the number of characters that *would have been written* on truncation, not the actual count. When a fragmented CPU list exceeds the buffer, ret grows past size, causing size - ret to underflow (both are size_t), and subsequent snprintf() calls write past the end of the caller's stack buffer.
Switch to scnprintf() which returns the actual number of characters written, making ret accumulation safe by construction.
--- tools/perf/util/cpumap.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-)
tools/perf/util/cpumap.c +9 -9
@@ -692,21 +692,21 @@ size_t cpu_map__snprint(struct perf_cpu_map *map, char *buf, size_t size)if (start ==-1 ) { start = i;if (last) { - ret +=snprintf (buf + ret, size - ret, -"%s%d" ,COMMA , -perf_cpu_map__cpu (map, i).cpu); + ret +=scnprintf (buf + ret, size - ret, +"%s%d" ,COMMA , +perf_cpu_map__cpu (map, i).cpu); } }else if (((i - start) != (cpu.cpu -perf_cpu_map__cpu (map, start).cpu)) || last) {int end = i -1 ;if (start == end) { - ret +=snprintf (buf + ret, size - ret, -"%s%d" ,COMMA , -perf_cpu_map__cpu (map, start).cpu); + ret +=scnprintf (buf + ret, size - ret, +"%s%d" ,COMMA , +perf_cpu_map__cpu (map, start).cpu); }else { - ret +=snprintf (buf + ret, size - ret, -"%s%d-%d" ,COMMA , -perf_cpu_map__cpu (map, start).cpu,perf_cpu_map__cpu (map, end).cpu); + ret +=scnprintf (buf + ret, size - ret, +"%s%d-%d" ,COMMA , +perf_cpu_map__cpu (map, start).cpu,perf_cpu_map__cpu (map, end).cpu); } first = false; start = i;
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH v4 3/11 perf tools: Use perf_env__get_cpu_topology() in machine__resolve()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
machine__resolve() accesses env->cpu[al->cpu].socket_id after checking al->cpu >= 0 and env->cpu != NULL, but without validating al->cpu against env->nr_cpus_avail. Since al->cpu comes from the untrusted perf.data sample, a crafted file with a large CPU index causes an out-of-bounds heap read.
Use perf_env__get_cpu_topology() which validates both NULL and bounds. Also bounds-check al->cpu before the cast to struct perf_cpu (int16_t):
without this, values like 65536 silently truncate to 0, bypassing the accessor's internal check and returning CPU 0's topology.
--- tools/perf/util/event.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-)
tools/perf/util/event.c +13 -2
@@ -14,6 +14,7 @@#include <linux/perf_event.h> #include "cpumap.h" #include "dso.h" +#include "env.h" #include "event.h" #include "debug.h" #include "hist.h"
@@ -836,8 +837,18 @@ int machine__resolve(struct machine *machine, struct addr_location *al,if (al->cpu >=0 ) {struct perf_env *env = machine->env; -if (env && env->cpu) - al->socket = env->cpu[al->cpu].socket_id; +/* +* Bounds-check al->cpu (s32) before casting to struct perf_cpu +* (int16_t): without this, e.g. 65536 truncates to 0 and silently +* returns CPU 0's topology. Can go once perf_cpu.cpu is widened. +*/ +if (env && al->cpu < env->nr_cpus_avail) { +struct cpu_topology_map *topo; + + topo =perf_env__get_cpu_topology (env, (struct perf_cpu ){ al->cpu }); +if (topo) + al->socket = topo->socket_id; + } }/* Account for possible out-of-order switch events. */
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 03/11] perf tools: Use perf_env__get_cpu_topology() in machine__resolve()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Out-of-bounds read on the global symbol_conf.parallelism_filter bitmap due to unclamped al->parallelism. --
commit 02d9d799b673cb65dd2a323c5686a0235ee2e563
Author: Arnaldo Carvalho de Melo <[email protected]>
perf tools: Use perf_env__get_cpu_topology() in machine__resolve()
This commit fixes an out-of-bounds heap read in machine__resolve() by properly validating al->cpu against env->nr_cpus_avail before accessing the CPU topology. It also adds a bounds check before casting to struct perf_cpu to prevent silent truncation on large CPU values.
Quoted 3 lines
tools/perf/util/event.c +0
-0
[ ... ]
Quoted 21 lines
tools/perf/util/event.c +12
-2@@ -836,8 +837,18 @@ int machine__resolve(struct machine *machine, struct addr_location *al,if (al->cpu >=0 ) {struct perf_env *env = machine->env; -if (env && env->cpu) - al->socket = env->cpu[al->cpu].socket_id; +/* +* Bounds-check al->cpu (s32) before casting to struct perf_cpu +* (int16_t): without this, e.g. 65536 truncates to 0 and silently +* returns CPU 0's topology. Can go once perf_cpu.cpu is widened. +*/ +if (env && al->cpu < env->nr_cpus_avail) { +struct cpu_topology_map *topo; + + topo =perf_env__get_cpu_topology (env, (struct perf_cpu ){ al->cpu }); +if (topo) + al->socket = topo->socket_id; + } }/* Account for possible out-of-order switch events. */
tools/perf/util/event.c
Quoted 2 lines
tools/perf/util/event.c +0
-0al->parallelism =max (1 ,min (machine->parallelism,machine__nr_cpus_avail (machine)));if (test_bit (al->parallelism, symbol_conf.parallelism_filter))
[Severity: High]
This is a pre-existing issue, but does this test_bit() allow an out-of-bounds read on the global symbol_conf.parallelism_filter bitmap?
Since al->parallelism is computed using machine__nr_cpus_avail(machine), which depends on the HEADER_NRCPUS value in the perf.data file, a crafted file could specify an extremely large number of CPUs.
If numerous PERF_RECORD_SWITCH events artificially inflate machine->parallelism, the unclamped al->parallelism value could far exceed the bounds of symbol_conf.parallelism_filter, which is statically sized to MAX_NR_CPUS + 1 bits (typically 4097 bits).
Could reading so far beyond the mapped boundaries of the global symbol_conf structure cause a segmentation fault when parsing an untrusted file?
Quoted 1 line
al->filtered|= (1 <<HIST_FILTER__PARALLELISM);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Arnaldo Carvalho de Melo
maintainerPATCH v4 4/11 perf tools: NULL bitmap pointers after bitmap_free()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Two call sites free bitmaps without NULLing the pointer, risking double-free if the structure is reused or cleanup is called twice:
- mmap__munmap(): map->affinity_mask.bits
- record__mmap_cpu_mask_free(): mask->bits
Set each pointer to NULL after bitmap_free().
--- tools/perf/builtin-record.c | 1 + tools/perf/util/mmap.c | 1 + 2 files changed, 2 insertions(+)
tools/perf/builtin-record.c +1 -0
@@ -3084,6 +3084,7 @@ static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits)static void record__mmap_cpu_mask_free (struct mmap_cpu_mask *mask) {bitmap_free (mask->bits); + mask->bits =NULL ; mask->nbits =0 ; }
tools/perf/util/mmap.c +1 -0
@@ -238,6 +238,7 @@ static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)void mmap__munmap (struct mmap *map) {bitmap_free (map->affinity_mask.bits); + map->affinity_mask.bits =NULL ;zstd_fini (&map->zstd_data);
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 04/11] perf tools: NULL bitmap pointers after bitmap_free()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mmap__munmap() sets map->affinity_mask.bits to NULL but fails to reset nbits, causing a NULL…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mmap__munmap() sets map->affinity_mask.bits to NULL but fails to reset nbits, causing a NULL pointer dereference on reuse --
commit 94be33133bc6900d241e747a65660202c60936fa
Author: Arnaldo Carvalho de Melo <[email protected]>
perf tools: NULL bitmap pointers after bitmap_free()
Two call sites free bitmaps without NULLing the pointer, risking double-free if the structure is reused or cleanup is called twice.
Quoted 11 lines
tools/perf/builtin-record.c +1
-0@@ -3084,6 +3084,7 @@ static int record__mmap_cpu_mask_alloc(struct mmap_cpu_mask *mask, int nr_bits)static void record__mmap_cpu_mask_free (struct mmap_cpu_mask *mask) {bitmap_free (mask->bits); + mask->bits =NULL ; mask->nbits =0 ; }
[ ... ]
tools/perf/util/mmap.c static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused) · line 241
Quoted 9 lines
tools/perf/util/mmap.c +1
-0@@ -238,6 +238,7 @@ static void perf_mmap__aio_munmap(struct mmap *map __maybe_unused)void mmap__munmap (struct mmap *map) {bitmap_free (map->affinity_mask.bits); + map->affinity_mask.bits =NULL ;
[Severity: Medium]
In record__mmap_cpu_mask_free() above, nbits is reset to 0, but it is not reset here.
If this struct mmap is later reused and mmap__mmap() is called with mp->affinity == PERF_AFFINITY_SYS, perf_mmap__setup_affinity_mask() is bypassed, leaving bits as NULL while nbits retains its old, positive value.
If the user runs perf with -vv, mmap__mmap() calls mmap_cpu_mask__scnprintf(), which passes the NULL pointer and positive nbits to bitmap_scnprintf().
Could this cause find_first_bit() to unconditionally dereference the NULL pointer array and crash the tool?
Should we also add map->affinity_mask.nbits = 0 here?
Quoted 2 lines
tools/perf/util/mmap.c +0
-0zstd_fini (&map->zstd_data);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Arnaldo Carvalho de Melo
maintainerPATCH v4 5/11 perf sched: Bounds-check prio before test_bit() in timehist
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
timehist_skip_sample() reads prio from untrusted tracepoint data via perf_sample__intval(sample, "prev_prio") without bounds validation. A crafted perf.data with prev_prio >= MAX_PRIO (140) causes test_bit() to read past the end of the prio_bitmap, which is only MAX_PRIO bits.
Add a prio >= 0 && prio < MAX_PRIO check before the test_bit() call. This also makes the != -1 sentinel check explicit as >= 0.
--- tools/perf/builtin-sched.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/builtin-sched.c +3 -1
@@ -2645,7 +2645,9 @@ static bool timehist_skip_sample(struct perf_sched *sched,else if (evsel__name_is (sample->evsel,"sched:sched_switch" )) prio =perf_sample__intval (sample,"prev_prio" ); -if (prio !=-1 && !test_bit (prio, sched->prio_bitmap)) { +/* prio comes from untrusted tracepoint data — bounds-check before test_bit */ +if (prio >=0 && + (prio >=MAX_PRIO || !test_bit (prio, sched->prio_bitmap))) { rc = true; sched->skipped_samples++; }
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH v4 6/11 perf sched: Fix idle-hist callchain display using wrong rb_first variant
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
timehist_print_idlehist_callchain() calls rb_first_cached() on sorted_root, but the sort function (callchain_param.sort) populates it via rb_insert_color() on the plain rb_root member — not the cached variant. This means rb_leftmost is never set, so rb_first_cached() always returns NULL and the entire callchain summary is silently dropped from --idle-hist output.
The original code in ba957ebb54893aca ("perf sched timehist: Show callchains for idle stat") was correct — it used struct rb_root and rb_first(). The bug was introduced when sorted_root was converted to rb_root_cached without converting the sort insertion path to use rb_insert_color_cached().
Use rb_first(&root->rb_root) to match how the tree was populated.
--- tools/perf/builtin-sched.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/builtin-sched.c +2 -1
@@ -3130,7 +3130,8 @@ static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)size_t ret =0 ;FILE *fp = stdout;struct callchain_node *chain; -struct rb_node *rb_node =rb_first_cached (root); +/* sort() uses rb_insert_color() on rb_root, not rb_root_cached */ +struct rb_node *rb_node =rb_first (&root->rb_root);printf (" %16s %8s %s\n" ,"Idle time (msec)" ,"Count" ,"Callchains" ); printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 06/11] perf sched: Fix idle-hist callchain display using wrong rb_first variant
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] In perf sched map, using the --color-pids option causes a segmentation fault and an invalid free due to type confusion on thread__priv(). --
commit 52b25a8ed38e1c2d458f164827bbcfd55e08f69b
Author: Arnaldo Carvalho de Melo <[email protected]>
perf sched: Fix idle-hist callchain display using wrong rb_first variant
This commit fixes a bug in the idle-hist callchain display where rb_first_cached() was used on a tree populated with rb_insert_color(). The tree lacks the leftmost cache, so rb_first_cached() returned NULL and dropped the callchain summary. The fix restores the original behavior by using rb_first() directly on the rb_root.
tools/perf/builtin-sched.c static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root) · line 3134
Quoted 11 lines
tools/perf/builtin-sched.c +2
-1@@ -3130,7 +3130,8 @@ static size_t timehist_print_idlehist_callchain(struct rb_root_cached *root)size_t ret =0 ;FILE *fp = stdout;struct callchain_node *chain; -struct rb_node *rb_node =rb_first_cached (root); +/* sort() uses rb_insert_color() on rb_root, not rb_root_cached */ +struct rb_node *rb_node =rb_first (&root->rb_root);
[Severity: High]
This isn't a bug introduced by this patch, but does the --color-pids option in perf sched map cause a segmentation fault and invalid free due to type confusion on thread__priv()?
When --color-pids is used, map__findnew_thread() sets thread__priv(thread) to the scalar value (void*)1 to mark the thread for coloring:
tools/perf/builtin-sched.c:map__findnew_thread() {
...
if (thread_map__has(sched->map.color_pids, tid))
color = true;
thread__set_priv(thread, color ? ((void*)1) : NULL);
...
}However, the rest of the perf sched code expects thread__priv() to hold a valid pointer to a struct thread_runtime. When map_switch_event() calls thread__get_runtime(), it retrieves the (void*)1 pointer and skips initialization because it is not NULL:
tools/perf/builtin-sched.c:thread__get_runtime() {
...
tr = thread__priv(thread);
if (tr == NULL) {
tr = thread__init_runtime(thread);
...
}Then map_switch_event() attempts to read tr->shortname[0], which dereferences the invalid address and causes an immediate segmentation fault:
tools/perf/builtin-sched.c:map_switch_event() {
...
tr = thread__get_runtime(sched_in);
if (tr == NULL)
goto out;
...
if (!tr->shortname[0]) {
...
}Furthermore, because cmd_sched() registers free() as the destructor for thread priv data:
tools/perf/builtin-sched.c:cmd_sched() {
...
thread__set_priv_destructor(free);
...
}Tearing down the session will attempt to call free((void*)1), corrupting the heap.
Could struct thread_runtime be expanded to include a coloring flag, or could the color state be tracked outside the thread__priv pointer to prevent this type confusion?
Quoted 3 lines
tools/perf/builtin-sched.c +0
-0printf (" %16s %8s %s\n" ,"Idle time (msec)" ,"Count" ,"Callchains" ); printf(" %.16s %.8s %.50s\n", graph_dotted_line, graph_dotted_line,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Arnaldo Carvalho de Melo
maintainerPATCH v4 7/11 perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
open() calls in dso.c and symbol-elf.c omit O_CLOEXEC, which leaks file descriptors to child processes spawned during symbol resolution (e.g., addr2line, objdump). This can exhaust the fd limit during long profiling sessions or when processing many DSOs.
Add O_CLOEXEC to all open() calls in both files (12 call sites).
--- tools/perf/util/dso.c | 4 ++-- tools/perf/util/symbol-elf.c | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-)
tools/perf/util/dso.c +2 -2
@@ -344,7 +344,7 @@ int filename__decompress(const char *name, char *pathname,* descriptor to the uncompressed file. */ if (!compressions[comp].is_compressed (name)) -return open (name,O_RDONLY ); +return open (name,O_RDONLY |O_CLOEXEC ); fd =mkstemp (tmpbuf);if (fd <0 ) {
@@ -1911,7 +1911,7 @@ static const u8 *__dso__read_symbol(struct dso *dso, const char *symfs_filename,int saved_errno;nsinfo__mountns_enter (dso__nsinfo (dso), &nsc); - fd =open (symfs_filename,O_RDONLY ); + fd =open (symfs_filename,O_RDONLY |O_CLOEXEC ); saved_errno = errno;nsinfo__mountns_exit (&nsc);if (fd <0 ) {
tools/perf/util/symbol-elf.c +10 -10
@@ -217,7 +217,7 @@ bool filename__has_section(const char *filename, const char *sec)GElf_Shdr shdr;bool found = false; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 )return false;
@@ -872,7 +872,7 @@ static int read_build_id(const char *filename, struct build_id *bid)if (size <BUILD_ID_SIZE ) goto out; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 ) goto out;
@@ -935,7 +935,7 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid)size_t size =sizeof (bid->data);int fd, err =-1 ; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 ) goto out;
@@ -995,7 +995,7 @@ int filename__read_debuglink(const char *filename, char *debuglink,if (err >=0 ) goto out; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 ) goto out;
@@ -1153,7 +1153,7 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name, type =dso__symtab_type (dso); }else { - fd =open (name,O_RDONLY ); + fd =open (name,O_RDONLY |O_CLOEXEC );if (fd <0 ) { *dso__load_errno (dso) = errno;return -1 ;
@@ -1952,7 +1952,7 @@ static int kcore__open(struct kcore *kcore, const char *filename) {GElf_Ehdr *ehdr; - kcore->fd =open (filename,O_RDONLY ); + kcore->fd =open (filename,O_RDONLY |O_CLOEXEC );if (kcore->fd ==-1 )return -1 ;
@@ -1985,7 +1985,7 @@ static int kcore__init(struct kcore *kcore, char *filename, int elfclass,if (temp) kcore->fd =mkstemp (filename);else - kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL ,0400 ); + kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL |O_CLOEXEC ,0400 );if (kcore->fd ==-1 )return -1 ;
@@ -2461,11 +2461,11 @@ static int kcore_copy__compare_files(const char *from_filename, {int from, to, err =-1 ; - from =open (from_filename,O_RDONLY ); + from =open (from_filename,O_RDONLY |O_CLOEXEC );if (from <0 )return -1 ; - to =open (to_filename,O_RDONLY ); + to =open (to_filename,O_RDONLY |O_CLOEXEC );if (to <0 ) goto out_close_from;
@@ -2883,7 +2883,7 @@ int get_sdt_note_list(struct list_head *head, const char *target)Elf *elf;int fd, ret; - fd =open (target,O_RDONLY ); + fd =open (target,O_RDONLY |O_CLOEXEC );if (fd <0 )return -EBADF ;
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 07/11] perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Incomplete fix for file descriptor leaks in DSO and ELF code.
Pre-existing issues:
- [Critical] Stack buffer overflow in sysfs__read_build_id when skipping non-matching GNU notes. --
commit 66274b4049c9a2092defa7caf237fe0295853d9c
Author: Arnaldo Carvalho de Melo <[email protected]>
perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code
The commit adds O_CLOEXEC to open() calls in dso.c and symbol-elf.c to prevent leaking file descriptors to child processes during symbol resolution.
tools/perf/util/dso.c int filename__decompress(const char *name, char *pathname, · line 349
Quoted 12 lines
tools/perf/util/dso.c +1
-1@@ -344,7 +344,7 @@ int filename__decompress(const char *name, char *pathname,* descriptor to the uncompressed file. */ if (!compressions[comp].is_compressed (name)) -return open (name,O_RDONLY ); +return open (name,O_RDONLY |O_CLOEXEC ); fd =mkstemp (tmpbuf);
[Severity: High]
Since the goal of this patch is to prevent leaking file descriptors to child processes, should we also address the mkstemp() calls in these files?
mkstemp() creates file descriptors without O_CLOEXEC. For instance, here in filename__decompress(), the temporary file descriptor will still be leaked. Should mkostemp(tmpbuf, O_CLOEXEC) be used instead?
[ ... ]
Quoted 4 lines
tools/perf/util/symbol-elf.c +0
-0
[ ... ]
tools/perf/util/symbol-elf.c int sysfs__read_build_id(const char *filename, struct build_id *bid) · line 940
Quoted 8 lines
tools/perf/util/symbol-elf.c +1
-1@@ -935,7 +935,7 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid)size_t size =sizeof (bid->data);int fd, err =-1 ; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 ) goto out;
[Severity: Critical]
This is a pre-existing issue, but while reviewing this function, could an unbounded read() happen later in sysfs__read_build_id() when skipping non-matching GNU notes?
If a crafted sysfs note has a maliciously large nhdr.n_descsz, the code does:
} else if (read(fd, bf, descsz) != (ssize_t)descsz)
Since bf is a fixed-size stack array (char bf[BUFSIZ]), could this read past the end of bf and overwrite the stack frame if descsz exceeds BUFSIZ?
[ ... ]
tools/perf/util/symbol-elf.c static int kcore__init(struct kcore *kcore, char *filename, int elfclass, · line 1990
Quoted 8 lines
tools/perf/util/symbol-elf.c +1
-1@@ -1985,7 +1985,7 @@ static int kcore__init(struct kcore *kcore, char *filename, int elfclass,if (temp) kcore->fd =mkstemp (filename);else - kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL ,0400 ); + kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL |O_CLOEXEC ,0400 );if (kcore->fd ==-1 )return -1 ;
[Severity: High]
Similarly to the issue in filename__decompress(), if temp is true here, mkstemp() is called without O_CLOEXEC.
There is also a similar mkstemp() call in read_gnu_debugdata() in this file that extracts debug data without O_CLOEXEC. Should all of these be updated to prevent leaking file descriptors?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 07/11] perf tools: Add O_CLOEXEC to open() calls in DSO and ELF code
Quoted 35 lines · On Tue, Jun 09, 2026 at 01:16:26AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
New issues:
- [High] Incomplete fix for file descriptor leaks in DSO and ELF code.Pre-existing issues:
- [Critical] Stack buffer overflow insysfs__read_build_idwhen skipping non-matching GNU notes. --commit
66274b4049c9a2092defa7caf237fe0295853d9c
Author: Arnaldo Carvalho de Melo <[email protected]>perf tools: Add
O_CLOEXECtoopen()calls in DSO and ELF codeThe commit adds
O_CLOEXECtoopen()calls indso.candsymbol-elf.cto prevent leaking file descriptors to child processes during symbol resolution.tools/perf/util/dso.c int filename__decompress(const char *name, char *pathname, · line 349
Quoted 12 lines
tools/perf/util/dso.c +1
-1@@ -344,7 +344,7 @@ int filename__decompress(const char *name, char *pathname,* descriptor to the uncompressed file. */ if (!compressions[comp].is_compressed (name)) -return open (name,O_RDONLY ); +return open (name,O_RDONLY |O_CLOEXEC ); fd =mkstemp (tmpbuf);[Severity: High]
Since the goal of this patch is to prevent leaking file descriptors to child processes, should we also address themkstemp()calls in these files?
Quoted 3 lines
mkstemp()creates file descriptors withoutO_CLOEXEC. For instance, here infilename__decompress(), the temporary file descriptor will still be leaked. Shouldmkostemp(tmpbuf, O_CLOEXEC)be used instead?
This is fixed in the next patch series.
Quoted 20 lines
[ ... ]
Quoted 4 lines
tools/perf/util/symbol-elf.c +0
-0[ ... ]
tools/perf/util/symbol-elf.c int sysfs__read_build_id(const char *filename, struct build_id *bid) · line 940
Quoted 8 lines
tools/perf/util/symbol-elf.c +1
-1@@ -935,7 +935,7 @@ int sysfs__read_build_id(const char *filename, struct build_id *bid)size_t size =sizeof (bid->data);int fd, err =-1 ; - fd =open (filename,O_RDONLY ); + fd =open (filename,O_RDONLY |O_CLOEXEC );if (fd <0 ) goto out;[Severity: Critical]
This is a pre-existing issue, but while reviewing this function, could an unboundedread()happen later insysfs__read_build_id()when skipping non-matching GNU notes?
Quoted 6 lines
If a crafted sysfs note has a maliciously large nhdr.n_descsz, the code does:
} else if (read(fd, bf, descsz) != (ssize_t)descsz)Since bf is a fixed-size stack array (char bf[BUFSIZ]), could this read past the end of bf and overwrite the stack frame if descsz exceeds BUFSIZ?
Ditto, as several other issues you reported in this series and in other series.
- Arnaldo
Quoted 20 lines
[ ... ]
tools/perf/util/symbol-elf.c static int kcore__init(struct kcore *kcore, char *filename, int elfclass, · line 1990
Quoted 8 lines
tools/perf/util/symbol-elf.c +1
-1@@ -1985,7 +1985,7 @@ static int kcore__init(struct kcore *kcore, char *filename, int elfclass,if (temp) kcore->fd =mkstemp (filename);else - kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL ,0400 ); + kcore->fd =open (filename,O_WRONLY |O_CREAT |O_EXCL |O_CLOEXEC ,0400 );if (kcore->fd ==-1 )return -1 ;[Severity: High]
Similarly to the issue infilename__decompress(), if temp is true here,mkstemp()is called withoutO_CLOEXEC.There is also a similar
mkstemp()call inread_gnu_debugdata()in this file that extracts debug data withoutO_CLOEXEC. Should all of these be updated to prevent leaking file descriptors?Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Arnaldo Carvalho de Melo
maintainerPATCH v4 8/11 perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Both functions accumulate formatted output via ret += snprintf(buf + ret, size - ret, ...). If the buffer is too small and snprintf() returns more than the remaining space, ret exceeds size and the next 'size - ret' underflows, causing snprintf() to write past the buffer end.
Switch to scnprintf() which returns the actual number of bytes written, making the accumulation safe.
--- tools/perf/util/bpf-event.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-)
tools/perf/util/bpf-event.c +6 -5
@@ -36,7 +36,7 @@ static int snprintf_hex(char *buf, size_t size, unsigned char *data, size_t len)size_t i;for (i =0 ; i < len; i++) - ret +=snprintf (buf + ret, size - ret,"%02x" , data[i]); + ret +=scnprintf (buf + ret, size - ret,"%02x" , data[i]);return ret; }
@@ -140,7 +140,7 @@ static int synthesize_bpf_prog_name(char *buf, int size,const struct btf_type *t;int name_len; - name_len =snprintf (buf, size,"bpf_prog_" ); + name_len =scnprintf (buf, size,"bpf_prog_" ); name_len +=snprintf_hex (buf + name_len, size - name_len, prog_tags[sub_id],BPF_TAG_SIZE );if (btf) {
@@ -153,9 +153,10 @@ static int synthesize_bpf_prog_name(char *buf, int size, short_name = info->name; }else short_name ="F" ; -if (short_name) - name_len +=snprintf (buf + name_len, size - name_len, -"_%s" , short_name); +if (short_name) { + name_len +=scnprintf (buf + name_len, size - name_len, +"_%s" , short_name); + }return name_len; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 08/11] perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Accumulation of snprintf return values combined with fixed increments causes buffer size integer underflows, leading to a stack buffer overflow in perf_pmu__for_each_event. --
commit 9c31711b5b63e0f7c6e422153e389b761e114baf
Author: Arnaldo Carvalho de Melo <[email protected]>
perf bpf: Use scnprintf() in snprintf_hex() and synthesize_bpf_prog_name()
This patch fixes a potential buffer overflow by switching from snprintf() to scnprintf() during formatted output accumulation. This prevents the remaining buffer size from underflowing if the output is truncated.
Quoted 4 lines
tools/perf/util/bpf-event.c +0
-0
[ ... ]
tools/perf/util/bpf-event.c static int synthesize_bpf_prog_name(char *buf, int size, · line 161
Quoted 13 lines
tools/perf/util/bpf-event.c +4
-3@@ -153,9 +153,10 @@ static int synthesize_bpf_prog_name(char *buf, int size, short_name = info->name; }else short_name ="F" ; -if (short_name) - name_len +=snprintf (buf + name_len, size - name_len, -"_%s" , short_name); +if (short_name) { + name_len +=scnprintf (buf + name_len, size - name_len, +"_%s" , short_name); + }return name_len; }
While we are fixing snprintf() accumulations here, there is a similar pre-existing issue in tools/perf/util/pmu.c:
tools/perf/util/pmu.c:perf_pmu__for_each_event() {
...
info.scale_unit = NULL;
if (strlen(event->unit) || event->scale != 1.0) {
info.scale_unit = buf + buf_used;
buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
"%G%s", event->scale, event->unit) + 1;
}
info.desc = event->desc;
info.long_desc = event->long_desc;
info.encoding_desc = buf + buf_used;
buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
"%.*s/%s/", (int)pmu_name_len, info.pmu_name, event->terms) + 1;...
}
[Severity: High]
This is a pre-existing issue, but the same pattern is present in perf_pmu__for_each_event().
Because snprintf() returns the length it would have written rather than the amount actually written, and we unconditionally add 1 to it, buf_used can exceed sizeof(buf).
When buf_used exceeds sizeof(buf), sizeof(buf) - buf_used will underflow to a large positive size.
Could the subsequent snprintf() call use this massive size limit and write to the out-of-bounds pointer buf + buf_used, overflowing buf[]?
Other similar bugs in hwmon_pmu.c and hist.c appear to be fixed in subsequent patches of this series, but pmu.c seems to remain vulnerable at the end of the series.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Arnaldo Carvalho de Melo
maintainerPATCH v4 9/11 perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
hists__scnprintf_title() accumulates formatted output into a buffer using scnprintf() for all filter clauses except the UID filter, which uses snprintf(). If the buffer fills up and snprintf() returns more than the remaining space, printed exceeds size and the next 'size - printed' underflows, causing later scnprintf() calls to write past the buffer.
Switch the UID filter clause to scnprintf() to match the rest of the function.
--- tools/perf/util/hist.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/hist.c +4 -3
@@ -2963,9 +2963,10 @@ int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool sh ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); -if (hists->uid_filter_str) - printed +=snprintf (bf + printed, size - printed, -", UID: %s" , hists->uid_filter_str); +if (hists->uid_filter_str) { + printed +=scnprintf (bf + printed, size - printed, +", UID: %s" , hists->uid_filter_str); + }if (thread) {if (hists__has (hists, thread)) { printed += scnprintf(bf + printed, size - printed,
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 09/11] perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Stack buffer overflow in perf_pmu__for_each_event due to unchecked snprintf return values underflowing the remaining buffer size. --
commit 4dd3b213a3499e8521320ddfbcd3cb731ac444d2
Author: Arnaldo Carvalho de Melo <[email protected]>
perf hists: Fix snprintf() in hists__scnprintf_title() UID filter path
hists__scnprintf_title() accumulates formatted output into a buffer using scnprintf() for all filter clauses except the UID filter, which uses snprintf(). If the buffer fills up and snprintf() returns more than the remaining space, printed exceeds size and the next 'size - printed' underflows, causing later scnprintf() calls to write past the buffer. Switch the UID filter clause to scnprintf() to match the rest of the function.
Quoted 5 lines
tools/perf/util/hist.c +0
-0
tools/perf/util/hist.c int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool sh · line 2969
Quoted 10 lines
tools/perf/util/hist.c +4
-3@@ -2963,9 +2963,10 @@ int __hists__scnprintf_title(struct hists *hists, char *bf, size_t size, bool sh ev_name, sample_freq_str, enable_ref ? ref : " ", nr_events); -if (hists->uid_filter_str) - printed +=snprintf (bf + printed, size - printed, -", UID: %s" , hists->uid_filter_str); +if (hists->uid_filter_str) { + printed +=scnprintf (bf + printed, size - printed, +", UID: %s" , hists->uid_filter_str); + }
[Severity: High]
This isn't a bug introduced by this patch, but while you are fixing this snprintf() underflow pattern, the exact same issue exists in tools/perf/util/pmu.c:perf_pmu__for_each_event() where it can cause a stack buffer overflow.
In that function, sequential snprintf() calls format event strings into a 1024-byte stack buffer, accumulating returned lengths into buf_used:
tools/perf/util/pmu.c:perf_pmu__for_each_event() {
...
if (strlen(event->unit) || event->scale != 1.0) {
info.scale_unit = buf + buf_used;
buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
"%G%s", event->scale, event->unit) + 1;
}
info.desc = event->desc;
info.long_desc = event->long_desc;
info.encoding_desc = buf + buf_used;
buf_used += snprintf(buf + buf_used, sizeof(buf) - buf_used,
"%.*s/%s/", (int)pmu_name_len, info.pmu_name, event->terms) + 1;...
}
Because snprintf() returns the length it would have written, if a long event alias or term string (e.g., from a parsed JSON definition) fills the buffer, buf_used will exceed sizeof(buf). On the next snprintf() call, the limit calculation sizeof(buf) - buf_used undergoes unsigned underflow to a massive positive size_t value.
Could this unsigned underflow disable bounds checking entirely and allow snprintf() to write string data out of bounds on the stack? Should we switch these calls to scnprintf() as well?
Quoted 3 lines
tools/perf/util/hist.c +0
-0if (thread) {if (hists__has (hists, thread)) { printed += scnprintf(bf + printed, size - printed,
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Arnaldo Carvalho de Melo
maintainerPATCH v4 10/11 perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
build_id__snprintf() and hwmon_pmu__read_events() accumulate formatted output via snprintf(), which returns the would-have-been-written count on truncation. In build_id__snprintf(), this inflates the return value beyond the buffer size. In hwmon_pmu__read_events(), len overshoots out_buf_len and the next 'out_buf_len - len' underflows.
Switch both to scnprintf() which returns actual bytes written.
In build_id__snprintf(), also tighten the loop guard from 'offs < bf_size' to 'offs + 1 < bf_size': since scnprintf() returns at most size-1, offs never reaches bf_size, and the original condition would spin doing zero-byte writes once the buffer fills.
--- tools/perf/util/build-id.c | 4 ++-- tools/perf/util/hwmon_pmu.c | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-)
tools/perf/util/build-id.c +2 -2
@@ -93,8 +93,8 @@ int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_sizereturn 0 ; } -for (size_t i =0 ; i < build_id->size && offs < bf_size; ++i) - offs +=snprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]); +for (size_t i =0 ; i < build_id->size && offs +1 < bf_size; ++i) + offs +=scnprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]);return offs; }
tools/perf/util/hwmon_pmu.c +6 -6
@@ -442,12 +442,12 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si buf[read_len] ='\0' ; val =strtoll (buf,/*endptr=*/ NULL ,10 ); - len +=snprintf (out_buf + len, out_buf_len - len,"%s%s%s=%g%s" , - len ==0 ?" " :", " , - hwmon_item_strs[bit], - is_alarm ?"_alarm" :"" , - (double )val /1000.0 , - hwmon_units[key.type]); + len +=scnprintf (out_buf + len, out_buf_len - len,"%s%s%s=%g%s" , + len ==0 ?" " :", " , + hwmon_item_strs[bit], + is_alarm ?"_alarm" :"" , + (double )val /1000.0 , + hwmon_units[key.type]); }close (fd); }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 10/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] build_id__snprintf() fails to null-terminate the output buffer when bf_size is 1. - [Low]…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] build_id__snprintf() fails to null-terminate the output buffer when bf_size is 1.
- [Low] hwmon_pmu__describe_items() spins doing useless sysfs operations once the output buffer fills. --
commit 0f670823c7b8e81b469db29dc89cf4b4de7e4cad
Author: Arnaldo Carvalho de Melo <[email protected]>
perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
This commit switches build_id__snprintf() and hwmon_pmu__describe_items() to use scnprintf() instead of snprintf() to prevent out-of-bounds length tracking. It also modifies the loop guard in build_id__snprintf() to prevent useless zero-byte writes once the buffer is full.
tools/perf/util/build-id.c int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_size · line 97
Quoted 12 lines
tools/perf/util/build-id.c +2
-2@@ -93,8 +93,8 @@ int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_sizereturn 0 ; } -for (size_t i =0 ; i < build_id->size && offs < bf_size; ++i) - offs +=snprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]); +for (size_t i =0 ; i < build_id->size && offs +1 < bf_size; ++i) + offs +=scnprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]);
[Severity: Medium]
If bf_size is 1, does this new loop condition skip execution entirely?
If build_id->size > 0 and bf_size == 1, the offs + 1 < bf_size condition is false on the first iteration. Because scnprintf is never called, would bf[0] be left un-null-terminated?
Quoted 2 lines
tools/perf/util/build-id.c +0
-0return offs; }
[ ... ]
Quoted 4 lines
tools/perf/util/hwmon_pmu.c +0
-0
[ ... ]
Quoted 9 lines
tools/perf/util/hwmon_pmu.c +0
-5@@ -442,12 +442,12 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si buf[read_len] ='\0' ; val =strtoll (buf,/*endptr=*/ NULL ,10 ); - len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s", - len == 0 ? " " : ", ", - hwmon_item_strs[bit], - is_alarm ? "_alarm" : "", - (double)val / 1000.0,
tools/perf/util/hwmon_pmu.c static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si · line 453
Quoted 10 lines
tools/perf/util/hwmon_pmu.c +6
-1- hwmon_units[key.type]); + len +=scnprintf (out_buf + len, out_buf_len - len,"%s%s%s=%g%s" , + len ==0 ?" " :", " , + hwmon_item_strs[bit], + is_alarm ?"_alarm" :"" , + (double )val /1000.0 , + hwmon_units[key.type]); }close (fd); }
[Severity: Low]
Does this loop also need an early break condition once the buffer fills?
The commit message notes that build_id__snprintf() was updated to avoid spinning doing zero-byte writes once the buffer fills.
In hwmon_pmu__describe_items(), once len reaches out_buf_len - 1, scnprintf will return 0. Does this mean the loop continues for all remaining hardware monitor items, performing unnecessary sysfs file open, read, and close operations only to do a 0-byte scnprintf write?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Arnaldo Carvalho de Melo
maintainerRe: [PATCH 10/11] perf tools: Use scnprintf() in build_id__snprintf() and hwmon read_events()
Quoted 31 lines · On Tue, Jun 09, 2026 at 01:17:17AM +0000, [email protected] wrote:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium]build_id__snprintf()fails to null-terminate the output buffer whenbf_sizeis 1.
- [Low]hwmon_pmu__describe_items()spins doing useless sysfs operations once the output buffer fills. --commit
0f670823c7b8e81b469db29dc89cf4b4de7e4cad
Author: Arnaldo Carvalho de Melo <[email protected]>perf tools: Use
scnprintf()inbuild_id__snprintf()and hwmonread_events()This commit switches
build_id__snprintf()andhwmon_pmu__describe_items()to usescnprintf()instead ofsnprintf()to prevent out-of-bounds length tracking. It also modifies the loop guard inbuild_id__snprintf()to prevent useless zero-byte writes once the buffer is full.tools/perf/util/build-id.c int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_size · line 97
Quoted 12 lines
tools/perf/util/build-id.c +2
-2@@ -93,8 +93,8 @@ int build_id__snprintf(const struct build_id *build_id, char *bf, size_t bf_sizereturn 0 ; } -for (size_t i =0 ; i < build_id->size && offs < bf_size; ++i) - offs +=snprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]); +for (size_t i =0 ; i < build_id->size && offs +1 < bf_size; ++i) + offs +=scnprintf (bf + offs, bf_size - offs,"%02x" , build_id->data[i]);[Severity: Medium]
Ifbf_sizeis 1, does this new loop condition skip execution entirely?
Quoted 3 lines
If
build_id->size> 0 andbf_size== 1, the offs + 1 <bf_sizecondition is false on the first iteration. Because scnprintf is never called, would bf[0] be left un-null-terminated?
Addressing this one in v5.
Quoted 26 lines
Quoted 2 lines
tools/perf/util/build-id.c +1
-0+++ b/tools/perf/util/hwmon_pmu.c[ ... ]
Quoted 9 lines
tools/perf/util/build-id.c +0
-5@@ -442,12 +442,12 @@ static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si buf[read_len] ='\0' ; val =strtoll (buf,/*endptr=*/ NULL ,10 ); - len += snprintf(out_buf + len, out_buf_len - len, "%s%s%s=%g%s", - len == 0 ? " " : ", ", - hwmon_item_strs[bit], - is_alarm ? "_alarm" : "", - (double)val / 1000.0,tools/perf/util/build-id.c static size_t hwmon_pmu__describe_items(struct hwmon_pmu *hwm, char *out_buf, si · line 453
Quoted 10 lines
tools/perf/util/build-id.c +6
-1- hwmon_units[key.type]); + len +=scnprintf (out_buf + len, out_buf_len - len,"%s%s%s=%g%s" , + len ==0 ?" " :", " , + hwmon_item_strs[bit], + is_alarm ?"_alarm" :"" , + (double )val /1000.0 , + hwmon_units[key.type]); }close (fd); }[Severity: Low]
Does this loop also need an early break condition once the buffer fills?
Quoted 7 lines
The commit message notes that
build_id__snprintf()was updated to avoid spinning doing zero-byte writes once the buffer fills.In
hwmon_pmu__describe_items(), once len reachesout_buf_len- 1, scnprintf will return 0. Does this mean the loop continues for all remaining hardware monitor items, performing unnecessary sysfs file open, read, and close operations only to do a 0-byte scnprintf write?
I've added this to a cleanups TODO file for future work, nice observation, the same pattern may happen in other places, worth a series.
- Arnaldo
Arnaldo Carvalho de Melo
maintainerPATCH v4 11/11 libperf: Document code simplification case for widening struct perf_cpu
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Add a bullet point to the libperf ABI TODO explaining the code simplification benefit of widening struct perf_cpu.cpu from int16_t to int: the narrow type forces defensive truncation checks at every boundary where wider CPU indices are narrowed, and values > 32767 silently wrap to negative numbers (two's complement), bypassing bounds validation without them.
--- tools/lib/perf/TODO | 8 ++++++++ 1 file changed, 8 insertions(+)
tools/lib/perf/TODO +8 -0
@@ -11,6 +11,14 @@ together. (x86_64 max is 8192, arm64 is 4096), but NR_CPUS limits keep growing. perf clamps to INT16_MAX in set_max_cpu_num() as a safety net. + - Code simplification: the int16_t forces defensive truncation + checks at every boundary where a wider CPU index (int from + sample->cpu, al->cpu, etc.) is narrowed into struct perf_cpu. + Without these checks, values > 32767 silently wrap to negative + numbers (two's complement), bypassing bounds validation. + Widening to int eliminates this entire class of silent + truncation bugs and removes the need for the INT16_MAX clamp + in set_max_cpu_num(). - Scope: struct perf_cpu is embedded everywhere — perf_cpu_map__cpu(), perf_cpu_map__min(), perf_cpu_map__max(), perf_cpu_map__has(), the for_each_cpu macros, and all internal callers. The perf_cpu_map
Signature · 1 line
2.54.0