Skip to content

fix(daemon): stamp the build fingerprint into ELF images after linking - #3045

Open
AlexMKX wants to merge 1 commit into
DeusData:mainfrom
GFN-CIS:gfn/build-stamp
Open

AlexMKX wants to merge 1 commit into
DeusData:mainfrom
GFN-CIS:gfn/build-stamp

Conversation

@AlexMKX

@AlexMKX AlexMKX commented Nov 19, 2026 •

Copy link
Copy Markdown

Refs #2683 — not Closes: this covers ELF only; Mach-O and PE keep the full hash (see below).

Problem

hook-augment (and every other short-lived CBM process) learns its own build fingerprint by hashing its whole executable (~300 MB). On CPUs without SHA instructions this is the portable path from #2683: measured ~1.9 s per hook call on an Intel Xeon E5-2690 v4 (Broadwell, no sha_ni), Linux, v0.12.1 release binary, warm permanent daemon — against the 2000 ms hook deadline. hook-augment-timeouts.log on that machine had 3,640 deadline_exceeded lines, a second identical machine 9,575.

Change (option 3 of #2683, embedded instead of an install-time file)

  • Definition. The build fingerprint is the SHA-256 of the image with a 32-byte payload read as zeros. The payload follows a 16-byte magic in a static slot (src/foundation/build_stamp.c). The magic exists as a literal only in the slot initializer; the hasher compares against the slot's own bytes, so an image holds it once.
  • One hashing function. cbm_daemon_build_fingerprint_native_file (POSIX and Windows) streams through cbm_build_stamp_hasher_*, which zeroes the payload after each magic in stream order, so the digest does not depend on read sizes. Daemon self-check (active_image_identity), peer verification and cbm_daemon_build_fingerprint_file all go through it.
  • Stamping never executes the image. scripts/build-stamp.py <file> (stdlib only) hashes the file, requires exactly one slot, writes the digest into the payload, fsyncs and re-verifies. scripts/build.sh stamps ELF builds (Linux/FreeBSD/NetBSD; without python3 it warns and leaves the build unstamped, which is still correct). scripts/ci/prepare-release-candidates.sh (already requires python3) re-stamps every Linux candidate after strip and before provenance hashing, so the "no candidate runs before VirusTotal selection" rule holds.
  • Contract. tests/test_release_candidate_derivation_contract.sh: the stub gets a slot, and every Linux candidate must hold exactly one slot whose payload equals the SHA-256 of the candidate with the payload zeroed. Verified to fail when the stamper is replaced by a no-op.
  • Fast path. cbm_index_supervisor_capture_build_fingerprint uses a non-zero payload as the process's fingerprint and does not read its image.
  • Unchanged. An unstamped image has a zero payload, so its fingerprint is exactly today's plain SHA-256: Mach-O and PE builds, test runners and any build that skips the step behave as before. Verification still hashes: the daemon checks its own image against the stamp at startup, and a peer running a different file is hashed and compared.

Why not Mach-O / PE in this PR

They are signed after linking (codesign in prepare-release-candidates.sh, re-signed by install.sh on macOS). Stamping before signing would make the stamp disagree with the signed file; supporting them needs the signature region excluded from the digest. Not attempted here and not tested — I have no macOS/Windows hosts. They keep the current full hash.

Evidence (Linux x86_64, Xeon E5-2690 v4, gcc 15.2, this branch on 4f8b66e)

check result
scripts/build.sh output, slot checked independently (find magic, zero payload, hashlib.sha256) 1 magic occurrence, payload == recomputed digest
real prepare-release-candidates.sh linux amd64 on that build all three candidates carry a correct stamp
daemon started from the stripped candidate (C startup self-check vs the Python-written stamp) started, build: 0.12.1-pr (dcbf30f1d00d…) — the two implementations agree
hook-augment PreToolUse Grep from that candidate, indexed project, daemon running 21 ms on each of three calls (was ~1.9 s every call); output unchanged
strace -f -y -e trace=pread64,read on hook-augment 0 reads of its own executable (#2683 comment measured 5,057 pread64 calls over the image)
copy of the binary (same bytes, other inode) as client admitted via full hash
binary with one byte changed after stamping, as daemon / as peer refused: stage=active_image_identity / client_image_rejected reason=fingerprint_mismatch
full scripts/test.sh (ASan+UBSan) all Step 0 contracts pass (incl. 0q); suites 10,604 passed, 11 skipped, 24 failed — all 24 in cli install/update tests failing with install_dir_group_or_world_writable (mode 0775): my checkout lives under a group-writable directory. daemon_version 17 (2 new), daemon, daemon_runtime, index_supervisor green
clang-format 20.1.8 on touched C files; make lint-no-suppress lint-memory-core lint-vendored-scanners; scripts/security-audit.sh clean / pass

New C tests (tests/test_daemon_version.c): digest equals plain SHA-256 without a stamp; digest ignores the payload and is identical for every chunk size 1…51 (magic and payload straddle every boundary) while a byte outside the payload still changes it.

Not run locally: cppcheck 2.20 and clang-tidy (not installed here), macOS/Windows builds, the release workflow itself. pair_verification: same-linker-output-v1 is a manifest label only, so per-variant stamps do not conflict with it.

Notes for review

  • Touches build.sh and prepare-release-candidates.sh, which CONTRIBUTING lists as needing design discussion — happy to adjust or split (mechanism vs. pipeline step) if you prefer a different place for stamping.
  • An earlier revision of this PR stamped by running the freshly built binary (codebase-memory-mcp build-stamp); Step 0q rightly rejected that, and it would also have executed the unscanned build in build.sh on the release runners. The subcommand is gone.
  • Observed while testing, not changed here: (1) when the daemon rejects a peer image (fingerprint_mismatch), the client keeps retrying and finally reports "answered no rendezvous within 30000 ms; the daemon runtime is likely dead" instead of the rejection reason; (2) tests/test_venue_parity_contract.sh runs bash scripts/ci/generate-sbom.py --help before the *.py branch re-runs it with python3; on a desktop with ImageMagick and DISPLAY set, import datetime becomes a screen grab that waits for a click and hangs the run.

This PR was written with Claude Code (Claude Opus 5.5) on behalf of the account owner, who directed the change and signs off the DCO.

🤖 Generated with Claude Code

@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

Every short-lived process (hook-augment in particular) learned its own
build fingerprint by hashing its whole ~300 MB executable. On CPUs without
SHA instructions that is ~1.9 s per hook call, past the 2000 ms hook
deadline, so hooks returned nothing (DeusData#2683).

Define the fingerprint as the SHA-256 of the image with a 32-byte stamp
payload read as zeros. The payload sits right after a 16-byte magic in a
static slot. scripts/build-stamp.py computes that digest and writes it
into the payload without executing the image; build.sh stamps ELF builds,
and prepare-release-candidates.sh re-stamps each Linux candidate after
strip, so no candidate runs before VirusTotal selection. A process with a
non-zero payload uses it as its fingerprint and does not read its own
image at all.

On an unstamped image the payload is already zero, so the fingerprint is
exactly the plain SHA-256 used today: Mach-O and PE builds (which are
signed after linking, so stamping would need the signature excluded) keep
the current behaviour, as do test runners.

The verification side is unchanged and still hashes: the daemon checks its
own image against the stamp at startup (stage active_image_identity), and
a peer running a different file is hashed and compared. A binary modified
after stamping is therefore refused both as daemon and as peer.

Refs DeusData#2683

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Alex MKX <25821291+AlexMKX@users.noreply.github.com>
@AlexMKX
AlexMKX marked this pull request as ready for review November 19, 2026 13:27
@AlexMKX
AlexMKX requested a review from DeusData as a code owner November 19, 2026 13:27
@DeusData

Copy link
Copy Markdown
Owner

Hi @AlexMKX, thank you for this, and especially for the measurements. About 1.9 s per hook call on a Broadwell CPU without SHA instructions, and thousands of deadline_exceeded lines on two machines, is exactly the evidence #2683 was missing. The stream-order hasher is careful work too.

We would like to take #2683 in a different direction, though, and want to say why before you put more time into this approach:

  • It covers ELF only, while hook-augment: prove the hook's build identity without hashing the whole executable in every hook process #2683 needs Linux, macOS and Windows to behave the same. Mach-O has to be signed after any change and PE has its own signature region, so we would end up maintaining a second mechanism.
  • It adds a post-link step to scripts/build.sh and to the release-candidate derivation, and it changes what "build fingerprint" means for every build.
  • Any byte change after stamping makes the daemon refuse to start, with no fallback. That includes a distribution's strip, patchelf or similar.
  • The index-worker build check would compare copied stamps instead of a hash of the image itself.

Our preference is option 1 from #2683. The daemon verifies the connecting hook's image (since #2335 it already holds a verified handle for peers), and the hook process stops hashing itself. That works the same way on all three platforms and leaves the release bytes untouched.

If you would like to build that, we would be glad to review it, and your measurement setup would be the perfect check for it. If not, we will pick it up ourselves and credit your measurements. We'll keep this PR open until that is settled.

One small note for a follow-up PR: please leave out the Co-Authored-By trailer for an AI assistant; we keep that kind of attribution out of the repository history.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants