Linux Trace Kernel
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@redhat.com>,
	 Arnaldo Carvalho de Melo <acme@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>,
	Steven Rostedt <rostedt@goodmis.org>,
	 Masami Hiramatsu <mhiramat@kernel.org>,
	linux-perf-users@vger.kernel.org,
	 linux-trace-kernel@vger.kernel.org, dmnk+artist@google.com
Subject: [PATCH v2] perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER
Date: Tue, 24 Nov 2026 18:06:11 +0000	[thread overview]
Message-ID: <20261124180611.75836-1-dmnk@google.com> (raw)

Under perf_event_paranoid = 2, an unprivileged caller can open a
task-scoped counting trace event (including tracepoints and tracefs
uprobes) with attr.exclude_kernel = 1 and attach trace event filters via
PERF_EVENT_IOC_SET_FILTER without passing perf_allow_kernel(). Trace
event filter predicates are parsed by create_filter() and support kernel
symbol and address matching (such as the .function modifier), exposing
kernel address information to callers that are not allowed kernel
profiling.

Require perf_allow_kernel() before applying trace event filters in
perf_event_set_filter().

Suggested-by: Steven Rostedt <rostedt@goodmis.org>
Signed-off-by: Dominik Maier <dmnk@google.com>
---
v2:
- Gate trace event filtering in perf_event_set_filter() with
  perf_allow_kernel() instead of only checking kallsyms_show_value() on
  numeric .function predicates in trace_events_filter.c (Steven Rostedt).
- v1: https://lore.kernel.org/linux-trace-kernel/20261119192233.2664654-1-dmnk@google.com/

Found with AI-assisted code review; tested in QEMU across root and
unprivileged callers at perf_event_paranoid = 1 and 2, and build-tested
with CONFIG_EVENT_TRACING both enabled and disabled.
 kernel/events/core.c | 36 +++++++++++++++++++++---------------
 1 file changed, 21 insertions(+), 15 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index e870f12c3ad8..e053de4c164c 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -12371,22 +12371,28 @@ static int perf_event_set_filter(struct perf_event *event, void __user *arg)
 
 #ifdef CONFIG_EVENT_TRACING
 	if (perf_event_is_tracing(event)) {
-		struct perf_event_context *ctx = event->ctx;
+		ret = perf_allow_kernel();
+		if (!ret) {
+			struct perf_event_context *ctx = event->ctx;
 
-		/*
-		 * Beware, here be dragons!!
-		 *
-		 * the tracepoint muck will deadlock against ctx->mutex, but
-		 * the tracepoint stuff does not actually need it. So
-		 * temporarily drop ctx->mutex. As per perf_event_ctx_lock() we
-		 * already have a reference on ctx.
-		 *
-		 * This can result in event getting moved to a different ctx,
-		 * but that does not affect the tracepoint state.
-		 */
-		mutex_unlock(&ctx->mutex);
-		ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
-		mutex_lock(&ctx->mutex);
+			/*
+			 * Beware, here be dragons!!
+			 *
+			 * the tracepoint muck will deadlock against ctx->mutex,
+			 * but the tracepoint stuff does not actually need it.
+			 * So temporarily drop ctx->mutex. As per
+			 * perf_event_ctx_lock() we already have a reference on
+			 * ctx.
+			 *
+			 * This can result in event getting moved to a different
+			 * ctx, but that does not affect the tracepoint state.
+			 */
+			mutex_unlock(&ctx->mutex);
+			ret = ftrace_profile_set_filter(event,
+							event->attr.config,
+							filter_str);
+			mutex_lock(&ctx->mutex);
+		}
 	} else
 #endif
 	if (has_addr_filter(event))
-- 
2.56.1.831.g720f3f5937-goog


                 reply	other threads:[~2026-11-24 18:06 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=20261124180611.75836-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=acme@kernel.org \
    --cc=dmnk+artist@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mark.rutland@arm.com \
    --cc=mhiramat@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.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