Skip to content

Commit da97e18

Browse files
joelagnelPeter Zijlstra
authored and
Peter Zijlstra
committed
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 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. Suggested-by: Peter Zijlstra <[email protected]> Co-developed-by: Peter Zijlstra <[email protected]> Signed-off-by: Joel Fernandes (Google) <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Acked-by: James Morris <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: [email protected] Cc: Yonghong Song <[email protected]> Cc: Kees Cook <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Alexei Starovoitov <[email protected]> Cc: [email protected] Cc: Jiri Olsa <[email protected]> Cc: Daniel Borkmann <[email protected]> Cc: [email protected] Cc: Song Liu <[email protected]> Cc: [email protected] Cc: Namhyung Kim <[email protected]> Cc: Matthew Garrett <[email protected]> Link: https://lkml.kernel.org/r/[email protected]
1 parent 39b656e commit da97e18

File tree

13 files changed

+261
-40
lines changed

13 files changed

+261
-40
lines changed

arch/powerpc/perf/core-book3s.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
9696
{
9797
return 0;
9898
}
99-
static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp) { }
99+
static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp) { }
100100
static inline u32 perf_get_misc_flags(struct pt_regs *regs)
101101
{
102102
return 0;
@@ -127,7 +127,7 @@ static unsigned long ebb_switch_in(bool ebb, struct cpu_hw_events *cpuhw)
127127
static inline void power_pmu_bhrb_enable(struct perf_event *event) {}
128128
static inline void power_pmu_bhrb_disable(struct perf_event *event) {}
129129
static void power_pmu_sched_task(struct perf_event_context *ctx, bool sched_in) {}
130-
static inline void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw) {}
130+
static inline void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw) {}
131131
static void pmao_restore_workaround(bool ebb) { }
132132
#endif /* CONFIG_PPC32 */
133133

@@ -179,7 +179,7 @@ static inline unsigned long perf_ip_adjust(struct pt_regs *regs)
179179
* pointed to by SIAR; this is indicated by the [POWER6_]MMCRA_SDSYNC, the
180180
* [POWER7P_]MMCRA_SDAR_VALID bit in MMCRA, or the SDAR_VALID bit in SIER.
181181
*/
182-
static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
182+
static inline void perf_get_data_addr(struct perf_event *event, struct pt_regs *regs, u64 *addrp)
183183
{
184184
unsigned long mmcra = regs->dsisr;
185185
bool sdar_valid;
@@ -204,8 +204,7 @@ static inline void perf_get_data_addr(struct pt_regs *regs, u64 *addrp)
204204
if (!(mmcra & MMCRA_SAMPLE_ENABLE) || sdar_valid)
205205
*addrp = mfspr(SPRN_SDAR);
206206

207-
if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
208-
is_kernel_addr(mfspr(SPRN_SDAR)))
207+
if (is_kernel_addr(mfspr(SPRN_SDAR)) && perf_allow_kernel(&event->attr) != 0)
209208
*addrp = 0;
210209
}
211210

@@ -444,7 +443,7 @@ static __u64 power_pmu_bhrb_to(u64 addr)
444443
}
445444

446445
/* Processing BHRB entries */
447-
static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
446+
static void power_pmu_bhrb_read(struct perf_event *event, struct cpu_hw_events *cpuhw)
448447
{
449448
u64 val;
450449
u64 addr;
@@ -472,8 +471,7 @@ static void power_pmu_bhrb_read(struct cpu_hw_events *cpuhw)
472471
* exporting it to userspace (avoid exposure of regions
473472
* where we could have speculative execution)
474473
*/
475-
if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN) &&
476-
is_kernel_addr(addr))
474+
if (is_kernel_addr(addr) && perf_allow_kernel(&event->attr) != 0)
477475
continue;
478476

479477
/* Branches are read most recent first (ie. mfbhrb 0 is
@@ -2087,12 +2085,12 @@ static void record_and_restart(struct perf_event *event, unsigned long val,
20872085

20882086
if (event->attr.sample_type &
20892087
(PERF_SAMPLE_ADDR | PERF_SAMPLE_PHYS_ADDR))
2090-
perf_get_data_addr(regs, &data.addr);
2088+
perf_get_data_addr(event, regs, &data.addr);
20912089

20922090
if (event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK) {
20932091
struct cpu_hw_events *cpuhw;
20942092
cpuhw = this_cpu_ptr(&cpu_hw_events);
2095-
power_pmu_bhrb_read(cpuhw);
2093+
power_pmu_bhrb_read(event, cpuhw);
20962094
data.br_stack = &cpuhw->bhrb_stack;
20972095
}
20982096

arch/x86/events/intel/bts.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -549,9 +549,11 @@ static int bts_event_init(struct perf_event *event)
549549
* Note that the default paranoia setting permits unprivileged
550550
* users to profile the kernel.
551551
*/
552-
if (event->attr.exclude_kernel && perf_paranoid_kernel() &&
553-
!capable(CAP_SYS_ADMIN))
554-
return -EACCES;
552+
if (event->attr.exclude_kernel) {
553+
ret = perf_allow_kernel(&event->attr);
554+
if (ret)
555+
return ret;
556+
}
555557

556558
if (x86_add_exclusive(x86_lbr_exclusive_bts))
557559
return -EBUSY;

arch/x86/events/intel/core.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3315,8 +3315,9 @@ static int intel_pmu_hw_config(struct perf_event *event)
33153315
if (x86_pmu.version < 3)
33163316
return -EINVAL;
33173317

3318-
if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
3319-
return -EACCES;
3318+
ret = perf_allow_cpu(&event->attr);
3319+
if (ret)
3320+
return ret;
33203321

33213322
event->hw.config |= ARCH_PERFMON_EVENTSEL_ANY;
33223323

arch/x86/events/intel/p4.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,8 +776,9 @@ static int p4_validate_raw_event(struct perf_event *event)
776776
* the user needs special permissions to be able to use it
777777
*/
778778
if (p4_ht_active() && p4_event_bind_map[v].shared) {
779-
if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
780-
return -EACCES;
779+
v = perf_allow_cpu(&event->attr);
780+
if (v)
781+
return v;
781782
}
782783

783784
/* ESCR EventMask bits may be invalid */

include/linux/lsm_hooks.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,6 +1818,14 @@ union security_list_options {
18181818
void (*bpf_prog_free_security)(struct bpf_prog_aux *aux);
18191819
#endif /* CONFIG_BPF_SYSCALL */
18201820
int (*locked_down)(enum lockdown_reason what);
1821+
#ifdef CONFIG_PERF_EVENTS
1822+
int (*perf_event_open)(struct perf_event_attr *attr, int type);
1823+
int (*perf_event_alloc)(struct perf_event *event);
1824+
void (*perf_event_free)(struct perf_event *event);
1825+
int (*perf_event_read)(struct perf_event *event);
1826+
int (*perf_event_write)(struct perf_event *event);
1827+
1828+
#endif
18211829
};
18221830

18231831
struct security_hook_heads {
@@ -2060,6 +2068,13 @@ struct security_hook_heads {
20602068
struct hlist_head bpf_prog_free_security;
20612069
#endif /* CONFIG_BPF_SYSCALL */
20622070
struct hlist_head locked_down;
2071+
#ifdef CONFIG_PERF_EVENTS
2072+
struct hlist_head perf_event_open;
2073+
struct hlist_head perf_event_alloc;
2074+
struct hlist_head perf_event_free;
2075+
struct hlist_head perf_event_read;
2076+
struct hlist_head perf_event_write;
2077+
#endif
20632078
} __randomize_layout;
20642079

20652080
/*

include/linux/perf_event.h

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ struct perf_guest_info_callbacks {
5656
#include <linux/perf_regs.h>
5757
#include <linux/cgroup.h>
5858
#include <linux/refcount.h>
59+
#include <linux/security.h>
5960
#include <asm/local.h>
6061

6162
struct perf_callchain_entry {
@@ -721,6 +722,9 @@ struct perf_event {
721722
struct perf_cgroup *cgrp; /* cgroup event is attach to */
722723
#endif
723724

725+
#ifdef CONFIG_SECURITY
726+
void *security;
727+
#endif
724728
struct list_head sb_list;
725729
#endif /* CONFIG_PERF_EVENTS */
726730
};
@@ -1241,19 +1245,41 @@ extern int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
12411245
int perf_event_max_stack_handler(struct ctl_table *table, int write,
12421246
void __user *buffer, size_t *lenp, loff_t *ppos);
12431247

