[PATCH v3 11/16] tracing/filters: Check for trailing characters after newline in filter string
From: Masami Hiramatsu (Google)
Date: Tue Oct 27 2026 - 20:48:01 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).
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@xxxxxxxxxx>
---
Changes in v3:
- Introduce filter_check_newline() to convert the first newline to
NUL and reject any non-whitespace characters after the newline with
an invalid input error (-EINVAL).
---
kernel/trace/trace_events_filter.c | 37 ++++++++++++++++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index 9d1446464058..f3c18324b07a 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -2441,6 +2441,31 @@ static int process_system_preds(struct trace_subsystem_dir *dir,
return -ENOMEM;
}
+/*
+ * Convert the first newline to '\0' and check that the rest of the input
+ * contains no non-whitespace characters.
+ */
+static int filter_check_newline(char *str)
+{
+ char *p;
+
+ if (!str)
+ return 0;
+
+ p = strchr(str, '\n');
+ if (!p)
+ return 0;
+
+ *p++ = '\0';
+ while (*p) {
+ if (!isspace(*p))
+ return -EINVAL;
+ p++;
+ }
+
+ return 0;
+}
+
static int create_filter_start(char *filter_string, bool set_str,
struct filter_parse_error **pse,
struct event_filter **filterp)
@@ -2452,6 +2477,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);
@@ -2570,6 +2599,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);
@@ -2624,6 +2657,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);