perf tools: Fix pre-existing bugs in machine, cs-etm, c2c, bpf, and dso
Arnaldo Carvalho de Melo
maintainercover perf tools: Fix pre-existing bugs in machine, cs-etm, c2c, bpf, and dso
Hi,
Nine more pre-existing bugs found by sashiko-bot during AI-assisted code review. All are independent of the perf-data-validation hardening series — they are latent bugs in surrounding code exposed during review.
The fixes are grouped by subsystem:
machine__init() error propagation (patches 1-2):
machine__init() always returns 0 on allocation failure because the error code is never propagated through the return statement. Callers (including machines__init() and __machine__new_host()) proceed with a partially initialized machine struct. The error cleanup also uses zfree() on refcounted kmaps instead of maps__zput(). Additionally, machines__findnew() and get_kernel_version() use sprintf() with unsanitized guestmount paths that can overflow PATH_MAX stack buffers.
CoreSight ETM metadata validation (patches 3-5):
cs_etm__process_auxtrace_info_full() reads num_cpu from untrusted perf.data and uses it directly in a multiplication that can overflow to zero on 32-bit, producing a zero-sized allocation followed by OOB writes. The minimum size check in cs_etm__process_auxtrace_info() doesn't cover the global header fields actually accessed. cs_etm__get_queue() indexes queue_array[] without bounds checking the CPU value from untrusted trace payload, and several queue iteration loops dereference .priv without NULL checks after array growth zero-initializes new entries.
c2c hist entry leaks (patches 6-7):
When c2c_hists__init() fails, dynamically allocated format structures are leaked because the error path frees the container without unregistering them. During resort merges, c2c_he_free() only walks the output-sorted tree (empty before resort), leaking all inner hist_entry objects from entries_in_array[] and entries_collapsed.
BPF prog info pointer validation (patch 8):
Several functions cast bpf_prog_info u64 fields to pointers without checking whether bpil_offs_to_addr() actually converted the file offsets. A crafted perf.data with PERF_BPIL_* bits unset but non-zero counts causes raw file offsets to be dereferenced as pointers.
DSO decompression errno (patch 9):
dso__get_filename() sets errno to a negative custom DSO_LOAD_ERRNO value on decompression failure. __open_dso() computes fd = -errno, producing a large positive value that looks like a valid fd, causing close_data_fd() to close an unrelated file descriptor.
Build-tested with gcc and clang. Passes perf test on x86_64.
Arnaldo Carvalho de Melo (9):
perf machine: Propagate machine__init() error to callers perf machine: Use snprintf() for guestmount path construction perf cs-etm: Validate num_cpu before metadata allocation perf cs-etm: Require full global header in auxtrace_info size check perf cs-etm: Bounds-check CPU in cs_etm__get_queue()
perf c2c: Free format list entries when c2c_hists__init() fails perf c2c: Fix hist entry and format list leaks in c2c_he_free() perf bpf: Validate array presence before casting BPF prog info pointers perf dso: Set standard errno on decompression failure
tools/perf/builtin-c2c.c | 3 ++- tools/perf/tests/hists_cumulate.c | 3 ++- tools/perf/tests/hists_filter.c | 3 ++- tools/perf/tests/hists_link.c | 3 ++- tools/perf/tests/hists_output.c | 3 ++- tools/perf/tests/thread-maps-share.c | 2 +- tools/perf/util/bpf-event.c | 20 ++++++++++++++++--- tools/perf/util/bpf-event.h | 4 ++-- tools/perf/util/cs-etm-base.c | 4 +++- tools/perf/util/cs-etm.c | 37 ++++++++++++++++++++++++++++++++++-- tools/perf/util/dso.c | 18 +++++++++++++++++- tools/perf/util/header.c | 3 +-- tools/perf/util/hist.c | 2 +- tools/perf/util/hist.h | 1 + tools/perf/util/machine.c | 23 +++++++++++++--------- tools/perf/util/machine.h | 2 +- tools/perf/util/session.c | 4 +++- 17 files changed, 106 insertions(+), 29 deletions(-)
Developed with AI assistance (Claude/sashiko), tagged in commits.
Thanks,
- Arnaldo
Arnaldo Carvalho de Melo
maintainerPATCH 1/9 perf machine: Propagate machine__init() error to callers
From: Arnaldo Carvalho de Melo <[email protected]>
machine__init() always returns 0 even when memory allocation fails, because commit 81f981d7ec43ed93 ("perf machine: Free root_dir in machine__init() error path") introduced 'int err = -ENOMEM' and an error cleanup path but left the final 'return 0' instead of 'return err'.
Fix by returning err, check the return value in __machine__new_host() which was ignoring it, and change machines__init() from void to int so it too can propagate the error to perf_session__new() and test callers.
The error cleanup also used zfree(&machine->kmaps), but kmaps is a refcounted maps structure — use maps__zput() to properly drop the reference, matching machine__exit().
--- tools/perf/tests/hists_cumulate.c | 3 ++- tools/perf/tests/hists_filter.c | 3 ++- tools/perf/tests/hists_link.c | 3 ++- tools/perf/tests/hists_output.c | 3 ++- tools/perf/tests/thread-maps-share.c | 2 +- tools/perf/util/machine.c | 15 ++++++++++----- tools/perf/util/machine.h | 2 +- tools/perf/util/session.c | 4 +++- 8 files changed, 23 insertions(+), 12 deletions(-)
tools/perf/tests/hists_cumulate.c +2 -1
@@ -723,7 +723,8 @@ static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt goto out; err =TEST_FAIL ; -machines__init (&machines); +if (machines__init (&machines)) + goto out;/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines);
tools/perf/tests/hists_filter.c +2 -1
@@ -131,7 +131,8 @@ static int test__hists_filter(struct test_suite *test __maybe_unused, int subtes goto out; err =TEST_FAIL ; -machines__init (&machines); +if (machines__init (&machines)) + goto out;/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines);
tools/perf/tests/hists_link.c +2 -1
@@ -303,7 +303,8 @@ static int test__hists_link(struct test_suite *test __maybe_unused, int subtest goto out; err =TEST_FAIL ; -machines__init (&machines); +if (machines__init (&machines)) + goto out;/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines);
tools/perf/tests/hists_output.c +2 -1
@@ -610,7 +610,8 @@ static int test__hists_output(struct test_suite *test __maybe_unused, int subtes goto out; err =TEST_FAIL ; -machines__init (&machines); +if (machines__init (&machines)) + goto out;/* setup threads/dso/map/symbols also */ machine =setup_fake_machine (&machines);
tools/perf/tests/thread-maps-share.c +1 -1
@@ -27,7 +27,7 @@ static int test__thread_maps_share(struct test_suite *test __maybe_unused, int s* other group (pid: 4, tids: 4, 5) */ -machines__init (&machines); +TEST_ASSERT_VAL ("failed to init machines" ,machines__init (&machines) ==0 ); machine = &machines.host;/* create process with 4 threads */
tools/perf/util/machine.c +10 -5
@@ -124,11 +124,11 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid) out:if (err) { -zfree (&machine->kmaps); +maps__zput (machine->kmaps);zfree (&machine->root_dir);zfree (&machine->mmap_name); } -return 0 ; +return err; }static struct machine *__machine__new_host (struct perf_env *host_env,bool kernel_maps)
@@ -138,7 +138,10 @@ static struct machine *__machine__new_host(struct perf_env *host_env, bool kerneif (!machine)return NULL ; -machine__init (machine,"" ,HOST_KERNEL_ID ); +if (machine__init (machine,"" ,HOST_KERNEL_ID ) !=0 ) { +free (machine); +return NULL ; + }if (kernel_maps &&machine__create_kernel_maps (machine) <0 ) {free (machine);
@@ -231,10 +234,12 @@ void machine__delete(struct machine *machine) } } -void machines__init (struct machines *machines) +int machines__init (struct machines *machines) { -machine__init (&machines->host,"" ,HOST_KERNEL_ID ); +int err =machine__init (&machines->host,"" ,HOST_KERNEL_ID ); + machines->guests =RB_ROOT_CACHED ; +return err; }void machines__exit (struct machines *machines)
tools/perf/util/machine.h +1 -1
@@ -152,7 +152,7 @@ struct machines {struct rb_root_cached guests; }; -void machines__init (struct machines *machines); +int machines__init (struct machines *machines);void machines__exit (struct machines *machines); void machines__process_guests(struct machines *machines,
tools/perf/util/session.c +3 -1
@@ -160,7 +160,9 @@ struct perf_session *__perf_session__new(struct perf_data *data, session->decomp_data.zstd_decomp = &session->zstd_data; session->active_decomp = &session->decomp_data;INIT_LIST_HEAD (&session->auxtrace_index); -machines__init (&session->machines); +if (machines__init (&session->machines)) + goto out_delete; +ordered_events__init (&session->ordered_events, ordered_events__deliver_event,NULL );
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 2/9 perf machine: Use snprintf() for guestmount path construction
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
machines__findnew(), machines__create_guest_kernel_maps(), and get_kernel_version() use sprintf() to build paths by prepending symbol_conf.guestmount or root_dir. All write into PATH_MAX stack buffers, but guestmount comes from user configuration and is not length-checked. A guestmount path at or near PATH_MAX causes a stack buffer overflow — and a truncated root_dir propagated to get_kernel_version() would overflow its own version[PATH_MAX] buffer when "/proc/version" is appended.
Switch to snprintf() with sizeof() to prevent overflow. The subsequent access()/fopen() calls will fail on a truncated path.
--- tools/perf/util/machine.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
tools/perf/util/machine.c +4 -4
@@ -334,7 +334,7 @@ struct machine *machines__findnew(struct machines *machines, pid_t pid)if ((pid !=HOST_KERNEL_ID ) && (pid !=DEFAULT_GUEST_KERNEL_ID ) && (symbol_conf.guestmount)) { -sprintf (path,"%s/%d" , symbol_conf.guestmount, pid); +snprintf (path,sizeof (path),"%s/%d" , symbol_conf.guestmount, pid);if (access (path,R_OK )) {static struct strlist *seen;
@@ -1261,9 +1261,9 @@ int machines__create_guest_kernel_maps(struct machines *machines) namelist[i]->d_name);continue ; } -sprintf (path,"%s/%s/proc/kallsyms" , - symbol_conf.guestmount, - namelist[i]->d_name); +snprintf (path,sizeof (path),"%s/%s/proc/kallsyms" , + symbol_conf.guestmount, + namelist[i]->d_name); ret =access (path,R_OK );if (ret) {pr_debug ("Can't access file %s\n" , path);
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 3/9 perf cs-etm: Validate num_cpu before metadata allocation
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
cs_etm__process_auxtrace_info_full() reads num_cpu from untrusted perf.data and uses it to allocate the metadata pointer array:
metadata = zalloc(sizeof(*metadata) * num_cpu);
On 32-bit, sizeof(*metadata) is 4, so num_cpu = 0x40000000 overflows the multiplication to 0, causing zalloc(0) to return a valid zero-sized allocation followed by out-of-bounds writes in the population loop.
Fix by computing priv_size early and using it to bound num_cpu: each CPU needs at least one u64 metadata entry, so num_cpu cannot exceed the total number of u64 entries in the event's private data area.
--- tools/perf/util/cs-etm.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/perf/util/cs-etm.c +12 -0
@@ -3431,6 +3431,18 @@ int cs_etm__process_auxtrace_info_full(union perf_event *event,/* First the global part */ ptr = (u64 *) auxtrace_info->priv; num_cpu = ptr[CS_PMU_TYPE_CPUS ] &0xffffffff ; + +/* +* Bound num_cpu by the event size: the global header consumes +* CS_ETM_HEADER_SIZE bytes, and each CPU needs at least one u64 +* metadata entry after that. +*/ + priv_size = total_size - event_header_size -INFO_HEADER_SIZE - +CS_ETM_HEADER_SIZE ; +if (num_cpu <=0 || priv_size <=0 || + num_cpu > priv_size / (int )sizeof (u64)) +return -EINVAL ; + metadata =zalloc (sizeof (*metadata) * num_cpu);if (!metadata)return -ENOMEM ;
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 4/9 perf cs-etm: Require full global header in auxtrace_info size check
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
cs_etm__process_auxtrace_info() checks that header.size covers event_header_size + INFO_HEADER_SIZE (16 bytes total), but then accesses ptr[CS_PMU_TYPE_CPUS] at offset 24 from the start of the event. A crafted 16-byte auxtrace_info event passes the size check but reads out-of-bounds.
Include CS_ETM_HEADER_SIZE in the minimum size check so that the global header entries (version, pmu_type_cpus, snapshot) are guaranteed to fit within the event.
--- tools/perf/util/cs-etm-base.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
tools/perf/util/cs-etm-base.c +3 -1
@@ -170,7 +170,9 @@ int cs_etm__process_auxtrace_info(union perf_event *event,u64 *ptr =NULL ;u64 hdr_version; -if (auxtrace_info->header.size < (event_header_size +INFO_HEADER_SIZE )) +/* Ensure priv[] is large enough for the global header entries */ +if (auxtrace_info->header.size < (event_header_size +INFO_HEADER_SIZE + +CS_ETM_HEADER_SIZE ))return -EINVAL ;/* First the global part */
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 5/9 perf cs-etm: Bounds-check CPU in cs_etm__get_queue()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
cs_etm__get_queue() indexes etm->queues.queue_array[cpu] without validating that cpu is within nr_queues. When processing AUX_OUTPUT_HW_ID events, the cpu value comes from untrusted perf.data trace payload and flows through cs_etm__process_trace_id_v0_1() and cs_etm__queue_aux_fragment() without bounds checking, allowing an out-of-bounds read with a crafted file.
Add a bounds check in cs_etm__get_queue() and NULL checks in all callers.
Also add NULL checks for queue_array[i].priv in the queue iteration loops in cs_etm__map_trace_id_v0() and cs_etm__process_trace_id_v0_1() — after auxtrace_queues__grow() new entries are zero-initialized so .priv can be NULL. Add a get_cpu_data() NULL check in cs_etm__process_trace_id_v0_1(), matching the existing check in cs_etm__process_trace_id_v0().
--- tools/perf/util/cs-etm.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-)
tools/perf/util/cs-etm.c +23 -2
@@ -292,8 +292,11 @@ static struct cs_etm_queue *cs_etm__get_queue(struct cs_etm_auxtrace *etm, int c {if (etm->per_thread_decoding)return etm->queues.queue_array[0 ].priv; -else -return etm->queues.queue_array[cpu].priv; + +if (cpu <0 || cpu >= (int )etm->queues.nr_queues) +return NULL ; + +return etm->queues.queue_array[cpu].priv; } static int cs_etm__map_trace_id_v0(struct cs_etm_auxtrace *etm, u8 trace_chan_id,
@@ -306,6 +309,9 @@ static int cs_etm__map_trace_id_v0(struct cs_etm_auxtrace *etm, u8 trace_chan_id* queue associated with that CPU so only one decoder is made. */ etmq =cs_etm__get_queue (etm, cpu_metadata[CS_ETM_CPU ]); +if (!etmq) +return -EINVAL ; +if (etmq->format ==UNFORMATTED )return cs_etm__insert_trace_id_node (etmq, trace_chan_id, cpu_metadata);
@@ -318,6 +324,9 @@ static int cs_etm__map_trace_id_v0(struct cs_etm_auxtrace *etm, u8 trace_chan_idint ret; etmq = etm->queues.queue_array[i].priv; +if (!etmq) +continue ; + ret =cs_etm__insert_trace_id_node (etmq, trace_chan_id, cpu_metadata);if (ret)
@@ -358,6 +367,9 @@ static int cs_etm__process_trace_id_v0_1(struct cs_etm_auxtrace *etm, int cpu,u32 sink_id =FIELD_GET (CS_AUX_HW_ID_SINK_ID_MASK , hw_id);u8 trace_id =FIELD_GET (CS_AUX_HW_ID_TRACE_ID_MASK , hw_id); +if (!etmq) +return -EINVAL ; +/* * Check sink id hasn't changed in per-cpu mode. In per-thread mode, * let it pass for now until an actual overlapping trace ID is hit. In
@@ -375,6 +387,9 @@ static int cs_etm__process_trace_id_v0_1(struct cs_etm_auxtrace *etm, int cpu,for (unsigned int i =0 ; i < etm->queues.nr_queues; ++i) {struct cs_etm_queue *other_etmq = etm->queues.queue_array[i].priv; +if (!other_etmq) +continue ; +/* Different sinks, skip */ if (other_etmq->sink_id != etmq->sink_id)continue ;
@@ -396,6 +411,9 @@ static int cs_etm__process_trace_id_v0_1(struct cs_etm_auxtrace *etm, int cpu, } cpu_data =get_cpu_data (etm, cpu); +if (!cpu_data) +return -EINVAL ; + ret =cs_etm__insert_trace_id_node (etmq, trace_id, cpu_data);if (ret)return ret;
@@ -3144,6 +3162,9 @@ static int cs_etm__queue_aux_fragment(struct perf_session *session, off_t file_o aux_offset + aux_size <= auxtrace_event->offset + auxtrace_event->size) {struct cs_etm_queue *etmq =cs_etm__get_queue (etm, auxtrace_event->cpu); +if (!etmq) +return -EINVAL ; +/* * If this AUX event was inside this buffer somewhere, create a new auxtrace event * based on the sizes of the aux event, and queue that fragment.
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 6/9 perf c2c: Free format list entries when c2c_hists__init() fails
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
When c2c_hists__init() fails partway through hpp_list__parse(), dynamically allocated format structures that were already added to hists->list are leaked because he__get_c2c_hists() frees the hists container without first unregistering the format entries.
Call perf_hpp__reset_output_field() before freeing the hists container on the error path, matching what c2c_he_free() already does on the normal destruction path.
--- tools/perf/builtin-c2c.c | 1 + 1 file changed, 1 insertion(+)
tools/perf/builtin-c2c.c +1 -0
@@ -226,6 +226,7 @@ he__get_c2c_hists(struct hist_entry *he, ret =c2c_hists__init (hists, sort, nr_header_lines, env);if (ret) { +perf_hpp__reset_output_field (&hists->list); c2c_he->hists =NULL ;free (hists);return NULL ;
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 7/9 perf c2c: Fix hist entry and format list leaks in c2c_he_free()
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
c2c_he_free() calls hists__delete_entries() which only walks the output-sorted entries tree. During c2c resort, when cacheline entries are merged and the redundant entry is freed, the inner hists have not been output-resorted yet, so hists->entries is empty. The actual inner hist_entry objects live in entries_in_array[] and entries_collapsed, which are never walked, leaking all inner hist_entry objects for every merged cacheline.
Additionally, the dynamically allocated format entries on hists->list are never unregistered or freed.
Fix both issues by switching to hists__delete_all_entries() which walks all rb_root trees, and calling perf_hpp__reset_output_field() to clean up format entries.
--- tools/perf/builtin-c2c.c | 2 +- tools/perf/util/hist.c | 2 +- tools/perf/util/hist.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-)
tools/perf/builtin-c2c.c +1 -1
@@ -184,7 +184,7 @@ static void c2c_he_free(void *he) c2c_he = container_of(he, struct c2c_hist_entry, he);if (c2c_he->hists) { -hists__delete_entries (&c2c_he->hists->hists); +hists__delete_all_entries (&c2c_he->hists->hists);perf_hpp__reset_output_field (&c2c_he->hists->list);zfree (&c2c_he->hists); }
tools/perf/util/hist.c +1 -1
@@ -3041,7 +3041,7 @@ static void hists__delete_remaining_entries(struct rb_root_cached *root) } } -static void hists__delete_all_entries (struct hists *hists) +void hists__delete_all_entries (struct hists *hists) {hists__delete_entries (hists);hists__delete_remaining_entries (&hists->entries_in_array[0 ]);
tools/perf/util/hist.h +1 -0
@@ -391,6 +391,7 @@ int hists__collapse_resort(struct hists *hists, struct ui_progress *prog);void hists__decay_entries (struct hists *hists,bool zap_user,bool zap_kernel);void hists__delete_entries (struct hists *hists); +void hists__delete_all_entries (struct hists *hists);void hists__output_recalc_col_len (struct hists *hists,int max_rows);struct hist_entry *hists__get_entry (struct hists *hists,int idx);
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 8/9 perf bpf: Validate array presence before casting BPF prog info pointers
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
Several functions cast bpf_prog_info fields (jited_ksyms, jited_func_lens, jited_prog_insns) from u64 to pointers and dereference them. These fields are only valid pointers if bpil_offs_to_addr() converted their file offsets to addresses, which only happens when the corresponding PERF_BPIL_* bits are set in info_linear->arrays.
A crafted perf.data can leave these bits unset while setting non-zero counts and offset values, causing the functions to dereference raw file offsets as pointers.
Add array bitmask validation to all perf.data processing paths:
- __bpf_event__print_bpf_prog_info(): check JITED_KSYMS and JITED_FUNC_LENS (changed to take struct perf_bpil *)
- machine__process_bpf_event_load(): check JITED_KSYMS
- bpf_read(): check JITED_INSNS before memcpy from jited_prog_insns
- dso__disassemble_filename(): check JITED_INSNS before returning jited_prog_insns pointer
--- tools/perf/util/bpf-event.c | 20 +++++++++++++++++--- tools/perf/util/bpf-event.h | 4 ++-- tools/perf/util/dso.c | 10 ++++++++++ tools/perf/util/header.c | 3 +-- 4 files changed, 30 insertions(+), 7 deletions(-)
tools/perf/util/bpf-event.c +17 -3
@@ -59,6 +59,10 @@ static int machine__process_bpf_event_load(struct machine *machine,return 0 ; info_linear = info_node->info_linear; +/* jited_ksyms is only valid if bpil_offs_to_addr() converted it */ +if (!(info_linear->arrays & (1UL <<PERF_BPIL_JITED_KSYMS ))) +return 0 ; +for (i =0 ; i < info_linear->info.nr_jited_ksyms; i++) {u64 *addrs = (u64 *)(uintptr_t )(info_linear->info.jited_ksyms);u64 addr = addrs[i];
@@ -959,12 +963,15 @@ int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)return evlist__add_sb_event (evlist, &attr, bpf_event__sb_cb, env); } -void __bpf_event__print_bpf_prog_info (struct bpf_prog_info *info, +void __bpf_event__print_bpf_prog_info (struct perf_bpil *info_linear,struct perf_env *env,FILE *fp) { -__u32 *prog_lens = (__u32 *)(uintptr_t )(info->jited_func_lens); -__u64 *prog_addrs = (__u64 *)(uintptr_t )(info->jited_ksyms); +struct bpf_prog_info *info = &info_linear->info; +__u64 required_arrays = (1UL <<PERF_BPIL_JITED_KSYMS ) | + (1UL <<PERF_BPIL_JITED_FUNC_LENS ); +__u32 *prog_lens; +__u64 *prog_addrs;char name[KSYM_NAME_LEN ];struct btf *btf =NULL ;u32 sub_prog_cnt, i;
@@ -974,6 +981,13 @@ void __bpf_event__print_bpf_prog_info(struct bpf_prog_info *info, sub_prog_cnt != info->nr_jited_func_lens)return ; +/* Ensure the arrays were present and converted by bpil_offs_to_addr() */ +if ((info_linear->arrays & required_arrays) != required_arrays) +return ; + + prog_lens = (__u32 *)(uintptr_t )(info->jited_func_lens); + prog_addrs = (__u64 *)(uintptr_t )(info->jited_ksyms); +if (info->btf_id) {struct btf_node *node;
tools/perf/util/bpf-event.h +2 -2
@@ -40,7 +40,7 @@ struct btf_node {int machine__process_bpf (struct machine *machine,union perf_event *event,struct perf_sample *sample);int evlist__add_bpf_sb_event (struct evlist *evlist,struct perf_env *env); -void __bpf_event__print_bpf_prog_info (struct bpf_prog_info *info, +void __bpf_event__print_bpf_prog_info (struct perf_bpil *info_linear,struct perf_env *env,FILE *fp);void bpf_metadata_free (struct bpf_metadata *metadata);
@@ -58,7 +58,7 @@ static inline int evlist__add_bpf_sb_event(struct evlist *evlist __maybe_unused,return 0 ; } -static inline void __bpf_event__print_bpf_prog_info (struct bpf_prog_info *info__maybe_unused , +static inline void __bpf_event__print_bpf_prog_info (struct perf_bpil *info_linear__maybe_unused ,struct perf_env *env__maybe_unused ,FILE *fp__maybe_unused ) {
tools/perf/util/dso.c +10 -0
@@ -880,6 +880,12 @@ static ssize_t bpf_read(struct dso *dso, u64 offset, char *data)return -1 ; } +/* jited_prog_insns is only valid if bpil_offs_to_addr() converted it */ +if (!(node->info_linear->arrays & (1UL <<PERF_BPIL_JITED_INSNS ))) { +dso__data (dso)->status =DSO_DATA_STATUS_ERROR ; +return -1 ; + } + len = node->info_linear->info.jited_prog_len; buf = (u8 *)(uintptr_t )node->info_linear->info.jited_prog_insns;
@@ -1995,6 +2001,10 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,return NULL ; } info_linear = info_node->info_linear; +if (!(info_linear->arrays & (1UL <<PERF_BPIL_JITED_INSNS ))) { + errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ; +return NULL ; + }assert (len <= info_linear->info.jited_prog_len); *out_buf_len = len;return (const u8 *)(uintptr_t )(info_linear->info.jited_prog_insns);
tools/perf/util/header.c +1 -2
@@ -2107,8 +2107,7 @@ static void print_bpf_prog_info(struct feat_fd *ff __maybe_unused, FILE *fp) node = rb_entry(next, struct bpf_prog_info_node, rb_node); next =rb_next (&node->rb_node); -__bpf_event__print_bpf_prog_info (&node->info_linear->info, - env, fp); +__bpf_event__print_bpf_prog_info (node->info_linear, env, fp); }up_read (&env->bpf_progs.lock);
Signature · 1 line
2.54.0
Arnaldo Carvalho de Melo
maintainerPATCH 9/9 perf dso: Set standard errno on decompression failure
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
dso__get_filename() sets errno to a negative custom DSO_LOAD_ERRNO value when kernel module decompression fails:
errno = *dso__load_errno(dso); /* e.g. -9996 */
The caller __open_dso() then computes fd = -errno, producing a large positive value (9996) that looks like a valid file descriptor. This can cause close_data_fd() to close an unrelated fd used by another subsystem.
Set errno to EIO instead. The detailed error code is already stored in dso__load_errno(dso) for diagnostic messages.
--- tools/perf/util/dso.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +7 -1
@@ -600,7 +600,13 @@ static char *dso__get_filename(struct dso *dso, const char *root_dir,size_t len =sizeof (newpath);if (dso__decompress_kmodule_path (dso, name, newpath, len) <0 ) { - errno = *dso__load_errno (dso); +/* +* Use a standard errno value, not the negative custom +* DSO_LOAD_ERRNO stored in dso__load_errno(dso): +* __open_dso() computes fd = -errno, so a negative +* errno produces a positive fd that looks valid. +*/ + errno =EIO ; goto out; }
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH 2/9] perf machine: Use snprintf() for guestmount path construction
In reply to Arnaldo Carvalho de MeloThank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
Pre-existing issues:
- [High] Unconditional memory leak of dirent elements returned by scandir().
- [High] NULL pointer dereference when strlist__new() fails.
- [High] NULL pointer dereference when machine__findnew_thread() fails to allocate a parent thread.
- [Medium] Loop iteration logic bug causes silent skipping of valid guest VM directories if one directory is inaccessible.
- [Medium] File descriptor leak on an early error return during nested directory processing.
- [Medium] Improper error handling missing errno = 0 before strtol() can cause valid PIDs to be skipped. --
commit 02cca33a0758d115755760c916ffc005383e2736
Author: Arnaldo Carvalho de Melo <[email protected]>
perf machine: Use snprintf() for guestmount path construction
This commit replaces sprintf() with snprintf() when constructing guestmount paths. This prevents potential stack buffer overflows caused by unbounded user-provided guestmount lengths.
tools/perf/util/machine.c struct machine *machines__findnew(struct machines *machines, pid_t pid) · line 339
Quoted 12 lines
tools/perf/util/machine.c +1
-1@@ -334,7 +334,7 @@ struct machine *machines__findnew(struct machines *machines, pid_t pid)if ((pid !=HOST_KERNEL_ID ) && (pid !=DEFAULT_GUEST_KERNEL_ID ) && (symbol_conf.guestmount)) { -sprintf (path,"%s/%d" , symbol_conf.guestmount, pid); +snprintf (path,sizeof (path),"%s/%d" , symbol_conf.guestmount, pid);if (access (path,R_OK )) {static struct strlist *seen;
[Severity: High]
This is a pre-existing issue, but can this cause a null pointer dereference if strlist__new() fails?
Looking just below this in machines__findnew():
if (!seen)
seen = strlist__new(NULL, NULL);
if (!strlist__has_entry(seen, path)) {If memory allocation fails, seen becomes null, and it is immediately passed to strlist__has_entry() which might try to dereference it.
[ ... ]
tools/perf/util/machine.c int machines__create_guest_kernel_maps(struct machines *machines) · line 1269
Quoted 13 lines
tools/perf/util/machine.c +3
-3@@ -1261,9 +1261,9 @@ int machines__create_guest_kernel_maps(struct machines *machines) namelist[i]->d_name);continue ; } -sprintf (path,"%s/%s/proc/kallsyms" , - symbol_conf.guestmount, - namelist[i]->d_name); +snprintf (path,sizeof (path),"%s/%s/proc/kallsyms" , + symbol_conf.guestmount, + namelist[i]->d_name); ret =access (path,R_OK );if (ret) {pr_debug ("Can't access file %s\n" , path);
[Severity: Medium]
This is a pre-existing issue, but should this loop continue instead of failing completely? Looking just below this in machines__create_guest_kernel_maps(), the code jumps to the failure label:
goto failure; } machines__create_kernel_maps(machines, pid);
By jumping to the failure label, it seems we skip processing all remaining valid guest directories if just one is inaccessible.
While reviewing this file, I noticed a few other pre-existing issues:
[Severity: Medium]
This is a pre-existing issue, but does this correctly handle errno?
tools/perf/util/machine.c:machines__create_guest_kernel_maps() {
...
pid = (pid_t)strtol(namelist[i]->d_name, &endp, 10);
if ((*endp != '\0') ||
(endp == namelist[i]->d_name) ||
(errno == ERANGE)) {...
}
The code checks if errno is ERANGE, but errno isn't set to 0 before calling strtol(). Could a previous operation leave errno as ERANGE, causing valid PIDs to be skipped here?
[Severity: High]
This is a pre-existing issue, but does this leak the individual directory entries returned by scandir()?
tools/perf/util/machine.c:machines__create_guest_kernel_maps() {
...
failure:
free(namelist); } return ret; }
The namelist array is freed here, but it looks like the individual struct dirent pointers allocated by scandir() and stored in namelist[i] are never freed before the array itself is freed.
[Severity: High]
This is a pre-existing issue, but in machine__process_fork_event(), is it possible to dereference a null pointer if thread allocation fails?
tools/perf/util/machine.c:machine__process_fork_event() {
...
struct thread *parent = machine__findnew_thread(machine,
event->fork.ppid,
event->fork.ptid);
...
if (thread__pid(parent) != (pid_t)event->fork.ppid) {...
}
If machine__findnew_thread() returns null due to memory pressure, it seems the pointer is accessed without a check.
[Severity: Medium]
This is a pre-existing issue, but does maps__set_modules_path_dir() leak a file descriptor on an early return?
tools/perf/util/machine.c:maps__set_modules_path_dir() {
...
io_dir__init(&iod, open(path, O_CLOEXEC | O_DIRECTORY | O_RDONLY)); ... /* Bounds check, should never happen. */ if (root_len >= path_size) return -1;
...
}
If the bounds check fails, the function returns immediately and skips the close() call for the directory file descriptor.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH 1/9] perf machine: Propagate machine__init() error to callers
In reply to Arnaldo Carvalho de MeloThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Error paths introduced by checking machines__init() result in cleanup routines destroying…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Error paths introduced by checking machines__init() result in cleanup routines destroying uninitialized mutexes and rwlocks.
- [High] Missing error handling for the newly propagated return value of machines__init() in aslr_tool__init(). --
commit 63ae9f1a874939f375ee99fefcd13baacfebe5eb
Author: Arnaldo Carvalho de Melo <[email protected]>
perf machine: Propagate machine__init() error to callers
machine__init() always returns 0 even when memory allocation fails, because commit 81f981d7ec43ed93 ("perf machine: Free root_dir in machine__init() error path") introduced 'int err = -ENOMEM' and an error cleanup path but left the final 'return 0' instead of 'return err'.
[ ... ]
tools/perf/tests/hists_cumulate.c static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt · line 727
Quoted 11 lines
tools/perf/tests/hists_cumulate.c +2
-1@@ -723,7 +723,8 @@ static int test__hists_cumulate(struct test_suite *test __maybe_unused, int subt goto out; err =TEST_FAIL ; -machines__init (&machines); +if (machines__init (&machines)) + goto out;
[Severity: High]
If machines__init() fails here, we jump to out:
out:
/* tear down everything */ evlist__delete(evlist); machines__exit(&machines); put_fake_samples();
Since machines is allocated on the stack and machines__init() can fail before fully initializing it, will machines__exit() try to clean up uninitialized state, such as calling down_write() on an uninitialized rwlock?
[ ... ]
tools/perf/util/session.c struct perf_session *__perf_session__new(struct perf_data *data, · line 165
Quoted 12 lines
tools/perf/util/session.c +3
-1@@ -160,7 +160,9 @@ struct perf_session *__perf_session__new(struct perf_data *data, session->decomp_data.zstd_decomp = &session->zstd_data; session->active_decomp = &session->decomp_data;INIT_LIST_HEAD (&session->auxtrace_index); -machines__init (&session->machines); +if (machines__init (&session->machines)) + goto out_delete; +
[Severity: High]
If we jump to out_delete here, perf_session__delete(session) is called, which unconditionally calls perf_env__exit(&session->header.env).
Because this goto happens before perf_env__init(&session->header.env) is called, could perf_env__exit() end up destroying uninitialized rwlocks and mutexes in the zero-allocated session->header.env?
[ ... ]
[Severity: High]
Although not modified in this patch, the newly propagated return value of machines__init() seems to be ignored in tools/perf/util/aslr.c in the aslr_tool__init() function.
If machines__init() fails there, could aslr_tool__init() silently proceed with an uninitialized machines structure, leading to crashes when it is subsequently used or destroyed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewRe: [PATCH 8/9] perf bpf: Validate array presence before casting BPF prog info pointers
In reply to Arnaldo Carvalho de MeloThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] Incomplete validation of BPF program info arrays leaves paths to arbitrary pointer dereferences open in synthesize_bpf_prog_name().
- [High] Missing array bitmask validation in symbol__disassemble_bpf_libbfd() leads to arbitrary pointer dereferences.
- [High] Attacker-controlled length in dso__read_symbol() triggers assertion failure (DoS) or out-of-bounds heap read. --
commit 2f222987999c231a301341e2fbb0abf020879147
Author: Arnaldo Carvalho de Melo <[email protected]>
perf bpf: Validate array presence before casting BPF prog info pointers
Several functions cast bpf_prog_info fields (jited_ksyms, jited_func_lens, jited_prog_insns) from u64 to pointers and dereference them. These fields are only valid pointers if bpil_offs_to_addr() converted their file offsets to addresses, which only happens when the corresponding PERF_BPIL_* bits are set in info_linear->arrays.
Quoted 4 lines
tools/perf/util/bpf-event.c +0
-0
[ ... ]
Quoted 4 lines
tools/perf/util/bpf-event.c +0
-0@@ -959,12 +963,15 @@ int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env)return evlist__add_sb_event (evlist, &attr, bpf_event__sb_cb, env); }
tools/perf/util/bpf-event.c int evlist__add_bpf_sb_event(struct evlist *evlist, struct perf_env *env) · line 972
Quoted 10 lines
tools/perf/util/bpf-event.c +4
-3-void __bpf_event__print_bpf_prog_info (struct bpf_prog_info *info, +void __bpf_event__print_bpf_prog_info (struct perf_bpil *info_linear,struct perf_env *env,FILE *fp) { -__u32 *prog_lens = (__u32 *)(uintptr_t )(info->jited_func_lens); -__u64 *prog_addrs = (__u64 *)(uintptr_t )(info->jited_ksyms); +struct bpf_prog_info *info = &info_linear->info; +__u64 required_arrays = (1UL <<PERF_BPIL_JITED_KSYMS ) | + (1UL <<PERF_BPIL_JITED_FUNC_LENS );
[Severity: High]
This isn't a bug introduced by this patch, but it looks like the validation remains incomplete. Could this leave a path for arbitrary pointer dereferences open in synthesize_bpf_prog_name()?
If a crafted perf.data file omits PERF_BPIL_PROG_TAGS and PERF_BPIL_FUNC_INFO from the bitmask but provides raw file offsets, bpil_offs_to_addr() won't convert or clear them. Since required_arrays doesn't check these flags, synthesize_bpf_prog_name() will then unconditionally cast info->prog_tags and conditionally cast info->func_info to pointers and dereference them.
Should PERF_BPIL_PROG_TAGS and PERF_BPIL_FUNC_INFO be added to the required_arrays bitmask check here?
[ ... ]
Quoted 4 lines
tools/perf/util/dso.c +0
-0
[ ... ]
tools/perf/util/dso.c const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename, · line 2008
Quoted 9 lines
tools/perf/util/dso.c +4
-0@@ -1995,6 +2001,10 @@ const u8 *dso__read_symbol(struct dso *dso, const char *symfs_filename,return NULL ; } info_linear = info_node->info_linear; +if (!(info_linear->arrays & (1UL <<PERF_BPIL_JITED_INSNS ))) { + errno =SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF ; +return NULL ; + }assert (len <= info_linear->info.jited_prog_len);
[Severity: High]
This is a pre-existing issue, but does this assertion evaluate attacker controlled lengths?
In dso__read_symbol(), the local variable len is calculated using sym->start and sym->end from a parsed PERF_RECORD_KSYMBOL event, which comes from the perf.data file.
Since both len and jited_prog_len come from the data source, could this assertion be forced to fail, crashing the tool? Or, if perf is compiled with NDEBUG, couldn't this bypass the check entirely, allowing an out-of-bounds heap read that exposes memory contents in the disassembly output?
[Severity: High]
This is a pre-existing issue, but it appears a similar lack of array bitmask validation exists in symbol__disassemble_bpf_libbfd() in tools/perf/util/libbfd.c. Was this file missed during the conversion?
Looking at symbol__disassemble_bpf_libbfd(), it directly casts and dereferences info_linear->info.jited_prog_insns and info_linear->info.jited_ksyms without checking if PERF_BPIL_JITED_INSNS or PERF_BPIL_JITED_KSYMS are set in the info_linear->arrays bitmask.
Additionally, it calls bpf_prog_linfo__new(), which blindly copies memory from info->line_info using memcpy(). A crafted perf.data file could provide raw file offsets for these fields while unsetting the bitmask flags, bypassing address conversion and potentially causing arbitrary memory reads.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
[email protected]
AI reviewRe: [PATCH 5/9] perf cs-etm: Bounds-check CPU in cs_etm__get_queue()
In reply to Arnaldo Carvalho de MeloThank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
Pre-existing issues:
- [High] Missing NULL pointer checks for dynamically expanded queue .priv fields allows malicious perf.data to crash the perf tool.
- [High] Integer overflow in idx validation bypasses queue array growth, leading to a massive out-of-bounds heap write.
- [Medium] Error path in cs_etm__process_auxtrace_info_full() leaks dynamically allocated cs_etm_queue contexts.
- [Medium] auxtrace_queues__grow() permanently leaks the old queue_array memory when expanding the queue. --
commit a1dea01e7aeb4b8a5c1245018dd3bd55a6dc9d91
Author: Arnaldo Carvalho de Melo <[email protected]>
perf cs-etm: Bounds-check CPU in cs_etm__get_queue()
cs_etm__get_queue() indexes etm->queues.queue_array[cpu] without validating that cpu is within nr_queues. This patch adds bounds checking and NULL checks to prevent out-of-bounds reads when processing untrusted perf.data files.
Quoted 3 lines
tools/perf/util/cs-etm.c +0
-0
[ ... ]
tools/perf/util/cs-etm.c static int cs_etm__map_trace_id_v0(struct cs_etm_auxtrace *etm, u8 trace_chan_id · line 329
Quoted 7 lines
tools/perf/util/cs-etm.c +3
-0@@ -318,6 +324,9 @@ static int cs_etm__map_trace_id_v0(struct cs_etm_auxtrace *etm, u8 trace_chan_idint ret; etmq = etm->queues.queue_array[i].priv; +if (!etmq) +continue ; +
[Severity: High]
This isn't a bug introduced by this patch, but since we are adding NULL checks for dynamically expanded queue .priv fields here, do we also need them in other functions?
auxtrace_queues__add_event() dynamically expands the queue_array if an untrusted perf.data file provides a large idx.
The newly allocated queues are zero-initialized, leaving their .priv pointers NULL.
A subsequent iteration in cs_etm__create_decoders() checks if the new queue is empty. Since a buffer was appended, it is not empty, and the code dereferences etmq->format unconditionally.
Additionally, if dump_trace is active, cs_etm__process_auxtrace_event() and cs_etm__dump_event() unconditionally dereference the NULL priv pointer.
Does this allow an untrusted file to cause memory corruption or a crash?
[ ... ]
[Severity: High]
This is a pre-existing issue, but does the integer overflow in idx validation bypass queue array growth, leading to an out-of-bounds heap write?
If untrusted perf.data input provides idx = 0xFFFFFFFF, the condition idx >= queues->nr_queues is met, but auxtrace_queues__grow(queues, idx + 1) wraps idx + 1 to 0.
tools/perf/util/auxtrace.c:auxtrace_queues__grow() {
...
if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
return -EINVAL;
...
}Because new_nr_queues is 0, the validation passes and the function falsely indicates success.
Then auxtrace_queues__queue_buffer() accesses:
queue = &queues->queue_array[0xFFFFFFFF]
and modifies memory via queue->set = true andlist_add_tail(&buffer->list, &queue->head).
Could this cause a massive out-of-bounds heap write?
[Severity: Medium]
This is a pre-existing issue, but does the error path in cs_etm__process_auxtrace_info_full() leak dynamically allocated cs_etm_queue contexts?
If a subsequent operation fails, the code jumps to the err_free_queues label:
tools/perf/util/cs-etm.c:cs_etm__process_auxtrace_info_full() {
...
err_free_queues:
auxtrace_queues__free(&etm->queues);
...
}auxtrace_queues__free() clears the array without releasing the nested priv pointers:
tools/perf/util/auxtrace.c:auxtrace_queues__free() {
...
zfree(&queues->queue_array);
queues->nr_queues = 0;
...
}Does this leak num_cpu * sizeof(struct cs_etm_queue) bytes on the error path?
[Severity: Medium]
This isn't a bug introduced by this patch, but does auxtrace_queues__grow() permanently leak the old queue_array memory when expanding the queue?
tools/perf/util/auxtrace.c:auxtrace_queues__grow() {
...
queues->queue_array = queue_array;
return 0;
...
}The old queues->queue_array pointer is overwritten with the new one without ever being freed via free() or zfree().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH 6/9] perf c2c: Free format list entries when c2c_hists__init() fails
In reply to Arnaldo Carvalho de MeloThank 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:
Pre-existing issues:
- [High] perf_hpp__setup_output_field() mistakenly appends format elements to the global perf_hpp_list.fields instead of the local list->fields, leading to an unbounded accumulation of c2c_fmt structures.
- [Medium] hpp_list__parse() and the PARSE_LIST macro silently overwrite and drop error return codes during format parsing.
--
commit f3900597f4c802568083103a6a640b3c7cb043fa
Author: Arnaldo Carvalho de Melo <[email protected]>
perf c2c: Free format list entries when c2c_hists__init() fails
This commit fixes a memory leak in the error path of he__get_c2c_hists(). It calls perf_hpp__reset_output_field() before freeing the hists container when c2c_hists__init() fails, ensuring dynamically allocated format entries are properly unregistered.
tools/perf/builtin-c2c.c he__get_c2c_hists(struct hist_entry *he, · line 227
Quoted 6 lines
tools/perf/builtin-c2c.c +0
-0@@ -226,6 +226,7 @@ he__get_c2c_hists(struct hist_entry *he, ret =c2c_hists__init (hists, sort, nr_header_lines, env);
[Severity: Medium]
This isn't a bug introduced by this patch, but does hpp_list__parse() (called within c2c_hists__init()) silently drop error return codes?
Looking at the PARSE_LIST macro in tools/perf/builtin-c2c.c, it initializes ret = 0 internally on every invocation:
#define PARSE_LIST(_list, _fn) \
do { \
char *tmp, *tok; \
ret = 0;In hpp_list__parse(), sequential macro invocations mean the first error code can be discarded:
PARSE_LIST(output, c2c_hists__init_output);
PARSE_LIST(sort, c2c_hists__init_sort);If the output parsing encounters an error, it breaks out and correctly sets ret, but the subsequent sort parsing invocation immediately resets ret = 0. Additionally, if strdup fails, it breaks but leaves ret = 0.
Could this cause allocation or parsing failures during c2c_hists__init() to be silently ignored, returning success despite missing formatting?
[Severity: High]
This is a pre-existing issue, but is there an unbounded accumulation of c2c_fmt structures occurring when he__get_c2c_hists() initializes per-cacheline history lists?
When c2c_hists__init() sets up the local list, it eventually calls perf_hpp__setup_output_field(). This function takes a local list parameter but uses an inline wrapper that hardcodes the global list:
tools/perf/util/hist.h:perf_hpp__column_register() {
perf_hpp_list__column_register(&perf_hpp_list, format);
}Because perf_hpp__setup_output_field() in tools/perf/ui/hist.c uses this global wrapper (perf_hpp__column_register(fmt)) instead of the local variant, the format elements intended for each cacheline's local history get appended to the global perf_hpp_list.fields.
Since perf c2c can process millions of cachelines, could this cause the global list to endlessly accumulate dynamically allocated format elements?
Quoted 5 lines
tools/perf/builtin-c2c.c +1
-0if (ret) { +perf_hpp__reset_output_field (&hists->list); c2c_he->hists =NULL ;free (hists);return NULL ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6