1244-
static inline bool perf_paranoid_tracepoint_raw(void)
1248+
/* Access to perf_event_open(2) syscall. */
1249+
#define PERF_SECURITY_OPEN 0
1250+
1251+
/* Finer grained perf_event_open(2) access control. */
1252+
#define PERF_SECURITY_CPU 1
1253+
#define PERF_SECURITY_KERNEL 2
1254+
#define PERF_SECURITY_TRACEPOINT 3
1255+
1256+
static inline int perf_is_paranoid(void)
12451257
{
12461258
return sysctl_perf_event_paranoid > -1;
12471259
}
12481260

1249-
static inline bool perf_paranoid_cpu(void)
1261+
static inline int perf_allow_kernel(struct perf_event_attr *attr)
12501262
{
1251-
return sysctl_perf_event_paranoid > 0;
1263+
if (sysctl_perf_event_paranoid > 1 && !capable(CAP_SYS_ADMIN))
1264+
return -EACCES;
1265+
1266+
return security_perf_event_open(attr, PERF_SECURITY_KERNEL);
12521267
}
12531268

1254-
static inline bool perf_paranoid_kernel(void)
1269+
static inline int perf_allow_cpu(struct perf_event_attr *attr)
12551270
{
1256-
return sysctl_perf_event_paranoid > 1;
1271+
if (sysctl_perf_event_paranoid > 0 && !capable(CAP_SYS_ADMIN))
1272+
return -EACCES;
1273+
1274+
return security_perf_event_open(attr, PERF_SECURITY_CPU);
1275+
}
1276+
1277+
static inline int perf_allow_tracepoint(struct perf_event_attr *attr)
1278+
{
1279+
if (sysctl_perf_event_paranoid > -1 && !capable(CAP_SYS_ADMIN))
1280+
return -EPERM;
1281+
1282+
return security_perf_event_open(attr, PERF_SECURITY_TRACEPOINT);
12571283
}
12581284

12591285
extern void perf_event_init(void);

include/linux/security.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,5 +1894,41 @@ static inline void security_bpf_prog_free(struct bpf_prog_aux *aux)
18941894
#endif /* CONFIG_SECURITY */
18951895
#endif /* CONFIG_BPF_SYSCALL */
18961896

1897-
#endif /* ! __LINUX_SECURITY_H */
1897+
#ifdef CONFIG_PERF_EVENTS
1898+
struct perf_event_attr;
1899+
1900+
#ifdef CONFIG_SECURITY
1901+
extern int security_perf_event_open(struct perf_event_attr *attr, int type);
1902+
extern int security_perf_event_alloc(struct perf_event *event);
1903+
extern void security_perf_event_free(struct perf_event *event);
1904+
extern int security_perf_event_read(struct perf_event *event);
1905+
extern int security_perf_event_write(struct perf_event *event);
1906+
#else
1907+
static inline int security_perf_event_open(struct perf_event_attr *attr,
1908+
int type)
1909+
{
1910+
return 0;
1911+
}
1912+
1913+
static inline int security_perf_event_alloc(struct perf_event *event)
1914+
{
1915+
return 0;
1916+
}
1917+
1918+
static inline void security_perf_event_free(struct perf_event *event)
1919+
{
1920+
}
1921+
1922+
static inline int security_perf_event_read(struct perf_event *event)
1923+
{
1924+
return 0;
1925+
}
18981926

1927+
static inline int security_perf_event_write(struct perf_event *event)
1928+
{
1929+
return 0;
1930+
}
1931+
#endif /* CONFIG_SECURITY */
1932+
#endif /* CONFIG_PERF_EVENTS */
1933+
1934+
#endif /* ! __LINUX_SECURITY_H */

