Re: perf: use-after-free in perf_release

From: Peter Zijlstra
Date: Tue Mar 14 2017 - 09:47:31 EST


On Tue, Mar 14, 2017 at 02:24:27PM +0100, Oleg Nesterov wrote:
> On 03/14, Peter Zijlstra wrote:
> >
> > On Tue, Mar 07, 2017 at 05:51:32PM +0100, Oleg Nesterov wrote:
> >
> > > inherit_event() returns NULL under is_orphaned_event() check, not ERR_PTR().
> > > Is it correct?
> >
> > Yes. This is all a tad tricky, but it seems to be correct.
> >
> > By returning NULL, not an error, we affect the silent discard of
> > orphaned events. This is correct, because otherwise
> > perf_event_release_kernel() would have come by and explicitly discarded
> > those events for us anyway.
>
> Thanks... I'll try to understand this later.
>
> > @@ -10608,7 +10627,6 @@ inherit_task_group(struct perf_event *event, struct task_struct *parent,
> > * First allocate and initialize a context for the
> > * child.
> > */
> > -
> > child_ctx = alloc_perf_context(parent_ctx->pmu, child);
> > if (!child_ctx)
> > return -ENOMEM;
> > @@ -10670,7 +10688,7 @@ static int perf_event_init_context(struct task_struct *child, int ctxn)
> > ret = inherit_task_group(event, parent, parent_ctx,
> > child, ctxn, &inherited_all);
> > if (ret)
> > - break;
> > + goto out_unlock;
> > }
> >
> > /*
> > @@ -10686,7 +10704,7 @@ static int perf_event_init_context(struct task_struct *child, int ctxn)
> > ret = inherit_task_group(event, parent, parent_ctx,
> > child, ctxn, &inherited_all);
> > if (ret)
> > - break;
> > + goto out_unlock;
>
> With this change you can also simplify inherit_task_group() a little bit,
> it no longer needs to nullify *inherited_all if inherit_group() fails.

Ah, that last one is broken because then we forget to re-enable
parent_ctx->rotate_disable.

So if we keep that a break, we still need that inherited_all thing as
well.