File size: 7,688 Bytes
cf36d98 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | #!/bin/bash -eu
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
################################################################################
# Fuzzer runner. Appends .options arguments and seed corpus to users args.
# Usage: $0 <fuzzer_name> <fuzzer_args>
sysctl -w vm.mmap_rnd_bits=28
export PATH=$OUT:$PATH
cd $OUT
DEBUGGER=${DEBUGGER:-}
FUZZER=$1
shift
# This env var is set by CIFuzz. CIFuzz fills this directory with the corpus
# from ClusterFuzz.
CORPUS_DIR=${CORPUS_DIR:-}
if [ -z "$CORPUS_DIR" ]
then
CORPUS_DIR="/tmp/${FUZZER}_corpus"
rm -rf $CORPUS_DIR && mkdir -p $CORPUS_DIR
fi
SANITIZER=${SANITIZER:-}
if [ -z $SANITIZER ]; then
# If $SANITIZER is not specified (e.g. calling from `reproduce` command), it
# is not important and can be set to any value.
SANITIZER="default"
fi
if [[ "$RUN_FUZZER_MODE" = interactive ]]; then
FUZZER_OUT="$OUT/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out"
else
FUZZER_OUT="/tmp/${FUZZER}_${FUZZING_ENGINE}_${SANITIZER}_out"
fi
function get_dictionary() {
local options_file="$FUZZER.options"
local dict_file="$FUZZER.dict"
local dict=""
if [[ -f "$options_file" ]]; then
dict=$(sed -n 's/^\s*dict\s*=\s*\(.*\)/\1/p' "$options_file" | tail -1)
fi
if [[ -z "$dict" && -f "$dict_file" ]]; then
dict="$dict_file"
fi
[[ -z "$dict" ]] && return
if [[ "$FUZZING_ENGINE" = "libfuzzer" ]]; then
printf -- "-dict=%s" "$dict"
elif [[ "$FUZZING_ENGINE" = "afl" ]]; then
printf -- "-x %s" "$dict"
elif [[ "$FUZZING_ENGINE" = "honggfuzz" ]]; then
printf -- "--dict %s" "$dict"
elif [[ "$FUZZING_ENGINE" = "centipede" ]]; then
printf -- "--dictionary %s" "$dict"
else
printf "Unexpected FUZZING_ENGINE: $FUZZING_ENGINE, ignoring\n" >&2
fi
}
function get_extra_binaries() {
[[ "$FUZZING_ENGINE" != "centipede" ]] && return
extra_binaries="$OUT/__centipede_${SANITIZER}/${FUZZER}"
if compgen -G "$extra_binaries" >> /dev/null; then
printf -- "--extra_binaries %s" \""$extra_binaries\""
fi
}
rm -rf $FUZZER_OUT && mkdir -p $FUZZER_OUT
SEED_CORPUS="${FUZZER}_seed_corpus.zip"
# TODO: Investigate why this code block is skipped
# by all default fuzzers in bad_build_check.
# They all set SKIP_SEED_CORPUS=1.
if [ -f $SEED_CORPUS ] && [ -z ${SKIP_SEED_CORPUS:-} ]; then
echo "Using seed corpus: $SEED_CORPUS"
unzip -o -d ${CORPUS_DIR}/ $SEED_CORPUS > /dev/null
fi
OPTIONS_FILE="${FUZZER}.options"
CUSTOM_LIBFUZZER_OPTIONS=""
if [ -f $OPTIONS_FILE ]; then
custom_asan_options=$(parse_options.py $OPTIONS_FILE asan)
if [ ! -z $custom_asan_options ]; then
export ASAN_OPTIONS="$ASAN_OPTIONS:$custom_asan_options"
fi
custom_msan_options=$(parse_options.py $OPTIONS_FILE msan)
if [ ! -z $custom_msan_options ]; then
export MSAN_OPTIONS="$MSAN_OPTIONS:$custom_msan_options"
fi
custom_ubsan_options=$(parse_options.py $OPTIONS_FILE ubsan)
if [ ! -z $custom_ubsan_options ]; then
export UBSAN_OPTIONS="$UBSAN_OPTIONS:$custom_ubsan_options"
fi
CUSTOM_LIBFUZZER_OPTIONS=$(parse_options.py $OPTIONS_FILE libfuzzer)
fi
if [[ "$FUZZING_ENGINE" = afl ]]; then
# Set afl++ environment options.
export ASAN_OPTIONS="$ASAN_OPTIONS:abort_on_error=1:symbolize=0:detect_odr_violation=0:"
export MSAN_OPTIONS="$MSAN_OPTIONS:exit_code=86:symbolize=0"
export UBSAN_OPTIONS="$UBSAN_OPTIONS:symbolize=0"
export AFL_I_DONT_CARE_ABOUT_MISSING_CRASHES=1
export AFL_SKIP_CPUFREQ=1
export AFL_TRY_AFFINITY=1
export AFL_FAST_CAL=1
export AFL_CMPLOG_ONLY_NEW=1
export AFL_FORKSRV_INIT_TMOUT=30000
export AFL_IGNORE_PROBLEMS=1
export AFL_IGNORE_UNKNOWN_ENVS=1
# If $OUT/afl_cmplog.txt is present this means the target was compiled for
# CMPLOG. So we have to add the proper parameters to afl-fuzz.
test -e "$OUT/afl_cmplog.txt" && AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -c $OUT/$FUZZER"
# If $OUT/afl++.dict we load it as a dictionary for afl-fuzz.
test -e "$OUT/afl++.dict" && AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -x $OUT/afl++.dict"
# Ensure timeout is a bit larger than 1sec as some of the OSS-Fuzz fuzzers
# are slower than this.
AFL_FUZZER_ARGS="$AFL_FUZZER_ARGS -t 5000+"
# AFL expects at least 1 file in the input dir.
echo input > ${CORPUS_DIR}/input
CMD_LINE="$OUT/afl-fuzz $AFL_FUZZER_ARGS -i $CORPUS_DIR -o $FUZZER_OUT $(get_dictionary) $* -- $OUT/$FUZZER"
echo afl++ setup:
env|grep AFL_
cat "$OUT/afl_options.txt"
elif [[ "$FUZZING_ENGINE" = honggfuzz ]]; then
# Honggfuzz expects at least 1 file in the input dir.
echo input > $CORPUS_DIR/input
# --exit_upon_crash: exit whith a first crash seen
# -V: verify crashes
# -R (report): save report file to this location
# -W (working dir): where the crashes go
# -v (verbose): don't use VTE UI, just stderr
# -z: use software-instrumentation of clang (trace-pc-guard....)
# -P: use persistent mode of fuzzing (i.e. LLVMFuzzerTestOneInput)
# -f: location of the initial (and destination) file corpus
# -n: number of fuzzing threads (and processes)
CMD_LINE="$OUT/honggfuzz -n 1 --exit_upon_crash -V -R /tmp/${FUZZER}_honggfuzz.report -W $FUZZER_OUT -v -z -P -f \"$CORPUS_DIR\" $(get_dictionary) $* -- \"$OUT/$FUZZER\""
if [[ $(LC_ALL=C grep -P "\x01_LIBHFUZZ_NETDRIVER_BINARY_SIGNATURE_\x02\xFF" "$FUZZER" ) ]]; then
# Honggfuzz Netdriver port. This must match the port in Clusterfuzz.
export HFND_TCP_PORT=8666
fi
elif [[ "$FUZZING_ENGINE" = centipede ]]; then
# Create the work and corpus directory for Centipede.
CENTIPEDE_WORKDIR="${CENTIPEDE_WORKDIR:-$OUT}"
# Centipede only saves crashes to crashes/ in workdir.
rm -rf $FUZZER_OUT
# --workdir: Dir that stores corpus&features in Centipede's own format.
# --corpus_dir: Location of the initial (and destination) file corpus.
# --fork_server: Execute the target(s) via the fork server.
# Run in fork mode to continue fuzzing indefinitely in case of
# OOMs, timeouts, and crashes.
# --exit_on_crash=0: Do not exit on crash.
# --timeout=1200: The process that executes target binary will abort
# if an input runs >= 1200s.
# --rss_limit_mb=0: Disable RSS limit.
# --address_space_limit_mb=0: No address space limit.
# --binary: The target binary under test without sanitizer.
# --extra_binary: The target binaries under test with sanitizers.
CMD_LINE="$OUT/centipede --workdir=$CENTIPEDE_WORKDIR --corpus_dir=\"$CORPUS_DIR\" --fork_server=1 --exit_on_crash=1 --timeout=1200 --rss_limit_mb=4096 --address_space_limit_mb=5120 $(get_dictionary) --binary=\"$OUT/${FUZZER}\" $(get_extra_binaries) $*"
else
CMD_LINE="$OUT/$FUZZER $FUZZER_ARGS $*"
if [ -z ${SKIP_SEED_CORPUS:-} ]; then
CMD_LINE="$CMD_LINE $CORPUS_DIR"
fi
if [[ ! -z ${CUSTOM_LIBFUZZER_OPTIONS} ]]; then
CMD_LINE="$CMD_LINE $CUSTOM_LIBFUZZER_OPTIONS"
fi
if [[ ! "$CMD_LINE" =~ "-dict=" ]]; then
if [ -f "$FUZZER.dict" ]; then
CMD_LINE="$CMD_LINE -dict=$FUZZER.dict"
fi
fi
CMD_LINE="$CMD_LINE < /dev/null"
fi
echo $CMD_LINE
# Unset OUT so the fuzz target can't rely on it.
unset OUT
if [ ! -z "$DEBUGGER" ]; then
CMD_LINE="$DEBUGGER $CMD_LINE"
fi
bash -c "$CMD_LINE"
|