Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 1 | # GWP-ASan |
| 2 | |
| 3 | GWP-ASan is a debug tool intended to detect heap memory errors in the wild. It |
Vlad Tsyrklevich | f9c9065 | 2018-12-28 21:15:03 | [diff] [blame] | 4 | samples allocations to a debug allocator, similar to ElectricFence or Page Heap, |
Vlad Tsyrklevich | 6e6402a | 2019-01-22 07:50:20 | [diff] [blame] | 5 | causing memory errors to crash and report additional debugging context about |
| 6 | the error. |
Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 7 | |
| 8 | ## Allocator |
| 9 | |
| 10 | The GuardedPageAllocator returns allocations on pages buffered on both sides by |
| 11 | guard pages. The allocations are either left- or right-aligned to detect buffer |
| 12 | overflows and underflows. When an allocation is freed, the page is marked |
| 13 | inaccessible so use-after-frees cause an exception (until that page is reused |
| 14 | for another allocation.) |
| 15 | |
| 16 | The allocator saves stack traces on every allocation and deallocation to |
| 17 | preserve debug context if that allocation results in a memory error. |
| 18 | |
Vlad Tsyrklevich | dc1a9a5e8 | 2018-12-18 18:04:01 | [diff] [blame] | 19 | The allocator implements a quarantine mechanism by allocating virtual memory for |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 20 | more allocations than the total number of physical pages it can return at any |
| 21 | given time. The difference forms a rudimentary quarantine. |
| 22 | |
| 23 | Because pages are re-used for allocations, it's possible that a long-lived |
| 24 | use-after-free will cause a crash long after the original allocation has been |
| 25 | replaced. In order to decrease the likelihood of incorrect stack traces being |
| 26 | reported, we allocate a lot of virtual memory but don't store metadata for every |
| 27 | allocation. That way though we may not be able to report the metadata for an old |
| 28 | allocation, we will not report incorrect stack traces. |
Vlad Tsyrklevich | dc1a9a5e8 | 2018-12-18 18:04:01 | [diff] [blame] | 29 | |
Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 30 | Allocations are sampled to the GuardedPageAllocator using an [allocator shim.](/base/allocator/README.md) |
| 31 | |
| 32 | ## Crash handler |
| 33 | |
| 34 | The allocator is designed so that memory errors with GWP-ASan allocations |
| 35 | intentionally trigger invalid access exceptions. A hook in the crashpad crash |
| 36 | handler process inspects crashes, determines if they are GWP-ASan exceptions, |
| 37 | and adds additional debug information to the crash minidump if so. |
| 38 | |
| 39 | The crash handler hook determines if the exception was related to GWP-ASan by |
| 40 | reading the allocator internals and seeing if the exception address was within |
| 41 | the bounds of the allocator region. If it is, the crash handler hook extracts |
| 42 | debug information about that allocation, such as thread IDs and stack traces |
| 43 | for allocation (and deallocation, if relevant) and writes it to the crash dump. |
| 44 | |
| 45 | The crash handler runs with elevated privileges so parsing information from a |
| 46 | lesser-privileged process is security sensitive. The GWP-ASan hook is specially |
| 47 | structured to minimize the amount of allocator logic it relies on and to |
| 48 | validate the allocator internals before reasoning about them. |
| 49 | |
| 50 | ## Status |
| 51 | |
| 52 | GWP-ASan is currently only implemented for the system allocator (e.g. not |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 53 | PartitionAlloc/Oilpan/v8) on Windows and macOS. It is currently enabled by |
| 54 | default. The allocator parameters can be manually modified by using the |
| 55 | following invocation: |
Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 56 | |
| 57 | ```shell |
| 58 | chrome --enable-features="GwpAsanMalloc<Study" \ |
| 59 | --force-fieldtrials=Study/Group1 \ |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 60 | --force-fieldtrial-params=Study.Group1:MaxAllocations/128/MaxMetadata/255/TotalPages/4096/AllocationSamplingFrequency/1000/ProcessSamplingProbability/1.0 |
Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 61 | ``` |
| 62 | |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 63 | GWP-ASan is tuned more aggressively in canary/dev (to increase the likelihood we |
| 64 | catch newly introduced bugs) and for the browser process (because of its |
| 65 | importance and sheer number of allocations.) |
| 66 | |
Vlad Tsyrklevich | 6e6402a | 2019-01-22 07:50:20 | [diff] [blame] | 67 | A [hotlist of bugs discovered by by GWP-ASan](https://bugs.chromium.org/p/chromium/issues/list?can=1&q=Hotlist%3DGWP-ASan) |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 68 | exists, though GWP-ASan crashes are filed without external visibility by |
| 69 | default. |
Vlad Tsyrklevich | 6e6402a | 2019-01-22 07:50:20 | [diff] [blame] | 70 | |
Vlad Tsyrklevich | 08bc0525 | 2018-12-04 06:58:54 | [diff] [blame] | 71 | ## Testing |
| 72 | |
| 73 | There is [not yet](https://crbug.com/910751) a way to intentionally trigger a |
| 74 | GWP-ASan exception. |
| 75 | |
| 76 | There is [not yet](https://crbug.com/910749) a way to inspect GWP-ASan data in |
Vlad Tsyrklevich | 4a2e4d20 | 2019-04-25 00:22:43 | [diff] [blame^] | 77 | a minidump (crash report) without access to Google's crash service. |