Skip to content

Commit 30a410b

Browse files
authored
Merge pull request DeusData#2335 from DeusData/fix/issue-1955
fix(daemon): hash a verified peer image copy once, not on every HELLO (DeusData#1955)
2 parents 78795ad + 1c73f86 commit 30a410b

3 files changed

Lines changed: 205 additions & 44 deletions

File tree

‎src/daemon/runtime.c‎

Lines changed: 145 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,23 @@ static atomic_bool runtime_force_peer_image_mismatch_seam;
3636
void 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;
5976
void 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. */
921978
static 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

965991
bool 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+
9831089
static 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);

‎src/daemon/runtime.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,12 @@ void cbm_daemon_runtime_force_peer_image_unverified_for_testing(bool force);
450450
* i.e. the tamper case that must still be rejected after unverifiable images
451451
* became admissible. */
452452
void cbm_daemon_runtime_force_peer_image_mismatch_for_testing(bool force);
453+
/* #1955 test seam: make the peer look like a different file with identical
454+
* bytes (second install path, package-manager copy) so the fingerprint
455+
* fallback runs; the counter reports how many full-image fingerprints the
456+
* HELLO path has computed in this process. */
457+
void cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(bool force);
458+
uint64_t cbm_daemon_runtime_peer_image_hashes_for_testing(void);
453459
/* Abandoned-request containment seams (2026-08-29 zombie incident). The
454460
* timeout override shrinks the join ceiling to test scale; zero restores the
455461
* production constant. The hook replaces the terminal containment stop with a

‎tests/test_daemon_runtime.c‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,59 @@ TEST(daemon_runtime_unverifiable_image_is_admitted_issue1539) {
19181918
PASS();
19191919
}
19201920

1921+
/* #1955: a client whose binary is a DIFFERENT file with identical bytes (a
1922+
* second install path, a package-manager copy) misses the active-image
1923+
* comparison, so the daemon proves it by hashing the peer's whole image. With
1924+
* a ~300 MB release binary that hash costs more than the client's 1000 ms
1925+
* HELLO budget: the client gave up, re-probed, and every re-probe started the
1926+
* same hash again on a fresh worker, so such a client waited out the full
1927+
* 30 s startup deadline against a healthy daemon (worse under indexing load).
1928+
* A verified image must be remembered: repeated admissions of the same copy
1929+
* hash it once. Asserted on the hash count, never on time. */
1930+
TEST(daemon_runtime_verified_peer_copy_is_hashed_once_issue1955) {
1931+
/* The first admission really hashes the test binary (hundreds of MB,
1932+
* slower still under sanitizers), so its HELLO gets a hang-guard ceiling,
1933+
* not a budget: the verdict is the hash count below. */
1934+
enum { ADMISSIONS = 3, HASHED_HELLO_CEILING_MS = 120000 };
1935+
cbm_daemon_build_identity_t identity =
1936+
runtime_test_identity("2.4.0", runtime_test_self_build());
1937+
runtime_test_fixture_t fixture;
1938+
bool started = runtime_test_fixture_start(&fixture, "image-copy-cache", &identity);
1939+
int admitted = 0;
1940+
/* An owner session keeps the ephemeral generation alive across the
1941+
* sequential admissions below (the last committed client leaving retires
1942+
* it). It connects before the seam, through the active-image fast path. */
1943+
cbm_daemon_runtime_connect_result_t owner_result = {0};
1944+
cbm_daemon_runtime_client_t *owner =
1945+
started ? cbm_daemon_runtime_client_connect(fixture.endpoint, &identity,
1946+
RUNTIME_TEST_TIMEOUT_MS, &owner_result)
1947+
: NULL;
1948+
uint64_t hashes_before = cbm_daemon_runtime_peer_image_hashes_for_testing();
1949+
1950+
cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(true);
1951+
for (int i = 0; owner && i < ADMISSIONS; i++) {
1952+
cbm_daemon_runtime_connect_result_t result = {0};
1953+
cbm_daemon_runtime_client_t *client = cbm_daemon_runtime_client_connect(
1954+
fixture.endpoint, &identity, HASHED_HELLO_CEILING_MS, &result);
1955+
if (client && result.status == CBM_DAEMON_RUNTIME_CONNECT_ACCEPTED &&
1956+
cbm_daemon_runtime_client_close(client, RUNTIME_TEST_TIMEOUT_MS)) {
1957+
admitted++;
1958+
} else if (client) {
1959+
(void)cbm_daemon_runtime_client_close(client, RUNTIME_TEST_TIMEOUT_MS);
1960+
}
1961+
}
1962+
cbm_daemon_runtime_force_peer_image_distinct_copy_for_testing(false);
1963+
uint64_t hashes = cbm_daemon_runtime_peer_image_hashes_for_testing() - hashes_before;
1964+
bool owner_closed = owner && cbm_daemon_runtime_client_close(owner, RUNTIME_TEST_TIMEOUT_MS);
1965+
runtime_test_fixture_finish(&fixture);
1966+
1967+
ASSERT_TRUE(started);
1968+
ASSERT_TRUE(owner_closed);
1969+
ASSERT_EQ(admitted, ADMISSIONS);
1970+
ASSERT_EQ(hashes, 1);
1971+
PASS();
1972+
}
1973+
19211974
TEST(daemon_runtime_unexpected_frame_payload_is_freed_once) {
19221975
static const uint8_t unexpected_payload[] = {0xde, 0xad, 0xbe, 0xef};
19231976
cbm_daemon_build_identity_t identity =
@@ -5119,6 +5172,7 @@ SUITE(daemon_runtime) {
51195172
RUN_TEST(daemon_runtime_exact_hello_issues_connection_bound_identity);
51205173
RUN_TEST(daemon_runtime_image_rejection_reaches_client_issue1383);
51215174
RUN_TEST(daemon_runtime_unverifiable_image_is_admitted_issue1539);
5175+
RUN_TEST(daemon_runtime_verified_peer_copy_is_hashed_once_issue1955);
51225176
RUN_TEST(daemon_runtime_unexpected_frame_payload_is_freed_once);
51235177
RUN_TEST(daemon_runtime_activation_rejects_forged_and_malformed_without_stop);
51245178
RUN_TEST(daemon_runtime_activation_ack_snapshots_then_interrupts_all_clients);

0 commit comments

Comments
 (0)