blob: d1c8dc0133de53c84ded8322ebfdef1976e3e9a8 [file] [log] [blame] [view]
Charlie Reis6d4943272023-01-25 22:46:311# Dangling Pointer Detector
Arthur Sonzognic59a2cc2022-05-13 18:01:142
Arthur Sonzogni3fe4b9c02023-06-02 11:50:383A pointer is dangling when it references freed memory. Typical examples can be
4found [here](https://docs.google.com/document/d/11YYsyPF9rQv_QFf982Khie3YuNPXV0NdhzJPojpZfco/edit?resourcekey=0-h1dr1uDzZGU7YWHth5TRAQ#heading=h.wxt96wl0k0sq).
5
Tom Sepez17cf6ea2023-02-07 17:11:356Dangling pointers are not a problem unless they are subsequently dereferenced
7and/or used for other purposes. Proving that pointers are unused has turned out
8to be difficult in general, especially in face of future modifications to
9the code. Hence, they are a source of UaF bugs and highly discouraged unless
10you are able to ensure that they can never be used after the pointed-to objects
11are freed.
Arthur Sonzognic59a2cc2022-05-13 18:01:1412
Charlie Reis6d4943272023-01-25 22:46:3113See also the [Dangling Pointers Guide](./dangling_ptr_guide.md) for how to fix
14cases where dangling pointers occur.
Arthur Sonzognic66acfe62022-12-08 14:27:2815
Arthur Sonzognic59a2cc2022-05-13 18:01:1416Behind build flags, Chrome implements a dangling pointer detector. It causes
17Chrome to crash, whenever a raw_ptr becomes dangling:
18```cpp
19raw_ptr<T> ptr_never_dangling;
20```
21
22On the other hand, we cannot simply ban all the usage of dangling pointers
23because there are valid use cases. The `DisableDanglingPtrDetection` option can
24be used to annotate "intentional-and-safe" dangling pointers. It is meant to be
25used as a last resort, only if there is no better way to re-architecture the
26code.
27```cpp
28raw_ptr<T, DisableDanglingPtrDetection> ptr_may_dangle;
29```
30
Charlie Reis6d4943272023-01-25 22:46:3131The `DanglingUntriaged` option has been used to annotate pre-existing dangling
Arthur Sonzognic66acfe62022-12-08 14:27:2832pointers in Chrome:
33```cpp
34raw_ptr<T, DanglingUntriaged> ptr_dangling_mysteriously;
35```
36Contrary to `DisableDanglingPtrDetection`, we don't know yet why it dangles. It
37is meant to be either refactored to avoid dangling, or turned into
38"DisableDanglingPtrDetection" with a comment explaining what happens.
39
Arthur Sonzognic59a2cc2022-05-13 18:01:1440# How to check for dangling pointers?
41
Arthur Sonzogni591d14e2023-08-08 17:03:0342On **Linux**, it is **enabled by default** on most configurations.
43To be precise: (`is_debug` or `dcheck_always_on`) and non `is_official` builds.
44
45For the other operating systems, this is gated by both build and runtime flags:
Arthur Sonzognic59a2cc2022-05-13 18:01:1446
47## Build flags
48
49```bash
50gn args ./out/dangling/
51```
52
53```gn
Takuto Ikuta7e616372024-05-15 06:00:4254use_remoteexec = true
Bartek Nowierskie10386132022-10-25 01:16:1455is_debug = false # Important! (*)
danakjd8d332e2023-02-06 19:37:5956is_component_build = false # Important! (*)
Arthur Sonzognic59a2cc2022-05-13 18:01:1457dcheck_always_on = true
danakjd8d332e2023-02-06 19:37:5958enable_backup_ref_ptr_support = true # true by default on some platforms
Arthur Sonzognic59a2cc2022-05-13 18:01:1459enable_dangling_raw_ptr_checks = true
60```
61
danakjd8d332e2023-02-06 19:37:5962(*) We want to emphasize that setting either `is_debug = false` or
63`is_component_build = false` is important. It is a common mistake to set
64`is_debug` to `true`, which in turn turns on component builds, which
Bartek Nowierskie10386132022-10-25 01:16:1465disables PartitionAlloc-Everywhere. `enable_backup_ref_ptr_support = true` can't
66be used without PartitionAlloc-Everywhere, and is silently set to `false`.
Bartek Nowierski4cb26312022-07-14 08:56:1067
Arthur Sonzognic59a2cc2022-05-13 18:01:1468## Runtime flags
69
70```bash
Arthur Sonzognibe6f0132022-07-06 13:54:1271./out/dangling/content_shell \
Paul Semel42184622022-07-07 16:10:1972 --enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr
Arthur Sonzognic59a2cc2022-05-13 18:01:1473```
74
Arthur Sonzognibe6f0132022-07-06 13:54:1275By default, Chrome will crash on the first dangling raw_ptr detected.
Arthur Sonzognic59a2cc2022-05-13 18:01:1476
Arthur Sonzognibe6f0132022-07-06 13:54:1277# Runtime flags options:
Arthur Sonzognic59a2cc2022-05-13 18:01:1478
Pârise90af2c2023-01-30 14:22:4179## Mode parameter
80
Arthur Sonzognibe6f0132022-07-06 13:54:1281### Crash (default)
Arthur Sonzognic59a2cc2022-05-13 18:01:1482
Arthur Sonzognic59a2cc2022-05-13 18:01:1483```bash
Paul Semel42184622022-07-07 16:10:1984--enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr:mode/crash
Arthur Sonzognibe6f0132022-07-06 13:54:1285```
86
Bartek Nowierski4cb26312022-07-14 08:56:1087### Record a list of signatures
Arthur Sonzognibe6f0132022-07-06 13:54:1288
89Example usage:
90```bash
91./out/dangling/content_shell \
Pârise90af2c2023-01-30 14:22:4192 --enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr:mode/log_only \
Arthur Sonzognibe6f0132022-07-06 13:54:1293 |& tee output
Arthur Sonzognic59a2cc2022-05-13 18:01:1494```
95
96The logs can be filtered and transformed into a tab separated table:
97```bash
98cat output \
Pâris5ec36bb2023-07-24 13:07:4199 | grep "[DanglingSignature]" \
Pârise90af2c2023-01-30 14:22:41100 | cut -f2,3,4,5 \
Arthur Sonzognic59a2cc2022-05-13 18:01:14101 | sort \
102 | uniq -c \
103 | sed -E 's/^ *//; s/ /\t/' \
104 | sort -rn
105```
106
107This is used to list issues and track progresses.
Pârise90af2c2023-01-30 14:22:41108
109## Type parameter
110### Select all dangling raw_ptr (default)
111
112The option: `type/all` selects every dangling pointer.
113
114Example usage:
115```bash
116./out/dangling/content_shell \
117 --enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr:type/all
118```
119
120### Select cross tasks dangling raw_ptr
121
122The option: `type/cross_task` selects dangling pointers that are released in a
123different task than the one where the memory was freed. Those are more likely to
124cause UAF.
125
126Example usage:
127```bash
128./out/dangling/content_shell \
129 --enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr:type/cross_task
130```
131
Tom Sepez17cf6ea2023-02-07 17:11:35132### Combination
Pârise90af2c2023-01-30 14:22:41133
134Both parameters can be combined, example usage:
135```bash
136./out/dangling/content_shell \
137 --enable-features=PartitionAllocBackupRefPtr,PartitionAllocDanglingPtr:mode/log_only/type/cross_task \
138 |& tee output
139```
Tom Sepez17cf6ea2023-02-07 17:11:35140
141# Alternative dangling pointer detector (experimental)
142
143The dangling pointer detector above works only against certain heap allocated
144objects, but there is an alternate form that catches other cases such as
145pointers to out-of-scope stack variables or pointers to deallocated shared
146memory regions. The GN arguments to enable it are:
147
148```gn
149enable_backup_ref_ptr_support=false
150is_asan=true
151is_component_build=false
152use_asan_backup_ref_ptr=false
Kalvin Lee3f1e9eda2024-07-18 21:53:23153use_raw_ptr_asan_unowned_impl=true
Tom Sepez17cf6ea2023-02-07 17:11:35154```
155
156This will crash when the object containing the dangling ptr is destructed,
157giving the usual three-stack trace from ASAN showing where the deleted object
158was allocated and freed.
Tom Sepez8fd4178672023-02-14 02:36:18159
160When running under this mode, there is no need to specify any --enable-features
161flag as above.