Skip to content

Commit 5dbd04e

Browse files
beaubelgraverostedt
authored andcommitted
tracing/user_events: Allow events to persist for perfmon_capable users
There are several scenarios that have come up where having a user_event persist even if the process that registered it exits. The main one is having a daemon create events on bootup that shouldn't get deleted if the daemon has to exit or reload. Another is within OpenTelemetry exporters, they wish to potentially check if a user_event exists on the system to determine if exporting the data out should occur. The user_event in this case must exist even in the absence of the owning process running (such as the above daemon case). Expose the previously internal flag USER_EVENT_REG_PERSIST to user processes. Upon register or delete of events with this flag, ensure the user is perfmon_capable to prevent random user processes with access to tracefs from creating events that persist after exit. Link: https://lkml.kernel.org/r/20230912180704.1284-2-beaub@linux.microsoft.com Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com> Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent bdf4fb6 commit 5dbd04e

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

include/uapi/linux/user_events.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@
1717
/* Create dynamic location entry within a 32-bit value */
1818
#define DYN_LOC(offset, size) ((size) << 16 | (offset))
1919

20+
/* List of supported registration flags */
21+
enum user_reg_flag {
22+
/* Event will not delete upon last reference closing */
23+
USER_EVENT_REG_PERSIST = 1U << 0,
24+
25+
/* This value or above is currently non-ABI */
26+
USER_EVENT_REG_MAX = 1U << 1,
27+
};
28+
2029
/*
2130
* Describes an event registration and stores the results of the registration.
2231
* This structure is passed to the DIAG_IOCSREG ioctl, callers at a minimum
@@ -33,7 +42,7 @@ struct user_reg {
3342
/* Input: Enable size in bytes at address */
3443
__u8 enable_size;
3544

36-
/* Input: Flags for future use, set to 0 */
45+
/* Input: Flags to use, if any */
3746
__u16 flags;
3847

3948
/* Input: Address to update when enabled */

kernel/trace/trace_events_user.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@
4949
#define EVENT_STATUS_PERF BIT(1)
5050
#define EVENT_STATUS_OTHER BIT(7)
5151

52-
/*
53-
* User register flags are not allowed yet, keep them here until we are
54-
* ready to expose them out to the user ABI.
55-
*/
56-
enum user_reg_flag {
57-
/* Event will not delete upon last reference closing */
58-
USER_EVENT_REG_PERSIST = 1U << 0,
59-
60-
/* This value or above is currently non-ABI */
61-
USER_EVENT_REG_MAX = 1U << 1,
62-
};
63-
6452
/*
6553
* Stores the system name, tables, and locks for a group of events. This
6654
* allows isolation for events by various means.
@@ -220,6 +208,17 @@ static u32 user_event_key(char *name)
220208
return jhash(name, strlen(name), 0);
221209
}
222210

211+
static bool user_event_capable(u16 reg_flags)
212+
{
213+
/* Persistent events require CAP_PERFMON / CAP_SYS_ADMIN */
214+
if (reg_flags & USER_EVENT_REG_PERSIST) {
215+
if (!perfmon_capable())
216+
return false;
217+
}
218+
219+
return true;
220+
}
221+
223222
static struct user_event *user_event_get(struct user_event *user)
224223
{
225224
refcount_inc(&user->refcnt);
@@ -1811,6 +1810,9 @@ static int user_event_free(struct dyn_event *ev)
18111810
if (!user_event_last_ref(user))
18121811
return -EBUSY;
18131812

1813+
if (!user_event_capable(user->reg_flags))
1814+
return -EPERM;
1815+
18141816
return destroy_user_event(user);
18151817
}
18161818

@@ -1926,10 +1928,13 @@ static int user_event_parse(struct user_event_group *group, char *name,
19261928
int argc = 0;
19271929
char **argv;
19281930

1929-
/* User register flags are not ready yet */
1930-
if (reg_flags != 0 || flags != NULL)
1931+
/* Currently don't support any text based flags */
1932+
if (flags != NULL)
19311933
return -EINVAL;
19321934

1935+
if (!user_event_capable(reg_flags))
1936+
return -EPERM;
1937+
19331938
/* Prevent dyn_event from racing */
19341939
mutex_lock(&event_mutex);
19351940
user = find_user_event(group, name, &key);
@@ -2062,6 +2067,9 @@ static int delete_user_event(struct user_event_group *group, char *name)
20622067
if (!user_event_last_ref(user))
20632068
return -EBUSY;
20642069

2070+
if (!user_event_capable(user->reg_flags))
2071+
return -EPERM;
2072+
20652073
return destroy_user_event(user);
20662074
}
20672075

0 commit comments

Comments
 (0)