⬢ DragonFlyBSD Kernel Audit
DF-2255 / extract.sh
← back to finding ↓ download raw
#!/bin/sh
# extract.sh -- regenerate df2255_verbatim.inc from the LIVE in-guest
# /usr/src copy of the ar9300 eeprom parser.  The eight target
# functions are extracted byte-identical (sed line ranges anchored on
# exact signature lines); the sha256 of the source file is recorded in
# the generated header and in extract.log (the sha256 gate).
#
# Any verification failure prints "FAIL" and exits non-zero.
set -e

SRC=${SRC:-/usr/src/sys/contrib/dev/ath/ath_hal/ar9300/ar9300_eeprom.c}
HDR=${HDR:-/usr/src/sys/contrib/dev/ath/ath_hal/ar9300/ar9300eep.h}
OUT=${1:-df2255_verbatim.inc}
LOG=${2:-extract.log}

fail=0

note() { echo "$@" >> "$LOG"; }

hdr_check() {
    if grep -q -F -- "$1" "$HDR"; then
        note "OK   hdr: $1"
    else
        note "FAIL hdr constant not found: $1"
        fail=1
    fi
}

fn_extract() {
    sig="$1"
    start=$(grep -n -F -x -- "$sig" "$SRC" | head -1 | cut -d: -f1) || true
    if [ -z "$start" ]; then
        note "FAIL fn signature not found: $sig"
        fail=1
        return 0
    fi
    start=$((start - 1))          # include the return-type line
    end=$(awk -v s="$start" 'NR>s && /^}$/ {print NR; exit}' "$SRC")
    if [ -z "$end" ]; then
        note "FAIL fn end not found for: $sig"
        fail=1
        return 0
    fi
    note "OK   fn: $sig (lines ${start}-${end})"
    sed -n "${start},${end}p" "$SRC"
    echo ""
}

: > "$LOG"
note "== extract.sh $(date) =="
note "source : $SRC"
note "sha256 : $(sha256 -q "$SRC")"
note "header : $HDR  sha256: $(sha256 -q "$HDR")"

# DF-2255 liveness probe: is the length>=1024 guard compiled out?
if grep -q -x '#ifdef DONTUSE' "$SRC"; then
    note "NOTE  DF-2255: 'if (length >= 1024)' guard is inside #ifdef DONTUSE"
    note "NOTE  DF-2255: -> guard COMPILED OUT, no bounds check on length"
else
    note "NOTE  DF-2255: no '#ifdef DONTUSE' guard around length check (fixed?)"
fi
# DF-2256 liveness probe: are read failures ignored?
if grep -q -F '(void)ar9300_calibration_data_read(ah, address - it, buffer + it, 1);' "$SRC"; then
    note "NOTE  DF-2256: read_array casts read result to (void) -> failures ignored"
else
    note "NOTE  DF-2256: read failure handling present (fixed?)"
fi

{
    echo "/* df2255_verbatim.inc -- GENERATED by extract.sh, do not edit."
    echo " * byte-identical extraction from:"
    echo " *   $SRC (sha256 $(sha256 -q "$SRC"))"
    echo " *   $HDR (sha256 $(sha256 -q "$HDR"))"
    echo " * generated: $(date)"
    echo " */"
    echo ""
    fn_extract "ar9300_eeprom_struct_default_find_by_id(int id) "
    fn_extract "ar9300_calibration_data_read_eeprom(struct ath_hal *ah, long address,"
    fn_extract "ar9300_calibration_data_read(struct ath_hal *ah, long address,"
    fn_extract "ar9300_calibration_data_read_array(struct ath_hal *ah, int address,"
    fn_extract "ar9300_compression_checksum(u_int8_t *data, int dsize)"
    fn_extract "ar9300_compression_header_unpack(u_int8_t *best, int *code, int *reference,"
    fn_extract "ar9300_uncompress_block(struct ath_hal *ah, u_int8_t *mptr, int mdata_size,"
    fn_extract "ar9300_eeprom_restore_internal_address(struct ath_hal *ah,"
} > "$OUT"

# constants hard-coded in the harness must match the real header
hdr_check '#define MSTATE 100'
hdr_check '#define MOUTPUT 2048'
hdr_check '#define MDEFAULT 15'
hdr_check '#define MVALUE 100'
hdr_check '#define reference_current 0'
hdr_check '#define compression_header_length 4'
hdr_check '#define compression_checksum_length 2'
hdr_check '#define AR9300_EEPROM_SIZE 16*1024'
hdr_check '_compress_none = 0,'
hdr_check '_compress_block,'
hdr_check 'calibration_data_none = 0,'
hdr_check 'calibration_data_eeprom,'

note "generated sha256: $(sha256 -q "$OUT")"
note "== extract.sh done (fail=$fail) =="

cat "$LOG"
[ "$fail" = 0 ] || { echo "EXTRACTION VERIFICATION FAILED"; exit 1; }
echo "extraction OK -> $OUT"