Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Catalin Marinas" <catalin.marinas@arm.com>,
	"Andrew Morton" <akpm@linux-foundation.org>
Cc: "Weilin Tong" <tongweilin@linux.alibaba.com>,
	"Breno Leitao" <leitao@debian.org>,
	"David Hildenbrand" <david@kernel.org>,
	linux-mm@kvack.org
Subject: Re: [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
Date: Fri, 04 Dec 2026 15:25:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-250094@kernel.org> (raw)
In-Reply-To: <20261203152515.2270778-2-catalin.marinas@arm.com>

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] mm: kmemleak: double remove and UAF in
  __kmemleak_do_cleanup()
--

commit 48955aa5e1c8f3cd5cbd6cee7646e58ca94dc439
Author: Catalin Marinas <catalin.marinas@arm.com>

mm: kmemleak: take kmemleak_lock in the disable-time object cleanup

This commit takes kmemleak_lock around removing each object from
object_list in __kmemleak_do_cleanup() by repeatedly popping the first
entry instead of traversing the list unlocked with
list_for_each_entry_safe().

Link: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com

> diff --git a/mm/kmemleak.c b/mm/kmemleak.c
> index 20c0eb41c8730..9c2d01fd41134 100644
> --- a/mm/kmemleak.c
> +++ b/mm/kmemleak.c

[ ... ]

> @@ -2473,15 +2473,25 @@ static const struct file_operations kmemleak_fops = {
>  
>  static void __kmemleak_do_cleanup(void)
>  {
> -	struct kmemleak_object *object, *tmp;
> +	struct kmemleak_object *object;
> +	unsigned long flags;
>  	unsigned int cnt = 0;
>  
>  	/*
> -	 * Kmemleak has already been disabled, no need for RCU list traversal
> -	 * or kmemleak_lock held.
> +	 * Callbacks that passed their enabled checks before kmemleak was
> +	 * disabled may still be adding or removing objects.
>  	 */
> -	list_for_each_entry_safe(object, tmp, &object_list, object_list) {
> -		__remove_object(object);
> +	while (true) {
> +		raw_spin_lock_irqsave(&kmemleak_lock, flags);
> +		object = list_first_entry_or_null(&object_list,
> +						  struct kmemleak_object,
> +						  object_list);
> +		if (object)
> +			__remove_object(object);
> +		raw_spin_unlock_irqrestore(&kmemleak_lock, flags);
> +
> +		if (!object)
> +			break;
>  		__delete_object(object);

[Severity: Medium]
Can this loop remove and delete the same kmemleak_object twice when
DELSTATE_NO_DELETE is set?

__remove_object() skips list_del_rcu(&object->object_list) when
DELSTATE_NO_DELETE is set, leaving the object at the head of object_list
while still erasing its rb_node and setting DELSTATE_REMOVED.

A concurrent scan thread can set DELSTATE_NO_DELETE in
kmemleak_cond_resched() because kmemleak_do_cleanup() calls
stop_scan_thread() before scan_mutex and drops scan_mutex before
cleanup, while start_scan_thread() does not check kmemleak_enabled:

CPU0                     kworker                   scan_thread
kmemleak_write()
  lock(&scan_mutex)
                         kmemleak_do_cleanup()
                           stop_scan_thread()
                           lock(&scan_mutex)
  start_scan_thread()
  unlock(&scan_mutex)
                           kmemleak_free_enabled=0
                           unlock(&scan_mutex)
                           __kmemleak_do_cleanup()
                                                   kmemleak_scan()
                                                     kmemleak_cond_resched()
                                                       DELSTATE_NO_DELETE
                                                       rcu_read_unlock()
                                                       cond_resched_...()
                           __remove_object(obj)
                           /* kept on list */
                           __delete_object(obj)
                           __remove_object(obj)
                             <-- double rb_erase()
                           __delete_object(obj)
                             <-- frees obj
                                                   /* use-after-free */

Once that pinned object reaches the head of object_list, the next
iteration pops it again, calling rb_erase() on an already-erased node
and corrupting the rb-tree.

The repeated __delete_object() also drops use_count to zero while
kmemleak_cond_resched() holds a pointer to the object outside
rcu_read_lock(), causing a use-after-free.

Checking kmemleak_enabled in start_scan_thread() and stopping
scan_thread under scan_mutex (or skipping entries with
DELSTATE_NO_DELETE in __kmemleak_do_cleanup()) would prevent this.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261203152515.2270778-1-catalin.marinas@arm.com?part=1


  reply	other threads:[~2026-12-04 15:25 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 15:25 [PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races Catalin Marinas
2026-12-03 15:25 ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup Catalin Marinas
2026-12-04 15:25   ` sashiko-bot [this message]
2026-12-03 15:25 ` [PATCH 2/3] mm: kmemleak: do not hex dump objects once free tracking has stopped Catalin Marinas
2026-12-03 15:25 ` [PATCH 3/3] mm: kmemleak: annotate the lockless accesses to the enable flags Catalin Marinas

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=sashiko-outbox-250094@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=akpm@linux-foundation.org \
    --cc=catalin.marinas@arm.com \
    --cc=david@kernel.org \
    --cc=leitao@debian.org \
    --cc=linux-mm@kvack.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=tongweilin@linux.alibaba.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