Skip to content

Commit cc1582c

Browse files
Jin YaoIngo Molnar
authored andcommitted
perf/core: Drop kernel samples even though :u is specified
When doing sampling, for example: perf record -e cycles:u ... On workloads that do a lot of kernel entry/exits we see kernel samples, even though :u is specified. This is due to skid existing. This might be a security issue because it can leak kernel addresses even though kernel sampling support is disabled. The patch drops the kernel samples if exclude_kernel is specified. For example, test on Haswell desktop: perf record -e cycles:u <mgen> perf report --stdio Before patch applied: 99.77% mgen mgen [.] buf_read 0.20% mgen mgen [.] rand_buf_init 0.01% mgen [kernel.vmlinux] [k] apic_timer_interrupt 0.00% mgen mgen [.] last_free_elem 0.00% mgen libc-2.23.so [.] __random_r 0.00% mgen libc-2.23.so [.] _int_malloc 0.00% mgen mgen [.] rand_array_init 0.00% mgen [kernel.vmlinux] [k] page_fault 0.00% mgen libc-2.23.so [.] __random 0.00% mgen libc-2.23.so [.] __strcasestr 0.00% mgen ld-2.23.so [.] strcmp 0.00% mgen ld-2.23.so [.] _dl_start 0.00% mgen libc-2.23.so [.] sched_setaffinity@@GLIBC_2.3.4 0.00% mgen ld-2.23.so [.] _start We can see kernel symbols apic_timer_interrupt and page_fault. After patch applied: 99.79% mgen mgen [.] buf_read 0.19% mgen mgen [.] rand_buf_init 0.00% mgen libc-2.23.so [.] __random_r 0.00% mgen mgen [.] rand_array_init 0.00% mgen mgen [.] last_free_elem 0.00% mgen libc-2.23.so [.] vfprintf 0.00% mgen libc-2.23.so [.] rand 0.00% mgen libc-2.23.so [.] __random 0.00% mgen libc-2.23.so [.] _int_malloc 0.00% mgen libc-2.23.so [.] _IO_doallocbuf 0.00% mgen ld-2.23.so [.] do_lookup_x 0.00% mgen ld-2.23.so [.] open_verify.constprop.7 0.00% mgen ld-2.23.so [.] _dl_important_hwcaps 0.00% mgen libc-2.23.so [.] sched_setaffinity@@GLIBC_2.3.4 0.00% mgen ld-2.23.so [.] _start There are only userspace symbols. Signed-off-by: Jin Yao <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Cc: <[email protected]> Cc: Alexander Shishkin <[email protected]> Cc: Arnaldo Carvalho de Melo <[email protected]> Cc: Jiri Olsa <[email protected]> Cc: Linus Torvalds <[email protected]> Cc: Namhyung Kim <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: Stephane Eranian <[email protected]> Cc: Thomas Gleixner <[email protected]> Cc: Vince Weaver <[email protected]> Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Cc: [email protected] Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Ingo Molnar <[email protected]>
1 parent 3e411b0 commit cc1582c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

kernel/events/core.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7316,6 +7316,21 @@ int perf_event_account_interrupt(struct perf_event *event)
73167316
return __perf_event_account_interrupt(event, 1);
73177317
}
73187318

7319+
static bool sample_is_allowed(struct perf_event *event, struct pt_regs *regs)
7320+
{
7321+
/*
7322+
* Due to interrupt latency (AKA "skid"), we may enter the
7323+
* kernel before taking an overflow, even if the PMU is only
7324+
* counting user events.
7325+
* To avoid leaking information to userspace, we must always
7326+
* reject kernel samples when exclude_kernel is set.
7327+
*/
7328+
if (event->attr.exclude_kernel && !user_mode(regs))
7329+
return false;
7330+
7331+
return true;
7332+
}
7333+
73197334
/*
73207335
* Generic event overflow handling, sampling.
73217336
*/
@@ -7336,6 +7351,12 @@ static int __perf_event_overflow(struct perf_event *event,
73367351

73377352
ret = __perf_event_account_interrupt(event, throttle);
73387353

7354+
/*
7355+
* For security, drop the skid kernel samples if necessary.
7356+
*/
7357+
if (!sample_is_allowed(event, regs))
7358+
return ret;
7359+
73397360
/*
73407361
* XXX event_limit might not quite work as expected on inherited
73417362
* events

0 commit comments

Comments
 (0)