In computing, shadow memory is a technique used to track and store information on computer memory used by a program during its execution. Shadow memory consists of shadow bytes that map to individual bits or one or more bytes in main memory. These shadow bytes are typically invisible to the original program and are used to record information about the original piece of data. --维基百科
================================================================= ==3905==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000060 at pc 0x7f09040c62c3 bp 0x7ffcf197b180 sp 0x7ffcf197a928 WRITE of size 8 at 0x603000000060 thread T0 #0 0x7f09040c62c2 in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 #1 0x5605f809b241 in main (/home/stre/Desktop/test1+0x1241) #2 0x7f0903e91fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #3 0x7f0903e9207c in __libc_start_main_impl ../csu/libc-start.c:409 #4 0x5605f809b144 in _start (/home/stre/Desktop/test1+0x1144)
0x603000000060 is located 0 bytes to the right of 32-byte region [0x603000000040,0x603000000060) allocated by thread T0 here: #0 0x7f0904140867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #1 0x5605f809b21e in main (/home/stre/Desktop/test1+0x121e) #2 0x7f0903e91fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
SUMMARY: AddressSanitizer: heap-buffer-overflow ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 in __interceptor_memcpy Shadow bytes around the buggy address: 0x0c067fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c067fff8000: fa fa 00 00 00 fa fa fa 00 00 00 00[fa]fa fa fa 0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==3905==ABORTING
其中:
1 2
==3905==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x603000000060at pc 0x7f09040c62c3bp 0x7ffcf197b180sp0x7ffcf197a928 WRITE of size 8at0x603000000060 thread T0
这两行内容告诉用户,这是一个堆越界的 bug ,发生于地址 0x603000000060 处
接下来:
1
0x603000000060 is located 0bytestotherightof32-byte region [0x603000000040,0x603000000060)
0x0c067fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c067fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c067fff8000: fa fa 00 00 00 fa fa fa 00 00 00 00[fa]fa fa fa 0x0c067fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c067fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
即,该影子地址内存储的值为fa,即不可访问,但源程序进行了访问,故报错。
修改源代码,若仅前3个字节(即共32+3个字节是申请的内存空间)可访问,则此处报错是如何
1 2 3
0x0c087fff7ff0:00000000000000000000000000000000 =>0x0c087fff8000: fa fa 00000000[03]fa fa fa fa fa fa fa fa fa 0x0c087fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Direct leak of 100byte(s) in 1 object(s) allocated from: #00x7fa9f49d0867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #10x55df42973225 in main (/home/stre/Desktop/leak+0x1225) #20x7fa9f4721fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
SUMMARY: AddressSanitizer: 100byte(s) leaked in 1 allocation(s).
================================================================= ==3573==ERROR: AddressSanitizer: heap-use-after-free on address 0x60b0000000f0 at pc 0x7fa6785892c3 bp 0x7fff93446ed0 sp 0x7fff93446678 WRITE of size13 at 0x60b0000000f0 thread T0 #00x7fa6785892c2 in __interceptor_memcpy ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 #10x5634c5025270 in main (/home/stre/Desktop/uaf+0x1270) #20x7fa678354fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #30x7fa67835507c in __libc_start_main_impl ../csu/libc-start.c:409 #40x5634c5025164 in _start (/home/stre/Desktop/uaf+0x1164)
0x60b0000000f0 is located 0 bytes inside of 100-byte region [0x60b0000000f0,0x60b000000154) freed by thread T0 here: #00x7fa678603517 in __interceptor_free ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:127 #10x5634c5025255 in main (/home/stre/Desktop/uaf+0x1255) #20x7fa678354fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
previously allocated by thread T0 here: #00x7fa678603867 in __interceptor_malloc ../../../../src/libsanitizer/asan/asan_malloc_linux.cpp:145 #10x5634c5025245 in main (/home/stre/Desktop/uaf+0x1245) #20x7fa678354fcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
SUMMARY: AddressSanitizer: heap-use-after-free ../../../../src/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:827 in __interceptor_memcpy Shadow bytes around the buggy address: 0x0c167fff7fc0: 00000000000000000000000000000000 0x0c167fff7fd0: 00000000000000000000000000000000 0x0c167fff7fe0: 00000000000000000000000000000000 0x0c167fff7ff0: 00000000000000000000000000000000 0x0c167fff8000: fa fa fa fa fa fa fa fa fd fd fd fd fd fd fd fd =>0x0c167fff8010: fd fd fd fd fd fa fa fa fa fa fa fa fa fa[fd]fd 0x0c167fff8020: fd fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa 0x0c167fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c167fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c167fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c167fff8060: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01020304050607 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==3573==ABORTING
asan 明确地指出了发生错误及地址是AddressSanitizer: heap-use-after-free on address 0x60b0000000f0
这个地址是在被释放的区域内0x60b0000000f0 is located 0 bytes inside of 100-byte region [0x60b0000000f0,0x60b000000154)freed by thread T0 here
================================================================= ==2180936==ERROR: AddressSanitizer: alloc-dealloc-mismatch (operatornew [] vs operatordelete) on 0x60b0000000f0// 1 #00x7fa9f877cc65 in operatordelete(void*, unsignedlong) ../../../../src/libsanitizer/ASAN/ASAN_new_delete.cc:177 #10x55d09d3fe33f in main /home/chenbing/Code/test/bad_delete.cpp:10 #20x7fa9f8152082 in __libc_start_main ../csu/libc-start.c:308 #30x55d09d3fe20d in _start (/home/chenbing/Code/test/bad_delete+0x120d)
0x60b0000000f0 is located 0 bytes inside of 100-byte region [0x60b0000000f0,0x60b000000154) // 2 allocated by thread T0 here: #00x7fa9f877b787 in operatornew[](unsignedlong) ../../../../src/libsanitizer/ASAN/ASAN_new_delete.cc:107 #10x55d09d3fe2e5 in main /home/chenbing/Code/test/bad_delete.cpp:6 #20x7fa9f8152082 in __libc_start_main ../csu/libc-start.c:308
SUMMARY: AddressSanitizer: alloc-dealloc-mismatch ../../../../src/libsanitizer/ASAN/ASAN_new_delete.cc:177 in operatordelete(void*, unsignedlong) ==2180936==HINT: if you don't care about these errors you may set ASAN_OPTIONS=alloc_dealloc_mismatch=0 ==2180936==ABORTING
~/Code/test$ g++ sbo.c -o sbo -fsanitize=address -g chenbing@GreatDB-CB:~/Code/test$ ./sbo ================================================================= ==2196928==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc33777f24 at pc 0x562dccb592b6 bp 0x7ffc33777d40 sp 0x7ffc33777d30 1) WRITE of size 4 at 0x7ffc33777f24 thread T0 #0 0x562dccb592b5 in main /home/chenbing/Code/test/sbo.c:6 #1 0x7f45bf52d082 in __libc_start_main ../csu/libc-start.c:308 #2 0x562dccb5910d in _start (/home/chenbing/Code/test/sbo+0x110d)
Address 0x7ffc33777f24 is located in stack of thread T0 at offset 452 in frame 2) #0 0x562dccb591d8 in main /home/chenbing/Code/test/sbo.c:4
This frame has 1 object(s): 3) [48, 448) 'stack_array' (line 5) <== Memory access at offset 452 overflows this variable HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork 4) (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-buffer-overflow /home/chenbing/Code/test/sbo.c:6 in main Shadow bytes around the buggy address: 5) 0x1000066e6f90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e6fa0: 00 00 00 00 00 00 00 00 00 00 00 00 f1 f1 f1 f1 0x1000066e6fb0: f1 f1 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e6fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e6fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x1000066e6fe0: 00 00 00 00[f3]f3 f3 f3 f3 f3 f3 f3 00 00 00 00 0x1000066e6ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e7000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e7010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e7020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x1000066e7030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASAN internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==2196928==ABORTING
================================================================= ==3701==ERROR: AddressSanitizer: global-buffer-overflow on address 0x55633ad321b4 at pc 0x55633ad2f216 bp 0x7ffff739eca0 sp 0x7ffff739ec90 WRITE of size 4 at 0x55633ad321b4 thread T0 #0 0x55633ad2f215 in main (/home/stre/Desktop/gl+0x1215) #1 0x7f5a1ea1dfcf in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58 #2 0x7f5a1ea1e07c in __libc_start_main_impl ../csu/libc-start.c:409 #3 0x55633ad2f104 in _start (/home/stre/Desktop/gl+0x1104)
0x55633ad321b4 is located 4 bytes to the right of global variable 'global_array' defined in 'gl.c:4:5' (0x55633ad32020) of size 400 SUMMARY: AddressSanitizer: global-buffer-overflow (/home/stre/Desktop/gl+0x1215) in main Shadow bytes around the buggy address: 0x0aace759e3e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e3f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e400: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e410: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e420: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0aace759e430: 00 00 00 00 00 00[f9]f9 f9 f9 f9 f9 00 00 00 00 0x0aace759e440: f9 f9 f9 f9 f9 f9 f9 f9 00 00 00 00 00 00 00 00 0x0aace759e450: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e460: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e470: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0aace759e480: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Shadow byte legend (one shadow byte represents 8 application bytes): Addressable: 00 Partially addressable: 01 02 03 04 05 06 07 Heap left redzone: fa Freed heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 Container overflow: fc Array cookie: ac Intra object redzone: bb ASan internal: fe Left alloca redzone: ca Right alloca redzone: cb Shadow gap: cc ==3701==ABORTING