Commit a5989d4e authored by Juntong Deng's avatar Juntong Deng Committed by Andrew Morton

kasan: improve free meta storage in Generic KASAN

Currently free meta can only be stored in object if the object is not
smaller than free meta.

After the improvement, when the object is smaller than free meta and SLUB
DEBUG is not enabled, it is possible to store part of the free meta in the
object, reducing the increased size of the red zone.

Example:

free meta size: 16 bytes
alloc meta size: 16 bytes
object size: 8 bytes
optimal redzone size (object_size <= 64): 16 bytes

Before improvement:
actual redzone size = alloc meta size + free meta size = 32 bytes

After improvement:
actual redzone size = alloc meta size + (free meta size - object size)
                    = 24 bytes

[juntong.deng@outlook.com: make kasan_metadata_size() adapt to the improved free meta storage]
  Link: https://lkml.kernel.org/r/VI1P193MB0752675D6E0A2D16CE656F8299BAA@VI1P193MB0752.EURP193.PROD.OUTLOOK.COM
Link: https://lkml.kernel.org/r/VI1P193MB0752DE2CCD9046B5FED0AA8E99B5A@VI1P193MB0752.EURP193.PROD.OUTLOOK.COMSigned-off-by: default avatarJuntong Deng <juntong.deng@outlook.com>
Suggested-by: default avatarDmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@gmail.com>
Cc: Andrey Ryabinin <ryabinin.a.a@gmail.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Vincenzo Frascino <vincenzo.frascino@arm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
parent f542b8e5
...@@ -362,6 +362,8 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, ...@@ -362,6 +362,8 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
{ {
unsigned int ok_size; unsigned int ok_size;
unsigned int optimal_size; unsigned int optimal_size;
unsigned int rem_free_meta_size;
unsigned int orig_alloc_meta_offset;
if (!kasan_requires_meta()) if (!kasan_requires_meta())
return; return;
...@@ -395,6 +397,9 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, ...@@ -395,6 +397,9 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
/* Continue, since free meta might still fit. */ /* Continue, since free meta might still fit. */
} }
ok_size = *size;
orig_alloc_meta_offset = cache->kasan_info.alloc_meta_offset;
/* /*
* Add free meta into redzone when it's not possible to store * Add free meta into redzone when it's not possible to store
* it in the object. This is the case when: * it in the object. This is the case when:
...@@ -402,22 +407,36 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size, ...@@ -402,22 +407,36 @@ void kasan_cache_create(struct kmem_cache *cache, unsigned int *size,
* be touched after it was freed, or * be touched after it was freed, or
* 2. Object has a constructor, which means it's expected to * 2. Object has a constructor, which means it's expected to
* retain its content until the next allocation, or * retain its content until the next allocation, or
* 3. Object is too small. * 3. Object is too small and SLUB DEBUG is enabled. Avoid
* free meta that exceeds the object size corrupts the
* SLUB DEBUG metadata.
* Otherwise cache->kasan_info.free_meta_offset = 0 is implied. * Otherwise cache->kasan_info.free_meta_offset = 0 is implied.
* If the object is smaller than the free meta and SLUB DEBUG
* is not enabled, it is still possible to store part of the
* free meta in the object.
*/ */
if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor || if ((cache->flags & SLAB_TYPESAFE_BY_RCU) || cache->ctor) {
cache->object_size < sizeof(struct kasan_free_meta)) {
ok_size = *size;
cache->kasan_info.free_meta_offset = *size; cache->kasan_info.free_meta_offset = *size;
*size += sizeof(struct kasan_free_meta); *size += sizeof(struct kasan_free_meta);
} else if (cache->object_size < sizeof(struct kasan_free_meta)) {
if (__slub_debug_enabled()) {
cache->kasan_info.free_meta_offset = *size;
*size += sizeof(struct kasan_free_meta);
} else {
rem_free_meta_size = sizeof(struct kasan_free_meta) -
cache->object_size;
*size += rem_free_meta_size;
if (cache->kasan_info.alloc_meta_offset != 0)
cache->kasan_info.alloc_meta_offset += rem_free_meta_size;
}
}
/* If free meta doesn't fit, don't add it. */ /* If free meta doesn't fit, don't add it. */
if (*size > KMALLOC_MAX_SIZE) { if (*size > KMALLOC_MAX_SIZE) {
cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META; cache->kasan_info.free_meta_offset = KASAN_NO_FREE_META;
cache->kasan_info.alloc_meta_offset = orig_alloc_meta_offset;
*size = ok_size; *size = ok_size;
} }
}
/* Calculate size with optimal redzone. */ /* Calculate size with optimal redzone. */
optimal_size = cache->object_size + optimal_redzone(cache->object_size); optimal_size = cache->object_size + optimal_redzone(cache->object_size);
......
Markdown is supported
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