Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races
@ 2026-12-03 15:25 Catalin Marinas
  2026-12-03 15:25 ` [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup Catalin Marinas
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Catalin Marinas @ 2026-12-03 15:25 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Weilin Tong, Breno Leitao, David Hildenbrand, linux-mm,
	linux-kernel

Hi Andrew,

Patch 1 here supersedes Weilin's "mm: kmemleak: serialize disable-time
cleanup" in mm-unstable, so I think we can drop that one. I reviewed it
but Sashiko found more problems in the area, so here's a more
comprehensive fix. If you'd rather keep it (it's harmless), I can rebase
on top. This series is based on v7.4-rc1.

When kmemleak is disabled at runtime, __kmemleak_do_cleanup() removes
the objects without kmemleak_lock. A "clear" written to debugfs can run
a second cleanup concurrently with cleanup_work's (reported by Weilin
[1]) and callbacks such as kmemleak_free() that passed their enabled
checks before the disable can still update the object tree and list
(reported by Sashiko [2]). Both corrupt the tree and list.

Patch 1 takes kmemleak_lock when removing each object in the cleanup,
covering both cases. Patch 2 skips the hex dump in the report once frees
are no longer tracked since the reported objects may have been freed.
Patch 3 annotates the lockless accesses to the enable flags for
completeness. I couldn't get KCSAN to trigger a warning.

Tested on arm64 under Qemu with KASAN. On v7.4-rc1, writing "off" and
then "clear" crashes reliably and patch 1 fixes it. The concurrent
callback race needed some delays inserted to trigger a KASAN
use-after-free in rb_erase() (also fixed by patch 1).

LLM help (Claude): race analysis, testing, code and commit log
checks/suggestions, identifying the hex dump problem fixed by patch 2.

[1] https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com
[2] https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463

Catalin Marinas (3):
  mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
  mm: kmemleak: do not hex dump objects once free tracking has stopped
  mm: kmemleak: annotate the lockless accesses to the enable flags

 mm/kmemleak.c | 73 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 45 insertions(+), 28 deletions(-)



^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
  2026-12-03 15:25 [PATCH 0/3] mm: kmemleak: fix the disable-time cleanup races Catalin Marinas
@ 2026-12-03 15:25 ` Catalin Marinas
  2026-12-04 15:25   ` sashiko-bot
  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
  2 siblings, 1 reply; 5+ messages in thread
From: Catalin Marinas @ 2026-12-03 15:25 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Weilin Tong, Breno Leitao, David Hildenbrand, linux-mm,
	linux-kernel

__kmemleak_do_cleanup() removes objects from object_list and the object
tree without kmemleak_lock, assuming nothing else changes them once
kmemleak is disabled. This is not the case:

- a "clear" written to the debugfs file before cleanup_work has cleared
  kmemleak_free_enabled runs the cleanup while frees are still tracked;

- a "clear" written while cleanup_work's own cleanup is still running
  starts a second, concurrent cleanup;

- when kmemleak disables itself, e.g. on an __alloc_object() failure, a
  kmemleak_free() or kmemleak_alloc() that passed its enabled check
  before the flag was cleared can still update the tree and list.

The concurrent updates corrupt the tree and list and lead to a
use-after-free in rb_erase().

Take kmemleak_lock when removing each object in the cleanup.

Fixes: 2abd839aa7e6 ("kmemleak: Do not corrupt the object_list during clean-up")
Reported-by: Weilin Tong <tongweilin@linux.alibaba.com>
Link: https://lore.kernel.org/r/149d93f2cfe20d355f4d9882ce2bf99030217772.1793871596.git.tongweilin@linux.alibaba.com
Reported-by: Sashiko <sashiko-bot@kernel.org>
Closes: https://sashiko.dev/#/bug/linux-d5496447-37ff-43f3-81ed-e1a5616df463
Cc: <stable@vger.kernel.org>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
---
 mm/kmemleak.c | 22 ++++++++++++++++------
 1 file changed, 16 insertions(+), 6 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 20c0eb41c873..9c2d01fd4113 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -619,7 +619,7 @@ static struct kmemleak_object *find_and_get_object(unsigned long ptr, int alias)
 
 /*
  * Remove an object from its object tree and object_list. Must be called with
- * the kmemleak_lock held _if_ kmemleak is still enabled.
+ * the kmemleak_lock held.
  */
 static void __remove_object(struct kmemleak_object *object)
 {
@@ -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);
 
 		/* Call cond_resched() once per 64 iterations to avoid soft lockup */


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/3] mm: kmemleak: do not hex dump objects once free tracking has stopped
  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-03 15:25 ` Catalin Marinas
  2026-12-03 15:25 ` [PATCH 3/3] mm: kmemleak: annotate the lockless accesses to the enable flags Catalin Marinas
  2 siblings, 0 replies; 5+ messages in thread
From: Catalin Marinas @ 2026-12-03 15:25 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Weilin Tong, Breno Leitao, David Hildenbrand, linux-mm,
	linux-kernel

If kmemleak is disabled after reporting leaks, the metadata is kept so
that the report can still be read, but kmemleak_free_enabled is cleared.
kmemleak_free() then no longer clears OBJECT_ALLOCATED, which the report
checks under object->lock before hex dumping an object. Reading the
report can therefore hex dump memory that has been freed, or unmapped in
the case of vmalloc and percpu areas.

Skip the hex dump once kmemleak_free_enabled is cleared. The callers
hold scan_mutex, under which kmemleak_do_cleanup() clears the flag.

Fixes: 74341703edca ("kmemleak: Report previously found leaks even after an error")
Cc: <stable@vger.kernel.org>
Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
---
 mm/kmemleak.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 9c2d01fd4113..0fd3ea174367 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -311,6 +311,13 @@ static void hex_dump_object(struct seq_file *seq,
 	if (WARN_ON_ONCE(object->flags & OBJECT_PHYS))
 		return;
 
+	/*
+	 * Frees are no longer tracked, so the object may have been freed or
+	 * unmapped. The callers hold scan_mutex, under which the flag is cleared.
+	 */
+	if (!kmemleak_free_enabled)
+		return;
+
 	if (object->flags & OBJECT_PERCPU)
 		ptr = (const u8 *)this_cpu_ptr((void __percpu *)object->pointer);
 


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/3] mm: kmemleak: annotate the lockless accesses to the enable flags
  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-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 ` Catalin Marinas
  2 siblings, 0 replies; 5+ messages in thread
