blob: 0751e2e3d97928dbcddd7e91dad8c59efb00c8ed [file] [log] [blame] [view]
Vlad Tsyrklevich08bc05252018-12-04 06:58:541# GWP-ASan
2
3GWP-ASan is a debug tool intended to detect heap memory errors in the wild. It
Vlad Tsyrklevichf9c90652018-12-28 21:15:034samples allocations to a debug allocator, similar to ElectricFence or Page Heap,
Vlad Tsyrklevich6e6402a2019-01-22 07:50:205causing memory errors to crash and report additional debugging context about
6the error.
Vlad Tsyrklevich08bc05252018-12-04 06:58:547
Vlad Tsyrklevichee4629b2019-10-24 20:07:068It is also known by its recursive backronym, GWP-ASan Will Provide Allocation
9Sanity.
10
Vlad Tsyrklevichff3290ed2019-11-16 07:45:1811To read a more in-depth explanation of GWP-ASan see [this post](https://sites.google.com/a/chromium.org/dev/Home/chromium-security/articles/gwp-asan).
12
Vlad Tsyrklevich08bc05252018-12-04 06:58:5413## Allocator
14
15The GuardedPageAllocator returns allocations on pages buffered on both sides by
16guard pages. The allocations are either left- or right-aligned to detect buffer
17overflows and underflows. When an allocation is freed, the page is marked
18inaccessible so use-after-frees cause an exception (until that page is reused
19for another allocation.)
20
21The allocator saves stack traces on every allocation and deallocation to
22preserve debug context if that allocation results in a memory error.
23
Vlad Tsyrklevichdc1a9a5e82018-12-18 18:04:0124The allocator implements a quarantine mechanism by allocating virtual memory for
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4325more allocations than the total number of physical pages it can return at any
26given time. The difference forms a rudimentary quarantine.
27
28Because pages are re-used for allocations, it's possible that a long-lived
29use-after-free will cause a crash long after the original allocation has been
30replaced. In order to decrease the likelihood of incorrect stack traces being
31reported, we allocate a lot of virtual memory but don't store metadata for every
32allocation. That way though we may not be able to report the metadata for an old
33allocation, we will not report incorrect stack traces.
Vlad Tsyrklevichdc1a9a5e82018-12-18 18:04:0134
Vlad Tsyrklevich08bc05252018-12-04 06:58:5435## Crash handler
36
37The allocator is designed so that memory errors with GWP-ASan allocations
38intentionally trigger invalid access exceptions. A hook in the crashpad crash
39handler process inspects crashes, determines if they are GWP-ASan exceptions,
40and adds additional debug information to the crash minidump if so.
41
42The crash handler hook determines if the exception was related to GWP-ASan by
43reading the allocator internals and seeing if the exception address was within
44the bounds of the allocator region. If it is, the crash handler hook extracts
45debug information about that allocation, such as thread IDs and stack traces
46for allocation (and deallocation, if relevant) and writes it to the crash dump.
47
48The crash handler runs with elevated privileges so parsing information from a
49lesser-privileged process is security sensitive. The GWP-ASan hook is specially
50structured to minimize the amount of allocator logic it relies on and to
51validate the allocator internals before reasoning about them.
52
53## Status
54
Vlad Tsyrklevichee4629b2019-10-24 20:07:0655GWP-ASan is implemented for malloc and PartitionAlloc. It is enabled by default
56on Windows and macOS. The allocator parameters can be manually modified by using
57an invocation like the following:
Vlad Tsyrklevich08bc05252018-12-04 06:58:5458
59```shell
60chrome --enable-features="GwpAsanMalloc<Study" \
61 --force-fieldtrials=Study/Group1 \
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4362 --force-fieldtrial-params=Study.Group1:MaxAllocations/128/MaxMetadata/255/TotalPages/4096/AllocationSamplingFrequency/1000/ProcessSamplingProbability/1.0
Vlad Tsyrklevich08bc05252018-12-04 06:58:5463```
64
Vlad Tsyrklevich04d86642019-05-21 00:22:5065GWP-ASan is tuned more aggressively in canary/dev, to increase the likelihood we
66catch newly introduced bugs, and for specific processes depending on the
67particular allocator.
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4368
Vlad Tsyrklevich6e6402a2019-01-22 07:50:2069A [hotlist of bugs discovered by by GWP-ASan](https://bugs.chromium.org/p/chromium/issues/list?can=1&q=Hotlist%3DGWP-ASan)
Vlad Tsyrklevichff3290ed2019-11-16 07:45:1870exists, though GWP-ASan crashes are filed Bug-Security, e.g. without external
71visibility, by default.
Vlad Tsyrklevich6e6402a2019-01-22 07:50:2072
Vlad Tsyrklevich04d86642019-05-21 00:22:5073## Limitations
74
75- GWP-ASan is configured with a small fixed-size amount of memory, so
76 long-lived allocations can quickly deplete the page pool and lead the
77 allocator to run out of memory. Depending on the sampling frequency and
78 distribution of allocation lifetimes this may lead to only allocations early
79 in the process lifetime being sampled.
80- Allocations over a page in size are not sampled.
81- The allocator skips zero-size allocations. Zero-size allocations on some
82 platforms return valid pointers and may be subject to lifetime and bounds
83 issues.
84- GWP-ASan does not intercept allocations for Oilpan or the v8 GC.
85- GWP-ASan does not hook PDFium's fork of PartitionAlloc.
86- Right-aligned allocations to catch overflows are not perfectly right-aligned,
87 so small out-of-bounds accesses may be missed.
Vlad Tsyrklevichee4629b2019-10-24 20:07:0688- GWP-ASan does not sample some early allocations that occur before field trial
89 initialization.
90- Depending on the platform, GWP-ASan may or may not hook malloc allocations
91 that occur in code not linked directly against Chrome.
Vlad Tsyrklevich04d86642019-05-21 00:22:5092
Vlad Tsyrklevich08bc05252018-12-04 06:58:5493## Testing
94
95There is [not yet](https://crbug.com/910751) a way to intentionally trigger a
96GWP-ASan exception.
97
98There is [not yet](https://crbug.com/910749) a way to inspect GWP-ASan data in
Vlad Tsyrklevich4a2e4d202019-04-25 00:22:4399a minidump (crash report) without access to Google's crash service.