From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-dy1-f202.google.com (mail-dy1-f202.google.com [74.125.82.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1850938837E for ; Tue, 16 Jun 2026 01:16:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781572592; cv=none; b=Tv1gVOuQ0hGi+EozatKM87BYNwCF0mdgIWl2wNH6lgDjP6ysca6f2kzA7Sv3GUW6QB8u0AyLB7JdAE5mUSFWhnLQwgOPcjg+5/M6ONsubPqV3oF8M4c/WdTwPVsI1UQq+gRKQOYTxf5A1/T0vH6hmjF9jeIFhfC5ynLygOmezRk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781572592; c=relaxed/simple; bh=zlKNDRleksEo18Bo4F5pa1gJEu8ZpW4kHFgmWSCc7ho=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=eWt42N5KsBGPMNiCm829mnLJ2d9rDinFVK5hN8P6YGc7C7aIqB+ciBevogznAn9mpRnGK3Jq8/M+7SR1XqqbYnm43BnIxZjadYpaSUyApyVtQS51haGZVtnX1TKWuhqpK0FuAH+lQbSUR5Mlp2ZjKq719GNfqOQg5OKB2Tyt8bY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=F6X91Oxj; arc=none smtp.client-ip=74.125.82.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="F6X91Oxj" Received: by mail-dy1-f202.google.com with SMTP id 5a478bee46e88-304ed777a96so2626944eec.1 for ; Mon, 15 Jun 2026 18:16:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781572590; x=1782177390; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=yCuiJ7cpJrvvup1SmgZEXA/C64pNGqrjRmbjWW0EDWo=; b=F6X91OxjsYCogNbfYbAlr5DM9FlNz+DPqEyYsN/A769lIcNHUd2bNUtRQ+Oqo8OJmo 86Yo+i24/iyjc7RweC0s3A/BqAXQFkWDmsueJu6cNcEWyqJGeRbrZC6lZYciBzu7CEf1 z8fmKObPMwW0STsapPt+HmhZiBiWpwazXtGeW2aaAFIowd6hZe3uEatNUieX9QqJyPie 95fSFoHML0cDDdz8Z4zchZ0rKu4uA+wQe4QjNblFMmdjLdM8gymbw1u1NwbOHXTmHVrX 7a6H/naNGzqvbcJD+cXbT6LBsIO8RAuRezklBmq44dRf4Kac8Exjj0D41rYNJwZRKzwo xLLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781572590; x=1782177390; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=yCuiJ7cpJrvvup1SmgZEXA/C64pNGqrjRmbjWW0EDWo=; b=cEDDbal9dMm774xADjJfcwK2hbNc2XZcpAD/d+kopQkjUBr98wDATEOR2Ipx4r1+y0 rHeBvUJ27Gf6r4ptzS4qkp0gzNy8bYjQ90HDd9dY1Hl/Z4UmzAP94wUVLCAvNYjehf2b Z40y9HE4RITXNLgKFJpsMyOdpU27U0k5nEzH1aBwGmALHytG74X4pRXXyQsJw0QquQgV y0Eolv4LLRrvKqEEvF5vy2zYRvdfZACNQPwFRbZpKHPWUMUXdNs+h6YlUT2b7tyXa/Yy SbcSbNgbro82aj9nmJHUUAxjPTLtUvdghgRzjsJFtT5F62VMpDTWo3EuYOXja84dcRMF 0xcQ== X-Forwarded-Encrypted: i=1; AFNElJ8pwItCbUMNv01aRsrhn2D4dxD1+WIoii/WXy5zozM5mopqgGmvhJCA38j/E77m1ThSnHeIQ9dtdRL3zMs=@vger.kernel.org X-Gm-Message-State: AOJu0Yy+IyQ48z0dmcFXAWLtiWQA80OpevbGGXT5fwHJlN6rrxf72ghh Nzhy8qVY7dsV2tYw6z11YcwvEm+YCu+Ivv1yoEgqUlfGR3uhpU+SROVFyi3Fi1/HR+mXY1Gi3aO RYfmD4S7TEg== X-Received: from dyclv10.prod.google.com ([2002:a05:7301:618a:b0:2f7:9a2b:1dba]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:7300:8615:b0:2c6:67b6:3acc with SMTP id 5a478bee46e88-30ba3961dc5mr981586eec.15.1781572589763; Mon, 15 Jun 2026 18:16:29 -0700 (PDT) Date: Mon, 15 Jun 2026 18:15:41 -0700 In-Reply-To: <20260616011543.4037138-1-irogers@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260615223011.3873955-1-irogers@google.com> <20260616011543.4037138-1-irogers@google.com> X-Mailer: git-send-email 2.54.0.1136.gdb2ca164c4-goog Message-ID: <20260616011543.4037138-20-irogers@google.com> Subject: [PATCH v21 19/21] perf python: Add type checking for parse_events/parse_metrics From: Ian Rogers To: irogers@google.com, acme@kernel.org, namhyung@kernel.org Cc: adrian.hunter@intel.com, alice.mei.rogers@gmail.com, dapeng1.mi@linux.intel.com, james.clark@linaro.org, leo.yan@linux.dev, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, mingo@redhat.com, peterz@infradead.org, tmricht@linux.ibm.com Content-Type: text/plain; charset="UTF-8" The threads and cpus parameters in parse_events and parse_metrics are parsed with the 'O' format specifier but blindly casted in the C extension. If a user passes an invalid object type, this leads to memory corruption when dereferencing the expected struct. Add runtime PyObject_TypeCheck validations in python.c to safely raise a TypeError if an invalid object is passed. Signed-off-by: Ian Rogers --- tools/perf/util/python.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index 4ec5a91d45c0..11e1f39c1bea 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -3341,6 +3341,20 @@ static PyObject *pyrf__parse_events(PyObject *self, PyObject *args) return NULL; } + if (pthreads && pthreads != Py_None && + !PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) { + PyErr_SetString(PyExc_TypeError, "threads must be a perf.thread_map or None"); + evlist__put(evlist); + return NULL; + } + + if (pcpus && pcpus != Py_None && + !PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) { + PyErr_SetString(PyExc_TypeError, "cpus must be a perf.cpu_map or None"); + evlist__put(evlist); + return NULL; + } + threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; cpus = (pcpus && pcpus != Py_None) ? @@ -3377,6 +3391,20 @@ static PyObject *pyrf__parse_metrics(PyObject *self, PyObject *args) return NULL; } + if (pthreads && pthreads != Py_None && + !PyObject_TypeCheck(pthreads, &pyrf_thread_map__type)) { + PyErr_SetString(PyExc_TypeError, "threads must be a perf.thread_map or None"); + evlist__put(evlist); + return NULL; + } + + if (pcpus && pcpus != Py_None && + !PyObject_TypeCheck(pcpus, &pyrf_cpu_map__type)) { + PyErr_SetString(PyExc_TypeError, "cpus must be a perf.cpu_map or None"); + evlist__put(evlist); + return NULL; + } + threads = (pthreads && pthreads != Py_None) ? ((struct pyrf_thread_map *)pthreads)->threads : NULL; cpus = (pcpus && pcpus != Py_None) ? -- 2.54.0.1136.gdb2ca164c4-goog