From: Catalin Marinas @ 2026-12-03 15:25 UTC (permalink / raw)
  To: Andrew Morton
  Cc: Weilin Tong, Breno Leitao, David Hildenbrand, linux-mm,
	linux-kernel

kmemleak_enabled and kmemleak_free_enabled are read without a lock in
the kmemleak callbacks while kmemleak_disable() and
kmemleak_do_cleanup() clear them. Use READ_ONCE() and WRITE_ONCE() for
these accesses. No functional change.

Signed-off-by: Catalin Marinas <catalin.marinas@arm.com>
---
 mm/kmemleak.c | 46 +++++++++++++++++++++++-----------------------
 1 file changed, 23 insertions(+), 23 deletions(-)

diff --git a/mm/kmemleak.c b/mm/kmemleak.c
index 0fd3ea174367..9af4fbf704a1 100644
--- a/mm/kmemleak.c
+++ b/mm/kmemleak.c
@@ -315,7 +315,7 @@ static void hex_dump_object(struct seq_file *seq,
 	 * Frees are no longer tracked, so the object may have been freed or
 	 * unmapped. The callers hold scan_mutex, under which the flag is cleared.
 	 */
-	if (!kmemleak_free_enabled)
+	if (!READ_ONCE(kmemleak_free_enabled))
 		return;
 
 	if (object->flags & OBJECT_PERCPU)
@@ -1121,7 +1121,7 @@ void __ref kmemleak_alloc(const void *ptr, size_t size, int min_count,
 {
 	pr_debug("%s(0x%px, %zu, %d)\n", __func__, ptr, size, min_count);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		create_object((unsigned long)ptr, size, min_count, gfp);
 }
 EXPORT_SYMBOL_GPL(kmemleak_alloc);
@@ -1140,7 +1140,7 @@ void __ref kmemleak_alloc_percpu(const void __percpu *ptr, size_t size,
 {
 	pr_debug("%s(0x%px, %zu)\n", __func__, ptr, size);
 
-	if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		create_object_percpu((__force unsigned long)ptr, size, 1, gfp);
 }
 EXPORT_SYMBOL_GPL(kmemleak_alloc_percpu);
@@ -1162,7 +1162,7 @@ void __ref kmemleak_vmalloc(const struct vm_struct *area, size_t size, gfp_t gfp
 	 * A min_count = 2 is needed because vm_struct contains a reference to
 	 * the virtual address of the vmalloc'ed block.
 	 */
-	if (kmemleak_enabled) {
+	if (READ_ONCE(kmemleak_enabled)) {
 		create_object((unsigned long)area->addr, size, 2, gfp);
 		object_set_excess_ref((unsigned long)area,
 				      (unsigned long)area->addr);
@@ -1181,7 +1181,7 @@ void __ref kmemleak_free(const void *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_free_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_free_enabled) && ptr && !IS_ERR(ptr))
 		delete_object_full((unsigned long)ptr, 0);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free);
@@ -1199,7 +1199,7 @@ void __ref kmemleak_free_part(const void *ptr, size_t size)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		delete_object_part((unsigned long)ptr, size, 0);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free_part);
@@ -1215,7 +1215,7 @@ void __ref kmemleak_free_percpu(const void __percpu *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_free_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_free_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		delete_object_full((__force unsigned long)ptr, OBJECT_PERCPU);
 }
 EXPORT_SYMBOL_GPL(kmemleak_free_percpu);
@@ -1235,7 +1235,7 @@ void __ref kmemleak_update_trace(const void *ptr)
 
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (!kmemleak_enabled || IS_ERR_OR_NULL(ptr))
+	if (!READ_ONCE(kmemleak_enabled) || IS_ERR_OR_NULL(ptr))
 		return;
 
 	object = find_and_get_object((unsigned long)ptr, 1);
@@ -1267,7 +1267,7 @@ void __ref kmemleak_not_leak(const void *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		make_gray_object((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_not_leak);
@@ -1284,7 +1284,7 @@ void __ref kmemleak_transient_leak(const void *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		reset_checksum((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_transient_leak);
@@ -1298,7 +1298,7 @@ void __ref kmemleak_ignore_percpu(const void __percpu *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR_PCPU(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR_PCPU(ptr))
 		make_black_object((unsigned long)ptr, OBJECT_PERCPU);
 }
 EXPORT_SYMBOL_GPL(kmemleak_ignore_percpu);
@@ -1316,7 +1316,7 @@ void __ref kmemleak_ignore(const void *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		make_black_object((unsigned long)ptr, 0);
 }
 EXPORT_SYMBOL(kmemleak_ignore);
@@ -1336,7 +1336,7 @@ void __ref kmemleak_scan_area(const void *ptr, size_t size, gfp_t gfp)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && size && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && size && !IS_ERR(ptr))
 		add_scan_area((unsigned long)ptr, size, gfp);
 }
 EXPORT_SYMBOL(kmemleak_scan_area);
@@ -1354,7 +1354,7 @@ void __ref kmemleak_no_scan(const void *ptr)
 {
 	pr_debug("%s(0x%px)\n", __func__, ptr);
 
-	if (kmemleak_enabled && ptr && !IS_ERR(ptr))
+	if (READ_ONCE(kmemleak_enabled) && ptr && !IS_ERR(ptr))
 		object_no_scan((unsigned long)ptr);
 }
 EXPORT_SYMBOL(kmemleak_no_scan);
@@ -1370,7 +1370,7 @@ void __ref kmemleak_alloc_phys(phys_addr_t phys, size_t size, gfp_t gfp)
 {
 	pr_debug("%s(0x%px, %zu)\n", __func__, &phys, size);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		/*
 		 * Create object with OBJECT_PHYS flag and
 		 * assume min_count 0.
@@ -1390,7 +1390,7 @@ void __ref kmemleak_free_part_phys(phys_addr_t phys, size_t size)
 {
 	pr_debug("%s(0x%px)\n", __func__, &phys);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		delete_object_part((unsigned long)phys, size, OBJECT_PHYS);
 }
 EXPORT_SYMBOL(kmemleak_free_part_phys);
@@ -1404,7 +1404,7 @@ void __ref kmemleak_ignore_phys(phys_addr_t phys)
 {
 	pr_debug("%s(0x%px)\n", __func__, &phys);
 
-	if (kmemleak_enabled)
+	if (READ_ONCE(kmemleak_enabled))
 		make_black_object((unsigned long)phys, OBJECT_PHYS);
 }
 EXPORT_SYMBOL(kmemleak_ignore_phys);
@@ -1533,7 +1533,7 @@ static void pointer_update_refs(struct kmemleak_object *scanned,
  */
 static int scan_should_stop(void)
 {
-	if (!kmemleak_enabled)
+	if (!READ_ONCE(kmemleak_enabled))
 		return 1;
 
 	/*
@@ -2413,14 +2413,14 @@ static ssize_t kmemleak_write(struct file *file, const char __user *user_buf,
 		return ret;
 
 	if (strncmp(buf, "clear", 5) == 0) {
-		if (kmemleak_enabled)
+		if (READ_ONCE(kmemleak_enabled))
 			kmemleak_clear();
 		else
 			__kmemleak_do_cleanup();
 		goto out;
 	}
 
-	if (!kmemleak_enabled) {
+	if (!READ_ONCE(kmemleak_enabled)) {
 		ret = -EPERM;
 		goto out;
 	}
@@ -2523,7 +2523,7 @@ static void kmemleak_do_cleanup(struct work_struct *work)
 	 * the memory accesses below is guaranteed by the kthread_stop()
 	 * function.
 	 */
-	kmemleak_free_enabled = 0;
+	WRITE_ONCE(kmemleak_free_enabled, 0);
 	mutex_unlock(&scan_mutex);
 
 	if (!kmemleak_found_leaks)
@@ -2545,13 +2545,13 @@ static void kmemleak_disable(void)
 		return;
 
 	/* stop any memory operation tracing */
-	kmemleak_enabled = 0;
+	WRITE_ONCE(kmemleak_enabled, 0);
 
 	/* check whether it is too early for a kernel thread */
 	if (kmemleak_late_initialized)
 		schedule_work(&cleanup_work);
 	else
-		kmemleak_free_enabled = 0;
+		WRITE_ONCE(kmemleak_free_enabled, 0);
 
 	pr_info("Kernel memory leak detector disabled\n");
 }


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/3] mm: kmemleak: take kmemleak_lock in the disable-time object cleanup
  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
  0 siblings, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-12-04 15:25 UTC (permalink / raw)
  To: Catalin Marinas, Andrew Morton
  Cc: Weilin Tong, Breno Leitao, David Hildenbrand, linux-mm

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


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-12-04 15:25 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox