[for-next][PATCH 13/15] tracing/filters: Check for trailing characters after newline in filter string

From: Steven Rostedt

Date: Thu Nov 26 2026 - 11:25:31 EST


From: "Masami Hiramatsu (Google)" <mhiramat@xxxxxxxxxx>

Each trace event or subsystem only holds a single filter, and
append_filter_err() / tracing_log_err() caret placement assumes a
single-line filter string. However, apply_event_filter() and
apply_subsystem_event_filter() only called strstrip(), which strips
trailing newlines but leaves embedded newlines intact.

An embedded newline in a filter string (for example inside 'CPUS{0\n"}')
causes cpulist_parse() / bitmap_parselist() to treat '\n' as the end of
the cpulist and ignore trailing characters before '}', desynchronizing
quote tracking between calc_stack() and parse_pred() and misaligning
error_log caret reporting.

Introduce an internal function filter_check_newline() to convert the
first newline to NUL and check that the rest of the input contains no
non-whitespace characters. If any non-whitespace character follows the
newline, report it as an invalid input error (-EINVAL) without modifying
the input string in-place.

Link: https://patch.msgid.link/179323792832.740114.8136681613581516243.stgit@devnote2
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
Signed-off-by: Steven Rostedt <rostedt@xxxxxxxxxxx>
---
kernel/trace/trace_events_filter.c | 36 ++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)

diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index fcdd4c552569..097b432647fc 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -2445,6 +2445,30 @@ static int process_system_preds(struct trace_subsystem_dir *dir,
return ret;
}

+/*
+ * Check that the rest of the input after the first newline contains no
+ * non-whitespace characters, and convert the newline to '\0'.
+ */
+static int filter_check_newline(char *str)
+{
+ char *nl, *p;
+
+ if (!str)
+ return 0;
+
+ nl = strchr(str, '\n');
+ if (!nl)
+ return 0;
+
+ for (p = nl + 1; *p; p++) {
+ if (!isspace(*p))
+ return -EINVAL;
+ }
+
+ *nl = '\0';
+ return 0;
+}
+
static int create_filter_start(char *filter_string, bool set_str,
struct filter_parse_error **pse,
struct event_filter **filterp)
@@ -2456,6 +2480,10 @@ static int create_filter_start(char *filter_string, bool set_str,
if (WARN_ON_ONCE(*pse || *filterp))
return -EINVAL;

+ err = filter_check_newline(filter_string);
+ if (err)
+ return err;
+
filter = kzalloc_obj(*filter);
if (filter && set_str) {
filter->filter_string = kstrdup(filter_string, GFP_KERNEL);
@@ -2574,6 +2602,10 @@ int apply_event_filter(struct trace_event_file *file, char *filter_string)
if (file->flags & EVENT_FILE_FL_FREED)
return -ENODEV;

+ err = filter_check_newline(filter_string);
+ if (err)
+ return err;
+
if (!strcmp(strstrip(filter_string), "0")) {
filter_disable(file);
filter = event_filter(file);
@@ -2628,6 +2660,10 @@ int apply_subsystem_event_filter(struct trace_subsystem_dir *dir,
if (!dir->nr_events)
return -ENODEV;

+ err = filter_check_newline(filter_string);
+ if (err)
+ return err;
+
if (!strcmp(strstrip(filter_string), "0")) {
filter_free_subsystem_preds(dir, tr);
remove_filter_string(system->filter);
--
2.53.0