public inbox for linux-arm-kernel@lists.infradead.org
 help / color / mirror / Atom feed
From: Steven Rostedt <rostedt@goodmis.org>
To: Cheng-Jui Wang <cheng-jui.wang@mediatek.com>
Cc: Masami Hiramatsu <mhiramat@kernel.org>,
	Matthias Brugger <matthias.bgg@gmail.com>,
	AngeloGioacchino Del Regno
	<angelogioacchino.delregno@collabora.com>,
	"Tom Zanussi" <zanussi@kernel.org>, <Bobule.chang@mediatek.com>,
	<wsd_upstream@mediatek.com>, Tze-nan Wu <Tze-nan.Wu@mediatek.com>,
	<stable@vger.kernel.org>, <linux-kernel@vger.kernel.org>,
	<linux-trace-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>,
	<linux-mediatek@lists.infradead.org>
Subject: Re: [PATCH] tracing: Fix use-after-free and double-free on last_cmd
Date: Sat, 18 Mar 2023 14:35:33 -0400	[thread overview]
Message-ID: <20230318143533.1890d9bc@rorschach.local.home> (raw)
In-Reply-To: <20230317053044.13828-1-cheng-jui.wang@mediatek.com>

On Fri, 17 Mar 2023 13:30:44 +0800
Cheng-Jui Wang <cheng-jui.wang@mediatek.com> wrote:

> From: "Tze-nan Wu" <Tze-nan.Wu@mediatek.com>

Hi!

Thanks for the report and the patch. Some nits below.

Also change the subject to:

	tracing/synthetic: Fix races on freeing last_cmd

> ---
>  kernel/trace/trace_events_synth.c | 23 +++++++++++++++++++----
>  1 file changed, 19 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/trace/trace_events_synth.c b/kernel/trace/trace_events_synth.c
> index 46d0abb32d0f..ce438eccab2e 100644
> --- a/kernel/trace/trace_events_synth.c
> +++ b/kernel/trace/trace_events_synth.c
> @@ -42,16 +42,25 @@ enum { ERRORS };
>  #undef C
>  #define C(a, b)		b
>  
> +static DEFINE_MUTEX(lastcmd_mutex);
> +
>  static const char *err_text[] = { ERRORS };
>  
>  static char *last_cmd;

Please keep the mutex and the variable it protects next to each other:

static DEFINE_MUTEX(lastcmd_mutex);
static char *last_cmd;

>  
>  static int errpos(const char *str)
>  {
> -	if (!str || !last_cmd)
> -		return 0;
> +	int ret = 0;
> +
> +	mutex_lock(&lastcmd_mutex);
> +	if (!str || !last_cmd) {

Change this to just:

	if (!str || !last_cmd)
		goto out;

> +		mutex_unlock(&lastcmd_mutex);
> +		return ret;

> +	}
>  
> -	return err_pos(last_cmd, str);
> +	ret = err_pos(last_cmd, str);

Add:

 out:

> +	mutex_unlock(&lastcmd_mutex);
> +	return ret;
>  }
>  
>  static void last_cmd_set(const char *str)
> @@ -59,18 +68,24 @@ static void last_cmd_set(const char *str)
>  	if (!str)
>  		return;
>  
> +	mutex_lock(&lastcmd_mutex);
>  	kfree(last_cmd);
>  

In this case, you can remove the space:

	mutex_lock(&lastcmd_mutex);
	kfree(last_cmd);
	last_cmd = kstrdup(str, GFP_KERNEL);
	mutex_unlock(&lastcmd_mutex);

>  	last_cmd = kstrdup(str, GFP_KERNEL);
> +	mutex_unlock(&lastcmd_mutex);
>  }
>  
>  static void synth_err(u8 err_type, u16 err_pos)
>  {
> -	if (!last_cmd)
> +	mutex_lock(&lastcmd_mutex);
> +	if (!last_cmd) {

This should be:

	if (!last_cmd)
		goto out;

> +		mutex_unlock(&lastcmd_mutex);
>  		return;
> +	}
>  
>  	tracing_log_err(NULL, "synthetic_events", last_cmd, err_text,
>  			err_type, err_pos);

 out:

> +	mutex_unlock(&lastcmd_mutex);
>  }
>  
>  static int create_synth_event(const char *raw_command);

Thanks,

-- Steve

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  reply	other threads:[~2023-03-18 18:37 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-17  5:30 [PATCH] tracing: Fix use-after-free and double-free on last_cmd Cheng-Jui Wang
2023-03-18 18:35 ` Steven Rostedt [this message]
2023-03-21 11:23   ` Tze-nan Wu (吳澤南)

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=20230318143533.1890d9bc@rorschach.local.home \
    --to=rostedt@goodmis.org \
    --cc=Bobule.chang@mediatek.com \
    --cc=Tze-nan.Wu@mediatek.com \
    --cc=angelogioacchino.delregno@collabora.com \
    --cc=cheng-jui.wang@mediatek.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mediatek@lists.infradead.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=matthias.bgg@gmail.com \
    --cc=mhiramat@kernel.org \
    --cc=stable@vger.kernel.org \
    --cc=wsd_upstream@mediatek.com \
    --cc=zanussi@kernel.org \
    /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