[PATCH 8/9] mm: kmemleak: erase page->freelist in slab_destroy

From: George Prekas
Date: Mon Jan 23 2023 - 12:05:46 EST


slab_destroy frees the allocated memory pointed by page->freelist. It
should also erase the reference to it. Otherwise, when the memory is
reused for another object, kmemleak will find the stale pointer and
erroneously increase the reference count of the new object. That will
lead to undetected memory leaks.

Signed-off-by: George Prekas <george@xxxxxxxxxxxxx>
---
mm/slab.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mm/slab.c b/mm/slab.c
index aa5eb725ee9c..12acce274502 100644
--- a/mm/slab.c
+++ b/mm/slab.c
@@ -1625,8 +1625,10 @@ static void slab_destroy(struct kmem_cache *cachep, struct slab *slab)
* From now on, we don't use freelist
* although actual page can be freed in rcu context
*/
- if (OFF_SLAB(cachep))
+ if (OFF_SLAB(cachep)) {
kfree(freelist);
+ kmemleak_erase(&slab->freelist);
+ }
}

/*
--
2.37.1