perf_event: Add support for LSM and SELinux checks
Subsystems (6)
PATCH 1/1 perf_event: Add support for LSM and SELinux checks
In currentl mainline, the degree of access to perf_event_open(2) system call depends on the perf_event_paranoid sysctl. This has a number of limitations:
1. The sysctl is only a single value. Many types of accesses are controlled based on the single value thus making the control very limited and coarse grained.
2. The sysctl is global, so if the sysctl is changed, then that means all processes get access to perf_event_open(2) opening the door to security issues.
This patch adds LSM and SELinux access checking which will be used in Android to access perf_event_open(2) for the purposes of attaching BPF programs to tracepoints, perf profiling and other operations from userspace. These operations are intended for production systems.
5 new LSM hooks are added:
1. perf_event_open: This controls access during the perf_event_open(2) syscall itself. The hook is called from all the places that the perf_event_paranoid sysctl is checked to keep it consistent with the systctl. The hook gets passed a 'type' argument which controls CPU, kernel and tracepoint accesses (in this context, CPU, kernel and tracepoint have the same semantics as the perf_event_paranoid sysctl). Additionally, I added an 'open' type which is similar to perf_event_paranoid sysctl == 3 patch carried in Android and several other distros but was rejected in mainline [1] in 2016.
2. perf_event_alloc: This allocates a new security object for the event which stores the current SID within the event. It will be useful when the perf event's FD is passed through IPC to another process which may try to read the FD. Appropriate security checks will limit access.
3. perf_event_free: Called when the event is closed.
4. perf_event_read: Called from the read(2) and mmap(2) syscalls for the event.
5. perf_event_write: Called from the ioctl(2) syscalls for the event.
[1] https://lwn.net/Articles/696240/
Since Peter had suggest LSM hooks in 2016 [1], I am adding his Suggested-by tag below.
To use this patch, we set the perf_event_paranoid sysctl to -1 and then apply selinux checking as appropriate (default deny everything, and then add policy rules to give access to domains that need it). In the future we can remove the perf_event_paranoid sysctl altogether.
---
Changes since RFC:
o Small nits, style changes (James Morris).
o Consolidation of code (Peter Zijlstra).
arch/x86/events/intel/bts.c | 8 ++-- arch/x86/events/intel/core.c | 5 ++- arch/x86/events/intel/p4.c | 5 ++- include/linux/lsm_hooks.h | 15 +++++++ include/linux/perf_event.h | 28 +++++++++--- include/linux/security.h | 39 +++++++++++++++- include/uapi/linux/perf_event.h | 9 ++++ kernel/events/core.c | 52 +++++++++++++++++----- kernel/trace/trace_event_perf.c | 15 ++++--- security/security.c | 27 +++++++++++ security/selinux/hooks.c | 69 +++++++++++++++++++++++++++++ security/selinux/include/classmap.h | 2 + security/selinux/include/objsec.h | 6 ++- 13 files changed, 250 insertions(+), 30 deletions(-)
arch/x86/events/intel/bts.c +5 -3
@@ -549,9 +549,11 @@ static int bts_event_init(struct perf_event *event)* Note that the default paranoia setting permits unprivileged * users to profile the kernel. */ -if (event->attr.exclude_kernel &&perf_paranoid_kernel () && - !capable (CAP_SYS_ADMIN )) -return -EACCES ; +if (event->attr.exclude_kernel) { + ret =perf_allow_kernel (&event->attr); +if (ret) +return ret; + }if (x86_add_exclusive (x86_lbr_exclusive_bts))return -EBUSY ;
arch/x86/events/intel/core.c +3 -2
@@ -3315,8 +3315,9 @@ static int intel_pmu_hw_config(struct perf_event *event)if (x86_pmu.version <3 )return -EINVAL ; -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) -return -EACCES ; + ret =perf_allow_cpu (&event->attr); +if (ret) +return ret; event->hw.config |=ARCH_PERFMON_EVENTSEL_ANY ;
arch/x86/events/intel/p4.c +3 -2
@@ -776,8 +776,9 @@ static int p4_validate_raw_event(struct perf_event *event)* the user needs special permissions to be able to use it */ if (p4_ht_active () && p4_event_bind_map[v].shared) { -if (perf_paranoid_cpu () && !capable (CAP_SYS_ADMIN )) -return -EACCES ; + v =perf_allow_cpu (&event->attr); +if (v) +return v; }/* ESCR EventMask bits may be invalid */
include/linux/lsm_hooks.h +15 -0
@@ -1818,6 +1818,14 @@ union security_list_options {void (*bpf_prog_free_security)(struct bpf_prog_aux *aux);#endif /* CONFIG_BPF_SYSCALL */ int (*locked_down)(enum lockdown_reason what); +#ifdef CONFIG_PERF_EVENTS +int (*perf_event_open)(struct perf_event_attr *attr,int type); +int (*perf_event_alloc)(struct perf_event *event); +void (*perf_event_free)(struct perf_event *event); +int (*perf_event_read)(struct perf_event *event); +int (*perf_event_write)(struct perf_event *event); + +#endif };struct security_hook_heads {
@@ -2060,6 +2068,13 @@ struct security_hook_heads {struct hlist_head bpf_prog_free_security;#endif /* CONFIG_BPF_SYSCALL */ struct hlist_head locked_down; +#ifdef CONFIG_PERF_EVENTS +struct hlist_head perf_event_open; +struct hlist_head perf_event_alloc; +struct hlist_head perf_event_free; +struct hlist_head perf_event_read; +struct hlist_head perf_event_write; +#endif } __randomize_layout;/*
include/linux/perf_event.h +23 -5
@@ -56,6 +56,7 @@ struct perf_guest_info_callbacks {#include <linux/perf_regs.h> #include <linux/cgroup.h> #include <linux/refcount.h> +#include <linux/security.h> #include <asm/local.h> struct perf_callchain_entry {
@@ -721,6 +722,9 @@ struct perf_event {struct perf_cgroup *cgrp;/* cgroup event is attach to */ #endif +#ifdef CONFIG_SECURITY +void *security; +#endif struct list_head sb_list;#endif /* CONFIG_PERF_EVENTS */ };
@@ -1241,19 +1245,33 @@ extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,int perf_event_max_stack_handler (struct ctl_table *table,int write,void __user *buffer,size_t *lenp,loff_t *ppos); -static inline bool perf_paranoid_tracepoint_raw (void ) +static inline int perf_is_paranoid (void ) {return sysctl_perf_event_paranoid >-1 ; } -static inline bool perf_paranoid_cpu (void ) +static inline int perf_allow_kernel (struct perf_event_attr *attr) { -return sysctl_perf_event_paranoid >0 ; +if (sysctl_perf_event_paranoid >1 && !capable (CAP_SYS_ADMIN )) +return -EACCES ; + +return security_perf_event_open (attr,PERF_SECURITY_KERNEL ); } -static inline bool perf_paranoid_kernel (void ) +static inline int perf_allow_cpu (struct perf_event_attr *attr) { -return sysctl_perf_event_paranoid >1 ; +if (sysctl_perf_event_paranoid >0 && !capable (CAP_SYS_ADMIN )) +return -EACCES ; + +return security_perf_event_open (attr,PERF_SECURITY_CPU ); +} + +static inline int perf_allow_tracepoint (struct perf_event_attr *attr) +{ +if (sysctl_perf_event_paranoid >-1 && !capable (CAP_SYS_ADMIN )) +return -EPERM ; + +return security_perf_event_open (attr,PERF_SECURITY_TRACEPOINT ); }extern void perf_event_init (void );
include/linux/security.h +38 -1
@@ -1894,5 +1894,42 @@ static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)#endif /* CONFIG_SECURITY */ #endif /* CONFIG_BPF_SYSCALL */ -#endif /* ! __LINUX_SECURITY_H */ +#ifdef CONFIG_PERF_EVENTS +struct perf_event_attr ; + +#ifdef CONFIG_SECURITY +extern int security_perf_event_open (struct perf_event_attr *attr,int type); +extern int security_perf_event_alloc (struct perf_event *event); +extern void security_perf_event_free (struct perf_event *event); +extern int security_perf_event_read (struct perf_event *event); +extern int security_perf_event_write (struct perf_event *event); +#else +static inline int security_perf_event_open (struct perf_event_attr *attr, +int type) +{ +return 0 ; +} +static inline int security_perf_event_alloc (struct perf_event *event) +{ +return 0 ; +} + +static inline void security_perf_event_free (struct perf_event *event) +{ +return 0 ; +} + +static inline int security_perf_event_read (struct perf_event *event) +{ +return 0 ; +} + +static inline int security_perf_event_write (struct perf_event *event) +{ +return 0 ; +} +#endif /* CONFIG_SECURITY */ +#endif /* CONFIG_PERF_EVENTS */ + +#endif /* ! __LINUX_SECURITY_H */
include/uapi/linux/perf_event.h +9 -0
@@ -427,6 +427,15 @@ struct perf_event_attr {__u16 __reserved_2;/* align to __u64 */ }; + +/* Access to perf_event_open(2) syscall. */ +#define PERF_SECURITY_OPEN 0 + +/* Finer grained perf_event_open(2) access control. */ +#define PERF_SECURITY_CPU 1 +#define PERF_SECURITY_KERNEL 2 +#define PERF_SECURITY_TRACEPOINT 3 +/* * Structure used by below PERF_EVENT_IOC_QUERY_BPF command * to query bpf programs attached to the same perf tracepoint
kernel/events/core.c +41 -11
122 lines not on this page · raw message
kernel/trace/trace_event_perf.c +10 -5
@@ -8,6 +8,7 @@#include <linux/module.h> #include <linux/kprobes.h> +#include <linux/security.h> #include "trace.h" #include "trace_probe.h"
@@ -26,8 +27,10 @@ static int total_ref_count;static int perf_trace_event_perm (struct trace_event_call *tp_event,struct perf_event *p_event) { +int ret; +if (tp_event->perf_perm) { -int ret = tp_event->perf_perm (tp_event, p_event); + ret = tp_event->perf_perm (tp_event, p_event);if (ret)return ret; }
@@ -46,8 +49,9 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,/* The ftrace function trace is allowed only for root. */ if (ftrace_event_is_function (tp_event)) { -if (perf_paranoid_tracepoint_raw () && !capable (CAP_SYS_ADMIN )) -return -EPERM ; + ret =perf_allow_tracepoint (&p_event->attr); +if (ret) +return ret;if (!is_sampling_event (p_event))return 0 ;
@@ -82,8 +86,9 @@ static int perf_trace_event_perm(struct trace_event_call *tp_event,* ...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 ; + ret =perf_allow_tracepoint (&p_event->attr); +if (ret) +return ret;return 0 ; }
security/security.c +27 -0
@@ -2404,3 +2404,30 @@ int security_locked_down(enum lockdown_reason what)return call_int_hook (locked_down,0 , what); }EXPORT_SYMBOL (security_locked_down); + +#ifdef CONFIG_PERF_EVENTS +int security_perf_event_open (struct perf_event_attr *attr,int type) +{ +return call_int_hook (perf_event_open,0 , attr, type); +} + +int security_perf_event_alloc (struct perf_event *event) +{ +return call_int_hook (perf_event_alloc,0 , event); +} + +void security_perf_event_free (struct perf_event *event) +{ +call_void_hook (perf_event_free, event); +} + +int security_perf_event_read (struct perf_event *event) +{ +return call_int_hook (perf_event_read,0 , event); +} + +int security_perf_event_write (struct perf_event *event) +{ +return call_int_hook (perf_event_write,0 , event); +} +#endif /* CONFIG_PERF_EVENTS */
security/selinux/hooks.c +69 -0
81 lines not on this page · raw message
security/selinux/include/classmap.h +2 -0
@@ -244,6 +244,8 @@ struct security_class_mapping secclass_map[] = { {"map_create", "map_read", "map_write", "prog_load", "prog_run"} }, {"xdp_socket" , { COMMON_SOCK_PERMS, NULL } }, + {"perf_event" , + {"open", "cpu", "kernel", "tracepoint", "read", "write"} }, { NULL } };
security/selinux/include/objsec.h +5 -1
@@ -141,7 +141,11 @@ struct pkey_security_struct { };struct bpf_security_struct { -u32 sid;/*SID of bpf obj creater*/ +u32 sid;/* SID of bpf obj creator */ +}; + +struct perf_event_security_struct { +u32 sid;/* SID of perf_event obj creator */ };extern struct lsm_blob_sizes selinux_blob_sizes;
Signature · 1 line
2.23.0.700.g56cf767bdb-goog
Quoted 4 lines · Joel Fernandes (Google)
This patch adds LSM and SELinux access checking which will be used in Android to access
perf_event_open(2)for the purposes of attaching BPF programs to tracepoints, perf profiling and other operations from userspace. These operations are intended for production systems.
Signature · 2 lines
James Morris <[email protected]>
Peter Zijlstra
maintainerQuoted 16 lines · Joel Fernandes (Google)
(quoted diff) +4
-0@@ -4761,6 +4762,7 @@ int perf_event_release_kernel(struct perf_event *event) } no_ctx: + security_perf_event_free(event); put_event(event); /* Must be the 'last' reference */ return 0; }@@ -10553,11 +10568,16 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, } } + err = security_perf_event_alloc(event); + if (err) + goto err_security;
Review perf_event_alloc(struct perf_event_attr *attr, int cpu, · line 10583
Quoted 10 lines
(quoted diff) +2
-0+ /* symmetric to unaccount_event() in _free_event() */ account_event(event); return event; +err_security: err_addr_filters: kfree(event->addr_filter_ranges);
There's a bunch of problems here I think:
- err_security is named wrong; the naming scheme is to name the label after the last thing that succeeded / first thing that needs to be undone.
- per that, you're forgetting to undo 'get_callchain_buffers()'
- perf_event_release_kernel() is not a full match to perf_event_alloc(), inherited events get created by perf_event_alloc() but never pass through perf_event_release_kernel().
I'm thinking the below patch on top should ammend these issues; please verify.
---
kernel/events/core.c +8 -3
@@ -4540,6 +4540,8 @@ static void _free_event(struct perf_evenunaccount_event (event); +security_perf_event_free (event); +if (event->rb) {/* * Can happen when we close an event with re-directed output.
@@ -4774,7 +4776,6 @@ int perf_event_release_kernel(struct per } no_ctx: -security_perf_event_free (event);put_event (event);/* Must be the 'last' reference */ return 0 ; }
@@ -10595,14 +10596,18 @@ perf_event_alloc(struct perf_event_attr err =security_perf_event_alloc (event);if (err) - goto err_security; + goto err_callchain_buffer;/* symmetric to unaccount_event() in _free_event() */ account_event (event);return event; -err_security: +err_callchain_buffer: +if (!event->parent) { +if (event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) +put_callchain_buffers (); + } err_addr_filters:kfree (event->addr_filter_ranges);
Quoted 31 lines · Peter Zijlstra
Quoted 16 lines · Joel Fernandes (Google)
kernel/events/core.c +4
-0@@ -4761,6 +4762,7 @@ int perf_event_release_kernel(struct perf_event *event) } no_ctx: +security_perf_event_free (event);put_event (event);/* Must be the 'last' reference */ return 0 ; }@@ -10553,11 +10568,16 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu, } } + err =security_perf_event_alloc (event); +if (err) + goto err_security;kernel/events/core.c perf_event_alloc(struct perf_event_attr *attr, int cpu, · line 10583
Quoted 10 lines
kernel/events/core.c +2
-0+/* symmetric to unaccount_event() in _free_event() */ account_event (event);return event; +err_security: err_addr_filters:kfree (event->addr_filter_ranges);There's a bunch of problems here I think:
Quoted 5 lines
-
err_securityis named wrong; the naming scheme is to name the label after the last thing that succeeded / first thing that needs to be undone.- per that, you're forgetting to undo '
get_callchain_buffers()'
Yes, you're right. Tested your fix below. Sorry to miss this.
Quoted 4 lines
-
perf_event_release_kernel()is not a full match toperf_event_alloc(), inherited events get created byperf_event_alloc()but never pass throughperf_event_release_kernel().
Oh, through inherit_event(). Thanks for pointing this semantic out, did not know that.
Quoted 2 lines
I'm thinking the below patch on top should ammend these issues; please verify.
Yes, applied your diff below and verified that the events are getting freed as they were in my initial set of tests. The diff also looks good to me.
I squashed your diff below and will resend as v3. Since you modified this patch a lot, I will add your Co-developed-by tag as well.
thanks, Peter!
- Joel
Quoted 41 lines
---
kernel/events/core.c +8
-3@@ -4540,6 +4540,8 @@ static void _free_event(struct perf_evenunaccount_event (event); +security_perf_event_free (event); +if (event->rb) {/* * Can happen when we close an event with re-directed output. @@ -4774,7 +4776,6 @@ int perf_event_release_kernel(struct per } no_ctx: -security_perf_event_free (event);put_event (event);/* Must be the 'last' reference */ return 0 ; }@@ -10595,14 +10596,18 @@ perf_event_alloc(struct perf_event_attr err =security_perf_event_alloc (event);if (err) - goto err_security; + goto err_callchain_buffer;/* symmetric to unaccount_event() in _free_event() */ account_event (event);return event; -err_security: +err_callchain_buffer: +if (!event->parent) { +if (event->attr.sample_type &PERF_SAMPLE_CALLCHAIN ) +put_callchain_buffers (); + } err_addr_filters:kfree (event->addr_filter_ranges);