Linux Trace Kernel
 help / color / mirror / Atom feed
* [PATCH v2] perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER
@ 2026-11-24 18:06 Dominik Maier
  2026-11-26 15:40 ` Peter Zijlstra
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Maier @ 2026-11-24 18:06 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim
  Cc: Mark Rutland, Steven Rostedt, Masami Hiramatsu, linux-perf-users,
	linux-trace-kernel, dmnk+artist

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER
  2026-11-24 18:06 [PATCH v2] perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER Dominik Maier
@ 2026-11-26 15:40 ` Peter Zijlstra
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Zijlstra @ 2026-11-26 15:40 UTC (permalink / raw)
  To: Dominik Maier
  Cc: Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Mark Rutland,
	Steven Rostedt, Masami Hiramatsu, linux-perf-users,
	linux-trace-kernel, dmnk+artist

On Tue, Nov 24, 2026 at 06:06:11PM +0000, Dominik Maier wrote:
> 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.

Does this want a Fixes tag? Or should I stick this in perf/core for the
next cycle?

>  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
> 

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26 15:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:06 [PATCH v2] perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER Dominik Maier
2026-11-26 15:40 ` Peter Zijlstra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox