@@ -36,6 +36,23 @@ static atomic_bool runtime_force_peer_image_mismatch_seam;
3636void cbm_daemon_runtime_force_peer_image_mismatch_for_testing (bool force ) {
3737 atomic_store (& runtime_force_peer_image_mismatch_seam , force );
3838}
39+ /* #1955 test seam: make the active-image comparison miss, exactly as when the
40+ * peer runs a DIFFERENT file with identical bytes (a second install path, a
41+ * package-manager cache copy). Everything after that comparison - the full
42+ * fingerprint fallback and the verified-image cache - still runs for real. The
43+ * counter records every full-image fingerprint the HELLO path computes, so a
44+ * test asserts how often the O(image size) hash runs instead of timing it. */
45+ static atomic_bool runtime_force_peer_image_distinct_copy_seam ;
46+ static atomic_uint_fast64_t runtime_peer_image_hash_count_seam ;
47+ void cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing (bool force ) {
48+ atomic_store (& runtime_force_peer_image_distinct_copy_seam , force );
49+ }
50+ uint64_t cbm_daemon_runtime_peer_image_hashes_for_testing (void ) {
51+ return (uint64_t )atomic_load (& runtime_peer_image_hash_count_seam );
52+ }
53+ static void runtime_note_peer_image_hash (void ) {
54+ (void )atomic_fetch_add (& runtime_peer_image_hash_count_seam , 1 );
55+ }
3956/* The abandoned-request containment path ends in process termination, which an
4057 * in-process harness cannot observe. The timeout override makes the ceiling
4158 * reachable in test time; the hook replaces termination with a recordable
@@ -59,6 +76,8 @@ static _Atomic uint32_t runtime_ephemeral_linger_timeout_seam = UINT32_MAX;
5976void cbm_daemon_runtime_service_set_ephemeral_linger_timeout_for_testing (uint32_t timeout_ms ) {
6077 atomic_store (& runtime_ephemeral_linger_timeout_seam , timeout_ms );
6178}
79+ #else
80+ static void runtime_note_peer_image_hash (void ) {}
6281#endif
6382
6483#ifdef _WIN32
@@ -88,6 +107,11 @@ enum {
88107 RUNTIME_ACCEPT_POLL_MS = 20 ,
89108 RUNTIME_WAIT_POLL_NS = 1000000 ,
90109 RUNTIME_WORKER_STACK_SIZE = 256 * 1024 ,
110+ /* Distinct peer image files (install paths / identical copies) whose full
111+ * fingerprint already matched this daemon. Each held entry pins one image
112+ * inode, so the bound is small; a full table only means later copies are
113+ * hashed per admission, exactly as before the table existed. */
114+ RUNTIME_VERIFIED_PEER_IMAGE_CAPACITY = 8 ,
91115 /* Bounds the drain-before-close wait for a peer consuming its final
92116 * response; interrupted or already-poisoned connections skip it. */
93117 RUNTIME_WORKER_DRAIN_TIMEOUT_MS = 2000 ,
@@ -240,6 +264,10 @@ struct cbm_daemon_runtime_service {
240264 char build_fingerprint [CBM_DAEMON_BUILD_FINGERPRINT_SIZE ];
241265 cbm_daemon_build_identity_t identity ;
242266 runtime_process_image_reference_t active_image ;
267+ /* Guarded by mutex. Retained references keep each verified inode alive,
268+ * so its stat identity cannot be reused by a different file. */
269+ runtime_process_image_reference_t verified_peer_images [RUNTIME_VERIFIED_PEER_IMAGE_CAPACITY ];
270+ size_t verified_peer_image_count ;
243271 char * conflict_log_path ;
244272 size_t conflict_log_cap_bytes ;
245273 uint64_t lease_timeout_ms ;
@@ -916,50 +944,48 @@ static bool runtime_process_image_reference_acquire(
916944 return ok ;
917945}
918946
947+ /* True when a retained reference is still the unchanged image it captured AND
948+ * the freshly acquired peer maps that same image. Metadata only: no hashing. */
949+ static bool runtime_process_image_reference_same (const runtime_process_image_reference_t * held ,
950+ const runtime_process_image_reference_t * peer ) {
951+ if (!held || !held -> held || !peer || !peer -> held ) {
952+ return false;
953+ }
954+ #ifdef _WIN32
955+ BY_HANDLE_FILE_INFORMATION held_now ;
956+ LARGE_INTEGER held_size_now ;
957+ return runtime_windows_file_snapshot (held -> file , & held_now , & held_size_now ) &&
958+ runtime_windows_file_snapshot_same (& held -> information , & held -> size , & held_now ,
959+ & held_size_now ) &&
960+ runtime_windows_file_snapshot_same (& held -> information , & held -> size , & peer -> information ,
961+ & peer -> size );
962+ #elif defined(__APPLE__ )
963+ struct stat held_now ;
964+ return fstat (held -> fd , & held_now ) == 0 && runtime_mac_stat_same (& held -> status , & held_now ) &&
965+ runtime_mac_stat_same (& held -> status , & peer -> status );
966+ #elif defined(__linux__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ )
967+ struct stat held_now ;
968+ return fstat (held -> fd , & held_now ) == 0 &&
969+ runtime_posix_stat_same_image (& held -> status , & held_now ) &&
970+ runtime_posix_stat_same_image (& held -> status , & peer -> status );
971+ #else
972+ return false;
973+ #endif
974+ }
975+
919976/* A false result means only "not proven identical". The caller must use the
920977 * full fingerprint fallback before rejecting the peer. */
921978static bool runtime_process_image_reference_matches_process (
922979 const runtime_process_image_reference_t * active , uint64_t process_id ) {
923980 if (!active || !active -> held || process_id == 0 ) {
924981 return false;
925982 }
926- #ifdef _WIN32
927- runtime_process_image_reference_t peer ;
928- runtime_process_image_reference_init (& peer );
929- bool same = runtime_process_image_reference_acquire (process_id , & peer , NULL );
930- BY_HANDLE_FILE_INFORMATION active_now ;
931- LARGE_INTEGER active_size_now ;
932- same = same && runtime_windows_file_snapshot (active -> file , & active_now , & active_size_now ) &&
933- runtime_windows_file_snapshot_same (& active -> information , & active -> size , & active_now ,
934- & active_size_now ) &&
935- runtime_windows_file_snapshot_same (& active -> information , & active -> size ,
936- & peer .information , & peer .size );
937- bool released = runtime_process_image_reference_release (& peer );
938- return same && released ;
939- #elif defined(__APPLE__ )
940- runtime_process_image_reference_t peer ;
941- runtime_process_image_reference_init (& peer );
942- bool same = runtime_process_image_reference_acquire (process_id , & peer , NULL );
943- struct stat active_now ;
944- same = same && fstat (active -> fd , & active_now ) == 0 &&
945- runtime_mac_stat_same (& active -> status , & active_now ) &&
946- runtime_mac_stat_same (& active -> status , & peer .status );
947- bool released = runtime_process_image_reference_release (& peer );
948- return same && released ;
949- #elif defined(__linux__ ) || defined(__FreeBSD__ ) || defined(__NetBSD__ )
950983 runtime_process_image_reference_t peer ;
951984 runtime_process_image_reference_init (& peer );
952- bool same = runtime_process_image_reference_acquire (process_id , & peer , NULL );
953- struct stat active_now ;
954- same = same && fstat (active -> fd , & active_now ) == 0 &&
955- runtime_posix_stat_same_image (& active -> status , & active_now ) &&
956- runtime_posix_stat_same_image (& active -> status , & peer .status );
985+ bool same = runtime_process_image_reference_acquire (process_id , & peer , NULL ) &&
986+ runtime_process_image_reference_same (active , & peer );
957987 bool released = runtime_process_image_reference_release (& peer );
958988 return same && released ;
959- #else
960- (void )process_id ;
961- return false;
962- #endif
963989}
964990
965991bool cbm_daemon_runtime_process_build_fingerprint (uint64_t process_id ,
@@ -980,6 +1006,86 @@ bool cbm_daemon_runtime_process_build_fingerprint(uint64_t process_id,
9801006 return ok ;
9811007}
9821008
1009+ typedef enum {
1010+ RUNTIME_PEER_IMAGE_VERIFIED ,
1011+ RUNTIME_PEER_IMAGE_UNVERIFIABLE ,
1012+ RUNTIME_PEER_IMAGE_MISMATCH ,
1013+ } runtime_peer_image_verdict_t ;
1014+
1015+ static bool runtime_verified_peer_image_known_locked (
1016+ const cbm_daemon_runtime_service_t * service , const runtime_process_image_reference_t * peer ) {
1017+ for (size_t i = 0 ; i < service -> verified_peer_image_count ; i ++ ) {
1018+ if (runtime_process_image_reference_same (& service -> verified_peer_images [i ], peer )) {
1019+ return true;
1020+ }
1021+ }
1022+ return false;
1023+ }
1024+
1025+ /* #1955: the HELLO must prove the peer runs this daemon's build. The cheap
1026+ * proof is "same image file as the daemon"; a peer running a DIFFERENT file
1027+ * with identical bytes (a second install path, a package-manager copy) needs
1028+ * the full fingerprint, i.e. hashing the whole (~300 MB) image. That hash is
1029+ * slower than the client's HELLO budget, so the client abandoned it and every
1030+ * re-probe started it again - admission never completed against a healthy
1031+ * daemon. A copy is now hashed once: its verified reference is retained and
1032+ * later admissions of the same unchanged file match it by metadata, exactly
1033+ * like the active image. The accepted set of images is unchanged. */
1034+ static runtime_peer_image_verdict_t runtime_peer_image_verify (cbm_daemon_runtime_service_t * service ,
1035+ uint64_t process_id ,
1036+ const char * requested_build ) {
1037+ runtime_process_image_reference_t peer ;
1038+ runtime_process_image_reference_init (& peer );
1039+ bool peer_held = runtime_process_image_reference_acquire (process_id , & peer , NULL );
1040+ bool known = false;
1041+ if (peer_held ) {
1042+ known = runtime_process_image_reference_same (& service -> active_image , & peer );
1043+ #ifdef CBM_ENABLE_TEST_SEAMS
1044+ if (atomic_load (& runtime_force_peer_image_distinct_copy_seam )) {
1045+ known = false;
1046+ }
1047+ #endif
1048+ if (!known ) {
1049+ cbm_mutex_lock (& service -> mutex );
1050+ known = runtime_verified_peer_image_known_locked (service , & peer );
1051+ cbm_mutex_unlock (& service -> mutex );
1052+ }
1053+ }
1054+ bool released = runtime_process_image_reference_release (& peer );
1055+ if (known && released ) {
1056+ return RUNTIME_PEER_IMAGE_VERIFIED ;
1057+ }
1058+
1059+ char fingerprint [CBM_DAEMON_BUILD_FINGERPRINT_SIZE ];
1060+ runtime_process_image_reference_t hashed ;
1061+ runtime_process_image_reference_init (& hashed );
1062+ runtime_note_peer_image_hash ();
1063+ bool fingerprinted = runtime_process_image_reference_acquire (process_id , & hashed , fingerprint );
1064+ bool verified = fingerprinted && strcmp (fingerprint , requested_build ) == 0 &&
1065+ strcmp (fingerprint , service -> identity .build_fingerprint ) == 0 ;
1066+ if (verified ) {
1067+ cbm_mutex_lock (& service -> mutex );
1068+ if (service -> verified_peer_image_count < RUNTIME_VERIFIED_PEER_IMAGE_CAPACITY &&
1069+ !runtime_verified_peer_image_known_locked (service , & hashed )) {
1070+ service -> verified_peer_images [service -> verified_peer_image_count ++ ] = hashed ;
1071+ runtime_process_image_reference_init (& hashed );
1072+ }
1073+ cbm_mutex_unlock (& service -> mutex );
1074+ }
1075+ (void )runtime_process_image_reference_release (& hashed );
1076+ if (verified ) {
1077+ return RUNTIME_PEER_IMAGE_VERIFIED ;
1078+ }
1079+ return fingerprinted ? RUNTIME_PEER_IMAGE_MISMATCH : RUNTIME_PEER_IMAGE_UNVERIFIABLE ;
1080+ }
1081+
1082+ static void runtime_verified_peer_images_release (cbm_daemon_runtime_service_t * service ) {
1083+ for (size_t i = 0 ; i < service -> verified_peer_image_count ; i ++ ) {
1084+ (void )runtime_process_image_reference_release (& service -> verified_peer_images [i ]);
1085+ }
1086+ service -> verified_peer_image_count = 0 ;
1087+ }
1088+
9831089static bool runtime_hello_response_encode (uint8_t out [CBM_DAEMON_RENDEZVOUS_RESPONSE_SIZE ],
9841090 const cbm_daemon_runtime_connect_result_t * result ) {
9851091 if (!out || !result ) {
@@ -1950,17 +2056,10 @@ static void *runtime_connection_worker(void *opaque) {
19502056 return NULL ;
19512057 }
19522058
1953- bool peer_image_verified = runtime_process_image_reference_matches_process (
1954- & service -> active_image , worker -> peer_process_id );
1955- bool peer_image_fingerprinted = false;
1956- if (!peer_image_verified ) {
1957- char peer_fingerprint [CBM_DAEMON_BUILD_FINGERPRINT_SIZE ];
1958- peer_image_fingerprinted =
1959- cbm_daemon_runtime_process_build_fingerprint (worker -> peer_process_id , peer_fingerprint );
1960- peer_image_verified = peer_image_fingerprinted &&
1961- strcmp (peer_fingerprint , requested_build ) == 0 &&
1962- strcmp (peer_fingerprint , service -> identity .build_fingerprint ) == 0 ;
1963- }
2059+ runtime_peer_image_verdict_t peer_image =
2060+ runtime_peer_image_verify (service , worker -> peer_process_id , requested_build );
2061+ bool peer_image_verified = peer_image == RUNTIME_PEER_IMAGE_VERIFIED ;
2062+ bool peer_image_fingerprinted = peer_image != RUNTIME_PEER_IMAGE_UNVERIFIABLE ;
19642063#ifdef CBM_ENABLE_TEST_SEAMS
19652064 if (atomic_load (& runtime_force_peer_image_unverified_seam )) {
19662065 peer_image_verified = false;
@@ -2378,6 +2477,7 @@ static void runtime_service_destroy_unstarted(cbm_daemon_runtime_service_t *serv
23782477 cbm_daemon_ipc_listener_close (service -> listener );
23792478 cbm_daemon_coordinator_free (service -> coordinator );
23802479 (void )runtime_process_image_reference_release (& service -> active_image );
2480+ runtime_verified_peer_images_release (service );
23812481 free (service -> conflict_log_path );
23822482 for (size_t i = 0 ; i < service -> worker_mutexes_initialized ; i ++ ) {
23832483 cbm_mutex_destroy (& service -> workers [i ].send_mutex );
@@ -2727,6 +2827,7 @@ bool cbm_daemon_runtime_service_free(cbm_daemon_runtime_service_t *service) {
27272827 }
27282828 cbm_daemon_coordinator_free (service -> coordinator );
27292829 (void )runtime_process_image_reference_release (& service -> active_image );
2830+ runtime_verified_peer_images_release (service );
27302831 free (service -> conflict_log_path );
27312832 for (size_t i = 0 ; i < service -> worker_mutexes_initialized ; i ++ ) {
27322833 cbm_mutex_destroy (& service -> workers [i ].send_mutex );
0 commit comments