Commit adae01a9 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Stephen Rothwell
Browse files

kasan: rename (un)poison_shadow to (un)poison_range



This is a preparatory commit for the upcoming addition of a new hardware
tag-based (MTE-based) KASAN mode.

The new mode won't be using shadow memory.  Rename external annotation
kasan_unpoison_shadow() to kasan_unpoison_range(), and introduce internal
functions (un)poison_range() (without kasan_ prefix).
Co-developed-by: default avatarMarco Elver <elver@google.com>
Link: https://lkml.kernel.org/r/fccdcaa13dc6b2211bf363d6c6d499279a54fe3a.1606161801.git.andreyknvl@google.com

Signed-off-by: default avatarMarco Elver <elver@google.com>
Signed-off-by: default avatarAndrey Konovalov <andreyknvl@google.com>
Signed-off-by: Vincenzo Frascino's avatarVincenzo Frascino <vincenzo.frascino@arm.com>
Reviewed-by: default avatarAlexander Potapenko <glider@google.com>
Tested-by: Vincenzo Frascino's avatarVincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Branislav Rankov <Branislav.Rankov@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarStephen Rothwell <sfr@canb.auug.org.au>
parent 7b449a04
...@@ -71,7 +71,7 @@ extern void kasan_enable_current(void); ...@@ -71,7 +71,7 @@ extern void kasan_enable_current(void);
/* Disable reporting bugs for current task */ /* Disable reporting bugs for current task */
extern void kasan_disable_current(void); extern void kasan_disable_current(void);
void kasan_unpoison_shadow(const void *address, size_t size); void kasan_unpoison_range(const void *address, size_t size);
void kasan_unpoison_task_stack(struct task_struct *task); void kasan_unpoison_task_stack(struct task_struct *task);
...@@ -108,7 +108,7 @@ struct kasan_cache { ...@@ -108,7 +108,7 @@ struct kasan_cache {
size_t __ksize(const void *); size_t __ksize(const void *);
static inline void kasan_unpoison_slab(const void *ptr) static inline void kasan_unpoison_slab(const void *ptr)
{ {
kasan_unpoison_shadow(ptr, __ksize(ptr)); kasan_unpoison_range(ptr, __ksize(ptr));
} }
size_t kasan_metadata_size(struct kmem_cache *cache); size_t kasan_metadata_size(struct kmem_cache *cache);
...@@ -117,7 +117,7 @@ void kasan_restore_multi_shot(bool enabled); ...@@ -117,7 +117,7 @@ void kasan_restore_multi_shot(bool enabled);
#else /* CONFIG_KASAN */ #else /* CONFIG_KASAN */
static inline void kasan_unpoison_shadow(const void *address, size_t size) {} static inline void kasan_unpoison_range(const void *address, size_t size) {}
static inline void kasan_unpoison_task_stack(struct task_struct *task) {} static inline void kasan_unpoison_task_stack(struct task_struct *task) {}
......
...@@ -225,8 +225,8 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node) ...@@ -225,8 +225,8 @@ static unsigned long *alloc_thread_stack_node(struct task_struct *tsk, int node)
if (!s) if (!s)
continue; continue;
/* Clear the KASAN shadow of the stack. */ /* Mark stack accessible for KASAN. */
kasan_unpoison_shadow(s->addr, THREAD_SIZE); kasan_unpoison_range(s->addr, THREAD_SIZE);
/* Clear stale pointers from reused stack. */ /* Clear stale pointers from reused stack. */
memset(s->addr, 0, THREAD_SIZE); memset(s->addr, 0, THREAD_SIZE);
......
...@@ -109,7 +109,7 @@ void *memcpy(void *dest, const void *src, size_t len) ...@@ -109,7 +109,7 @@ void *memcpy(void *dest, const void *src, size_t len)
* Poisons the shadow memory for 'size' bytes starting from 'addr'. * Poisons the shadow memory for 'size' bytes starting from 'addr'.
* Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE. * Memory addresses should be aligned to KASAN_SHADOW_SCALE_SIZE.
*/ */
void kasan_poison_shadow(const void *address, size_t size, u8 value) void poison_range(const void *address, size_t size, u8 value)
{ {
void *shadow_start, *shadow_end; void *shadow_start, *shadow_end;
...@@ -130,7 +130,7 @@ void kasan_poison_shadow(const void *address, size_t size, u8 value) ...@@ -130,7 +130,7 @@ void kasan_poison_shadow(const void *address, size_t size, u8 value)
__memset(shadow_start, value, shadow_end - shadow_start); __memset(shadow_start, value, shadow_end - shadow_start);
} }
void kasan_unpoison_shadow(const void *address, size_t size) void unpoison_range(const void *address, size_t size)
{ {
u8 tag = get_tag(address); u8 tag = get_tag(address);
...@@ -149,7 +149,7 @@ void kasan_unpoison_shadow(const void *address, size_t size) ...@@ -149,7 +149,7 @@ void kasan_unpoison_shadow(const void *address, size_t size)
if (is_kfence_address(address)) if (is_kfence_address(address))
return; return;
kasan_poison_shadow(address, size, tag); poison_range(address, size, tag);
if (size & KASAN_SHADOW_MASK) { if (size & KASAN_SHADOW_MASK) {
u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size); u8 *shadow = (u8 *)kasan_mem_to_shadow(address + size);
...@@ -161,12 +161,17 @@ void kasan_unpoison_shadow(const void *address, size_t size) ...@@ -161,12 +161,17 @@ void kasan_unpoison_shadow(const void *address, size_t size)
} }
} }
void kasan_unpoison_range(const void *address, size_t size)
{
unpoison_range(address, size);
}
static void __kasan_unpoison_stack(struct task_struct *task, const void *sp) static void __kasan_unpoison_stack(struct task_struct *task, const void *sp)
{ {
void *base = task_stack_page(task); void *base = task_stack_page(task);
size_t size = sp - base; size_t size = sp - base;
kasan_unpoison_shadow(base, size); unpoison_range(base, size);
} }
/* Unpoison the entire stack for a task. */ /* Unpoison the entire stack for a task. */
...@@ -185,7 +190,7 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark) ...@@ -185,7 +190,7 @@ asmlinkage void kasan_unpoison_task_stack_below(const void *watermark)
*/ */
void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1)); void *base = (void *)((unsigned long)watermark & ~(THREAD_SIZE - 1));
kasan_unpoison_shadow(base, watermark - base); unpoison_range(base, watermark - base);
} }
void kasan_alloc_pages(struct page *page, unsigned int order) void kasan_alloc_pages(struct page *page, unsigned int order)
...@@ -199,13 +204,13 @@ void kasan_alloc_pages(struct page *page, unsigned int order) ...@@ -199,13 +204,13 @@ void kasan_alloc_pages(struct page *page, unsigned int order)
tag = random_tag(); tag = random_tag();
for (i = 0; i < (1 << order); i++) for (i = 0; i < (1 << order); i++)
page_kasan_tag_set(page + i, tag); page_kasan_tag_set(page + i, tag);
kasan_unpoison_shadow(page_address(page), PAGE_SIZE << order); unpoison_range(page_address(page), PAGE_SIZE << order);
} }
void kasan_free_pages(struct page *page, unsigned int order) void kasan_free_pages(struct page *page, unsigned int order)
{ {
if (likely(!PageHighMem(page))) if (likely(!PageHighMem(page)))
kasan_poison_shadow(page_address(page), poison_range(page_address(page),
PAGE_SIZE << order, PAGE_SIZE << order,
KASAN_FREE_PAGE); KASAN_FREE_PAGE);
} }
...@@ -297,18 +302,18 @@ void kasan_poison_slab(struct page *page) ...@@ -297,18 +302,18 @@ void kasan_poison_slab(struct page *page)
for (i = 0; i < compound_nr(page); i++) for (i = 0; i < compound_nr(page); i++)
page_kasan_tag_reset(page + i); page_kasan_tag_reset(page + i);
kasan_poison_shadow(page_address(page), page_size(page), poison_range(page_address(page), page_size(page),
KASAN_KMALLOC_REDZONE); KASAN_KMALLOC_REDZONE);
} }
void kasan_unpoison_object_data(struct kmem_cache *cache, void *object) void kasan_unpoison_object_data(struct kmem_cache *cache, void *object)
{ {
kasan_unpoison_shadow(object, cache->object_size); unpoison_range(object, cache->object_size);
} }
void kasan_poison_object_data(struct kmem_cache *cache, void *object) void kasan_poison_object_data(struct kmem_cache *cache, void *object)
{ {
kasan_poison_shadow(object, poison_range(object,
round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE), round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE),
KASAN_KMALLOC_REDZONE); KASAN_KMALLOC_REDZONE);
} }
...@@ -424,7 +429,7 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object, ...@@ -424,7 +429,7 @@ static bool __kasan_slab_free(struct kmem_cache *cache, void *object,
} }
rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE); rounded_up_size = round_up(cache->object_size, KASAN_SHADOW_SCALE_SIZE);
kasan_poison_shadow(object, rounded_up_size, KASAN_KMALLOC_FREE); poison_range(object, rounded_up_size, KASAN_KMALLOC_FREE);
if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) || if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine) ||
unlikely(!(cache->flags & SLAB_KASAN))) unlikely(!(cache->flags & SLAB_KASAN)))
...@@ -467,9 +472,9 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object, ...@@ -467,9 +472,9 @@ static void *__kasan_kmalloc(struct kmem_cache *cache, const void *object,
tag = assign_tag(cache, object, false, keep_tag); tag = assign_tag(cache, object, false, keep_tag);
/* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */ /* Tag is ignored in set_tag without CONFIG_KASAN_SW_TAGS */
kasan_unpoison_shadow(set_tag(object, tag), size); unpoison_range(set_tag(object, tag), size);
kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start, poison_range((void *)redzone_start, redzone_end - redzone_start,
KASAN_KMALLOC_REDZONE); KASAN_KMALLOC_REDZONE);
if (cache->flags & SLAB_KASAN) if (cache->flags & SLAB_KASAN)
kasan_set_track(&get_alloc_info(cache, object)->alloc_track, flags); kasan_set_track(&get_alloc_info(cache, object)->alloc_track, flags);
...@@ -508,9 +513,9 @@ void * __must_check kasan_kmalloc_large(const void *ptr, size_t size, ...@@ -508,9 +513,9 @@ void * __must_check kasan_kmalloc_large(const void *ptr, size_t size,
KASAN_SHADOW_SCALE_SIZE); KASAN_SHADOW_SCALE_SIZE);
redzone_end = (unsigned long)ptr + page_size(page); redzone_end = (unsigned long)ptr + page_size(page);
kasan_unpoison_shadow(ptr, size); unpoison_range(ptr, size);
kasan_poison_shadow((void *)redzone_start, redzone_end - redzone_start, poison_range((void *)redzone_start, redzone_end - redzone_start,
KASAN_PAGE_REDZONE); KASAN_PAGE_REDZONE);
return (void *)ptr; return (void *)ptr;
} }
...@@ -542,7 +547,7 @@ void kasan_poison_kfree(void *ptr, unsigned long ip) ...@@ -542,7 +547,7 @@ void kasan_poison_kfree(void *ptr, unsigned long ip)
kasan_report_invalid_free(ptr, ip); kasan_report_invalid_free(ptr, ip);
return; return;
} }
kasan_poison_shadow(ptr, page_size(page), KASAN_FREE_PAGE); poison_range(ptr, page_size(page), KASAN_FREE_PAGE);
} else { } else {
__kasan_slab_free(page->slab_cache, ptr, ip, false); __kasan_slab_free(page->slab_cache, ptr, ip, false);
} }
...@@ -728,7 +733,7 @@ int kasan_populate_vmalloc(unsigned long addr, unsigned long size) ...@@ -728,7 +733,7 @@ int kasan_populate_vmalloc(unsigned long addr, unsigned long size)
* // vmalloc() allocates memory * // vmalloc() allocates memory
* // let a = area->addr * // let a = area->addr
* // we reach kasan_populate_vmalloc * // we reach kasan_populate_vmalloc
* // and call kasan_unpoison_shadow: * // and call unpoison_range:
* STORE shadow(a), unpoison_val * STORE shadow(a), unpoison_val
* ... * ...
* STORE shadow(a+99), unpoison_val x = LOAD p * STORE shadow(a+99), unpoison_val x = LOAD p
...@@ -763,7 +768,7 @@ void kasan_poison_vmalloc(const void *start, unsigned long size) ...@@ -763,7 +768,7 @@ void kasan_poison_vmalloc(const void *start, unsigned long size)
return; return;
size = round_up(size, KASAN_SHADOW_SCALE_SIZE); size = round_up(size, KASAN_SHADOW_SCALE_SIZE);
kasan_poison_shadow(start, size, KASAN_VMALLOC_INVALID); poison_range(start, size, KASAN_VMALLOC_INVALID);
} }
void kasan_unpoison_vmalloc(const void *start, unsigned long size) void kasan_unpoison_vmalloc(const void *start, unsigned long size)
...@@ -771,7 +776,7 @@ void kasan_unpoison_vmalloc(const void *start, unsigned long size) ...@@ -771,7 +776,7 @@ void kasan_unpoison_vmalloc(const void *start, unsigned long size)
if (!is_vmalloc_or_module_addr(start)) if (!is_vmalloc_or_module_addr(start))
return; return;
kasan_unpoison_shadow(start, size); unpoison_range(start, size);
} }
static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr, static int kasan_depopulate_vmalloc_pte(pte_t *ptep, unsigned long addr,
......
...@@ -203,11 +203,11 @@ static void register_global(struct kasan_global *global) ...@@ -203,11 +203,11 @@ static void register_global(struct kasan_global *global)
{ {
size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE); size_t aligned_size = round_up(global->size, KASAN_SHADOW_SCALE_SIZE);
kasan_unpoison_shadow(global->beg, global->size); unpoison_range(global->beg, global->size);
kasan_poison_shadow(global->beg + aligned_size, poison_range(global->beg + aligned_size,
global->size_with_redzone - aligned_size, global->size_with_redzone - aligned_size,
KASAN_GLOBAL_REDZONE); KASAN_GLOBAL_REDZONE);
} }
void __asan_register_globals(struct kasan_global *globals, size_t size) void __asan_register_globals(struct kasan_global *globals, size_t size)
...@@ -286,13 +286,12 @@ void __asan_alloca_poison(unsigned long addr, size_t size) ...@@ -286,13 +286,12 @@ void __asan_alloca_poison(unsigned long addr, size_t size)
WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE)); WARN_ON(!IS_ALIGNED(addr, KASAN_ALLOCA_REDZONE_SIZE));
kasan_unpoison_shadow((const void *)(addr + rounded_down_size), unpoison_range((const void *)(addr + rounded_down_size),
size - rounded_down_size); size - rounded_down_size);
kasan_poison_shadow(left_redzone, KASAN_ALLOCA_REDZONE_SIZE, poison_range(left_redzone, KASAN_ALLOCA_REDZONE_SIZE,
KASAN_ALLOCA_LEFT); KASAN_ALLOCA_LEFT);
kasan_poison_shadow(right_redzone, poison_range(right_redzone, padding_size + KASAN_ALLOCA_REDZONE_SIZE,
padding_size + KASAN_ALLOCA_REDZONE_SIZE, KASAN_ALLOCA_RIGHT);
KASAN_ALLOCA_RIGHT);
} }
EXPORT_SYMBOL(__asan_alloca_poison); EXPORT_SYMBOL(__asan_alloca_poison);
...@@ -302,7 +301,7 @@ void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom) ...@@ -302,7 +301,7 @@ void __asan_allocas_unpoison(const void *stack_top, const void *stack_bottom)
if (unlikely(!stack_top || stack_top > stack_bottom)) if (unlikely(!stack_top || stack_top > stack_bottom))
return; return;
kasan_unpoison_shadow(stack_top, stack_bottom - stack_top); unpoison_range(stack_top, stack_bottom - stack_top);
} }
EXPORT_SYMBOL(__asan_allocas_unpoison); EXPORT_SYMBOL(__asan_allocas_unpoison);
......
...@@ -150,7 +150,8 @@ static inline bool addr_has_shadow(const void *addr) ...@@ -150,7 +150,8 @@ static inline bool addr_has_shadow(const void *addr)
return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START)); return (addr >= kasan_shadow_to_mem((void *)KASAN_SHADOW_START));
} }
void kasan_poison_shadow(const void *address, size_t size, u8 value); void poison_range(const void *address, size_t size, u8 value);
void unpoison_range(const void *address, size_t size);
/** /**
* check_memory_region - Check memory region, and report if invalid access. * check_memory_region - Check memory region, and report if invalid access.
......
...@@ -153,7 +153,7 @@ EXPORT_SYMBOL(__hwasan_storeN_noabort); ...@@ -153,7 +153,7 @@ EXPORT_SYMBOL(__hwasan_storeN_noabort);
void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size) void __hwasan_tag_memory(unsigned long addr, u8 tag, unsigned long size)
{ {
kasan_poison_shadow((void *)addr, size, tag); poison_range((void *)addr, size, tag);
} }
EXPORT_SYMBOL(__hwasan_tag_memory); EXPORT_SYMBOL(__hwasan_tag_memory);
......
...@@ -1179,7 +1179,7 @@ size_t ksize(const void *objp) ...@@ -1179,7 +1179,7 @@ size_t ksize(const void *objp)
* We assume that ksize callers could use whole allocated area, * We assume that ksize callers could use whole allocated area,
* so we need to unpoison this area. * so we need to unpoison this area.
*/ */
kasan_unpoison_shadow(objp, size); kasan_unpoison_range(objp, size);
return size; return size;
} }
EXPORT_SYMBOL(ksize); EXPORT_SYMBOL(ksize);
......
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