Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3142351ybi; Thu, 18 Jul 2019 21:51:40 -0700 (PDT) X-Google-Smtp-Source: APXvYqyYpqlTutkvNSPXB4zhHqovde3C2dlX633nFhgZ55RCFnoO/SXxCAsij4zkWEkAM9z5XiEu X-Received: by 2002:a17:90a:350c:: with SMTP id q12mr55550229pjb.46.1563511899938; Thu, 18 Jul 2019 21:51:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563511899; cv=none; d=google.com; s=arc-20160816; b=mefhGHmIEGnOGKBQGlEk4Jm4DOgyrcL49mXzU4DaSpiuiDCxqCaYO5ZH4aLSNA6rO8 8yvjWTN6AifotfVIGdw4Ktulvd6YchkrEo+rPhwSeE885EuL9YWP9llUezbN5y01k0kA Q9+ZKP6b0TvqNivDDEcjX7ElCnbaeIEvCELf9i/dYbEOh04doLB+0KZnfBvlL627LgNt EXk8u/UMEVGuzZKDYLuQcw5gPQJJpaQvwsVVqiqNzcD8f3yiZ4m7w8ovGNZ452VkWDGU n2qfS2e4FXGmfr/MCB/JDecIcDja5ERa5PZ/RV5QD6E8NR44FJarQVAfRwQPblYikLSs OjVg== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=i4/mXppflb3pJdt8SbVCuQwuGwhKHpTCPezEDPvOBM8=; b=v+FT/eHvAqV8qMAx0zUsQ0tbJ7WGi3jAESOsiQQbuipCl3JCHO0KkVAyPRKGqgTXrF fJgCrYcP70D3eWPKaUD5TUhA2gm738p+jW4IXzxAXgLrzcE31uTh5JuIT5JrgPp/MiCh 6Bvq6v9Z533FSnUg9miVsDIBVgkX6Ht1UhN3Bygvc6OhNLOj7NXX5vPx5gaZ77yjX95v b7b3RBf/Ne8w6g+CSOUWkZf5rHo9wyVTw74n51Ha2GbW9V5s2uDgW33o9gBsKypxp3rp 0WJhs0ZsLOfUNOYI0PX/GxssjKo2ojozR7EUWyR4Lq6TBHRbwjmFGzFcmvbb+MR8fIAO qOdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=gz6+rTug; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t9si348744pjw.22.2019.07.18.21.51.24; Thu, 18 Jul 2019 21:51:39 -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=gz6+rTug; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729354AbfGSEBM (ORCPT + 99 others); Fri, 19 Jul 2019 00:01:12 -0400 Received: from mail.kernel.org ([198.145.29.99]:32796 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729317AbfGSEBH (ORCPT ); Fri, 19 Jul 2019 00:01:07 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id AA3AB218BA; Fri, 19 Jul 2019 04:01:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508866; bh=DFafnbAHopfkugyR/u2EBGYtfhirSvbWGoa3dRt0uaQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gz6+rTugbr1rNkKcKRCTMm8M5FYcrGPBQY0x3zX7kHzpj796ssfuh96kvF4Z3lZsa L3j74SKhGFm49IS3l50DSjlEia7G3RaCi/iLmjP0PVlgxXpWgfA8kU/o+D3fhwZJfJ vjaxOf5Gjad/BdghK2z/6FRynE+dfwS5b6pRnvJ4= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: 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 AUTOSEL 5.2 128/171] perf map: Fix potential NULL pointer dereference found by smatch tool Date: Thu, 18 Jul 2019 23:55:59 -0400 Message-Id: <20190719035643.14300-128-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190719035643.14300-1-sashal@kernel.org> References: <20190719035643.14300-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Leo Yan [ Upstream commit 363bbaef63ffebcc745239fe80a953ebb5ac9ec9 ] Based on the following report from Smatch, fix the potential NULL pointer dereference check. tools/perf/util/map.c:479 map__fprintf_srccode() error: we previously assumed 'state' could be null (see line 466) tools/perf/util/map.c 465 /* Avoid redundant printing */ 466 if (state && 467 state->srcfile && 468 !strcmp(state->srcfile, srcfile) && 469 state->line == line) { 470 free(srcfile); 471 return 0; 472 } 473 474 srccode = find_sourceline(srcfile, line, &len); 475 if (!srccode) 476 goto out_free_line; 477 478 ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); 479 state->srcfile = srcfile; ^^^^^^^ 480 state->line = line; ^^^^^^^ This patch validates 'state' pointer before access its elements. 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 Fixes: dd2e18e9ac20 ("perf tools: Support 'srccode' output") Link: http://lkml.kernel.org/r/20190702103420.27540-8-leo.yan@linaro.org Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Sasha Levin --- tools/perf/util/map.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tools/perf/util/map.c b/tools/perf/util/map.c index ee71efb9db62..9c81ee092784 100644 --- a/tools/perf/util/map.c +++ b/tools/perf/util/map.c @@ -470,8 +470,11 @@ int map__fprintf_srccode(struct map *map, u64 addr, goto out_free_line; ret = fprintf(fp, "|%-8d %.*s", line, len, srccode); - state->srcfile = srcfile; - state->line = line; + + if (state) { + state->srcfile = srcfile; + state->line = line; + } return ret; out_free_line: -- 2.20.1