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,
Matt Turner <mattst88@gmail.com>
Subject: [PATCH v8 3/3] perf tools gtk: fix two hierarchy-view stack buffer overflows
Date: Tue, 08 Sep 2026 22:50:36 -0400 [thread overview]
Message-ID: <20260908-perf-gtk2-v8-3-e90d5d155f0d@gmail.com> (raw)
In-Reply-To: <20260908-perf-gtk2-v8-0-e90d5d155f0d@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.
That same loop also saves bf/size at the top of each iteration but
only restored hpp->buf/hpp->size to them before recursing into
non-leaf children. Leaf entries left the buffer state advanced from
the format loop, so the next sibling in the traversal inherited a
shrunk hpp->size and an already-advanced hpp->buf, eventually running
hpp->size down to 0 and pointing bf past the end of the stack buffer
for the strim(bf) call.
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, and by restoring
hpp->buf/hpp->size unconditionally after formatting each entry instead
of only before recursing.
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 | 29 +++++++++++++++++++++--------
1 file changed, 21 insertions(+), 8 deletions(-)
diff --git a/tools/perf/ui/gtk/hists.c b/tools/perf/ui/gtk/hists.c
index 716dcf02bd0e..80df3fec8ea1 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,15 +457,26 @@ 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);
- if (!he->leaf) {
- hpp->buf = bf;
- hpp->size = size;
+ hpp->buf = bf;
+ hpp->size = size;
+ if (!he->leaf) {
perf_gtk__add_hierarchy_entries(hists, &he->hroot_out,
store, &iter, hpp,
min_pcnt);
@@ -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-09 2:50 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-09 2:50 [PATCH v8 0/3] perf tools: port UI from GTK2 to GTK4 Matt Turner
2026-09-09 2:50 ` [PATCH v8 1/3] tools: port perf ui from GTK 2 to GTK 4 Matt Turner
2026-09-09 2:50 ` [PATCH v8 2/3] perf tools: make the GTK4 report browser actually loadable at runtime Matt Turner
2026-09-09 2:50 ` Matt Turner [this message]
2026-09-09 11:13 ` [PATCH v8 0/3] perf tools: port UI from GTK2 to GTK4 Arnaldo Carvalho de Melo
2026-09-09 11:25 ` Arnaldo Carvalho de Melo
2026-09-09 11:29 ` Arnaldo Carvalho de Melo
2026-09-09 11:35 ` Arnaldo Carvalho de Melo
2026-09-09 11:43 ` Arnaldo Carvalho de Melo
2026-09-09 11:52 ` Arnaldo Carvalho de Melo
2026-09-09 20:02 ` Arnaldo Carvalho de Melo
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=20260908-perf-gtk2-v8-3-e90d5d155f0d@gmail.com \
--to=mattst88@gmail.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=alexander.shishkin@linux.intel.com \
--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
all inboxes | Powered by JetHome®