Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(smoke): assert codesign --verify rejects a tampered binary (10c)
Martin Vogel99a21a91a06a
diff --git a/scripts/smoke-test.sh b/scripts/smoke-test.shindex a6cc02060..2057aa4a9 100755--- a/scripts/smoke-test.sh+++ b/scripts/smoke-test.sh@@ -1208,33 +1208,37 @@ if [ "$(uname -s)" = "Darwin" ]; thenBIN_ARCH=$(file "$SECURITY_BIN" | grep -o 'arm64\|x86_64' | head -1)if [ "$BIN_ARCH" = "arm64" ]; then- # arm64: a SIGNED binary whose CODE has been tampered (CodeDirectory hash mismatch) must be- # SIGKILLed (exit 137 = 128+9) by AMFI before user code runs. Neither `codesign- # --remove-signature` nor garbling the signature blob triggers this — since macOS 11 a binary- # with a missing/invalid signature is ad-hoc re-signed on exec by newer macOS and RUNS (exit 0),- # which made this test flaky then consistently red on updated CI runner images. The reliable- # trigger is tampering the signed CODE while leaving the valid signature attached: the kernel- # validates the executed page against the (intact) CodeDirectory hash, finds the mismatch, and- # kills the process. Done on a SEPARATE copy so the original $SECURITY_BIN stays intact for the- # 10e re-sign test. Refs: github.com/garrytan/gstack#997, github.com/nodejs/node#40827.+ # arm64 integrity check: tampering the signed code must be DETECTED by the code signature.+ # The original check (run a tampered/unsigned binary and expect SIGKILL 137) is NOT deterministic+ # on current macOS for an ad-hoc-signed CLI binary, which is why this test went flaky then red.+ # Observed on CI:+ # - `codesign --remove-signature` then run -> macOS 11+ ad-hoc re-signs on exec and RUNS (exit 0)+ # - garbling only the signature blob then run -> same, re-signed/ignored (exit 0)+ # - tampering the code then run -> with no CS_KILL/hardened-runtime flag the kernel does NOT+ # kill the page; it executes the garbage and crashes with SIGILL (exit 132), not 137+ # (runs 28350650225 / 28354735368 / 28360363173 / 28365724001). The deterministic, meaningful+ # invariant is that `codesign --verify` REJECTS a tampered binary -- the CodeDirectory page hashes+ # no longer match the modified code -- while the untampered binary verifies cleanly (10a above).+ # This is a pure userspace hash check (no tampered code is ever executed). Done on a SEPARATE copy+ # so the original $SECURITY_BIN stays intact for the 10e re-sign test.+ # Refs: github.com/garrytan/gstack#997, github.com/nodejs/node#40827.TAMPER_BIN="${SECURITY_BIN}.tampered"cp "$SECURITY_BIN" "$TAMPER_BIN"- # Zero the entry-point instructions (LC_MAIN entryoff = start of __text) plus a span of early- # __text, leaving the Mach-O header + load commands (offset 0..entryoff) intact so the binary- # still parses. The tampered code pages no longer match their CodeDirectory hashes -> SIGKILL.+ # Tamper signed code: zero the entry-point instructions (LC_MAIN entryoff = start of __text) plus+ # a span of early __text, leaving the Mach-O header + load commands + signature blob intact so the+ # file still parses and still carries its now-stale signature for --verify to reject.ENTRY_OFF=$(otool -l "$SECURITY_BIN" 2>/dev/null | awk '/LC_MAIN/{f=1} f&&/entryoff/{print $2; exit}')ENTRY_OFF=${ENTRY_OFF:-2184}dd if=/dev/zero of="$TAMPER_BIN" bs=1 seek="$ENTRY_OFF" count=4096 conv=notrunc 2>/dev/nulldd if=/dev/zero of="$TAMPER_BIN" bs=4096 seek=4 count=512 conv=notrunc 2>/dev/null- UNSIGNED_EXIT=0- "$TAMPER_BIN" --version > /dev/null 2>&1 || UNSIGNED_EXIT=$?- rm -f "$TAMPER_BIN"- if [ "$UNSIGNED_EXIT" -eq 137 ] || [ "$UNSIGNED_EXIT" -eq 9 ]; then- echo "OK 10c: tampered-code arm64 binary killed (exit $UNSIGNED_EXIT)"- else- echo "FAIL 10c: tampered-code arm64 exit=$UNSIGNED_EXIT (expected 137)"+ if codesign --verify "$TAMPER_BIN" 2>/dev/null; then+ echo "FAIL 10c: codesign --verify ACCEPTED a tampered arm64 binary (integrity check broken)"+ rm -f "$TAMPER_BIN"exit 1+ else+ echo "OK 10c: codesign --verify rejected tampered arm64 binary"fi+ rm -f "$TAMPER_BIN"else# x86_64: signing is not enforced; an unsigned binary should still runcodesign --remove-signature "$SECURITY_BIN" 2>/dev/null || true