Fuzzing is automated test generation that feeds lots of weird inputs into your code to shake out bugs. Modern fuzzers are coverage-guided: they learn which inputs open new code paths and then mutate those winners to go deeper. Pair that with runtime "oracles" like ASan (AddressSanitizer) and UBSan (UndefinedBehaviorSanitizer), and you catch memory corruption, overflows, UAFs, and UB quickly.
Black-box fuzzing involves testing a program without any knowledge of its internal structure, code, or logic. The fuzzer generates inputs (e.g., random strings, malformed files, or network packets) and feeds them to the target, observing only external behavior such as crashes, errors, or unexpected outputs.
Black-box fuzzing is ideal for quick, initial testing of closed-source applications, legacy systems, or scenarios where instrumentation is impractical. For example, fuzzing a proprietary PDF reader by feeding it thousands of malformed PDF files to detect crashes.
Grey-box fuzzing strikes a balance between black-box and white-box approaches by incorporating lightweight instrumentation to gain partial visibility into the program's execution. It uses feedback, such as code coverage (e.g., which code branches or edges are executed), to guide input generation and prioritize inputs that explore new paths.
Grey-box fuzzing is widely used in modern software development and security testing. For instance, fuzzing a network protocol implementation with AFL++ to find buffer overflows by prioritizing inputs that trigger new code paths. It’s particularly effective for open-source projects or systems where source code or binary instrumentation is feasible.
White-box fuzzing leverages detailed knowledge of the program’s internal structure, often using advanced techniques like symbolic execution or concolic execution to systematically explore code paths. It analyzes the program's control flow and constraints to generate inputs that target specific execution paths.
White-box fuzzing is best suited for critical, well-defined components where deep analysis is justified, such as cryptographic libraries or protocol parsers. For example, using KLEE to test a compression library for edge-case bugs by solving constraints to reach rare code paths.
In practice, many fuzzing workflows combine elements of black-box, grey-box, and white-box fuzzing to maximize effectiveness. For example:
Input strategies:
| Fuzzer | Style | Coverage Source | How You Drive It | Strengths | Gotchas |
| libFuzzer | In-process, mutation-based (grey-box) | LLVM SanitizerCoverage (edge counters) | Implement int LLVMFuzzerTestOneInput(const uint8_t*, size_t) | Very fast; unit-test style harness, great with ASan/UBSan | Single target per binary, in-process requires fuzzer-safe code (no global leaks/stateful crashes) |
| AFL++ | Fork-server (grey-box), plus QEMU/Frida modes | Compiler RT or binary instr., QEMU DBT, Frida hooks | Feed files/stdin to main(), use persistent_mode for speed | Handles big apps/whole programs, rich corpus tools, many modes | Slower per-exec than in-process, best perf needs harnessing/persistent loop |
| LibAFL | Modular framework: in-process or fork-server; QEMU/Frida | Compiler instr., QEMU DBT, Frida hooks, custom observers | Write a harness (Rust or C/C++ via bindings); plug-and-play mutators/schedulers, supports persistent loops | Highly customizable, advanced schedulers, scale-out/distributed friendly, mobile (Frida) support | Steeper learning curve, more engineering to assemble a pipeline, fewer drop-in harnesses |
| Honggfuzz | Fork-server (grey-box) | Binary instrumentation | Similar to AFL++ (files/stdin), persistent mode available | Simple setup; solid performance, handy features (perf counters, sanitizers) | Smaller ecosystem vs. AFL++, fewer modes/integration options |
Start with your target & constraints.
Have source and a callable parser/decoder? Use libFuzzer.
Fuzzing a whole program or binary-only target? Use AFL++.
Need a customizable, scalable pipeline? Use LibAFL.
Want a lean fork-server with solid performance? Use Honggfuzz.
Rule of thumb: libFuzzer for fast, source-instrumented libraries, AFL++ for programs & binaries (QEMU/Frida as needed), LibAFL for advanced/bespoke pipelines, Honggfuzz for a straightforward fork-server.
The harness is the adapter between the fuzzer and your code under test. For libFuzzer, you implement:
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Validate a bit, then call into the parser/decoder.
// Always deterministic, no global state, always return 0.
return 0;
}
How to Create a Harness (Step-by-Step)
clang++ -O1 -g \
-fsanitize=address,undefined -fno-omit-frame-pointer \
-fsanitize=fuzzer \
target_fuzz.cc -o target_fuzz
Let's put this into practice by building fuzzing harnesses for Android-used libraries. We'll focus on two real vulnerabilities: libpng (CVE-2019-7317, use-after-free) and libwebp (CVE-2023-4863, heap overflow).
The use-after-free occurs because the png_image_free_function is incorrectly called under png_safe_execute. This can cause the program to reuse memory that has already been freed, leading to a crash.
# 1.6.36 is vulnerable. (Fix came in 1.6.37.)
curl -L -o v1.6.36.tar.gz https://download.sourceforge.net/libpng/libpng-1.6.36.tar.gz
tar xf v1.6.36.tar.gz && cd libpng-1.6.36
# Build static lib with sanitizers for host
CFLAGS="-O1 -g -fsanitize=fuzzer-no-link,address,undefined -fno-omit-frame-pointer" \
./configure --enable-static --disable-shared
make -j
cd ..
We're using libFuzzer as our fuzzer of choice because it runs in-process (in-memory) and is fast. To build a good harness, we first need to understand how to call the target APIs correctly, what a valid call looks like, required initialization, and cleanup. So we start by reading the libpng documentation and selecting the right functions to drive from our harness (see: libpng manual: https://www.libpng.org/pub/png/libpng-manual.txt).
We include the png header files to access the required png functions to set up the harness.
#include "png.h"
#include "pnglibconf.h"
To use libFuzzer, our harness must implement its entry point.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
This is the standard libFuzzer hook. The in-process fuzzer calls this function many times with mutated inputs.
To guard against oversized inputs, we check the mutated buffer's length:
if (size < 8 || size > (1<<20)) return 0;
if (png_sig_cmp(const_cast<png_bytep>(data), 0, 8)) return 0;
const_cast is used because png_sig_cmp expects a non-const pointer type. (It does not modify the bytes.)png_image img;
memset(&img, 0, sizeof img);
img.version = PNG_IMAGE_VERSION;
Zero-init is required; version must be set per the simplified API contract.
if (!png_image_begin_read_from_memory(&img, data, size)) return 0;
img.width, img.height, and other fields become available.if (img.width > 4096 || img.height > 4096) {
png_image_free(&img);
return 0;
}
img.format = PNG_FORMAT_RGBA;
size_t out_size = PNG_IMAGE_SIZE(img);
if (out_size == 0 || out_size > (1<<24)) { // ~16MB
png_image_free(&img);
return 0;
}
void* buffer = malloc(out_size);
if (!buffer) { png_image_free(&img); return 0; }
Caller-allocated buffer keeps control over memory use (vs. letting the library allocate huge blocks).
(void)png_image_finish_read(&img, nullptr, buffer, 0, nullptr);
free(buffer);
png_image_free(&img);
return 0;
To prevent per-iteration leaks, ensure cleanup runs on every path.
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <cstdlib>
#include "png.h"
#include "pnglibconf.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size < 8 || size > (1<<20)) return 0;
if (png_sig_cmp(const_cast<png_bytep>(data), 0, 8)) return 0;
png_image img;
memset(&img, 0, sizeof img);
img.version = PNG_IMAGE_VERSION;
if (!png_image_begin_read_from_memory(&img, data, size)) return 0;
if (img.width > 4096 || img.height > 4096) {
png_image_free(&img);
return 0;
}
img.format = PNG_FORMAT_RGBA;
size_t out_size = PNG_IMAGE_SIZE(img);
if (out_size == 0 || out_size > (1<<24)) { png_image_free(&img);
return 0;
}
void* buffer = malloc(out_size);
if (!buffer) {
png_image_free(&img);
return 0;
}
// Tries to fully decode; libpng will traverse multiple code paths
(void)png_image_finish_read(&img, nullptr, buffer, 0, nullptr);
free(buffer);
png_image_free(&img);
return 0;
}
clang++ -g -O0 -fsanitize=fuzzer,address png_fuzz.cc -o png_fuzz -I libpng-1.6.36/ libpng-1.6.36/.libs/libpng16.a -lz
You can copy the PNG corpus files from the libpng directory and run the fuzzer.
#create directory for seed files
mkdir seed
cp libpng-1.6.36/*.png seed/
./png_fuzz in/ seed/
Within a minute, the fuzzer should crash and ASan will produce a report.
You'll find the crash file in the same directory as your harness. Use it for deeper analysis, e.g. building an exploit or PoC, and you can reproduce the crash by running the fuzzer directly on that file.
The bug lives in the lossless WebP (VP8L) decoder, specifically in how it builds Huffman coding tables. A maliciously crafted WebP can cause BuildHuffmanTable to construct tables larger than the allocated buffer, triggering a heap buffer overflow. (See the WebP API docs: http://developers.google.com/speed/webp/docs/api)
To build the fuzzing harness, we consult the libwebp documentation to choose the right decode APIs and call sequence — what a valid invocation looks like, the required initialization (WebPDecoderConfig, colorspace), incremental vs. one-shot decode, and proper cleanup (WebPFreeDecBuffer, WebPIDelete). This ensures the harness drives the vulnerable code paths deterministically while staying stable and leak-free.
curl -L -o libwebp-1.3.1.tar.gz https://storage.googleapis.com/downloads.webmproject.org/releases/webp/libwebp-1.3.1.tar.gz
tar xf libwebp-1.3.1.tar.gz && cd libwebp-1.3.1
CFLAGS="-O1 -g -fsanitize=fuzzer-no-link,address,undefined -fno-omit-frame-pointer" \
./configure --enable-static --disable-shared
make -j
cd ..
#include "webp/decode.h"
webp/decode.h exposes the decoder (incremental + one-shot) and config structs.
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
The function libFuzzer repeatedly calls with mutated inputs (in-process).
WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config)) return 0;
config.output.colorspace = MODE_rgbA; // premultiplied RGBA
config.options.use_threads = 0; // no threads deterministic
It initializes the decoder configuration with safe defaults, forces a specific colorspace so the decoder exercises real pixel pipelines, and disables threading to avoid nondeterministic races or masking crashes.
size_t mid = size / 2; if (mid < 4) mid = 4;
WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
if (!idec) { WebPFreeDecBuffer(&config.output); return 0; }
The code first computes a halfway point in the input (mid = size / 2, clamped to at least 4 bytes) so the initial chunk isn't small. It then creates an incremental decoder with WebPIDecode(NULL, 0, &config) to consume the data in chunks. If the decoder allocation fails, it frees the output buffer stored in config and returns immediately.
VP8StatusCode st = WebPIUpdate(idec, data, mid);
if (st == VP8_STATUS_OUT_OF_MEMORY || st == VP8_STATUS_USER_ABORT) {
WebPIDelete(idec);
WebPFreeDecBuffer(&config.output);
return 0;
}
The harness feeds the first half of the input to the incremental decoder with WebPIUpdate(idec, data, mid), which typically parses the RIFF/WEBP header and discovers basic features. If this call returns VP8_STATUS_OUT_OF_MEMORY or VP8_STATUS_USER_ABORT, it cleans up by deleting the decoder and freeing the output buffer in config, then returns immediately to abort the current iteration.
(void)WebPIUpdate(idec, data + mid, size - mid);
It then feeds the second half of the input with WebPIUpdate(idec, data + mid, size - mid) to continue the bitstream, which drives the decoder into deeper stages such as the lossless VP8L or lossy VP8 paths, including entropy/Huffman table construction, alpha processing, and other decode logic.
WebPIDelete(idec);
Prevents leaks between iterations (critical in in-process fuzzing).
(void)WebPDecode(data, size, &config);
WebPFreeDecBuffer(&config.output);
return 0;
}
The harness also exercises the one-shot decode path by calling WebPDecode(data, size, &config), which traverses slightly different glue and validation code than the incremental flow. After this call, it explicitly frees the output buffer stored in config — which libwebp allocated via WebPFreeDecBuffer(&config.output) — and then returns.
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include "webp/decode.h"
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (!data || size < 4 || size > (1 << 20)) return 0;
WebPDecoderConfig config;
if (!WebPInitDecoderConfig(&config)) return 0;
config.output.colorspace = MODE_rgbA; // (premultiplied RGBA)
config.options.use_threads = 0;
size_t mid = size / 2; if (mid < 4) mid = 4;
WebPIDecoder* idec = WebPIDecode(NULL, 0, &config);
if (!idec) { WebPFreeDecBuffer(&config.output); return 0; }
VP8StatusCode st = WebPIUpdate(idec, data, mid);
if (st == VP8_STATUS_OUT_OF_MEMORY || st == VP8_STATUS_USER_ABORT) {
WebPIDelete(idec);
WebPFreeDecBuffer(&config.output);
return 0;
}
(void)WebPIUpdate(idec, data + mid, size - mid);
int last_y = 0, w = 0, h = 0, stride = 0;
(void)WebPIDecGetRGB(idec, &last_y, &w, &h, &stride);
WebPIDelete(idec);
// one-shot path to traverse different code
(void)WebPDecode(data, size, &config);
WebPFreeDecBuffer(&config.output);
return 0;
}
clang++ -O1 -g -fsanitize=address,undefined,fuzzer \
-Ilibwebp-1.3.1 \
webp_fuzz.cc libwebp-1.3.1/src/.libs/libwebp.a -o webp_fuzz
mkdir -p in/ # directory for mutatied corpus
mkdir -p corpus_webp/ # directory for seed files
printf "RIFF\x2A\x00\x00\x00WEBPVP8L" > corpus_webp/seed_vp8l.webp
wget https://storage.googleapis.com/downloads.webmproject.org/webp/testdata/fuzzer/fuzz_seed_corpus.zip
unzip fuzz_seed_corpus.zip -d corpus_webp/
./webp_fuzz -timeout=10 in/ corpus_webp/
After 8–10 hours running the fuzzer, we got the crash.
While fuzzing, we hit frequent OOMs and slow-path timeouts. Our initial generic WebP harness did reach the Huffman code path, but crashes were slow to surface. So we needed to refine it into a VP8L-focused libFuzzer harness that adds strict checks on mutated inputs and tight resource caps. This version is designed for stability while reliably driving the lossless WebP (VP8L) Huffman path — the area implicated in CVE-2023-4863.
You can run the fuzzer with flags like -ignore_ooms=1 (and even -ignore_timeouts=1) to keep the campaign moving past crashes and hangs, but it's not a silver bullet. Some mutated images can drive resident memory (RSS) so high that the process still runs out of memory despite the ignore setting. In practice, you'll want to combine those flags with tighter resource limits — e.g., -rss_limit_mb, -max_len, and dimension/pixel caps in the harness — to prevent pathological inputs from blowing up memory in the first place.
WebPGetFeatures: quickly probes width/height/format, then clamps dimensions/pixel count.WebPDecodeRGBAInto (small, fixed size) to prevent huge internal allocations.#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <vector>
#include <algorithm>
#include "webp/decode.h"
static inline uint32_t rd32le(const uint8_t* p) {
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* d, size_t n) {
if (!d || n < 20 || n > (1u << 20)) return 0; // 1 MiB cap
// Minimal RIFF/WEBP header gate
if (memcmp(d, "RIFF", 4) != 0) return 0;
if (n < 12 || memcmp(d + 8, "WEBP", 4) != 0) return 0;
// Scan chunks; require VP8L, and clamp per-chunk size
bool seen_vp8l = false;
const size_t MAX_CHUNK = 32 * 1024; // keep chunks small to avoid OOM/slow paths
size_t off = 12;
while (off + 8 <= n) {
const uint8_t* h = d + off;
uint32_t sz = rd32le(h + 4);
const char tag[5] = { (char)h[0], (char)h[1], (char)h[2], (char)h[3], 0 };
// sanity: declared size must fit in file; also cap it
if (off + 8ull + sz > n) return 0;
if (sz > MAX_CHUNK) return 0;
if (memcmp(tag, "VP8L", 4) == 0) seen_vp8l = true;
// chunks are padded to even size
size_t adv = 8 + ((sz + 1) & ~1u);
if (adv == 0) return 0; // overflow guard
off += adv;
}
if (!seen_vp8l) return 0;
// Cheap feature probe (discovers width/height/format)
WebPBitstreamFeatures feat;
if (WebPGetFeatures(d, n, &feat) != VP8_STATUS_OK) return 0;
// We want lossless path (format == 2). Still allow unknown; the decoder will check.
if (feat.format != 2 /*lossless*/ && feat.format != 0 /*undefined*/) return 0;
// Clamp pixel count to a tiny output to avoid big work; still drives Huffman code build.
const int W = std::min(feat.width > 0 ? feat.width : 256, 256);
const int H = std::min(feat.height > 0 ? feat.height : 256, 256);
if (W <= 0 || H <= 0) return 0;
const size_t stride = (size_t)W * 4;
const size_t out_sz = stride * (size_t)H;
if (out_sz == 0 || out_sz > (1u << 22)) return 0; // ~4 MiB cap
std::vector<uint8_t> buf(out_sz, 0xCC);
// Decode into caller buffer (prevents internal huge allocs)
(void)WebPDecodeRGBAInto(d, n, buf.data(), out_sz, (int)stride);
return 0;
}
clang++ -O1 -g -fsanitize=address,undefined,fuzzer \
-Ilibwebp-1.3.1 \
webp_fuzz.cc libwebp-1.3.1/src/.libs/libwebp.a -o webp_fuzz
./webp_fuzz -timeout=10 in/ corpus_webp/
With the refined harness, execution was smoother and consistently reached the lossless (VP8L) code path without OOMs or timeouts. We were also able to reproduce the crash within 2–3 hours.
In this blog post, we started with a simple goal: explain what fuzzing is, the main types, and then apply fuzzing to Android-relevant libraries and turn it into a repeatable workflow that surfaces real bugs. Using libFuzzer with ASan/UBSan, we built two harnesses that balance depth and stability: a straightforward decoder harness for libpng (reproducing the CVE-2019-7317 use-after-free on 1.6.36) and a refined, VP8L-focused harness for libwebp (triggering the CVE-2023-4863 Huffman overflow on 1.3.1). Along the way, we handled the practical realities of image-codec fuzzing — OOMs, slow units, and timeouts — by adding size/dimension caps, caller-allocated buffers, and input pre-parsing (RIFF/VP8L checks).
The result is more than two PoCs: it's a portable methodology you can apply to other native libraries used in Android apps. Pick a vulnerable or high-risk parser, study its public API, design a deterministic, leak-free harness that exercises deep code paths, and add sensible validations so the fuzzer explores productively instead of drowning in pathological inputs. We also showed how to reproduce deterministically and minimize crashing inputs. With these patterns in hand, you can iterate across additional file formats and media stacks common on Android — confident your fuzzing will be fast, stable, and capable of rediscovering (and preventing) real-world vulnerabilities.
Now that we've covered fuzzing with source available, do you want to learn advanced binary-only fuzzing — where you don't have the source, you pull a library from a mobile device, reverse it to find target functions, and fuzz it by emulating with QEMU or fuzzing on-device with Frida mode — and then move on to exploitation?
Check out our courses that cover full chain exploitation:
Android Userland Fuzzing and Exploitation
https://www.mobilehackinglab.com/courses/android-fuzzing-exploitation
Android Kernel Fuzzing and Exploitation
https://www.mobilehackinglab.com/courses/kernel-fuzzing
Get both together with a special bundle sale with 60% discount!
https://www.mobilehackinglab.com/bundles/fuzzing-kernel
Want to learn to chain AppSec bugs to get remote code execution?
Advanced Android Hacking - Road to Pwn2Own
https://www.mobilehackinglab.com/courses/advanced-android-hacking