1*2d543d20SAndroid Build Coastguard Worker#!/bin/bash 2*2d543d20SAndroid Build Coastguard Worker 3*2d543d20SAndroid Build Coastguard Worker# The script is used to build the fuzz targets run on ClusterFuzz. It has to be 4*2d543d20SAndroid Build Coastguard Worker# compatible with the "build.sh" script described at 5*2d543d20SAndroid Build Coastguard Worker# https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh 6*2d543d20SAndroid Build Coastguard Worker# More precisely, it should use environment variables like OUT, LIB_FUZZING_ENGINE 7*2d543d20SAndroid Build Coastguard Worker# and so on (https://google.github.io/oss-fuzz/getting-started/new-project-guide/#buildsh-script-environment), 8*2d543d20SAndroid Build Coastguard Worker# and the fuzz targets have to be linked with $CXX even though the project is written 9*2d543d20SAndroid Build Coastguard Worker# in C: https://google.github.io/oss-fuzz/getting-started/new-project-guide/#Requirements 10*2d543d20SAndroid Build Coastguard Worker 11*2d543d20SAndroid Build Coastguard Worker# To make it easier to build the fuzz targets locally, the script can also work in "local" 12*2d543d20SAndroid Build Coastguard Worker# mode. To run secilc-fuzzer against a test case (named, say, CRASH) triggering an issue 13*2d543d20SAndroid Build Coastguard Worker# the following commands should be run 14*2d543d20SAndroid Build Coastguard Worker# 15*2d543d20SAndroid Build Coastguard Worker# $ ./scripts/oss-fuzz.sh 16*2d543d20SAndroid Build Coastguard Worker# $ ./out/secilc-fuzzer CRASH 17*2d543d20SAndroid Build Coastguard Worker 18*2d543d20SAndroid Build Coastguard Worker# To run the fuzzer against the corpus OSS-Fuzz has accumulated so far it should be 19*2d543d20SAndroid Build Coastguard Worker# downloaded, unpacked and passed to the fuzzer: 20*2d543d20SAndroid Build Coastguard Worker# 21*2d543d20SAndroid Build Coastguard Worker# $ wget https://storage.googleapis.com/selinux-backup.clusterfuzz-external.appspot.com/corpus/libFuzzer/selinux_secilc-fuzzer/public.zip 22*2d543d20SAndroid Build Coastguard Worker# $ unzip -d CORPUS public.zip 23*2d543d20SAndroid Build Coastguard Worker# $ ./out/secilc-fuzzer CORPUS/ 24*2d543d20SAndroid Build Coastguard Worker 25*2d543d20SAndroid Build Coastguard Workerset -eux 26*2d543d20SAndroid Build Coastguard Worker 27*2d543d20SAndroid Build Coastguard Workercd "$(dirname -- "$0")/.." 28*2d543d20SAndroid Build Coastguard Worker 29*2d543d20SAndroid Build Coastguard Workerexport DESTDIR=${DESTDIR:-$(pwd)/DESTDIR} 30*2d543d20SAndroid Build Coastguard Worker 31*2d543d20SAndroid Build Coastguard WorkerSANITIZER=${SANITIZER:-address} 32*2d543d20SAndroid Build Coastguard Workerflags="-O1 -fno-omit-frame-pointer -gline-tables-only -DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -fsanitize=$SANITIZER -fsanitize=fuzzer-no-link" 33*2d543d20SAndroid Build Coastguard Worker 34*2d543d20SAndroid Build Coastguard Workerexport CC=${CC:-clang} 35*2d543d20SAndroid Build Coastguard Workerexport CFLAGS="${CFLAGS:-$flags} -I$DESTDIR/usr/include -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64" 36*2d543d20SAndroid Build Coastguard Worker 37*2d543d20SAndroid Build Coastguard Workerexport CXX=${CXX:-clang++} 38*2d543d20SAndroid Build Coastguard Workerexport CXXFLAGS=${CXXFLAGS:-$flags} 39*2d543d20SAndroid Build Coastguard Worker 40*2d543d20SAndroid Build Coastguard Workerexport OUT=${OUT:-$(pwd)/out} 41*2d543d20SAndroid Build Coastguard Workermkdir -p "$OUT" 42*2d543d20SAndroid Build Coastguard Worker 43*2d543d20SAndroid Build Coastguard Workerexport LIB_FUZZING_ENGINE=${LIB_FUZZING_ENGINE:--fsanitize=fuzzer} 44*2d543d20SAndroid Build Coastguard Worker 45*2d543d20SAndroid Build Coastguard Workerrm -rf "$DESTDIR" 46*2d543d20SAndroid Build Coastguard Workermake -C libsepol clean 47*2d543d20SAndroid Build Coastguard Worker# LIBSO and LIBMAP shouldn't be expanded here because their values are unknown until Makefile 48*2d543d20SAndroid Build Coastguard Worker# has been read by make 49*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2016 50*2d543d20SAndroid Build Coastguard Workermake -C libsepol V=1 LD_SONAME_FLAGS='-soname,$(LIBSO),--version-script=$(LIBMAP)' -j"$(nproc)" install 51*2d543d20SAndroid Build Coastguard Worker 52*2d543d20SAndroid Build Coastguard Worker## secilc fuzzer ## 53*2d543d20SAndroid Build Coastguard Worker 54*2d543d20SAndroid Build Coastguard Worker# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by 55*2d543d20SAndroid Build Coastguard Worker# the compiler/linker so they shouldn't be quoted 56*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 57*2d543d20SAndroid Build Coastguard Worker$CC $CFLAGS -c -o secilc-fuzzer.o libsepol/fuzz/secilc-fuzzer.c 58*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 59*2d543d20SAndroid Build Coastguard Worker$CXX $CXXFLAGS $LIB_FUZZING_ENGINE secilc-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/secilc-fuzzer" 60*2d543d20SAndroid Build Coastguard Worker 61*2d543d20SAndroid Build Coastguard Workerzip -r "$OUT/secilc-fuzzer_seed_corpus.zip" secilc/test 62*2d543d20SAndroid Build Coastguard Worker 63*2d543d20SAndroid Build Coastguard Worker## binary policy fuzzer ## 64*2d543d20SAndroid Build Coastguard Worker 65*2d543d20SAndroid Build Coastguard Worker# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by 66*2d543d20SAndroid Build Coastguard Worker# the compiler/linker so they shouldn't be quoted 67*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 68*2d543d20SAndroid Build Coastguard Worker$CC $CFLAGS -c -o binpolicy-fuzzer.o libsepol/fuzz/binpolicy-fuzzer.c 69*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 70*2d543d20SAndroid Build Coastguard Worker$CXX $CXXFLAGS $LIB_FUZZING_ENGINE binpolicy-fuzzer.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/binpolicy-fuzzer" 71*2d543d20SAndroid Build Coastguard Worker 72*2d543d20SAndroid Build Coastguard Workerzip -j "$OUT/binpolicy-fuzzer_seed_corpus.zip" libsepol/fuzz/policy.bin 73*2d543d20SAndroid Build Coastguard Worker 74*2d543d20SAndroid Build Coastguard Worker## checkpolicy fuzzer ## 75*2d543d20SAndroid Build Coastguard Worker 76*2d543d20SAndroid Build Coastguard Workermake -C checkpolicy clean 77*2d543d20SAndroid Build Coastguard Workermake -C checkpolicy V=1 -j"$(nproc)" checkobjects 78*2d543d20SAndroid Build Coastguard Worker# CFLAGS, CXXFLAGS and LIB_FUZZING_ENGINE have to be split to be accepted by 79*2d543d20SAndroid Build Coastguard Worker# the compiler/linker so they shouldn't be quoted 80*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 81*2d543d20SAndroid Build Coastguard Worker$CC $CFLAGS -Icheckpolicy/ -c -o checkpolicy-fuzzer.o checkpolicy/fuzz/checkpolicy-fuzzer.c 82*2d543d20SAndroid Build Coastguard Worker# shellcheck disable=SC2086 83*2d543d20SAndroid Build Coastguard Worker$CXX $CXXFLAGS $LIB_FUZZING_ENGINE checkpolicy-fuzzer.o checkpolicy/*.o "$DESTDIR/usr/lib/libsepol.a" -o "$OUT/checkpolicy-fuzzer" 84*2d543d20SAndroid Build Coastguard Worker 85*2d543d20SAndroid Build Coastguard Workerzip -j "$OUT/checkpolicy-fuzzer_seed_corpus.zip" checkpolicy/fuzz/min_pol.mls.conf 86*2d543d20SAndroid Build Coastguard Workercp checkpolicy/fuzz/checkpolicy-fuzzer.dict "$OUT/" 87