Security ·
Defensive Fuzzing on Apple Silicon with libFuzzer, ASan, and LLDB
Building a coverage-guided fuzzing harness to catch stack buffer overflows and produce reproducible crash reports before production
This project implements an automated defensive fuzzing harness for C code on Apple Silicon. It feeds thousands of mutated inputs to a target parser, monitors every memory access with sanitizers, preserves exact crashing inputs, and drives LLDB to produce a structured HTML crash report.
There is no blind randomness or heuristic guesswork involved: the setup relies on coverage-guided mutation paired with runtime memory instrumentation.
Verified Run Summary
| Metric | Result |
|---|---|
| Fuzzing engine | libFuzzer integrated with Clang |
| Inputs executed before crash | 29,898 |
| Reproducible crash artifacts saved | 2 |
| Distinct root cause | 1 signature: stack-buffer-overflow |
| Crash state captured | PC register via LLDB on Apple Silicon |
The end-to-end test suite also passed: it compiles the binaries, accepts a valid input, rejects a malformed input, catches the overflow under AddressSanitizer, and generates the final HTML report.
How Coverage-Guided Fuzzing Works
Unlike throwing random bytes at a binary, libFuzzer instruments the code at compile time to track which branches and basic blocks each input reaches. Starting from a small corpus of valid seeds, the engine applies mutations—tweaking integers, deleting or inserting bytes, splicing two seeds, and repeating fragments—while prioritizing mutations that uncover new execution paths.
When an input triggers a memory violation, execution halts and the exact offending payload is written to disk for deterministic replay.
The Controlled Target Bug: target_demo
To verify that the detection and reporting pipeline works end-to-end, we built target_demo, a C program containing an intentional vulnerability.
The parser expects strings formatted as:
LEN=4;DATA=PING
Internally, it allocates a 32-byte stack buffer. The parser verifies that the input contains at least as many bytes as LEN declares, but fails to check whether LEN fits inside the 32-byte destination buffer. An input such as:
LEN=33;DATA=<33 bytes>
declares 33 bytes and supplies 33 bytes, overflowing the 32-byte buffer by one byte and overwriting adjacent stack memory. That triggers a stack-buffer-overflow. The flaw is intentional—it acts as a known crash target to validate the harness instrumentation.
Pipeline Architecture
valid seeds
↓
libFuzzer mutates inputs thousands of times
↓
harness feeds each input to the parser
↓
ASan/UBSan monitor every memory access
↓
SIGSEGV or SIGABRT halts the run
↓
exact crashing input is saved to disk
↓
target_demo replays the artifact
↓
gdb or LLDB extracts registers and backtrace
↓
crash-report.html renders the evidence
1. Seed Corpus Generation
scripts/generate_corpus.py builds small, valid, distinct seed inputs. One seed sits right near the boundary: it declares 31 bytes while carrying trailing data. That gives the fuzzer a short path to cross the 32-byte limit with minimal mutation.
2. In-Process Mutation with libFuzzer
The harness exposes LLVMFuzzerTestOneInput, allowing libFuzzer to invoke the parser thousands of times inside a single process without spawning a new process per test case.
3. Sanitizer Instrumentation
The target is compiled with:
- AddressSanitizer (ASan) to catch out-of-bounds reads and writes;
- UndefinedBehaviorSanitizer (UBSan) to catch invalid C behavior;
- debug symbols and frame pointers to preserve readable stack traces.
The runner also configures libFuzzer to explicitly intercept SIGSEGV and SIGABRT. When the 33-byte write hits the 32-byte buffer, ASan halts execution and pinpoints parser.c, the memcpy call site, the write size, and the stack frame.
4. Artifact Persistence
Each crashing input is saved under artifacts/crash-*. This guarantees deterministic reproduction: the exact same payload can be replayed against target_demo repeatedly during debugging.
5. Register and Backtrace Extraction
The post-processor replays each artifact against target_demo under a debugger. It uses gdb if present; on macOS on Apple Silicon, it uses LLDB and extracts the PC (Program Counter) register along with the full backtrace. On x86/x86_64 systems, it captures EIP or RIP instead.
6. HTML Report Generation
report/crash-report.html aggregates:
- total artifact count;
- deduplicated root-cause count;
- full AddressSanitizer signature;
- instruction pointer register value;
- input size and SHA-256 digest;
- exact reproduction command;
- sanitizer output, registers, backtrace, and full execution log.
A full-page PNG screenshot is also saved for quick visual inspection.
Deduplication: Two Artifacts, One Root Cause
The verified run produced two crash files: one caused the parser to copy 33 bytes, and the other copied 35 bytes. While the inputs differ in size and content, both hit the exact same source line and yield the identical ASan signature.
The report preserves both artifacts as reproducible evidence while grouping them under a single stack-buffer-overflow signature. This prevents inflating bug counts when distinct inputs exercise the same underlying defect.
Repository Layout
src/parser.c: test parser containing the intentional missing bounds check.src/target_demo.c: standalone binary that reads an input file orstdin.src/fuzz_harness.c:LLVMFuzzerTestOneInputentry point forlibFuzzer.scripts/generate_corpus.py: initial seed corpus generator.scripts/run_fuzz.sh: builds, runs the fuzzer, collects artifacts, and triggers analysis.scripts/analyze_crashes.py: automatesgdb/LLDBinspection and builds the HTML report.tests/test_end_to_end.sh: end-to-end verification script.Makefile: unified targets for build, test, fuzzing, and reporting.
Running the Harness
From the project root:
make test
make fuzz FUZZ_SECONDS=10
open report/crash-report.html
make test must finish with PASS. make fuzz may exit before the 10-second timeout if it hits a crash; early termination means the runner found a fault, saved the artifact, and built the report before returning control.
Reading the Crash Report
Four fields identify the fault immediately:
stack-buffer-overflow: class of stack memory corruption.WRITE of size 33: exact size of the out-of-bounds write.parser.c:59: source file and line where the overflow occurs.PC=...: instruction address where the CPU halted during replay.
If two crash cards share the exact same ASan SUMMARY, they almost certainly stem from the same defect; a different summary or source line warrants separate triage.
Scope and Limitations
What This Proves
libFuzzer, ASan, and UBSan are properly integrated on Apple Silicon.- Coverage-guided mutations reach boundary conditions inside the parser.
- Out-of-bounds writes are caught deterministically.
- Crashing inputs are preserved and remain reproducible.
- Register state and visual reports are extracted automatically.
What This Does Not Prove
- That any arbitrary program wired into the harness is automatically bug-free.
- That a 10-second run is sufficient to explore a large codebase.
- That two saved artifacts represent two separate vulnerabilities.
- That
AFL++is installed in the environment (this setup useslibFuzzer). - That
target_demoneeds fixing (its vulnerability is intentional).
Applying This to Production Code
To adapt the harness to real targets:
- Swap out the demo parser for the target function under audit.
- Build a small, non-sensitive corpus of realistic inputs.
- Add seeds covering numeric boundaries, optional fields, and edge-case formats.
- Run short smoke campaigns first, followed by longer fuzzing runs.
- Group crashes by signature, fix the root cause, and replay every saved artifact against the patch.
- Re-run the fuzzer to confirm the path is closed and retain the crashing inputs as regression tests.
The goal of fuzzing is not to collect crash files, but to turn each finding into a deterministic regression test that keeps the defect from returning.