Skip to contentSkip to conversation

tracing: Reject filter commands in perf function event filters

PATCH New Jaeyeong Lee 1 participant 1 message Nov 25

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing: Reject filter commands in perf function event filters

The perf function event (ftrace:function) keeps a CPU number in event->ftrace_ops.private: perf_ftrace_function_register() stores nr_cpu_ids there, and the TRACE_REG_PERF_ADD/DEL callbacks store smp_processor_id() and nr_cpu_ids.

Since commit 31f505dc7033 ("ftrace: Implement :mod: cache filtering on kernel command line"), ftrace_set_regex() treats ops->private as a struct trace_array when the pattern contains a ":mod:" command. The "ip" filter of a perf function event is passed unchanged to ftrace_set_filter() and ftrace_set_notrace(), so a filter such as

ip == foo:mod:bar

makes ftrace_set_regex() use the CPU number as a trace_array pointer. When the module is not loaded, ftrace_set_hash() sets FTRACE_HASH_FL_MOD on the filter hash of the new perf event, which is still NULL. If an earlier pattern in the same filter created the hash (for example "ip == vfs_read foo:mod:bar"), cache_mod() then dereferences &tr->mod_trace at a small address instead:

  BUG: kernel NULL pointer dereference, address: 0000000000000018
  RIP: 0010:ftrace_set_hash+0x210/0x260
  Call Trace:
   ftrace_set_regex+0xc0/0x160
   ftrace_profile_set_filter+0x24f/0x340
   _perf_ioctl+0x531/0xb20
   perf_ioctl+0x42/0x70
   __x64_sys_ioctl+0x96/0xe0
   do_syscall_64+0xf9/0x540

  BUG: kernel NULL pointer dereference, address: 00000000000001f2
  RIP: 0010:cache_mod+0xb7/0x1f0
  Call Trace:
   ftrace_set_regex+0x121/0x160
   ftrace_profile_set_filter+0x24f/0x340
   _perf_ioctl+0x531/0xb20

Perf function filters only take function name patterns, and the perf ops has no trace_array to cache module filters in. Reject patterns that contain the ':' command separator before they reach ftrace_set_regex(). This is the result ftrace_set_regex() already returns for such patterns when ops->private is NULL.

Both crashes were reproduced on v7.4-rc4 (8d7fe9885c62) in QEMU/KVM with x86_64_defconfig plus CONFIG_FUNCTION_TRACER, using a test that opens ftrace:function with perf_event_open() and sets the filters above with PERF_EVENT_IOC_SET_FILTER as root. With this change both filters return -EINVAL without a crash, and plain filters such as "ip == vfs_read", "ip != vfs_write" and
"ip == vfs_read || ip == vfs_write" are still accepted.

This bug was found by static analysis.

Fixes 31f505dc7033 ("ftrace: Implement :mod: cache filtering on kernel command line")
---
 kernel/trace/trace_events_filter.c | 3 +++
 1 file changed, 3 insertions(+)
kernel/trace/trace_events_filter.c +3 -0
index a9d77df3e..da48210a6 100644
@@ -2669,6 +2669,9 @@ static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter,
 {
 	int ret;
 
+	if (strnchr(re, len, ':'))
+		return -EINVAL;
+
 	if (filter)
 		ret = ftrace_set_filter(ops, re, len, reset);
 	else
Signature · 1 line
2.43.0