etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

setup.sh (5371B)

      1 #!/bin/bash
      2 # setup.sh - bootstrap libksba fuzzing environment.
      3 # Same machine specs as the libusb harness: AFL++ from source, CLASSIC+LAF
      4 # instrumentation, a CmpLog sibling per harness, ASan+UBSan+signed-overflow at -O0 -g.
      5 # Usage: bash setup.sh [--skip-deps] [--skip-afl]
      6 set -euo pipefail
      7 
      8 SCRIPT_DIR="$(cd "$(dirname "${0}")" && pwd)"
      9 cd "${SCRIPT_DIR}"
     10 
     11 AFL_REPO="https://github.com/AFLplusplus/AFLplusplus.git"
     12 AFL_BIN="${SCRIPT_DIR}/afl-build/usr/local/bin"
     13 
     14 # libksba defaults to git tip (1.8.x, reportable upstream now). For the line
     15 # dirmngr/gpgsm actually link against in distros, set LIBKSBA_TAG=libksba-1.6.6.
     16 LIBKSBA_REPO="${LIBKSBA_REPO:-https://github.com/gpg/libksba.git}"
     17 LIBKSBA_TAG="${LIBKSBA_TAG:-master}"
     18 
     19 # Drop signed-integer-overflow + shift from UBSan: libksba's DER decoders use the
     20 # `c = *s++ << 24` idiom (unsigned char promoted to signed int) which trips
     21 # shift-base for any value >= 0x80 << 24. Pure noise. Real OOBs still caught by ASan.
     22 CFLAGS_FUZZ="-fsanitize=address,undefined -fno-sanitize=signed-integer-overflow,shift -g -O0 -fno-omit-frame-pointer"
     23 CFLAGS_FUZZ+=" -Wno-implicit-function-declaration -Wno-unused-function -Wno-unused-variable"
     24 
     25 HARNESSES="fuzz_cert fuzz_crl fuzz_ocsp fuzz_cms"
     26 
     27 SKIP_DEPS=0
     28 SKIP_AFL=0
     29 for arg in "$@"; do
     30     [[ "$arg" == "--skip-deps" ]] && SKIP_DEPS=1
     31     [[ "$arg" == "--skip-afl"  ]] && SKIP_AFL=1
     32 done
     33 
     34 log() { echo "[*] $*"; }
     35 ok()  { echo "[+] $*"; }
     36 die() { echo "[!] $*" >&2; exit 1; }
     37 
     38 if [[ "${SKIP_DEPS}" -eq 0 ]]; then
     39     log "Installing dependencies..."
     40     sudo apt-get update -qq
     41     sudo apt-get install -y \
     42         build-essential git tmux python3 \
     43         clang llvm lld pkg-config gdb \
     44         autoconf automake libtool gettext bison texinfo \
     45         libgpg-error-dev openssl
     46     ok "Dependencies installed."
     47 fi
     48 
     49 if [[ "${SKIP_AFL}" -eq 1 ]]; then
     50     command -v afl-clang-fast > /dev/null 2>&1 || die "afl-clang-fast not found."
     51 elif [[ -x "${AFL_BIN}/afl-clang-fast" ]]; then
     52     ok "AFL++ already built."
     53     export PATH="${AFL_BIN}:${PATH}"
     54 else
     55     log "Building AFL++..."
     56     [[ -d "AFLplusplus" ]] || git clone --depth=1 "${AFL_REPO}" AFLplusplus
     57     ( cd AFLplusplus && make source-only -j"$(nproc)" \
     58         && make install PREFIX="${SCRIPT_DIR}/afl-build/usr/local" )
     59     export PATH="${AFL_BIN}:${PATH}"
     60     ok "AFL++ built."
     61 fi
     62 export PATH="${AFL_BIN}:${PATH}"
     63 
     64 if [[ -d "libksba-src/.git" ]]; then
     65     ok "libksba-src present ($(cd libksba-src && git describe --tags --always))."
     66 else
     67     log "Cloning libksba ${LIBKSBA_TAG}..."
     68     git clone "${LIBKSBA_REPO}" libksba-src
     69     ( cd libksba-src && git checkout -q "${LIBKSBA_TAG}" )
     70     ok "libksba-src cloned."
     71 fi
     72 
     73 [[ -x libksba-src/configure ]] || ( cd libksba-src && ./autogen.sh >/dev/null 2>&1 )
     74 
     75 # Out-of-tree builds keep the CLASSIC/CmpLog/plain object sets separate. Build gl
     76 # then just libksba.la: the rest of src (ber-dump, tests) needs more deps.
     77 build_lib() {
     78     local dir="$1" cc="$2"; shift 2
     79     log "  building ${dir} (CC=${cc})..."
     80     rm -rf "${dir}"
     81     mkdir -p "${dir}"
     82     ( cd "${dir}" \
     83       && env "$@" "${SCRIPT_DIR}/libksba-src/configure" \
     84             CC="${cc}" CFLAGS="${CFLAGS_FUZZ}" \
     85             --enable-static --disable-shared --disable-maintainer-mode >/dev/null \
     86       && env "$@" make -C gl  -j"$(nproc)" >/dev/null \
     87       && env "$@" make -C src libksba.la -j"$(nproc)" >/dev/null )
     88     [[ -f "${dir}/src/.libs/libksba.a" ]] || die "${dir}: libksba.a not produced"
     89 }
     90 
     91 log "Building libksba: instrumented (CLASSIC+LAF), CmpLog, and a plain-ASan copy for repro..."
     92 build_lib build-cls     afl-clang-fast AFL_LLVM_INSTRUMENT=CLASSIC AFL_LLVM_LAF_ALL=1
     93 build_lib build-cmplog  afl-clang-fast AFL_LLVM_INSTRUMENT=CLASSIC AFL_LLVM_LAF_ALL=1 AFL_LLVM_CMPLOG=1
     94 build_lib build-plain   clang
     95 ok "libksba built."
     96 
     97 GPGERR_CFLAGS="$(gpg-error-config --cflags 2>/dev/null || pkg-config --cflags gpg-error 2>/dev/null || true)"
     98 GPGERR_LIBS="$(gpg-error-config --libs 2>/dev/null || pkg-config --libs gpg-error 2>/dev/null || echo -lgpg-error)"
     99 
    100 # gnulib convenience archive libksba links against; omit gracefully if absent.
    101 gnulib() { local a="$1/gl/.libs/libgnu.a"; [[ -f "$a" ]] && echo "$a"; }
    102 CLS_LIBS="build-cls/src/.libs/libksba.a $(gnulib build-cls)"
    103 CMP_LIBS="build-cmplog/src/.libs/libksba.a $(gnulib build-cmplog)"
    104 PLAIN_LIBS="build-plain/src/.libs/libksba.a $(gnulib build-plain)"
    105 
    106 # harnesses: AFL persistent, CmpLog sibling, and a plain-clang ASan repro binary.
    107 log "Building harnesses..."
    108 for h in ${HARNESSES}; do
    109     log "  ${h}..."
    110     AFL_LLVM_INSTRUMENT=CLASSIC AFL_LLVM_LAF_ALL=1 \
    111         afl-clang-fast ${CFLAGS_FUZZ} -I build-cls/src -I libksba-src/src ${GPGERR_CFLAGS} \
    112         "${h}.c" ${CLS_LIBS} ${GPGERR_LIBS} -o "${h}"
    113     AFL_LLVM_INSTRUMENT=CLASSIC AFL_LLVM_LAF_ALL=1 AFL_LLVM_CMPLOG=1 \
    114         afl-clang-fast ${CFLAGS_FUZZ} -I build-cmplog/src -I libksba-src/src ${GPGERR_CFLAGS} \
    115         "${h}.c" ${CMP_LIBS} ${GPGERR_LIBS} -o "${h}.cmplog"
    116     clang ${CFLAGS_FUZZ} -DKSBA_FUZZ_STANDALONE -I build-plain/src -I libksba-src/src ${GPGERR_CFLAGS} \
    117         "${h}.c" ${PLAIN_LIBS} ${GPGERR_LIBS} -o "${h}.repro"
    118 done
    119 ok "Harnesses built."
    120 
    121 if [[ -d corpus/cert && "$(ls corpus/cert 2>/dev/null | wc -l)" -gt 0 ]]; then
    122     ok "Corpus present."
    123 else
    124     log "Generating corpus..."
    125     bash gen_corpus.sh
    126 fi
    127 
    128 ok "Done. Run: bash start-fuzzers.sh"