Intel-XE Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Vinay Belgaumkar" <vinay.belgaumkar@intel.com>
Cc: intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: Re: [PATCH v7 2/2] perf: Add checks to prevent null ptr access
Date: Wed, 02 Sep 2026 16:12:31 +0000	[thread overview]
Message-ID: <20260902161231.79E151F00A3D@smtp.kernel.org> (raw)
In-Reply-To: <20260902154852.742844-3-vinay.belgaumkar@intel.com>

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

Pre-existing issues:
- [Critical] The added NULL and state checks do not fully resolve the Time-of-Check to Time-of-Use (TOCTOU) race condition against PMU unregistration, leaving NULL pointer dereference and Use-After-Free (UAF) vulnerabilities.
--

commit 63da6f63d2cf2058c6cbd31229c510f17f749641
Author: Vinay Belgaumkar <vinay.belgaumkar@intel.com>

perf: Add checks to prevent null ptr access

This patch attempts to prevent null pointer accesses by adding revoked
state checks in perf_event_read_local() and null checks in
__perf_event_read_cpu().

> diff --git a/kernel/events/core.c b/kernel/events/core.c
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c

[ ... ]

> @@ -4788,14 +4788,19 @@ static inline const struct cpumask *perf_scope_cpu_topology_cpumask(unsigned int
>  
>  static int __perf_event_read_cpu(struct perf_event *event, int event_cpu)
>  {
> +	struct pmu *pmu = READ_ONCE(event->pmu);
>  	int local_cpu = smp_processor_id();
>  	u16 local_pkg, event_pkg;
>  
>  	if ((unsigned)event_cpu >= nr_cpu_ids)
>  		return event_cpu;
>  
> +	if (!pmu)
> +		return -ENODEV;
> +
>  	if (event->group_caps & PERF_EV_CAP_READ_SCOPE) {
> -		const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(event->pmu->scope, event_cpu);
> +		const struct cpumask *cpumask = perf_scope_cpu_topology_cpumask(pmu->scope,
> +										event_cpu);

[Severity: Critical]
This is a pre-existing issue, but does reading pmu->scope here leave us
vulnerable to a use-after-free if the PMU object is concurrently freed?

Since we only read the pointer with READ_ONCE() and do not appear to be
holding RCU or a lock protecting the PMU's lifetime against unregistration,
could the PMU be detached and freed right before we access it?

[ ... ]

> @@ -4917,6 +4922,11 @@ int perf_event_read_local(struct perf_event *event, u64 *value,
>  		goto out;
>  	}
>  
> +	if (READ_ONCE(event->state) <= PERF_EVENT_STATE_REVOKED) {
> +		ret = -ENODEV;
> +		goto out;
> +	}
> +

[Severity: Critical]
This isn't a bug introduced by this patch, but does this state check fully
resolve the time-of-check to time-of-use race condition against PMU
unregistration?

If a userspace program triggers a BPF map update on a perf event array, it
calls perf_event_read_local() without holding rcu_read_lock(). If a concurrent
hardware hot-unplug calls perf_pmu_unregister(), it might issue an IPI to a
remote CPU to detach the event while this local CPU passes the revoked state
check.

If that race occurs, the PMU can be freed immediately after this check, but
further down in perf_event_read_local() we still directly dereference
event->pmu:

    if (event_oncpu == smp_processor_id())
        event->pmu->read(event);

Could this lead to a NULL pointer dereference or use-after-free since the
local IRQ disable doesn't serialize against the remote IPI during PMU
unregistration?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260902154852.742844-1-vinay.belgaumkar@intel.com?part=2

  reply	other threads:[~2026-09-02 16:12 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-02 15:48 [PATCH v7 0/2] perf: Add some null ptr checks Vinay Belgaumkar
2026-09-02 15:48 ` [PATCH v7 1/2] perf: Fix null pointer access in is_include_guest_event() Vinay Belgaumkar
2026-09-03  1:50   ` Mi, Dapeng
2026-09-02 15:48 ` [PATCH v7 2/2] perf: Add checks to prevent null ptr access Vinay Belgaumkar
2026-09-02 16:12   ` sashiko-bot [this message]
2026-09-02 17:43 ` ✓ CI.KUnit: success for perf: Add some null ptr checks (rev3) Patchwork
2026-09-02 18:28 ` ✓ Xe.CI.BAT: " Patchwork
2026-09-03  7:47 ` ✗ Xe.CI.FULL: failure " Patchwork

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=20260902161231.79E151F00A3D@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dri-devel@lists.freedesktop.org \
    --cc=intel-xe@lists.freedesktop.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=vinay.belgaumkar@intel.com \
    /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