cfd3e08e11f1
make the native provenance gate an allow-list over every packaged library (v2.17.33)
- time
- 2026-09-11 04:48 UTC · 2 months ago
- machine
- koh2
- agent
- deepseek/deepseek-v4-flash
- session
- assessment findings remediation
- parent
- 31faca9201f2
↓ peach-android-cfd3e08e.tar.gzatlas at this save
files changed3 files
scripts/check-native-provenance.py+96 −18scripts/kepr-build-apk.sh+9 −5scripts/test-native-provenance.sh+35 −2scripts/check-native-provenance.py+96 −18
@@ -1,17 +1,41 @@11 #!/usr/bin/env python32-"""Check shipped native-library bytes against inventory and provenance documentation."""2+"""Check the native libraries a release ships against a checked-in allow-list.33 4+The inventory is keyed by PACKAGED path — `lib/<abi>/<name>` — so it covers every5+native library in the artifact, not only the ones committed under jniLibs. A6+library that arrives through a dependency (AAR/prefab) never appears in the source7+tree, which is why the packaged artifact is checked directly.8+9+Controls:10+ 1. Allow-list. Every native library in the source set and in the packaged APK11+ must have an inventory record, and every record for a shipped ABI must be12+ present. A newly added .so is refused until it is recorded and reviewed.13+ 2. Integrity. Each record's digest must match the bytes: the committed source14+ file where one exists, otherwise the packaged entry.15+ 3. Provenance. The Noise core records must match the authoritative cohort in16+ docs/NOISE-CORE-PROVENANCE.md.17+18+Usage: check-native-provenance.py [release.apk]19+"""20+421 from hashlib import sha256522 from pathlib import Path623 import re724 import sys25+import zipfile826 927 ROOT = Path(__file__).resolve().parents[1]1028 INVENTORY = ROOT / "docs" / "android-native-provenance-inventory.txt"1129 PROVENANCE = ROOT / "docs" / "NOISE-CORE-PROVENANCE.md"30+JNI_LIBS = ROOT / "app" / "src" / "main" / "jniLibs"31+NOISE_CORE = "libpeach_noise_core.so"1232 1333 14-def digest(path: Path) -> str:34+def digest_bytes(data: bytes) -> str:35+ return sha256(data).hexdigest()36+37+38+def digest_file(path: Path) -> str:1539 hasher = sha256()1640 with path.open("rb") as stream:1741 for chunk in iter(lambda: stream.read(1024 * 1024), b""):@@ -24,35 +48,89 @@2448 raise SystemExit(f"checked-in native inventory not found: {INVENTORY}")2549 records = {}2650 for line in INVENTORY.read_text().splitlines():27- if not line.strip():51+ if not line.strip() or line.startswith("#"):2852 continue2953 try:3054 path, expected = line.split("|", 1)3155 except ValueError:3256 raise SystemExit(f"invalid native inventory line: {line}")33- records[path] = expected57+ records[path.strip()] = expected.strip()3458 return records3559 3660 61+def abi_of(packaged_path: str) -> str:62+ return Path(packaged_path).parent.name63+64+3765 records = inventory_records()3866 if not records:3967 raise SystemExit("native inventory is empty")40-for relative, expected in records.items():41- path = ROOT / relative68+69+# ── 1a. Source set: every committed library must be recorded, and match. ─────70+if not JNI_LIBS.is_dir():71+ raise SystemExit(f"native source set not found: {JNI_LIBS}")72+source_set = {}73+for path in sorted(JNI_LIBS.rglob("*.so")):4274 if not path.is_file():43- raise SystemExit(f"native library not found: {relative}")44- actual = digest(path)45- if actual != expected:46- raise SystemExit(f"native library digest mismatch for {relative}: recorded {expected}, actual {actual}")75+ continue76+ packaged = f"lib/{path.parent.name}/{path.name}"77+ source_set[packaged] = digest_file(path)4778 48-text = PROVENANCE.read_text()49-for relative, expected in records.items():50- abi = Path(relative).parent.name51- match = re.search(rf"^- `{re.escape(abi)}`: `([0-9a-f]+)`$", text, re.MULTILINE)79+unrecorded_source = sorted(set(source_set) - set(records))80+if unrecorded_source:81+ raise SystemExit(82+ "native source set contains unrecorded libraries: " + ", ".join(unrecorded_source)83+ )84+for packaged, actual in source_set.items():85+ if records[packaged] != actual:86+ raise SystemExit(87+ f"committed native library digest mismatch for {packaged}: "88+ f"recorded {records[packaged]}, actual {actual}"89+ )90+91+# ── 1b/2b. Packaged artifact: the shipped set for each ABI must equal the92+# recorded set for that ABI, and every packaged entry must match its record. ──93+if len(sys.argv) > 1:94+ apk = Path(sys.argv[1])95+ if not apk.is_file():96+ raise SystemExit(f"release APK not found: {apk}")97+ with zipfile.ZipFile(apk) as archive:98+ packaged = {99+ name: digest_bytes(archive.read(name))100+ for name in archive.namelist()101+ if name.startswith("lib/") and name.endswith(".so")102+ }103+ if not packaged:104+ raise SystemExit("release APK contains no native libraries")105+ shipped_abis = {abi_of(name) for name in packaged}106+ expected = {name for name in records if abi_of(name) in shipped_abis}107+ unrecorded = sorted(set(packaged) - set(records))108+ missing = sorted(expected - set(packaged))109+ if unrecorded or missing:110+ raise SystemExit(111+ "packaged native libraries do not match the inventory: "112+ f"packaged but unrecorded {unrecorded}; recorded but absent {missing}"113+ )114+ for name, actual in packaged.items():115+ if records[name] != actual:116+ raise SystemExit(117+ f"packaged native library digest mismatch for {name}: "118+ f"recorded {records[name]}, actual {actual}"119+ )120+121+# ── 3. Provenance document agrees with the Noise core records. ──────────────122+provenance = PROVENANCE.read_text()123+for packaged, expected in records.items():124+ if Path(packaged).name != NOISE_CORE:125+ continue126+ abi = abi_of(packaged)127+ match = re.search(rf"^- `{re.escape(abi)}`: `([0-9a-f]+)`$", provenance, re.MULTILINE)52128 if match is None:53129 raise SystemExit(f"provenance document has no authoritative hash for {abi}")54- documented = match.group(1)55- if documented != expected:56- raise SystemExit(f"provenance digest mismatch for {relative}: recorded {expected}, documented {documented}")130+ if match.group(1) != expected:131+ raise SystemExit(132+ f"provenance digest mismatch for {packaged}: "133+ f"recorded {expected}, documented {match.group(1)}"134+ )57135 58-print(f"PASS: native provenance verified ({len(records)} libraries)")136+print(f"PASS: native provenance verified ({len(records)} packaged libraries)")scripts/kepr-build-apk.sh+9 −5
@@ -55,11 +55,6 @@5555 echo "refusing to publish: release manifest inventory drift" >&25656 exit 15757 }58-python3 "$ROOT_DIR/scripts/check-native-provenance.py" ||59- {60- echo "refusing to publish: native provenance drift" >&261- exit 162- }6358 6459 case "${KEPR_TARGET:-}" in6560 android-arm64)@@ -86,6 +81,15 @@8681 echo "refusing to publish: Gradle output metadata was not produced" >&28782 exit 18883 fi84+85+# PB-26-123: the inventory is an allow-list of native libraries. It is checked86+# against the source set AND against the packaged APK, so a library that87+# arrived only through a dependency is refused too.88+python3 "$ROOT_DIR/scripts/check-native-provenance.py" "$APK" ||89+ {90+ echo "refusing to publish: native provenance drift" >&291+ exit 192+ }8993 9094 BADGING_FILE=$(mktemp "${TMPDIR:-/tmp}/peach-apk-badging.XXXXXX")9195 cleanup_badging() {scripts/test-native-provenance.sh+35 −2
@@ -1,5 +1,6 @@11 #!/bin/sh2-# Hermetic negative-path test for native-library provenance drift.2+# Hermetic negative-path tests for the native-library provenance allow-list.3+# No Android SDK, Gradle, or built APK is required.34 set -eu45 56 SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)@@ -17,10 +18,42 @@1718 # to sit in a `scripts/` directory of the temporary tree, not at its root.1819 cp "$CHECK" "$TEMP_ROOT/scripts/check-native-provenance.py"1920 21+# Positive control: the genuine tree passes.2022 python3 "$TEMP_ROOT/scripts/check-native-provenance.py" || exit 123+24+# An unrecorded library in the source set is refused even though every recorded25+# digest still matches (PB-26-123): the inventory is an allow-list.26+: > "$TEMP_ROOT/app/src/main/jniLibs/arm64-v8a/libextra.so"27+if python3 "$TEMP_ROOT/scripts/check-native-provenance.py" >/dev/null 2>&1; then28+ echo "FAIL: an unrecorded native library was accepted" >&229+ exit 130+fi31+trash "$TEMP_ROOT/app/src/main/jniLibs/arm64-v8a/libextra.so"32+python3 "$TEMP_ROOT/scripts/check-native-provenance.py" || exit 133+34+# A library that reached the artifact only through a dependency never appears35+# under jniLibs, so the packaged APK is checked directly. Here the recorded set36+# is present and correct; only the extra entry must refuse.37+python3 - "$TEMP_ROOT/fake.apk" \38+ "$TEMP_ROOT/app/src/main/jniLibs/arm64-v8a/libpeach_noise_core.so" <<'PY'39+import sys, zipfile40+apk, noise_core = sys.argv[1], sys.argv[2]41+with open(noise_core, "rb") as stream:42+ payload = stream.read()43+with zipfile.ZipFile(apk, "w") as archive:44+ archive.writestr("lib/arm64-v8a/libpeach_noise_core.so", payload)45+ archive.writestr("lib/arm64-v8a/libdependency.so", b"unreviewed")46+PY47+if python3 "$TEMP_ROOT/scripts/check-native-provenance.py" "$TEMP_ROOT/fake.apk" >/dev/null 2>&1; then48+ echo "FAIL: an unrecorded packaged native library was accepted" >&249+ exit 150+fi51+52+# A recorded library whose bytes changed is refused.2153 printf '\n' >> "$TEMP_ROOT/app/src/main/jniLibs/arm64-v8a/libpeach_noise_core.so"2254 if python3 "$TEMP_ROOT/scripts/check-native-provenance.py" >/dev/null 2>&1; then2355 echo "FAIL: mutated native library was accepted" >&22456 exit 12557 fi26-printf 'PASS: native provenance drift gate\n'58+59+printf 'PASS: native provenance allow-list and drift gate\n'