Re: [PATCH v1 2/2] perf/core: Fake regs for leaked kernel samples

From: peterz
Date: Wed Aug 05 2020 - 12:50:08 EST


On Wed, Aug 05, 2020 at 02:44:54PM +0200, peterz@xxxxxxxxxxxxx wrote:

> How's this?

Clearly I didn't even hold it near a compiler...

> ---
> kernel/events/core.c | 38 +++++++++++++++++++++++++++++++++-----
> 1 file changed, 33 insertions(+), 5 deletions(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index 7c436d705fbd..3e4e328b521a 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -6988,23 +6988,49 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
> return callchain ?: &__empty_callchain;
> }
>
> +/*
> + * Due to interrupt latency (skid), we may enter the kernel before taking the
> + * PMI, even if the PMU is configured to only count user events. To avoid
> + * leaking kernel addresses, use task_pt_regs(), when available.
> + */
> +static struct pt_regs *sanitize_sample_regs(struct perf_event *event, struct pt_regs *regs)
> +{
> + struct pt_regs *sample_regs = regs;
> +
> + /* user only */
> + if (!event->attr.exclude_kernel || !event->attr.exclude_hv ||
> + !event->attr.exclude_host || !event->attr.exclude_guest)
> + return sample_regs;
> +
> + if (sample_regs(regs))
> + return sample_regs;

That wants to he:

if (user_regs(regs))
return sample_regs;

> +
> + if (!(current->flags & PF_KTHREAD)) {

s/{//

> + sample_regs = task_pt_regs(current);
> + else
> + instruction_pointer_set(regs, -1L);
> +
> + return sample_regs;
> +}