Linux Trace Kernel
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: "sashiko-bot@kernel.org" <sashiko-bot@kernel.org>,
	"sashiko-reviews@lists.linux.dev"
	<sashiko-reviews@lists.linux.dev>
Cc: David Carlier <devnexen@gmail.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Mathieu Desnoyers <mathieu.desnoyers@efficios.com>,
	linux-trace-kernel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v4] tracing: Fix use-after-free freeing trigger private data
Date: Fri, 24 Jul 2026 09:42:26 -0400	[thread overview]
Message-ID: <20260724094226.39206825@gandalf.local.home> (raw)
In-Reply-To: <20260724030523.19081-1-devnexen@gmail.com>


[ Replying to Sashiko ]

> --- Patch [1]: [PATCH v4] tracing: Fix use-after-free freeing trigger private data ---
> commit cd29772ccd2634c1b696094501e9c7fd0bcd1c25
> Author: David Carlier <devnexen@gmail.com>
> 
> > diff --git a/kernel/trace/trace_events_hist.c b/kernel/trace/trace_events_hist.c
> > index 82ce492ab2681..58d28cd1afa3b 100644
> > --- a/kernel/trace/trace_events_hist.c
> > +++ b/kernel/trace/trace_events_hist.c
> > @@ -6349,6 +6349,7 @@ static void event_hist_trigger_free(struct event_trigger_data *data)
> >  
> >  		trigger_data_free(data);
> >  
> > +		tracepoint_synchronize_unregister();
> >  		remove_hist_vars(hist_data);
> >  
> >  		unregister_field_var_hists(hist_data);
> 
> This isn't a bug introduced by this patch, but since we are fixing the teardown
> paths here, do the error paths in event_hist_trigger_parse() and
> event_enable_trigger_parse() need similar synchronization?
> 
> For example, in event_hist_trigger_parse():
> 
> out_free:
> 	remove_hist_vars(hist_data);
> 	trigger_data_free(trigger_data);
> 	destroy_hist_data(hist_data);
> 
> If trigger registration fails after the trigger was briefly published to
> file->triggers, could a concurrent tracepoint that observed the trigger
> dereference the synchronously freed hist_data because we didn't wait for an RCU
> grace period?

Where was it published to file->triggers?

I see it happening as the last operation that can fail. If that fails, then
we hit the error path where it is not registered. Is there someplace it can
become visible and then fail that I'm not seeing?

From what I see, the last command before "success" is:

  ret = hist_trigger_enable(trigger_data, file);

And that does:

static int hist_trigger_enable(struct event_trigger_data *data,
			       struct trace_event_file *file)
{
	int ret = 0;

	list_add_tail_rcu(&data->list, &file->triggers);

	update_cond_flag(file);

	if (trace_event_trigger_enable_disable(file, 1) < 0) {
		list_del_rcu(&data->list);
		update_cond_flag(file);
		ret--;
	}

	return ret;
}

Now it adds to the file trigger and then tries to enable that event. If it
fails to enable it, it removes it from the list. But if the event is not
enabled, it can't be racing with this.

> 
> [ ... ]
> 
> > diff --git a/kernel/trace/trace_events_trigger.c b/kernel/trace/trace_events_trigger.c
> > index 655db2e825134..46e60b70a4bbc 100644
> > --- a/kernel/trace/trace_events_trigger.c
> > +++ b/kernel/trace/trace_events_trigger.c
> > @@ -1728,9 +1740,9 @@ void event_enable_trigger_free(struct event_trigger_data *data)
> >  	if (!data->ref) {
> >  		/* Remove the SOFT_MODE flag */
> >  		trace_event_enable_disable(enable_data->file, 0, 1);
> > +		data->private_data_free = enable_trigger_private_data_free;
> >  		trace_event_put_ref(enable_data->file->event_call);
> >  		trigger_data_free(data);
> > -		kfree(enable_data);
> >  	}
> >  }
> 
> This is a pre-existing issue, but by deferring the kfree of enable_data while
> keeping trace_event_put_ref() synchronous, could we still face a Use-After-Free
> issue during module unload?
> 
> If trace_event_put_ref(enable_data->file->event_call) allows the target module
> to be unloaded and its trace event files to be freed immediately, could a
> concurrent tracepoint RCU reader dereference the freed enable_data->file in
> event_enable_trigger() when attempting to clear
> EVENT_FILE_FL_SOFT_DISABLED_BIT?

Hmm, this does look like it can be an issue. I'll write up a patch on top
of this one.

-- Steve

      reply	other threads:[~2026-07-24 13:42 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24  3:05 [PATCH v4] tracing: Fix use-after-free freeing trigger private data David Carlier
2026-07-24 13:42 ` Steven Rostedt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260724094226.39206825@gandalf.local.home \
    --to=rostedt@goodmis.org \
    --cc=devnexen@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mathieu.desnoyers@efficios.com \
    --cc=mhiramat@kernel.org \
    --cc=sashiko-bot@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox