perf_event: Add support for LSM and SELinux checks
In current mainline, the degree of access to perf_event_open(2) system
call depends on the perf_event_paranoid sysctl. This has a number of
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
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
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  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.
Since Peter had suggest LSM hooks in 2016 , 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.
RELEASE_NOTE=Backport "perf_event: Add support for LSM and SELinux
Suggested-by: Peter Zijlstra <email@example.com>
Co-developed-by: Peter Zijlstra <firstname.lastname@example.org>
Signed-off-by: Joel Fernandes (Google) <email@example.com>
Signed-off-by: Peter Zijlstra (Intel) <firstname.lastname@example.org>
Acked-by: James Morris <email@example.com>
Cc: Arnaldo Carvalho de Melo <firstname.lastname@example.org>
Cc: Yonghong Song <email@example.com>
Cc: Kees Cook <firstname.lastname@example.org>
Cc: Ingo Molnar <email@example.com>
Cc: Alexei Starovoitov <firstname.lastname@example.org>
Cc: Jiri Olsa <email@example.com>
Cc: Daniel Borkmann <firstname.lastname@example.org>
Cc: Song Liu <email@example.com>
Cc: Namhyung Kim <firstname.lastname@example.org>
Cc: Matthew Garrett <email@example.com>
Reviewed-by: Vaibhav Rustagi <firstname.lastname@example.org>
Reviewed-by: Aditya Kali <email@example.com>
(cherry picked from commit 32f7dedff547c8e35d3a0fa912f1be6639f16cc3)
13 files changed