From: Matt Turner <mattst88@gmail.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@kernel.org>, Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>
Cc: linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org,
bpf@vger.kernel.org, Matt Turner <mattst88@gmail.com>
Subject: [PATCH v7 3/3] perf tools gtk: fix two hierarchy-view stack buffer overflows
Date: Sun, 06 Sep 2026 16:13:08 -0400 [thread overview]
Message-ID: <20260906-perf-gtk2-v7-3-1ece839fbca0@gmail.com> (raw)
In-Reply-To: <20260906-perf-gtk2-v7-0-1ece839fbca0@gmail.com>
perf_gtk__show_hierarchy() builds a merged column header for the
hierarchy view with unbounded strcat() calls into a 512-byte stack
buffer. The pieces being appended come from tracepoint field names and
sort-key headers in perf.data, so a file with enough dynamic sort keys
or long enough field names overflows the buffer.
perf_gtk__add_hierarchy_entries() has a related bug in the loop that
formats each entry's value columns. fmt->entry()/fmt->color() return
via scnprintf(), so ret is clamped to at most hpp->size - 1, but
advance_hpp(hpp, ret + 2) doesn't clamp: when ret hits that maximum,
ret + 2 exceeds hpp->size by one, and hpp->size (size_t) underflows to
roughly SIZE_MAX. The next iteration's fmt->entry() then writes into
the caller's stack buffer using that bogus size, a second overflow.
Fix the header builder by tracking the write offset and using
scnprintf() for each append, same pattern already used elsewhere in
this file. Fix the entry loop by clamping the amount passed to
advance_hpp() to what's actually left in the buffer.
Both bugs predate the perf GTK UI's move to GTK 4; neither function is
touched by that port.
Signed-off-by: Matt Turner <mattst88@gmail.com>
---
tools/perf/ui/gtk/hists.c | 23 ++++++++++++++++++-----
1 file changed, 18 insertions(+), 5 deletions(-)
diff --git a/tools/perf/ui/gtk/hists.c b/tools/perf/ui/gtk/hists.c
index 716dcf02bd0e..342d4c3fecb0 100644
--- a/tools/perf/ui/gtk/hists.c
+++ b/tools/perf/ui/gtk/hists.c
@@ -449,7 +449,7 @@ static void perf_gtk__add_hierarchy_entries(struct hists *hists,
bf = hpp->buf;
size = hpp->size;
perf_hpp_list__for_each_format(he->hpp_list, fmt) {
- int ret;
+ int ret, inc;
if (fmt->color)
ret = fmt->color(fmt, hpp, he);
@@ -457,7 +457,18 @@ static void perf_gtk__add_hierarchy_entries(struct hists *hists,
ret = fmt->entry(fmt, hpp, he);
snprintf(hpp->buf + ret, hpp->size - ret, " ");
- advance_hpp(hpp, ret + 2);
+ /*
+ * ret can be as large as hpp->size - 1, so ret + 2
+ * can exceed hpp->size. advance_hpp() doesn't clamp,
+ * so passing that through would underflow the
+ * size_t hpp->size and let a later fmt->entry() in
+ * this loop write past the end of the caller's
+ * stack buffer.
+ */
+ inc = ret + 2;
+ if (inc > (int)hpp->size)
+ inc = hpp->size;
+ advance_hpp(hpp, inc);
}
gtk_tree_store_set(store, &iter, col_idx, strim(bf), -1);
@@ -505,6 +516,7 @@ static void perf_gtk__show_hierarchy(GtkWidget *window, struct hists *hists,
GtkWidget *view;
int col_idx;
int nr_cols = 0;
+ int ret;
char s[512];
char buf[512];
bool first_node, first_col;
@@ -541,9 +553,10 @@ static void perf_gtk__show_hierarchy(GtkWidget *window, struct hists *hists,
/* construct merged column header since sort keys share single column */
buf[0] = '\0';
first_node = true;
+ ret = 0;
list_for_each_entry_continue(fmt_node, &hists->hpp_formats, list) {
if (!first_node)
- strcat(buf, " / ");
+ ret += scnprintf(buf + ret, sizeof(buf) - ret, " / ");
first_node = false;
first_col = true;
@@ -552,11 +565,11 @@ static void perf_gtk__show_hierarchy(GtkWidget *window, struct hists *hists,
continue;
if (!first_col)
- strcat(buf, "+");
+ ret += scnprintf(buf + ret, sizeof(buf) - ret, "+");
first_col = false;
fmt->header(fmt, &hpp, hists, 0, NULL);
- strcat(buf, strim(hpp.buf));
+ ret += scnprintf(buf + ret, sizeof(buf) - ret, "%s", strim(hpp.buf));
}
}
--
2.54.0
next prev parent reply other threads:[~2026-09-06 20:13 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-06 20:13 [PATCH v7 0/3] perf tools: port UI from GTK2 to GTK4 Matt Turner
2026-09-06 20:13 ` [PATCH v7 1/3] tools: port perf ui from GTK 2 to GTK 4 Matt Turner
2026-09-06 20:25 ` sashiko-bot
2026-09-06 20:52 ` Matt Turner
2026-09-06 20:13 ` [PATCH v7 2/3] perf tools: make the GTK4 report browser actually loadable at runtime Matt Turner
2026-09-06 20:23 ` sashiko-bot
2026-09-06 20:13 ` Matt Turner [this message]
2026-09-06 20:25 ` [PATCH v7 3/3] perf tools gtk: fix two hierarchy-view stack buffer overflows sashiko-bot
2026-09-06 20:59 ` [PATCH v7 0/3] perf tools: port UI from GTK2 to GTK4 Alexei Starovoitov
2026-09-06 21:05 ` Matt Turner
2026-09-08 17:28 ` Ian Rogers
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260906-perf-gtk2-v7-3-1ece839fbca0@gmail.com \
--to=mattst88@gmail.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--cc=bpf@vger.kernel.org \
--cc=irogers@google.com \
--cc=james.clark@linaro.org \
--cc=jolsa@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox