Re: [PATCH] perf: Allow restricted kernel breakpoints on user addresses

From: Marco Elver
Date: Thu Sep 08 2022 - 03:59:40 EST


On Wed, 7 Sept 2022 at 14:39, Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Fri, Sep 02, 2022 at 12:00:57PM +0200, Marco Elver wrote:
>
> > +/*
> > + * Check if unprivileged users are allowed to set up breakpoints on user
> > + * addresses that also count when the kernel accesses them.
> > + */
> > +static bool perf_allow_kernel_breakpoint(struct perf_event_attr *attr)
> > +{
> > + if (attr->type != PERF_TYPE_BREAKPOINT)
> > + return false;
> > +
> > + /*
> > + * The sample may contain IPs, registers, or other information that may
> > + * disclose kernel addresses or timing information. Disallow any kind of
> > + * additional sample information.
> > + */
> > + if (attr->sample_type)
> > + return false;
>
> This feels a bit weird; should that perhaps be is_sampling_event()?

is_sampling_event() just checks for sample_period. In fact, we still
want to set sample_period to get overflow events. That in itself is
not dangerous.

What's problematic is if the samples contain additional information,
which can be specified in sample_type. For example if PERF_SAMPLE_IP
is set, it might leak kernel IPs, and that's bad. Since it's safest to
disallow any kind of extra information, we just check if sample_type
is zero.

> > +
> > + /*
> > + * Only allow kernel breakpoints on user addresses.
> > + */
> > + return access_ok((void __user *)(unsigned long)attr->bp_addr, attr->bp_len);
> > +}
> > +
> > +int perf_allow_kernel(struct perf_event_attr *attr)
> > +{
> > + if (sysctl_perf_event_paranoid > 1 && !perfmon_capable() &&
> > + !perf_allow_kernel_breakpoint(attr))
>
> I'm on the fence about this; one the one hand it feels weird to have a
> breakpoint exception here and not a pmu specific callback for instance;
> OTOH, leaving security policy like that up to pmu drivers sounds like a
> really bad idea too.
>
> Keep it as is I suppose, just me thinking out loud or so.

Ack. I also think this should stay in core, as it's also easier to audit.