Commit 0d4ca4c9 authored by Marco Elver's avatar Marco Elver Committed by Linus Torvalds
Browse files

mm/kasan: add object validation in ksize()

ksize() has been unconditionally unpoisoning the whole shadow memory
region associated with an allocation.  This can lead to various undetected
bugs, for example, double-kzfree().

Specifically, kzfree() uses ksize() to determine the actual allocation
size, and subsequently zeroes the memory.  Since ksize() used to just
unpoison the whole shadow memory region, no invalid free was detected.

This patch addresses this as follows:

1. Add a check in ksize(), and only then unpoison the memory region.

2. Preserve kasan_unpoison_slab() semantics by explicitly unpoisoning
   the shadow memory region using the size obtained from __ksize().

1. With SLAB allocator: a) normal boot without warnings; b) verified the
   added double-kzfree() is detected.
2. With SLUB allocator: a) normal boot without warnings; b) verified the
   added double-kzfree() is detected.

[ s/BUG_ON/WARN_ON_ONCE/, per Kees]

Signed-off-by: default avatarMarco Elver <>
Acked-by: default avatarKees Cook <>
Reviewed-by: default avatarAndrey Ryabinin <>
Cc: Dmitry Vyukov <>
Cc: Alexander Potapenko <>
Cc: Andrey Konovalov <>
Cc: Christoph Lameter <>
Cc: Pekka Enberg <>
Cc: David Rientjes <>
Cc: Joonsoo Kim <>
Cc: Mark Rutland <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 10d1f8cb
......@@ -76,8 +76,11 @@ void kasan_free_shadow(const struct vm_struct *vm);
int kasan_add_zero_shadow(void *start, unsigned long size);
void kasan_remove_zero_shadow(void *start, unsigned long size);
size_t ksize(const void *);
static inline void kasan_unpoison_slab(const void *ptr) { ksize(ptr); }
size_t __ksize(const void *);
static inline void kasan_unpoison_slab(const void *ptr)
kasan_unpoison_shadow(ptr, __ksize(ptr));
size_t kasan_metadata_size(struct kmem_cache *cache);
bool kasan_save_enable_multi_shot(void);
......@@ -1613,7 +1613,27 @@ EXPORT_SYMBOL(kzfree);
size_t ksize(const void *objp)
size_t size = __ksize(objp);
size_t size;
if (WARN_ON_ONCE(!objp))
return 0;
* We need to check that the pointed to object is valid, and only then
* unpoison the shadow memory below. We use __kasan_check_read(), to
* generate a more useful report at the time ksize() is called (rather
* than later where behaviour is undefined due to potential
* use-after-free or double-free).
* If the pointed to memory is invalid we return 0, to avoid users of
* ksize() writing to and potentially corrupting the memory region.
* We want to perform the check before __ksize(), to avoid potentially
* crashing in __ksize() due to accessing invalid metadata.
if (unlikely(objp == ZERO_SIZE_PTR) || !__kasan_check_read(objp, 1))
return 0;
size = __ksize(objp);
* We assume that ksize callers could use whole allocated area,
* so we need to unpoison this area.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment