Linux Perf Users
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Ian Rogers" <irogers@google.com>
Cc: linux-perf-users@vger.kernel.org
Subject: Re: [PATCH v1 12/13] perf python: Add thread uninitialized checks
Date: Tue, 23 Jun 2026 22:49:51 +0000	[thread overview]
Message-ID: <20260623224951.E2F331F000E9@smtp.kernel.org> (raw)
In-Reply-To: <20260623053515.2677888-13-irogers@google.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Pre-existing issues:
- [High] The patch incompletely fixes the missing initialization checks by omitting `pyrf_thread__get_cpu`, leaving a NULL pointer dereference vulnerability.
- [High] The `perf.pmu` type methods lack initialization guards, allowing uninitialized objects to cause NULL pointer dereferences.
- [Medium] The Python object `count_values` is leaked in multiple error paths within `pyrf_evsel__read`.
--

commit 95a2c7473298664fd72c7086443470ff19559438
Author: Ian Rogers <irogers@google.com>

perf python: Add thread uninitialized checks

The patch adds CHECK_INITIALIZED checks to the thread attribute getters
(get_pid, get_tid, get_ppid) in the perf python module to prevent crashes
when accessing uninitialized thread objects.

> diff --git a/tools/perf/util/python.c b/tools/perf/util/python.c
> index 03b4e88091074..70f985ad3dd20 100644
> --- a/tools/perf/util/python.c
> +++ b/tools/perf/util/python.c
> @@ -3743,16 +3743,19 @@ static PyMethodDef pyrf_thread__methods[] = {
>  
>  static PyObject *pyrf_thread__get_pid(struct pyrf_thread *pthread, void *closure __maybe_unused)
>  {
> +	CHECK_INITIALIZED(pthread->thread, "thread");
>  	return PyLong_FromLong(thread__pid(pthread->thread));
>  }
>  
>  static PyObject *pyrf_thread__get_tid(struct pyrf_thread *pthread, void *closure __maybe_unused)
>  {
> +	CHECK_INITIALIZED(pthread->thread, "thread");
>  	return PyLong_FromLong(thread__tid(pthread->thread));
>  }
>  
>  static PyObject *pyrf_thread__get_ppid(struct pyrf_thread *pthread, void *closure __maybe_unused)
>  {
> +	CHECK_INITIALIZED(pthread->thread, "thread");
>  	return PyLong_FromLong(thread__ppid(pthread->thread));
>  }
>  

[Severity: High]
This isn't a bug introduced by this patch, but pyrf_thread__get_cpu() is
missing a similar check:

tools/perf/util/python.c:pyrf_thread__get_cpu() {
    return PyLong_FromLong(thread__cpu(pthread->thread));
}

Could accessing the .cpu property on an uninitialized thread object cause a
NULL pointer dereference? Does it make sense to add a CHECK_INITIALIZED
guard here as well?

[Severity: High]
This isn't a bug introduced by this patch, but looking at the perf.pmu
methods, they also appear to lack initialization checks:

tools/perf/util/python.c:pyrf_pmu__name() {
    return PyUnicode_FromString(ppmu->pmu->name);
}

tools/perf/util/python.c:pyrf_pmu__events() {
    ...
    ret = perf_pmu__for_each_event(ppmu->pmu,
                                   false,
                                   py_list,
                                   pyrf_pmu__events_cb);
    ...
}

Since perf.pmu uses PyType_GenericNew, could a script instantiate an
uninitialized PMU and trigger a NULL pointer dereference when calling .name()
or .events()?

[Severity: Medium]
This isn't a bug introduced by this patch, but I noticed a leak in
pyrf_evsel__read() on its error paths:

tools/perf/util/python.c:pyrf_evsel__read() {
    count_values = PyObject_New(struct pyrf_counts_values,
                                &pyrf_counts_values__type);
    if (!count_values)
        return NULL;

    if (!PyArg_ParseTuple(args, "ii", &cpu, &thread))
        return NULL;

    cpu_idx = perf_cpu_map__idx(evsel->core.cpus, (struct perf_cpu){.cpu = cpu});
    if (cpu_idx < 0) {
        PyErr_Format(PyExc_TypeError, "CPU %d is not part of evsel's CPUs", cpu);
        return NULL;
    }
    ...
}

Does returning NULL directly on argument parsing or bounds checking failures
leak the newly allocated count_values object? Should we call
Py_DECREF(count_values) before returning?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260623053515.2677888-1-irogers@google.com?part=12

  reply	other threads:[~2026-06-23 22:49 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-23  5:35 [PATCH v1 00/13] perf python: Fix python extension bugs and memory leaks Ian Rogers
2026-06-23  5:35 ` [PATCH v1 01/13] perf script: Fix metric_evlist leak in script_find_metrics Ian Rogers
2026-06-23  5:35 ` [PATCH v1 02/13] perf stat: Fix evsel_list leak in cmd_stat Ian Rogers
2026-06-23  5:35 ` [PATCH v1 03/13] perf top: Fix sb_evlist leak Ian Rogers
2026-06-23 21:26   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 04/13] perf python: Fix memory leak in pyrf_evlist__get_pollfd Ian Rogers
2026-06-23 21:34   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 05/13] perf synthetic-events: Fix uninitialized pthread_join Ian Rogers
2026-06-23 21:41   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 06/13] perf data: Fix directory file descriptor leak in perf_data__switch Ian Rogers
2026-06-23  5:35 ` [PATCH v1 07/13] perf test: Fix skiplist leak in cmd_test Ian Rogers
2026-06-23  5:35 ` [PATCH v1 08/13] perf python: Check counts_values size in set_values Ian Rogers
2026-06-23 22:08   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 09/13] perf python: Validate CPU and thread maps in pyrf_evsel__open Ian Rogers
2026-06-23 22:17   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 10/13] perf python: Validate attribute setters in pyrf_evsel Ian Rogers
2026-06-23  5:35 ` [PATCH v1 11/13] perf python: Zero initialize perf_data in pyrf_data__init Ian Rogers
2026-06-23 22:44   ` sashiko-bot
2026-06-23  5:35 ` [PATCH v1 12/13] perf python: Add thread uninitialized checks Ian Rogers
2026-06-23 22:49   ` sashiko-bot [this message]
2026-06-23  5:35 ` [PATCH v1 13/13] perf python: Fix MetricGroup return type in perf.pyi Ian Rogers
2026-06-23 22:41   ` sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260623224951.E2F331F000E9@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=irogers@google.com \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox