Skip to content

Commit 80938dd

Browse files
committed
mm/kmemleak: fix UAF bug in kmemleak_scan()
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2151065 Upstream Status: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git/commit/?h=mm-stable&id=782e4179535971c3574c367bfaaefea8970b3e0b commit 782e417 Author: Waiman Long <longman@redhat.com> Date: Wed, 18 Jan 2023 23:01:11 -0500 mm/kmemleak: fix UAF bug in kmemleak_scan() Commit 6edda04 ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") fixes soft lockup problem in kmemleak_scan() by periodically doing a cond_resched(). It does take a reference of the current object before doing it. Unfortunately, if the object has been deleted from the object_list, the next object pointed to by its next pointer may no longer be valid after coming back from cond_resched(). This can result in use-after-free and other nasty problem. Fix this problem by adding a del_state flag into kmemleak_object structure to synchronize the object deletion process between kmemleak_cond_resched() and __remove_object() to make sure that the object remained in the object_list in the duration of the cond_resched() call. Link: https://lkml.kernel.org/r/20230119040111.350923-3-longman@redhat.com Fixes: 6edda04 ("mm/kmemleak: prevent soft lockup in first object iteration loop of kmemleak_scan()") Signed-off-by: Waiman Long <longman@redhat.com> Reviewed-by: Catalin Marinas <catalin.marinas@arm.com> Cc: Muchun Song <songmuchun@bytedance.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Waiman Long <longman@redhat.com>
1 parent 91d920f commit 80938dd

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

mm/kmemleak.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
*
1414
* The following locks and mutexes are used by kmemleak:
1515
*
16-
* - kmemleak_lock (raw_spinlock_t): protects the object_list modifications and
17-
* accesses to the object_tree_root (or object_phys_tree_root). The
18-
* object_list is the main list holding the metadata (struct kmemleak_object)
19-
* for the allocated memory blocks. The object_tree_root and object_phys_tree_root
20-
* are red black trees used to look-up metadata based on a pointer to the
16+
* - kmemleak_lock (raw_spinlock_t): protects the object_list as well as
17+
* del_state modifications and accesses to the object_tree_root (or
18+
* object_phys_tree_root). The object_list is the main list holding the
19+
* metadata (struct kmemleak_object) for the allocated memory blocks.
20+
* The object_tree_root and object_phys_tree_root are red
21+
* black trees used to look-up metadata based on a pointer to the
2122
* corresponding memory block. The object_phys_tree_root is for objects
2223
* allocated with physical address. The kmemleak_object structures are
2324
* added to the object_list and object_tree_root (or object_phys_tree_root)
@@ -147,6 +148,7 @@ struct kmemleak_object {
147148
struct rcu_head rcu; /* object_list lockless traversal */
148149
/* object usage count; object freed when use_count == 0 */
149150
atomic_t use_count;
151+
unsigned int del_state; /* deletion state */
150152
unsigned long pointer;
151153
size_t size;
152154
/* pass surplus references to this pointer */
@@ -177,6 +179,11 @@ struct kmemleak_object {
177179
/* flag set for object allocated with physical address */
178180
#define OBJECT_PHYS (1 << 4)
179181

182+
/* set when __remove_object() called */
183+
#define DELSTATE_REMOVED (1 << 0)
184+
/* set to temporarily prevent deletion from object_list */
185+
#define DELSTATE_NO_DELETE (1 << 1)
186+
180187
#define HEX_PREFIX " "
181188
/* number of bytes to print per line; must be 16 or 32 */
182189
#define HEX_ROW_SIZE 16
@@ -567,7 +574,9 @@ static void __remove_object(struct kmemleak_object *object)
567574
rb_erase(&object->rb_node, object->flags & OBJECT_PHYS ?
568575
&object_phys_tree_root :
569576
&object_tree_root);
570-
list_del_rcu(&object->object_list);
577+
if (!(object->del_state & DELSTATE_NO_DELETE))
578+
list_del_rcu(&object->object_list);
579+
object->del_state |= DELSTATE_REMOVED;
571580
}
572581

573582
/*
@@ -634,6 +643,7 @@ static struct kmemleak_object *__create_object(unsigned long ptr, size_t size,
634643
object->count = 0; /* white color initially */
635644
object->jiffies = jiffies;
636645
object->checksum = 0;
646+
object->del_state = 0;
637647

638648
/* task information */
639649
if (in_hardirq()) {
@@ -1473,9 +1483,22 @@ static void kmemleak_cond_resched(struct kmemleak_object *object)
14731483
if (!get_object(object))
14741484
return; /* Try next object */
14751485

1486+
raw_spin_lock_irq(&kmemleak_lock);
1487+
if (object->del_state & DELSTATE_REMOVED)
1488+
goto unlock_put; /* Object removed */
1489+
object->del_state |= DELSTATE_NO_DELETE;
1490+
raw_spin_unlock_irq(&kmemleak_lock);
1491+
14761492
rcu_read_unlock();
14771493
cond_resched();
14781494
rcu_read_lock();
1495+
1496+
raw_spin_lock_irq(&kmemleak_lock);
1497+
if (object->del_state & DELSTATE_REMOVED)
1498+
list_del_rcu(&object->object_list);
1499+
object->del_state &= ~DELSTATE_NO_DELETE;
1500+
unlock_put:
1501+
raw_spin_unlock_irq(&kmemleak_lock);
14791502
put_object(object);
14801503
}
14811504

0 commit comments

Comments
 (0)