mm/kasan: get rid of ->state in struct kasan_alloc_meta
The state of object currently tracked in two places - shadow memory, and the ->state field in struct kasan_alloc_meta. We can get rid of the latter. The will save us a little bit of memory. Also, this allow us to move free stack into struct kasan_alloc_meta, without increasing memory consumption. So now we should always know when the last time the object was freed. This may be useful for long delayed use-after-free bugs. As a side effect this fixes following UBSAN warning: UBSAN: Undefined behaviour in mm/kasan/quarantine.c:102:13 member access within misaligned address ffff88000d1efebc for type 'struct qlist_node' which requires 8 byte alignment Link: http://lkml.kernel.org/r/1470062715-14077-5-git-send-email-aryabinin@virtuozzo.com Reported-by: kernel test robot <xiaolong.ye@intel.com> Signed-off-by: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Cc: Christoph Lameter <cl@linux.com> Cc: Pekka Enberg <penberg@kernel.org> Cc: David Rientjes <rientjes@google.com> Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
This commit is contained in:

committed by
Linus Torvalds

szülő
47b5c2a0f0
commit
b3cbd9bf77
@@ -442,11 +442,6 @@ void kasan_poison_object_data(struct kmem_cache *cache, void *object)
|
||||
kasan_poison_shadow(object,
|
||||
round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
|
||||
KASAN_KMALLOC_REDZONE);
|
||||
if (cache->flags & SLAB_KASAN) {
|
||||
struct kasan_alloc_meta *alloc_info =
|
||||
get_alloc_info(cache, object);
|
||||
alloc_info->state = KASAN_STATE_INIT;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int in_irqentry_text(unsigned long ptr)
|
||||
@@ -510,6 +505,17 @@ struct kasan_free_meta *get_free_info(struct kmem_cache *cache,
|
||||
return (void *)object + cache->kasan_info.free_meta_offset;
|
||||
}
|
||||
|
||||
void kasan_init_slab_obj(struct kmem_cache *cache, const void *object)
|
||||
{
|
||||
struct kasan_alloc_meta *alloc_info;
|
||||
|
||||
if (!(cache->flags & SLAB_KASAN))
|
||||
return;
|
||||
|
||||
alloc_info = get_alloc_info(cache, object);
|
||||
__memset(alloc_info, 0, sizeof(*alloc_info));
|
||||
}
|
||||
|
||||
void kasan_slab_alloc(struct kmem_cache *cache, void *object, gfp_t flags)
|
||||
{
|
||||
kasan_kmalloc(cache, object, cache->object_size, flags);
|
||||
@@ -529,34 +535,27 @@ static void kasan_poison_slab_free(struct kmem_cache *cache, void *object)
|
||||
|
||||
bool kasan_slab_free(struct kmem_cache *cache, void *object)
|
||||
{
|
||||
s8 shadow_byte;
|
||||
|
||||
/* RCU slabs could be legally used after free within the RCU period */
|
||||
if (unlikely(cache->flags & SLAB_DESTROY_BY_RCU))
|
||||
return false;
|
||||
|
||||
if (likely(cache->flags & SLAB_KASAN)) {
|
||||
struct kasan_alloc_meta *alloc_info;
|
||||
struct kasan_free_meta *free_info;
|
||||
|
||||
alloc_info = get_alloc_info(cache, object);
|
||||
free_info = get_free_info(cache, object);
|
||||
|
||||
switch (alloc_info->state) {
|
||||
case KASAN_STATE_ALLOC:
|
||||
alloc_info->state = KASAN_STATE_QUARANTINE;
|
||||
set_track(&free_info->track, GFP_NOWAIT);
|
||||
kasan_poison_slab_free(cache, object);
|
||||
quarantine_put(free_info, cache);
|
||||
return true;
|
||||
case KASAN_STATE_QUARANTINE:
|
||||
case KASAN_STATE_FREE:
|
||||
pr_err("Double free");
|
||||
dump_stack();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
shadow_byte = READ_ONCE(*(s8 *)kasan_mem_to_shadow(object));
|
||||
if (shadow_byte < 0 || shadow_byte >= KASAN_SHADOW_SCALE_SIZE) {
|
||||
pr_err("Double free");
|
||||
dump_stack();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
kasan_poison_slab_free(cache, object);
|
||||
|
||||
if (unlikely(!(cache->flags & SLAB_KASAN)))
|
||||
return false;
|
||||
|
||||
set_track(&get_alloc_info(cache, object)->free_track, GFP_NOWAIT);
|
||||
quarantine_put(get_free_info(cache, object), cache);
|
||||
return true;
|
||||
}
|
||||
|
||||
void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
|
||||
@@ -579,13 +578,9 @@ void kasan_kmalloc(struct kmem_cache *cache, const void *object, size_t size,
|
||||
kasan_unpoison_shadow(object, size);
|
||||
kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start,
|
||||
KASAN_KMALLOC_REDZONE);
|
||||
if (cache->flags & SLAB_KASAN) {
|
||||
struct kasan_alloc_meta *alloc_info =
|
||||
get_alloc_info(cache, object);
|
||||
|
||||
alloc_info->state = KASAN_STATE_ALLOC;
|
||||
set_track(&alloc_info->track, flags);
|
||||
}
|
||||
if (cache->flags & SLAB_KASAN)
|
||||
set_track(&get_alloc_info(cache, object)->alloc_track, flags);
|
||||
}
|
||||
EXPORT_SYMBOL(kasan_kmalloc);
|
||||
|
||||
|
Reference in New Issue
Block a user