Grammar-Based Fuzzing

Structure-aware fuzzing in which a grammar, protobuf schema, template or protocol model constrains generation and mutation so inputs survive early parsing and validation and reach deep logic — the answer to byte-level mutators stalling on languages, nested binary containers and stateful protocols.

How it works

  • Generative vs mutational. Generative fuzzers build each sample from scratch from the grammar; mutational grammar fuzzers mutate parse trees so outputs stay grammatical, and with coverage feedback keep samples that hit new edges (P0: mutational grammar fuzzing, The Fuzzing Book).
  • Why byte mutation fails. MP4 is an n-ary atom tree where every atom carries a 4-byte size covering its descendants; a naive insert/delete invalidates every ancestor’s size, the demuxer rejects the file early, and the probability a length-changing mutation is seen by deep parsing code drops exponentially with atom depth (Atredis: targeted MP4 fuzzers). The same plateau hits text formats under AFL’s bit-flip/arith/splice mutators (GHSL Fuzzing Taskflow).
  • Spectrum of structure. dictionaries → FuzzedDataProvider-shaped harnesses → libFuzzer custom mutators → protobuf (libprotobuf-mutator, “already close to a grammar fuzzer”) → full grammars (Nautilus, Jackalope, Fandango, syzlang) (Testing Handbook: Writing harnesses). AFL++‘s tutorial index covers the libprotobuf and Superion routes (AFL++ Tutorials).

Techniques

Partially structure-aware custom mutators

Atredis’s qtdemux fuzzers mutate one target atom inside a valid template, then walk the ancestor chain and fix sizes; a libFuzzer custom mutator repairs size/entry-count fields only 50% of the time so malformed-length paths stay reachable (Atredis). GHSL’s Fuzzing Taskflow ships per-format dictionaries and LLVMFuzzerCustomMutator C files (JSON token splicing, balanced-bracket duplication; XML, regex, PNG, TLV) and auto-adds guard magic constants to the dictionary (GHSL Fuzzing Taskflow).

Protobuf on binaries

Airbus fuzzed a closed-source X509 parser (parse_cert_buf overflow) with AFL++-QEMU persistent mode plus libprotobuf via AFL_CUSTOM_MUTATOR_LIBRARY / AFL_CUSTOM_MUTATOR_ONLY and AFL_QEMU_PERSISTENT_ADDR/_RET/_HOOK, after afl-cmin/afl-tmin corpus prep (Airbus SecLab).

Grammar engines

  • Nautilus in Go via gosentry: go test -fuzz with grammar-based inputs over existing testing.F harnesses (gosentry). LibAFL 0.16.1 improved grammar fuzzing in Frida mode (LibAFL 0.16.1).
  • Jackalope (P0) for XSLT and JIT engines; Fuzzilli is the generative JS engine fuzzer behind continuous V8 fuzzing (P0: Fuzzilli vs BigSleep).

Protocols and state

  • Spec → grammar by hand: Doyensec rewrote MS-SMB2 wire formats (e.g. smb2_ioctl_req) into syzkaller descriptions, parsed responses for TreeID/SessionID to make state transitions valid, and patched NTLMv2/credit limits and max connections = 128 out of the way — yielding a long list of ksmbd CVEs (Doyensec ksmbd; detail in Kernel Fuzzing).
  • Conversations as grammar: Fandango 1.1 specs describe both messages and inter-message constraints; it can play client or server, read replies and continue from the reached state (FTP, DNS examples) — “for a protocol, the test case is the interaction” (Fandango 1.1).
  • Block-based: boofuzz Session/s_initialize/s_string definitions with multi-stage states and checksums (CRC32, IPv4) (Mastering Boofuzz); Scapy’s fuzz() randomizes every non-computed field, e.g. send(IP(dst="t")/fuzz(UDP()/NTP(version=4)),loop=1) (Scapy usage).
  • Prefix replay: InterceptFuzz sits between AFL++ and a single-process target, records benign traffic into a prefix tree, replays a prefix to a state ID, then injects fuzzed packets — 7 new crash signatures in OpenThread, beating Concat and adapted AFLNet (InterceptFuzz). The protocol-fuzzing survey frames syntax acquisition and state as the core problems (Protocol fuzzing survey).

LLM-derived grammars and generators

  • ChatAFL extracts message grammars from RFC text with an LLM, then mutates/predicts message sequences; beats AFLNet and NSFuzz on state and code coverage in ProFuzzBench (ChatAFL).
  • G2Fuzz (USENIX Sec ‘25) has the LLM write Python input generators for non-textual formats (TIFF, MP4) — “holistic search” — and lets AFL++ do “local search”, so the LLM is called rarely; beats AFL++, Fuzztruction and FormatFuzzer on UNIFUZZ/FuzzBench, 3 CVEs (G2Fuzz). GenEvo does the same with constraint solving: 25 new vulns (GenEvo).
  • Typos (typed template fuzzing) turns seeds into typed templates via an LLM, no per-language parser; 21 input languages, 135 bugs (128 new) in compilers/runtimes (Typos).
    See LLM-Assisted Fuzzing.

Pitfalls (Project Zero)

  1. More coverage ≠ more bugs. The libxslt bug needs generate-id(document('')) — dataflow between two functions. Two corpus samples each using one function have the same union coverage as the bug, so the combining sample is never saved; chaining three+ calls gets exponentially harder. Dataflow coverage would help; no practical implementation exists.
  2. Greedy saving makes corpora near-identical. Mutated children carry most of their parent.
  3. Fix: periodically restart each worker with an empty corpus for T seconds, then merge with the server corpus for T more. On old libxslt, T=3600 and T=600 both beat long-lived sessions in unique crashes and time-to-crash; long sessions varied 5 vs 2 crashes by start conditions (P0).

Notable cases

  • GStreamer qtdemux: CVE-2025-47183 (heap OOB read), -47219, -47806/-47807/-47808 (subtitle NULL deref / stack overflow), plus CVE-2026-46469/-46470 (div-by-zero) and a PlayReady-metadata info leak CVE-2026-46471 — all after years of format-agnostic fuzzing (Atredis); a further OOB read CVE-2026-125041 came from targeted fuzzing (GStreamer CVE-2026-125041).
  • The Fuzzing Book’s grammar techniques are credited with 2,600+ bugs in Firefox/Chrome/Edge JS interpreters (The Fuzzing Book).

Sources