Received: by 2002:a25:c205:0:0:0:0:0 with SMTP id s5csp5812230ybf; Thu, 5 Mar 2020 07:28:35 -0800 (PST) X-Google-Smtp-Source: ADFU+vuwOAxDdT4flgZsN+vmkJfxAizDdkTF4Ih8v4XoCeOWn9zNNpmK7VLCYmxn5uKnfoi1g/CU X-Received: by 2002:a05:6830:23b9:: with SMTP id m25mr3067143ots.354.1583422115492; Thu, 05 Mar 2020 07:28:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1583422115; cv=none; d=google.com; s=arc-20160816; b=rn3WBqycNSIQjfoo83NkBnuNoA0D5xDzeBAiuaEO+/pLpwaQehgbwceg7xd4oOzrOY ysQW+TW09GNGv5i4m2mKcrQa7dlFECq6RX8xYYfK8gvLStBEjqHx3BopR0mJdvLegkDa 19cQaouAaI9JzR7s7AIH/DMvCc/90eyJ9VlM+XtVrT6HaGMKtpL1gwOigv/cyDWpkleM j1/uk+SUP3T6cf7u92yD1H7e5NIvMzUvXTqkgm4zZ4sAse+l623J5o9g8S9dmMaqahuH OAz8lKwb3yiwy2jrtS4+d/ToKnWmYtzP1ryxaiw+X9SG0iDkPKqdeuNmXIKgE1dN5dvx 2fcw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=szMYTUanak9bpgOmdh9Y4uhe2prEGOy08Ect7MjVS/Y=; b=vFYGv2WPieIyO1v/xmPFp5F9HJq+EkdCcIrQozAj8ufsN3FcaCKd4zBDXKLVsn8u5L 58mzAJTkzhqfgmGJiNJxDeDOkw4TDDsRvhPUNA8AjMcp2Rc7xgsmhYHdHckU7cJ10n4C sqHxVN4QmtJU/eRR7QmjEHeDhfum1SbTHZ142agnNzu8POH8OwQ5DGelyQUx7WmleaZI 9I0dJzN2mOyJrVo/KgB+ZeBnfAhMWCPFm4YF5luGSFHwcUmFqIeLqgPI2KgIKc5B5r7X Xl6cI4KpxqMW90FcGnC1JtYNTxrYUq9IwCC8dAX4kPqZAclyZNaTH2GthvFcK+kK9LQX zj8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=E6EeGLjf; 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=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id c6si733709otk.233.2020.03.05.07.28.23; Thu, 05 Mar 2020 07:28:35 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=E6EeGLjf; 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=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726498AbgCEP2E (ORCPT + 99 others); Thu, 5 Mar 2020 10:28:04 -0500 Received: from us-smtp-delivery-1.mimecast.com ([205.139.110.120]:59459 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1725990AbgCEP2D (ORCPT ); Thu, 5 Mar 2020 10:28:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1583422082; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=szMYTUanak9bpgOmdh9Y4uhe2prEGOy08Ect7MjVS/Y=; b=E6EeGLjfrYqYmuK/TmfL0qr6d1w2G4u2q5yfGKx6sp6kNa1swsOqD/UL3Uk9tAJmBzqyK2 hVOF56zspTyBBmcGtlGVi69IM1a+6Rn765DSla1AZwJ8mTk+y0TgDNXNPrVlPCtXVyrh7H GFG8lvLz5lXCfX5/yUa/nuyr5uKUcPA= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-142-7ft2CNpQM7K72aqx1wN1qA-1; Thu, 05 Mar 2020 10:28:00 -0500 X-MC-Unique: 7ft2CNpQM7K72aqx1wN1qA-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 819FA805745; Thu, 5 Mar 2020 15:27:59 +0000 (UTC) Received: from sandy.ghostprotocols.net (ovpn-112-13.phx2.redhat.com [10.3.112.13]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E7FEF60BE0; Thu, 5 Mar 2020 15:27:58 +0000 (UTC) Received: by sandy.ghostprotocols.net (Postfix, from userid 1000) id C9D1011A; Thu, 5 Mar 2020 12:27:55 -0300 (BRT) Date: Thu, 5 Mar 2020 12:27:55 -0300 From: Arnaldo Carvalho de Melo To: zhe.he@windriver.com Cc: jolsa@kernel.org, ak@linux.intel.com, meyerk@hpe.com, linux-kernel@vger.kernel.org, acme@kernel.org Subject: Re: [PATCH] perf: Fix crash due to null pointer dereference when iterating cpu map Message-ID: <20200305152755.GA6958@redhat.com> References: <1583405239-352868-1-git-send-email-zhe.he@windriver.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1583405239-352868-1-git-send-email-zhe.he@windriver.com> X-Url: http://acmel.wordpress.com User-Agent: Mutt/1.5.20 (2009-12-10) X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Em Thu, Mar 05, 2020 at 06:47:19PM +0800, zhe.he@windriver.com escreveu: > From: He Zhe > > NULL pointer may be passed to perf_cpu_map__cpu and then cause the > following crash. > > perf ftrace -G start_kernel ls > failed to set tracing filters > [ 208.710716] perf[341]: segfault at 4 ip 00000000567c7c98 > sp 00000000ff937ae0 error 4 in perf[56630000+1b2000] > [ 208.724778] Code: fc ff ff e8 aa 9b 01 00 8d b4 26 00 00 00 00 8d > 76 00 55 89 e5 83 ec 18 65 8b 0d 14 00 00 00 89 > 4d f4 31 c9 8b 45 08 8b9 > Segmentation fault I'm not being able to repro this here, what is the tree you are using? - Arnaldo > Program received signal SIGSEGV, Segmentation fault. > 0x5677dc98 in perf_cpu_map__cpu (cpus=0x0, idx=0) at cpumap.c:250 > 250 cpumap.c: No such file or directory. > (gdb) bt > 0 0x5677dc98 in perf_cpu_map__cpu (cpus=0x0, idx=0) at cpumap.c:250 > 1 0x566790bd in evlist__close (evlist=0x56a6f470) at util/evlist.c:1222 > 2 0x566792aa in evlist__delete (evlist=evlist@entry=0x56a6f470) > at util/evlist.c:152 > 3 0x5667936b in evlist__delete (evlist=0x56a6f470) at util/evlist.c:148 > 4 0x565efd39 in cmd_ftrace (argc=1, argv=0xffffdd18) at builtin-ftrace.c:520 > 5 0x56660ee7 in run_builtin (p=0x56993004 , argc=4, > argv=0xffffdd18) at perf.c:312 > 6 0x565e7fae in handle_internal_command (argv=, > argc=) at perf.c:364 > 7 run_argv (argcp=, argv=) at perf.c:408 > 8 main (argc=, argv=) at perf.c:538 > > Add null pointer check for iteration and NULL assignment for all_cpus. > And there is no need to iterate if there is no cpus. > > Signed-off-by: He Zhe > --- > tools/lib/perf/cpumap.c | 4 ++-- > tools/lib/perf/evlist.c | 1 + > 2 files changed, 3 insertions(+), 2 deletions(-) > > diff --git a/tools/lib/perf/cpumap.c b/tools/lib/perf/cpumap.c > index f93f4e703e4c..128386647ac0 100644 > --- a/tools/lib/perf/cpumap.c > +++ b/tools/lib/perf/cpumap.c > @@ -247,7 +247,7 @@ struct perf_cpu_map *perf_cpu_map__new(const char *cpu_list) > > int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx) > { > - if (idx < cpus->nr) > + if (cpus && idx < cpus->nr) > return cpus->map[idx]; > > return -1; > @@ -255,7 +255,7 @@ int perf_cpu_map__cpu(const struct perf_cpu_map *cpus, int idx) > > int perf_cpu_map__nr(const struct perf_cpu_map *cpus) > { > - return cpus ? cpus->nr : 1; > + return cpus ? cpus->nr : 0; > } > > bool perf_cpu_map__empty(const struct perf_cpu_map *map) > diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c > index ae9e65aa2491..d57adf3020fe 100644 > --- a/tools/lib/perf/evlist.c > +++ b/tools/lib/perf/evlist.c > @@ -127,6 +127,7 @@ void perf_evlist__exit(struct perf_evlist *evlist) > perf_cpu_map__put(evlist->cpus); > perf_thread_map__put(evlist->threads); > evlist->cpus = NULL; > + evlist->all_cpus = NULL; > evlist->threads = NULL; > fdarray__exit(&evlist->pollfd); > } > -- > 2.24.1