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 X-Spam-Level: X-Spam-Status: No, score=-16.0 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E561FC433DB for ; Tue, 16 Mar 2021 13:01:51 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id B4FD06505E for ; Tue, 16 Mar 2021 13:01:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234463AbhCPNBY (ORCPT ); Tue, 16 Mar 2021 09:01:24 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:55755 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234451AbhCPNAx (ORCPT ); Tue, 16 Mar 2021 09:00:53 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1615899652; 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=eJuyiijJa+f0O+UIDwcydLxD2CGkRu/MADpV+IhycDo=; b=A8oP+Cw+P0a5//LcclLqtpLWtRwuAX6iw5+g48TPccxGmHTsD6FqZNuEQ7zsmPql6hjRjI gKBOv+slx4pkBjeW6KXzwqlXNv+xR24CeA7qcm+LbR5SSAlhr0VvyiLsW4PwIAbyH+dtnX v7rNIZwMbvTrfW5kUzq5PJXhvzBDGBY= 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-23-CzINVc3aO42P6NEXexJnHA-1; Tue, 16 Mar 2021 09:00:50 -0400 X-MC-Unique: CzINVc3aO42P6NEXexJnHA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 81032100C676; Tue, 16 Mar 2021 13:00:48 +0000 (UTC) Received: from krava (unknown [10.40.193.217]) by smtp.corp.redhat.com (Postfix) with SMTP id 3F31210023B5; Tue, 16 Mar 2021 13:00:46 +0000 (UTC) Date: Tue, 16 Mar 2021 14:00:45 +0100 From: Jiri Olsa To: Jackie Liu Cc: linux-kernel@vger.kernel.org, Jackie Liu , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Namhyung Kim Subject: Re: [PATCH v4] perf tools: perf_event_paranoid and kptr_restrict may crash on 'perf top' Message-ID: References: <20210316012453.1156-1-liuyun01@kylinos.cn> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210316012453.1156-1-liuyun01@kylinos.cn> X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 16, 2021 at 09:24:53AM +0800, Jackie Liu wrote: > After install the libelf-dev package and compiling perf, kptr_restrict=2 > and perf_event_paranoid=3 will cause perf top to crash, because the > value of /proc/kallsyms cannot be obtained, which leads to > info->jited_ksyms == NULL. In order to solve this problem, Add a > judgment before use. > > v3->v4: > Fix memory leaks in more places Acked-by: Jiri Olsa thanks, jirka > > v2->v3: > free info_linear memory and move code above, don't do those extra btf > checks. > > Suggested-by: Jiri Olsa > Cc: Peter Zijlstra > Cc: Ingo Molnar > Cc: Arnaldo Carvalho de Melo > Cc: Mark Rutland > Cc: Alexander Shishkin > Cc: Namhyung Kim > Signed-off-by: Jackie Liu > --- > tools/perf/util/bpf-event.c | 13 ++++++++++--- > 1 file changed, 10 insertions(+), 3 deletions(-) > > diff --git a/tools/perf/util/bpf-event.c b/tools/perf/util/bpf-event.c > index 57d58c81a5f8..cdecda1ddd36 100644 > --- a/tools/perf/util/bpf-event.c > +++ b/tools/perf/util/bpf-event.c > @@ -196,25 +196,32 @@ static int perf_event__synthesize_one_bpf_prog(struct perf_session *session, > } > > if (info_linear->info_len < offsetof(struct bpf_prog_info, prog_tags)) { > + free(info_linear); > pr_debug("%s: the kernel is too old, aborting\n", __func__); > return -2; > } > > info = &info_linear->info; > + if (!info->jited_ksyms) { > + free(info_linear); > + return -1; > + } > > /* number of ksyms, func_lengths, and tags should match */ > sub_prog_cnt = info->nr_jited_ksyms; > if (sub_prog_cnt != info->nr_prog_tags || > - sub_prog_cnt != info->nr_jited_func_lens) > + sub_prog_cnt != info->nr_jited_func_lens) { > + free(info_linear); > return -1; > + } > > /* check BTF func info support */ > if (info->btf_id && info->nr_func_info && info->func_info_rec_size) { > /* btf func info number should be same as sub_prog_cnt */ > if (sub_prog_cnt != info->nr_func_info) { > pr_debug("%s: mismatch in BPF sub program count and BTF function info count, aborting\n", __func__); > - err = -1; > - goto out; > + free(info_linear); > + return -1; > } > if (btf__get_from_id(info->btf_id, &btf)) { > pr_debug("%s: failed to get BTF of id %u, aborting\n", __func__, info->btf_id); > -- > 2.25.1 >