mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jaeyeong Lee <lee@jaeyeong.cc>
To: Steven Rostedt <rostedt@goodmis.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Jaeyeong Lee <lee@jaeyeong.cc>,
	stable@vger.kernel.org
Subject: [PATCH] tracing: Reject filter commands in perf function event filters
Date: Wed, 25 Nov 2026 06:04:44 +0000	[thread overview]
Message-ID: <20261125060411.445-1-lee@jaeyeong.cc> (raw)

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



                 reply	other threads:[~2026-11-25  6:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261125060411.445-1-lee@jaeyeong.cc \
    --to=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=rostedt@goodmis.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

all inboxes | Powered by JetHome®