Skip to content

Commit 45bac49

Browse files
committed
fix(daemon): cache only the process's own image fingerprint
The fingerprint cache sat in every fingerprinting acquire, including peer verification. Limit it to the process's own image: every acquire site now passes the process id to the shared resolve step, which marks a peer's key invalid, so a peer is never looked up, never committed, and is hashed on every fingerprint. Re-hashing of verified peer copies is handled by the runtime service's retained peer references (#2335). The daemon's saving is unchanged: it hashes its own image once at start instead of twice. A process has one own image, so the cache drops from 8 entries to 1, which removes the slot search and the round-robin eviction. The cache comment no longer claims that SetFileTime cannot reset the Windows change time; SetFileInformationByHandle can set it. Limited to the own image, the key does not rely on it: Windows keeps a running image locked against modification for as long as the process, and so the cache, exists. Test: daemon_runtime_process_fingerprint_caches_only_own_image spawns this runner as a peer, whose image has the same file identity as the own image, and asserts that two peer fingerprints cost two hashes, two own fingerprints one more, and a later peer fingerprint one more again. Before this change the second peer fingerprint was served from the cache. Signed-off-by: Martin Vogel <[email protected]>
1 parent 9545ea4 commit 45bac49

2 files changed

Lines changed: 138 additions & 87 deletions

File tree

‎src/daemon/runtime.c‎

