[PATCH v4] tracing: compare named histogram expressions recursively
From: Kyuwon Cho
Date: Sun Dec 06 2026 - 03:07:26 EST
Named histogram matching checks only the outer hist_field properties.
Different expressions with compatible wrapper fields can therefore share
the first trigger expression tree, which can read beyond records of the
second event.
Compare the complete operand trees before sharing named histogram data.
Bound the recursive comparison to the maximum depth allowed by the
expression parser and its modifier and alias wrappers.
Fixes: 100719dcef44 ("tracing: Add simple expression support to hist triggers")
Signed-off-by: Kyuwon Cho <kyuwon.cho@xxxxxxxx>
---
Changes in v4:
- Add the explicit recursion-depth limit requested by Steven Rostedt. The
limit covers three parsed subexpressions plus one modifier or alias wrapper.
- Rebase onto the current trace/for-next tip.
Changes in v3:
- Ignore the presentation-only field name when comparing constant fields,
as identified by the Sashiko review. A folded constant is printed as a
literal and reparsed without a name.
- Rebase onto trace/for-next.
Changes in v2:
- Correct the Fixes trailer to 100719dcef44, which introduced recursive
expression operands, as identified by Sashiko.
v3: https://lore.kernel.org/all/179633347054.69950.8886355849048927258@xxxxxxxx/
v2: https://lore.kernel.org/all/179632407670.3855722.5777623149223641405@xxxxxxxx/
v1: https://lore.kernel.org/all/179620737571.2194197.15463715908322121429@xxxxxxxx/
review reply: https://lore.kernel.org/all/179643157816.1720715.5946320154470436468@xxxxxxxx/
The issue was found by an LLM-guided test generator that we are
developing as a research prototype.
A reproducer is available on request.
kernel/trace/trace_events_hist.c | 72 ++++++++++++++++++++++++--------
1 file changed, 54 insertions(+), 18 deletions(-)
diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
index d4702ad4e003..94928d8f4919 100644
--- a/kernel/trace/trace_events_hist.c
+++ b/kernel/trace/trace_events_hist.c
@@ -92,6 +92,7 @@ typedef u64 (*hist_field_fn_t) (struct hist_field *field,
void *event);
#define HIST_FIELD_OPERANDS_MAX 2
+#define HIST_FIELD_DEPTH_MAX 4
#define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX)
#define HIST_ACTIONS_MAX 8
#define HIST_DIV_SHIFT 20 /* For optimizing division by constants */
@@ -6474,6 +6475,57 @@ static bool compatible_field(struct ftrace_event_field *field,
return true;
}
+static bool compatible_optional_string(const char *str, const char *test_str)
+{
+ if (!str || !test_str)
+ return str == test_str;
+
+ return strcmp(str, test_str) == 0;
+}
+
+static bool compatible_hist_field(struct hist_field *field,
+ struct hist_field *test_field,
+ unsigned int level)
+{
+ unsigned int i;
+
+ /* Three parsed subexpressions plus one modifier or alias wrapper. */
+ if (level > HIST_FIELD_DEPTH_MAX)
+ return false;
+
+ if (field->flags != test_field->flags ||
+ field->fn_num != test_field->fn_num ||
+ !compatible_field(field->field, test_field->field) ||
+ field->offset != test_field->offset ||
+ field->size != test_field->size ||
+ field->is_signed != test_field->is_signed ||
+ field->buckets != test_field->buckets ||
+ field->operator != test_field->operator ||
+ field->constant != test_field->constant ||
+ !compatible_optional_string(field->type, test_field->type) ||
+ (!(field->flags & HIST_FIELD_FL_CONST) &&
+ !compatible_optional_string(field->name, test_field->name)) ||
+ !compatible_optional_string(field->var.name,
+ test_field->var.name) ||
+ !compatible_optional_string(field->system, test_field->system) ||
+ !compatible_optional_string(field->event_name,
+ test_field->event_name))
+ return false;
+
+ for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) {
+ if (!field->operands[i] || !test_field->operands[i]) {
+ if (field->operands[i] != test_field->operands[i])
+ return false;
+ continue;
+ }
+ if (!compatible_hist_field(field->operands[i],
+ test_field->operands[i], level + 1))
+ return false;
+ }
+
+ return true;
+}
+
static bool hist_trigger_match(struct event_trigger_data *data,
struct event_trigger_data *data_test,
struct event_trigger_data *named_data,
@@ -6481,7 +6533,6 @@ static bool hist_trigger_match(struct event_trigger_data *data,
{
struct tracing_map_sort_key *sort_key, *sort_key_test;
struct hist_trigger_data *hist_data, *hist_data_test;
- struct hist_field *key_field, *key_field_test;
unsigned int i;
if (named_data && (named_data != data_test) &&
@@ -6506,23 +6557,8 @@ static bool hist_trigger_match(struct event_trigger_data *data,
}
for_each_hist_field(i, hist_data) {
- key_field = hist_data->fields[i];
- key_field_test = hist_data_test->fields[i];
-
- if (key_field->flags != key_field_test->flags)
- return false;
- if (!compatible_field(key_field->field, key_field_test->field))
- return false;
- if (key_field->offset != key_field_test->offset)
- return false;
- if (key_field->size != key_field_test->size)
- return false;
- if (key_field->is_signed != key_field_test->is_signed)
- return false;
- if (!!key_field->var.name != !!key_field_test->var.name)
- return false;
- if (key_field->var.name &&
- strcmp(key_field->var.name, key_field_test->var.name) != 0)
+ if (!compatible_hist_field(hist_data->fields[i],
+ hist_data_test->fields[i], 0))
return false;
}
--
2.43.0