Enable perf tracing for unprivileged users
Subsystems (2)
v5 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 v5:
- Move event_define_fields() before directory creation. If event_define_fields() fails then we don't need to cleanup whatever dirs were created.
- New read-only eventfs file system with the same structure as /sys/kernel/tracing/events/ to handle files read by unprivileged users.
- Allow unprivileged users to fall back to /sys/kernel/events/ when they cannot access /sys/kernel/tracing/events/.
- Factor out reused code into helper function that checks if a tracepoint should be restricted in commit 5.
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 (5):
eventfs: define event fields before directory creation
tracefs: add read-only eventfs filesystem at /sys/kernel/events perf tools: fall back to eventfs for unprivileged event discovery perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
perf: enable unprivileged syscall tracing with perf trace
fs/tracefs/event_inode.c | 61 ++++++++++++++++++ fs/tracefs/inode.c | 95 ++++++++++++++++++++++++++- fs/tracefs/internal.h | 3 + include/linux/trace_events.h | 1 + include/linux/tracefs.h | 4 ++ include/uapi/linux/magic.h | 1 + kernel/events/core.c | 28 +++++++- kernel/trace/trace.h | 2 + kernel/trace/trace_event_perf.c | 28 +++++++- kernel/trace/trace_events.c | 100 +++++++++++++++++++++++++++-- tools/lib/api/fs/fs.c | 10 +++ tools/lib/api/fs/fs.h | 1 + tools/lib/api/fs/tracing_path.c | 52 +++++++++++++-- tools/lib/api/fs/tracing_path.h | 1 + tools/perf/util/evsel.c | 14 +++- tools/perf/util/tp_pmu.c | 5 +- tools/perf/util/trace-event-info.c | 19 +++--- 17 files changed, 395 insertions(+), 30 deletions(-)
Signature · 1 line
2.54.0
PATCH v5 1/5 eventfs: define event fields before directory creation
Move the event_define_fields() call in event_create_dir() before the eventfs directory creation. Previously, a failure after directory creation wouldn't clean up eventfs_inode because the error path didn't call eventfs_remove_dir(). This eliminates the need to clean up the eventfs directories if event_define_fields() fails.
--- kernel/trace/trace_events.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)
kernel/trace/trace_events.c +7 -6
@@ -3190,6 +3190,13 @@ event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file)if (WARN_ON_ONCE (strcmp (call->class->system,TRACE_SYSTEM ) ==0 ))return -ENODEV ; + ret =event_define_fields (call); +if (ret <0 ) { +pr_warn ("Could not initialize trace point events/%s\n" , +trace_event_name (call)); +return ret; + } + e_events =event_subsystem_dir (tr, call->class->system, file, parent);if (!e_events)return -ENOMEM ;
@@ -3208,12 +3215,6 @@ event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file) file->ei = ei; - ret =event_define_fields (call); -if (ret <0 ) { -pr_warn ("Could not initialize trace point events/%s\n" , name); -return ret; - } -/* Gets decremented on freeing of the "enable" file */ event_file_get (file);
Signature · 1 line
2.54.0
PATCH v5 2/5 tracefs: add read-only eventfs filesystem at /sys/kernel/events
In reply to Anubhav ShelatIntroduce a new read-only pseudo-filesystem "eventfs" mounted at /sys/kernel/events that exposes trace event format and id files (mode 0444) to unprivileged users. This allows tools like perf to discover event formats without requiring access to the full tracefs/debugfs mount.
The eventfs filesystem reuses the existing eventfs_inode lazy-lookup infrastructure. A new set of super_operations (eventfs_ro_super_operations) shares the tracefs inode allocator so that eventfs_get_inode() and get_tracefs() work on the RO superblock. The superblock is manually initialized to ensure the root inode is allocated with tracefs_alloc_inode, allowing the root to serve directly as the events directory without another events subdirectory.
Each qualifying event gets a subsystem directory containing format and id files. The top-level events directory also exposes header_page and header_event. Similar to tracefs, a change will need to be made in systemd to mount this filesystem automatically.
--- fs/tracefs/event_inode.c | 61 +++++++++++++++++++++++ fs/tracefs/inode.c | 95 +++++++++++++++++++++++++++++++++++- fs/tracefs/internal.h | 3 ++ include/linux/trace_events.h | 1 + include/linux/tracefs.h | 4 ++ include/uapi/linux/magic.h | 1 + kernel/trace/trace.h | 2 + kernel/trace/trace_events.c | 87 +++++++++++++++++++++++++++++++++ 8 files changed, 252 insertions(+), 2 deletions(-)
fs/tracefs/event_inode.c +61 -0
@@ -812,6 +812,67 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentryreturn ERR_PTR (-ENOMEM ); } +/** +* eventfs_create_events_dir_ro - create a read-only events directory +* @name: The name of the top level directory to create. +* @entries: A list of entries that represent the files under this directory +* @size: The number of @entries +* @data: The default data to pass to the files (an entry may override it). +* +* This function configures the eventfs filesystem root as a read-only +* trace event directory using the existing eventfs_inode lazy-lookup +* infrastructure. +* +* See eventfs_create_dir() for use of @entries. +*/ +struct eventfs_inode *eventfs_create_events_dir_ro (const char *name, +const struct eventfs_entry *entries, +int size,void *data) +{ +struct dentry *dentry; +struct eventfs_root_inode *rei; +struct eventfs_inode *ei; +struct tracefs_inode *ti; +struct inode *inode; + + dentry =eventfs_ro_get_root (); +if (IS_ERR (dentry)) +return ERR_CAST (dentry); + + inode =d_inode (dentry); + + ei =alloc_root_ei (name); +if (!ei) + goto fail; + + rei =get_root_inode (ei); + rei->events_dir = dentry; + + ei->entries = entries; + ei->nr_entries = size; + ei->data = data; + +INIT_LIST_HEAD (&ei->children); +INIT_LIST_HEAD (&ei->list); + + ti =get_tracefs (inode); + ti->flags |=TRACEFS_EVENT_INODE ; + ti->private = ei; + + inode->i_op = &eventfs_dir_inode_operations; + inode->i_fop = &eventfs_file_operations; + + dentry->d_fsdata =get_ei (ei); + +return ei; + + fail: +cleanup_ei (ei); +dput (dentry); +eventfs_ro_put_root (); +return ERR_PTR (-ENOMEM ); +} +/** * eventfs_remove_rec - remove eventfs dir or file from list * @ei: eventfs_inode to be removed.
fs/tracefs/inode.c +93 -2
119 lines not on this page · raw message
fs/tracefs/internal.h +3 -0
@@ -73,6 +73,9 @@ struct dentry *tracefs_end_creating(struct dentry *dentry);struct dentry *tracefs_failed_creating (struct dentry *dentry);struct inode *tracefs_get_inode (struct super_block *sb); +struct dentry *eventfs_ro_get_root (void ); +void eventfs_ro_put_root (void ); +void eventfs_remount (struct tracefs_inode *ti,bool update_uid,bool update_gid);void eventfs_d_release (struct dentry *dentry);
include/linux/trace_events.h +1 -0
@@ -648,6 +648,7 @@ struct trace_event_file {struct trace_event_call *event_call;struct event_filter __rcu *filter;struct eventfs_inode *ei; +struct eventfs_inode *ei_ro;struct trace_array *tr;struct trace_subsystem_dir *system;struct list_head triggers;
include/linux/tracefs.h +4 -0
@@ -87,6 +87,10 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inodeconst struct eventfs_entry *entries, int size, void *data); +struct eventfs_inode *eventfs_create_events_dir_ro (const char *name, +const struct eventfs_entry *entries, +int size,void *data); +void eventfs_remove_events_dir (struct eventfs_inode *ei);void eventfs_remove_dir (struct eventfs_inode *ei);
include/uapi/linux/magic.h +1 -0
@@ -75,6 +75,7 @@#define STACK_END_MAGIC 0x57AC6E9D#define TRACEFS_MAGIC 0x74726163 +#define EVENTFS_SUPER_MAGIC 0x65766673#define V9FS_MAGIC 0x01021997
kernel/trace/trace.h +2 -0
@@ -416,6 +416,7 @@ struct trace_array {struct dentry *options;struct dentry *percpu_dir;struct eventfs_inode *event_dir; +struct eventfs_inode *event_dir_ro;struct trace_options *topts;struct list_head systems;struct list_head events;
@@ -1604,6 +1605,7 @@ struct trace_subsystem_dir {struct event_subsystem *subsystem;struct trace_array *tr;struct eventfs_inode *ei; +struct eventfs_inode *ei_ro;int ref_count;int nr_events; };
kernel/trace/trace_events.c +87 -0
133 lines not on this page · raw message
Signature · 1 line
2.54.0
Steven Rostedt
maintainerRe: [PATCH v5 2/5] tracefs: add read-only eventfs filesystem at /sys/kernel/events
BTW, the tracing subsystem always capitalizes the subject:
tracefs: Add read-only eventfs filesystem at /sys/kernel/events
Quoted 14 lines · On Wed, 15 Jul 2026 09:52:23 -0400 Anubhav Shelat <[email protected]> wrote:
Introduce a new read-only pseudo-filesystem "eventfs" mounted at
/sys/kernel/eventsthat exposes trace event format and id files (mode 0444) to unprivileged users. This allows tools like perf to discover event formats without requiring access to the full tracefs/debugfs mount.The eventfs filesystem reuses the existing
eventfs_inodelazy-lookup infrastructure. A new set ofsuper_operations(eventfs_ro_super_operations) shares the tracefs inode allocator so thateventfs_get_inode()andget_tracefs()work on the RO superblock. The superblock is manually initialized to ensure the root inode is allocated withtracefs_alloc_inode, allowing the root to serve directly as the events directory without another events subdirectory.
Quoted 7 lines
Each qualifying event gets a subsystem directory containing format and id files. The top-level events directory also exposes
header_pageandheader_event. Similar to tracefs, a change will need to be made in systemd to mount this filesystem automatically.Assisted-by CLAUDE:claude-opus-4 ApogeeSigned-off-by Anubhav Shelat <[email protected]>
Anyway, I didn't like the way this patch duplicated the eventfs_inodes as that just wastes memory. It duplicates all the events for just making them have some read only files to show. So I took another approach.
I started with this patch and then changed it to instead have two look up functions. One normal, and one for the read only file system.
It adds a "read_only" field to the eventfs_entry (which is just what describes what files to print for every event). For the fields we want to show to the normal user, the "read_only" field is set to true.
Then when the /sys/kernel/events directory is read, it uses the "read_only" version of the look up and it will only show the events that are marked as "read_only". It also changes the mode to allow normal users to read the files.
I replaced the eventfs_create_events_dir_ro() with eventfs_create_events_ro_copy(). It takes a name and an eventfs_inode to use to copy as the "read_only" version.
Below is the patch I used to replace this one. Feel free to start with it, clean it up a bit, and resubmit a v6.
Thanks!
-- Steve
fs/tracefs/event_inode.c +102 -8
@@ -151,7 +151,11 @@ static inline struct eventfs_inode *get_ei(struct eventfs_inode *ei)static struct dentry *eventfs_root_lookup (struct inode *dir,struct dentry *dentry,unsigned int flags); +static struct dentry *eventfs_root_lookup_ro (struct inode *dir, +struct dentry *dentry, +unsigned int flags);static int eventfs_iterate (struct file *file,struct dir_context *ctx); +static int eventfs_ro_iterate (struct file *file,struct dir_context *ctx);static void update_attr (struct eventfs_attr *attr,struct iattr *iattr) {
@@ -229,6 +233,10 @@ static const struct inode_operations eventfs_dir_inode_operations = { .setattr = eventfs_set_attr, }; +static const struct inode_operations eventfs_ro_dir_inode_operations = { + .lookup = eventfs_root_lookup_ro, +}; +static const struct inode_operations eventfs_file_inode_operations = { .setattr = eventfs_set_attr, };
@@ -239,6 +247,12 @@ static const struct file_operations eventfs_file_operations = { .llseek = generic_file_llseek, }; +static const struct file_operations eventfs_ro_file_operations = { + .read = generic_read_dir, + .iterate_shared = eventfs_ro_iterate, + .llseek = generic_file_llseek, +}; +static void eventfs_set_attrs (struct eventfs_inode *ei,bool update_uid,kuid_t uid,bool update_gid,kgid_t gid,int level) {
@@ -402,7 +416,7 @@ static struct dentry *lookup_file(struct eventfs_inode *parent_ei,* a eventfs_inode. */ static struct dentry *lookup_dir_entry (struct dentry *dentry, -struct eventfs_inode *pei,struct eventfs_inode *ei) +struct eventfs_inode *pei,struct eventfs_inode *ei,bool ro) {struct inode *inode;umode_t mode =S_IFDIR |S_IRWXU |S_IRUGO |S_IXUGO ;
@@ -411,8 +425,13 @@ static struct dentry *lookup_dir_entry(struct dentry *dentry,if (unlikely (!inode))return ERR_PTR (-ENOMEM ); - inode->i_op = &eventfs_dir_inode_operations; - inode->i_fop = &eventfs_file_operations; +if (ro) { + inode->i_op = &eventfs_ro_dir_inode_operations; + inode->i_fop = &eventfs_ro_file_operations; + }else { + inode->i_op = &eventfs_dir_inode_operations; + inode->i_fop = &eventfs_file_operations; + }/* All directories will have the same inode number */ inode->i_ino =eventfs_dir_ino (ei);
@@ -514,9 +533,10 @@ lookup_file_dentry(struct dentry *dentry,* list, if @dentry found go ahead and create the file/dir */ -static struct dentry *eventfs_root_lookup (struct inode *dir, -struct dentry *dentry, -unsigned int flags) +static struct dentry *__eventfs_root_lookup (struct inode *dir, +struct dentry *dentry, +unsigned int flags, +bool ro) {struct eventfs_inode *ei_child;struct tracefs_inode *ti;
@@ -539,7 +559,7 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,/* A child is freed and removed from the list at the same time */ if (WARN_ON_ONCE (ei_child->is_freed))return NULL ; -return lookup_dir_entry (dentry, ei, ei_child); +return lookup_dir_entry (dentry, ei, ei_child, ro); }for (int i =0 ; i < ei->nr_entries; i++) {
@@ -551,20 +571,40 @@ static struct dentry *eventfs_root_lookup(struct inode *dir,if (strcmp (name, entry->name) !=0 )continue ; +if (ro && !entry->read_only) +return NULL ; + data = ei->data;if (entry->callback (name, &mode, &data, &fops) <=0 )return NULL ; +if (ro) + mode |=0444 ; +return lookup_file_dentry (dentry, ei, i, mode, data, fops); }return NULL ; } +static struct dentry *eventfs_root_lookup (struct inode *dir, +struct dentry *dentry, +unsigned int flags) +{ +return __eventfs_root_lookup (dir, dentry, flags, false); +} + +static struct dentry *eventfs_root_lookup_ro (struct inode *dir, +struct dentry *dentry, +unsigned int flags) +{ +return __eventfs_root_lookup (dir, dentry, flags, true); +} +/* * Walk the children of a eventfs_inode to fill in getdents(). */ -static int eventfs_iterate (struct file *file,struct dir_context *ctx) +static int __eventfs_iterate (struct file *file,struct dir_context *ctx,bool ro) {const struct file_operations *fops;struct inode *f_inode =file_inode (file);
@@ -606,6 +646,9 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx) entry = &ei->entries[i]; name = entry->name; +if (ro && !entry->read_only) +continue ; +/* If ei->is_freed then just bail here, nothing more to do */ scoped_guard(mutex, &eventfs_mutex) {if (ei->is_freed)
@@ -650,6 +693,16 @@ static int eventfs_iterate(struct file *file, struct dir_context *ctx)return 1 ; } +static int eventfs_iterate (struct file *file,struct dir_context *ctx) +{ +return __eventfs_iterate (file, ctx, false); +} + +static int eventfs_ro_iterate (struct file *file,struct dir_context *ctx) +{ +return __eventfs_iterate (file, ctx, true); +} +/** * eventfs_create_dir - Create the eventfs_inode for this directory * @name: The name of the directory to create.
@@ -812,6 +865,47 @@ struct eventfs_inode *eventfs_create_events_dir(const char *name, struct dentryreturn ERR_PTR (-ENOMEM ); } +/** +* eventfs_create_events_dir_ro - create a read-only events directory +* @name: The name of the top level directory to create. +* @entries: A list of entries that represent the files under this directory +* @size: The number of @entries +* @data: The default data to pass to the files (an entry may override it). +* +* This function configures the eventfs filesystem root as a read-only +* trace event directory using the existing eventfs_inode lazy-lookup +* infrastructure. +* +* See eventfs_create_dir() for use of @entries. +*/ +int eventfs_create_events_ro_copy (const char *name,struct eventfs_inode *ei) +{ +struct dentry *dentry; +struct tracefs_inode *ti; +struct inode *inode; + + dentry =eventfs_ro_get_root (); +if (IS_ERR (dentry)) +return PTR_ERR (dentry); + + inode =d_inode (dentry); + +INIT_LIST_HEAD (&ei->children); +INIT_LIST_HEAD (&ei->list); + + ti =get_tracefs (inode); + ti->flags |=TRACEFS_EVENT_INODE ; + ti->private = ei; + + inode->i_op = &eventfs_ro_dir_inode_operations; + inode->i_fop = &eventfs_ro_file_operations; + +/* This is never freed */ + dentry->d_fsdata =get_ei (ei); + +return 0 ; +} +/** * eventfs_remove_rec - remove eventfs dir or file from list * @ei: eventfs_inode to be removed.
fs/tracefs/inode.c +93 -2
119 lines not on this page · raw message
fs/tracefs/internal.h +3 -0
@@ -73,6 +73,9 @@ struct dentry *tracefs_end_creating(struct dentry *dentry);struct dentry *tracefs_failed_creating (struct dentry *dentry);struct inode *tracefs_get_inode (struct super_block *sb); +struct dentry *eventfs_ro_get_root (void ); +void eventfs_ro_put_root (void ); +void eventfs_remount (struct tracefs_inode *ti,bool update_uid,bool update_gid);void eventfs_d_release (struct dentry *dentry);
include/linux/tracefs.h +3 -0
@@ -75,6 +75,7 @@ struct eventfs_entry {const char *name;eventfs_callback callback;eventfs_release release; +bool read_only; };struct eventfs_inode ;
@@ -87,6 +88,8 @@ struct eventfs_inode *eventfs_create_dir(const char *name, struct eventfs_inodeconst struct eventfs_entry *entries, int size, void *data); +int eventfs_create_events_ro_copy (const char *name,struct eventfs_inode *ei); +void eventfs_remove_events_dir (struct eventfs_inode *ei);void eventfs_remove_dir (struct eventfs_inode *ei);
include/uapi/linux/magic.h +1 -0
@@ -75,6 +75,7 @@#define STACK_END_MAGIC 0x57AC6E9D#define TRACEFS_MAGIC 0x74726163 +#define EVENTFS_SUPER_MAGIC 0x65766673#define V9FS_MAGIC 0x01021997
kernel/trace/trace_events.c +13 -0
@@ -3235,17 +3235,20 @@ event_create_dir(struct eventfs_inode *parent, struct trace_event_file *file) { .name = "format", .callback = event_callback, + .read_only = true, }, #ifdef CONFIG_PERF_EVENTS { .name = "id", .callback = event_callback, + .read_only = true, }, #endif #ifdef CONFIG_BPF_EVENTS { .name = "btf_ids", .callback = event_callback, + .read_only = true, }, #endif #define NR_RO_EVENT_ENTRIES (1 + IS_ENABLED(CONFIG_PERF_EVENTS) + \
@@ -4652,6 +4655,7 @@ static int events_callback(const char *name, umode_t *mode, void **data,static int create_event_toplevel_files (struct dentry *parent,struct trace_array *tr) { +static bool event_dir_ro_created;struct eventfs_inode *e_events;struct dentry *entry;int nr_entries;
@@ -4659,10 +4663,12 @@ create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) { .name = "header_page", .callback = events_callback, + .read_only = true, }, { .name = "header_event", .callback = events_callback, + .read_only = true, }, #define NR_RO_TOP_ENTRIES 2 /* Readonly files must be above this line and counted by NR_RO_TOP_ENTRIES. */
@@ -4705,6 +4711,13 @@ create_event_toplevel_files(struct dentry *parent, struct trace_array *tr) tr->event_dir = e_events; +if (!event_dir_ro_created && (tr->flags &TRACE_ARRAY_FL_GLOBAL )) { +int ret; + + ret =eventfs_create_events_ro_copy ("events" , e_events); +if (!ret) + event_dir_ro_created = true; + }return 0 ; }
PATCH v5 3/5 perf tools: fall back to eventfs for unprivileged event discovery
In reply to Anubhav ShelatWhen tracefs events are not readable by unprivileged users, fall back to /sys/kernel/events (the read-only eventfs mount) for tracepoint format and id file discovery. This allows perf trace to work for unprivileged users on kernels that expose the eventfs filesystem.
The fallback is transparent: get_events_file() tries the tracefs path first and only switches to eventfs when access() fails. On kernels without eventfs, the existing error path is preserved.
--- tools/lib/api/fs/fs.c | 10 ++++++ tools/lib/api/fs/fs.h | 1 + tools/lib/api/fs/tracing_path.c | 52 ++++++++++++++++++++++++++---- tools/lib/api/fs/tracing_path.h | 1 + tools/perf/util/tp_pmu.c | 5 +-- tools/perf/util/trace-event-info.c | 19 ++++++----- 6 files changed, 71 insertions(+), 17 deletions(-)
tools/lib/api/fs/fs.c +10 -0
@@ -46,6 +46,10 @@#define BPF_FS_MAGIC 0xcafe4a11#endif +#ifndef EVENTFS_SUPER_MAGIC +#define EVENTFS_SUPER_MAGIC 0x65766673 +#endif +static const char *const sysfs__known_mountpoints[] = {"/sys" ,0 ,
@@ -88,6 +92,11 @@ static const char * const bpf_fs__known_mountpoints[] = { 0, }; +static const char *const eventfs__known_mountpoints[] = { +"/sys/kernel/events" , +0 , +}; +struct fs {const char *const name;const char *const *const mounts;
@@ -150,6 +159,7 @@ FS(debugfs, debugfs, DEBUGFS);FS (tracefs, tracefs,TRACEFS );FS (hugetlbfs, hugetlbfs,HUGETLBFS );FS (bpf_fs, bpf,BPF_FS ); +FS (eventfs, eventfs,EVENTFS_SUPER );static bool fs__read_mounts (struct fs *fs) {
tools/lib/api/fs/fs.h +1 -0
@@ -36,6 +36,7 @@ FS(debugfs) FS(tracefs) FS(hugetlbfs) FS(bpf_fs) +FS(eventfs)#undef FS
tools/lib/api/fs/tracing_path.c +45 -7
97 lines not on this page · raw message
tools/lib/api/fs/tracing_path.h +1 -0
@@ -14,6 +14,7 @@ const char *tracing_path_mount(void);char *get_tracing_file (const char *name);void put_tracing_file (char *file); +char *get_events_dir (void );char *get_events_file (const char *name);void put_events_file (char *file);
tools/perf/util/tp_pmu.c +3 -2
@@ -6,6 +6,7 @@#include <api/io_dir.h> #include <linux/kernel.h> #include <errno.h> +#include <stdlib.h> #include <string.h> int tp_pmu__id (const char *sys,const char *name)
@@ -15,7 +16,7 @@ int tp_pmu__id(const char *sys, const char *name)int id, err;if (!tp_dir) -return -1 ; +return -errno;scnprintf (path,PATH_MAX ,"%s/%s/id" , tp_dir, name);put_events_file (tp_dir);
@@ -66,7 +67,7 @@ int tp_pmu__for_each_tp_sys(void *state, tp_sys_callback cb)struct io_dirent64 *events_ent;struct io_dir events_dir;int ret =0 ; -char *events_dir_path =get_tracing_file ("events" ); +char *events_dir_path =get_events_dir ();if (!events_dir_path)return -errno;
tools/perf/util/trace-event-info.c +11 -8
@@ -92,8 +92,9 @@ static int record_header_files(void)int err = -EIO ;if (!path) { + err = -errno;pr_debug ("can't get tracing/events/header_page" ); -return -ENOMEM ; +return err; }if (stat (path, &st) <0 ) {
@@ -115,8 +116,8 @@ static int record_header_files(void) path =get_events_file ("header_event" );if (!path) { + err = -errno;pr_debug ("can't get tracing/events/header_event" ); - err = -ENOMEM ; goto out; }
@@ -228,13 +229,14 @@ static int record_ftrace_files(struct tracepoint_path *tps) path =get_events_file ("ftrace" );if (!path) { + ret = -errno;pr_debug ("can't get tracing/events/ftrace" ); -return -ENOMEM ; +return ret; } ret =copy_event_system (path, tps); -put_tracing_file (path); +put_events_file (path);return ret; }
@@ -256,15 +258,16 @@ static int record_event_files(struct tracepoint_path *tps)struct stat st;char *path;char *sys; -DIR *dir; +DIR *dir =NULL ;int count =0 ;int ret;int err; - path =get_tracing_file ("events" ); + path =get_events_dir ();if (!path) { + err = -errno;pr_debug ("can't get tracing/events" ); -return -ENOMEM ; + goto out; } dir =opendir (path);
@@ -315,7 +318,7 @@ static int record_event_files(struct tracepoint_path *tps) out:if (dir)closedir (dir); -put_tracing_file (path); +put_events_file (path);return err; }
Signature · 1 line
2.54.0
PATCH v5 4/5 perf evsel: don't set PERF_SAMPLE_IP for unprivileged tracepoints
In reply to Anubhav ShelatFor 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
@@ -1571,7 +1571,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 both 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
PATCH v5 5/5 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 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) 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. 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.
3. 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 | 28 +++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 4 deletions(-)
kernel/events/core.c +25 -3
@@ -13910,9 +13910,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 +27 -1
@@ -24,6 +24,16 @@ typedef typeof(unsigned long [PERF_MAX_TRACE_SIZE / sizeof(unsigned long)])/* Count the events in use (per event id, not per instance) */ static int total_ref_count; +/* Check if perf tracepoint is restricted for unprivileged users */ +static bool perf_tp_is_restricted (struct perf_event *p_event) +{ +if (p_event->attr.exclude_kernel) +return false; +if (sysctl_perf_event_paranoid <=1 ||perfmon_capable ()) +return false; +return true; +} +static int perf_trace_event_perm (struct trace_event_call *tp_event,struct perf_event *p_event) {
@@ -72,9 +82,25 @@ 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 ) && + !(tp_event->flags &TRACE_EVENT_FL_UPROBE ) && +perf_tp_is_restricted (p_event)) +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 (perf_tp_is_restricted (p_event) && + !(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 ) {
Signature · 1 line
2.54.0