kernel/events/core.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,8 +4229,9 @@ find_get_context(struct pmu *pmu, struct task_struct *task,
42294229

42304230
if (!task) {
42314231
/* Must be root to operate on a CPU event: */
4232-
if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
4233-
return ERR_PTR(-EACCES);
4232+
err = perf_allow_cpu(&event->attr);
4233+
if (err)
4234+
return ERR_PTR(err);
42344235

42354236
cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
42364237
ctx = &cpuctx->ctx;
@@ -4539,6 +4540,8 @@ static void _free_event(struct perf_event *event)
45394540

45404541
unaccount_event(event);
45414542

4543+
security_perf_event_free(event);
4544+
45424545
if (event->rb) {
45434546
/*
45444547
* Can happen when we close an event with re-directed output.
@@ -4992,6 +4995,10 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
49924995
struct perf_event_context *ctx;
49934996
int ret;
49944997

4998+
ret = security_perf_event_read(event);
4999+
if (ret)
5000+
return ret;
5001+
49955002
ctx = perf_event_ctx_lock(event);
49965003
ret = __perf_read(event, buf, count);
49975004
perf_event_ctx_unlock(event, ctx);
@@ -5256,6 +5263,11 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
52565263
struct perf_event_context *ctx;
52575264
long ret;
52585265

5266+
/* Treat ioctl like writes as it is likely a mutating operation. */
5267+
ret = security_perf_event_write(event);
5268+
if (ret)
5269+
return ret;
5270+
52595271
ctx = perf_event_ctx_lock(event);
52605272
ret = _perf_ioctl(event, cmd, arg);
52615273
perf_event_ctx_unlock(event, ctx);
@@ -5719,6 +5731,10 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
57195731
if (!(vma->vm_flags & VM_SHARED))
57205732
return -EINVAL;
57215733

5734+
ret = security_perf_event_read(event);
5735+
if (ret)
5736+
return ret;
5737+
57225738
vma_size = vma->vm_end - vma->vm_start;
57235739

57245740
if (vma->vm_pgoff == 0) {
@@ -5844,7 +5860,7 @@ static int perf_mmap(struct file *file, struct vm_area_struct *vma)
58445860
lock_limit >>= PAGE_SHIFT;
58455861
locked = atomic64_read(&vma->vm_mm->pinned_vm) + extra;
58465862

5847-
if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
5863+
if ((locked > lock_limit) && perf_is_paranoid() &&
58485864
!capable(CAP_IPC_LOCK)) {
58495865
ret = -EPERM;
58505866
goto unlock;
@@ -10578,11 +10594,20 @@ perf_event_alloc(struct perf_event_attr *attr, int cpu,
1057810594
}
1057910595
}
1058010596

10597+
err = security_perf_event_alloc(event);
10598+
if (err)
10599+
goto err_callchain_buffer;
10600+
1058110601
/* symmetric to unaccount_event() in _free_event() */
1058210602
account_event(event);
1058310603

1058410604
return event;
1058510605

10606+
err_callchain_buffer:
10607+
if (!event->parent) {
10608+
if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
10609+
put_callchain_buffers();
10610+
}
1058610611
err_addr_filters:
1058710612
kfree(event->addr_filter_ranges);
1058810613

@@ -10671,9 +10696,11 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
1067110696
attr->branch_sample_type = mask;
1067210697
}
1067310698
/* privileged levels capture (kernel, hv): check permissions */
10674-
if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
10675-
&& perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
10676-
return -EACCES;
10699+
if (mask & PERF_SAMPLE_BRANCH_PERM_PLM) {
10700+
ret = perf_allow_kernel(attr);
10701+
if (ret)
10702+
return ret;
10703+
}
1067710704
}
1067810705

1067910706
if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
@@ -10886,13 +10913,19 @@ SYSCALL_DEFINE5(perf_event_open,
1088610913
if (flags & ~PERF_FLAG_ALL)
1088710914
return -EINVAL;
1088810915

10916+
/* Do we allow access to perf_event_open(2) ? */
10917+
err = security_perf_event_open(&attr, PERF_SECURITY_OPEN);
10918+
if (err)
10919+
return err;
10920+
1088910921
err = perf_copy_attr(attr_uptr, &attr);
1089010922
if (err)
1089110923
return err;
1089210924

1089310925
if (!attr.exclude_kernel) {
10894-
if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
10895-
return -EACCES;
10926+
err = perf_allow_kernel(&attr);
10927+
if (err)
10928+
return err;
1089610929
}
1089710930

1089810931
if (attr.namespaces) {
@@ -10909,9 +10942,11 @@ SYSCALL_DEFINE5(perf_event_open,
1090910942
}
1091010943

1091110944
/* Only privileged users can get physical addresses */
10912-
if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR) &&
10913-
perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
10914-
return -EACCES;
10945+
if ((attr.sample_type & PERF_SAMPLE_PHYS_ADDR)) {
10946+
err = perf_allow_kernel(&attr);
10947+
if (err)
10948+
return err;
10949+
}
1091510950

1091610951
err = security_locked_down(LOCKDOWN_PERF);
1091710952
if (err && (attr.sample_type & PERF_SAMPLE_REGS_INTR))

0 commit comments

Comments
 (0)