Skip to content

Commit f4ff01b

Browse files
authored
Merge pull request DeusData#2410 from DeusData/fix/test-runner-isolate-runtime-dir
fix(test): isolate the C test runners' daemon runtime
2 parents 8b8cafb + 533eee0 commit f4ff01b

6 files changed

Lines changed: 182 additions & 16 deletions

File tree

‎src/daemon/bootstrap.c‎

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,21 +241,49 @@ static const char *bootstrap_runtime_parent_override(char *buffer, size_t capaci
241241
return value && value[0] != '\0' ? value : NULL;
242242
}
243243

244+
/* An explicit parent keeps precedence: it carries the compile-time test seam
245+
* and the lifecycle guards' isolated namespace. NULL means the account-wide
246+
* default rendezvous. */
247+
static const char *bootstrap_runtime_parent_resolve(const char *runtime_parent, char *buffer,
248+
size_t capacity) {
249+
return runtime_parent ? runtime_parent : bootstrap_runtime_parent_override(buffer, capacity);
250+
}
251+
252+
#ifdef CBM_ENABLE_TEST_SEAMS
253+
/* Armed once by a test runner before any suite starts its threads. */
254+
static bool g_bootstrap_default_runtime_forbidden = false;
255+
256+
void cbm_daemon_bootstrap_forbid_default_runtime_for_test(bool forbid) {
257+
g_bootstrap_default_runtime_forbidden = forbid;
258+
}
259+
260+
bool cbm_daemon_bootstrap_default_runtime_refused_for_test(const char *runtime_parent) {
261+
char override_parent[BOOTSTRAP_PATH_CAP];
262+
return g_bootstrap_default_runtime_forbidden &&
263+
!bootstrap_runtime_parent_resolve(runtime_parent, override_parent,
264+
sizeof(override_parent));
265+
}
266+
#endif
267+
244268
cbm_daemon_ipc_endpoint_t *cbm_daemon_bootstrap_endpoint_new(const char *runtime_parent) {
245269
char key[CBM_DAEMON_KEY_SIZE];
246270
if (!cbm_daemon_rendezvous_key(key)) {
247271
return NULL;
248272
}
249-
/* An explicit parent keeps precedence: it carries the compile-time test
250-
* seam and the lifecycle guards' isolated namespace. The override is
251-
* resolved HERE, the one function every product endpoint goes through
252-
* (daemon, MCP client, local CLI, index worker, activation), so no call
253-
* site can silently keep the default. */
273+
/* The override is resolved HERE, the one function every product endpoint
274+
* goes through (daemon, MCP client, local CLI, index worker, activation),
275+
* so no call site can silently keep the default. */
254276
char override_parent[BOOTSTRAP_PATH_CAP];
255277
const char *parent =
256-
runtime_parent
257-
? runtime_parent
258-
: bootstrap_runtime_parent_override(override_parent, sizeof(override_parent));
278+
bootstrap_runtime_parent_resolve(runtime_parent, override_parent, sizeof(override_parent));
279+
#ifdef CBM_ENABLE_TEST_SEAMS
280+
if (!parent && g_bootstrap_default_runtime_forbidden) {
281+
(void)fprintf(stderr, "codebase-memory-mcp: test run refused the default daemon runtime "
282+
"directory (no explicit parent and CBM_RUNTIME_DIR unset): the "
283+
"developer's live daemon listens there\n");
284+
return NULL;
285+
}
286+
#endif
259287
return cbm_daemon_ipc_endpoint_new(key, parent);
260288
}
261289

‎src/daemon/bootstrap.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ bool cbm_daemon_process_role_requires_client(cbm_daemon_process_role_t role);
4545
* ABI values must never create parallel daemon namespaces. */
4646
cbm_daemon_ipc_endpoint_t *cbm_daemon_bootstrap_endpoint_new(const char *runtime_parent);
4747

48+
#ifdef CBM_ENABLE_TEST_SEAMS
49+
/* Test runners only; no product binary arms it. Once armed, an endpoint
50+
* request that names no runtime parent while CBM_RUNTIME_DIR is unset (one
51+
* that would resolve to the account-wide default rendezvous, where the
52+
* developer's live daemon listens) is refused with a diagnostic on stderr. */
53+
void cbm_daemon_bootstrap_forbid_default_runtime_for_test(bool forbid);
54+
/* The same decision cbm_daemon_bootstrap_endpoint_new makes, without
55+
* resolving anything: true when that call would be refused. */
56+
bool cbm_daemon_bootstrap_default_runtime_refused_for_test(const char *runtime_parent);
57+
#endif
58+
4859
/* Cross-platform launch policy for the daemon child. The child is invoked
4960
* directly (never through a shell), with exactly argv[0] plus the one hidden
5061
* internal argument. It is detached from the launching client's lifetime and

‎tests/repro/repro_main.c‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ int tf_skip_count = 0;
2828
#include "test_framework.h"
2929
#include "repro_runner.h"
3030
#include "foundation/compat.h" /* cbm_setenv — #845 supervisor kill switch */
31+
#include "daemon/bootstrap.h" /* refuse the developer's default daemon rendezvous */
3132

