- OCaml 87%
- C 8%
- Raku 5%
crow #
A campaign runner for AFL fuzzing of OCaml Crowbar targets.
AFL (american fuzzy lop) is a fuzzer: it mutates input files, runs a binary compiled with branch instrumentation on each one, and keeps an input when it drives the program through a combination of branches, a path, that no earlier input reached. Crowbar is an OCaml library that turns a property test into a program AFL can drive: the test reads its random choices from AFL's input bytes, so a mutated input is a new test case.
A monorepo with dozens of fuzz targets cannot give each one its own
afl-fuzz process for days: there are more targets than cores, and most
targets stop finding new paths after a few hours. crow runs one campaign
over all of them. It finds the fuzz executables declared in the workspace,
builds them with AFL instrumentation, and rotates them over the available
cores in fixed time slices. At the start of each slice a scoring rule (crow's
Scheduler) ranks the targets and the top --cpus of them run. A target never
fuzzed scores 1000. Any other scores 10 per path found so far, plus 100 times
its paths per second of AFL run time, plus 50 per crash; a fourth term, meant to
add 100 for a target that found paths in the last five minutes, compares the
wall clock with a duration and never applies. A new target therefore runs first,
and afterwards the targets with the most paths and crashes keep their cores. It
reads each AFL instance's fuzzer_stats back into one status table and lists
the crashes and hangs of every target.
Discovery looks for a fuzz/dune file, in any directory of the workspace
but a hidden, build, vendored or cram-test one, that lists the crowbar or
the alcobar library. A Crowbar target reads its input on standard input, an
Alcobar one from the file named last on its command line.
Install #
$ opam install crow
If opam cannot find the package, add the overlay repository first:
$ opam repo add samoht https://tangled.org/gazagnaire.org/opam-overlay.git
$ opam update
$ opam install crow
Usage #
$ crow init # add the afl context to dune-workspace
$ crow list # list the fuzz targets
$ crow start --cpus=8 --slice=15m --duration=24h
$ crow status # speed, executions, paths, crashes
$ crow crashes # crashing inputs, per target
$ crow hangs # inputs that timed out
$ crow repro ID # rerun a crash or hang
$ crow stop
$ crow check --budget=40m --cpus=8 ocaml-cbor # the fuzz gate's verdict
crow start takes the number of cores (--cpus, 4 by default), the length of
a rotation slice (--slice, required, such as 15m), an optional total
duration (--duration, with a suffix s, m, h or d), a comma-separated
list of --targets (all of them by default), and the --output directory
(_fuzz by default). The output directory holds one directory per target and
one per AFL instance below it.
AFL++ built with shm_open, as every macOS build is, hands its target the
coverage map as a descriptor, which the OCaml runtime's AFL support cannot
read. crow carries a small preload library that maps the descriptor for it,
writes it into the output directory and names it first in AFL_PRELOAD for
every AFL++ tool it starts, so a campaign needs no environment of its own.
A process the target starts inherits the map's variables but may not hold its
descriptor, so it runs without the map rather than abort.
crow start holds its campaign while it runs: it locks campaign.lock in the
output directory and records itself in campaign.json there. crow stop
sends SIGTERM to that process and to no other, and crow start then stops the
instances it started and exits. A second crow start on the same output
directory refuses while the first runs.
Check #
crow check PACKAGE... is a fuzz gate: one bounded campaign over the
campaigns of the given package directories, with a verdict. A package's
campaigns are the targets under its directory and under no nested package
(a directory holding an opam file), each an Alcobar runner that writes its
seeds with --gen-corpus DIR. The design is OSS-Fuzz's batch fuzzing: one
scheduler shares --cpus cores among every campaign for --budget, and each
campaign's corpus is kept from run to run, under --state
($XDG_CACHE_HOME/crow by default), with the inputs that crashed it, both
named by the BLAKE2b digest of their contents.
A run asks afl-showmap to trace the first campaign, and stops when AFL cannot
read its coverage map. Each campaign's seeds are written by a copy of its
runner in an empty directory, so a runner that reads a file beside it fails
here rather than under AFL; afl-cmin prunes them with the kept corpus
against this build, and the kept corpus becomes what it keeps. The campaign
then runs as crow start does, in slices of the budget over twice the rounds
that visit every campaign once, at most 300 s. Every kept crash is replayed
on this build in a fresh process and grouped by crash state, how the replay
ended and the last line it printed.
Each failure is a case of its package, printed on standard error as
PACKAGE: CASE: a campaign whose seeds could not be prepared, one that ran
no execution, each crash state (named after the first input kept for it, with
the command that replays it), the crashes AFL saved that no fresh-process
replay reproduces (a crash that needs the state earlier inputs left in a
persistent-mode process), and a campaign that fuzzed under half its cores'
budget. The last line counts the cases; crow check exits 1 when there is
one, 2 when the campaign could not run, and 0 otherwise.
Workspace #
AFL needs binaries built with -afl-instrument, which crow builds in an afl
context of the dune-workspace file. crow init creates the file, or adds the
context to an existing one:
(context
(default
(name afl)
(profile afl)))
(env
(afl
(ocamlopt_flags (:standard -afl-instrument))))
Library #
let run () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
let fs = Eio.Stdenv.fs env in
let process_mgr = Eio.Stdenv.process_mgr env in
let clock = Eio.Stdenv.clock env in
let stdout = Eio.Stdenv.stdout env in
let stderr = Eio.Stdenv.stderr env in
let verbose = false in
let targets = Crow.Target.discover ~fs in
(match
Crow.build_targets ~sw ~process_mgr ~stdout ~stderr ~verbose targets
with
| Ok () -> ()
| Error errs ->
Fmt.epr "%d build error(s)@." (List.length errs);
exit 1);
let campaign, processes =
Crow.start_campaign ~sw ~process_mgr ~fs ~stdout ~stderr ~verbose ~cpus:8 ()
in
Fmt.pr "Started %d AFL instance(s) in %s@." (List.length processes)
(Crow.Campaign.output_dir campaign);
let status = Crow.status ~fs () in
Fmt.pr "%a@." Crow.pp_target_table status;
Crow.Afl.stop_all ~clock processes
License #
ISC. See LICENSE.md.