From fc62400157246a6076d2bd78670178acdf830e2a Mon Sep 17 00:00:00 2001 From: Codex Date: Tue, 3 Nov 2026 14:35:42 -0600 Subject: [PATCH] Use a pinned BusyBox source mirror Try the Buildroot release mirror after a failed primary download, retaining the existing SHA256, bounded attempts and verified-cache behavior. Keep the new timeout, corruption and production-call controls in regular CI. Fixes #41. --- .github/workflows/ci.yml | 4 +- qemu/build-busybox.sh | 3 +- qemu/lib.sh | 9 ++- qemu/tests/busybox-source-mirror.sh | 92 +++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100755 qemu/tests/busybox-source-mirror.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c884531..3033e64 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -237,7 +237,9 @@ jobs: - name: QEMU busybox fetch regression tests # Retry on a bad sha256, the cached copy, and a server that answers # and then stalls (#1588), against local listeners: no network. - run: bash qemu/tests/busybox-fetch.sh + run: | + bash qemu/tests/busybox-fetch.sh + bash qemu/tests/busybox-source-mirror.sh - name: unpack-buildroot regression tests # Fresh unpack, leave-existing-tree-alone, and every refusal path # (unpatched selectors, bad tarball, half-present tree, failed diff --git a/qemu/build-busybox.sh b/qemu/build-busybox.sh index 01d5add..a8e5f6b 100644 --- a/qemu/build-busybox.sh +++ b/qemu/build-busybox.sh @@ -121,7 +121,8 @@ bb_main() { rm -f "$bin.inputs" tar="${BB_SRC_TARBALL:-$out/busybox-${BB_SRC_VER}.tar.bz2}" - qemu_fetch_verified "$BB_SRC_URL" "$BB_SRC_SHA256" "$tar" "busybox ${BB_SRC_VER} source" + qemu_fetch_verified "$BB_SRC_URL" "$BB_SRC_SHA256" "$tar" "busybox ${BB_SRC_VER} source" \ + "$BB_SRC_MIRROR_URL" scratch="$(mktemp -d "${TMPDIR:-/tmp}/warden-busybox.XXXXXX")" # Expanded now on purpose: the trap must remove this run's directory. diff --git a/qemu/lib.sh b/qemu/lib.sh index cff57f2..27be7f3 100644 --- a/qemu/lib.sh +++ b/qemu/lib.sh @@ -11,6 +11,9 @@ BB_SRC_VER=1.37.0 # Read by build-busybox.sh, which sources this file. # shellcheck disable=SC2034 BB_SRC_URL="https://busybox.net/downloads/busybox-${BB_SRC_VER}.tar.bz2" +# Buildroot mirrors this exact pinned release; verification is unchanged. +# shellcheck disable=SC2034 +BB_SRC_MIRROR_URL="https://sources.buildroot.net/busybox/busybox-${BB_SRC_VER}.tar.bz2" BB_SRC_SHA256=3311dff32e746499f4df0d5df04d7eb396382d7e108bb9250e7b519b837043a4 qemu_log() { printf '\033[36m== %s\033[0m\n' "$*"; } @@ -18,9 +21,10 @@ qemu_log() { printf '\033[36m== %s\033[0m\n' "$*"; } # Download URL to DEST and verify it against the sha256 WANT. A DEST that # already matches is kept. FAILS CLOSED: three bounded attempts, then exit 1; # a mismatching download never lands at DEST. -# Usage: qemu_fetch_verified URL WANT DEST LABEL +# Usage: qemu_fetch_verified URL WANT DEST LABEL [MIRROR_URL] qemu_fetch_verified() { local url="$1" want="$2" dest="$3" label="$4" got + local mirror="${5:-}" if [ -f "$dest" ]; then got="$(sha256sum "$dest" | awk '{print $1}')" if [ "$want" = "$got" ]; then @@ -40,6 +44,9 @@ qemu_fetch_verified() { local attempt tmp="${dest}.download.$$" rm -f "$tmp" for attempt in 1 2 3; do + if [ "$attempt" -gt 1 ] && [ -n "$mirror" ]; then + url="$mirror" + fi qemu_log "downloading $url (attempt $attempt/3)" if curl --retry 1 --retry-delay "$delay" --retry-connrefused \ --connect-timeout "$stall" --speed-limit 1024 --speed-time "$stall" \ diff --git a/qemu/tests/busybox-source-mirror.sh b/qemu/tests/busybox-source-mirror.sh new file mode 100755 index 0000000..f965182 --- /dev/null +++ b/qemu/tests/busybox-source-mirror.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# The sourced downloader runs before the later production-call test stub. +# shellcheck disable=SC2218 +set -euo pipefail + +TEST_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +QEMU_DIR="$(cd "$TEST_DIR/.." && pwd)" +SCRATCH="$(mktemp -d "${TMPDIR:-/tmp}/busybox-source-mirror.XXXXXX")" +trap 'rm -rf "$SCRATCH"' EXIT + +mkdir -p "$SCRATCH/bin" +printf 'pinned source fixture' > "$SCRATCH/good" +WANT="$(sha256sum "$SCRATCH/good" | awk '{print $1}')" +export MIRROR_LOG="$SCRATCH/requests" MIRROR_GOOD="$SCRATCH/good" +export MIRROR_MODE=good +cat > "$SCRATCH/bin/curl" <<'CURL' +#!/usr/bin/env bash +set -euo pipefail +url= out= +while [ "$#" -gt 0 ]; do + case "$1" in + -o) shift; out="$1" ;; + https://*) url="$1" ;; + esac + shift +done +printf '%s\n' "$url" >> "$MIRROR_LOG" +[ -n "$out" ] || exit 2 +case "$url" in + https://primary.invalid/source) exit 28 ;; + https://mirror.invalid/source) + if [ "$MIRROR_MODE" = good ]; then cp "$MIRROR_GOOD" "$out"; + else printf 'corrupt mirror response' > "$out"; fi ;; + *) exit 2 ;; +esac +CURL +chmod 0755 "$SCRATCH/bin/curl" +export PATH="$SCRATCH/bin:$PATH" +# shellcheck source=qemu/lib.sh disable=SC1091 +source "$QEMU_DIR/lib.sh" + +qemu_fetch_verified https://primary.invalid/source "$WANT" \ + "$SCRATCH/fetched" source https://mirror.invalid/source +cmp "$SCRATCH/good" "$SCRATCH/fetched" +printf '%s\n' https://primary.invalid/source https://mirror.invalid/source > "$SCRATCH/expected" +cmp "$MIRROR_LOG" "$SCRATCH/expected" +echo 'PASS primary timeout uses verified mirror' + +: > "$MIRROR_LOG" +qemu_fetch_verified https://primary.invalid/source "$WANT" \ + "$SCRATCH/fetched" source https://mirror.invalid/source +test ! -s "$MIRROR_LOG" +echo 'PASS verified cache avoids both sources' + +export MIRROR_MODE=bad +: > "$MIRROR_LOG" +if (qemu_fetch_verified https://primary.invalid/source "$WANT" \ + "$SCRATCH/refused" source https://mirror.invalid/source) > "$SCRATCH/refusal" 2>&1; then + echo 'FAIL corrupt mirror was accepted' >&2 + exit 1 +fi +test "$(wc -l < "$MIRROR_LOG")" -eq 3 +test ! -e "$SCRATCH/refused" +test -z "$(find "$SCRATCH" -maxdepth 1 -name 'refused.download.*' -print -quit)" +grep -q 'after 3 verified attempts' "$SCRATCH/refusal" +echo 'PASS corrupt mirror refused within three attempts' + +# Exercise the production source call with the downloader intercepted only +# after build-busybox.sh loads its constants. Refuse before extraction. +# shellcheck source=qemu/build-busybox.sh disable=SC1091 +source "$QEMU_DIR/build-busybox.sh" +mkdir -p "$SCRATCH/toolchain" +printf '#!/bin/sh\nexit 0\n' > "$SCRATCH/toolchain/fake-gcc" +chmod 0755 "$SCRATCH/toolchain/fake-gcc" +qemu_fetch_verified() { + [ "$1" = "$BB_SRC_URL" ] && [ "$2" = "$BB_SRC_SHA256" ] + [ "$5" = "$BB_SRC_MIRROR_URL" ] + printf '%s\n' 'production mirror call verified' > "$SCRATCH/wired" + return 42 +} +set +e +(set -e; OUT="$SCRATCH/build" CROSS_COMPILE="$SCRATCH/toolchain/fake-" JOBS=1 bb_main) \ + > "$SCRATCH/wiring-log" 2>&1 +status=$? +set -e +if [ "$status" -ne 42 ]; then + cat "$SCRATCH/wiring-log" >&2 + echo 'FAIL production source call did not stop at the verified downloader' >&2 + exit 1 +fi +grep -qx 'production mirror call verified' "$SCRATCH/wired" +echo 'PASS production source fetch supplies pinned mirror'