Skip to content

Commit a9afed9

Browse files
Gary-Hobsonxiaoxiang781216
authored andcommitted
mm/kasan: print memory around poisoned address
Print shadow memory following asan format. ==3118004==ERROR: AddressSanitizer: global-buffer-overflow on address 0x5618ac32a100 at pc 0x5618ac32727f bp 0x7ffe5f3e66f0 sp 0x7ffe5f3e66e0 WRITE of size 4 at 0x5618ac32a100 thread T0 #0 0x5618ac32727e in main /home/baerg/vela/x4b/asan_test.c:12 #1 0x7f221ce29d8f in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #2 0x7f221ce29e3f in __libc_start_main_impl ../csu/libc-start.c:392 apache#3 0x5618ac327144 in _start (/home/baerg/vela/x4b/asan_test+0x1144) 0x5618ac32a100 is located 0 bytes to the right of global variable 'buffer' defined in 'asan_test.c:5:6' (0x5618ac32a0e0) of size 32 SUMMARY: AddressSanitizer: global-buffer-overflow /home/baerg/vela/x4b/asan_test.c:12 in main Shadow bytes around the buggy address: 0x0ac39585d3d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d3e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d3f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d400: 00 00 00 00 00 00 00 00 f9 f9 f9 f9 f9 f9 f9 f9 0x0ac39585d410: f9 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00 =>0x0ac39585d420:[f9]f9 f9 f9 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d430: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d440: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0ac39585d470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Signed-off-by: yinshengkai <yinshengkai@xiaomi.com>
1 parent 5a39e83 commit a9afed9

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

mm/kasan/kasan.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <debug.h>
2929
#include <stdbool.h>
3030
#include <stdint.h>
31+
#include <stdio.h>
3132

3233
#include "kasan.h"
3334

@@ -74,6 +75,12 @@ struct kasan_region_s
7475
uintptr_t shadow[1];
7576
};
7677

78+
/****************************************************************************
79+
* Private Function Prototypes
80+
****************************************************************************/
81+
82+
static bool kasan_is_poisoned(FAR const void *addr, size_t size);
83+
7784
/****************************************************************************
7885
* Private Data
7986
****************************************************************************/
@@ -136,6 +143,49 @@ static FAR uintptr_t *kasan_mem_to_shadow(FAR const void *ptr, size_t size,
136143
return NULL;
137144
}
138145

146+
static void kasan_show_memory(FAR const uint8_t *addr, size_t size,
147+
size_t dumpsize)
148+
{
149+
FAR const uint8_t *start = (FAR const uint8_t *)
150+
(((uintptr_t)addr) & ~0xf) - dumpsize;
151+
FAR const uint8_t *end = start + 2 * dumpsize;
152+
FAR const uint8_t *p = start;
153+
char buffer[256];
154+
155+
_alert("Shadow bytes around the buggy address:\n");
156+
for (p = start; p < end; p += 16)
157+
{
158+
int ret = sprintf(buffer, " %p: ", p);
159+
int i;
160+
161+
for (i = 0; i < 16; i++)
162+
{
163+
if (kasan_is_poisoned(p + i, 1))
164+
{
165+
if (p + i == addr)
166+
{
167+
ret += sprintf(buffer + ret,
168+
"\b[\033[31m%02x\033[0m ", p[i]);
169+
}
170+
else if (p + i == addr + size - 1)
171+
{
172+
ret += sprintf(buffer + ret, "\033[31m%02x\033[0m]", p[i]);
173+
}
174+
else
175+
{
176+
ret += sprintf(buffer + ret, "\033[31m%02x\033[0m ", p[i]);
177+
}
178+
}
179+
else
180+
{
181+
ret += sprintf(buffer + ret, "\033[37m%02x\033[0m ", p[i]);
182+
}
183+
}
184+
185+
_alert("%s\n", buffer);
186+
}
187+
}
188+
139189
static void kasan_report(FAR const void *addr, size_t size,
140190
bool is_write,
141191
FAR void *return_address)
@@ -148,6 +198,8 @@ static void kasan_report(FAR const void *addr, size_t size,
148198
"size is %zu, return address: %p\n",
149199
is_write ? "write" : "read",
150200
addr, size, return_address);
201+
202+
kasan_show_memory(addr, size, 80);
151203
PANIC();
152204
}
153205

0 commit comments

Comments
 (0)