Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3800905ybi; Mon, 29 Jul 2019 12:53:58 -0700 (PDT) X-Google-Smtp-Source: APXvYqyZtkq++M60IDQvbn3KfDUqag4b4HEnu0HohxFcvrblvNkzxlWx/2NH7lMyMH0GsWYQmIB3 X-Received: by 2002:aa7:9092:: with SMTP id i18mr37977448pfa.101.1564430038410; Mon, 29 Jul 2019 12:53:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564430038; cv=none; d=google.com; s=arc-20160816; b=KRQ9aPJ5nxf0nYgQwNSkDBmie3ePBickzltwz0Cpw7ZQ1xrgqFvGWqMtoV5uPEu51+ U3yQrQRcn06qX/bAr9b5wKK3c1k7q17PvYsmW1LsNTg6bWxgLJ4xHsZ3WgxE53RcWlt8 CwyfQyrvqu3rBCFyBKkuUYu0J09VGl/rHhKuT7m2NeRfH4Ud7wNsRPcwTgXvrEIemE/Q MKQZSR7HbXh3k0nDyuh+k7JrXL0JwPK7IxhL91ccmfQu33KWEU8CAjNtoAZPGyv9S3IP 4vRKHSNJIHL3VaSyCVNEbrN512LJQkM034fi2fBb0chx6AsHU/Qo/iiloG0BhNIXwCds sBvQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=BP/g5aAhTLfQKw6Jf7Dj/r/PutAYxFPoHluuCbiCClg=; b=lNEH7qFCsE/8zg8F/nGKf4wtKFpEN32gJF39wz5hgGbh4PQE/J9zEm44kiyLX4NxL7 3YgVU63c8EP2XWR2nqrQoXBVWAadBC5amU+nnxP2Qdv7u2tnkrCgAyZrC54oaJ+Gri8f vsggykFEuI94+9UI+tJI2mzSW/Hv5ndbNCzpQk4mmFe4N9kWE6twaQ/PTMKBydXwdYlN sC1cKQPS5pG86N8sFtACUz/bY09gK6Uo7jY0OcOGu2yCRGmf+M1ImbhTxW5GFs8AzYDb WIi2eV/6wr+HNgXw1z1WgtvIT+ZHoWyuA13czwuXFHbSgJzLA597DoMB0li0++BUgEwh WZqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=PFE5piDe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m143si28478719pfd.224.2019.07.29.12.53.43; Mon, 29 Jul 2019 12:53:58 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=PFE5piDe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2390548AbfG2TvY (ORCPT + 99 others); Mon, 29 Jul 2019 15:51:24 -0400 Received: from mail.kernel.org ([198.145.29.99]:42710 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2403850AbfG2TvW (ORCPT ); Mon, 29 Jul 2019 15:51:22 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 36B00217D6; Mon, 29 Jul 2019 19:51:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564429881; bh=weIz1uBlyldbyeAv5HDGI4Fix09FhaoFEQLoHR14ie8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=PFE5piDe3XbzXX/b9rJFPGMucgtkiA6Q1t03tAX/4w/kAtMu/+vNYA9PlJ8LPY9GA +UL+Mm5jpZJU0opd5KKO5aek80m/a6E9vS2b6a0hSemo9hkKOHZB1pvcVvA2fD7GfV 6OkpmdmOXnxfwyyFMkPKVze78VmE8P8w1Im6pjJA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Leo Yan , Jiri Olsa , Adrian Hunter , Alexander Shishkin , Alexey Budankov , Alexios Zavras , Andi Kleen , Changbin Du , "David S. Miller" , Davidlohr Bueso , Eric Saint-Etienne , Jin Yao , Konstantin Khlebnikov , Mathieu Poirier , Namhyung Kim , Peter Zijlstra , Rasmus Villemoes , Song Liu , Suzuki Poulouse , Thomas Gleixner , Thomas Richter , linux-arm-kernel@lists.infradead.org, Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 5.2 126/215] perf annotate: Fix dereferencing freed memory found by the smatch tool Date: Mon, 29 Jul 2019 21:22:02 +0200 Message-Id: <20190729190801.169409742@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190739.971253303@linuxfoundation.org> References: <20190729190739.971253303@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org [ Upstream commit 600c787dbf6521d8d07ee717ab7606d5070103ea ] Based on the following report from Smatch, fix the potential dereferencing freed memory check. tools/perf/util/annotate.c:1125 disasm_line__parse() error: dereferencing freed memory 'namep' tools/perf/util/annotate.c 1100 static int disasm_line__parse(char *line, const char **namep, char **rawp) 1101 { 1102 char tmp, *name = ltrim(line); [...] 1114 *namep = strdup(name); 1115 1116 if (*namep == NULL) 1117 goto out_free_name; [...] 1124 out_free_name: 1125 free((void *)namep); ^^^^^ 1126 *namep = NULL; ^^^^^^ 1127 return -1; 1128 } If strdup() fails to allocate memory space for *namep, we don't need to free memory with pointer 'namep', which is resident in data structure disasm_line::ins::name; and *namep is NULL pointer for this failure, so it's pointless to assign NULL to *namep again. Committer note: Freeing namep, which is the address of the first entry of the 'struct ins' that is the first member of struct disasm_line would in fact free that disasm_line instance, if it was allocated via malloc/calloc, which, later, would a dereference of freed memory. Signed-off-by: Leo Yan Acked-by: Jiri Olsa Cc: Adrian Hunter Cc: Alexander Shishkin Cc: Alexey Budankov Cc: Alexios Zavras Cc: Andi Kleen Cc: Changbin Du Cc: David S. Miller Cc: Davidlohr Bueso Cc: Eric Saint-Etienne Cc: Jin Yao Cc: Konstantin Khlebnikov Cc: Mathieu Poirier Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Rasmus Villemoes Cc: Song Liu Cc: Suzuki Poulouse Cc: Thomas Gleixner Cc: Thomas Richter Cc: linux-arm-kernel@lists.infradead.org Link: http://lkml.kernel.org/r/20190702103420.27540-5-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/annotate.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c index c8ce13419d9b..b8dfcfe08bb1 100644 --- a/tools/perf/util/annotate.c +++ b/tools/perf/util/annotate.c @@ -1113,16 +1113,14 @@ static int disasm_line__parse(char *line, const char **namep, char **rawp) *namep = strdup(name); if (*namep == NULL) - goto out_free_name; + goto out; (*rawp)[0] = tmp; *rawp = ltrim(*rawp); return 0; -out_free_name: - free((void *)namep); - *namep = NULL; +out: return -1; } -- 2.20.1