Lines changed: 76 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -754,23 +754,31 @@ static bool runtime_posix_stat_same_image(const struct stat *first, const struct
754754

755755
/* ── Executable-image fingerprint cache ──────────────────────────────────────
756756
* The build-identity fingerprint is a SHA-256 over the entire executable
757-
* (cbm_daemon_build_fingerprint_native_file): about a second of CPU per hash
758-
* for a ~300 MB image. A daemon process otherwise pays it twice at start (the
757+
* (cbm_daemon_build_fingerprint_native_file), a full read of a ~300 MB image.
758+
* A daemon process otherwise pays it twice for its own image at start (the
759759
* supervisor's startup capture, then the runtime service's active-image check),
760-
* so the digest is cached per process, keyed by the file identity the acquire
761-
* path verifies as stable across the hash: device, inode, size, mtime and
762-
* change time (on Windows FILE_BASIC_INFO.ChangeTime, which the kernel
763-
* maintains and SetFileTime cannot reset, unlike the creation time). A rebuilt
764-
* or replaced binary rolls that key and misses, so the cache never returns a
765-
* stale digest and build-cohort admission keeps its exact meaning. The cache is
766-
* process-local: a fresh process (a hook, a CLI call) still hashes its own image
767-
* once. A hit does not weaken the identity proof: the acquire chain still
768-
* brackets the held image with its before/after stat and process-maps checks,
769-
* so the returned digest is bound to that same verified image; only the re-read
770-
* of bytes whose identity is unchanged is skipped. A freshly hashed digest is
771-
* committed only after that post-hash check has passed, under the identity
772-
* observed after the hash, so an image that changed under the hash is never
773-
* cached. */
760+
* so the digest of this process's OWN image is cached, keyed by the file
761+
* identity the acquire path verifies as stable across the hash: device, inode,
762+
* size, mtime and change time (on Windows FILE_BASIC_INFO.ChangeTime). A peer's
763+
* image is never cached: it is hashed on every fingerprinting acquire
764+
* (re-hashing of a verified peer copy is avoided separately, #1955).
765+
*
766+
* Limiting the cache to the own image is what makes that key sufficient. On
767+
* POSIX the change time moves on every write and cannot be set back. A Windows
768+
* change time can be set through SetFileInformationByHandle, but Windows keeps
769+
* a running image locked against modification for as long as the process, and
770+
* so this cache, exists. A rebuilt or replaced binary rolls the key and misses,
771+
* so the cache never returns a stale digest and build-cohort admission keeps
772+
* its exact meaning. One entry suffices: a process has one own image.
773+
*
774+
* The cache is process-local: a fresh process (a hook, a CLI call) still hashes
775+
* its own image once. A hit does not weaken the identity proof: the acquire
776+
* chain still brackets the held image with its before/after stat and
777+
* process-maps checks, so the returned digest is bound to that same verified
778+
* image; only the re-read of bytes whose identity is unchanged is skipped. A
779+
* freshly hashed digest is committed only after that post-hash check has
780+
* passed, under the identity observed after the hash, so an image that changed
781+
* under the hash is never cached. */
774782
#if defined(_WIN32) || defined(__APPLE__) || defined(__linux__) || defined(__FreeBSD__) || \
775783
defined(__NetBSD__)
776784

@@ -787,15 +795,10 @@ typedef struct {
787795
int64_t ctime_nanoseconds;
788796
} runtime_fingerprint_key_t;
789797

790-
#define RUNTIME_FINGERPRINT_CACHE_CAP 8
791-
798+
/* The single entry: this process's own image. */
792799
static atomic_flag runtime_fingerprint_cache_lock = ATOMIC_FLAG_INIT;
793-
static struct {
794-
runtime_fingerprint_key_t key;
795-
char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
796-
} runtime_fingerprint_cache[RUNTIME_FINGERPRINT_CACHE_CAP];
797-
static size_t runtime_fingerprint_cache_count;
798-
static size_t runtime_fingerprint_cache_victim;
800+
static runtime_fingerprint_key_t runtime_fingerprint_cache_key;
801+
static char runtime_fingerprint_cache_digest[CBM_DAEMON_BUILD_FINGERPRINT_SIZE];
799802

800803
#ifdef CBM_ENABLE_TEST_SEAMS
801804
static atomic_int runtime_fingerprint_hash_call_count;
@@ -825,15 +828,10 @@ static bool runtime_fingerprint_cache_lookup(const runtime_fingerprint_key_t *ke
825828
if (!key->valid) {
826829
return false;
827830
}
828-
bool hit = false;
829831
runtime_fingerprint_cache_enter();
830-
for (size_t index = 0; index < runtime_fingerprint_cache_count; index++) {
831-
if (runtime_fingerprint_key_equal(&runtime_fingerprint_cache[index].key, key)) {
832-
memcpy(out, runtime_fingerprint_cache[index].fingerprint,
833-
CBM_DAEMON_BUILD_FINGERPRINT_SIZE);
834-
hit = true;
835-
break;
836-
}
832+
bool hit = runtime_fingerprint_key_equal(&runtime_fingerprint_cache_key, key);
833+
if (hit) {
834+
memcpy(out, runtime_fingerprint_cache_digest, CBM_DAEMON_BUILD_FINGERPRINT_SIZE);
837835
}
838836
runtime_fingerprint_cache_leave();
839837
return hit;
@@ -846,25 +844,8 @@ static void runtime_fingerprint_cache_store(
846844
return;
847845
}
848846
runtime_fingerprint_cache_enter();
849-
size_t slot = RUNTIME_FINGERPRINT_CACHE_CAP;
850-
for (size_t index = 0; index < runtime_fingerprint_cache_count; index++) {
851-
if (runtime_fingerprint_key_equal(&runtime_fingerprint_cache[index].key, key)) {
852-
slot = index;
853-
break;
854-
}
855-
}
856-
if (slot == RUNTIME_FINGERPRINT_CACHE_CAP) {
857-
if (runtime_fingerprint_cache_count < RUNTIME_FINGERPRINT_CACHE_CAP) {
858-
slot = runtime_fingerprint_cache_count++;
859-
} else {
860-
slot = runtime_fingerprint_cache_victim;
861-
runtime_fingerprint_cache_victim =
862-
(runtime_fingerprint_cache_victim + 1) % RUNTIME_FINGERPRINT_CACHE_CAP;
863-
}
864-
}
865-
runtime_fingerprint_cache[slot].key = *key;
866-
memcpy(runtime_fingerprint_cache[slot].fingerprint, fingerprint,
867-
CBM_DAEMON_BUILD_FINGERPRINT_SIZE);
847+
runtime_fingerprint_cache_key = *key;
848+
memcpy(runtime_fingerprint_cache_digest, fingerprint, CBM_DAEMON_BUILD_FINGERPRINT_SIZE);
868849
runtime_fingerprint_cache_leave();
869850
}
870851

@@ -883,24 +864,31 @@ static bool runtime_fingerprint_hash_native_file(uintptr_t native_file,
883864
return cbm_daemon_build_fingerprint_native_file(native_file, out);
884865
}
885866

886-
/* Resolve the digest for the image identity `key`: from the cache when it
887-
* already holds that identity, otherwise by hashing the held native image.
888-
* `cached` reports which. A freshly hashed digest is deliberately not stored
889-
* here: the acquire site commits it (runtime_fingerprint_cache_commit) only
890-
* once its post-hash identity check has proven the hashed bytes belong to the
891-
* identity they were keyed on. */
892-
static bool runtime_build_fingerprint_resolve(uintptr_t native_file,
893-
const runtime_fingerprint_key_t *key,
867+
/* Resolve the digest for the image identity `key` of process `process_id`:
868+
* from the cache when that process is this one and the cache already holds
869+
* that identity, otherwise by hashing the held native image. `cached` reports
870+
* which. Every acquire site resolves through here, so this is where the cache
871+
* is limited to the own image: a peer's key is marked invalid, which is never
872+
* looked up and never committed. A freshly hashed digest is deliberately not
873+
* stored here: the acquire site commits it (runtime_fingerprint_cache_commit)
874+
* only once its post-hash identity check has proven the hashed bytes belong to
875+
* the identity they were keyed on. */
876+
static bool runtime_build_fingerprint_resolve(uintptr_t native_file, uint64_t process_id,
877+
runtime_fingerprint_key_t *key,
894878
char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE],
895879
bool *cached) {
880+
if (process_id != runtime_current_process_id()) {
881+
key->valid = false;
882+
}
896883
*cached = runtime_fingerprint_cache_lookup(key, fingerprint);
897884
return *cached || runtime_fingerprint_hash_native_file(native_file, fingerprint);
898885
}
899886

900887
/* Commit a freshly hashed digest after the acquire chain has passed. The
901888
* identity observed after the hash must equal the one the digest was resolved
902889
* under; otherwise the bytes read belong to no single identity and are not
903-
* cached. A cache hit has nothing to commit. */
890+
* cached. A cache hit has nothing to commit, and neither has a peer, whose
891+
* resolved key is invalid. */
904892
static void runtime_fingerprint_cache_commit(
905893
const runtime_fingerprint_key_t *before, const runtime_fingerprint_key_t *after, bool cached,
906894
const char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) {
@@ -938,11 +926,11 @@ static void runtime_fingerprint_key_windows(HANDLE file,
938926
}
939927

940928
static bool runtime_build_fingerprint_resolve_windows(
941-
HANDLE file, const BY_HANDLE_FILE_INFORMATION *information, const LARGE_INTEGER *size,
942-
char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE], runtime_fingerprint_key_t *key,
943-
bool *cached) {
929+
HANDLE file, uint64_t process_id, const BY_HANDLE_FILE_INFORMATION *information,
930+
const LARGE_INTEGER *size, char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE],
931+
runtime_fingerprint_key_t *key, bool *cached) {
944932
runtime_fingerprint_key_windows(file, information, size, key);
945-
return runtime_build_fingerprint_resolve((uintptr_t)file, key, fingerprint, cached);
933+
return runtime_build_fingerprint_resolve((uintptr_t)file, process_id, key, fingerprint, cached);
946934
}
947935

948936
static void runtime_fingerprint_cache_commit_windows(
@@ -977,11 +965,11 @@ static void runtime_fingerprint_key_posix(const struct stat *status,
977965
}
978966

979967
static bool runtime_build_fingerprint_resolve_posix(
980-
uintptr_t native_file, const struct stat *status,
968+
uintptr_t native_file, uint64_t process_id, const struct stat *status,
981969
char fingerprint[CBM_DAEMON_BUILD_FINGERPRINT_SIZE], runtime_fingerprint_key_t *key,
982970
bool *cached) {
983971
runtime_fingerprint_key_posix(status, key);
984-
return runtime_build_fingerprint_resolve(native_file, key, fingerprint, cached);
972+
return runtime_build_fingerprint_resolve(native_file, process_id, key, fingerprint, cached);
985973
}
986974

987975
static void runtime_fingerprint_cache_commit_posix(
@@ -996,9 +984,8 @@ static void runtime_fingerprint_cache_commit_posix(
996984
#ifdef CBM_ENABLE_TEST_SEAMS
997985
void cbm_daemon_runtime_fingerprint_cache_reset_for_testing(void) {
998986
runtime_fingerprint_cache_enter();
999-
memset(runtime_fingerprint_cache, 0, sizeof(runtime_fingerprint_cache));
1000-
runtime_fingerprint_cache_count = 0;
1001-
runtime_fingerprint_cache_victim = 0;
987+
memset(&runtime_fingerprint_cache_key, 0, sizeof(runtime_fingerprint_cache_key));
988+
memset(runtime_fingerprint_cache_digest, 0, sizeof(runtime_fingerprint_cache_digest));
1002989
runtime_fingerprint_cache_leave();
1003990
atomic_store_explicit(&runtime_fingerprint_hash_call_count, 0, memory_order_relaxed);
1004991
atomic_store_explicit(&runtime_fingerprint_hash_stub_active, false, memory_order_release);
@@ -1041,15 +1028,17 @@ bool cbm_daemon_runtime_fingerprint_cache_resolve_for_testing(
10411028
const cbm_daemon_runtime_fingerprint_identity_for_testing_t *after,
10421029
char out[CBM_DAEMON_BUILD_FINGERPRINT_SIZE]) {
10431030
/* There is no native image behind a synthetic identity: only the stubbed
1044-
* hash may ever be reached from here. */
1031+
* hash may ever be reached from here. The identity stands for this
1032+
* process's own image, the only one the cache serves. */
10451033
if (!before || !after || !out ||
10461034
!atomic_load_explicit(&runtime_fingerprint_hash_stub_active, memory_order_acquire)) {
10471035
return false;
10481036
}
10491037
runtime_fingerprint_key_t key_before = runtime_fingerprint_key_for_testing(before);
10501038
runtime_fingerprint_key_t key_after = runtime_fingerprint_key_for_testing(after);
10511039
bool cached = false;
1052-
if (!runtime_build_fingerprint_resolve((uintptr_t)0, &key_before, out, &cached)) {
1040+
if (!runtime_build_fingerprint_resolve((uintptr_t)0, runtime_current_process_id(), &key_before,
1041+
out, &cached)) {
10531042
return false;
10541043
}
10551044
runtime_fingerprint_cache_commit(&key_before, &key_after, cached, out);
@@ -1102,9 +1091,9 @@ static bool runtime_process_image_reference_acquire(
11021091
: INVALID_HANDLE_VALUE;
11031092
free(open_path);
11041093
ok = ok && runtime_windows_file_snapshot(file, &file_before, &size_before) &&
1105-
(!fingerprint ||
1106-
runtime_build_fingerprint_resolve_windows(file, &file_before, &size_before, fingerprint,
1107-
&fingerprint_key, &fingerprint_cached)) &&
1094+
(!fingerprint || runtime_build_fingerprint_resolve_windows(
1095+
file, process_id, &file_before, &size_before, fingerprint,
1096+
&fingerprint_key, &fingerprint_cached)) &&
11081097
runtime_windows_file_snapshot(file, &file_after, &size_after) &&
11091098
runtime_windows_file_snapshot_same(&file_before, &size_before, &file_after, &size_after) &&
11101099
runtime_windows_process_image_snapshot(process, &process_after) &&
@@ -1146,9 +1135,9 @@ static bool runtime_process_image_reference_acquire(
11461135
bool fingerprint_cached = false;
11471136
ok = ok && fd >= 0 && fstat(fd, &file_before) == 0 && S_ISREG(file_before.st_mode) &&
11481137
runtime_mac_process_maps_file_executable(pid, &file_before) &&
1149-
(!fingerprint ||
1150-
runtime_build_fingerprint_resolve_posix((uintptr_t)fd, &file_before, fingerprint,
1151-
&fingerprint_key, &fingerprint_cached)) &&
1138+
(!fingerprint || runtime_build_fingerprint_resolve_posix(
1139+
(uintptr_t)fd, process_id, &file_before, fingerprint,
1140+
&fingerprint_key, &fingerprint_cached)) &&
11521141
fstat(fd, &file_after) == 0 && runtime_mac_stat_same(&file_before, &file_after) &&
11531142
runtime_mac_process_maps_file_executable(pid, &file_after) &&
11541143
runtime_mac_process_instance(pid, &process_after) &&
@@ -1177,13 +1166,13 @@ static bool runtime_process_image_reference_acquire(
11771166
struct stat image_after;
11781167
runtime_fingerprint_key_t fingerprint_key = {0};
11791168
bool fingerprint_cached = false;
1180-
bool ok =
1181-
image_fd >= 0 && fstat(image_fd, &image_before) == 0 && S_ISREG(image_before.st_mode) &&
1182-
(!fingerprint ||
1183-
runtime_build_fingerprint_resolve_posix((uintptr_t)image_fd, &image_before, fingerprint,
1184-
&fingerprint_key, &fingerprint_cached)) &&
1185-
fstat(image_fd, &image_after) == 0 &&
1186-
runtime_posix_stat_same_image(&image_before, &image_after);
1169+
bool ok = image_fd >= 0 && fstat(image_fd, &image_before) == 0 &&
1170+
S_ISREG(image_before.st_mode) &&
1171+
(!fingerprint || runtime_build_fingerprint_resolve_posix(
1172+
(uintptr_t)image_fd, process_id, &image_before, fingerprint,
1173+
&fingerprint_key, &fingerprint_cached)) &&
1174+
fstat(image_fd, &image_after) == 0 &&
1175+
runtime_posix_stat_same_image(&image_before, &image_after);
11871176
int verify_fd = ok ? openat(process_fd, "exe", O_RDONLY | O_CLOEXEC) : -1;
11881177
struct stat verify_status;
11891178
ok = ok && verify_fd >= 0 && fstat(verify_fd, &verify_status) == 0 &&
@@ -1227,9 +1216,9 @@ static bool runtime_process_image_reference_acquire(
12271216
runtime_fingerprint_key_t fingerprint_key = {0};
12281217
bool fingerprint_cached = false;
12291218
ok = image_fd >= 0 && fstat(image_fd, &image_before) == 0 && S_ISREG(image_before.st_mode) &&
1230-
(!fingerprint ||
1231-
runtime_build_fingerprint_resolve_posix((uintptr_t)image_fd, &image_before, fingerprint,
1232-
&fingerprint_key, &fingerprint_cached)) &&
1219+
(!fingerprint || runtime_build_fingerprint_resolve_posix(
1220+
(uintptr_t)image_fd, process_id, &image_before, fingerprint,
1221+
&fingerprint_key, &fingerprint_cached)) &&
12331222
fstat(image_fd, &image_after) == 0 &&
12341223
runtime_posix_stat_same_image(&image_before, &image_after);
12351224
if (ok) {

0 commit comments

Comments
 (0)