Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4423368ioa; Wed, 27 Apr 2022 03:38:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz237LGXnhfW5Ba2M7zRBA4s9z8WOSZ2m55dg+1PVtPxH+fGehYm0vAE1S2ETW7tLyNuue+ X-Received: by 2002:a63:2c8a:0:b0:3aa:86ea:f2c9 with SMTP id s132-20020a632c8a000000b003aa86eaf2c9mr23345786pgs.46.1651055934459; Wed, 27 Apr 2022 03:38:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651055934; cv=none; d=google.com; s=arc-20160816; b=TgxZBSV8obeTfUpWOsSLaSqGn15yROb+sB2RsZrwGgKMzE7GD82lBfX98WEMHxGd82 ot8fDNTf8nZ/erTpJLZHUN4qWuVTFaJgIVXETs6niYywVGzqm8XMweqo4MHDNpIQBD4N iIaxke8PElRAuZZ5Y0gV7lDzAiBYxq2HPqvNqAJPrZn2DOkynhP1mdHJyLnFjXID7B7/ /j24tfCSn53HJXlg+pB71UuQ6ouN0KCc40f0Wde7hy4rvg/s0Khwj6jyire+eponyYJ6 7Nz9Z0jNpPiCOBRHmOX9aiC6NkXNcsGYnyyrqoNmuwvTxXDvElxZJvwjrXHgHNuc3Vi8 G6ig== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=iLlpPbGYp/AvIZXpDzmbmLhdtnWIKRlPIOWRsOUTQ04=; b=0rq0O3Kva2vc7se+QIX9jtIIpe9V/4+qcpaaDl++DFrM2piW1VG1Ij7MlCBQ56NZhe VsUU2wPefEK2zdLkRm1iETtfQhNL0AO/2W/h1ryVSko9UCd1MArsLyflawY5jJ683hH5 O3kQg8eaan+65Z9YCrMBJAwdF0yIZ7xIeGNHtlhpjAnNcWP4WApeXYZSChtgKhE1mMe4 ggJw5WX8yPhpbUtVxqccDN5H32vmBd6iew6s7jze7K7RGADWh8IXsAWm1Z42ZuPAnjQ2 JC7/QpfiZxN+v8qtiU/UcmbKodNzJ3Nwi5BpHwch93Ai5O/NdoWu2lz3sSzml9QMw7gG CrJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1F1sFbSr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id o13-20020a656a4d000000b003a298545d5asi1429671pgu.323.2022.04.27.03.38.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 03:38:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1F1sFbSr; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 76E953D7473; Wed, 27 Apr 2022 02:49:58 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347003AbiDZJXJ (ORCPT + 99 others); Tue, 26 Apr 2022 05:23:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55810 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345662AbiDZI5p (ORCPT ); Tue, 26 Apr 2022 04:57:45 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72AF43889; Tue, 26 Apr 2022 01:42:41 -0700 (PDT) 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 ams.source.kernel.org (Postfix) with ESMTPS id 2568FB81CFA; Tue, 26 Apr 2022 08:42:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 648B9C385A0; Tue, 26 Apr 2022 08:42:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650962558; bh=v3d3wJ8d7he5GKQ/gI1Bji2FZeuuYgCu3YXM/O7M85o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1F1sFbSr3M4k/cKzIpGewqR/6N3lrMuYI7WWovAnfpSC7eRLJs3JsNxVCVjn18UeF oViDC96CZUz0fincMCVaM7OAAMuEE0P8pnUUy34KjnA9a03QMsqhyjgDFWMyZuOwC/ 2g8tfwmmkq+XG649Sr8sYaI6zByCIwCYZYCa1t74= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Adrian Hunter , Ian Rogers , Jiri Olsa , Arnaldo Carvalho de Melo Subject: [PATCH 5.17 002/146] perf tools: Fix segfault accessing sample_id xyarray Date: Tue, 26 Apr 2022 10:19:57 +0200 Message-Id: <20220426081750.123290229@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081750.051179617@linuxfoundation.org> References: <20220426081750.051179617@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Adrian Hunter commit a668cc07f990d2ed19424d5c1a529521a9d1cee1 upstream. perf_evsel::sample_id is an xyarray which can cause a segfault when accessed beyond its size. e.g. # perf record -e intel_pt// -C 1 sleep 1 Segmentation fault (core dumped) # That is happening because a dummy event is opened to capture text poke events accross all CPUs, however the mmap logic is allocating according to the number of user_requested_cpus. In general, perf sometimes uses the evsel cpus to open events, and sometimes the evlist user_requested_cpus. However, it is not necessary to determine which case is which because the opened event file descriptors are also in an xyarray, the size of whch can be used to correctly allocate the size of the sample_id xyarray, because there is one ID per file descriptor. Note, in the affected code path, perf_evsel fd array is subsequently used to get the file descriptor for the mmap, so it makes sense for the xyarrays to be the same size there. Fixes: d1a177595b3a824c ("libperf: Adopt perf_evlist__mmap()/munmap() from tools/perf") Fixes: 246eba8e9041c477 ("perf tools: Add support for PERF_RECORD_TEXT_POKE") Signed-off-by: Adrian Hunter Acked-by: Ian Rogers Cc: Adrian Hunter Cc: Jiri Olsa Cc: stable@vger.kernel.org # 5.5+ Link: https://lore.kernel.org/r/20220413114232.26914-1-adrian.hunter@intel.com Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Greg Kroah-Hartman --- tools/lib/perf/evlist.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -577,7 +577,6 @@ int perf_evlist__mmap_ops(struct perf_ev { struct perf_evsel *evsel; const struct perf_cpu_map *cpus = evlist->cpus; - const struct perf_thread_map *threads = evlist->threads; if (!ops || !ops->get || !ops->mmap) return -EINVAL; @@ -589,7 +588,7 @@ int perf_evlist__mmap_ops(struct perf_ev perf_evlist__for_each_entry(evlist, evsel) { if ((evsel->attr.read_format & PERF_FORMAT_ID) && evsel->sample_id == NULL && - perf_evsel__alloc_id(evsel, perf_cpu_map__nr(cpus), threads->nr) < 0) + perf_evsel__alloc_id(evsel, evsel->fd->max_x, evsel->fd->max_y) < 0) return -ENOMEM; }