Allow syscalls tracepoints to non privileged users
cover Allow syscalls tracepoints to non privileged users
Hi,
Tell me if you're fine with it, and if so I'll push it.
Thanks.
Frederic Weisbecker (5):
tracing: New flag to allow non privileged users to use a trace event
tracing: New macro to set up initial event flags value
tracing: Allow raw syscall trace events for non privileged users
tracing: Allow syscall trace events for non privileged users
tracing: Remove useless syscall ftrace_event_call declaration
include/linux/ftrace_event.h | 10 ++++++++++ include/linux/syscalls.h | 10 ++++------ include/linux/tracepoint.h | 4 ++++ include/trace/events/syscalls.h | 4 ++++ include/trace/ftrace.h | 7 +++++++ kernel/perf_event.c | 9 --------- kernel/trace/trace_event_perf.c | 31 ++++++++++++++++++++++++++++++- 7 files changed, 59 insertions(+), 16 deletions(-)
PATCH 1/5 tracing: New flag to allow non privileged users to use a trace event
This adds a new trace event internal flag that allows them to be used in perf by non privileged users in case of task bound tracing.
This is desired for syscalls tracepoint because they don't leak global system informations, like some other tracepoints.
--- include/linux/ftrace_event.h | 2 ++ kernel/perf_event.c | 9 --------- kernel/trace/trace_event_perf.c | 31 ++++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 10 deletions(-)
include/linux/ftrace_event.h +2 -0
@@ -154,12 +154,14 @@ enum {TRACE_EVENT_FL_ENABLED_BIT ,TRACE_EVENT_FL_FILTERED_BIT ,TRACE_EVENT_FL_RECORDED_CMD_BIT , + TRACE_EVENT_FL_CAP_ANY_BIT, };enum {TRACE_EVENT_FL_ENABLED = (1 <<TRACE_EVENT_FL_ENABLED_BIT ),TRACE_EVENT_FL_FILTERED = (1 <<TRACE_EVENT_FL_FILTERED_BIT ),TRACE_EVENT_FL_RECORDED_CMD = (1 <<TRACE_EVENT_FL_RECORDED_CMD_BIT ), +TRACE_EVENT_FL_CAP_ANY = (1 <<TRACE_EVENT_FL_CAP_ANY_BIT ), }; struct ftrace_event_call {
kernel/perf_event.c +0 -9
@@ -4747,15 +4747,6 @@ static int perf_tp_event_init(struct perf_event *event)if (event->attr.type !=PERF_TYPE_TRACEPOINT )return -ENOENT ; -/* -* Raw tracepoint data is a severe data leak, only allow root to -* have these. -*/ -if ((event->attr.sample_type &PERF_SAMPLE_RAW ) && -perf_paranoid_tracepoint_raw () && - !capable (CAP_SYS_ADMIN )) -return -EPERM ; - err =perf_trace_init (event);if (err)return err;
kernel/trace/trace_event_perf.c +30 -1
@@ -21,17 +21,46 @@ 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; +static int perf_trace_event_perm (struct ftrace_event_call *tp_event, +struct perf_event *p_event) +{ +/* No tracing, just counting, so no obvious leak */ +if (!(p_event->attr.sample_type &PERF_SAMPLE_RAW )) +return 0 ; + +/* Some events are ok to be traced by non-root users... */ +if (p_event->attach_state ==PERF_ATTACH_TASK ) { +if (tp_event->flags &TRACE_EVENT_FL_CAP_ANY ) +return 0 ; + } + +/* +* ...otherwise raw tracepoint data can be a severe data leak, +* only allow root to have these. +*/ +if (perf_paranoid_tracepoint_raw () && !capable (CAP_SYS_ADMIN )) +return -EPERM ; + +return 0 ; +} +static int perf_trace_event_init (struct ftrace_event_call *tp_event,struct perf_event *p_event) {struct hlist_head __percpu *list; -int ret = -ENOMEM ; +int ret;int cpu; + ret =perf_trace_event_perm (tp_event, p_event); +if (ret) +return ret; + p_event->tp_event = tp_event;if (tp_event->perf_refcount++ >0 )return 0 ; + ret = -ENOMEM ; + list = alloc_percpu(struct hlist_head);if (!list) goto fail;
Signature · 1 line
1.6.2.3
PATCH 2/5 tracing: New macro to set up initial event flags value
In reply to Frederic WeisbeckerThis introduces the new TRACE_EVENT_FLAGS() macro in order to set up initial event flags value.
This macro must simply follow the definition of a trace event and take the event name and the flag value as parameters:
TRACE_EVENT(my_event, .....
....
);
TRACE_EVENT_FLAGS(my_event, 1)
This will set up 1 as the initial my_event->flags value.
--- include/linux/tracepoint.h | 4 ++++ include/trace/ftrace.h | 12 ++++++++++++ 2 files changed, 16 insertions(+), 0 deletions(-)
include/linux/tracepoint.h +4 -0
@@ -234,6 +234,8 @@ do_trace: \ PARAMS(void *__data, proto), \ PARAMS(__data, args)) +#define TRACE_EVENT_FLAGS (event, flag) +#endif /* DECLARE_TRACE */ #ifndef TRACE_EVENT
@@ -354,4 +356,6 @@ do_trace: \ assign, print, reg, unreg) \ DECLARE_TRACE(name, PARAMS(proto), PARAMS(args)) +#define TRACE_EVENT_FLAGS (event, flag) +#endif /* ifdef TRACE_EVENT (see note above) */
include/trace/ftrace.h +12 -0
@@ -82,6 +82,15 @@TRACE_EVENT (name,PARAMS (proto),PARAMS (args), \ PARAMS(tstruct), PARAMS(assign), PARAMS(print)) \ +#undef TRACE_EVENT_FLAGS +#define TRACE_EVENT_FLAGS (name, value) \ + static int __init trace_init_flags_##name(void) \ + { \ + event_##name.flags = value; \ + return 0; \ + } \ + early_initcall(trace_init_flags_##name); +#include TRACE_INCLUDE (TRACE_INCLUDE_FILE )
@@ -129,6 +138,9 @@#define DEFINE_EVENT_PRINT (template, name, proto, args, print) \ DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args)) +#undef TRACE_EVENT_FLAGS +#define TRACE_EVENT_FLAGS (event, flag) +#include TRACE_INCLUDE (TRACE_INCLUDE_FILE )/*
Signature · 1 line
1.6.2.3
PATCH 3/5 tracing: Allow raw syscall trace events for non privileged users
In reply to Frederic WeisbeckerThis allows non privileged users to use the raw syscall trace events for task bound tracing in perf.
It is safe because raw syscall trace events don't leak system wide informations.
--- include/trace/events/syscalls.h | 4 ++++ 1 files changed, 4 insertions(+), 0 deletions(-)
include/trace/events/syscalls.h +4 -0
@@ -40,6 +40,8 @@ TRACE_EVENT_FN(sys_enter, syscall_regfunc, syscall_unregfunc ); +TRACE_EVENT_FLAGS(sys_enter, TRACE_EVENT_FL_CAP_ANY) + TRACE_EVENT_FN(sys_exit, TP_PROTO(struct pt_regs *regs, long ret),
@@ -62,6 +64,8 @@ TRACE_EVENT_FN(sys_exit, syscall_regfunc, syscall_unregfunc ); +TRACE_EVENT_FLAGS(sys_exit, TRACE_EVENT_FL_CAP_ANY) +#endif /* CONFIG_HAVE_SYSCALL_TRACEPOINTS */ #endif /* _TRACE_EVENTS_SYSCALLS_H */
Signature · 1 line
1.6.2.3
PATCH 4/5 tracing: Allow syscall trace events for non privileged users
In reply to Frederic WeisbeckerAs for the raw syscalls events, individual syscall events won't leak system wide information on task bound tracing. Allow non privileged users to use them in such workflow.
--- include/linux/ftrace_event.h | 8 ++++++++ include/linux/syscalls.h | 6 ++++-- include/trace/ftrace.h | 7 +------ 3 files changed, 13 insertions(+), 8 deletions(-)
include/linux/ftrace_event.h +8 -0
@@ -198,6 +198,14 @@ struct ftrace_event_call {#endif }; +#define __TRACE_EVENT_FLAGS (name, value) \ + static int __init trace_init_flags_##name(void) \ + { \ + event_##name.flags = value; \ + return 0; \ + } \ + early_initcall(trace_init_flags_##name); +#define PERF_MAX_TRACE_SIZE 2048#define MAX_FILTER_PRED 32
include/linux/syscalls.h +4 -2
@@ -137,7 +137,8 @@ extern struct trace_event_functions exit_syscall_print_funcs; .class = &event_class_syscall_enter, \ .event.funcs = &enter_syscall_print_funcs, \ .data = (void *)&__syscall_meta_##sname,\ - } + }; \ + __TRACE_EVENT_FLAGS(enter_##sname, TRACE_EVENT_FL_CAP_ANY)#define SYSCALL_TRACE_EXIT_EVENT (sname) \ static struct syscall_metadata \
@@ -152,7 +153,8 @@ extern struct trace_event_functions exit_syscall_print_funcs; .class = &event_class_syscall_exit, \ .event.funcs = &exit_syscall_print_funcs, \ .data = (void *)&__syscall_meta_##sname,\ - } + }; \ + __TRACE_EVENT_FLAGS(exit_##sname, TRACE_EVENT_FL_CAP_ANY)#define SYSCALL_METADATA (sname, nb) \ SYSCALL_TRACE_ENTER_EVENT(sname); \
include/trace/ftrace.h +1 -6
@@ -84,12 +84,7 @@#undef TRACE_EVENT_FLAGS#define TRACE_EVENT_FLAGS (name, value) \ - static int __init trace_init_flags_##name(void) \ - { \ - event_##name.flags = value; \ - return 0; \ - } \ - early_initcall(trace_init_flags_##name); + __TRACE_EVENT_FLAGS(name, value)#include TRACE_INCLUDE (TRACE_INCLUDE_FILE )
Signature · 1 line
1.6.2.3
PATCH 5/5 tracing: Remove useless syscall ftrace_event_call declaration
In reply to Frederic WeisbeckerIt is defined right after, which makes the declaration completely useless.
--- include/linux/syscalls.h | 4 ---- 1 files changed, 0 insertions(+), 4 deletions(-)
include/linux/syscalls.h +0 -4
@@ -127,8 +127,6 @@ extern struct trace_event_functions exit_syscall_print_funcs;#define SYSCALL_TRACE_ENTER_EVENT (sname) \ static struct syscall_metadata \ __attribute__((__aligned__(4))) __syscall_meta_##sname; \ - static struct ftrace_event_call \ - __attribute__((__aligned__(4))) event_enter_##sname; \ static struct ftrace_event_call __used \ __attribute__((__aligned__(4))) \ __attribute__((section("_ftrace_events"))) \
@@ -143,8 +141,6 @@ extern struct trace_event_functions exit_syscall_print_funcs;#define SYSCALL_TRACE_EXIT_EVENT (sname) \ static struct syscall_metadata \ __attribute__((__aligned__(4))) __syscall_meta_##sname; \ - static struct ftrace_event_call \ - __attribute__((__aligned__(4))) event_exit_##sname; \ static struct ftrace_event_call __used \ __attribute__((__aligned__(4))) \ __attribute__((section("_ftrace_events"))) \
Signature · 1 line
1.6.2.3
Quoted 3 lines · Frederic Weisbecker
Hi,
Tell me if you're fine with it, and if so I'll push it.
Yeah, very nice work!
Thanks,
Ingo
Quoted 3 lines · Frederic Weisbecker
Hi,
Tell me if you're fine with it, and if so I'll push it.
Looks good to me, thanks!