diff --git a/Makefile.cbm b/Makefile.cbm index 7643c33763..09eaf77aef 100644 --- a/Makefile.cbm +++ b/Makefile.cbm @@ -372,6 +372,7 @@ FOUNDATION_SRCS = \ src/foundation/subprocess.c \ src/foundation/git_env.c \ src/foundation/sha256.c \ + src/foundation/build_stamp.c \ src/foundation/secure_random.c \ src/foundation/macos_acl.c \ src/foundation/private_file_lock.c \ diff --git a/scripts/build-stamp.py b/scripts/build-stamp.py new file mode 100755 index 0000000000..cd64ef7ae4 --- /dev/null +++ b/scripts/build-stamp.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 +"""Stamp the build identity into a linked codebase-memory-mcp image (#2683). + +The build fingerprint is the SHA-256 of the image with the 32 bytes that +follow the stamp magic read as zeros (src/foundation/build_stamp.c). This +script computes that digest and writes it into those 32 bytes, so the process +can read its own fingerprint instead of hashing its whole image at startup. + +It only reads and writes bytes: the image is never executed, which keeps the +release pipeline's rule that no candidate runs before VirusTotal selection. +The image must contain the magic exactly once. Prints the digest. +""" + +from __future__ import annotations + +import hashlib +import os +import sys + +MAGIC = b"\x89CBM-BUILDSTAMP\x01" +PAYLOAD_LEN = 32 + + +def stamp_digest(data: bytearray) -> tuple[str, list[int]]: + """Zero every payload in stream order and hash; return digest and offsets.""" + payloads = [] + at = data.find(MAGIC) + while at != -1: + start = at + len(MAGIC) + data[start : start + PAYLOAD_LEN] = bytes(len(data[start : start + PAYLOAD_LEN])) + payloads.append(start) + at = data.find(MAGIC, at + 1) + return hashlib.sha256(data).hexdigest(), payloads + + +def main(argv: list[str]) -> int: + if len(argv) != 2: + print("usage: build-stamp.py ", file=sys.stderr) + return 2 + path = argv[1] + with open(path, "rb") as handle: + data = bytearray(handle.read()) + digest, payloads = stamp_digest(data) + if len(payloads) != 1 or payloads[0] + PAYLOAD_LEN > len(data): + print(f"build-stamp: {path}: expected exactly one stamp slot, found {len(payloads)}", + file=sys.stderr) + return 1 + payload = bytes.fromhex(digest) + fd = os.open(path, os.O_WRONLY | getattr(os, "O_NOFOLLOW", 0)) + try: + os.pwrite(fd, payload, payloads[0]) + os.fsync(fd) + finally: + os.close(fd) + # Re-read: the stored payload must be the digest of the stamped file. + with open(path, "rb") as handle: + stamped = bytearray(handle.read()) + stored = bytes(stamped[payloads[0] : payloads[0] + PAYLOAD_LEN]) + again, _ = stamp_digest(stamped) + if stored != payload or again != digest: + print(f"build-stamp: {path}: verification failed after writing", file=sys.stderr) + return 1 + print(digest) + return 0 + + +if __name__ == "__main__": + sys.exit(main(sys.argv)) diff --git a/scripts/build.sh b/scripts/build.sh index a8035f9cd3..b784aaeba9 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -208,4 +208,21 @@ else CFLAGS_EXTRA="$CFLAGS_EXTRA" "${EXTRA_MAKE_ARGS[@]+"${EXTRA_MAKE_ARGS[@]}"}" fi +# Step 3 (#2683): stamp the build identity into ELF executables so short-lived +# processes read it instead of hashing the whole image. The stamper only reads +# and writes bytes; it never runs the binary. Release candidates are stamped +# again after strip by scripts/ci/prepare-release-candidates.sh. +case "$(uname -s)" in +Linux | FreeBSD | NetBSD) + if command -v python3 >/dev/null 2>&1; then + python3 "$ROOT/scripts/build-stamp.py" "${BUILD_DIR}/codebase-memory-mcp" >/dev/null || { + echo "error: build-stamp failed" >&2 + exit 1 + } + else + echo "warning: python3 not found; leaving the build unstamped (it hashes itself at startup)" >&2 + fi + ;; +esac + echo "=== Build complete: ${BUILD_DIR}/codebase-memory-mcp ===" diff --git a/scripts/ci/prepare-release-candidates.sh b/scripts/ci/prepare-release-candidates.sh index 3c197c41dc..b419ef5aef 100755 --- a/scripts/ci/prepare-release-candidates.sh +++ b/scripts/ci/prepare-release-candidates.sh @@ -187,6 +187,21 @@ if ! "$STRIP_TOOL" --strip-debug "$DEBUG_STRIPPED" 2>/dev/null; then DEBUG_STRIP_INVOCATION="$(basename "$STRIP_TOOL") -S" fi +# #2683: stamp the build identity into every ELF candidate. This must run +# after strip (strip rewrites the bytes the stamp covers) and before any +# signing; Mach-O and PE stay unstamped and keep hashing their image. The +# stamper reads and writes bytes only: no candidate is executed here. +case "$GOOS" in +linux) + for candidate in "$UNSTRIPPED" "$DEBUG_STRIPPED" "$STRIPPED"; do + python3 "$ROOT/scripts/build-stamp.py" "$candidate" >/dev/null || { + echo "prepare-release-candidates: build-stamp failed for $candidate" >&2 + exit 2 + } + done + ;; +esac + UNSTRIPPED_PRE_SIGN_SHA="$(sha256_file "$UNSTRIPPED")" DEBUG_STRIPPED_PRE_SIGN_SHA="$(sha256_file "$DEBUG_STRIPPED")" STRIPPED_PRE_SIGN_SHA="$(sha256_file "$STRIPPED")" diff --git a/src/daemon/service.c b/src/daemon/service.c index 13413be2ff..926bfd8616 100644 --- a/src/daemon/service.c +++ b/src/daemon/service.c @@ -8,6 +8,7 @@ #include "daemon/ipc.h" #endif +#include "foundation/build_stamp.h" #include "foundation/sha256.h" #include @@ -370,8 +371,10 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, return false; } - cbm_sha256_ctx context; - cbm_sha256_init(&context); + /* The fingerprint excludes the build-stamp payload (#2683); on an + * unstamped image this equals a plain SHA-256 of the bytes. */ + cbm_build_stamp_hasher_t context; + cbm_build_stamp_hasher_init(&context); unsigned char buffer[DAEMON_SERVICE_IO_CAP]; off_t offset = 0; bool ok = true; @@ -380,7 +383,7 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, size_t request = remaining < (off_t)sizeof(buffer) ? (size_t)remaining : sizeof(buffer); ssize_t count = pread(fd, buffer, request, offset); if (count > 0) { - cbm_sha256_update(&context, buffer, (size_t)count); + cbm_build_stamp_hasher_update(&context, buffer, (size_t)count); offset += (off_t)count; } else if (count == 0) { ok = false; @@ -398,7 +401,7 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, return false; } uint8_t digest[CBM_SHA256_DIGEST_LEN]; - cbm_sha256_final(&context, digest); + cbm_build_stamp_hasher_final(&context, digest); digest_to_hex(digest, out); return true; } @@ -697,8 +700,9 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, (info.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) == 0 && windows_same_file_identity(&original_info, &info) && GetFileSizeEx(file, &before) != 0 && before.QuadPart >= 0; - cbm_sha256_ctx context; - cbm_sha256_init(&context); + /* Same stamp-excluding digest as the POSIX variant (#2683). */ + cbm_build_stamp_hasher_t context; + cbm_build_stamp_hasher_init(&context); unsigned char buffer[DAEMON_SERVICE_IO_CAP]; uint64_t total = 0; while (ok) { @@ -708,7 +712,7 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, } else if (count == 0) { break; } else { - cbm_sha256_update(&context, buffer, count); + cbm_build_stamp_hasher_update(&context, buffer, count); total += count; } } @@ -725,7 +729,7 @@ bool cbm_daemon_build_fingerprint_native_file(uintptr_t native_file, return false; } uint8_t digest[CBM_SHA256_DIGEST_LEN]; - cbm_sha256_final(&context, digest); + cbm_build_stamp_hasher_final(&context, digest); digest_to_hex(digest, out); return true; } diff --git a/src/foundation/build_stamp.c b/src/foundation/build_stamp.c new file mode 100644 index 0000000000..e1e437bd57 --- /dev/null +++ b/src/foundation/build_stamp.c @@ -0,0 +1,144 @@ +/* + * build_stamp.c — see build_stamp.h. + */ +#include "foundation/build_stamp.h" + +#include + +#if defined(__GNUC__) || defined(__clang__) +#define CBM_BUILD_STAMP_USED __attribute__((used)) +#else +#define CBM_BUILD_STAMP_USED +#endif + +/* The slot: magic followed by the payload. volatile keeps the compiler from + * folding reads of the (initially zero) payload into constants; the bytes the + * process reads are the ones the stamp step wrote into the file. */ +CBM_BUILD_STAMP_USED static volatile const uint8_t + cbm_build_stamp_slot[CBM_BUILD_STAMP_MAGIC_LEN + CBM_BUILD_STAMP_PAYLOAD_LEN] = { + 0x89, 'C', 'B', 'M', '-', 'B', 'U', 'I', 'L', 'D', 'S', 'T', 'A', 'M', 'P', 0x01, + /* payload: 32 zero bytes until stamped */ +}; + +void cbm_build_stamp_magic(uint8_t out[CBM_BUILD_STAMP_MAGIC_LEN]) { + for (size_t i = 0; i < CBM_BUILD_STAMP_MAGIC_LEN; i++) { + out[i] = cbm_build_stamp_slot[i]; + } +} + +static const char k_hex_digits[] = "0123456789abcdef"; + +static void stamp_to_hex(const uint8_t bytes[CBM_BUILD_STAMP_PAYLOAD_LEN], + char out[CBM_BUILD_STAMP_HEX_SIZE]) { + for (size_t i = 0; i < CBM_BUILD_STAMP_PAYLOAD_LEN; i++) { + out[i * 2U] = k_hex_digits[(bytes[i] >> 4U) & 0x0FU]; + out[i * 2U + 1U] = k_hex_digits[bytes[i] & 0x0FU]; + } + out[CBM_BUILD_STAMP_PAYLOAD_LEN * 2U] = '\0'; +} + +bool cbm_build_stamp_self_fingerprint(char out[CBM_BUILD_STAMP_HEX_SIZE]) { + if (!out) { + return false; + } + out[0] = '\0'; + uint8_t payload[CBM_BUILD_STAMP_PAYLOAD_LEN]; + uint8_t any = 0; + for (size_t i = 0; i < CBM_BUILD_STAMP_PAYLOAD_LEN; i++) { + payload[i] = cbm_build_stamp_slot[CBM_BUILD_STAMP_MAGIC_LEN + i]; + any |= payload[i]; + } + if (any == 0) { + return false; + } + stamp_to_hex(payload, out); + return true; +} + +void cbm_build_stamp_hasher_init(cbm_build_stamp_hasher_t *hasher) { + memset(hasher, 0, sizeof(*hasher)); + cbm_sha256_init(&hasher->sha); + cbm_build_stamp_magic(hasher->magic); +} + +static void stamp_note_match(cbm_build_stamp_hasher_t *hasher, uint64_t payload_at) { + hasher->matches++; + uint64_t end = payload_at + CBM_BUILD_STAMP_PAYLOAD_LEN; + if (end > hasher->zero_until) { + hasher->zero_until = end; + } +} + +/* Zero the payload bytes of data[from..len) that fall before zero_until. */ +static void stamp_zero_pending(const cbm_build_stamp_hasher_t *hasher, uint8_t *data, size_t len, + uint64_t base, size_t from) { + if (hasher->zero_until <= base + from) { + return; + } + uint64_t stop = hasher->zero_until - base; + size_t end = stop < (uint64_t)len ? (size_t)stop : len; + if (end > from) { + memset(data + from, 0, end - from); + } +} + +void cbm_build_stamp_hasher_update(cbm_build_stamp_hasher_t *hasher, uint8_t *data, size_t len) { + if (!hasher || !data || len == 0) { + return; + } + const size_t magic_len = CBM_BUILD_STAMP_MAGIC_LEN; + const uint64_t base = hasher->offset; + /* Payload carried over from a match in an earlier call. */ + stamp_zero_pending(hasher, data, len, base, 0); + + /* Matches that start in the retained tail and end inside data. Bytes are + * read from the transformed stream, so zeroed payload never matches. */ + for (size_t start = 0; start < hasher->tail_len; start++) { + size_t from_tail = hasher->tail_len - start; + if (magic_len - from_tail > len) { + continue; + } + if (memcmp(hasher->tail + start, hasher->magic, from_tail) == 0 && + memcmp(data, hasher->magic + from_tail, magic_len - from_tail) == 0) { + size_t payload = magic_len - from_tail; + stamp_note_match(hasher, base + payload); + stamp_zero_pending(hasher, data, len, base, payload); + } + } + + /* Matches that lie entirely inside data, in stream order. */ + size_t pos = 0; + while (len - pos >= magic_len) { + const uint8_t *hit = memchr(data + pos, hasher->magic[0], len - pos - magic_len + 1U); + if (!hit) { + break; + } + size_t at = (size_t)(hit - data); + if (memcmp(hit, hasher->magic, magic_len) == 0) { + stamp_note_match(hasher, base + at + magic_len); + stamp_zero_pending(hasher, data, len, base, at + magic_len); + } + pos = at + 1U; + } + + cbm_sha256_update(&hasher->sha, data, len); + + /* Keep the last magic_len - 1 transformed bytes for the next call. */ + const size_t keep = magic_len - 1U; + if (len >= keep) { + memcpy(hasher->tail, data + len - keep, keep); + hasher->tail_len = keep; + } else { + size_t total = hasher->tail_len + len; + size_t drop = total > keep ? total - keep : 0; + memmove(hasher->tail, hasher->tail + drop, hasher->tail_len - drop); + memcpy(hasher->tail + hasher->tail_len - drop, data, len); + hasher->tail_len = total - drop; + } + hasher->offset = base + len; +} + +void cbm_build_stamp_hasher_final(cbm_build_stamp_hasher_t *hasher, + uint8_t digest[CBM_SHA256_DIGEST_LEN]) { + cbm_sha256_final(&hasher->sha, digest); +} diff --git a/src/foundation/build_stamp.h b/src/foundation/build_stamp.h new file mode 100644 index 0000000000..2ed62bf021 --- /dev/null +++ b/src/foundation/build_stamp.h @@ -0,0 +1,57 @@ +/* + * build_stamp.h — Build identity stamped into the executable after linking. + * + * The build fingerprint is the SHA-256 of the executable image with the + * 32-byte stamp payload treated as zeros. A post-link step + * (scripts/build-stamp.py, which never executes the image) computes that + * digest and writes it into the payload, so a process can read its own + * fingerprint from memory instead of hashing its ~300 MB image on every + * start (#2683). + * + * An unstamped image has an all-zero payload, so its fingerprint equals a + * plain SHA-256 of the file and the process falls back to hashing itself. + * The daemon still hashes images it has to verify (its own image at startup + * and peers running a different file), so a wrong stamp is refused there. + */ +#ifndef CBM_FOUNDATION_BUILD_STAMP_H +#define CBM_FOUNDATION_BUILD_STAMP_H + +#include "foundation/sha256.h" + +#include +#include +#include + +#define CBM_BUILD_STAMP_MAGIC_LEN 16U +#define CBM_BUILD_STAMP_PAYLOAD_LEN 32U +#define CBM_BUILD_STAMP_HEX_SIZE (CBM_BUILD_STAMP_PAYLOAD_LEN * 2U + 1U) + +/* Streaming SHA-256 over an image in which the CBM_BUILD_STAMP_PAYLOAD_LEN + * bytes following every occurrence of the stamp magic are read as zeros. The + * transform is applied to the byte stream, so the digest does not depend on + * how the input is split into update() calls. update() zeroes payload bytes + * in the caller's buffer before hashing them. */ +typedef struct { + cbm_sha256_ctx sha; + uint8_t magic[CBM_BUILD_STAMP_MAGIC_LEN]; + uint8_t tail[CBM_BUILD_STAMP_MAGIC_LEN - 1U]; + size_t tail_len; + uint64_t offset; /* stream offset of the next byte fed */ + uint64_t zero_until; /* payload bytes before this offset are zeroed */ + size_t matches; /* magic occurrences seen so far */ +} cbm_build_stamp_hasher_t; + +void cbm_build_stamp_hasher_init(cbm_build_stamp_hasher_t *hasher); +void cbm_build_stamp_hasher_update(cbm_build_stamp_hasher_t *hasher, uint8_t *data, size_t len); +void cbm_build_stamp_hasher_final(cbm_build_stamp_hasher_t *hasher, + uint8_t digest[CBM_SHA256_DIGEST_LEN]); + +/* Copy the stamp magic out of this executable's own slot. The magic exists + * as a literal only in the slot initializer, so the image holds it once. */ +void cbm_build_stamp_magic(uint8_t out[CBM_BUILD_STAMP_MAGIC_LEN]); + +/* The fingerprint stamped into this executable, as lowercase hex. False when + * the payload is all zeros (an unstamped build). */ +bool cbm_build_stamp_self_fingerprint(char out[CBM_BUILD_STAMP_HEX_SIZE]); + +#endif /* CBM_FOUNDATION_BUILD_STAMP_H */ diff --git a/src/mcp/index_supervisor.c b/src/mcp/index_supervisor.c index 701ac57436..2880cf01b8 100644 --- a/src/mcp/index_supervisor.c +++ b/src/mcp/index_supervisor.c @@ -4,6 +4,7 @@ #include "index_supervisor.h" #include "daemon/runtime.h" +#include "foundation/build_stamp.h" #include "foundation/compat.h" #include "foundation/compat_fs.h" /* cbm_mkdir_p, cbm_fopen */ #include "foundation/constants.h" @@ -196,6 +197,14 @@ bool cbm_index_supervisor_capture_build_fingerprint(void) { } #endif char captured[CBM_INDEX_WORKER_BUILD_FINGERPRINT_SIZE] = {0}; + /* #2683: a stamped build carries its fingerprint in its own image, so a + * short-lived process (hook-augment) does not hash ~300 MB to learn it. + * This is only the claim the process presents; the daemon verifies peer + * images itself and checks its own image against the stamp at startup. */ + if (cbm_build_stamp_self_fingerprint(captured)) { + (void)snprintf(g_build_fingerprint, sizeof(g_build_fingerprint), "%s", captured); + return true; + } if (!cbm_daemon_runtime_process_build_fingerprint((uint64_t)worker_getpid(), captured)) { return false; } diff --git a/tests/test_daemon_version.c b/tests/test_daemon_version.c index 4f368d3577..928ef0b111 100644 --- a/tests/test_daemon_version.c +++ b/tests/test_daemon_version.c @@ -12,6 +12,7 @@ #include "daemon/runtime.h" #include "daemon/service.h" #include "daemon/service_internal.h" +#include "foundation/build_stamp.h" #include "foundation/compat.h" #include "foundation/compat_fs.h" #include "foundation/compat_thread.h" @@ -244,6 +245,93 @@ TEST(daemon_build_fingerprint_hashes_exact_executable_bytes) { PASS(); } +/* ── #2683: build stamp ───────────────────────────────────────────── */ + +enum { STAMP_TEST_PREFIX = 1000, STAMP_TEST_SUFFIX = 777 }; + +/* prefix | magic | payload | suffix, with a deterministic byte pattern. */ +static size_t stamp_test_image(uint8_t *out, size_t cap, uint8_t payload_fill, bool with_magic) { + size_t need = STAMP_TEST_PREFIX + CBM_BUILD_STAMP_MAGIC_LEN + CBM_BUILD_STAMP_PAYLOAD_LEN + + STAMP_TEST_SUFFIX; + if (cap < need) { + return 0; + } + for (size_t i = 0; i < need; i++) { + out[i] = (uint8_t)((i * 131U + 7U) & 0x7FU); /* never 0x89, the magic's first byte */ + } + if (with_magic) { + cbm_build_stamp_magic(out + STAMP_TEST_PREFIX); + memset(out + STAMP_TEST_PREFIX + CBM_BUILD_STAMP_MAGIC_LEN, payload_fill, + CBM_BUILD_STAMP_PAYLOAD_LEN); + } + return need; +} + +static void stamp_test_digest_hex(const uint8_t digest[CBM_SHA256_DIGEST_LEN], + char out[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { + static const char hex[] = "0123456789abcdef"; + for (size_t i = 0; i < CBM_SHA256_DIGEST_LEN; i++) { + out[i * 2U] = hex[digest[i] >> 4U]; + out[i * 2U + 1U] = hex[digest[i] & 0x0FU]; + } + out[CBM_SHA256_DIGEST_LEN * 2U] = '\0'; +} + +/* Feed a copy of image in chunks of chunk bytes; return the hex digest. */ +static size_t stamp_test_hash_chunked(const uint8_t *image, size_t len, size_t chunk, + char out[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { + uint8_t copy[4096]; + memcpy(copy, image, len); + cbm_build_stamp_hasher_t hasher; + cbm_build_stamp_hasher_init(&hasher); + for (size_t at = 0; at < len; at += chunk) { + size_t take = len - at < chunk ? len - at : chunk; + cbm_build_stamp_hasher_update(&hasher, copy + at, take); + } + uint8_t digest[CBM_SHA256_DIGEST_LEN]; + cbm_build_stamp_hasher_final(&hasher, digest); + stamp_test_digest_hex(digest, out); + return hasher.matches; +} + +TEST(build_stamp_digest_equals_plain_sha256_without_a_stamp) { + uint8_t image[4096]; + size_t len = stamp_test_image(image, sizeof(image), 0, false); + ASSERT_TRUE(len > 0); + char plain[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + cbm_sha256_hex(image, len, plain); + char stamped[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + ASSERT_EQ(stamp_test_hash_chunked(image, len, len, stamped), 0); + ASSERT_STR_EQ(stamped, plain); + PASS(); +} + +TEST(build_stamp_digest_ignores_payload_for_every_chunk_split) { + uint8_t zeroed[4096]; + uint8_t filled[4096]; + size_t len = stamp_test_image(zeroed, sizeof(zeroed), 0x00, true); + ASSERT_EQ(stamp_test_image(filled, sizeof(filled), 0xA5, true), len); + /* The fingerprint of an image is the plain SHA-256 with a zero payload. */ + char expected[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + cbm_sha256_hex(zeroed, len, expected); + /* Every chunk size, so the magic and payload straddle every boundary. */ + for (size_t chunk = 1; chunk <= CBM_BUILD_STAMP_MAGIC_LEN + CBM_BUILD_STAMP_PAYLOAD_LEN + 3; + chunk++) { + char got[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + ASSERT_EQ(stamp_test_hash_chunked(filled, len, chunk, got), 1); + ASSERT_STR_EQ(got, expected); + } + char whole[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + ASSERT_EQ(stamp_test_hash_chunked(filled, len, len, whole), 1); + ASSERT_STR_EQ(whole, expected); + /* A change outside the payload still changes the fingerprint. */ + filled[STAMP_TEST_PREFIX + CBM_BUILD_STAMP_MAGIC_LEN + CBM_BUILD_STAMP_PAYLOAD_LEN] ^= 1U; + char tampered[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + ASSERT_EQ(stamp_test_hash_chunked(filled, len, 7, tampered), 1); + ASSERT_STR_NEQ(tampered, expected); + PASS(); +} + TEST(daemon_hello_accepts_only_the_exact_active_build_identity) { cbm_daemon_build_identity_t active = version_test_identity("2.4.0", BUILD_A); cbm_daemon_build_identity_t exact = version_test_identity("2.4.0", BUILD_A); @@ -693,6 +781,8 @@ TEST(daemon_conflict_log_windows_concurrent_appends_are_not_dropped) { SUITE(daemon_version) { RUN_TEST(daemon_rendezvous_key_is_stable_and_version_independent); RUN_TEST(daemon_build_fingerprint_hashes_exact_executable_bytes); + RUN_TEST(build_stamp_digest_equals_plain_sha256_without_a_stamp); + RUN_TEST(build_stamp_digest_ignores_payload_for_every_chunk_split); RUN_TEST(daemon_hello_accepts_only_the_exact_active_build_identity); RUN_TEST(daemon_hello_version_conflict_exposes_active_and_requested_builds); RUN_TEST(daemon_conflict_remedy_names_session_managed_daemon_and_its_sessions); diff --git a/tests/test_release_candidate_derivation_contract.sh b/tests/test_release_candidate_derivation_contract.sh index 9dc00cf052..6ff7662057 100755 --- a/tests/test_release_candidate_derivation_contract.sh +++ b/tests/test_release_candidate_derivation_contract.sh @@ -52,6 +52,9 @@ pathlib.Path(sys.argv[1]).write_text( #include static const char release_canary[] = "codebase-memory-mcp OMIT_LOAD_EXTENSION candidate-derivation-contract"; +/* The build-stamp slot (src/foundation/build_stamp.c): magic + 32-byte payload. */ +__attribute__((used)) static volatile const unsigned char release_stamp_slot[48] = { + 0x89, 'C', 'B', 'M', '-', 'B', 'U', 'I', 'L', 'D', 'S', 'T', 'A', 'M', 'P', 0x01}; static _Thread_local unsigned long release_tls_counter; static int visible_symbol_for_real_strip(int value) { return value + 17; } int main(int argc, char **argv) { @@ -143,6 +146,7 @@ if [row["transform"] for row in rows] != ["copy", "strip-debug", "strip"]: raise SystemExit("FAIL: transforms are not explicit copy/strip-debug/strip") if any(row["pair_verification"] != "same-linker-output-v1" for row in rows): raise SystemExit("FAIL: common linker-output provenance is not recorded") +stamp_magic = b"\x89CBM-BUILDSTAMP\x01" if target.startswith("darwin-") and any(row["signature"] != "adhoc-verified" for row in rows): raise SystemExit("FAIL: both final Darwin candidates must be ad-hoc-signature verified") for row in rows: @@ -153,6 +157,17 @@ for row in rows: data = path.read_bytes() if hashlib.sha256(data).hexdigest() != row["sha256"] or len(data) != int(row["size"]): raise SystemExit(f"FAIL: provenance is not content-bound: {path}") + if target.startswith("linux-"): + # #2683: each ELF candidate carries the SHA-256 of itself with the + # stamp payload read as zeros, written after strip. + at = data.find(stamp_magic) + if at == -1 or data.find(stamp_magic, at + 1) != -1: + raise SystemExit(f"FAIL: candidate does not hold exactly one stamp slot: {path}") + start = at + len(stamp_magic) + zeroed = bytearray(data) + zeroed[start : start + 32] = bytes(32) + if data[start : start + 32] != hashlib.sha256(zeroed).digest(): + raise SystemExit(f"FAIL: candidate build stamp is missing or stale: {path}") if stat.S_IMODE(path.stat().st_mode) & 0o222: raise SystemExit(f"FAIL: published candidate is writable: {path}") if not stat.S_IMODE(path.stat().st_mode) & 0o111: