| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. -eu |
| 2 | |
| 3 | # Build the project |
| 4 | ./build/autogen.sh |
| 5 | ./configure |
| 6 | makescip-shell system . . make. -j$(nprocscip-shell system . . nproc.) all |
| 7 | |
| 8 | FUZZ_DIR=$SRC/libarchive/contrib/oss-fuzz |
| 9 | TEST_DIR=$SRC/libarchive/libarchive/test |
| 10 | |
| 11 | # Common libraries for linking |
| 12 | LIBS=".libs/libarchive.a -Wl,-Bstatic -lbz2 -llzo2 -lxml2 -llzma -lz -lcrypto -llz4 -licuuc -licudata -Wl,-Bdynamic" |
| 13 | |
| 14 | # Function to build a fuzzer |
| 15 | build_fuzzerscip-shell shell libarchive 3.8.7-1 build_fuzzer().() { |
| 16 | local name=$1 |
| 17 | local source=$2 |
| 18 | echoscip-shell system . . echo. "Building fuzzer: $name" |
| 19 | $CXX $CXXFLAGS -Ilibarchive \ |
| 20 | "$source" \ |
| 21 | -o "$OUT/$name" $LIB_FUZZING_ENGINE $LIBS |
| 22 | } |
| 23 | |
| 24 | # Build all format-specific fuzzers |
| 25 | FUZZERS=( |
| 26 | "libarchive_fuzzer" |
| 27 | "libarchive_tar_fuzzer" |
| 28 | "libarchive_zip_fuzzer" |
| 29 | "libarchive_7zip_fuzzer" |
| 30 | "libarchive_rar_fuzzer" |
| 31 | "libarchive_rar5_fuzzer" |
| 32 | "libarchive_xar_fuzzer" |
| 33 | "libarchive_cab_fuzzer" |
| 34 | "libarchive_lha_fuzzer" |
| 35 | "libarchive_iso9660_fuzzer" |
| 36 | "libarchive_cpio_fuzzer" |
| 37 | "libarchive_warc_fuzzer" |
| 38 | "libarchive_mtree_fuzzer" |
| 39 | "libarchive_ar_fuzzer" |
| 40 | "libarchive_filter_fuzzer" |
| 41 | "libarchive_entry_fuzzer" |
| 42 | "libarchive_write_fuzzer" |
| 43 | "libarchive_linkify_fuzzer" |
| 44 | "libarchive_match_fuzzer" |
| 45 | "libarchive_encryption_fuzzer" |
| 46 | "libarchive_read_disk_fuzzer" |
| 47 | "libarchive_write_disk_fuzzer" |
| 48 | "libarchive_seek_fuzzer" |
| 49 | "libarchive_string_fuzzer" |
| 50 | "libarchive_roundtrip_fuzzer" |
| 51 | ) |
| 52 | |
| 53 | for fuzzer in "${FUZZERS[@]}"; do |
| 54 | if [scip-shell system . . `[`. -f "$FUZZ_DIR/${fuzzer}.cc" ]; then |
| 55 | build_fuzzerscip-shell shell libarchive 3.8.7-1 build_fuzzer(). "$fuzzer" "$FUZZ_DIR/${fuzzer}.cc" |
| 56 | fi |
| 57 | done |
| 58 | |
| 59 | # Copy dictionaries and options |
| 60 | cpscip-shell system . . cp. "$FUZZ_DIR"/*.dict "$OUT/" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 61 | cpscip-shell system . . cp. "$FUZZ_DIR"/*.options "$OUT/" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 62 | |
| 63 | # Build seed corpora |
| 64 | echoscip-shell system . . echo. "Building seed corpora..." |
| 65 | |
| 66 | # Main fuzzer corpus (existing) |
| 67 | cpscip-shell system . . cp. "$FUZZ_DIR/corpus.zip" "$OUT/libarchive_fuzzer_seed_corpus.zip" |
| 68 | |
| 69 | # Function to create corpus from test files |
| 70 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus().() { |
| 71 | local name=$1 |
| 72 | local pattern=$2 |
| 73 | local dir="/tmp/${name}_corpus" |
| 74 | |
| 75 | mkdirscip-shell system . . mkdir. -p "$dir" |
| 76 | for f in $TEST_DIR/$pattern; do |
| 77 | if [scip-shell system . . `[`. -f "$f" ]; then |
| 78 | base=$(basenamescip-shell system . . basename. "$f" .uu) |
| 79 | uudecode -o "$dir/$base" "$f" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 80 | fi |
| 81 | done |
| 82 | |
| 83 | if [scip-shell system . . `[`. "$(lsscip-shell system . . ls. -A $dir 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ]; then |
| 84 | zip -j "$OUT/${name}_seed_corpus.zip" "$dir"/* 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 85 | echoscip-shell system . . echo. "Created corpus for $name with $(lsscip-shell system . . ls. $dir | wcscip-shell system . . wc. -l) files" |
| 86 | fi |
| 87 | rmscip-shell system . . rm. -rf "$dir" |
| 88 | } |
| 89 | |
| 90 | # Create format-specific corpora |
| 91 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_tar_fuzzer" "test_compat_*tar*.uu" |
| 92 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_zip_fuzzer" "test_*zip*.uu" |
| 93 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_7zip_fuzzer" "test_read_format_7zip*.uu" |
| 94 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_rar_fuzzer" "test_read_format_rar_*.uu" |
| 95 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_rar5_fuzzer" "test_read_format_rar5*.uu" |
| 96 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_xar_fuzzer" "test_read_format_xar*.uu" |
| 97 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_cab_fuzzer" "test_read_format_cab*.uu" |
| 98 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_lha_fuzzer" "test_read_format_lha*.uu" |
| 99 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_iso9660_fuzzer" "test_read_format_iso*.uu" |
| 100 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_cpio_fuzzer" "test_compat_cpio*.uu" |
| 101 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_warc_fuzzer" "test_read_format_warc*.uu" |
| 102 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_mtree_fuzzer" "test_read_format_mtree*.uu" |
| 103 | create_corpusscip-shell shell libarchive 3.8.7-1 create_corpus(). "libarchive_ar_fuzzer" "test_read_format_ar*.uu" |
| 104 | |
| 105 | # Filter corpus - use compressed test files |
| 106 | mkdirscip-shell system . . mkdir. -p /tmp/filter_corpusscip-shell filesystem . . `/tmp/filter_corpus`. |
| 107 | for f in $TEST_DIR/*.gz.uu $TEST_DIR/*.bz2.uu $TEST_DIR/*.xz.uu $TEST_DIR/*.lz4.uu $TEST_DIR/*.zst.uu $TEST_DIR/*.Z.uu; do |
| 108 | if [scip-shell system . . `[`. -f "$f" ]; then |
| 109 | base=$(basenamescip-shell system . . basename. "$f" .uu) |
| 110 | uudecode -o "/tmp/filter_corpus/$base" "$f" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 111 | fi |
| 112 | done |
| 113 | if [scip-shell system . . `[`. "$(lsscip-shell system . . ls. -A /tmp/filter_corpusscip-shell filesystem . . `/tmp/filter_corpus`. 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ]; then |
| 114 | zip -j "$OUT/libarchive_filter_fuzzer_seed_corpus.zip" /tmp/filter_corpus/* 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 115 | fi |
| 116 | rmscip-shell system . . rm. -rf /tmp/filter_corpusscip-shell filesystem . . `/tmp/filter_corpus`. |
| 117 | |
| 118 | # Encryption corpus - encrypted archives |
| 119 | mkdirscip-shell system . . mkdir. -p /tmp/encryption_corpusscip-shell filesystem . . `/tmp/encryption_corpus`. |
| 120 | for f in $TEST_DIR/*encrypt*.uu $TEST_DIR/*password*.uu; do |
| 121 | if [scip-shell system . . `[`. -f "$f" ]; then |
| 122 | base=$(basenamescip-shell system . . basename. "$f" .uu) |
| 123 | uudecode -o "/tmp/encryption_corpus/$base" "$f" 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 124 | fi |
| 125 | done |
| 126 | if [scip-shell system . . `[`. "$(lsscip-shell system . . ls. -A /tmp/encryption_corpusscip-shell filesystem . . `/tmp/encryption_corpus`. 2>/dev/nullscip-shell filesystem . . `/dev/null`.)" ]; then |
| 127 | zip -j "$OUT/libarchive_encryption_fuzzer_seed_corpus.zip" /tmp/encryption_corpus/* 2>/dev/nullscip-shell filesystem . . `/dev/null`. || truescip-shell system . . true. |
| 128 | fi |
| 129 | rmscip-shell system . . rm. -rf /tmp/encryption_corpusscip-shell filesystem . . `/tmp/encryption_corpus`. |
| 130 | |
| 131 | echoscip-shell system . . echo. "Build complete! Built ${#FUZZERS[@]} fuzzers." |