Skip to content

Commit 5312824

Browse files
xairytorvalds
authored andcommitted
kasan, kmemleak: pass tagged pointers to kmemleak
Right now we call kmemleak hooks before assigning tags to pointers in KASAN hooks. As a result, when an objects gets allocated, kmemleak sees a differently tagged pointer, compared to the one it sees when the object gets freed. Fix it by calling KASAN hooks before kmemleak's ones. Link: http://lkml.kernel.org/r/cd825aa4897b0fc37d3316838993881daccbe9f5.1549921721.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Reported-by: Qian Cai <cai@lca.pw> Cc: Alexander Potapenko <glider@google.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Christoph Lameter <cl@linux.com> Cc: David Rientjes <rientjes@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Evgeniy Stepanov <eugenis@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Cc: Kostya Serebryany <kcc@google.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: Vincenzo Frascino <vincenzo.frascino@arm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent e1db95b commit 5312824

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

mm/slab.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,9 @@ static inline void slab_post_alloc_hook(struct kmem_cache *s, gfp_t flags,
437437

438438
flags &= gfp_allowed_mask;
439439
for (i = 0; i < size; i++) {
440-
void *object = p[i];
441-
442-
kmemleak_alloc_recursive(object, s->object_size, 1,
440+
p[i] = kasan_slab_alloc(s, p[i], flags);
441+
kmemleak_alloc_recursive(p[i], s->object_size, 1,
443442
s->flags, flags);
444-
p[i] = kasan_slab_alloc(s, object, flags);
445443
}
446444

447445
if (memcg_kmem_enabled())

mm/slab_common.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1228,8 +1228,8 @@ void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
12281228
flags |= __GFP_COMP;
12291229
page = alloc_pages(flags, order);
12301230
ret = page ? page_address(page) : NULL;
1231-
kmemleak_alloc(ret, size, 1, flags);
12321231
ret = kasan_kmalloc_large(ret, size, flags);
1232+
kmemleak_alloc(ret, size, 1, flags);
12331233
return ret;
12341234
}
12351235
EXPORT_SYMBOL(kmalloc_order);

mm/slub.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,8 +1374,9 @@ static inline void dec_slabs_node(struct kmem_cache *s, int node,
13741374
*/
13751375
static inline void *kmalloc_large_node_hook(void *ptr, size_t size, gfp_t flags)
13761376
{
1377+
ptr = kasan_kmalloc_large(ptr, size, flags);
13771378
kmemleak_alloc(ptr, size, 1, flags);
1378-
return kasan_kmalloc_large(ptr, size, flags);
1379+
return ptr;
13791380
}
13801381

13811382
static __always_inline void kfree_hook(void *x)

0 commit comments

Comments
 (0)