Skip to contentSkip to conversation

perf c2c hardening

PATCH Superseded Arnaldo Carvalho de Melo 2 participants 4 messages Aug 2 perf events

v1 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Arnaldo Carvalho de Melo

author maintainer
#0 lore raw

cover perf c2c hardening

Hi,

Please consider merging,

- Arnaldo

Arnaldo Carvalho de Melo (2):
perf c2c: Fix error masking and OOM handling in hpp_list__parse() perf c2c: Clean up registered formats on c2c_hists__init() failure

 tools/perf/builtin-c2c.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 1/2 perf c2c: Fix error masking and OOM handling 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 by checking strdup() return values before proceeding, and preserving the first error across PARSE_LIST calls by checking ret and jumping to the cleanup label.

Fixes 2d388bd0c9d3 ("perf c2c report: Add stdio output support")
Cc Jiri Olsa <[email protected]>
Assisted-by Claude:claude-opus-4.6
---
 tools/perf/builtin-c2c.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
tools/perf/builtin-c2c.c +11 -0
index c9584dbedf77afe8..332d360ca533424b 100644
@@ -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;
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH 1/2] perf c2c: Fix error masking and OOM handling in hpp_list__parse()

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] The patch introduces early exits in `hpp_list__parse` on parsing errors, skipping the critical…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] The patch introduces early exits in hpp_list__parse on parsing errors, skipping the critical perf_hpp__setup_output_field initialization. Callers like c2c_hists__reinit ignore this error, causing histograms to be rendered without necessary output fields.
- [Low] Missing explicit inclusion of <string.h> and <stdlib.h> for POSIX libc functions introduced by the patch.
--

--- Patch [1]: [PATCH 1/2] perf c2c: Fix error masking and OOM handling in hpp_list__parse() --- commit 52205c976b0de26bcbb5609f521d63f4dbe4ebec
Author: Arnaldo Carvalho de Melo <[email protected]>

perf c2c: Fix error masking and OOM handling in hpp_list__parse()

This commit fixes error masking in hpp_list__parse() by checking strdup() return values and preserving the first error across PARSE_LIST calls. It prevents silently treating OOM failures as empty input.

Quoted 4 lines
tools/perf/builtin-c2c.c +0 -0
index c9584dbedf77a..332d360ca5334 100644

[ ... ]

tools/perf/builtin-c2c.c static int hpp_list__parse(struct perf_hpp_list *hpp_list, · line 2093

Quoted 2 lines
tools/perf/builtin-c2c.c +0 -0
@@ -2093,8 +2093,18 @@ static int hpp_list__parse(struct perf_hpp_list *hpp_list,
 	char *sort   = sort_   ? strdup(sort_) : NULL;

[Severity: Low]
Does this file need explicit inclusions for string.h and stdlib.h?

The perf subsystem guidelines require standard POSIX inclusions for musl compatibility when using functions like strdup() and free().

Quoted 7 lines
tools/perf/builtin-c2c.c +5 -0
 	int ret;
 
+	/* strdup() returns NULL on OOM, don't silently treat as empty */
+	if ((output_ && !output) || (sort_ && !sort)) {
+		ret = -ENOMEM;
+		goto out;
+	}

tools/perf/builtin-c2c.c static int hpp_list__parse(struct perf_hpp_list *hpp_list, · line 2110

Quoted 10 lines
tools/perf/builtin-c2c.c +5 -0
+
 	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);

[Severity: Medium]
Does skipping perf_hpp__setup_output_field() leave the output fields list uninitialized when an error occurs?

Callers of hpp_list__parse(), such as resort_cl_cb() and perf_c2c__report(), appear to ignore its return value. For example:

tools/perf/builtin-c2c.c:resort_cl_cb() {
    ...
    c2c_hists__reinit(c2c_hists, c2c.cl_output, c2c.cl_resort, env);
    ...
}
tools/perf/builtin-c2c.c:perf_c2c__report() {
    ...
    c2c_hists__reinit(&c2c.hists, output_str, sort_str, 
                      perf_session__env(session));
    ...
}

If an error is returned due to invalid keys or OOM, the callers proceed with a broken state, which could lead to rendered histograms without necessary output fields.

[ ... ]

Quoted 8 lines
tools/perf/builtin-c2c.c +1 -0
@@ -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;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author maintainer
#3 lore raw

PATCH 2/2 perf c2c: Clean up registered formats on c2c_hists__init() failure

In reply to Arnaldo Carvalho de Melo

From: 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.

Fixes 78b275437873 ("perf c2c report: Add sample processing")
Cc Jiri Olsa <[email protected]>
Assisted-by Claude:claude-opus-4.6
---
 tools/perf/builtin-c2c.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
tools/perf/builtin-c2c.c +9 -1
index 332d360ca533424b..84f0f0ba56131b7b 100644
@@ -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