Re: [PATCH 1/3] [BUGFIX] tracing: Fix double free of event_trigger_data

From: Masami Hiramatsu
Date: Tue Jul 24 2018 - 21:05:29 EST


On Tue, 24 Jul 2018 16:49:59 -0400
Steven Rostedt <rostedt@xxxxxxxxxxx> wrote:

> On Wed, 25 Jul 2018 00:09:09 +0900
> Masami Hiramatsu <mhiramat@xxxxxxxxxx> wrote:
>
> > Hmm, your patch seems to leak a memory since event_trigger_init() will
> > be called twice on same trigger_data (Note that event_trigger_init()
> > does not init ref counter, but increment it.) So we should decrement
> > it when we find it is succeeded. Moreover, if register_trigger()
>
> Good catch, and easily fixed.
>
> > fails before calling data->ops->init() (see -EEXIST case), the ref
> > counter will be 0 (-1 +1). But if it fails after data->ops->init(),
> > the ref counter will be 1 (-1 +1 +1). It still be unstable.
> > (Ah, that means we may have another trouble...)
>
> I'm not sure there's a problem here. I now have:
>
> out_reg:
> /* Up the trigger_data count to make sure reg doesn't free it on failuer */
> event_trigger_init(trigger_ops, trigger_data);
> ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file);
> /*
> * The above returns on success the # of functions enabled,
> * but if it didn't find any functions it returns zero.
> * Consider no functions a failure too.
> */
> if (!ret) {
> ret = -ENOENT;
> } else if (ret > 0)
> ret = 0;

Can we mixed up ret == 0 and ret > 0? It seems cmd_ops->reg() == 0
is a failure case.

>
> /* Down the counter of trigger_data or free it if not used anymore */
> event_trigger_free(trigger_ops, trigger_data);
> out:
> return ret;
>
> Thus we increment trigger_data before calling reg, and free it
> afterward. But if reg() did an init too, then the event_trigger_free()
> just decs the ref counter.

To avoid confusion, I would like to suggest to rename those pair to
event_trigger_data_get/put(). :)

>
> As for register_trigger()
>
>
> >
> > >
> > > P.S. This brings up another minor bug. The failure should return ENOMEM
> > > not ENOENT.
> >
> > Hmm it seems we should review the register_trigger() implementation.
> > It should return the return value of trace_event_trigger_enable_disable(),
> > shouldn't it?
> >
>
> Yeah, that's not done well. I'll fix it up.

Thanks!
>
> Thanks for pointing it out.
>
> -- Steve


--
Masami Hiramatsu <mhiramat@xxxxxxxxxx>