diff --git a/mm/kasan/kasan.c b/mm/kasan/kasan.c index 8e0aebc10eb80..566e3edb7eb49 100644 --- a/mm/kasan/kasan.c +++ b/mm/kasan/kasan.c @@ -28,6 +28,7 @@ #include #include #include +#include #include "kasan.h" @@ -74,6 +75,12 @@ struct kasan_region_s uintptr_t shadow[1]; }; +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +static bool kasan_is_poisoned(FAR const void *addr, size_t size); + /**************************************************************************** * Private Data ****************************************************************************/ @@ -136,6 +143,49 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size, return NULL; } +static void kasan_show_memory(FAR const uint8_t *addr, size_t size, + size_t dumpsize) +{ + FAR const uint8_t *start = (FAR const uint8_t *) + (((uintptr_t)addr) & ~0xf) - dumpsize; + FAR const uint8_t *end = start + 2 * dumpsize; + FAR const uint8_t *p = start; + char buffer[256]; + + _alert("Shadow bytes around the buggy address:\n"); + for (p = start; p < end; p += 16) + { + int ret = sprintf(buffer, " %p: ", p); + int i; + + for (i = 0; i < 16; i++) + { + if (kasan_is_poisoned(p + i, 1)) + { + if (p + i == addr) + { + ret += sprintf(buffer + ret, + "\b[\033[31m%02x\033[0m ", p[i]); + } + else if (p + i == addr + size - 1) + { + ret += sprintf(buffer + ret, "\033[31m%02x\033[0m]", p[i]); + } + else + { + ret += sprintf(buffer + ret, "\033[31m%02x\033[0m ", p[i]); + } + } + else + { + ret += sprintf(buffer + ret, "\033[37m%02x\033[0m ", p[i]); + } + } + + _alert("%s\n", buffer); + } +} + static void kasan_report(FAR const void *addr, size_t size, bool is_write, FAR void *return_address) @@ -148,6 +198,8 @@ static void kasan_report(FAR const void *addr, size_t size, "size is %zu, return address: %p\n", is_write ? "write" : "read", addr, size, return_address); + + kasan_show_memory(addr, size, 80); PANIC(); }