[PATCH v8 3/3] perf tools gtk: fix two hierarchy-view stack buffer overflows
From: Matt Turner
Date: Tue Sep 08 2026 - 22:53:23 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.
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@xxxxxxxxx>
---
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