Skip to content
All articles

Security

A practical guide to C++ fuzzing with libFuzzer and AddressSanitizer

Feeding my own parser the inputs I can think of really means testing my own blind spots. Fuzzing lets the machine find them instead. A step-by-step guide with libFuzzer and AddressSanitizer.

Fahrettin Baştürk5 min read

When you write C++ code that parses HTTP requests or produces JSON, you usually test it with the inputs you can think of: a well-formed request, a missing header, an empty body. The problem is that the inputs you don't think of are exactly the ones your code doesn't handle. The moment I realized my own tests were really testing my own blind spots, I started taking fuzzing seriously.

Fuzzing means feeding a program thousands or millions of automatically generated, mostly malformed inputs and catching the cases where it crashes or shows undefined behavior. In languages like C and C++ that don't guarantee memory safety, it's one of the most efficient ways to harden a parser.

Why it isn't blind guessing

Old-school fuzzers generated random bytes and threw them at the program. Modern fuzzers are coverage-guided: the compiler adds small counters to your code, and the fuzzer sees which input reaches a new code path. Inputs that open new paths are kept and mutated further. That way the fuzzer gradually "learns" your if conditions.

Your first fuzz target

libFuzzer ships with Clang. All you need is a function the fuzzer calls with each input:

#include <cstddef>
#include <cstdint>
#include <string_view>
#include "http_parser.hpp"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
    std::string_view input(reinterpret_cast<const char*>(data), size);
    HttpRequest req;
    parse_request(input, req);   // must not crash, leak or hit undefined behavior
    return 0;
}

When compiling, we turn on two things together: the fuzzer and the sanitizers.

clang++ -g -O1 -fsanitize=fuzzer,address,undefined fuzz_http.cpp http_parser.cpp -o fuzz_http
mkdir -p corpus
./fuzz_http corpus/ -max_len=4096

Why AddressSanitizer is essential

On its own, fuzzing only finds inputs that crash. But in C++ many bugs don't crash: writing one byte past an array, reading freed memory, signed integer overflow. The program keeps running, and the bug explodes somewhere else, later. AddressSanitizer (ASan) catches these on the line where they happen, and UndefinedBehaviorSanitizer (UBSan) catches undefined behavior. Fuzzing's real power shows up when it's combined with sanitizers.

When a crash is found, libFuzzer saves the input to a file named crash-<hash>, and ASan gives you a full stack trace: which line, which memory region, how many bytes out of bounds.

Corpus, dictionaries and minimization

  • Corpus: giving the fuzzer a few realistic starting examples speeds it up a lot. For an HTTP parser, a handful of well-formed request files is enough.
  • Dictionary: with -dict=http.dict you can give the fuzzer keywords like "GET", "Content-Length:" and "\r\n". Finding those at random would take a very long time.
  • Minimization: a crashing input is usually long and meaningless. ./fuzz_http -minimize_crash=1 crash-... shrinks it to the smallest input that still triggers the bug. Understanding the bug is much easier after that.

Turn every crash into a test

After fixing a crash, don't throw the input away. Add it to the corpus and to your unit tests; if the bug ever comes back, the first run will catch it. Running the fuzzer for a few minutes in CI on every change is also a cheap way to make sure new code hasn't broken the parser.

Where to start

Fuzzing isn't for every piece of code. It pays off most in:

  1. Anything that parses external data: network protocols, file formats, JSON, XML.
  2. Compression, decryption and serialization code.
  3. The small helpers you wrote yourself and swear nobody can break. They're usually the first to crash.

The HTTP proxy and JSON serializer I wrote in C++ is exactly this kind of code: it takes bytes from outside and turns them into structure. My first advice to anyone writing code like this is to add a small fuzz target next to the unit tests.

AFL++ and Google's OSS-Fuzz are larger-scale versions of the same idea. But to get started, Clang and one function are enough. Leaving a fuzzer running overnight and checking the results in the morning makes you rethink how much you trust your own code. For me, that was the most educational part.

Fahrettin Baştürk

Software Engineer & Consultant

Share
  • C++
  • Fuzzing
  • Security
  • Testing

Working on a similar system or problem?Tell me about your project