Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4442412ioa; Wed, 27 Apr 2022 04:05:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwftRH8vvTcmhm4E6Q/msyop+fVH1K1ZBKsHuREIywQUnwOtgTcqYoug0pujEdg/pcv/+Gn X-Received: by 2002:a17:902:e748:b0:15c:e3b9:bba3 with SMTP id p8-20020a170902e74800b0015ce3b9bba3mr22411680plf.139.1651057559234; Wed, 27 Apr 2022 04:05:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651057559; cv=none; d=google.com; s=arc-20160816; b=ilB77pdPwq5xSYAuHuRtzAkzikl9gjq92LTAs/Rcz34Mg/+r1dC461TYnIOHsDSpPS DZhpENyZ4nSFtXf53WS7aO038JjgDTYuxUuvJRg7BeosOE+7EAz79O1AtiDnpvF2WfJ2 Yj9m4PzDr8niXha7DM601ADt8tU78HACEAdsFWStWHk30oDIGSCWXckrXVUkk4SJwTjy KOwZ67U6Wsfae9q+JixB7TN1MoOZeItlep/2jema/u1kFcCPadkgJ6KqsDXg9MuATlnD wGdKv1TXkZ8zhrL+Qn4wim3wiFsqNRFmQXKIWz3VaktKMYa2Hs+HRyGQy82FYV4Axkuw MyOg== 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=iOWBV2xMgmATE6kNknHNWqy7Tyej3glDz7ExO769bvZCCOf5VvdxxLT9SBviDU9UEx OVWpLeGQChfVp42lXlBJFtJD+AWgvLRT5yL6DvdBhgvkWwZQC+6er2XT4gTnh2Jhwn2A Pgtvi//mAC3wQY4LdgodB0aApl0it/rWh8ZmfM/D/3aLGwS3uvBpKRa9IiOUnXsX/7hp 9gdBz/K35um3oqkq7Ie6OIRd269m/TrlSj8Ddf2q+ownLQtNvaWXC6KLa8RQlIkPrFRZ Hxwwr4z5iwUy2X+YDSrhoUKWmjZMrSQPc5KLLs/f9RANgV197x9xTHMRoo3gngySOF3c RdJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=iZpMRm+X; 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 w191-20020a6382c8000000b003abaac3b51esi1022864pgd.700.2022.04.27.04.05.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:05:59 -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=iZpMRm+X; 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 153B433A797; Wed, 27 Apr 2022 03:16:28 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346825AbiDZJBR (ORCPT + 99 others); Tue, 26 Apr 2022 05:01:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346885AbiDZIp2 (ORCPT ); Tue, 26 Apr 2022 04:45:28 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89FE864C4; Tue, 26 Apr 2022 01:36:12 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 268D36185C; Tue, 26 Apr 2022 08:36:12 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31A29C385AC; Tue, 26 Apr 2022 08:36:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650962171; bh=v3d3wJ8d7he5GKQ/gI1Bji2FZeuuYgCu3YXM/O7M85o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iZpMRm+XASXQLK8kknecqQh5UBTZv9hmoacSUtjMoDJ5Uy7vuCY6c29Lflsq+Uq5+ ORO/XkoHGXODm93GQLtcuS7hIXUSFItd59X4yIOm2Ae2Us1ySIxNFkNJrON+OTXMhY L6J3w/RixkjbOwl2pp4g5dqKw0//668cdF8S8Pgk= 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.15 010/124] perf tools: Fix segfault accessing sample_id xyarray Date: Tue, 26 Apr 2022 10:20:11 +0200 Message-Id: <20220426081747.590453260@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081747.286685339@linuxfoundation.org> References: <20220426081747.286685339@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; }