CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tools / extract / audit-scan.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | # audit-scan.sh: secrets, private data and binaries in EVERY object of a repo's |
| 4 | # history (every blob ever reachable from any ref, plus every commit and tag |
| 5 | # message), not just the tip. |
| 6 | # |
| 7 | # tools/extract/audit-scan.sh REPO OUT_DIR |
| 8 | # |
| 9 | # Writes OUT_DIR/{secrets,private,binaries,compressed,unscanned,messages}.txt and |
| 10 | # prints counts. Rules: rules-secrets.txt (credential shapes; any hit is a |
| 11 | # finding) and rules-private.txt (IPs, hostnames, emails, home paths; reviewed by |
| 12 | # hand). Binary blobs (any NUL byte) are listed in binaries.txt AND still scanned |
| 13 | # as bytes; gzip/bzip2/xz/zstd/zip blobs are also scanned decompressed (listed in |
| 14 | # compressed.txt). Exit 1 if any secret rule matches, 2 if a compressed blob could |
| 15 | # not be decompressed (unscanned.txt): an unread blob is not a clean blob. |
| 16 | # Run tools/extract/audit-selftest.sh first: it plants one of each secret shape |
| 17 | # (including inside a binary and a gzip blob) and fails if the scanner misses any. |
| 18 | set -euo pipefail |
| 19 | export LC_ALL=C |
| 20 | |
| 21 | repo=${1:?repo} |
| 22 | out=${2:?out dir} |
| 23 | here=$(cd "$(dirname "$0")" && pwd) |
| 24 | mkdir -p "$out" |
| 25 | tmp=$(mktemp -d); trap 'rm -rf "$tmp"' EXIT |
| 26 | # grep -P takes one pattern: join the rule lines as (?:a)|(?:b)|... |
| 27 | secret_re=$(grep -v "^#" "$here/rules-secrets.txt" | grep . | sed "s/.*/(?:&)/" | paste -sd"|") |
| 28 | private_re=$(grep -v "^#" "$here/rules-private.txt" | grep . | sed "s/.*/(?:&)/" | paste -sd"|") |
| 29 | |
| 30 | git -C "$repo" rev-list --objects --all > "$tmp/objs" |
| 31 | cut -d' ' -f1 "$tmp/objs" | git -C "$repo" cat-file --batch-check='%(objectname) %(objecttype) %(objectsize)' > "$tmp/types" |
| 32 | # path for each blob (first name it was seen under) |
| 33 | awk 'NF>=2 {sha=$1; $1=""; sub(/^ /,""); if (!(sha in p)) p[sha]=$0} END {for (s in p) print s"\t"p[s]}' "$tmp/objs" | sort > "$tmp/paths" |
| 34 | |
| 35 | : > "$out/secrets.txt"; : > "$out/private.txt"; : > "$out/binaries.txt" |
| 36 | : > "$out/compressed.txt"; : > "$out/unscanned.txt" |
| 37 | # scan FILE LABEL: secret and private rules over the raw bytes (-a: a NUL byte |
| 38 | # must not make grep skip the file or stop matching). |
| 39 | matches() { |
| 40 | local rc=0 match_fd |
| 41 | # Open separately: a shell redirection failure must not look like grep's no-match status. |
| 42 | if ! exec {match_fd}> "$tmp/matches"; then |
| 43 | echo 'AUDIT INCOMPLETE: cannot open match output' >&2 |
| 44 | exit 2 |
| 45 | fi |
| 46 | grep "$@" >&"$match_fd" || rc=$? |
| 47 | exec {match_fd}>&- |
| 48 | if (( rc > 1 )); then |
| 49 | echo "AUDIT INCOMPLETE: grep failed with status $rc" >&2 |
| 50 | exit 2 |
| 51 | fi |
| 52 | } |
| 53 | prefix() { |
| 54 | local label=$1 line |
| 55 | while IFS= read -r line; do |
| 56 | printf '%s:%s\n' "$label" "$line" |
| 57 | done |
| 58 | } |
| 59 | scan() { |
| 60 | matches -naP -e "$secret_re" "$1" |
| 61 | tr -d '\000' < "$tmp/matches" | cut -c1-200 | prefix "$sha $2" >> "$out/secrets.txt" |
| 62 | matches -noaP -e "$private_re" "$1" |
| 63 | tr -d '\000' < "$tmp/matches" | prefix "$2" >> "$out/private.txt" |
| 64 | } |
| 65 | mkdir -p "$tmp/b" |
| 66 | nblob=0 |
| 67 | while read -r sha type size; do |
| 68 | [[ $type == blob ]] || continue |
| 69 | nblob=$((nblob + 1)) |
| 70 | path=$(awk -F'\t' -v s="$sha" '$1==s {print $2; exit}' "$tmp/paths") |
| 71 | git -C "$repo" cat-file blob "$sha" > "$tmp/b/x" |
| 72 | # Binary = contains a NUL byte. Counted with tr, not grep -P '\x00': GNU |
| 73 | # grep 3.11 under LC_ALL=C never matches NUL that way, which left this list |
| 74 | # empty on every run before 2026-10-27 (CUDApp#89). |
| 75 | nul_bytes=$(tr -dc '\000' < "$tmp/b/x" | wc -c) |
| 76 | if [[ $nul_bytes -gt 0 ]]; then |
| 77 | echo "$sha $size $path" >> "$out/binaries.txt" |
| 78 | fi |
| 79 | scan "$tmp/b/x" "$path" |
| 80 | magic=$(od -An -tx1 -N6 "$tmp/b/x" | tr -d ' \n') |
| 81 | dec= |
| 82 | case $magic in |
| 83 | 1f8b*) dec="gzip -dc" ;; |
| 84 | 425a68*) dec="bzip2 -dc" ;; |
| 85 | fd377a585a00) dec="xz -dc" ;; |
| 86 | 28b52ffd*) dec="zstd -dcq" ;; |
| 87 | 504b0304*) dec="unzip -p" ;; |
| 88 | esac |
| 89 | if [[ -n $dec ]]; then |
| 90 | echo "$sha $size $path ${dec%% *}" >> "$out/compressed.txt" |
| 91 | # tar members inside come out concatenated; the byte scan covers them. |
| 92 | if $dec "$tmp/b/x" > "$tmp/b/z" 2>/dev/null; then |
| 93 | scan "$tmp/b/z" "$path(${dec%% *})" |
| 94 | else |
| 95 | echo "$sha $size $path ${dec%% *}" >> "$out/unscanned.txt" |
| 96 | fi |
| 97 | fi |
| 98 | done < "$tmp/types" |
| 99 | |
| 100 | # commit and tag messages (and identities, for the record) |
| 101 | git -C "$repo" log --all --format='%H%n%B' > "$tmp/msgs" |
| 102 | git -C "$repo" for-each-ref refs/tags --format='%(objectname)%0a%(contents)' >> "$tmp/msgs" |
| 103 | matches -nP -e "$secret_re" "$tmp/msgs" |
| 104 | cut -c1-200 < "$tmp/matches" | prefix MSG >> "$out/secrets.txt" |
| 105 | matches -noP -e "$private_re" "$tmp/msgs" |
| 106 | prefix MSG < "$tmp/matches" >> "$out/private.txt" |
| 107 | cp "$tmp/msgs" "$out/messages.txt" |
| 108 | |
| 109 | # private hits, grouped by the matched text (the reviewer reads this list) |
| 110 | cut -d: -f3- "$out/private.txt" | sort | uniq -c | sort -rn > "$out/private-summary.txt" |
| 111 | |
| 112 | echo "blobs $nblob binaries $(wc -l < "$out/binaries.txt") compressed $(wc -l < "$out/compressed.txt") (unscanned $(wc -l < "$out/unscanned.txt")) secret-hits $(wc -l < "$out/secrets.txt") private-hits $(wc -l < "$out/private.txt") ($(wc -l < "$out/private-summary.txt") distinct)" |
| 113 | [[ ! -s $out/secrets.txt ]] || exit 1 |
| 114 | [[ ! -s $out/unscanned.txt ]] || exit 2 |