|
| 1 | +import time |
| 2 | +import subprocess |
| 3 | +from argparse import ArgumentParser |
| 4 | +from enum import Enum, auto |
| 5 | +from dataclasses import dataclass |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | + |
| 9 | +class MsgType(Enum): |
| 10 | + """Type of libfuzzer log message""" |
| 11 | + |
| 12 | + READ = auto() |
| 13 | + INITED = auto() |
| 14 | + NEW = auto() |
| 15 | + REDUCE = auto() |
| 16 | + pulse = auto() |
| 17 | + DONE = auto() |
| 18 | + RELOAD = auto() |
| 19 | + |
| 20 | + |
| 21 | +@dataclass(repr=True) |
| 22 | +class Msg: |
| 23 | + """Libfuzzer log message contents""" |
| 24 | + |
| 25 | + ty: MsgType |
| 26 | + n_inputs: int |
| 27 | + cov: int | None |
| 28 | + ft: int | None |
| 29 | + |
| 30 | + |
| 31 | +# See libfuzzer output format: https://llvm.org/docs/LibFuzzer.html#output |
| 32 | +def parse_line(line: str) -> Msg | None: |
| 33 | + """Parse libfuzzer log message""" |
| 34 | + |
| 35 | + line = line.split() |
| 36 | + |
| 37 | + msg_ty = None |
| 38 | + for ty in MsgType: |
| 39 | + if ty.name in line: |
| 40 | + msg_ty = ty |
| 41 | + break |
| 42 | + if not msg_ty: |
| 43 | + return None |
| 44 | + |
| 45 | + n_inputs = 0 |
| 46 | + if line[0][0] == "#": |
| 47 | + n_inputs = int(line[0][1:]) |
| 48 | + |
| 49 | + cov = None |
| 50 | + if "cov:" in line: |
| 51 | + cov_i = line.index("cov:") |
| 52 | + cov = int(line[cov_i + 1]) |
| 53 | + |
| 54 | + ft = None |
| 55 | + if "ft:" in line: |
| 56 | + ft_i = line.index("ft:") |
| 57 | + ft = int(line[ft_i + 1]) |
| 58 | + |
| 59 | + return Msg(msg_ty, n_inputs, cov, ft) |
| 60 | + |
| 61 | + |
| 62 | +class Supervisor: |
| 63 | + """A wrapper to run the fuzzer until stopping criteria are satisfied |
| 64 | + or an error is encountered""" |
| 65 | + |
| 66 | + def __init__( |
| 67 | + self, fuzzer_name: str, libfuzzer_log: Path, corpus_dir: Path, wait_2x_cov: bool |
| 68 | + ): |
| 69 | + self.fuzzer_name = fuzzer_name |
| 70 | + self.libfuzzer_log = libfuzzer_log |
| 71 | + self.corpus_dir = corpus_dir |
| 72 | + self.wait_2x_cov = wait_2x_cov |
| 73 | + self.start_time = time.monotonic() |
| 74 | + self.init_cov = None |
| 75 | + self.latest_cov = None |
| 76 | + self.init_ft = None |
| 77 | + self.latest_ft = None |
| 78 | + self.latest_n_inputs = 0 |
| 79 | + self.latest_cov_inc = self.start_time |
| 80 | + |
| 81 | + def criteria_satisfied(self) -> bool: |
| 82 | + """Indicates whether stopping criteria of the corresponding fuzzer |
| 83 | + are satisfied. E.g. it was running long enough and covered enough paths.""" |
| 84 | + |
| 85 | + # Coverage increased at least twice in comparison with corpus |
| 86 | + cov = False |
| 87 | + if self.init_cov: |
| 88 | + cov = self.latest_cov >= self.init_cov * 2 |
| 89 | + |
| 90 | + # Number of unique paths (features) increased at least twice |
| 91 | + # in comparison with corpus |
| 92 | + ft = False |
| 93 | + if self.init_ft: |
| 94 | + ft = self.latest_ft >= self.init_ft * 2 |
| 95 | + |
| 96 | + # At least 100_000 fuzzer test runs |
| 97 | + n_inputs = self.latest_n_inputs > 100_000 |
| 98 | + |
| 99 | + # Last coverage increase was detected 2 hours ago |
| 100 | + cov_inc = (time.monotonic() - self.latest_cov_inc) > 2 * 60 * 60 |
| 101 | + |
| 102 | + return (cov or ft or not self.wait_2x_cov) and n_inputs and cov_inc |
| 103 | + |
| 104 | + def update_stats(self, msg: Msg): |
| 105 | + self.latest_n_inputs = msg.n_inputs |
| 106 | + if msg.cov: |
| 107 | + if self.latest_cov and msg.cov > self.latest_cov: |
| 108 | + self.latest_cov_inc = time.monotonic() |
| 109 | + self.latest_cov = msg.cov |
| 110 | + if msg.ft: |
| 111 | + self.latest_ft = msg.ft |
| 112 | + |
| 113 | + if msg.ty == MsgType.INITED: |
| 114 | + self.init_cov = msg.cov |
| 115 | + self.init_ft = msg.ft |
| 116 | + |
| 117 | + def run(self): |
| 118 | + """Run the corresponding fuzzer until either stopping criteria are |
| 119 | + satisfied or fuzzing fails with an error indicating that some problem |
| 120 | + was detected.""" |
| 121 | + |
| 122 | + print(f"Running {self.fuzzer_name}") |
| 123 | + proc = subprocess.Popen( |
| 124 | + [ |
| 125 | + f"./test/fuzz/{self.fuzzer_name}_fuzzer", |
| 126 | + # Read from both corpus folders, write only to the first |
| 127 | + self.corpus_dir.resolve(), |
| 128 | + f"test/static/corpus/{self.fuzzer_name}", |
| 129 | + ], |
| 130 | + stdout=subprocess.PIPE, |
| 131 | + stderr=subprocess.PIPE, |
| 132 | + text=True, |
| 133 | + ) |
| 134 | + |
| 135 | + with self.libfuzzer_log.open("w") as libfuzzer_log: |
| 136 | + for line in proc.stderr: |
| 137 | + msg = parse_line(line) |
| 138 | + # Add timestamp in seconds relative to the starting time of a fuzzer |
| 139 | + line = f"{time.monotonic() - self.start_time}s {line}" |
| 140 | + libfuzzer_log.write(line) |
| 141 | + # if it is not a conventional log message print and continue |
| 142 | + if not msg: |
| 143 | + print(line.strip("\n"), end="\r\n") |
| 144 | + continue |
| 145 | + |
| 146 | + # On pulse print the current state |
| 147 | + if msg.ty == MsgType.pulse: |
| 148 | + print(vars(self), end="\r\n") |
| 149 | + |
| 150 | + self.update_stats(msg) |
| 151 | + if self.criteria_satisfied(): |
| 152 | + print(f"Fuzzer {self.fuzzer_name} satisfied criteria") |
| 153 | + proc.terminate() |
| 154 | + try: |
| 155 | + proc.wait(timeout=15) |
| 156 | + except subprocess.TimeoutExpired: |
| 157 | + proc.kill() |
| 158 | + return |
| 159 | + |
| 160 | + outs, errs = proc.communicate(timeout=15) |
| 161 | + print(outs) |
| 162 | + print(errs) |
| 163 | + code = proc.poll() |
| 164 | + if code != 0 and (not self.criteria_satisfied()): |
| 165 | + raise Exception( |
| 166 | + f"Fuzzer {self.fuzzer_name} stopped without satisfying criteria. Exit code: {code}" |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | +# The script takes the fuzzing target name as the first argument. |
| 171 | +# Then it runs the fuzzing target until either stopping criteria are satisfied |
| 172 | +# or fuzzer detects a bug and fails. |
| 173 | +# Fuzzers should be built first. See README.md for instructions. |
| 174 | +if __name__ == "__main__": |
| 175 | + parser = ArgumentParser( |
| 176 | + prog="Fuzzer Supervisor", |
| 177 | + description="Runs a supplied fuzzer target until either stopping criteria\ |
| 178 | + are satisfied or fuzzer detects a bug and fails", |
| 179 | + ) |
| 180 | + parser.add_argument("fuzzer_target_name") |
| 181 | + parser.add_argument( |
| 182 | + "--libfuzzer-log", |
| 183 | + required=True, |
| 184 | + type=Path, |
| 185 | + help="File where libfuzzer log will be written", |
| 186 | + ) |
| 187 | + parser.add_argument( |
| 188 | + "--corpus-dir", |
| 189 | + required=True, |
| 190 | + type=Path, |
| 191 | + help="Directory where libuzzer generated corpus will be stored", |
| 192 | + ) |
| 193 | + parser.add_argument( |
| 194 | + "--wait-2x-cov", |
| 195 | + action="store_true", |
| 196 | + help="Enables 'coverage increased at least twice' criteria", |
| 197 | + ) |
| 198 | + args = parser.parse_args() |
| 199 | + Supervisor( |
| 200 | + args.fuzzer_target_name, args.libfuzzer_log, args.corpus_dir, args.wait_2x_cov |
| 201 | + ).run() |
0 commit comments