3233
/* Per-suite summary + filter. RUN_SUITE prints a one-line
3334
* "[SUITE] <name> P passed, F failed" report (greppable for which suites still
@@ -131,6 +132,13 @@ int main(void) {
131132
* supervisor gate already ignores unmarked hosts; pin the kill switch too.
132133
* A test that exercises the supervisor must explicitly re-enable it. */
133134
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
135+
#ifdef CBM_ENABLE_TEST_SEAMS
136+
/* No reproduction needs the daemon rendezvous. Its default is the
137+
* developer's live daemon whatever HOME says, so a case that ever resolves
138+
* an endpoint without an explicit parent or CBM_RUNTIME_DIR is refused
139+
* loudly instead of reaching it. */
140+
cbm_daemon_bootstrap_forbid_default_runtime_for_test(true);
141+
#endif
134142

135143
/* Unbuffered: a reproduction may crash/_exit (or a sanitizer may _exit on a
136144
* leak) before stdio flushes — keep every printed line so the summary and the

‎tests/test_daemon_bootstrap.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,11 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
400400
char relocated_runtime[BOOTSTRAP_TEST_PATH_CAP] = {0};
401401
char explicit_runtime[BOOTSTRAP_TEST_PATH_CAP] = {0};
402402
char unusable[BOOTSTRAP_TEST_PATH_CAP] = {0};
403+
/* The runner isolates the rendezvous for the whole run through this same
404+
* variable, so the test must hand back exactly what it found. */
405+
char previous_value[BOOTSTRAP_TEST_PATH_CAP] = {0};
406+
const char *previous =
407+
cbm_safe_getenv("CBM_RUNTIME_DIR", previous_value, sizeof(previous_value), NULL);
403408
int written = snprintf(override_parent, sizeof(override_parent),
404409
"%s/cbm-bootstrap-runtime-env-XXXXXX", cbm_tmpdir());
405410
if (written <= 0 || written >= (int)sizeof(override_parent) || !cbm_mkdtemp(override_parent)) {
@@ -436,8 +441,10 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
436441
unusable_set ? cbm_daemon_bootstrap_endpoint_new(NULL) : NULL;
437442

438443
/* Restore before asserting: a failed assertion returns immediately, and a
439-
* leaked CBM_RUNTIME_DIR would follow every later suite in this process. */
440-
(void)cbm_unsetenv("CBM_RUNTIME_DIR");
444+
* leaked override, or a dropped run value, would follow every later suite
445+
* in this process. */
446+
bool restored = previous ? cbm_setenv("CBM_RUNTIME_DIR", previous_value, 1) == 0
447+
: cbm_unsetenv("CBM_RUNTIME_DIR") == 0;
441448
cbm_daemon_ipc_endpoint_free(refused);
442449
cbm_daemon_ipc_endpoint_free(relocated);
443450
if (relocated_runtime[0] != '\0') {
@@ -448,6 +455,7 @@ TEST(daemon_bootstrap_runtime_dir_env_relocates_rendezvous) {
448455
}
449456
(void)cbm_rmdir(override_parent);
450457

458+
ASSERT_TRUE(restored);
451459
ASSERT_TRUE(prepared);
452460
ASSERT_TRUE(explicit_started);
453461
ASSERT_TRUE(explicit_canonical);

‎tests/test_helpers.h‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,19 @@ static inline char *th_mktempdir(const char *prefix) {
180180
* leaf made by cbm_mkdtemp() has a private DACL. Keep these security-sensitive
181181
* fixtures under LocalAppData on Windows; preserve the ordinary temporary-root
182182
* behavior on POSIX. */
183+
static inline const char *th_secure_runtime_base(void) {
184+
#ifdef _WIN32
185+
return cbm_app_local_dir();
186+
#else
187+
return cbm_tmpdir();
188+
#endif
189+
}
190+
183191
static inline bool th_secure_runtime_parent_new(char *out, size_t out_cap, const char *tag) {
184192
if (!out || out_cap == 0 || !tag || !tag[0]) {
185193
return false;
186194
}
187-
#ifdef _WIN32
188-
const char *base = cbm_app_local_dir();
189-
#else
190-
const char *base = cbm_tmpdir();
191-
#endif
195+
const char *base = th_secure_runtime_base();
192196
if (!base || !base[0]) {
193197
out[0] = '\0';
194198
return false;

‎tests/test_main.c‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ int tf_skip_count = 0;
1818
#include "foundation/mem.h" /* cbm_mem_init — worker budget */
1919
#include "foundation/log.h" /* worker liveness heartbeat probe */
2020
#include "foundation/platform.h" /* cbm_file_exists — blocking-git marker */
21+
#include "daemon/bootstrap.h" /* runner rendezvous isolation */
2122
#include "daemon/runtime.h" /* bounded worker response probe */
2223
#include "daemon/ipc.h" /* Windows private-lock re-exec probe */
2324
#include "daemon/version_cohort.h" /* Windows crash-turnover re-exec probe */
@@ -187,11 +188,15 @@ static int tf_maybe_run_blocking_git_probe(int argc, char **argv) {
187188
* this sentinel and restore it; atexit removes anything left behind by an
188189
* assertion that returns before fixture cleanup. */
189190
static char tf_home_sentinel[512];
191+
static char tf_runtime_sentinel[512];
190192

191193
static void tf_cleanup_cache_sentinel(void) {
192194
if (tf_home_sentinel[0]) {
193195
th_rmtree(tf_home_sentinel);
194196
}
197+
if (tf_runtime_sentinel[0]) {
198+
th_rmtree(tf_runtime_sentinel);
199+
}
195200
}
196201

197202
/* Client home overrides the CLI honours BEFORE $HOME, so redirecting HOME alone
@@ -243,9 +248,108 @@ static bool tf_setup_cache_sentinel(void) {
243248
cbm_unsetenv(tf_client_home_overrides[i]);
244249
}
245250
atexit(tf_cleanup_cache_sentinel);
251+
/* HOME and CBM_CACHE_DIR do not move the daemon rendezvous: its key is a
252+
* constant product hash under the system temp root. Without this, an
253+
* install/uninstall/update test or any endpoint resolved with no explicit
254+
* parent reaches the developer's live daemon. The run always gets a fresh
255+
* private directory; an inherited value is replaced, never reused (the
256+
* rule scripts/test-runtime.sh applies to the shell harness).
257+
* th_secure_runtime_parent_new anchors it where the IPC ancestry check
258+
* accepts it (LocalAppData on Windows). */
259+
if (!th_secure_runtime_parent_new(tf_runtime_sentinel, sizeof(tf_runtime_sentinel), "run")) {
260+
tf_runtime_sentinel[0] = '\0';
261+
return false;
262+
}
263+
if (cbm_setenv("CBM_RUNTIME_DIR", tf_runtime_sentinel, 1) != 0) {
264+
return false;
265+
}
266+
#ifdef CBM_ENABLE_TEST_SEAMS
267+
/* Belt and braces: a fixture that later drops the variable gets a loud
268+
* refusal instead of the default rendezvous. */
269+
cbm_daemon_bootstrap_forbid_default_runtime_for_test(true);
270+
#endif
246271
return true;
247272
}
248273

274+
static bool tf_path_has_parent(const char *path, const char *parent) {
275+
size_t length = parent ? strlen(parent) : 0;
276+
return path && length > 0 && strncmp(path, parent, length) == 0 &&
277+
(path[length] == '/' || path[length] == '\\');
278+
}
279+
280+
/* The daemon rendezvous is not derived from HOME or the cache: its key is a
281+
* constant product hash and its default parent is the system temp root. With
282+
* CBM_RUNTIME_DIR unset, every endpoint a test resolves is therefore the
283+
* developer's live daemon socket (<tmp>/cbm-daemon-<uid>/cbm-<key>.sock).
284+
* Every check runs before anything is resolved, so a run without isolation
285+
* fails here and this test never reaches that socket itself. */
286+
TEST(runner_isolation_daemon_runtime_dir_is_private) {
287+
char runtime_env[sizeof(tf_runtime_sentinel)] = {0};
288+
const char *value = cbm_safe_getenv("CBM_RUNTIME_DIR", runtime_env, sizeof(runtime_env), NULL);
289+
ASSERT_NOT_NULL(value);
290+
ASSERT_TRUE(value[0] != '\0');
291+
/* The fresh per-run directory, never a value inherited from the caller. */
292+
ASSERT_STR_EQ(value, tf_runtime_sentinel);
293+
294+
const char *root = th_secure_runtime_base();
295+
ASSERT_NOT_NULL(root);
296+
char canonical_root[CBM_SZ_4K] = {0};
297+
char canonical_run[CBM_SZ_4K] = {0};
298+
ASSERT_TRUE(cbm_canonical_path(root, canonical_root, sizeof(canonical_root)) != 0);
299+
ASSERT_TRUE(cbm_canonical_path(value, canonical_run, sizeof(canonical_run)) != 0);
300+
/* Strictly below the temp root: the root itself is the default rendezvous
301+
* parent (/tmp on POSIX, LocalAppData on Windows). */
302+
ASSERT_TRUE(tf_path_has_parent(canonical_run, canonical_root));
303+
304+
/* Only now resolve through the product resolver. A NULL parent is what
305+
* every product call site passes: daemon, MCP client, CLI, activation. */
306+
cbm_daemon_ipc_endpoint_t *endpoint = cbm_daemon_bootstrap_endpoint_new(NULL);
307+
const char *resolved = endpoint ? cbm_daemon_ipc_endpoint_runtime_dir(endpoint) : NULL;
308+
char canonical_resolved[CBM_SZ_4K] = {0};
309+
bool resolved_ok = resolved && cbm_canonical_path(resolved, canonical_resolved,
310+
sizeof(canonical_resolved)) != 0;
311+
cbm_daemon_ipc_endpoint_free(endpoint);
312+
ASSERT_TRUE(resolved_ok);
313+
ASSERT_TRUE(tf_path_has_parent(canonical_resolved, canonical_run));
314+
#ifndef _WIN32
315+
char live_default[CBM_SZ_4K];
316+
(void)snprintf(live_default, sizeof(live_default), "%s/cbm-daemon-%lu", canonical_root,
317+
(unsigned long)geteuid());
318+
ASSERT_TRUE(strcmp(canonical_resolved, live_default) != 0);
319+
#endif
320+
PASS();
321+
}
322+
323+
#ifdef CBM_ENABLE_TEST_SEAMS
324+
/* The runner arms the bootstrap guard, so a fixture that drops the variable
325+
* gets a refusal instead of the default rendezvous. Only the decision is
326+
* exercised: no endpoint is ever built while the variable is absent. */
327+
TEST(runner_isolation_refuses_default_daemon_runtime) {
328+
ASSERT_TRUE(tf_runtime_sentinel[0] != '\0');
329+
bool allowed_with_run_dir = !cbm_daemon_bootstrap_default_runtime_refused_for_test(NULL);
330+
331+
(void)cbm_unsetenv("CBM_RUNTIME_DIR");
332+
bool refused_without = cbm_daemon_bootstrap_default_runtime_refused_for_test(NULL);
333+
bool explicit_allowed =
334+
!cbm_daemon_bootstrap_default_runtime_refused_for_test(tf_runtime_sentinel);
335+
/* Restore before asserting: a failed assertion returns immediately. */
336+
bool restored = cbm_setenv("CBM_RUNTIME_DIR", tf_runtime_sentinel, 1) == 0;
337+
338+
ASSERT_TRUE(restored);
339+
ASSERT_TRUE(allowed_with_run_dir);
340+
ASSERT_TRUE(refused_without);
341+
ASSERT_TRUE(explicit_allowed);
342+
PASS();
343+
}
344+
#endif
345+
346+
SUITE(runner_isolation) {
347+
RUN_TEST(runner_isolation_daemon_runtime_dir_is_private);
348+
#ifdef CBM_ENABLE_TEST_SEAMS
349+
RUN_TEST(runner_isolation_refuses_default_daemon_runtime);
350+
#endif
351+
}
352+
249353
/* Fast real-process probes for the async index-supervisor contract. They run
250354
* only in a child admitted by the exact build-bound worker grammar. */
251355
static void tf_index_worker_probe(const char *args_json, const char *response_out) {
@@ -1056,7 +1160,7 @@ int main(int argc, char **argv) {
10561160
* A test that exercises the supervisor must explicitly re-enable it. */
10571161
cbm_setenv("CBM_INDEX_SUPERVISOR", "0", 1);
10581162
if (!tf_setup_cache_sentinel()) {
1059-
fprintf(stderr, "failed to create isolated test cache\n");
1163+
fprintf(stderr, "failed to create isolated test cache and daemon runtime\n");
10601164
return 2;
10611165
}
10621166

@@ -1080,6 +1184,9 @@ int main(int argc, char **argv) {
10801184
printf("\n codebase-memory-mcp C test suite\n");
10811185
}
10821186

1187+
/* Runner isolation first: nothing below may reach the developer's daemon. */
1188+
RUN_SELECTED_SUITE(runner_isolation);
1189+
10831190
/* Foundation */
10841191
RUN_SELECTED_SUITE(arena);
10851192
RUN_SELECTED_SUITE(hash_table);

0 commit comments

Comments
 (0)