// Check the cases where we access first k bytes of the qword // and these k bytes are unpoisoned. boolSlowPathCheck(shadow_value, address, kAccessSize){ last_accessed_byte = (address & 7) + kAccessSize - 1; return (last_accessed_byte >= shadow_value); } ...
Test AddressSanitizer usage: asan-test [ -bfloprs ]
-b heap buffer overflow -f heap use after free -l heap memory leak -o global buffer overflow -p stack use after scope -r stack use after return -s stack buffer overflow
$ ./asan-test -b ================================================================= ==57360==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x604000000038 at pc 0x55bf46fd64ed bp 0x7ffced908dc0 sp 0x7ffced908db0 READ of size 4 at 0x604000000038 thread T0 #0 0x55bf46fd64ec in heap_buffer_overflow /home/zixi/coding/asan-test.c:34 #1 0x55bf46fd6a3f in main /home/zixi/coding/asan-test.c:88 #2 0x7fd16f6560b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #3 0x55bf46fd628d in _start (/home/zixi/coding/asan-test+0x128d)
0x604000000038 is located 0 bytes to the right of 40-byte region [0x604000000010,0x604000000038) allocated by thread T0 here: #0 0x7fd16f92ebc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8) #1 0x55bf46fd646c in heap_buffer_overflow /home/zixi/coding/asan-test.c:32 #2 0x55bf46fd6a3f in main /home/zixi/coding/asan-test.c:88 #3 0x7fd16f6560b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
SUMMARY: AddressSanitizer: heap-buffer-overflow /home/zixi/coding/asan-test.c:34 in heap_buffer_overflow Shadow bytes around the buggy address: 0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c087fff8000: fa fa 00 00 00 00 00[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 0x0c087fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8050: 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 ... ==57360==ABORTING
Stack OOB测试用例如下所示。ASan报告了局部对象越界读错误。由于局部变量位于堆栈空间中,所以列出了函数stack_buffr_overflow的起始行号asan-test.c:37。与Heap OOB报告不同的是,局部变量的前后红区的影子内存毒化值是不一样的,之前Stack left redzone为0xf1,之后Stack right redzone为0xf3。使用不同的毒化值(都是0x80之后的负值),有利于快速区分不同的错误类型。
$ ./asan-test -s ================================================================= ==57370==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7f1cf5044058 at pc 0x55d8b7e9d601 bp 0x7ffc830c29e0 sp 0x7ffc830c29d0 READ of size 4 at 0x7f1cf5044058 thread T0 #0 0x55d8b7e9d600 in stack_buffer_overflow /home/zixi/coding/asan-test.c:40 #1 0x55d8b7e9daec in main /home/zixi/coding/asan-test.c:108 #2 0x7f1cf87760b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #3 0x55d8b7e9d28d in _start (/home/zixi/coding/asan-test+0x128d)
Address 0x7f1cf5044058 is located in stack of thread T0 at offset 88 in frame #0 0x55d8b7e9d505 in stack_buffer_overflow /home/zixi/coding/asan-test.c:37
This frame has 1 object(s): [48, 88) 's' (line 38) <== Memory access at offset 88 overflows this variable HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-buffer-overflow /home/zixi/coding/asan-test.c:40 in stack_buffer_overflow Shadow bytes around the buggy address: 0x0fe41ea007b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea007c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea007d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea007e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea007f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0fe41ea00800: f1 f1 f1 f1 f1 f1 00 00 00 00 00[f3]f3 f3 f3 f3 0x0fe41ea00810: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea00820: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea00830: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea00840: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe41ea00850: 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 ... ==57370==ABORTING
$ ./asan-test -o ================================================================= ==57367==ERROR: AddressSanitizer: global-buffer-overflow on address 0x564363ea4048 at pc 0x564363ea1383 bp 0x7ffc0d6085d0 sp 0x7ffc0d6085c0 READ of size 4 at 0x564363ea4048 thread T0 #0 0x564363ea1382 in global_buffer_overflow /home/zixi/coding/asan-test.c:16 #1 0x564363ea1a6c in main /home/zixi/coding/asan-test.c:98 #2 0x7f8cb43890b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #3 0x564363ea128d in _start (/home/zixi/coding/asan-test+0x128d)
$ ./asan-test -f ================================================================= ==57363==ERROR: AddressSanitizer: heap-use-after-free on address 0x604000000034 at pc 0x558b4a45444e bp 0x7ffccf4ca790 sp 0x7ffccf4ca780 READ of size 4 at 0x604000000034 thread T0 #0 0x558b4a45444d in heap_use_after_free /home/zixi/coding/asan-test.c:28 #1 0x558b4a454a4e in main /home/zixi/coding/asan-test.c:91 #2 0x7fc7cc98b0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #3 0x558b4a45428d in _start (/home/zixi/coding/asan-test+0x128d)
0x604000000034 is located 36 bytes inside of 40-byte region [0x604000000010,0x604000000038) freed by thread T0 here: #0 0x7fc7ccc637cf in __interceptor_free (/lib/x86_64-linux-gnu/libasan.so.5+0x10d7cf) #1 0x558b4a454412 in heap_use_after_free /home/zixi/coding/asan-test.c:27 #2 0x558b4a454a4e in main /home/zixi/coding/asan-test.c:91 #3 0x7fc7cc98b0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
previously allocated by thread T0 here: #0 0x7fc7ccc63bc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8) #1 0x558b4a4543bd in heap_use_after_free /home/zixi/coding/asan-test.c:25 #2 0x558b4a454a4e in main /home/zixi/coding/asan-test.c:91 #3 0x7fc7cc98b0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
SUMMARY: AddressSanitizer: heap-use-after-free /home/zixi/coding/asan-test.c:28 in heap_use_after_free Shadow bytes around the buggy address: 0x0c087fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0c087fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0c087fff8000: fa fa fd fd fd fd[fd]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 0x0c087fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa 0x0c087fff8050: 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 ... ==57363==ABORTING
Direct leak of 40 byte(s) in 1 object(s) allocated from: #0 0x7f06b85b1bc8 in malloc (/lib/x86_64-linux-gnu/libasan.so.5+0x10dbc8) #1 0x5574a8bcd3a0 in heap_leak /home/zixi/coding/asan-test.c:20 #2 0x5574a8bcda5d in main /home/zixi/coding/asan-test.c:94 #3 0x7f06b82d90b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
SUMMARY: AddressSanitizer: 40 byte(s) leaked in 1 allocation(s).
./asan-test -p ================================================================= ==57368==ERROR: AddressSanitizer: stack-use-after-scope on address 0x7f06f0a9b020 at pc 0x56121a7548d9 bp 0x7ffd1de0d050 sp 0x7ffd1de0d040 WRITE of size 4 at 0x7f06f0a9b020 thread T0 #0 0x56121a7548d8 in stack_use_after_scope /home/zixi/coding/asan-test.c:57 #1 0x56121a754a7b in main /home/zixi/coding/asan-test.c:101 #2 0x7f06f42cd0b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #3 0x56121a75428d in _start (/home/zixi/coding/asan-test+0x128d)
Address 0x7f06f0a9b020 is located in stack of thread T0 at offset 32 in frame #0 0x56121a7547d0 in stack_use_after_scope /home/zixi/coding/asan-test.c:52
This frame has 1 object(s): [32, 36) 'c' (line 54) <== Memory access at offset 32 is inside this variable HINT: this may be a false positive if your program uses some custom stack unwind mechanism, swapcontext or vfork (longjmp and C++ exceptions *are* supported) SUMMARY: AddressSanitizer: stack-use-after-scope /home/zixi/coding/asan-test.c:57 in stack_use_after_scope Shadow bytes around the buggy address: 0x0fe15e14b5b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b5c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b5d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b5e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b5f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x0fe15e14b600: f1 f1 f1 f1[f8]f3 f3 f3 00 00 00 00 00 00 00 00 0x0fe15e14b610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b620: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b630: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b640: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x0fe15e14b650: 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 ... ==57368==ABORTING
$ export ASAN_OPTIONS=detect_stack_use_after_return=1 $ env | grep ASAN ASAN_OPTIONS=detect_stack_use_after_return=1 $ ./asan-test -r ================================================================= ==57369==ERROR: AddressSanitizer: stack-use-after-return on address 0x7f5493e93030 at pc 0x55a356890ac9 bp 0x7ffd22c5cf30 sp 0x7ffd22c5cf20 READ of size 4 at 0x7f5493e93030 thread T0 #0 0x55a356890ac8 in main /home/zixi/coding/asan-test.c:105 #1 0x7f54975c50b2 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x270b2) #2 0x55a35689028d in _start (/home/zixi/coding/asan-test+0x128d)
Address 0x7f5493e93030 is located in stack of thread T0 at offset 48 in frame #0 0x55a356890682 in stack_use_after_return /home/zixi/coding/asan-test.c:45