Skip to contentSkip to conversation

tracing: Reject .function on the function trace event

PATCH Superseded David Carlier 4 participants 4 messages Nov 14 – Nov 18

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Reject .function on the function trace event

For the function event, parse_pred() handles the .function suffix before the ftrace_event_is_function() case, so "ip.function == schedule" builds a predicate with no regex. Setting it through perf then dereferences the NULL pred->regex in ftrace_function_set_filter_pred().

The "ip" field of that event already takes a function name, so reject the suffix there.

Fixes e6745a4da964 ("tracing: Add a way to filter function addresses to function names")
---
 kernel/trace/trace_events_filter.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_events_filter.c +6 -0
index c10951bcfc5f..11bb751c4388 100644
@@ -1743,6 +1743,12 @@ static int parse_pred(const char *str, void *data,
 	pred->op = op;
 
 	if (function) {
+		/* Function events already take a function name on 'ip' */
+		if (ftrace_event_is_function(call)) {
+			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
+			goto err_free;
+		}
+
 		/* The field must be the same size as long */
 		if (field->size != sizeof(long)) {
 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Critical] tracing: out-of-bounds read in…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Critical] tracing: out-of-bounds read in parse_pred() on empty function filter operand [1]
--

commit e67c952556cf8df0fc74a91895fdbea1c0dd54b5
Author: David Carlier <[email protected]>

tracing: Reject .function on the function trace event

This commit rejects the .function filter suffix on the function trace event so that parse_pred() does not build a predicate with a NULL regex.

Quoted 7 lines
kernel/trace/trace_events_filter.c +0 -0
index 5e74d2dae40a3..9c807191166ba 100644
@@ -1744,6 +1744,12 @@ static int parse_pred(const char *str, void *data,
 	pred->op = op;
 

kernel/trace/trace_events_filter.c static int parse_pred(const char *str, void *data, · line 1755

Quoted 10 lines
kernel/trace/trace_events_filter.c +6 -0
 	if (function) {
+		/* Function events already take a function name on 'ip' */
+		if (ftrace_event_is_function(call)) {
+			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
+			goto err_free;
+		}
+
 		/* The field must be the same size as long */
 		if (field->size != sizeof(long)) {
 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);

[Severity: Critical]
This problem wasn't introduced by this patch, but can the paired ftrace_event_is_function(call) branch in parse_pred() read past the end of str [1] when the filter has no operand after the operator (for example, "ip ==")?

kernel/trace/trace_events_filter.c:parse_pred() {
	...
	} else if (ftrace_event_is_function(call)) {
		...
		if (str[i] == '\'' || str[i] == '"')
			q = str[i];
		else
			q = 0;

		for (i++; str[i]; i++) {
	...
}

When str ends after the operator and optional whitespace, str[i] is the terminating NUL byte and q is set to 0. The loop initializer i++ then advances i one byte past the NUL terminator before checking str[i], so parse_pred() scans out-of-bounds slab memory, copies it into pred->regex->pattern, and returns the out-of-bounds offset to predicate_parse().

[1] https://sashiko.dev/#/bug/linux-911099f1-cea5-4f84-ba41-25fbcbb66dad

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to David Carlier
Quoted 1 line · On Sat, 14 Nov 2026 19:59:14 +0000 David Carlier <[email protected]> wrote:
Fixes e6745a4da964 ("tracing: Add a way to filter function addresses to function names")

regex was an embedded struct in filter_pred until cfb58e278c6a ("tracing/filters: Dynamically allocate filter_pred.regex"). Before that the pattern came out of the zeroed predicate empty and __ftrace_function_set_filter() returned -EINVAL, so cfb58e278c6a looks like the tag you want.

maintainer
#3 lore raw
In reply to David Carlier
Quoted 22 lines · On Sat, 14 Nov 2026 19:59:14 +0000 David Carlier <[email protected]> wrote:

For the function event, parse_pred() handles the .function suffix before the ftrace_event_is_function() case, so "ip.function == schedule" builds a predicate with no regex. Setting it through perf then dereferences the NULL pred->regex in ftrace_function_set_filter_pred().

The "ip" field of that event already takes a function name, so reject the suffix there.

Fixes e6745a4da964 ("tracing: Add a way to filter function addresses to function names")
---
 kernel/trace/trace_events_filter.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/trace/trace_events_filter.c +0 -0
index c10951bcfc5f..11bb751c4388 100644
@@ -1743,6 +1743,12 @@ static int parse_pred(const char *str, void *data,
 	pred->op = op;
 

kernel/trace/trace_events_filter.c static int parse_pred(const char *str, void *data, · line 1754

Quoted 10 lines
kernel/trace/trace_events_filter.c +6 -0
 	if (function) {
+		/* Function events already take a function name on 'ip' */
+		if (ftrace_event_is_function(call)) {
+			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);
+			goto err_free;
+		}
+
 		/* The field must be the same size as long */
 		if (field->size != sizeof(long)) {
 			parse_error(pe, FILT_ERR_ILLEGAL_FIELD_OP, pos + i);

Instead of rejecting it, why not just ignore it?

kernel/trace/trace_events_filter.c +3 -1
index a1c298210059..e64c44ab3ee5 100644
@@ -1742,7 +1742,9 @@ static int parse_pred(const char *str, void *data,
 
 	/* See if the field is a kernel function name */
 	if ((len = str_has_prefix(str + i, ".function"))) {
-		function = true;
+		/* Ignore if event is already a function */
+		if (!ftrace_event_is_function(call))
+			function = true;
 		i += len;
 	}
 

The above fixes the issue as well, and allows things to work as expected.

-- Steve