xref: /aosp_15_r20/external/AFLplusplus/utils/libdislocator/README.md (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1*08b48e0bSAndroid Build Coastguard Worker# libdislocator, an abusive allocator
2*08b48e0bSAndroid Build Coastguard Worker
3*08b48e0bSAndroid Build Coastguard WorkerFor the general instruction manual, see [docs/README.md](../../docs/README.md).
4*08b48e0bSAndroid Build Coastguard Worker
5*08b48e0bSAndroid Build Coastguard WorkerThis is a companion library that can be used as a drop-in replacement for the
6*08b48e0bSAndroid Build Coastguard Workerlibc allocator in the fuzzed binaries. It improves the odds of bumping into
7*08b48e0bSAndroid Build Coastguard Workerheap-related security bugs in several ways:
8*08b48e0bSAndroid Build Coastguard Worker
9*08b48e0bSAndroid Build Coastguard Worker  - It allocates all buffers so that they are immediately adjacent to a
10*08b48e0bSAndroid Build Coastguard Worker    subsequent PROT_NONE page, causing most off-by-one reads and writes to
11*08b48e0bSAndroid Build Coastguard Worker    immediately segfault,
12*08b48e0bSAndroid Build Coastguard Worker
13*08b48e0bSAndroid Build Coastguard Worker  - It adds a canary immediately below the allocated buffer, to catch writes to
14*08b48e0bSAndroid Build Coastguard Worker    negative offsets (won't catch reads, though),
15*08b48e0bSAndroid Build Coastguard Worker
16*08b48e0bSAndroid Build Coastguard Worker  - It sets the memory returned by malloc() to garbage values, improving the
17*08b48e0bSAndroid Build Coastguard Worker    odds of crashing when the target accesses uninitialized data,
18*08b48e0bSAndroid Build Coastguard Worker
19*08b48e0bSAndroid Build Coastguard Worker  - It sets freed memory to PROT_NONE and does not actually reuse it, causing
20*08b48e0bSAndroid Build Coastguard Worker    most use-after-free bugs to segfault right away,
21*08b48e0bSAndroid Build Coastguard Worker
22*08b48e0bSAndroid Build Coastguard Worker  - It forces all realloc() calls to return a new address - and sets PROT_NONE
23*08b48e0bSAndroid Build Coastguard Worker    on the original block. This catches use-after-realloc bugs,
24*08b48e0bSAndroid Build Coastguard Worker
25*08b48e0bSAndroid Build Coastguard Worker  - It checks for calloc() overflows and can cause soft or hard failures of
26*08b48e0bSAndroid Build Coastguard Worker    alloc requests past a configurable memory limit (AFL_LD_LIMIT_MB,
27*08b48e0bSAndroid Build Coastguard Worker    AFL_LD_HARD_FAIL).
28*08b48e0bSAndroid Build Coastguard Worker
29*08b48e0bSAndroid Build Coastguard Worker  - Optionally, in platforms supporting it, huge pages can be used by passing
30*08b48e0bSAndroid Build Coastguard Worker    `USEHUGEPAGE=1` to make.
31*08b48e0bSAndroid Build Coastguard Worker
32*08b48e0bSAndroid Build Coastguard Worker  - Optionally, in platforms supporting it, `named` pages can be used by passing
33*08b48e0bSAndroid Build Coastguard Worker    `USENAMEDPAGE=1` to make.
34*08b48e0bSAndroid Build Coastguard Worker
35*08b48e0bSAndroid Build Coastguard Worker  - Size alignment to `max_align_t` can be enforced with `AFL_ALIGNED_ALLOC=1`. In
36*08b48e0bSAndroid Build Coastguard Worker    this case, a tail canary is inserted in the padding bytes at the end of the
37*08b48e0bSAndroid Build Coastguard Worker    allocated zone. This reduces the ability of libdislocator to detect
38*08b48e0bSAndroid Build Coastguard Worker    off-by-one bugs but also it makes libdislocator compliant to the C standard.
39*08b48e0bSAndroid Build Coastguard Worker
40*08b48e0bSAndroid Build Coastguard WorkerBasically, it is inspired by some of the non-default options available for the
41*08b48e0bSAndroid Build Coastguard WorkerOpenBSD allocator - see malloc.conf(5) on that platform for reference. It is
42*08b48e0bSAndroid Build Coastguard Workeralso somewhat similar to several other debugging libraries, such as gmalloc and
43*08b48e0bSAndroid Build Coastguard WorkerDUMA - but is simple, plug-and-play, and designed specifically for fuzzing jobs.
44*08b48e0bSAndroid Build Coastguard Worker
45*08b48e0bSAndroid Build Coastguard WorkerNote that it does nothing for stack-based memory handling errors. The
46*08b48e0bSAndroid Build Coastguard Worker-fstack-protector-all setting for GCC / clang, enabled when using AFL_HARDEN,
47*08b48e0bSAndroid Build Coastguard Workercan catch some subset of that.
48*08b48e0bSAndroid Build Coastguard Worker
49*08b48e0bSAndroid Build Coastguard WorkerThe allocator is slow and memory-intensive (even the tiniest allocation uses up
50*08b48e0bSAndroid Build Coastguard Worker4 kB of physical memory and 8 kB of virtual mem), making it completely
51*08b48e0bSAndroid Build Coastguard Workerunsuitable for "production" uses; but it can be faster and more hassle-free than
52*08b48e0bSAndroid Build Coastguard WorkerASAN / MSAN when fuzzing small, self-contained binaries.
53*08b48e0bSAndroid Build Coastguard Worker
54*08b48e0bSAndroid Build Coastguard WorkerTo use this library, run AFL++ like so:
55*08b48e0bSAndroid Build Coastguard Worker
56*08b48e0bSAndroid Build Coastguard Worker```
57*08b48e0bSAndroid Build Coastguard WorkerAFL_PRELOAD=/path/to/libdislocator.so ./afl-fuzz [...other params...]
58*08b48e0bSAndroid Build Coastguard Worker```
59*08b48e0bSAndroid Build Coastguard Worker
60*08b48e0bSAndroid Build Coastguard WorkerYou *have* to specify path, even if it's just ./libdislocator.so or
61*08b48e0bSAndroid Build Coastguard Worker$PWD/libdislocator.so.
62*08b48e0bSAndroid Build Coastguard Worker
63*08b48e0bSAndroid Build Coastguard WorkerSimilarly to afl-tmin, the library is not "proprietary" and can be used with
64*08b48e0bSAndroid Build Coastguard Workerother fuzzers or testing tools without the need for any code tweaks. It does not
65*08b48e0bSAndroid Build Coastguard Workerrequire AFL-instrumented binaries to work.
66*08b48e0bSAndroid Build Coastguard Worker
67*08b48e0bSAndroid Build Coastguard WorkerNote that the AFL_PRELOAD approach (which AFL++ internally maps to LD_PRELOAD or
68*08b48e0bSAndroid Build Coastguard WorkerDYLD_INSERT_LIBRARIES, depending on the OS) works only if the target binary is
69*08b48e0bSAndroid Build Coastguard Workerdynamically linked. Otherwise, attempting to use the library will have no
70*08b48e0bSAndroid Build Coastguard Workereffect.
71