Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Jaeyeong Lee <lee@jaeyeong.cc>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: Re: [PATCH] tracing: Reject filter commands in perf function event filters
Date: Fri, 27 Nov 2026 13:03:46 -0500	[thread overview]
Message-ID: <20261127130346.2caf63af@gandalf.local.home> (raw)
In-Reply-To: <20261125060411.445-1-lee@jaeyeong.cc>

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

      reply	other threads:[~2026-11-27 18:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261127130346.2caf63af@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=lee@jaeyeong.cc \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox