From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id E763AC61DA4 for ; Sat, 18 Mar 2023 18:37:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=fpXhY2QTwQy2DvT34LbGP1iFGnDlnbkU0CF1r3xLU2U=; b=ExNvTldmgrrJfn UEpETcuUcS5iXWsfxjHGcAKgenhbZDTACNB3BnGFbs5I5CB3/36BumUq+tkcMXVbQtX/qZDb4YrQv PEHAKh2+reXVc5FgQCUd7FLfCQJcgSQTbd2SjiGjEgBMTq+29yYfIpE/otPhef7AUhMDWVV5Hg+hv 5941TiAkd2c8gXCb3/3J3mH0kFYW7uOwUV2t4FhT4zVhe1rpv10Q9dBRBeMDIuEPky3x5VmWhIgol 7kNKJwVIyKtx33gvDsJkskdBi0TW2dEcFteY85NTf+p2x7wx4nNCOK4zGpau7k5ZFwA6s4IQRZDwH WJLCvqoVXKTJJGftev5A==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pdbP4-005AL4-1k; Sat, 18 Mar 2023 18:35:42 +0000 Received: from dfw.source.kernel.org ([139.178.84.217]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pdbP0-005AKW-11; Sat, 18 Mar 2023 18:35:40 +0000 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id D2E7460EF6; Sat, 18 Mar 2023 18:35:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 45902C4339C; Sat, 18 Mar 2023 18:35:35 +0000 (UTC) Date: Sat, 18 Mar 2023 14:35:33 -0400 From: Steven Rostedt To: Cheng-Jui Wang Cc: Masami Hiramatsu , Matthias Brugger , AngeloGioacchino Del Regno , "Tom Zanussi" , , , Tze-nan Wu , , , , , Subject: Re: [PATCH] tracing: Fix use-after-free and double-free on last_cmd Message-ID: <20230318143533.1890d9bc@rorschach.local.home> In-Reply-To: <20230317053044.13828-1-cheng-jui.wang@mediatek.com> References: <20230317053044.13828-1-cheng-jui.wang@mediatek.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230318_113538_396089_47158422 X-CRM114-Status: GOOD ( 21.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, 17 Mar 2023 13:30:44 +0800 Cheng-Jui Wang wrote: > From: "Tze-nan Wu" 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