[PATCH v7 3/3] perf tools gtk: fix two hierarchy-view stack buffer overflows
From: Matt Turner
Date: Sun Sep 06 2026 - 16:14:35 EST
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@xxxxxxxxx>
---
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