From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f198.google.com (mail-pf1-f198.google.com [209.85.210.198]) (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 A7C4C3ABD91 for ; Sun, 9 Aug 2026 07:15:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786259722; cv=none; b=Dms6dF8e5PehTFa9A0S4N8Lo8dkHPQn7NG2Yb3Q25ZWubW6vsEC6VI03Ta0EC8L7S3xc7lXxDy55Riud/puZt0v6SelnnqgfRQkwTOQArXIBav901YBEbV+un0YY/4gB+VI1YWfB25uD3bQ3x/taNEpzTVtZdb4jAeqhvzBNY3Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786259722; c=relaxed/simple; bh=/S/53oJkDp5iKRz8rYm+z/UcbxOvb+U5L/9rZurOV5Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=H2WbwrM5Xe4+IZAlOogSqQIJ7jmdIuUkq7ApbQGrpZ1LNlfepT5X8Ixq0S0TfIVMUxpAJmkUFljzLDy+wzImPyfokosGtn4LdGBtnIQWVlHTdeQcwnLHkDIvABGLW6T3mktcVOXHwLkktZsSGWJyQ1tSoesHjgQ2mf8fs6ssOD0= 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=Mw6Xwg1C; arc=none smtp.client-ip=209.85.210.198 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="Mw6Xwg1C" Received: by mail-pf1-f198.google.com with SMTP id d2e1a72fcca58-84eee2147ffso1362379b3a.0 for ; Sun, 09 Aug 2026 00:15:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786259717; x=1786864517; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ZnAKzRhK/xXX0RZtcxB/zK8LsdPI/TmfxzA+YrwKiiY=; b=Mw6Xwg1ChlUpLn4L+YkM13lTrtnSV9Z1y7SpHN2uEPqtnkqfc+Qetz0ALlnbvN/HBR HVR2kGTmcrhQtmocE8Tq+TcSdS36J2RBLkkPnjdBXCdILjPlMAN165zdIVpFEj6rvz8S QAfez7zFhvL2kN19eS2dxm3YJAbKqshJRanHXXEhWQpuISA/XO/1nF6bzZQJKHc3YVkz kQN2l/PQOArF2lRDSSZBdYaBffVIm7+nlNzoi09vjapurI10PnT8yozPI0WfFJAJ1Qa/ oWZzfuHbnCzb8OoGHhmvvOEbR6VJFZZKJ0HWGs9xr5Dv0sn+H1zTzTegdtU0Pwhgoqk7 UK7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786259717; x=1786864517; h=content-type: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:content-type; bh=ZnAKzRhK/xXX0RZtcxB/zK8LsdPI/TmfxzA+YrwKiiY=; b=fCKThO39EqZuMp0+dzuhnXJdTPsX2+QKUQhnp7ZEfuCo0WpNR9tMSJitu/wF8Nrir9 NQhnlKJCJh7SXGcD51zfrH53yiRKGpLNkr4MQvtyAF8XfX4r9LmII23B3owqtVxgSSRW 0ojrCofjKGhG2mSNKXJvbpCAK9gMsY5vN2gGHtZMx2v0/PsTe0yy/51rg9lTxq6fVaWE eTj9g3/yb+Dc5hE9pPAQFKmNHoT4wDcbbAxHT3E8pJH6lr+WE2EyHQ1qePqpNXGOQ1ux f+a4nh+ZZEz87o581d7HEIjqlIYRw+69FToxD5b77NoLMpD7pvCxMhZ1pB7S92TsQXo5 fEbw== X-Forwarded-Encrypted: i=1; AHgh+RrCfqEzXr51Ry8jjQBC/gDYwo4H8Kp8Smc0TChYvu1tAh1nISdE1dMT57a+QL4IvGOLNBP+Gc4VPlU0sbA=@vger.kernel.org X-Gm-Message-State: AOJu0YzFdEdJSuo4YSv44naUNJcgwSZZmIRLn+67+laHJORPY9qdj5v0 O/H1aCMoe0xLQUcfvp6YSPbuijbhyCXE9v/hhjEwSZju7UwEz3hmd5bv2cBKC9xZhgsjXMtuXpj ROyq+VBf1/Q== X-Received: from dli20-n1.prod.google.com ([2002:a05:7022:294:10b0:13b:97f1:acba]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d52f:b0:3c3:9070:96b3 with SMTP id adf61e73a8af0-3cbce6caba0mr14267702637.3.1786259716640; Sun, 09 Aug 2026 00:15:16 -0700 (PDT) Date: Sun, 9 Aug 2026 00:14:46 -0700 In-Reply-To: <20260809071455.1221514-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: <20260624051535.3584977-1-irogers@google.com> <20260809071455.1221514-1-irogers@google.com> X-Mailer: git-send-email 2.55.0.679.g6767b8d81c-goog Message-ID: <20260809071455.1221514-8-irogers@google.com> Subject: [PATCH v3 07/15] perf python: Check counts_values size in set_values 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 set_values function incorrectly assumed the list contained exactly 5 elements. Add a check to prevent out-of-bounds access. Fixes: 877108e42b1b ("perf tools: Initial python binding") Signed-off-by: Ian Rogers --- tools/perf/util/python.c | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c index c7ae1f85aec6..f64d662bd3ee 100644 --- a/tools/perf/util/python.c +++ b/tools/perf/util/python.c @@ -1881,11 +1881,11 @@ static void pyrf_counts_values__delete(struct pyrf_counts_values *pcounts_values 0, help } static PyMemberDef pyrf_counts_values_members[] = { - counts_values_member_def(val, T_ULONG, "Value of event"), - counts_values_member_def(ena, T_ULONG, "Time for which enabled"), - counts_values_member_def(run, T_ULONG, "Time for which running"), - counts_values_member_def(id, T_ULONG, "Unique ID for an event"), - counts_values_member_def(lost, T_ULONG, "Num of lost samples"), + counts_values_member_def(val, T_ULONGLONG, "Value of event"), + counts_values_member_def(ena, T_ULONGLONG, "Time for which enabled"), + counts_values_member_def(run, T_ULONGLONG, "Time for which running"), + counts_values_member_def(id, T_ULONGLONG, "Unique ID for an event"), + counts_values_member_def(lost, T_ULONGLONG, "Num of lost samples"), { .name = NULL, }, }; @@ -1895,8 +1895,15 @@ static PyObject *pyrf_counts_values_get_values(struct pyrf_counts_values *self, if (!vals) return NULL; - for (int i = 0; i < 5; i++) - PyList_SetItem(vals, i, PyLong_FromLong(self->values.values[i])); + for (int i = 0; i < 5; i++) { + PyObject *val = PyLong_FromUnsignedLongLong(self->values.values[i]); + + if (!val) { + Py_DECREF(vals); + return NULL; + } + PyList_SetItem(vals, i, val); + } return vals; } @@ -1907,19 +1914,34 @@ static int pyrf_counts_values_set_values(struct pyrf_counts_values *self, PyObje Py_ssize_t size; PyObject *item = NULL; + if (list == NULL) { + PyErr_SetString(PyExc_TypeError, "cannot delete attribute"); + return -1; + } + if (!PyList_Check(list)) { PyErr_SetString(PyExc_TypeError, "Value assigned must be a list"); return -1; } size = PyList_Size(list); + if (size != 5) { + PyErr_SetString(PyExc_ValueError, "List must have exactly 5 entries"); + return -1; + } + for (Py_ssize_t i = 0; i < size; i++) { + unsigned long long val; + item = PyList_GetItem(list, i); if (!PyLong_Check(item)) { PyErr_SetString(PyExc_TypeError, "List members should be numbers"); return -1; } - self->values.values[i] = PyLong_AsLong(item); + val = PyLong_AsUnsignedLongLong(item); + if (val == (unsigned long long)-1 && PyErr_Occurred()) + return -1; + self->values.values[i] = val; } return 0; -- 2.55.0.679.g6767b8d81c-goog