tracing: Reject filter commands in perf function event filters
Subsystems (3)
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.
--- kernel/trace/trace_events_filter.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/trace/trace_events_filter.c +3 -0
@@ -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
Steven Rostedt
maintainerQuoted 5 lines · On Wed, 25 Nov 2026 06:04:44 +0000 Jaeyeong Lee <[email protected]> wrote:
The perf function event (ftrace:function) keeps a CPU number in
event->ftrace_ops.private:perf_ftrace_function_register()storesnr_cpu_idsthere, and the TRACE_REG_PERF_ADD/DEL callbacks storesmp_processor_id()andnr_cpu_ids.
Quoted 7 lines
Since commit
31f505dc7033("ftrace: Implement :mod: cache filtering on kernel command line"),ftrace_set_regex()treatsops->privateas a structtrace_arraywhen the pattern contains a ":mod:" command. The "ip" filter of a perf function event is passed unchanged toftrace_set_filter()andftrace_set_notrace(), so a filter such asip == foo:mod:bar
Hmm, I think this should be allowed to filter only on modules. Just not cache the module if it doesn't exist.
Quoted 53 lines
makes
ftrace_set_regex()use the CPU number as atrace_arraypointer. When the module is not loaded,ftrace_set_hash()setsFTRACE_HASH_FL_MODon 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_readfoo:mod:bar"),cache_mod()then dereferences&tr->mod_traceat 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/0xb20Perf function filters only take function name patterns, and the perf ops has no
trace_arrayto cache module filters in. Reject patterns that contain the ':' command separator before they reachftrace_set_regex(). This is the resultftrace_set_regex()already returns for such patterns whenops->privateis NULL.Both crashes were reproduced on v7.4-rc4 (
8d7fe9885c62) in QEMU/KVM withx86_64_defconfigplusCONFIG_FUNCTION_TRACER, using a test that opens ftrace:function withperf_event_open()and sets the filters above withPERF_EVENT_IOC_SET_FILTERas root. With this change both filters return-EINVALwithout 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")Signed-off-by Jaeyeong Lee <[email protected]>--- kernel/trace/trace_events_filter.c | 3 +++ 1 file changed, 3 insertions(+)kernel/trace/trace_events_filter.c +0
-0
kernel/trace/trace_events_filter.c static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter, · line 2677
Quoted 9 lines
kernel/trace/trace_events_filter.c +3
-0@@ -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
Does this work to fix the problem?
kernel/trace/ftrace.c +8 -4
@@ -6808,9 +6808,6 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,/* This can also handle :mod: parsing */ if (next) { -if (!tr) -return -EINVAL ; - command =strsep (&next,":" );if (strcmp (command,"mod" ) !=0 )return -EINVAL ;
@@ -6850,8 +6847,15 @@ ftrace_set_regex(struct ftrace_ops *ops, unsigned char *buf, int len,int ftrace_set_filter (struct ftrace_ops *ops,unsigned char *buf,int len,int reset) { +void *private = ops->private; +int ret; +ftrace_ops_init (ops); -return ftrace_set_regex (ops, buf, len, reset,1 ); +/* ftrace_set_regex expects private to be a trace_array or NULL */ + ops->private =NULL ; + ret =ftrace_set_regex (ops, buf, len, reset,1 ); + ops->private = private; +return ret; }EXPORT_SYMBOL_GPL (ftrace_set_filter);
-- Steve