Skip to content

Commit 678ff6a

Browse files
shakeelbtorvalds
authored andcommitted
mm: slab: fix potential double free in ___cache_free
With the commit 10befea ("mm: memcg/slab: use a single set of kmem_caches for all allocations"), it becomes possible to call kfree() from the slabs_destroy(). The functions cache_flusharray() and do_drain() calls slabs_destroy() on array_cache of the local CPU without updating the size of the array_cache. This enables the kfree() call from the slabs_destroy() to recursively call cache_flusharray() which can potentially call free_block() on the same elements of the array_cache of the local CPU and causing double free and memory corruption. To fix the issue, simply update the local CPU array_cache cache before calling slabs_destroy(). Fixes: 10befea ("mm: memcg/slab: use a single set of kmem_caches for all allocations") Signed-off-by: Shakeel Butt <shakeelb@google.com> Reviewed-by: Roman Gushchin <guro@fb.com> Tested-by: Ming Lei <ming.lei@redhat.com> Reported-by: kernel test robot <rong.a.chen@intel.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Ted Ts'o <tytso@mit.edu> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 7c7ec32 commit 678ff6a

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

mm/slab.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,6 +1632,10 @@ static void slab_destroy(struct kmem_cache *cachep, struct page *page)
16321632
kmem_cache_free(cachep->freelist_cache, freelist);
16331633
}
16341634

1635+
/*
1636+
* Update the size of the caches before calling slabs_destroy as it may
1637+
* recursively call kfree.
1638+
*/
16351639
static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
16361640
{
16371641
struct page *page, *n;
@@ -2153,8 +2157,8 @@ static void do_drain(void *arg)
21532157
spin_lock(&n->list_lock);
21542158
free_block(cachep, ac->entry, ac->avail, node, &list);
21552159
spin_unlock(&n->list_lock);
2156-
slabs_destroy(cachep, &list);
21572160
ac->avail = 0;
2161+
slabs_destroy(cachep, &list);
21582162
}
21592163

21602164
static void drain_cpu_caches(struct kmem_cache *cachep)
@@ -3402,9 +3406,9 @@ static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
34023406
}
34033407
#endif
34043408
spin_unlock(&n->list_lock);
3405-
slabs_destroy(cachep, &list);
34063409
ac->avail -= batchcount;
34073410
memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
3411+
slabs_destroy(cachep, &list);
34083412
}
34093413

34103414
/*

0 commit comments

Comments
 (0)