Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4361185ioa; Wed, 27 Apr 2022 01:57:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyY7ETqfqas5MbXx+uqrFLEdWeVn1aUSlaJwS8W+yXw4MpXl76HlMvfws1/SjN2jN9wIe8+ X-Received: by 2002:a17:902:e80e:b0:15d:4397:2874 with SMTP id u14-20020a170902e80e00b0015d43972874mr5429279plg.54.1651049833162; Wed, 27 Apr 2022 01:57:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651049833; cv=none; d=google.com; s=arc-20160816; b=BwSSvxZHASiz0xcbXc9F+ya/+LDAosmGwaaoR2rh/NmJX8cpC6XFTVKpoG7VVp67zB dPazDDV1kuHqHZ66zWufAHK4nnJ6u7Zh9oMmD3/0+ATEiGM7sLuP45UsEMmtM+hrh3aq KhZdDERH1W31Li7DlBb2gFT+uWpJW902xIXYS/YE67IqldvVh+CbE7rAGdcMLRblFalZ 9IFpTaGBjltahN2hhko9G/pJecs5zVoXJu7FswM/wjz3ARRmx7pthg+KqzCI6Dbqf9IE F/82mj883vfyj1UePVH8bE2Xek/DeuS4xYhGY1zFBeipx5gHvmIJrGPPBfO2V9onxo7F LPPg== 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=EWjXxXQq4V31nS01jNfbAMN2m5qiKQ4rHOPtwhmE3V4=; b=XZpHMhWhJofb36bfrW/nxJn1lub7JjIOK8URUPPpagR8VrpENomT7wTD7i6Uj9Am4N yviyEo7liS7Pw/HlMYOfhl6OYKDJK8AbfFfakEq2WOc42jIYDCVhfyjG8U7JnOE7utQt JIHntJ07y4taw1i8rwu6WBelJsTT5sUGjkgW7hQl7wD2qnSciSDE6Xp+iIRRLGbXRgJD I2v9zDaY8Vr6mmrzC8tS7Z7gupKCw/OKigutZofiM6IGolf1xhSouvHBmaT7sXbz1IIv DgZ7DGIFe5Uvi11Au9xdJHB+55mswBONkdpaV6i2asEUZ4N1fvicf7i3PQAjB6bZxHOQ 43YA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=brjzEDde; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id bj10-20020a170902850a00b00158b60545dcsi1011943plb.207.2022.04.27.01.57.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 01:57:13 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=brjzEDde; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 C1D831CDECE; Wed, 27 Apr 2022 01:54:40 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S243625AbiDZIzp (ORCPT + 99 others); Tue, 26 Apr 2022 04:55:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58756 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345583AbiDZIl7 (ORCPT ); Tue, 26 Apr 2022 04:41:59 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 28E01159787; Tue, 26 Apr 2022 01:33:17 -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 67D6B6185D; Tue, 26 Apr 2022 08:33:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5FA2BC385A0; Tue, 26 Apr 2022 08:33:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650961995; bh=/TMUWetx3+P/4PGL0bH92LjO0vr668Ype3Zm2jhJ/4g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=brjzEDdeaySDRgMOpVrKt8uamF+zIv0w8PvypiuCVJ4vmRwo/f+oP5qwxMgHfnkaO 9Gcy39CjgpJARE5kYQ2pehOUsbfzr1gzjPJSkZLR3ScPfhIx9mnPeLeoSNyM1REiNG LkuRlI8dvkfqaWAamOxsPg3L2GzYI7nKehVT+RbU= 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.10 04/86] perf tools: Fix segfault accessing sample_id xyarray Date: Tue, 26 Apr 2022 10:20:32 +0200 Message-Id: <20220426081741.332847179@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081741.202366502@linuxfoundation.org> References: <20220426081741.202366502@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 @@ -571,7 +571,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; @@ -583,7 +582,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; }