Skip to content

Commit 9da4d66

Browse files
authored
Merge pull request DeusData#2115 from DeusData/fix/activation-guard-home-scope
fix(cli): scope the activation guard to the install-target HOME so a sandbox install can't drain the host daemon
2 parents 042a58b + fa99ff6 commit 9da4d66

7 files changed

Lines changed: 715 additions & 16 deletions

File tree

‎scripts/run-test-wave.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,19 @@
2626
SUMMARY = re.compile(r"^ (?P<passed>[0-9]+) passed")
2727
FAILED = re.compile(r"(?:^|, )(?P<failed>[0-9]+) failed")
2828
SKIPPED = re.compile(r"(?:^|, )(?P<skipped>[0-9]+) skipped")
29-
SLOW_SUITES = frozenset(("incremental", "store_arch", "daemon_runtime"))
29+
# Suites whose honest runtime does not fit the default per-suite budget, and so
30+
# get --slow-timeout instead. This is a statement about SIZE, never about
31+
# flakiness: every suite here is deterministic and simply long, and a racy suite
32+
# must be made deterministic rather than given more clock.
33+
#
34+
# `cli` joined the list because the classification had gone stale, not because
35+
# anything regressed. It spends 497s of the 900s default on macos-14 -- the
36+
# FASTEST macOS runner -- while the macos-15-intel runner in the same matrix is
37+
# 2.4-3.6x slower on comparable suites (daemon_runtime 842s vs 349s,
38+
# stack_overflow_b 277s vs 76s). 497s at that ratio cannot fit, so the suite was
39+
# killed at 900s and reported as hung. daemon_runtime, at 842s on that same
40+
# runner, survives only because it was already listed here.
41+
SLOW_SUITES = frozenset(("incremental", "store_arch", "daemon_runtime", "cli"))
3042
POLL_SECONDS = 0.05
3143

3244
# WHY: the Windows descendant probe below is a cold `powershell.exe` + CIM

‎src/cli/cli.c‎

Lines changed: 150 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,12 @@ typedef struct {
215215
bool cleanup_ok;
216216
bool original_cache_environment_present;
217217
bool cache_environment_overridden;
218+
/* Scope: what THIS activation replaces decides whether any session must
219+
* be quiesced at all, and the target cache namespace decides WHICH
220+
* cohort. A skipped coordination holds no lock and drains nobody. */
221+
bool quiesce_required;
222+
bool coordination_skipped;
223+
char scope_detail[CBM_SZ_1K];
218224
} cli_activation_production_context_t;
219225

220226
static cbm_cli_activation_ops_t g_cli_activation_test_ops;
@@ -531,6 +537,91 @@ static void cli_activation_release_cleanup_lease(cli_activation_production_conte
531537
}
532538
}
533539

