AddressSanitizer

AddressSanitizer (or ASan) is an open source programming tool by Google that detects memory corruption bugs such as buffer overflows or accesses to a dangling pointer (use-after-free). AddressSanitizer is based on compiler instrumentation and directly-mapped shadow memory. AddressSanitizer is currently implemented in Clang (starting from version 3.1[1]) , GCC (starting from version 4.8[2]) and Xcode (starting from version 7.0[3]) . On average, the instrumentation increases processing time by about 73% and memory usage by 240%.[4]

Users

Chromium and Firefox developers are active users of AddressSanitizer;[5][6] the tool has found hundreds of bugs in these web browsers.[7] A number of bugs were found in FFmpeg[8] and FreeType.[9] The Linux kernel has enabled the AddressSanitizer for the x86-64 architecture as of Linux version 4.0.

KernelAddressSanitizer

The KernelAddressSanitizer (KASAN) detects dynamic memory errors in the Linux kernel.[10] Kernel instrumentation requires a special feature in the compiler supplying the -fsanitize=kernel-address command line option, since kernels do not use the same address space as normal programs.[11][12]

Examples

Heap-use-after-free

1 // To compile: g++ -O -g -fsanitize=address heap-use-after-free.cc
2 int main(int argc, char **argv) {
3   int *array = new int[100];
4   delete [] array;
5   return array[argc];  // BOOM
6 }
$ ./a.out
==5587==ERROR: AddressSanitizer: heap-use-after-free on address 0x61400000fe44 at pc 0x47b55f bp 0x7ffc36b28200 sp 0x7ffc36b281f8
READ of size 4 at 0x61400000fe44 thread T0
    #0 0x47b55e in main /home/test/example_UseAfterFree.cc:7
    #1 0x7f15cfe71b14 in __libc_start_main (/lib64/libc.so.6+0x21b14)
    #2 0x47b44c in _start (/root/a.out+0x47b44c)

0x61400000fe44 is located 4 bytes inside of 400-byte region [0x61400000fe40,0x61400000ffd0)
freed by thread T0 here:
    #0 0x465da9 in operator delete[](void*) (/root/a.out+0x465da9)
    #1 0x47b529 in main /home/test/example_UseAfterFree.cc:6

previously allocated by thread T0 here:
    #0 0x465aa9 in operator new[](unsigned long) (/root/a.out+0x465aa9)
    #1 0x47b51e in main /home/test/example_UseAfterFree.cc:5

SUMMARY: AddressSanitizer: heap-use-after-free /home/test/example_UseAfterFree.cc:7 main
Shadow bytes around the buggy address:
  0x0c287fff9f70: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fff9f80: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fff9f90: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fff9fa0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fff9fb0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
=>0x0c287fff9fc0: fa fa fa fa fa fa fa fa[fd]fd fd fd fd fd fd fd
  0x0c287fff9fd0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c287fff9fe0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
  0x0c287fff9ff0: fd fd fd fd fd fd fd fd fd fd fa fa fa fa fa fa
  0x0c287fffa000: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c287fffa010: 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
  Heap right redzone:    fb
  Freed heap region:     fd
  Stack left redzone:    f1
  Stack mid redzone:     f2
  Stack right redzone:   f3
  Stack partial redzone: f4
  Stack after return:    f5
  Stack use after scope: f8
  Global redzone:        f9
  Global init order:     f6
  Poisoned by user:      f7
  ASan internal:         fe
==5587==ABORTING

Heap-buffer-overflow

1 // RUN: clang++ -O -g -fsanitize=address %t && ./a.out
2 int main(int argc, char **argv) {
3   int *array = new int[100];
4   array[0] = 0;
5   int res = array[argc + 100];  // BOOM
6   delete [] array;
7   return res;
8 }
==25372==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000ffd4 at pc 0x0000004ddb59 bp 0x7fffea6005a0 sp 0x7fffea600598
READ of size 4 at 0x61400000ffd4 thread T0
    #0 0x46bfee in main /tmp/main.cpp:4:13

0x61400000ffd4 is located 4 bytes to the right of 400-byte region [0x61400000fe40,0x61400000ffd0)
allocated by thread T0 here:
    #0 0x4536e1 in operator delete[](void*)
    #1 0x46bfb9 in main /tmp/main.cpp:2:16

Stack-buffer-overflow

1 // RUN: clang -O -g -fsanitize=address %t && ./a.out
2 int main(int argc, char **argv) {
3   int stack_array[100];
4   stack_array[1] = 0;
5   return stack_array[argc + 100];  // BOOM
6 }
==7405==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff64740634 at pc 0x46c103 bp 0x7fff64740470 sp 0x7fff64740468
READ of size 4 at 0x7fff64740634 thread T0
    #0 0x46c102 in main /tmp/example_StackOutOfBounds.cc:5

Address 0x7fff64740634 is located in stack of thread T0 at offset 436 in frame
    #0 0x46bfaf in main /tmp/example_StackOutOfBounds.cc:2

  This frame has 1 object(s):
    [32, 432) 'stack_array' <== Memory access at offset 436 overflows this variable

Global-buffer-overflow

// RUN: clang -O -g -fsanitize=address %t && ./a.out
int global_array[100] = {-1};
int main(int argc, char **argv) {
  return global_array[argc + 100];  // BOOM
}
==7455==ERROR: AddressSanitizer: global-buffer-overflow on address 0x000000689b54 at pc 0x46bfd8 bp 0x7fff515e5ba0 sp 0x7fff515e5b98
READ of size 4 at 0x000000689b54 thread T0
    #0 0x46bfd7 in main /tmp/example_GlobalOutOfBounds.cc:4

0x000000689b54 is located 4 bytes to the right of 
  global variable 'global_array' from 'example_GlobalOutOfBounds.cc' (0x6899c0) of size 400

Limitations

AddressSanitizer does not detect any uninitialized memory reads (but this is detected by MemorySanitizer[13]), and only detects some use-after-return bugs.[14] It is also not capable of detecting all arbitrary memory corruption bugs, nor all arbitrary write bugs due to integer underflow/overflows (when the integer with undefined behavior is used to calculate memory address offsets). Adjacent buffers in structs and classes are not protected from overflow, in part to prevent breaking backwards compatibility.[15]

gollark: I am compiling nginx and you cannot, naturally, hope to flee.
gollark: I MAY be forced to update the nginx version in osmarks.net.
gollark: FPGA you?!
gollark: Hmm, you know, I could optimize this bootloader a bit if I used self-modifying code.
gollark: The other other one... reads numbers, maybe.

See also

References

  1. "LLVM 3.1 Release Notes". LLVM. Retrieved 8 February 2014.
  2. "GCC 4.8 Release Notes". GCC. Retrieved 8 February 2014.
  3. "Address Sanitizer | Apple Developer Documentation".
  4. Konstantin Serebryany; Derek Bruening; Alexander Potapenko; Dmitry Vyukov. "AddressSanitizer: a fast address sanity checker" (PDF). Proceedings of the 2012 USENIX conference on Annual Technical Conference.
  5. Abhishek Arya; Cris Neckar; Chrome Security Team. "Fuzzing for Security".
  6. "Securing Firefox: Trying new code analysis techniques". Archived from the original on 2016-03-07. Retrieved 2018-06-18.
  7. "Some of the bugs found by AddressSanitizer".
  8. Mateusz Jurczyk; Gynvael Coldwind (2014-01-10). "FFmpeg and a thousand fixes".
  9. "Search results for AddressSanitizer in FreeType Bugs".
  10. "KernelAddressSanitizer (KASAN)". Archived from the original on 2016-12-23. Retrieved 2016-12-08.
  11. Jake Edge. "The kernel address sanitizer".
  12. Jonathan Corbet. "3.20 merge window part 2".
  13. "MemorySanitizer".
  14. "ComparisonOfMemoryTools". AddressSanitizer Wiki. Retrieved 1 December 2017.
  15. "Bypassing AddressSanitizer" (PDF). Eric Wimberley. Retrieved 1 July 2014.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.