Commit b5f6e0fc authored by Marco Elver's avatar Marco Elver Committed by Linus Torvalds

mm/kasan: change kasan_check_{read,write} to return boolean

This changes {,__}kasan_check_{read,write} functions to return a boolean
denoting if the access was valid or not.

[sfr@canb.auug.org.au: include types.h for "bool"]
  Link: http://lkml.kernel.org/r/20190705184949.13cdd021@canb.auug.org.au
Link: http://lkml.kernel.org/r/20190626142014.141844-3-elver@google.comSigned-off-by: default avatarMarco Elver <elver@google.com>
Signed-off-by: default avatarStephen Rothwell <sfr@canb.auug.org.au>
Reviewed-by: default avatarAndrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Dmitry Vyukov <dvyukov@google.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Konovalov <andreyknvl@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>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Kees Cook <keescook@chromium.org>
Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
parent 7d8ad890
...@@ -2,19 +2,25 @@ ...@@ -2,19 +2,25 @@
#ifndef _LINUX_KASAN_CHECKS_H #ifndef _LINUX_KASAN_CHECKS_H
#define _LINUX_KASAN_CHECKS_H #define _LINUX_KASAN_CHECKS_H
#include <linux/types.h>
/* /*
* __kasan_check_*: Always available when KASAN is enabled. This may be used * __kasan_check_*: Always available when KASAN is enabled. This may be used
* even in compilation units that selectively disable KASAN, but must use KASAN * even in compilation units that selectively disable KASAN, but must use KASAN
* to validate access to an address. Never use these in header files! * to validate access to an address. Never use these in header files!
*/ */
#ifdef CONFIG_KASAN #ifdef CONFIG_KASAN
void __kasan_check_read(const volatile void *p, unsigned int size); bool __kasan_check_read(const volatile void *p, unsigned int size);
void __kasan_check_write(const volatile void *p, unsigned int size); bool __kasan_check_write(const volatile void *p, unsigned int size);
#else #else
static inline void __kasan_check_read(const volatile void *p, unsigned int size) static inline bool __kasan_check_read(const volatile void *p, unsigned int size)
{ } {
static inline void __kasan_check_write(const volatile void *p, unsigned int size) return true;
{ } }
static inline bool __kasan_check_write(const volatile void *p, unsigned int size)
{
return true;
}
#endif #endif
/* /*
...@@ -25,10 +31,14 @@ static inline void __kasan_check_write(const volatile void *p, unsigned int size ...@@ -25,10 +31,14 @@ static inline void __kasan_check_write(const volatile void *p, unsigned int size
#define kasan_check_read __kasan_check_read #define kasan_check_read __kasan_check_read
#define kasan_check_write __kasan_check_write #define kasan_check_write __kasan_check_write
#else #else
static inline void kasan_check_read(const volatile void *p, unsigned int size) static inline bool kasan_check_read(const volatile void *p, unsigned int size)
{ } {
static inline void kasan_check_write(const volatile void *p, unsigned int size) return true;
{ } }
static inline bool kasan_check_write(const volatile void *p, unsigned int size)
{
return true;
}
#endif #endif
#endif #endif
...@@ -87,15 +87,15 @@ void kasan_disable_current(void) ...@@ -87,15 +87,15 @@ void kasan_disable_current(void)
current->kasan_depth--; current->kasan_depth--;
} }
void __kasan_check_read(const volatile void *p, unsigned int size) bool __kasan_check_read(const volatile void *p, unsigned int size)
{ {
check_memory_region((unsigned long)p, size, false, _RET_IP_); return check_memory_region((unsigned long)p, size, false, _RET_IP_);
} }
EXPORT_SYMBOL(__kasan_check_read); EXPORT_SYMBOL(__kasan_check_read);
void __kasan_check_write(const volatile void *p, unsigned int size) bool __kasan_check_write(const volatile void *p, unsigned int size)
{ {
check_memory_region((unsigned long)p, size, true, _RET_IP_); return check_memory_region((unsigned long)p, size, true, _RET_IP_);
} }
EXPORT_SYMBOL(__kasan_check_write); EXPORT_SYMBOL(__kasan_check_write);
......
...@@ -166,29 +166,30 @@ static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size) ...@@ -166,29 +166,30 @@ static __always_inline bool memory_is_poisoned(unsigned long addr, size_t size)
return memory_is_poisoned_n(addr, size); return memory_is_poisoned_n(addr, size);
} }
static __always_inline void check_memory_region_inline(unsigned long addr, static __always_inline bool check_memory_region_inline(unsigned long addr,
size_t size, bool write, size_t size, bool write,
unsigned long ret_ip) unsigned long ret_ip)
{ {
if (unlikely(size == 0)) if (unlikely(size == 0))
return; return true;
if (unlikely((void *)addr < if (unlikely((void *)addr <
kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
kasan_report(addr, size, write, ret_ip); kasan_report(addr, size, write, ret_ip);
return; return false;
} }
if (likely(!memory_is_poisoned(addr, size))) if (likely(!memory_is_poisoned(addr, size)))
return; return true;
kasan_report(addr, size, write, ret_ip); kasan_report(addr, size, write, ret_ip);
return false;
} }
void check_memory_region(unsigned long addr, size_t size, bool write, bool check_memory_region(unsigned long addr, size_t size, bool write,
unsigned long ret_ip) unsigned long ret_ip)
{ {
check_memory_region_inline(addr, size, write, ret_ip); return check_memory_region_inline(addr, size, write, ret_ip);
} }
void kasan_cache_shrink(struct kmem_cache *cache) void kasan_cache_shrink(struct kmem_cache *cache)
......
...@@ -128,7 +128,15 @@ static inline bool addr_has_shadow(const void *addr) ...@@ -128,7 +128,15 @@ static inline bool addr_has_shadow(const void *addr)
void kasan_poison_shadow(const void *address, size_t size, u8 value); void kasan_poison_shadow(const void *address, size_t size, u8 value);
void check_memory_region(unsigned long addr, size_t size, bool write, /**
* check_memory_region - Check memory region, and report if invalid access.
* @addr: the accessed address
* @size: the accessed size
* @write: true if access is a write access
* @ret_ip: return address
* @return: true if access was valid, false if invalid
*/
bool check_memory_region(unsigned long addr, size_t size, bool write,
unsigned long ret_ip); unsigned long ret_ip);
void *find_first_bad_addr(void *addr, size_t size); void *find_first_bad_addr(void *addr, size_t size);
......
...@@ -76,7 +76,7 @@ void *kasan_reset_tag(const void *addr) ...@@ -76,7 +76,7 @@ void *kasan_reset_tag(const void *addr)
return reset_tag(addr); return reset_tag(addr);
} }
void check_memory_region(unsigned long addr, size_t size, bool write, bool check_memory_region(unsigned long addr, size_t size, bool write,
unsigned long ret_ip) unsigned long ret_ip)
{ {
u8 tag; u8 tag;
...@@ -84,7 +84,7 @@ void check_memory_region(unsigned long addr, size_t size, bool write, ...@@ -84,7 +84,7 @@ void check_memory_region(unsigned long addr, size_t size, bool write,
void *untagged_addr; void *untagged_addr;
if (unlikely(size == 0)) if (unlikely(size == 0))
return; return true;
tag = get_tag((const void *)addr); tag = get_tag((const void *)addr);
...@@ -106,22 +106,24 @@ void check_memory_region(unsigned long addr, size_t size, bool write, ...@@ -106,22 +106,24 @@ void check_memory_region(unsigned long addr, size_t size, bool write,
* set to KASAN_TAG_KERNEL (0xFF)). * set to KASAN_TAG_KERNEL (0xFF)).
*/ */
if (tag == KASAN_TAG_KERNEL) if (tag == KASAN_TAG_KERNEL)
return; return true;
untagged_addr = reset_tag((const void *)addr); untagged_addr = reset_tag((const void *)addr);
if (unlikely(untagged_addr < if (unlikely(untagged_addr <
kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) { kasan_shadow_to_mem((void *)KASAN_SHADOW_START))) {
kasan_report(addr, size, write, ret_ip); kasan_report(addr, size, write, ret_ip);
return; return false;
} }
shadow_first = kasan_mem_to_shadow(untagged_addr); shadow_first = kasan_mem_to_shadow(untagged_addr);
shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1); shadow_last = kasan_mem_to_shadow(untagged_addr + size - 1);
for (shadow = shadow_first; shadow <= shadow_last; shadow++) { for (shadow = shadow_first; shadow <= shadow_last; shadow++) {
if (*shadow != tag) { if (*shadow != tag) {
kasan_report(addr, size, write, ret_ip); kasan_report(addr, size, write, ret_ip);
return; return false;
} }
} }
return true;
} }
#define DEFINE_HWASAN_LOAD_STORE(size) \ #define DEFINE_HWASAN_LOAD_STORE(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