540+
typedef enum {
541+
CLI_ACTIVATION_SCOPE_ACTIVE = 0,
542+
CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE,
543+
CLI_ACTIVATION_SCOPE_FOREIGN_COHORT,
544+
CLI_ACTIVATION_SCOPE_ERROR,
545+
} cli_activation_scope_t;
546+
547+
static void cli_activation_log_guard_decision(const cli_activation_production_context_t *context,
548+
const char *decision, const char *active_cache) {
549+
char clients[32];
550+
(void)snprintf(clients, sizeof(clients), "%llu",
551+
(unsigned long long)context->daemon_result.active_clients);
552+
const char *scope = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
553+
cbm_log_info("activation.guard", "scope", scope ? scope : "", "cache",
554+
context->cache_fingerprint, "clients", clients, "decision", decision,
555+
"active_cache", active_cache ? active_cache : "");
556+
}
557+
558+
/* Whose sessions does this activation have to stop? The rendezvous directory
559+
* is per OS account (service.h), so the host daemon of another HOME /
560+
* CBM_CACHE_DIR meets this activation at the very same endpoint; only the
561+
* cache fingerprint in the cohort identity separates the namespaces. An
562+
* `install` into a sandbox HOME used to drain the host daemon and every MCP
563+
* client behind it although nothing it touched belonged to the host.
564+
*
565+
* Two questions, answered from the activation's own target: does it replace
566+
* anything at all (a --skip-binary install without an index reset publishes
567+
* nothing), and whose cohort is active. Admission with an immediate deadline
568+
* is the cheapest authoritative read of the active lifetime record: OK means
569+
* the cohort is ours or empty, CONFLICT names the active identity (its cache
570+
* fingerprint is filled for every conflict kind), BUSY means another
571+
* activation holds maintenance and the barrier waits for it as before. */
572+
static cli_activation_scope_t cli_activation_resolve_scope(
573+
cli_activation_production_context_t *context) {
574+
const char *runtime_dir = cbm_daemon_ipc_endpoint_runtime_dir(context->endpoint);
575+
const char *scope = runtime_dir ? runtime_dir : "";
576+
const char *action = cli_activation_action_text(context->action);
577+
if (!context->quiesce_required) {
578+
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
579+
"nothing to quiesce: published binary and indexes unchanged; scope=%s",
580+
scope);
581+
cli_activation_log_guard_decision(context, "nothing_to_replace", NULL);
582+
printf("No CBM session needs to stop for %s: the published binary and indexes are "
583+
"unchanged.\n",
584+
action);
585+
(void)fflush(stdout);
586+
return CLI_ACTIVATION_SCOPE_NOTHING_TO_REPLACE;
587+
}
588+
cbm_version_cohort_lease_t *lease = NULL;
589+
cbm_daemon_conflict_t conflict;
590+
memset(&conflict, 0, sizeof(conflict));
591+
cbm_version_cohort_status_t status = cbm_version_cohort_acquire(
592+
context->cohort_manager, &context->identity, cbm_now_ms(), &lease, &conflict);
593+
cli_activation_release_cleanup_lease(context, &lease);
594+
if (lease) {
595+
return CLI_ACTIVATION_SCOPE_ERROR;
596+
}
597+
switch (status) {
598+
case CBM_VERSION_COHORT_OK:
599+
case CBM_VERSION_COHORT_BUSY:
600+
return CLI_ACTIVATION_SCOPE_ACTIVE;
601+
case CBM_VERSION_COHORT_CONFLICT:
602+
break;
603+
default:
604+
return CLI_ACTIVATION_SCOPE_ERROR;
605+
}
606+
if (!conflict.active_cache_fingerprint[0] ||
607+
strcmp(conflict.active_cache_fingerprint, context->cache_fingerprint) == 0) {
608+
/* Same cache namespace, another build or version: that IS the daemon
609+
* this activation replaces. An unreadable active cache stays in scope
610+
* rather than silently exempting a same-namespace daemon. */
611+
return CLI_ACTIVATION_SCOPE_ACTIVE;
612+
}
613+
(void)snprintf(context->scope_detail, sizeof(context->scope_detail),
614+
"active cohort serves another cache namespace (%.12s), this %s targets "
615+
"%.12s; scope=%s; nothing stopped",
616+
conflict.active_cache_fingerprint, action, context->cache_fingerprint, scope);
617+
cli_activation_log_guard_decision(context, "foreign_cohort", conflict.active_cache_fingerprint);
618+
printf("Leaving active CBM sessions untouched: they serve another cache namespace than "
619+
"this %s targets.\n",
620+
action);
621+
(void)fflush(stdout);
622+
return CLI_ACTIVATION_SCOPE_FOREIGN_COHORT;
623+
}
624+
534625
static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lock_t *lease_out) {
535626
cli_activation_production_context_t *context = opaque;
536627
if (lease_out) {
@@ -539,6 +630,26 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
539630
if (!context || !context->cohort_manager || !lease_out) {
540631
return CLI_ERR;
541632
}
633+
cli_activation_scope_t scope = cli_activation_resolve_scope(context);
634+
if (scope == CLI_ACTIVATION_SCOPE_ERROR) {
635+
return CLI_ERR;
636+
}
637+
if (scope != CLI_ACTIVATION_SCOPE_ACTIVE) {
638+
/* Nothing in the target namespace is being replaced, or the only
639+
* active cohort serves another namespace: hold no maintenance,
640+
* admission, lifetime or startup lock (each wakes or blocks the other
641+
* namespace's sessions) and send no drain request. */
642+
if (!cli_activation_log_event(context, "quiesce_skipped", context->scope_detail)) {
643+
return CLI_ERR;
644+
}
645+
context->coordination_skipped = true;
646+
context->mutation_authorized = true;
647+
*lease_out = context;
648+
return 1;
649+
}
650+
printf("Stopping active CBM sessions and operations for %s...\n",
651+
cli_activation_action_text(context->action));
652+
(void)fflush(stdout);
542653
cbm_version_cohort_quiesce_result_t quiesce = CBM_VERSION_COHORT_QUIESCE_NOT_NEEDED;
543654
cbm_version_cohort_lease_t *lease = NULL;
544655
context->control_deadline_ms = cli_activation_deadline_after(CLI_ACTIVATION_CONTROL_TIMEOUT_MS);
@@ -585,6 +696,8 @@ static int cli_activation_production_reserve(void *opaque, cbm_cli_activation_lo
585696
}
586697

587698
context->cohort_lease = lease;
699+
cli_activation_log_guard_decision(
700+
context, context->shutdown_requested ? "cohort_drained" : "no_active_cohort", NULL);
588701
if (!cli_activation_log_event(context, "daemon_stopped",
589702
context->shutdown_requested ? "cohort drained"
590703
: "no active cohort")) {
@@ -603,6 +716,13 @@ static void cli_activation_production_release(void *opaque, cbm_cli_activation_l
603716
if (!context) {
604717
return;
605718
}
719+
if (context->coordination_skipped) {
720+
/* Nothing was held: the token is the context itself. */
721+
if (lease != (cbm_cli_activation_lock_t)context) {
722+
context->cleanup_ok = false;
723+
}
724+
return;
725+
}
606726
/* Global release order is the inverse of acquisition: startup first,
607727
* then lifetime/admission/maintenance through the cohort lease. */
608728
if (context->startup_lock) {
@@ -629,9 +749,11 @@ static void cli_activation_production_diagnostic(void *opaque, const char *messa
629749
static bool cli_activation_production_context_init(cli_activation_production_context_t *context,
630750
cbm_daemon_runtime_activation_action_t action,
631751
const char *target_version,
632-
const char *target_build) {
752+
const char *target_build,
753+
bool quiesce_required) {
633754
memset(context, 0, sizeof(*context));
634755
context->action = action;
756+
context->quiesce_required = quiesce_required;
635757
context->target_version = target_version;
636758
context->target_build = target_build;
637759
context->cleanup_ok = true;
@@ -738,16 +860,22 @@ static void cli_activation_production_context_close(cli_activation_production_co
738860
context->original_cache_environment = NULL;
739861
}
740862

741-
static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
742-
const char *target_version, const char *target_build,
743-
cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
863+
/* quiesce_required: false when the activation publishes no binary and resets
864+
* no index (a config-only install) — nothing running is then replaced, and
865+
* no session is stopped for it. */
866+
static int cli_activation_guard_scoped(cbm_daemon_runtime_activation_action_t action,
867+
const char *target_version, const char *target_build,
868+
bool quiesce_required,
869+
cbm_cli_activation_mutation_fn mutation,
870+
void *mutation_context) {
744871
if (g_cli_activation_test_ops_set) {
745872
return cbm_cli_activation_guard_with_ops(&g_cli_activation_test_ops, mutation,
746873
mutation_context);
747874
}
748875

749876
cli_activation_production_context_t context;
750-
if (!cli_activation_production_context_init(&context, action, target_version, target_build)) {
877+
if (!cli_activation_production_context_init(&context, action, target_version, target_build,
878+
quiesce_required)) {
751879
cli_activation_production_context_close(&context);
752880
/* #1856: this refusal used to print the generic text BARE. Every other
753881
* emitter routes through cli_activation_diagnostic, which appends the
@@ -760,9 +888,6 @@ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
760888
cli_activation_diagnostic(NULL, CLI_ACTIVATION_REFUSED_MESSAGE);
761889
return CLI_TRUE;
762890
}
763-
printf("Stopping active CBM sessions and operations for %s...\n",
764-
cli_activation_action_text(action));
765-
(void)fflush(stdout);
766891
if (!cli_activation_log_event(&context, "requested", NULL)) {
767892
cli_activation_production_context_close(&context);
768893
(void)fprintf(stderr, "error: activation request could not be recorded safely; "
@@ -804,6 +929,13 @@ static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
804929
return rc;
805930
}
806931

932+
static int cli_activation_guard(cbm_daemon_runtime_activation_action_t action,
933+
const char *target_version, const char *target_build,
934+
cbm_cli_activation_mutation_fn mutation, void *mutation_context) {
935+
return cli_activation_guard_scoped(action, target_version, target_build, true, mutation,
936+
mutation_context);
937+
}
938+
807939
/* Tar header field offsets */
808940
#define TAR_NAME_LEN 101 /* filename field: bytes 0-99 + NUL */
809941
#define TAR_SIZE_OFFSET 124 /* octal size field offset */
@@ -11203,11 +11335,16 @@ int cbm_cmd_install(int argc, char **argv) {
1120311335
.force = force,
1120411336
.dry_run = dry_run,
1120511337
};
11206-
int activation_rc =
11207-
dry_run ? cli_install_activate(&activation)
11208-
: cli_activation_guard(CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
11209-
has_binary_validator ? binary_validator.fingerprint : NULL,
11210-
cli_install_activate, &activation);
11338+
/* What this install replaces decides whether any session must stop: with
11339+
* the published binary untouched (--skip-binary, or an externally managed
11340+
* binary) and no index reset, agent configs are refreshed while every
11341+
* session stays up. */
11342+
bool quiesce_required = has_binary_validator || delete_indexes;
11343+
int activation_rc = dry_run ? cli_install_activate(&activation)
11344+
: cli_activation_guard_scoped(
11345+
CBM_DAEMON_RUNTIME_ACTIVATION_INSTALL, CBM_VERSION,
11346+
has_binary_validator ? binary_validator.fingerprint : NULL,
11347+
quiesce_required, cli_install_activate, &activation);
1121111348
if (activation.binary_transaction) {
1121211349
(void)cli_activation_transaction_abort(&activation.binary_transaction);
1121311350
}

‎src/foundation/sha256.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ static const uint32_t K[64] = {
2525
#define SIG1(x) (ROTR(x, 17) ^ ROTR(x, 19) ^ ((x) >> 10))
2626

2727
static void sha256_transform(cbm_sha256_ctx *c, const uint8_t *data) {
28-
uint32_t m[64];
28+
/* Scratch comes from the context, not this frame: a 256-byte local here
29+
* is fake-stacked by ASan's use-after-return mode on every 64-byte block
30+
* (see the note on cbm_sha256_ctx::sched). Identical values, allocated
31+
* once per hash instead of once per block. */
32+
uint32_t *m = c->sched;
2933
for (int i = 0, j = 0; i < 16; i++, j += 4) {
3034
m[i] = ((uint32_t)data[j] << 24) | ((uint32_t)data[j + 1] << 16) |
3135
((uint32_t)data[j + 2] << 8) | (uint32_t)data[j + 3];

‎src/foundation/sha256.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ typedef struct {
1717
uint64_t bitlen;
1818
uint8_t buf[64];
1919
size_t buflen;
20+
/* Message-schedule scratch for one compression round. It lives in the
21+
* context rather than in sha256_transform's own frame because a 256-byte
22+
* local there is large enough for ASan's use-after-return fake stack,
23+
* which then heap-allocates it on EVERY 64-byte block — millions of
24+
* __asan_stack_malloc calls to fingerprint one large file, turning
25+
* sanitized binary fingerprinting into minutes. Here it is allocated once
26+
* per hash instead of once per block. */
27+
uint32_t sched[64];
2028
} cbm_sha256_ctx;
2129

2230
void cbm_sha256_init(cbm_sha256_ctx *c);

‎src/mcp/index_supervisor.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,16 @@ const char *cbm_index_supervisor_build_fingerprint(void) {
149149
return g_build_fingerprint[0] ? g_build_fingerprint : NULL;
150150
}
151151

152+
#if defined(CBM_CLI_ENABLE_TEST_API)
153+
void cbm_index_supervisor_set_build_fingerprint_for_test(const char *fingerprint) {
154+
if (!fingerprint || !worker_fingerprint_valid(fingerprint)) {
155+
return;
156+
}
157+
g_build_fingerprint_capture_attempted = true;
158+
(void)snprintf(g_build_fingerprint, sizeof(g_build_fingerprint), "%s", fingerprint);
159+
}
160+
#endif
161+
152162
static bool worker_fingerprint_valid(const char *fingerprint) {
153163
if (!fingerprint || strlen(fingerprint) != CBM_INDEX_WORKER_BUILD_FINGERPRINT_LENGTH) {
154164
return false;

‎src/mcp/index_supervisor.h‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ void cbm_index_worker_log_begin(const char *args_json, const char *repo_path);
7676
bool cbm_index_supervisor_capture_build_fingerprint(void);
7777
const char *cbm_index_supervisor_build_fingerprint(void);
7878

79+
#if defined(CBM_CLI_ENABLE_TEST_API)
80+
/* Test-only: the runner captures a synthetic stub (see the capture seam) so
81+
* spawned workers start instantly, but a daemon runtime service can only carry
82+
* the REAL image hash. A fixture that runs a real daemon and drives the
83+
* production activation guard against it aligns the captured value with that
84+
* hash for its duration and restores the previous value afterwards. Invalid
85+
* input is ignored. Never compiled into production. */
86+
void cbm_index_supervisor_set_build_fingerprint_for_test(const char *fingerprint);
87+
#endif
88+
7989
typedef struct {
8090
const char *expected_build_fingerprint;
8191
const char *args_json;

0 commit comments

Comments
 (0)