perf c2c hardening
Arnaldo Carvalho de Melo
maintainerv2 cover perf c2c hardening
Hi,
Please consider merging,
Two hardening fixes for perf c2c found by the sashiko-bot AI reviewer.
Patch 1 fixes two silent failure modes in hpp_list__parse(): error masking across successive PARSE_LIST invocations, and OOM from strdup() being silently treated as empty input. The fix adds early exits, which exposed a pre-existing problem: both callers of c2c_hists__reinit() had always discarded its return value, so errors would now go undetected. Patch 1 also fixes the full caller chain: resort_cl_cb() now checks and propagates the error, and perf_c2c__report() checks both c2c_hists__reinit() and hists__iterate_cb() -- which already stops iteration on callback errors.
Patch 2 fixes a format list leak: when c2c_hists__init() fails partway through, entries registered via perf_hpp_list__column_register() and perf_hpp_list__register_sort_field() are left on the hpp_list. perf_c2c_report() does not call perf_hpp__reset_output_field() on the error path, so those entries leak. The fix moves cleanup into c2c_hists__init() itself so all callers are protected.
Changes since v1 ([email protected]):
Patch 1:
- Addressed sashiko-bot [Medium] finding: the early exits added by v1 skip perf_hpp__setup_output_field(), making undetected errors in the callers consequential. Fixed by propagating the error through resort_cl_cb() and perf_c2c__report() (hists__iterate_cb() already propagated callback return values).
- Dismissed sashiko-bot [Low] finding (missing string.h/stdlib.h):
strdup() and free() were already used in this function before this patch; string2.h (included at the top of the file) pulls in string.h. No new library calls were introduced.
- Subject updated to reflect the additional caller fixes.
This series was developed with AI assistance (Claude Sonnet 4.6).
- Arnaldo
Arnaldo Carvalho de Melo (2):
perf c2c: Fix error masking, OOM, and unchecked caller errors in
hpp_list__parse()
perf c2c: Clean up registered formats on c2c_hists__init() failure
tools/perf/builtin-c2c.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-)
Signature · 1 line
2.55.0
Arnaldo Carvalho de Melo
maintainerPATCH v2 1/2 perf c2c: Fix error masking, OOM, and unchecked caller errors in hpp_list__parse()
From: Arnaldo Carvalho de Melo <[email protected]>
hpp_list__parse() has two bugs:
1. The PARSE_LIST macro resets ret = 0 at the start of each invocation, so an error from output parsing is silently overwritten when the sort parsing block runs. The function returns success with partially initialized state.
2. When the caller passes a non-NULL output_ or sort_ string, but strdup() returns NULL due to OOM, NULL is passed to PARSE_LIST which treats it as empty input (the "if (!_list) break" branch). No error is returned.
Fix both by checking strdup() return values before proceeding and adding a cleanup label so that ret from each PARSE_LIST call is checked before the next runs, preserving the first error.
The early exits now skip perf_hpp__setup_output_field(), which means c2c_hists__reinit() can return a non-zero value in cases that previously always succeeded silently. Both callers discarded its return:resort_cl_cb() continued into hists__collapse_resort() on a broken list, and perf_c2c__report() proceeded with uninitialised hists. Fix the full
chain: check and propagate the error in resort_cl_cb() -- hists__iterate_cb() already stops iteration and returns the callback error -- and check both c2c_hists__reinit() and hists__iterate_cb() in perf_c2c__report().
--- tools/perf/builtin-c2c.c | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-)
tools/perf/builtin-c2c.c +23 -3
@@ -2093,8 +2093,18 @@ static int hpp_list__parse(struct perf_hpp_list *hpp_list,char *sort = sort_ ?strdup (sort_) :NULL ;int ret; +/* strdup() returns NULL on OOM, don't silently treat as empty */ +if ((output_ && !output) || (sort_ && !sort)) { + ret = -ENOMEM ; + goto out; + } +PARSE_LIST (output, c2c_hists__init_output); +if (ret) + goto out;PARSE_LIST (sort, c2c_hists__init_sort); +if (ret) + goto out;/* copy sort keys to output fields */ perf_hpp__setup_output_field (hpp_list);
@@ -2111,6 +2121,7 @@ static int hpp_list__parse(struct perf_hpp_list *hpp_list,perf_hpp__append_sort_keys (&hists->list);#endif +out:free (output);free (sort);return ret;
@@ -2281,6 +2292,7 @@ static int resort_cl_cb(struct hist_entry *he, void *arg)struct c2c_hist_entry *c2c_he;struct c2c_hists *c2c_hists;bool display =he__display (he, &c2c.shared_clines_stats); +int ret; c2c_he = container_of(he, struct c2c_hist_entry, he); c2c_hists = c2c_he->hists;
@@ -2291,7 +2303,9 @@ static int resort_cl_cb(struct hist_entry *he, void *arg) c2c_he->cacheline_idx = idx++;calc_width (c2c_he); -c2c_hists__reinit (c2c_hists, c2c.cl_output, c2c.cl_resort, env); + ret =c2c_hists__reinit (c2c_hists, c2c.cl_output, c2c.cl_resort, env); +if (ret) +return ret;hists__collapse_resort (&c2c_hists->hists,NULL );hists__output_resort_cb (&c2c_hists->hists,NULL , filter_cb);
@@ -3356,13 +3370,19 @@ static int perf_c2c__report(int argc, const char **argv)else if (c2c.display ==DISPLAY_SNP_PEER ) sort_str ="tot_peer" ; -c2c_hists__reinit (&c2c.hists, output_str, sort_str,perf_session__env (session)); + err =c2c_hists__reinit (&c2c.hists, output_str, sort_str,perf_session__env (session)); +if (err) { +pr_err ("Failed to reinitialize hists\n" ); + goto out_mem2node; + }ui_progress__init (&prog, c2c.hists.hists.nr_entries,"Sorting..." );hists__collapse_resort (&c2c.hists.hists,NULL );hists__output_resort_cb (&c2c.hists.hists, &prog, resort_shared_cl_cb); -hists__iterate_cb (&c2c.hists.hists, resort_cl_cb,perf_session__env (session)); + err =hists__iterate_cb (&c2c.hists.hists, resort_cl_cb,perf_session__env (session)); +if (err) + goto out_mem2node;ui_progress__finish ();
Signature · 1 line
2.55.0
[email protected]
AI reviewRe: [PATCH 1/2] perf c2c: Fix error masking, OOM, and unchecked caller errors in hpp_list__parse()
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:
- [Medium] The newly added error handling if (ret) goto out; is bypassed because the PARSE_LIST macro silently masks non-terminal errors (like OOM) from individual tokens, undermining the commit's intended fix.
Pre-existing issues:
- [Low] Missing explicit POSIX standard header inclusions for libc functions. --
--- Patch [1]: [PATCH 1/2] perf c2c: Fix error masking, OOM, and unchecked caller errors in hpp_list__parse() --- commit 840153db94480f61d734e3e7494d6e8309bb2f8f
Author: Arnaldo Carvalho de Melo <[email protected]>
perf c2c: Fix error masking, OOM, and unchecked caller errors in hpp_list__parse()
This commit addresses error handling bugs in hpp_list__parse() where output parsing errors were silently overwritten and OOM conditions for strdup() were ignored. It also fixes unhandled return values from c2c_hists__reinit() and hists__iterate_cb() in the calling functions.
tools/perf/builtin-c2c.c static int hpp_list__parse(struct perf_hpp_list *hpp_list, · line 2100
Quoted 13 lines
tools/perf/builtin-c2c.c +5
-0@@ -2093,8 +2093,18 @@ static int hpp_list__parse(struct perf_hpp_list *hpp_list,char *sort = sort_ ?strdup (sort_) :NULL ;int ret; +/* strdup() returns NULL on OOM, don't silently treat as empty */ +if ((output_ && !output) || (sort_ && !sort)) { + ret = -ENOMEM ; + goto out; + }
[Severity: Low]
This is a pre-existing issue, but does this file correctly include the POSIX standard headers for the libc functions it uses? Functions like strdup() and free() require <string.h> and <stdlib.h>. Relying on implicit inclusion can cause build failures on musl libc systems.
tools/perf/builtin-c2c.c static int hpp_list__parse(struct perf_hpp_list *hpp_list, · line 2104
Quoted 3 lines
tools/perf/builtin-c2c.c +2
-0PARSE_LIST (output, c2c_hists__init_output); +if (ret) + goto out;
[Severity: Medium]
Will this properly catch all errors from the parsing callback? Looking at the PARSE_LIST macro definition:
ret = _fn(hpp_list, tok, env);
if (ret == -EINVAL) {
pr_err("Invalid --fields key: '%s'", tok);
break;
} else if (ret == -ESRCH) {
pr_err("Unknown --fields key: '%s'", tok);
break;
}If the callback returns an error like -1 due to an allocation failure on a token that isn't the last in the list, the loop won't break. Does this mean subsequent iterations could succeed and overwrite ret to 0, causing the newly added if (ret) goto out; check to be bypassed?
Quoted 4 lines
tools/perf/builtin-c2c.c +2
-0PARSE_LIST (sort, c2c_hists__init_sort); +if (ret) + goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Arnaldo Carvalho de Melo
maintainerPATCH v2 2/2 perf c2c: Clean up registered formats on c2c_hists__init() failure
In reply to Arnaldo Carvalho de MeloFrom: Arnaldo Carvalho de Melo <[email protected]>
When c2c_hists__init() calls hpp_list__parse() and it fails partway through, format structures registered via perf_hpp_list__column_register() and perf_hpp_list__register_sort_field() are left on the hpp_list. The c2c_he__alloc_hists() caller handles this with perf_hpp__reset_output_field(),
but perf_c2c_report() does not, leaking the partially registered entries.
Fix by adding cleanup inside c2c_hists__init() itself, so all callers are protected.
--- tools/perf/builtin-c2c.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
tools/perf/builtin-c2c.c +9 -1
@@ -2132,6 +2132,8 @@ static int c2c_hists__init(struct c2c_hists *hists,int nr_header_lines, struct perf_env *env) { + int ret; +__hists__init (&hists->hists, &hists->list);/*
@@ -2144,7 +2146,13 @@ static int c2c_hists__init(struct c2c_hists *hists,/* Overload number of header lines.*/ hists->list.nr_header_lines = nr_header_lines; -return hpp_list__parse (&hists->list,/*output=*/ NULL , sort, env); + ret =hpp_list__parse (&hists->list,/*output=*/ NULL , sort, env); + +/* Unregister any formats added before the failure point */ +if (ret) +perf_hpp__reset_output_field (&hists->list); + +return ret; } static int c2c_hists__reinit(struct c2c_hists *c2c_hists,
Signature · 1 line
2.55.0