Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp22244lqz; Fri, 29 Mar 2024 07:25:41 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUAMXt7FzVG1H3dFO1t0SoORGidDa4QSwoAKXeb0KAYVDSumPsiX/v6G7rblQZbxNoMzG2c0McAp1+zY1XJG7RCDfHVhcT0fsxSLY5pPQ== X-Google-Smtp-Source: AGHT+IH/jtLwUoOKuTTXfhioAolEmtbSKPCNLmp8G1kpth9dNOI8XMPPhBCaZT5TlzKxXMtqNCT7 X-Received: by 2002:a17:90a:ce03:b0:29a:9fad:4ad9 with SMTP id f3-20020a17090ace0300b0029a9fad4ad9mr2383582pju.3.1711722340963; Fri, 29 Mar 2024 07:25:40 -0700 (PDT) Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [2604:1380:45e3:2400::1]) by mx.google.com with ESMTPS id ob15-20020a17090b390f00b002a2091cac86si3467917pjb.51.2024.03.29.07.25.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Mar 2024 07:25:40 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-124916-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) client-ip=2604:1380:45e3:2400::1; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@kernel.org header.s=k20201202 header.b=cI3QSzx1; arc=fail (body hash mismatch); spf=pass (google.com: domain of linux-kernel+bounces-124916-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45e3:2400::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-124916-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id A489928614D for ; Fri, 29 Mar 2024 14:25:40 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8AACA2D376F; Fri, 29 Mar 2024 12:47:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="cI3QSzx1" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8450A2D378B; Fri, 29 Mar 2024 12:47:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716426; cv=none; b=GvS3y7T4V6gC19yLxeZQvxvjGZsynabqnlAtt1lGlOuZRG4gMHbnnI/11fHwEQ1dxJGxFCswQ6MRjUp7T1fUinMbhguq4NjYWYg38mvZOgJOytxYlEaFuEGjUdheZvwIv6Wg9IaQ7VmMJQ3tl+DEZIKXUhvnpqs/FCczt2WiKrg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711716426; c=relaxed/simple; bh=zoBsz7Xwd6AJJkhxQy7Eg/UipSbWzdwmNWsxw16djjM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=fBEpQTiEdwDaI8PsDOJjOA1d942aAhNqpzJEk8Q8YkZY7JWFWabxVvdVvqtGzLg3IV1LLFOdIxqfbb74oo4lqqjoIS82EBY3+QD44pQDU4MxDh+YIhk/QJwx+yiQW+J6QMk1MNgwXJeIek/A5H530WK4JFPXpWqIVhCeRn9pVg4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=cI3QSzx1; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id F370FC43394; Fri, 29 Mar 2024 12:47:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711716426; bh=zoBsz7Xwd6AJJkhxQy7Eg/UipSbWzdwmNWsxw16djjM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cI3QSzx1aPCmO4b5/NduEAOS9LsA5FpbMD94AlVDV9Jtsa4XfXsttJ5/GH0Wyf60F tKil/U6OwBle6mrDH4nik7SNORLdc1bVSyHzWBFyTWR69aBf7PWi+bi1V2y/gB4f+X QOdYQI1RFahA763UdPvrivI4DTvLGZEY7gFMxMD/p6Rz5PgDmQagOD3IFW65NXJE4F SZPLe2pITONC7kkm8eygzBqScFFGjtSlqG1PTy0TBdu3Mihc1ekTrUg2vTKst1fLU8 xL81tePYPG4M1YVfQ21nhfnj0DTWq3X+IU4NgBk6VT+9Dqjp8soUYhyAiUk6Cr4Ekz Y7ZRZz8k/7LcA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Ian Rogers , Yang Jihong , Namhyung Kim , Sasha Levin , peterz@infradead.org, mingo@redhat.com, acme@kernel.org, adrian.hunter@intel.com, linux-perf-users@vger.kernel.org Subject: [PATCH AUTOSEL 6.1 32/52] libperf evlist: Avoid out-of-bounds access Date: Fri, 29 Mar 2024 08:45:26 -0400 Message-ID: <20240329124605.3091273-32-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329124605.3091273-1-sashal@kernel.org> References: <20240329124605.3091273-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.1.83 Content-Transfer-Encoding: 8bit From: Ian Rogers [ Upstream commit 1947b92464c3268381604bbe2ac977a3fd78192f ] Parallel testing appears to show a race between allocating and setting evsel ids. As there is a bounds check on the xyarray it yields a segv like: ``` AddressSanitizer:DEADLYSIGNAL ================================================================= ==484408==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000010 ==484408==The signal is caused by a WRITE memory access. ==484408==Hint: address points to the zero page. #0 0x55cef5d4eff4 in perf_evlist__id_hash tools/lib/perf/evlist.c:256 #1 0x55cef5d4f132 in perf_evlist__id_add tools/lib/perf/evlist.c:274 #2 0x55cef5d4f545 in perf_evlist__id_add_fd tools/lib/perf/evlist.c:315 #3 0x55cef5a1923f in store_evsel_ids util/evsel.c:3130 #4 0x55cef5a19400 in evsel__store_ids util/evsel.c:3147 #5 0x55cef5888204 in __run_perf_stat tools/perf/builtin-stat.c:832 #6 0x55cef5888c06 in run_perf_stat tools/perf/builtin-stat.c:960 #7 0x55cef58932db in cmd_stat tools/perf/builtin-stat.c:2878 .. ``` Avoid this crash by early exiting the perf_evlist__id_add_fd and perf_evlist__id_add is the access is out-of-bounds. Signed-off-by: Ian Rogers Cc: Yang Jihong Signed-off-by: Namhyung Kim Link: https://lore.kernel.org/r/20240229070757.796244-1-irogers@google.com Signed-off-by: Sasha Levin --- tools/lib/perf/evlist.c | 18 ++++++++++++------ tools/lib/perf/include/internal/evlist.h | 4 ++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/tools/lib/perf/evlist.c b/tools/lib/perf/evlist.c index 61b637f29b827..b871923c7e5cd 100644 --- a/tools/lib/perf/evlist.c +++ b/tools/lib/perf/evlist.c @@ -233,10 +233,10 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist) static void perf_evlist__id_hash(struct perf_evlist *evlist, struct perf_evsel *evsel, - int cpu, int thread, u64 id) + int cpu_map_idx, int thread, u64 id) { int hash; - struct perf_sample_id *sid = SID(evsel, cpu, thread); + struct perf_sample_id *sid = SID(evsel, cpu_map_idx, thread); sid->id = id; sid->evsel = evsel; @@ -254,21 +254,27 @@ void perf_evlist__reset_id_hash(struct perf_evlist *evlist) void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, - int cpu, int thread, u64 id) + int cpu_map_idx, int thread, u64 id) { - perf_evlist__id_hash(evlist, evsel, cpu, thread, id); + if (!SID(evsel, cpu_map_idx, thread)) + return; + + perf_evlist__id_hash(evlist, evsel, cpu_map_idx, thread, id); evsel->id[evsel->ids++] = id; } int perf_evlist__id_add_fd(struct perf_evlist *evlist, struct perf_evsel *evsel, - int cpu, int thread, int fd) + int cpu_map_idx, int thread, int fd) { u64 read_data[4] = { 0, }; int id_idx = 1; /* The first entry is the counter value */ u64 id; int ret; + if (!SID(evsel, cpu_map_idx, thread)) + return -1; + ret = ioctl(fd, PERF_EVENT_IOC_ID, &id); if (!ret) goto add; @@ -297,7 +303,7 @@ int perf_evlist__id_add_fd(struct perf_evlist *evlist, id = read_data[id_idx]; add: - perf_evlist__id_add(evlist, evsel, cpu, thread, id); + perf_evlist__id_add(evlist, evsel, cpu_map_idx, thread, id); return 0; } diff --git a/tools/lib/perf/include/internal/evlist.h b/tools/lib/perf/include/internal/evlist.h index 850f07070036c..cf77db75291b9 100644 --- a/tools/lib/perf/include/internal/evlist.h +++ b/tools/lib/perf/include/internal/evlist.h @@ -127,11 +127,11 @@ u64 perf_evlist__read_format(struct perf_evlist *evlist); void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel, - int cpu, int thread, u64 id); + int cpu_map_idx, int thread, u64 id); int perf_evlist__id_add_fd(struct perf_evlist *evlist, struct perf_evsel *evsel, - int cpu, int thread, int fd); + int cpu_map_idx, int thread, int fd); void perf_evlist__reset_id_hash(struct perf_evlist *evlist); -- 2.43.0