Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 5280641F344; Sat, 12 Sep 2026 08:47:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789202847; cv=none; b=UAxKqNRP8OjeKo/b4rvNGv6JGkK1TOu9RSk8NwhBDRjYOKYSS4X4YpQG0qmKbVnkK643YrDXUFss6N7ODjvS6MH9dZq02rvTki2pM434t9uVCdb35SjfgBp25AdFoJskdL8vvmk1rFXTL1nGNj654u3EiRa0RR48XJg3TnnfPKA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1789202847; c=relaxed/simple; bh=ss2BDpSa9lxCJx9LaOeZwMY75YxFuXuUvvr+6sztey4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mYhD7EXw7cn6nzDaURGwIBAFZdIun+DVwcL2HSNrc5/kT32m9gGZl4LMQmcilX3iGfJi/bvAg3GZKqBhLxSOM7j5LsK+qF3gHHfGN/drr6XImercEg9oF58uTaNaoSktsqwbt2cP65TZ5SpmnvZmg7fYZLmbJOaqXcl8Fnr98qo= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=P7xMbCur; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="P7xMbCur" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0A6711F000FF; Sat, 12 Sep 2026 08:47:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1789202839; bh=VMN5FUYZ1YuF5VP3cAKM/AeDmRavNBOIFzN5v6sJ7ww=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=P7xMbCur+d8WUqazL74AEDdv5lNrR0vkpRUkuYYD4xorwDx98VIEU8PkNR0u18V7U uKOCgBIXTfAg4npwPOVF/gWG1ymBAQ1LOISGOV0DYzXb5+18X/gAY565o7icqtK23k IlwHBxr43HcDK8wnA0Dl+s9x1QctF1NuPZtp9LlE= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ian Rogers , Adrian Hunter , Alice Rogers , Dapeng Mi , Ingo Molnar , James Clark , Leo Yan , Namhyung Kim , Peter Zijlstra , Thomas Richter , Arnaldo Carvalho de Melo , Sasha Levin Subject: [PATCH 7.2 1279/1815] perf python: Handle Py_None for thread and cpu maps Date: Sat, 12 Sep 2026 08:50:29 +0200 Message-ID: <20260912065719.041766383@linuxfoundation.org> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20260912065648.999753832@linuxfoundation.org> References: <20260912065648.999753832@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.2-stable review patch. If anyone has any objections, please let me know. ------------------ From: Ian Rogers [ Upstream commit 054d1c7717b95d9089cd3b80e9e85fe1f2ea0471 ] The python stubs allow passing None for threads and cpus to the perf.parse_events() and perf.parse_metrics() bindings. However, PyArg_ParseTuple parses None into a Py_None object, which is not a NULL pointer. Because the C code lacked an explicit check for Py_None, it would cast Py_None to a pyrf_thread_map/pyrf_cpu_map struct pointer and dereference it, causing a memory corruption crash. Fix this pre-existing issue by explicitly checking for Py_None alongside NULL in pyrf__parse_events, pyrf__parse_metrics, and pyrf_evsel__open. Signed-off-by: Ian Rogers Cc: Adrian Hunter Cc: Alice Rogers Cc: Dapeng Mi Cc: Ingo Molnar Cc: James Clark Cc: Leo Yan Cc: Namhyung Kim Cc: Peter Zijlstra Cc: Thomas Richter Signed-off-by: Arnaldo Carvalho de Melo Signed-off-by: Namhyung Kim Stable-dep-of: 9a142beb1eba ("perf python: Validate CPU and thread maps in pyrf_evsel__open") Signed-off-by: Sasha Levin --- tools/perf/util/python.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 5aec515bd70b3..a04e3f9ddc475 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -1107,10 +1107,10 @@ static PyObject *pyrf_evsel__open(struct pyrf_evsel *pevsel, &pcpus, &pthreads, &group, &inherit)) return NULL; - if (pthreads != NULL) + if (pthreads != NULL && pthreads != Py_None) threads = ((struct pyrf_thread_map *)pthreads)->threads; - if (pcpus != NULL) + if (pcpus != NULL && pcpus != Py_None) cpus = ((struct pyrf_cpu_map *)pcpus)->cpus; evsel->core.attr.inherit = inherit; @@ -2333,8 +2333,10 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args) return NULL; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads : NULL; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; parse_events_error__init(&err); perf_evlist__set_maps(evlist__core(evlist), cpus, threads); @@ -2367,8 +2369,10 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args) return NULL; } - threads = pthreads ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; - cpus = pcpus ? ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; + threads = (pthreads && pthreads != Py_None) ? + ((struct pyrf_thread_map *)pthreads)->threads : NULL; + cpus = (pcpus && pcpus != Py_None) ? + ((struct pyrf_cpu_map *)pcpus)->cpus : NULL; perf_evlist__set_maps(evlist__core(evlist), cpus, threads); ret = metricgroup__parse_groups(evlist, pmu ?: "all", -- 2.53.0