* [PATCH] tracing: Reject filter commands in perf function event filters
@ 2026-11-25 6:04 Jaeyeong Lee
2026-11-27 18:03 ` Steven Rostedt
0 siblings, 1 reply; 2+ messages in thread
From: Jaeyeong Lee @ 2026-11-25 6:04 UTC (permalink / raw)
To: Steven Rostedt, Masami Hiramatsu
Cc: Mathieu Desnoyers, linux-trace-kernel, linux-kernel,
Jaeyeong Lee, stable
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")
Cc: stable@vger.kernel.org
Signed-off-by: Jaeyeong Lee <lee@jaeyeong.cc>
---
kernel/trace/trace_events_filter.c | 3 +++
1 file changed, 3 insertions(+)
diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
index a9d77df3e..da48210a6 100644
--- a/kernel/trace/trace_events_filter.c
+++ b/kernel/trace/trace_events_filter.c
@@ -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
--
2.43.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [PATCH] tracing: Reject filter commands in perf function event filters
2026-11-25 6:04 [PATCH] tracing: Reject filter commands in perf function event filters Jaeyeong Lee
@ 2026-11-27 18:03 ` Steven Rostedt
0 siblings, 0 replies; 2+ messages in thread
From: Steven Rostedt @ 2026-11-27 18:03 UTC (permalink / raw)
To: Jaeyeong Lee
Cc: Masami Hiramatsu, Mathieu Desnoyers, linux-trace-kernel,
linux-kernel, stable
On Wed, 25 Nov 2026 06:04:44 +0000
Jaeyeong Lee <lee@jaeyeong.cc> wrote:
> 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
Hmm, I think this should be allowed to filter only on modules.
Just not cache the module if it doesn't exist.
>
> 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")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jaeyeong Lee <lee@jaeyeong.cc>
> ---
> kernel/trace/trace_events_filter.c | 3 +++
> 1 file changed, 3 insertions(+)
>
> diff --git a/kernel/trace/trace_events_filter.c b/kernel/trace/trace_events_filter.c
> index a9d77df3e..da48210a6 100644
> --- a/kernel/trace/trace_events_filter.c
> +++ b/kernel/trace/trace_events_filter.c
> @@ -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?
diff --git a/kernel/trace/ftrace.c b/kernel/trace/ftrace.c
index 5b161959e701..6e82941ee2e9 100644
--- a/kernel/trace/ftrace.c
+++ b/kernel/trace/ftrace.c
@@ -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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-27 18:03 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-25 6:04 [PATCH] tracing: Reject filter commands in perf function event filters Jaeyeong Lee
2026-11-27 18:03 ` Steven Rostedt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®