Enable perf tracing for unprivileged users
Subsystems (2)
v4 cover Enable perf tracing for unprivileged users
Enable users to use perf-trace to trace their own processes, like strace but without the overhead of ptrace(). Ensure that users cannot access other users' or systemwide tracing data.
Changes in v4:
- Preserve security_perf_event_open(PERF_SECURITY_KERNEL) LSM hook in the tp_bypass path.
- Lift the PERF_SAMPLE_IP check out of the tp_bypass path above the PERF_SAMPLE_RAW branch so it applies to counting and sampling. This also allows us to ensure PERF_SAMPLE_IP is set for uprobes.
- Block counting path for TRACE_EVENT_FL_CAP_ANY for unprivileged users with sysctl_perf_event_paranoid > 1.
Changes in v3:
- Don't set PERF_SAMPLE_IP for unprivileged tracepoints. This allows us to exclude PERF_SAMPLE_IP from kaddr_leak without weakening KASLR.
- Mount tracefs as world-traversable so users can access eventfs directories.
Anubhav Shelat (3):
perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
perf: enable unprivileged syscall tracing with perf trace
tracefs: make root directory world-traversable
fs/tracefs/inode.c | 2 +- kernel/events/core.c | 28 +++++++++++++++++++++++++--- kernel/trace/trace_event_perf.c | 21 ++++++++++++++++++++- kernel/trace/trace_events.c | 16 ++++++++++++++-- tools/perf/util/evsel.c | 14 +++++++++++++- 5 files changed, 73 insertions(+), 8 deletions(-)
Signature · 1 line
2.54.0
PATCH v4 1/3 perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
For tracepoint events the IP is a static kernel address.
It doesn't vary by sample and provides no useful information for unprivileged users. Skipping setting PERF_SAMPLE_IP for unprivileged tracepoints avoids exposing a kernel address that reveals the KASLR base offset.
Make an exception for uprobes, which are registered as PERF_TYPE_TRACEPOINT, because the IP is important for their functionality and is a safe userspace address. Detect them with __probe_ip (entry) and __probe_ret_ip (return) using evsel__field().
--- tools/perf/util/evsel.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
tools/perf/util/evsel.c +13 -1
@@ -1509,7 +1509,19 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts, attr->write_backward = opts->overwrite ?1 :0 ; attr->read_format =PERF_FORMAT_LOST ; -evsel__set_sample_bit (evsel,IP ); +/* +* Don't set PERF_SAMPLE_IP for unprivileged kernel tracepoints to +* avoid exposing kernel addresses. Uprobes expose only userspace +* addresses so they're safe. Detect entry and return uprobes. +*/ +if (attr->type !=PERF_TYPE_TRACEPOINT ||perf_event_paranoid_check (1 ) +#ifdef HAVE_LIBTRACEEVENT + ||evsel__field (evsel,"__probe_ip" ) + ||evsel__field (evsel,"__probe_ret_ip" ) +#endif + ) +evsel__set_sample_bit (evsel,IP ); +evsel__set_sample_bit (evsel,TID );if (evsel->sample_read) {
Signature · 1 line
2.54.0
[email protected]
AI reviewRe: [PATCH v4 1/3] perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Uprobes silently lose the `PERF_SAMPLE_IP` bit when `perf` is built without `libtraceevent`…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Uprobes silently lose the PERF_SAMPLE_IP bit when perf is built without libtraceevent support, breaking uprobe profiling for unprivileged users. --
commit 07ebb257f5c9c09215f15379588d99009f389e9e
Author: Anubhav Shelat <[email protected]>
perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
This commit avoids exposing kernel addresses by dropping the PERF_SAMPLE_IP bit for unprivileged tracepoints. It makes an exception for uprobes since they expose safe userspace addresses.
Quoted 3 lines
tools/perf/util/evsel.c +0
-0
[ ... ]
Quoted 7 lines
tools/perf/util/evsel.c +2
-1@@ -1509,7 +1509,19 @@ void evsel__config(struct evsel *evsel, const struct record_opts *opts, attr->write_backward = opts->overwrite ?1 :0 ; attr->read_format =PERF_FORMAT_LOST ; -evsel__set_sample_bit (evsel,IP ); +/* +* Don't set PERF_SAMPLE_IP for unprivileged kernel tracepoints to
tools/perf/util/evsel.c void evsel__config(struct evsel *evsel, const struct record_opts *opts, · line 1523
Quoted 10 lines
tools/perf/util/evsel.c +10
-0+* avoid exposing kernel addresses. Uprobes expose only userspace +* addresses so they're safe. Detect entry and return uprobes. +*/ +if (attr->type !=PERF_TYPE_TRACEPOINT ||perf_event_paranoid_check (1 ) +#ifdef HAVE_LIBTRACEEVENT + ||evsel__field (evsel,"__probe_ip" ) + ||evsel__field (evsel,"__probe_ret_ip" ) +#endif + ) +evsel__set_sample_bit (evsel,IP );
Does this silently drop the PERF_SAMPLE_IP bit for unprivileged uprobes when perf is built without libtraceevent support?
If HAVE_LIBTRACEEVENT is not defined, the uprobe detection logic is compiled out, meaning the condition evaluates to false for unprivileged uprobes.
Is there a fallback detection mechanism that could be used here (such as checking the event or PMU name) to ensure uprobe profiling continues to work for unprivileged users on these builds?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v4 2/3 perf: enable unprivileged syscall tracing with perf trace
In reply to Anubhav ShelatAllow unprivileged users to trace their own processes' syscalls using perf trace, similar to strace without the intrusive overhead of ptrace().
Currently, perf trace requires CAP_PERFMON or paranoid level ≤ 1 even though the kernel has existing infrastructure (TRACE_EVENT_FL_CAP_ANY) specifically designed to mark syscall tracepoints as safe for unprivileged access. To fix this:
1. Loosen the condition in perf_event_open() which requires privileges for all events with exclude_kernel=0. This allows perf_event_open() to bypass the paranoid check for task-attached tracepoint events. Ensure that sample types which can expose kernel addresses to unprivileged users are blocked. Ensure the PERF_SECURITY_KERNEL LSM hook is preserved.
2. Make the format and id tracefs files world-readable only for tracepoints with TRACE_EVENT_FL_CAP_ANY, allowing unprivileged users to see syscall tracepoint ids without exposing sensitive information.
3. Add a check to perf_trace_event_perm() to block PERF_SAMPLE_IP on kernel tracepoints for unprivileged users to prevent KASLR bypass. We do this here rather than in kaddr_leak because perf_trace_event_perm() can distinguish between kernel tracepoints and uprobe tracepoints, where the IP is a safe user space address and is necessary for uprobe functionality.
4. Restrict pure counting events (no PERF_SAMPLE_RAW) to TRACE_EVENT_FL_CAP_ANY tracepoints preventing unprivileged users from counting internal kernel tracepoints while preserving current behavior for exclude_kernel=1 events.
Example usage after this change:
$ perf trace ls # works as unprivileged user $ perf trace # system-wide, still requires privileges $ perf trace -p 1234 # requires ptrace permission on pid 1234
--- kernel/events/core.c | 28 +++++++++++++++++++++++++--- kernel/trace/trace_event_perf.c | 21 ++++++++++++++++++++- kernel/trace/trace_events.c | 16 ++++++++++++++-- 3 files changed, 59 insertions(+), 6 deletions(-)
kernel/events/core.c +25 -3
@@ -13873,9 +13873,31 @@ SYSCALL_DEFINE5(perf_event_open,return err;if (!attr.exclude_kernel) { - err =perf_allow_kernel (); -if (err) -return err; +bool tp_bypass = false; + +/* Check unprivileged tracepoints */ +if (attr.type ==PERF_TYPE_TRACEPOINT && pid !=-1 ) { +/* +* Block sample types that expose kernel addresses to +* prevent KASLR bypass +*/ +u64 kaddr_leak =PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_BRANCH_STACK | +PERF_SAMPLE_ADDR | +PERF_SAMPLE_REGS_INTR ; + + tp_bypass = !(attr.sample_type & kaddr_leak); + } + +if (!tp_bypass) { + err =perf_allow_kernel (); +if (err) +return err; + }else { + err =security_perf_event_open (PERF_SECURITY_KERNEL ); +if (err) +return err; + } }if (attr.namespaces) {
kernel/trace/trace_event_perf.c +20 -1
@@ -72,9 +72,28 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,return -EINVAL ; } +/* +* PERF_SAMPLE_IP on kernel tracepoints exposes a kernel text +* address, weakening KASLR. Block for unprivileged users unless +* the tracepoint is a uprobe (userspace IP, safe to expose). +*/ +if ((p_event->attr.sample_type &PERF_SAMPLE_IP ) && + !p_event->attr.exclude_kernel && + !(tp_event->flags &TRACE_EVENT_FL_UPROBE ) && + sysctl_perf_event_paranoid >1 && !perfmon_capable ()) +return -EACCES ; +/* No tracing, just counting, so no obvious leak */ -if (!(p_event->attr.sample_type &PERF_SAMPLE_RAW )) +if (!(p_event->attr.sample_type &PERF_SAMPLE_RAW )) { +/* Prevent unprivileged users from counting kernel tracepoints */ +if (!p_event->attr.exclude_kernel && + sysctl_perf_event_paranoid >1 && !perfmon_capable ()) { +if (!(p_event->attach_state ==PERF_ATTACH_TASK && + (tp_event->flags &TRACE_EVENT_FL_CAP_ANY ))) +return -EACCES ; + }return 0 ; + }/* Some events are ok to be traced by non-root users... */ if (p_event->attach_state ==PERF_ATTACH_TASK ) {
kernel/trace/trace_events.c +14 -2
@@ -3050,7 +3050,13 @@ static int event_callback(const char *name, umode_t *mode, void **data,struct trace_event_call *call = file->event_call;if (strcmp (name,"format" ) ==0 ) { - *mode =TRACE_MODE_READ ; +/* +* Make format tracefs file world readable for tracepoints with +* TRACE_EVENT_FL_CAP_ANY +*/ + *mode = (call->flags &TRACE_EVENT_FL_CAP_ANY ) ? + (TRACE_MODE_READ |0004 ) : +TRACE_MODE_READ ; *fops = &ftrace_event_format_fops;return 1 ; }
@@ -3086,7 +3092,13 @@ static int event_callback(const char *name, umode_t *mode, void **data,#ifdef CONFIG_PERF_EVENTS if (call->event.type && call->class->reg &&strcmp (name,"id" ) ==0 ) { - *mode =TRACE_MODE_READ ; +/* +* Make id tracefs file world readable for tracepoints with +* TRACE_EVENT_FL_CAP_ANY +*/ + *mode = (call->flags &TRACE_EVENT_FL_CAP_ANY ) ? + (TRACE_MODE_READ |0004 ) : +TRACE_MODE_READ ; *data = (void *)(long )call->event.type; *fops = &ftrace_event_id_fops;return 1 ;
Signature · 1 line
2.54.0
Peter Zijlstra
maintainerRe: [PATCH v4 2/3] perf: enable unprivileged syscall tracing with perf trace
Quoted 26 lines · Anubhav Shelat
Allow unprivileged users to trace their own processes' syscalls using perf trace, similar to strace without the intrusive overhead of
ptrace().Currently, perf trace requires
CAP_PERFMONor paranoid level ≤ 1 even though the kernel has existing infrastructure (TRACE_EVENT_FL_CAP_ANY) specifically designed to mark syscall tracepoints as safe for unprivileged access. To fix this:1. Loosen the condition in
perf_event_open()which requires privileges for all events with exclude_kernel=0. This allowsperf_event_open()to bypass the paranoid check for task-attached tracepoint events. Ensure that sample types which can expose kernel addresses to unprivileged users are blocked. Ensure thePERF_SECURITY_KERNELLSM hook is preserved.2. Make the format and id tracefs files world-readable only for tracepoints with
TRACE_EVENT_FL_CAP_ANY, allowing unprivileged users to see syscall tracepoint ids without exposing sensitive information.3. Add a check to
perf_trace_event_perm()to blockPERF_SAMPLE_IPon kernel tracepoints for unprivileged users to prevent KASLR bypass. We do this here rather than inkaddr_leakbecauseperf_trace_event_perm()can distinguish between kernel tracepoints and uprobe tracepoints, where the IP is a safe user space address and is necessary for uprobe functionality.
Quoted 4 lines
4. Restrict pure counting events (no
PERF_SAMPLE_RAW) toTRACE_EVENT_FL_CAP_ANYtracepoints preventing unprivileged users from counting internal kernel tracepoints while preserving current behavior for exclude_kernel=1 events.
Typically patches are supposed to a single thing, you're listing 4 things. What gives?
Quoted 26 lines
Example usage after this change:
$ perf trace ls # works as unprivileged user $ perf trace # system-wide, still requires privileges $ perf trace -p 1234 # requires ptrace permission on pid 1234Assisted-by Claude:claude-sonnet-4.5Signed-off-by Anubhav Shelat <[email protected]>--- kernel/events/core.c | 28 +++++++++++++++++++++++++--- kernel/trace/trace_event_perf.c | 21 ++++++++++++++++++++- kernel/trace/trace_events.c | 16 ++++++++++++++-- 3 files changed, 59 insertions(+), 6 deletions(-)kernel/events/core.c +2
-3@@ -13873,9 +13873,31 @@ SYSCALL_DEFINE5(perf_event_open,return err;if (!attr.exclude_kernel) { - err =perf_allow_kernel (); -if (err) -return err; +bool tp_bypass = false; +
kernel/events/core.c SYSCALL_DEFINE5(perf_event_open, · line 13887
Quoted 10 lines
kernel/events/core.c +10
-0+/* Check unprivileged tracepoints */ +if (attr.type ==PERF_TYPE_TRACEPOINT && pid !=-1 ) { +/* +* Block sample types that expose kernel addresses to +* prevent KASLR bypass +*/ +u64 kaddr_leak =PERF_SAMPLE_CALLCHAIN | +PERF_SAMPLE_BRANCH_STACK | +PERF_SAMPLE_ADDR | +PERF_SAMPLE_REGS_INTR ;
PERF_SAMPLE_IP should be here too, no?
And I'm not sure if tracepoints can trigger it, but PHYS_ADDR also seems something we shouldn't allow.
And we're sure RAW doesn't include pointers?
Quoted 37 lines
kernel/events/core.c +13
-0+ + tp_bypass = !(attr.sample_type & kaddr_leak); + } + +if (!tp_bypass) { + err =perf_allow_kernel (); +if (err) +return err; + }else { + err =security_perf_event_open (PERF_SECURITY_KERNEL ); +if (err) +return err; + } }if (attr.namespaces) {kernel/trace/trace_event_perf.c +11
-1@@ -72,9 +72,28 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,return -EINVAL ; } +/* +* PERF_SAMPLE_IP on kernel tracepoints exposes a kernel text +* address, weakening KASLR. Block for unprivileged users unless +* the tracepoint is a uprobe (userspace IP, safe to expose). +*/ +if ((p_event->attr.sample_type &PERF_SAMPLE_IP ) && + !p_event->attr.exclude_kernel && + !(tp_event->flags &TRACE_EVENT_FL_UPROBE ) && + sysctl_perf_event_paranoid >1 && !perfmon_capable ()) +return -EACCES ; +/* No tracing, just counting, so no obvious leak */ -if (!(p_event->attr.sample_type &PERF_SAMPLE_RAW ))
kernel/trace/trace_event_perf.c static int perf_trace_event_perm(struct trace_event_call *tp_event, · line 96
Quoted 10 lines
kernel/trace/trace_event_perf.c +9
-0+if (!(p_event->attr.sample_type &PERF_SAMPLE_RAW )) { +/* Prevent unprivileged users from counting kernel tracepoints */ +if (!p_event->attr.exclude_kernel && + sysctl_perf_event_paranoid >1 && !perfmon_capable ()) { +if (!(p_event->attach_state ==PERF_ATTACH_TASK && + (tp_event->flags &TRACE_EVENT_FL_CAP_ANY ))) +return -EACCES ; + }return 0 ; + }
Maybe use less AI and try and type this yourself. I think you'll find that repeating the same clauses over and over gets tiresome. IIRC they invented something for that in the 60s or so :/
Quoted 26 lines
kernel/trace/trace_event_perf.c +0
-0/* Some events are ok to be traced by non-root users... */ if (p_event->attach_state ==PERF_ATTACH_TASK ) {kernel/trace/trace_events.c +7
-2@@ -3050,7 +3050,13 @@ static int event_callback(const char *name, umode_t *mode, void **data,struct trace_event_call *call = file->event_call;if (strcmp (name,"format" ) ==0 ) { - *mode =TRACE_MODE_READ ; +/* +* Make format tracefs file world readable for tracepoints with +* TRACE_EVENT_FL_CAP_ANY +*/ + *mode = (call->flags &TRACE_EVENT_FL_CAP_ANY ) ? + (TRACE_MODE_READ |0004 ) : +TRACE_MODE_READ ; *fops = &ftrace_event_format_fops;return 1 ; }@@ -3086,7 +3092,13 @@ static int event_callback(const char *name, umode_t *mode, void **data,#ifdef CONFIG_PERF_EVENTS if (call->event.type && call->class->reg &&strcmp (name,"id" ) ==0 ) { - *mode =TRACE_MODE_READ ;
kernel/trace/trace_events.c static int event_callback(const char *name, umode_t *mode, void **data, · line 3104
Quoted 10 lines
kernel/trace/trace_events.c +7
-0+/* +* Make id tracefs file world readable for tracepoints with +* TRACE_EVENT_FL_CAP_ANY +*/ + *mode = (call->flags &TRACE_EVENT_FL_CAP_ANY ) ? + (TRACE_MODE_READ |0004 ) : +TRACE_MODE_READ ; *data = (void *)(long )call->event.type; *fops = &ftrace_event_id_fops;return 1 ;
Again, you're doing the same thing in multiple places. If only there was something to re-use a previous expression.
None of this gives me warm and fuzzy feelings.
Re: [PATCH v4 2/3] perf: enable unprivileged syscall tracing with perf trace
Quoted 2 lines · Peter Zijlstra
Typically patches are supposed to a single thing, you're listing 4 things. What gives?
All four changes need to be made together to work properly. The second point could be pulled out as a separate patch, but will be replaced with the eventfs that Steve suggested. The other three points represent a single logical change: selectively loosening the perf_event_open() restrictions without exposing kernel data or breaking uprobe functionality.
Quoted 1 line
PERF_SAMPLE_IPshould be here too, no?
If PERF_SAMPLE_IP is added to the kaddr_leak mask it blocks uprobes, so the PERF_SAMPLE_IP check is in the trace_event_perf.c changes where I can exempt uprobes:
+ if ((p_event->attr.sample_type & PERF_SAMPLE_IP) &&
+ !p_event->attr.exclude_kernel && + !(tp_event->flags & TRACE_EVENT_FL_UPROBE) && + sysctl_perf_event_paranoid > 1 && !perfmon_capable())
+ return -EACCES;
Quoted 2 lines
And I'm not sure if tracepoints can trigger it, but
PHYS_ADDRalso seems something we shouldn't allow.
There's a check for unprivileged access to PHYS_ADDR at core.c:13917 so I didn't add it to kaddr_leak.
Quoted 1 line
And we're sure RAW doesn't include pointers
PERF_SAMPLE_RAW for TRACE_EVENT_FL_CAP_ANY tracepoints doesn't include kernel pointers.
Quoted 4 lines
Again, you're doing the same thing in multiple places. If only there was something to re-use a previous expression.
None of this gives me warm and fuzzy feelings.
You're right. I'll factor the checks out for the next version.
Anubhav
PATCH v4 3/3 tracefs: make root directory world-traversable
In reply to Anubhav ShelatChange the default tracefs mount mode from 0700 to 0755. This allows unprivileged users to access the eventfs directories underneath which already use 0755.
Tracing data files use mode 0440 and 0640 so they are not exposed by this change. Only the format and id files, which have been marked as work-readable, become accessible.
Directory listings of kprobes and uprobes, which contain functions or binaries, become visible to unprivileged users but do not contain kernel addresses. Admins using probes can restore the previous behavior with chmod or mount -o mode=700.
--- fs/tracefs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/tracefs/inode.c +1 -1
@@ -23,7 +23,7 @@#include <linux/slab.h> #include "internal.h" -#define TRACEFS_DEFAULT_MODE 0700 +#define TRACEFS_DEFAULT_MODE 0755static struct kmem_cache *tracefs_inode_cachep__ro_after_init ;static struct vfsmount *tracefs_mount;
Signature · 1 line
2.54.0
Steven Rostedt
maintainerRe: [PATCH v4 3/3] tracefs: make root directory world-traversable
Quoted 4 lines · On Fri, 15 May 2026 15:40:07 -0400 Anubhav Shelat <[email protected]> wrote:
Change the default tracefs mount mode from 0700 to 0755. This allows unprivileged users to access the eventfs directories underneath which already use 0755.
Quoted 9 lines
Tracing data files use mode 0440 and 0640 so they are not exposed by this change. Only the format and id files, which have been marked as work-readable, become accessible.
Directory listings of kprobes and uprobes, which contain functions or binaries, become visible to unprivileged users but do not contain kernel addresses. Admins using probes can restore the previous behavior with chmod or mount -o mode=700.
I've been thinking about this and I believe a better approach is to make a eventfs that is mounted at:
/sys/kernel/events
and be the same directory structure as /sys/kernel/tracing/events but only contain read only files like "id" and "format". This directory would be mounted as 555 and readable by all.
-- Steve