From 7b15adee5d1097e24649f85773b0d87b3507c6f8 Mon Sep 17 00:00:00 2001 From: Martin Vogel Date: Sat, 14 Nov 2026 12:21:10 +0100 Subject: [PATCH] fix(daemon): classify a differing peer-image digest as a mismatch in every verify outcome runtime_peer_image_verify now reports a peer image whose computed digest differs from this build as RUNTIME_PEER_IMAGE_MISMATCH regardless of the post-hash consistency re-check. Images that cannot be hashed, or whose digest matches, are handled as before (#1539). runtime_process_image_reference_acquire_observed (the former acquire body) also hands back the digest it computed; the old acquire name stays a wrapper, so every other caller is unchanged. Test (daemon_runtime suite): daemon_runtime_peer_image_mismatch_stays_rejected_when_recheck_fails, three HELLOs: differing digest -> rejected (control); differing digest with a failed re-check -> rejected; matching digest with a failed re-check -> admitted (unchanged). A CBM_ENABLE_TEST_SEAMS-only seam, cbm_daemon_runtime_fail_image_recheck_after_hash_for_testing, fails the re-check after the digest was computed; the existing distinct-copy seam and hash stub decide the digest. Signed-off-by: Martin Vogel --- src/daemon/runtime.c | 54 ++++++++++++++++++++++--- src/daemon/runtime.h | 4 ++ tests/test_daemon_runtime.c | 81 +++++++++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 6 deletions(-) diff --git a/src/daemon/runtime.c b/src/daemon/runtime.c index eb2eddf70..3bc1a323f 100644 --- a/src/daemon/runtime.c +++ b/src/daemon/runtime.c @@ -53,6 +53,13 @@ uint64_t cbm_daemon_runtime_peer_image_hashes_for_testing(void) { static void runtime_note_peer_image_hash(void) { (void)atomic_fetch_add(&runtime_peer_image_hash_count_seam, 1); } +/* The in-process harness peer is this very process, so its post-hash identity + * re-check always holds. While armed, every fingerprinting image acquisition + * fails that re-check after its digest was computed. */ +static atomic_bool runtime_fail_image_recheck_after_hash_seam; +void cbm_daemon_runtime_fail_image_recheck_after_hash_for_testing(bool fail) { + atomic_store(&runtime_fail_image_recheck_after_hash_seam, fail); +} /* The abandoned-request containment path ends in process termination, which an * in-process harness cannot observe. The timeout override makes the ceiling * reachable in test time; the hook replaces termination with a recordable @@ -1097,10 +1104,18 @@ bool cbm_daemon_runtime_fingerprint_cache_resolve_for_testing( /* Acquire one process instance's mapped image as a native object. Supplying a * fingerprint hashes that same held object; NULL performs metadata-only * acquisition for the HELLO fast path. Every platform brackets the process - * identity/image lookup before returning the retained reference. */ -static bool runtime_process_image_reference_acquire( + * identity/image lookup before returning the retained reference. + * `fingerprint` keeps the digest only when the whole bracket held. `observed`, + * when supplied, receives the digest that was computed even if the post-hash + * re-check then failed ("" when none was computed), so a caller can tell an + * image that was read and differed from one that could not be read. */ +static bool runtime_process_image_reference_acquire_observed( uint64_t process_id, runtime_process_image_reference_t *reference, - char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { + char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE], + char observed[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { + if (observed) { + observed[0] = '\0'; + } if (!reference || process_id == 0) { return false; } @@ -1282,12 +1297,28 @@ static bool runtime_process_image_reference_acquire( (void)process_id; bool ok = false; #endif +#ifdef CBM_ENABLE_TEST_SEAMS + if (ok && fingerprint && atomic_load(&runtime_fail_image_recheck_after_hash_seam)) { + (void)runtime_process_image_reference_release(reference); + ok = false; + } +#endif + if (observed && fingerprint) { + (void)snprintf(observed, CBM_DAEMON_BUILD_FINGERPRINT_SIZE, "%s", fingerprint); + } if (!ok && fingerprint) { fingerprint[0] = '\0'; } return ok; } +static bool runtime_process_image_reference_acquire( + uint64_t process_id, runtime_process_image_reference_t *reference, + char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) { + return runtime_process_image_reference_acquire_observed(process_id, reference, fingerprint, + NULL); +} + /* True when a retained reference is still the unchanged image it captured AND * the freshly acquired peer maps that same image. Metadata only: no hashing. */ static bool runtime_process_image_reference_same(const runtime_process_image_reference_t *held, @@ -1401,10 +1432,12 @@ static runtime_peer_image_verdict_t runtime_peer_image_verify(cbm_daemon_runtime } char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + char observed[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; runtime_process_image_reference_t hashed; runtime_process_image_reference_init(&hashed); runtime_note_peer_image_hash(); - bool fingerprinted = runtime_process_image_reference_acquire(process_id, &hashed, fingerprint); + bool fingerprinted = runtime_process_image_reference_acquire_observed(process_id, &hashed, + fingerprint, observed); bool verified = fingerprinted && strcmp(fingerprint, requested_build) == 0 && strcmp(fingerprint, service->identity.build_fingerprint) == 0; if (verified) { @@ -1420,7 +1453,15 @@ static runtime_peer_image_verdict_t runtime_peer_image_verify(cbm_daemon_runtime if (verified) { return RUNTIME_PEER_IMAGE_VERIFIED; } - return fingerprinted ? RUNTIME_PEER_IMAGE_MISMATCH : RUNTIME_PEER_IMAGE_UNVERIFIABLE; + /* A digest that was computed and differs from this build is a mismatch + * whatever the post-hash re-check concluded. Only an image that could not + * be hashed at all, or whose digest matched before its re-check failed, + * stays unverifiable. */ + bool observed_differs = strcmp(observed, requested_build) != 0 || + strcmp(observed, service->identity.build_fingerprint) != 0; + bool observed_mismatch = observed[0] != '\0' && observed_differs; + return fingerprinted || observed_mismatch ? RUNTIME_PEER_IMAGE_MISMATCH + : RUNTIME_PEER_IMAGE_UNVERIFIABLE; } static void runtime_verified_peer_images_release(cbm_daemon_runtime_service_t *service) { @@ -2442,7 +2483,8 @@ static void *runtime_connection_worker(void *opaque) { * * fingerprint_mismatch — the peer's image WAS read and hashes differently * than the running daemon. That is the tamper/skew case the gate exists - * for. Still rejected, hard. + * for. Still rejected, hard, whatever the post-hash re-check concluded + * (the digest was read). * image_unverifiable — the peer's image could not be examined at all * (ephemeral npx cache paths, ptrace_scope restrictions, sandboxed * hosts). Nothing was contradicted; we simply could not look. The peer diff --git a/src/daemon/runtime.h b/src/daemon/runtime.h index 361a7e781..bb2ca247b 100644 --- a/src/daemon/runtime.h +++ b/src/daemon/runtime.h @@ -463,6 +463,10 @@ void cbm_daemon_runtime_force_peer_image_mismatch_for_testing(bool force); * HELLO path has computed in this process. */ void cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(bool force); uint64_t cbm_daemon_runtime_peer_image_hashes_for_testing(void); +/* Fail the post-hash identity re-check of every fingerprinting image + * acquisition after its digest was computed (the in-process peer's re-check + * otherwise always holds). */ +void cbm_daemon_runtime_fail_image_recheck_after_hash_for_testing(bool fail); /* Abandoned-request containment seams (2026-08-29 zombie incident). The * timeout override shrinks the join ceiling to test scale; zero restores the * production constant. The hook replaces the terminal containment stop with a diff --git a/tests/test_daemon_runtime.c b/tests/test_daemon_runtime.c index c34c45f5b..0da691eef 100644 --- a/tests/test_daemon_runtime.c +++ b/tests/test_daemon_runtime.c @@ -2112,6 +2112,86 @@ TEST(daemon_runtime_verified_peer_copy_is_hashed_once_issue1955) { PASS(); } +typedef enum { + RUNTIME_TEST_HELLO_ADMITTED, + RUNTIME_TEST_HELLO_REJECTED_MISMATCH, + RUNTIME_TEST_HELLO_OTHER, +} runtime_test_hello_outcome_t; + +/* One HELLO whose full peer-image hash yields `digest`, with the post-hash + * identity re-check failing when fail_recheck is set. The fingerprint cache is + * reset on both sides: the in-process peer is this process, so its stubbed + * digest would otherwise be cached as this image's own. */ +static runtime_test_hello_outcome_t runtime_test_hello_with_peer_digest( + const runtime_test_fixture_t *fixture, const cbm_daemon_build_identity_t *identity, + const char *digest, bool fail_recheck) { + cbm_daemon_runtime_fingerprint_cache_reset_for_testing(); + cbm_daemon_runtime_fingerprint_cache_set_hash_stub_for_testing(digest); + cbm_daemon_runtime_fail_image_recheck_after_hash_for_testing(fail_recheck); + cbm_daemon_runtime_connect_result_t result = {0}; + cbm_daemon_runtime_client_t *client = cbm_daemon_runtime_client_connect( + fixture->endpoint, identity, RUNTIME_TEST_TIMEOUT_MS, &result); + cbm_daemon_runtime_fail_image_recheck_after_hash_for_testing(false); + cbm_daemon_runtime_fingerprint_cache_reset_for_testing(); + if (client) { + bool accepted = result.status == CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED; + bool closed = cbm_daemon_runtime_client_close(client, RUNTIME_TEST_TIMEOUT_MS); + return accepted && closed ? RUNTIME_TEST_HELLO_ADMITTED : RUNTIME_TEST_HELLO_OTHER; + } + return result.status == CBM_DAEMON_RUNTIME_CONNECT_REJECTED && + strstr(result.message, "fingerprint_mismatch") != NULL + ? RUNTIME_TEST_HELLO_REJECTED_MISMATCH + : RUNTIME_TEST_HELLO_OTHER; +} + +/* runtime_peer_image_verify hashes the peer's image and then re-checks that + * the peer still runs that same image. A digest that was read and differs from + * this build is a fingerprint_mismatch rejection whatever that re-check + * concludes; an image that matched before its re-check failed stays + * admissible (#1539) exactly as before. The in-process peer is this process, + * so the seams stand in for it: distinct_copy forces the full hash, the hash + * stub decides the digest, and the re-check seam fails the post-hash re-check. */ +TEST(daemon_runtime_peer_image_mismatch_stays_rejected_when_recheck_fails) { + static const char other_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE] = + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; + char own_build[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]; + snprintf(own_build, sizeof(own_build), "%s", runtime_test_self_build()); + cbm_daemon_build_identity_t identity = runtime_test_identity("2.4.0", own_build); + runtime_test_fixture_t fixture; + bool started = runtime_test_fixture_start(&fixture, "image-recheck", &identity); + /* An owner session keeps the ephemeral generation alive across the + * sequential HELLOs below; it connects through the active-image fast path + * before any seam is armed. */ + cbm_daemon_runtime_connect_result_t owner_result = {0}; + cbm_daemon_runtime_client_t *owner = + started ? cbm_daemon_runtime_client_connect(fixture.endpoint, &identity, + RUNTIME_TEST_TIMEOUT_MS, &owner_result) + : NULL; + runtime_test_hello_outcome_t intact = RUNTIME_TEST_HELLO_OTHER; + runtime_test_hello_outcome_t mismatch_recheck_failed = RUNTIME_TEST_HELLO_OTHER; + runtime_test_hello_outcome_t match_recheck_failed = RUNTIME_TEST_HELLO_OTHER; + + cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(true); + if (owner) { + /* Control: a differing digest with the re-check intact is rejected. */ + intact = runtime_test_hello_with_peer_digest(&fixture, &identity, other_build, false); + mismatch_recheck_failed = + runtime_test_hello_with_peer_digest(&fixture, &identity, other_build, true); + match_recheck_failed = + runtime_test_hello_with_peer_digest(&fixture, &identity, own_build, true); + } + cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(false); + bool owner_closed = owner && cbm_daemon_runtime_client_close(owner, RUNTIME_TEST_TIMEOUT_MS); + runtime_test_fixture_finish(&fixture); + + ASSERT_TRUE(started); + ASSERT_TRUE(owner_closed); + ASSERT_EQ(intact, RUNTIME_TEST_HELLO_REJECTED_MISMATCH); + ASSERT_EQ(mismatch_recheck_failed, RUNTIME_TEST_HELLO_REJECTED_MISMATCH); + ASSERT_EQ(match_recheck_failed, RUNTIME_TEST_HELLO_ADMITTED); + PASS(); +} + TEST(daemon_runtime_unexpected_frame_payload_is_freed_once) { static const uint8_t unexpected_payload[] = {0xde, 0xad, 0xbe, 0xef}; cbm_daemon_build_identity_t identity = @@ -5634,6 +5714,7 @@ SUITE(daemon_runtime) { RUN_TEST(daemon_runtime_image_rejection_reaches_client_issue1383); RUN_TEST(daemon_runtime_unverifiable_image_is_admitted_issue1539); RUN_TEST(daemon_runtime_verified_peer_copy_is_hashed_once_issue1955); + RUN_TEST(daemon_runtime_peer_image_mismatch_stays_rejected_when_recheck_fails); RUN_TEST(daemon_runtime_unexpected_frame_payload_is_freed_once); RUN_TEST(daemon_runtime_activation_rejects_forged_and_malformed_without_stop); RUN_TEST(daemon_runtime_activation_ack_snapshots_then_interrupts_all_clients);