12 GWP-ASan is a sampled allocator framework that assists in finding use-after-free
13 and heap-buffer-overflow bugs in production environments. It informally is a
14 recursive acronym, "**G**\WP-ASan **W**\ill **P**\rovide **A**\llocation
17 GWP-ASan is based on the classic
18 `Electric Fence Malloc Debugger <https://linux.die.net/man/3/efence>`_, with a
19 key adaptation. Notably, we only choose a very small percentage of allocations
20 to sample, and apply guard pages to these sampled allocations only. The sampling
21 is small enough to allow us to have very low performance overhead.
23 There is a small, tunable memory overhead that is fixed for the lifetime of the
24 process. This is approximately ~40KiB per process using the default settings,
25 depending on the average size of your allocations.
30 Unlike `AddressSanitizer <https://clang.llvm.org/docs/AddressSanitizer.html>`_,
31 GWP-ASan does not induce a significant performance overhead. ASan often requires
32 the use of dedicated canaries to be viable in production environments, and as
33 such is often impractical. Moreover, ASan's runtime is not developed with
34 security consideration in mind, making compiled binaries more vulnerable to
37 However, GWP-ASan is only capable of finding a subset of the memory issues
38 detected by ASan. Furthermore, GWP-ASan's bug detection capabilities are
39 only probabilistic. As such, we recommend using ASan over GWP-ASan in testing,
40 as well as anywhere else that guaranteed error detection is more valuable than
41 the 2x execution slowdown/binary size bloat. For the majority of production
42 environments, this impact is too high and security is indispensable, so GWP-ASan
43 proves extremely useful.
49 **Please note:** The implementation of GWP-ASan is largely in-flux, and these
50 details are subject to change. There are currently other implementations of
51 GWP-ASan, such as the implementation featured in
52 `Chromium <https://cs.chromium.org/chromium/src/components/gwp_asan/>`_. The
53 long-term support goal is to ensure feature-parity where reasonable, and to
54 support compiler-rt as the reference implementation.
59 GWP-ASan is not a replacement for a traditional allocator. Instead, it works by
60 inserting stubs into a supporting allocator to redirect allocations to GWP-ASan
61 when they're chosen to be sampled. These stubs are generally implemented in the
62 implementation of ``malloc()``, ``free()`` and ``realloc()``. The stubs are
63 extremely small, which makes using GWP-ASan in most allocators fairly trivial.
64 The stubs follow the same general pattern (example ``malloc()`` pseudocode
69 #ifdef INSTALL_GWP_ASAN_STUBS
70 gwp_asan::GuardedPoolAllocator GWPASanAllocator;
73 void* YourAllocator::malloc(..) {
74 #ifdef INSTALL_GWP_ASAN_STUBS
75 if (GWPASanAllocator.shouldSample(..))
76 return GWPASanAllocator.allocate(..);
79 // ... the rest of your allocator code here.
82 Then, all the supporting allocator needs to do is compile with
83 ``-DINSTALL_GWP_ASAN_STUBS`` and link against the GWP-ASan library! For
84 performance reasons, we strongly recommend static linkage of the GWP-ASan
87 Guarded Allocation Pool
88 -----------------------
90 The core of GWP-ASan is the guarded allocation pool. Each sampled allocation is
91 backed using its own *guarded* slot, which may consist of one or more accessible
92 pages. Each guarded slot is surrounded by two *guard* pages, which are mapped as
93 inaccessible. The collection of all guarded slots makes up the *guarded
96 Buffer Underflow/Overflow Detection
97 -----------------------------------
99 We gain buffer-overflow and buffer-underflow detection through these guard
100 pages. When a memory access overruns the allocated buffer, it will touch the
101 inaccessible guard page, causing memory exception. This exception is caught and
102 handled by the internal crash handler. Because each allocation is recorded with
103 metadata about where (and by what thread) it was allocated and deallocated, we
104 can provide information that will help identify the root cause of the bug.
106 Allocations are randomly selected to be either left- or right-aligned to provide
107 equal detection of both underflows and overflows.
109 Use after Free Detection
110 ------------------------
112 The guarded allocation pool also provides use-after-free detection. Whenever a
113 sampled allocation is deallocated, we map its guarded slot as inaccessible. Any
114 memory accesses after deallocation will thus trigger the crash handler, and we
115 can provide useful information about the source of the error.
117 Please note that the use-after-free detection for a sampled allocation is
118 transient. To keep memory overhead fixed while still detecting bugs, deallocated
119 slots are randomly reused to guard future allocations.
124 GWP-ASan already ships by default in the
125 `Scudo Hardened Allocator <https://llvm.org/docs/ScudoHardenedAllocator.html>`_,
126 so building with ``-fsanitize=scudo`` is the quickest and easiest way to try out
132 GWP-ASan's configuration is managed by the supporting allocator. We provide a
133 generic configuration management library that is used by Scudo. It allows
134 several aspects of GWP-ASan to be configured through the following methods:
136 - When the GWP-ASan library is compiled, by setting
137 ``-DGWP_ASAN_DEFAULT_OPTIONS`` to the options string you want set by default.
138 If you're building GWP-ASan as part of a compiler-rt/LLVM build, add it during
139 cmake configure time (e.g. ``cmake ... -DGWP_ASAN_DEFAULT_OPTIONS="..."``). If
140 you're building GWP-ASan outside of compiler-rt, simply ensure that you
141 specify ``-DGWP_ASAN_DEFAULT_OPTIONS="..."`` when building
142 ``optional/options_parser.cpp``).
144 - By defining a ``__gwp_asan_default_options`` function in one's program that
145 returns the options string to be parsed. Said function must have the following
146 prototype: ``extern "C" const char* __gwp_asan_default_options(void)``, with a
147 default visibility. This will override the compile time define;
149 - Depending on allocator support (Scudo has support for this mechanism): Through
150 an environment variable, containing the options string to be parsed. In Scudo,
151 this is through `SCUDO_OPTIONS=GWP_ASAN_${OPTION_NAME}=${VALUE}` (e.g.
152 `SCUDO_OPTIONS=GWP_ASAN_SampleRate=100`). Options defined this way will
153 override any definition made through ``__gwp_asan_default_options``.
155 The options string follows a syntax similar to ASan, where distinct options
156 can be assigned in the same string, separated by colons.
158 For example, using the environment variable:
162 GWP_ASAN_OPTIONS="MaxSimultaneousAllocations=16:SampleRate=5000" ./a.out
164 Or using the function:
168 extern "C" const char *__gwp_asan_default_options() {
169 return "MaxSimultaneousAllocations=16:SampleRate=5000";
172 The following options are available:
174 +----------------------------+---------+--------------------------------------------------------------------------------+
175 | Option | Default | Description |
176 +----------------------------+---------+--------------------------------------------------------------------------------+
177 | Enabled | true | Is GWP-ASan enabled? |
178 +----------------------------+---------+--------------------------------------------------------------------------------+
179 | PerfectlyRightAlign | false | When allocations are right-aligned, should we perfectly align them up to the |
180 | | | page boundary? By default (false), we round up allocation size to the nearest |
181 | | | power of two (2, 4, 8, 16) up to a maximum of 16-byte alignment for |
182 | | | performance reasons. Setting this to true can find single byte |
183 | | | buffer-overflows at the cost of performance, and may be incompatible with |
184 | | | some architectures. |
185 +----------------------------+---------+--------------------------------------------------------------------------------+
186 | MaxSimultaneousAllocations | 16 | Number of simultaneously-guarded allocations available in the pool. |
187 +----------------------------+---------+--------------------------------------------------------------------------------+
188 | SampleRate | 5000 | The probability (1 / SampleRate) that a page is selected for GWP-ASan |
189 | | | sampling. Sample rates up to (2^31 - 1) are supported. |
190 +----------------------------+---------+--------------------------------------------------------------------------------+
191 | InstallSignalHandlers | true | Install GWP-ASan signal handlers for SIGSEGV during dynamic loading. This |
192 | | | allows better error reports by providing stack traces for allocation and |
193 | | | deallocation when reporting a memory error. GWP-ASan's signal handler will |
194 | | | forward the signal to any previously-installed handler, and user programs |
195 | | | that install further signal handlers should make sure they do the same. Note, |
196 | | | if the previously installed SIGSEGV handler is SIG_IGN, we terminate the |
197 | | | process after dumping the error report. |
198 +----------------------------+---------+--------------------------------------------------------------------------------+
203 The below code has a use-after-free bug, where the ``string_view`` is created as
204 a reference to the temporary result of the ``string+`` operator. The
205 use-after-free occurs when ``sv`` is dereferenced on line 8.
209 1: #include <iostream>
211 3: #include <string_view>
214 6: std::string s = "Hellooooooooooooooo ";
215 7: std::string_view sv = s + "World\n";
219 Compiling this code with Scudo+GWP-ASan will probabilistically catch this bug
220 and provide us a detailed error report:
224 $ clang++ -fsanitize=scudo -g buggy_code.cpp
225 $ for i in `seq 1 500`; do
226 SCUDO_OPTIONS="GWP_ASAN_SampleRate=100" ./a.out > /dev/null;
229 | *** GWP-ASan detected a memory error ***
230 | Use after free at 0x7feccab26000 (0 bytes into a 41-byte allocation at 0x7feccab26000) by thread 31027 here:
232 | #9 ./a.out(_ZStlsIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_St17basic_string_viewIS3_S4_E+0x45) [0x55585c0afa55]
233 | #10 ./a.out(main+0x9f) [0x55585c0af7cf]
234 | #11 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7fecc966952b]
235 | #12 ./a.out(_start+0x2a) [0x55585c0867ba]
237 | 0x7feccab26000 was deallocated by thread 31027 here:
239 | #7 ./a.out(main+0x83) [0x55585c0af7b3]
240 | #8 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7fecc966952b]
241 | #9 ./a.out(_start+0x2a) [0x55585c0867ba]
243 | 0x7feccab26000 was allocated by thread 31027 here:
245 | #12 ./a.out(main+0x57) [0x55585c0af787]
246 | #13 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7fecc966952b]
247 | #14 ./a.out(_start+0x2a) [0x55585c0867ba]
249 | *** End GWP-ASan report ***
252 To symbolize these stack traces, some care has to be taken. Scudo currently uses
253 GNU's ``backtrace_symbols()`` from ``<execinfo.h>`` to unwind. The unwinder
254 provides human-readable stack traces in ``function+offset`` form, rather than
255 the normal ``binary+offset`` form. In order to use addr2line or similar tools to
256 recover the exact line number, we must convert the ``function+offset`` to
257 ``binary+offset``. A helper script is available at
258 ``compiler-rt/lib/gwp_asan/scripts/symbolize.sh``. Using this script will
259 attempt to symbolize each possible line, falling back to the previous output if
260 anything fails. This results in the following output:
265 $ cat my_gwp_asan_error.txt | symbolize.sh
267 | *** GWP-ASan detected a memory error ***
268 | Use after free at 0x7feccab26000 (0 bytes into a 41-byte allocation at 0x7feccab26000) by thread 31027 here:
270 | #9 /usr/lib/gcc/x86_64-linux-gnu/8.0.1/../../../../include/c++/8.0.1/string_view:547
271 | #10 /tmp/buggy_code.cpp:8
273 | 0x7feccab26000 was deallocated by thread 31027 here:
275 | #7 /tmp/buggy_code.cpp:8
276 | #8 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7fecc966952b]
277 | #9 ./a.out(_start+0x2a) [0x55585c0867ba]
279 | 0x7feccab26000 was allocated by thread 31027 here:
281 | #12 /tmp/buggy_code.cpp:7
282 | #13 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xeb) [0x7fecc966952b]
283 | #14 ./a.out(_start+0x2a) [0x55585c0867ba]
285 | *** End GWP-ASan report ***