Skip to contentSkip to conversation

perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER

PATCH New Dominik Maier 2 participants 2 messages Nov 24

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 perf/core: check perf_allow_kernel() in PERF_EVENT_IOC_SET_FILTER

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().

---
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/[email protected]/

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(-)
kernel/events/core.c +21 -15
index e870f12c3ad8..e053de4c164c 100644
@@ -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))
Signature · 1 line
2.56.1.831.g720f3f5937-goog
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines

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.

Quoted 5 lines

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

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1