0baddf7887
Daemon lifecycle and Windows correctness, verified on a real Windows 11 ARM64 VM through the maintained test-infrastructure/vm drivers, plus the macOS and Linux arm64 suites and the container lint gate. Daemon lifecycle: - daemon start/stop/status subcommands. `daemon start` launches a PERMANENT daemon (spawn shape is byte-exact argv; survives idle periods and session ends) and reports an already-active daemon instead of failing. Permanence is honored at every stop latch: last-committed-client disconnect, host initial-client window, coordinator release, and application final-session close — a permanent daemon also keeps admitting new sessions after its last one closes. - daemon stop refuses while sessions are active and lists the blocking peers (pid/role) that must finish first; an idle daemon drains through the activation-shutdown machinery with the ACK ordered after connection interrupts. A second stop is idempotent. The wire ops are no-cohort first-frame requests with peer fingerprint authentication, so stop/status never conflict with an exact-build admission gate. - One-shot CLI commands now execute through the daemon (index workers keep their local supervised path). A cold CLI run that had to spawn a temporary daemon prints a hint that `daemon start` removes the per-command startup tax; a warm daemon is recycled silently. - Hooks are connect-only fail-open: with no daemon present the hook emits a visible, rate-limited notice (Claude-dialect systemMessage plus stderr for other dialects) and always exits 0 — augmentation is never allowed to block the caller's tool use. - Version skew: a newer-build client automatically drains an older-build permanent daemon (strict semantic-version triples only; dev builds never auto-drain) and the build-conflict message names `cbm daemon stop` as the manual escape hatch. Windows IPC/runtime (real-VM verified): - ipc(win): persistent pending overlapped ConnectNamedPipe. The accept path used to destroy its listening pipe instance on every 20 ms poll timeout; a client attaching in the teardown window was severed or left on an orphaned pipe object whose HELLO no server handle could ever read, absorbing the connect until the client's own timeout expired. The pending connect now survives poll timeouts and nothing is destroyed while a client could be attaching. - ipc(win): drain-before-close for final responses. Closing a named-pipe server handle can discard a just-sent response before the peer reads it (POSIX stream sockets never lose buffered data on close). A bounded cbm_daemon_ipc_connection_drain (read-until-peer-EOF; no-op on POSIX, immediate on interrupted connections) now precedes close in runtime_worker_finish and runtime_reject_inline, so hello-conflict, capacity and disconnect acknowledgements reliably reach the peer. - runtime: CLOSE_INTENT wire frame. A Windows named-pipe client has no transport half-close, so close_begin now announces departure with an explicit frame (ordered after APPLICATION_CANCEL, before the local interrupt); the server releases the client's admission on receipt instead of waiting for the handle to close. Admission-drop timing is now identical to POSIX shutdown() semantics on every platform. - runtime(win): client close cancellation. close_begin serializes with request publication under the send lock, best-effort sends the active token's APPLICATION_CANCEL frame, then interrupts local I/O; the server cancels MCP/subprocess work promptly. Contract tests accept both correct outcomes (interrupted transport or decoded CANCELLED). - runtime: activation acknowledgement ordering. The activation ACK is the requester's license to act on "snapshotted and draining", so every connection interrupt is now initiated before the ACK is sent; a session could previously get one more request serviced after the requester observed the ACK. - service(win): deadline-bounded private-file prepare. The conflict-log prepare retry loop (100 x Sleep(2), which rounds up to the ~16 ms timer granularity) burned ~1.6 s against permanently obstructed paths, stalling hello rejections past the client's timeout. The retry budget is now a 250 ms deadline; transient share collisions still retry. - subprocess(win): cmd.exe /C payload encoder quotes metacharacters correctly (root cause of the git-on-Windows failure cluster). - watcher: SHA-256 buffer sizing (CBM_SZ_64 -> CBM_SZ_128) and a native Windows stop/unwatch cancellation test with exact-image verification. - httpd: send_all writes in bounded 64 KiB slices. A single giant nonblocking send() on Windows is absorbed wholesale into AFD kernel buffering regardless of SO_SNDBUF, so send deadlines and interrupts could never engage against a slow peer (and the full payload was pinned in nonpaged pool). Slicing restores a deterministic backpressure point; a test hook pins SO_SNDBUF for the deadline and interrupt tests. - ui/http: shutdown lifecycle — interrupt checks, response-wide send deadline, explicit connection states, refusal to free a server while a listener-owned connection is active. Windows long-path support: - Central path-aware wide conversion (canonicalize via GetFullPathNameW and prepend the extended-length prefix for absolute paths >=240) at the compat chokepoints (cbm_fopen/compat_fs/mkstemp/mkdtemp), sqlite store opens, and the daemon build-fingerprint/log paths. Deep managed installs (a 64-hex generation directory routinely exceeds MAX_PATH) now index, stage and activate correctly. - activation transaction: its own file APIs and the component-walking ancestry validators now operate in the extended-length namespace; the launcher path is canonicalized (and prefixed when deep) once at entry so every downstream exact-string comparison stays form-consistent. - Executable self-resolution uses the wide APIs (GetModuleFileNameW, GetFileAttributesW) so non-ASCII install paths survive argv[0] resolution. Windows launcher install/uninstall transaction: - FileRenameInfoEx names are NUL-terminated in an over-allocated buffer. FileNameLength governs per the contract, but filter drivers read FileName as NUL-terminated and appended adjacent heap bytes to created names — a flaky, garbage-suffixed rename target. Both the CLI and the launcher rename helpers are fixed. - Uninstall retires state via rename-aside (.cbm -> .cbm-retired-v1-<tag>-<pid>) with the retired tag shortened to 16 hex chars so the bare rename target stays under the FileRenameInfoEx NT-conversion ceiling at guard depths; 64 bits still uniquely identify the generation. - When the running launcher's mapped generation backings pin .cbm against rename, the backings are relocated to activation-<pid>-N .retired tombstones beside the install (a mapped image may be renamed, never deleted; the launcher's liveness-guarded sweep reclaims stale tombstones). Every relocation is recorded, and a FAILED uninstall reverses the moves after restoring .cbm — via MoveFileExW with extended-length paths on both arguments, since the deep generation target is beyond the handle-based rename's bare-path reach — so a restored install keeps its generation backings and stays runnable. - After a committed uninstall the retired tree's backings are relocated out so the tree is shallow enough for the detached cleanup's rd, and the cleanup's working directory strips the extended-length prefix (CreateProcessW lpCurrentDirectory silently ignores prefixed paths). - Files created under Administrators-default-owner directories (CopyFileW destinations, CREATE_NEW tombstones, probe directories) are explicitly owner-stamped so the exact-owner validators hold on runner images; guard fixtures stamp hand-built trees the same way. Diagnostics, tests and infra: - diagnostics: discovery is now an always-delivered JSON control record (new cbm_log_control) that survives CBM_LOG_LEVEL suppression and paths containing spaces; placement honors $TMPDIR with /tmp fallback via a diagnostics-local helper; the soak parser reads the JSON record; documented in docs/CONFIGURATION.md. Red-first coverage for suppressed log levels, TMPDIR-with-spaces, and native Windows output-contract assertions. - tests(win): daemon_ipc/daemon_frontend fixtures now build endpoint parents with production-shaped ancestry (LocalAppData on Windows, via th_secure_runtime_parent_new) — the runtime ancestry validation correctly refuses temp roots whose ancestors grant mutation rights to Authenticated Users (C:/msys64/tmp, GitHub-runner work dirs) — and drive the documented startup-owner publication flow before reading generation-bound endpoint addresses. This turns the 26 Windows failures previously visible in CI's full-test job green without weakening any validation. - tests(win): the launcher guard covers the full permanent-launcher contract including failed-uninstall restore and immediate reinstall after uninstall; new daemon lifecycle and reworked hook-augment guards run the start/recycle/stop flow end to end. - tests: CBM_SKIP_PERF is now actually consumed by the test runner (it was set by CI but never read, so perf suites ran everywhere); four throughput/bench suites are classified as perf, the heavy store_arch suite moved to the slow-timeout tier, and two wall-clock-sensitive assertions were rewritten as invariant checks with coarse hang-detector backstops. - build/test infra: build-dir safety contract, UI dev-proxy security contract, soak daemon-recovery contract, path-safety helper, the Windows VM worktree-sync contract wired into scripts/test.sh, and vm/win.sh guards building its clean embedded-UI product in an isolated BUILD_DIR so it cannot clobber the incremental test build. provision-windows.sh now installs Node.js for the guards UI build. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
587 lines
21 KiB
C
587 lines
21 KiB
C
/*
|
|
* test_diagnostics.c — Security and lifecycle guards for the optional
|
|
* periodic diagnostics writer.
|
|
*/
|
|
#include "test_framework.h"
|
|
|
|
#include "../src/foundation/compat.h"
|
|
#include "../src/foundation/compat_fs.h"
|
|
#include "../src/foundation/diagnostics.h"
|
|
#include "../src/foundation/log.h"
|
|
#include "../src/foundation/platform.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
|
|
#include <aclapi.h>
|
|
#include <process.h>
|
|
#define getpid _getpid
|
|
#else
|
|
#include <sys/stat.h>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
typedef struct {
|
|
char directory[1024];
|
|
char snapshot[1024];
|
|
char trajectory[1024];
|
|
} diagnostics_paths_t;
|
|
|
|
static void diagnostics_wait_ms(unsigned int milliseconds) {
|
|
struct timespec delay = {
|
|
.tv_sec = (time_t)(milliseconds / 1000U),
|
|
.tv_nsec = (long)((milliseconds % 1000U) * 1000000U),
|
|
};
|
|
(void)cbm_nanosleep(&delay, NULL);
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
static bool diagnostics_file_equals(const char *path, const char *expected) {
|
|
char contents[128] = "";
|
|
FILE *file = cbm_fopen(path, "rb");
|
|
if (!file) {
|
|
return false;
|
|
}
|
|
size_t length = fread(contents, 1, sizeof(contents) - 1, file);
|
|
bool closed = fclose(file) == 0;
|
|
contents[length] = '\0';
|
|
return closed && strcmp(contents, expected) == 0;
|
|
}
|
|
#endif
|
|
|
|
static bool diagnostics_capture_paths(diagnostics_paths_t *paths) {
|
|
return cbm_diag_test_copy_paths(paths->directory, sizeof(paths->directory), paths->snapshot,
|
|
sizeof(paths->snapshot), paths->trajectory,
|
|
sizeof(paths->trajectory));
|
|
}
|
|
|
|
static bool diagnostics_wait_for_file(const char *path, unsigned int timeout_ms) {
|
|
uint64_t deadline = cbm_now_ms() + timeout_ms;
|
|
while (cbm_now_ms() < deadline) {
|
|
if (cbm_file_exists(path)) {
|
|
return true;
|
|
}
|
|
diagnostics_wait_ms(10);
|
|
}
|
|
return cbm_file_exists(path);
|
|
}
|
|
|
|
static char *diagnostics_read_file(const char *path) {
|
|
FILE *file = cbm_fopen(path, "rb");
|
|
if (!file || fseek(file, 0, SEEK_END) != 0) {
|
|
if (file) {
|
|
(void)fclose(file);
|
|
}
|
|
return NULL;
|
|
}
|
|
long length = ftell(file);
|
|
if (length < 0 || fseek(file, 0, SEEK_SET) != 0) {
|
|
(void)fclose(file);
|
|
return NULL;
|
|
}
|
|
char *contents = malloc((size_t)length + 1);
|
|
if (!contents) {
|
|
(void)fclose(file);
|
|
return NULL;
|
|
}
|
|
size_t read_length = fread(contents, 1, (size_t)length, file);
|
|
bool closed = fclose(file) == 0;
|
|
bool complete = read_length == (size_t)length && closed;
|
|
contents[read_length] = '\0';
|
|
if (!complete) {
|
|
free(contents);
|
|
return NULL;
|
|
}
|
|
return contents;
|
|
}
|
|
|
|
static void diagnostics_cleanup_outputs(const diagnostics_paths_t *paths) {
|
|
char temporary[1100];
|
|
char rotated[1100];
|
|
if (snprintf(temporary, sizeof(temporary), "%s.tmp", paths->snapshot) > 0) {
|
|
(void)cbm_unlink(temporary);
|
|
}
|
|
if (snprintf(rotated, sizeof(rotated), "%s.1", paths->trajectory) > 0) {
|
|
(void)cbm_unlink(rotated);
|
|
}
|
|
(void)cbm_unlink(paths->snapshot);
|
|
(void)cbm_unlink(paths->trajectory);
|
|
(void)cbm_rmdir(paths->directory);
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
TEST(diagnostics_rejects_predictable_tmp_symlinks) {
|
|
char snapshot_link[256];
|
|
char trajectory_link[256];
|
|
char snapshot_target[256];
|
|
char trajectory_target[256];
|
|
const char *snapshot_seed = "snapshot sentinel\n";
|
|
const char *trajectory_seed = "trajectory sentinel\n";
|
|
int pid = (int)getpid();
|
|
|
|
ASSERT_LT(
|
|
snprintf(snapshot_link, sizeof(snapshot_link), "/tmp/cbm-diagnostics-%d.json.tmp", pid),
|
|
(int)sizeof(snapshot_link));
|
|
ASSERT_LT(
|
|
snprintf(trajectory_link, sizeof(trajectory_link), "/tmp/cbm-diagnostics-%d.ndjson", pid),
|
|
(int)sizeof(trajectory_link));
|
|
ASSERT_LT(snprintf(snapshot_target, sizeof(snapshot_target),
|
|
"/tmp/cbm-diagnostics-snapshot-sentinel-%d", pid),
|
|
(int)sizeof(snapshot_target));
|
|
ASSERT_LT(snprintf(trajectory_target, sizeof(trajectory_target),
|
|
"/tmp/cbm-diagnostics-trajectory-sentinel-%d", pid),
|
|
(int)sizeof(trajectory_target));
|
|
|
|
(void)cbm_unlink(snapshot_link);
|
|
(void)cbm_unlink(trajectory_link);
|
|
(void)cbm_unlink(snapshot_target);
|
|
(void)cbm_unlink(trajectory_target);
|
|
|
|
FILE *snapshot = cbm_fopen(snapshot_target, "wb");
|
|
ASSERT_NOT_NULL(snapshot);
|
|
ASSERT_GTE(fputs(snapshot_seed, snapshot), 0);
|
|
ASSERT_EQ(fclose(snapshot), 0);
|
|
FILE *trajectory = cbm_fopen(trajectory_target, "wb");
|
|
ASSERT_NOT_NULL(trajectory);
|
|
ASSERT_GTE(fputs(trajectory_seed, trajectory), 0);
|
|
ASSERT_EQ(fclose(trajectory), 0);
|
|
ASSERT_EQ(symlink(snapshot_target, snapshot_link), 0);
|
|
ASSERT_EQ(symlink(trajectory_target, trajectory_link), 0);
|
|
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
ASSERT_TRUE(cbm_diag_start());
|
|
diagnostics_paths_t outputs;
|
|
bool captured = diagnostics_capture_paths(&outputs);
|
|
bool emitted = captured && diagnostics_wait_for_file(outputs.snapshot, 1000);
|
|
cbm_diag_stop();
|
|
ASSERT_EQ(cbm_unsetenv("CBM_DIAGNOSTICS"), 0);
|
|
|
|
bool snapshot_untouched = diagnostics_file_equals(snapshot_target, snapshot_seed);
|
|
bool trajectory_untouched = diagnostics_file_equals(trajectory_target, trajectory_seed);
|
|
(void)cbm_unlink(snapshot_link);
|
|
(void)cbm_unlink(trajectory_link);
|
|
(void)cbm_unlink(snapshot_target);
|
|
(void)cbm_unlink(trajectory_target);
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
|
|
ASSERT_TRUE(captured);
|
|
ASSERT_TRUE(emitted);
|
|
ASSERT_TRUE(snapshot_untouched);
|
|
ASSERT_TRUE(trajectory_untouched);
|
|
PASS();
|
|
}
|
|
#endif
|
|
|
|
TEST(diagnostics_stop_interrupts_periodic_wait) {
|
|
char legacy_snapshot[256];
|
|
char legacy_trajectory[256];
|
|
int pid = (int)getpid();
|
|
ASSERT_LT(snprintf(legacy_snapshot, sizeof(legacy_snapshot), "%s/cbm-diagnostics-%d.json",
|
|
cbm_tmpdir(), pid),
|
|
(int)sizeof(legacy_snapshot));
|
|
ASSERT_LT(snprintf(legacy_trajectory, sizeof(legacy_trajectory), "%s/cbm-diagnostics-%d.ndjson",
|
|
cbm_tmpdir(), pid),
|
|
(int)sizeof(legacy_trajectory));
|
|
(void)cbm_unlink(legacy_snapshot);
|
|
(void)cbm_unlink(legacy_trajectory);
|
|
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
ASSERT_TRUE(cbm_diag_start());
|
|
|
|
diagnostics_paths_t outputs;
|
|
bool captured = diagnostics_capture_paths(&outputs);
|
|
/* A visible snapshot proves the writer has reached its periodic wait. */
|
|
bool emitted = captured && diagnostics_wait_for_file(outputs.snapshot, 1000);
|
|
uint64_t started_ms = cbm_now_ms();
|
|
cbm_diag_stop();
|
|
uint64_t elapsed_ms = cbm_now_ms() - started_ms;
|
|
ASSERT_EQ(cbm_unsetenv("CBM_DIAGNOSTICS"), 0);
|
|
(void)cbm_unlink(legacy_snapshot);
|
|
(void)cbm_unlink(legacy_trajectory);
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
|
|
ASSERT_TRUE(captured);
|
|
ASSERT_TRUE(emitted);
|
|
ASSERT_LT(elapsed_ms, UINT64_C(1000));
|
|
PASS();
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
TEST(diagnostics_outputs_are_owner_private_regular_files) {
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
ASSERT_TRUE(cbm_diag_start());
|
|
diagnostics_paths_t outputs;
|
|
bool captured = diagnostics_capture_paths(&outputs);
|
|
bool snapshot_emitted = captured && diagnostics_wait_for_file(outputs.snapshot, 1000);
|
|
bool trajectory_emitted = captured && diagnostics_wait_for_file(outputs.trajectory, 1000);
|
|
|
|
struct stat directory_state;
|
|
struct stat snapshot_state;
|
|
struct stat trajectory_state;
|
|
bool secure = captured && snapshot_emitted && trajectory_emitted &&
|
|
lstat(outputs.directory, &directory_state) == 0 &&
|
|
S_ISDIR(directory_state.st_mode) && directory_state.st_uid == geteuid() &&
|
|
(directory_state.st_mode & 07777) == 0700 &&
|
|
lstat(outputs.snapshot, &snapshot_state) == 0 &&
|
|
S_ISREG(snapshot_state.st_mode) && snapshot_state.st_uid == geteuid() &&
|
|
snapshot_state.st_nlink == 1 && (snapshot_state.st_mode & 07777) == 0600 &&
|
|
lstat(outputs.trajectory, &trajectory_state) == 0 &&
|
|
S_ISREG(trajectory_state.st_mode) && trajectory_state.st_uid == geteuid() &&
|
|
trajectory_state.st_nlink == 1 && (trajectory_state.st_mode & 07777) == 0600;
|
|
|
|
cbm_diag_stop();
|
|
ASSERT_EQ(cbm_unsetenv("CBM_DIAGNOSTICS"), 0);
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
ASSERT_TRUE(secure);
|
|
PASS();
|
|
}
|
|
#endif
|
|
|
|
TEST(diagnostics_stalled_writer_cannot_retain_shutdown) {
|
|
cbm_diag_test_hold_writer(true);
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
if (!cbm_diag_start()) {
|
|
cbm_diag_test_hold_writer(false);
|
|
(void)cbm_unsetenv("CBM_DIAGNOSTICS");
|
|
FAIL("failed to start held diagnostics writer");
|
|
}
|
|
diagnostics_paths_t outputs;
|
|
bool captured = diagnostics_capture_paths(&outputs);
|
|
uint64_t reach_deadline = cbm_now_ms() + 1000;
|
|
while (!cbm_diag_test_writer_reached() && cbm_now_ms() < reach_deadline) {
|
|
diagnostics_wait_ms(10);
|
|
}
|
|
bool reached = cbm_diag_test_writer_reached();
|
|
if (!captured || !reached) {
|
|
cbm_diag_test_hold_writer(false);
|
|
cbm_diag_stop();
|
|
(void)cbm_unsetenv("CBM_DIAGNOSTICS");
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
FAIL("diagnostics writer did not reach the held I/O boundary");
|
|
}
|
|
|
|
uint64_t started_ms = cbm_now_ms();
|
|
cbm_diag_stop();
|
|
uint64_t elapsed_ms = cbm_now_ms() - started_ms;
|
|
bool abandoned = cbm_diag_test_abandoned();
|
|
|
|
cbm_diag_test_hold_writer(false);
|
|
bool reset = false;
|
|
uint64_t reset_deadline = cbm_now_ms() + 1000;
|
|
while (!reset && cbm_now_ms() < reset_deadline) {
|
|
reset = cbm_diag_test_reset_abandoned();
|
|
if (!reset) {
|
|
diagnostics_wait_ms(10);
|
|
}
|
|
}
|
|
(void)cbm_unsetenv("CBM_DIAGNOSTICS");
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
|
|
ASSERT_LT(elapsed_ms, UINT64_C(1500));
|
|
ASSERT_TRUE(abandoned);
|
|
ASSERT_TRUE(reset);
|
|
PASS();
|
|
}
|
|
|
|
TEST(diagnostics_soak_discovers_daemon_emitted_paths) {
|
|
char *script = diagnostics_read_file("scripts/soak-test.sh");
|
|
ASSERT_NOT_NULL(script);
|
|
bool parses_start_event = strstr(script, "\"event\":\"diagnostics.start\"") != NULL;
|
|
bool isolates_daemon = strstr(script, "CBM_CACHE_DIR=\"$SOAK_CACHE_DIR_VALUE\"") != NULL;
|
|
bool legacy_predictable_path = strstr(script, "/tmp/cbm-diagnostics-${SERVER_PID}") != NULL;
|
|
free(script);
|
|
|
|
ASSERT_TRUE(parses_start_event);
|
|
ASSERT_TRUE(isolates_daemon);
|
|
ASSERT_FALSE(legacy_predictable_path);
|
|
PASS();
|
|
}
|
|
|
|
|
|
/* ── Discovery record contract ───────────────────────────────────────── */
|
|
|
|
static char diagnostics_discovery_line[2048];
|
|
static int diagnostics_discovery_records;
|
|
|
|
static void diagnostics_discovery_sink(const char *line) {
|
|
if (!line || !strstr(line, "\"event\":\"diagnostics.start\"")) {
|
|
return;
|
|
}
|
|
diagnostics_discovery_records++;
|
|
(void)snprintf(diagnostics_discovery_line, sizeof(diagnostics_discovery_line), "%s", line);
|
|
}
|
|
|
|
static bool diagnostics_discovery_value(const char *key, char *out, size_t out_size) {
|
|
char marker[64];
|
|
if (snprintf(marker, sizeof(marker), "\"%s\":\"", key) >= (int)sizeof(marker)) {
|
|
return false;
|
|
}
|
|
const char *cursor = strstr(diagnostics_discovery_line, marker);
|
|
if (!cursor) {
|
|
return false;
|
|
}
|
|
cursor += strlen(marker);
|
|
size_t used = 0;
|
|
while (*cursor && *cursor != '"' && used + 1 < out_size) {
|
|
if (*cursor == '\\' && cursor[1] != '\0') {
|
|
cursor++;
|
|
char decoded = *cursor;
|
|
if (decoded == 'n') {
|
|
decoded = '\n';
|
|
} else if (decoded == 't') {
|
|
decoded = '\t';
|
|
} else if (decoded == 'r') {
|
|
decoded = '\r';
|
|
}
|
|
out[used++] = decoded;
|
|
cursor++;
|
|
continue;
|
|
}
|
|
out[used++] = *cursor++;
|
|
}
|
|
out[used] = '\0';
|
|
return *cursor == '"';
|
|
}
|
|
|
|
TEST(diagnostics_discovery_record_survives_suppressed_log_level) {
|
|
diagnostics_discovery_records = 0;
|
|
diagnostics_discovery_line[0] = '\0';
|
|
cbm_log_set_level(CBM_LOG_NONE);
|
|
cbm_log_set_sink_ex(diagnostics_discovery_sink, CBM_LOG_SINK_REPLACE);
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
bool started = cbm_diag_start();
|
|
diagnostics_paths_t outputs;
|
|
bool captured = started && diagnostics_capture_paths(&outputs);
|
|
cbm_diag_stop();
|
|
(void)cbm_unsetenv("CBM_DIAGNOSTICS");
|
|
cbm_log_set_sink(NULL);
|
|
cbm_log_set_level(CBM_LOG_INFO);
|
|
|
|
char recorded_snapshot[1024] = "";
|
|
char recorded_trajectory[1024] = "";
|
|
bool decoded =
|
|
diagnostics_discovery_value("snapshot", recorded_snapshot, sizeof(recorded_snapshot)) &&
|
|
diagnostics_discovery_value("trajectory", recorded_trajectory,
|
|
sizeof(recorded_trajectory));
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
ASSERT_TRUE(started);
|
|
ASSERT_TRUE(captured);
|
|
/* Exactly one discovery record, delivered despite CBM_LOG_LEVEL=none, and
|
|
* its decoded paths are byte-exact against the writer's own paths. */
|
|
ASSERT_EQ(diagnostics_discovery_records, 1);
|
|
ASSERT_TRUE(decoded);
|
|
ASSERT_EQ(strcmp(recorded_snapshot, outputs.snapshot), 0);
|
|
ASSERT_EQ(strcmp(recorded_trajectory, outputs.trajectory), 0);
|
|
PASS();
|
|
}
|
|
|
|
#ifndef _WIN32
|
|
TEST(diagnostics_placement_honors_tmpdir_with_spaces) {
|
|
char saved_tmpdir[1024] = "";
|
|
const char *current_tmpdir = getenv("TMPDIR");
|
|
bool had_tmpdir = current_tmpdir != NULL;
|
|
if (had_tmpdir) {
|
|
ASSERT_LT(snprintf(saved_tmpdir, sizeof(saved_tmpdir), "%s", current_tmpdir),
|
|
(int)sizeof(saved_tmpdir));
|
|
}
|
|
char base[256];
|
|
ASSERT_LT(snprintf(base, sizeof(base), "/tmp/cbm diag spaces %d", (int)getpid()),
|
|
(int)sizeof(base));
|
|
(void)cbm_rmdir(base);
|
|
ASSERT_EQ(mkdir(base, 0700), 0);
|
|
ASSERT_EQ(cbm_setenv("TMPDIR", base, 1), 0);
|
|
diagnostics_discovery_records = 0;
|
|
diagnostics_discovery_line[0] = '\0';
|
|
cbm_log_set_sink_ex(diagnostics_discovery_sink, CBM_LOG_SINK_REPLACE);
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
bool started = cbm_diag_start();
|
|
diagnostics_paths_t outputs;
|
|
bool captured = started && diagnostics_capture_paths(&outputs);
|
|
bool emitted = captured && diagnostics_wait_for_file(outputs.snapshot, 1000);
|
|
cbm_diag_stop();
|
|
(void)cbm_unsetenv("CBM_DIAGNOSTICS");
|
|
cbm_log_set_sink(NULL);
|
|
if (had_tmpdir) {
|
|
(void)cbm_setenv("TMPDIR", saved_tmpdir, 1);
|
|
} else {
|
|
(void)cbm_unsetenv("TMPDIR");
|
|
}
|
|
|
|
char recorded[1024] = "";
|
|
bool decoded = diagnostics_discovery_value("snapshot", recorded, sizeof(recorded));
|
|
bool under_base = captured && strncmp(outputs.directory, base, strlen(base)) == 0;
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
(void)cbm_rmdir(base);
|
|
ASSERT_TRUE(started);
|
|
ASSERT_TRUE(captured);
|
|
ASSERT_TRUE(emitted);
|
|
/* The documented contract: honor $TMPDIR (with /tmp fallback), and the
|
|
* discovery record must round-trip a path containing spaces exactly. */
|
|
ASSERT_TRUE(under_base);
|
|
ASSERT_TRUE(decoded);
|
|
ASSERT_EQ(strcmp(recorded, outputs.snapshot), 0);
|
|
PASS();
|
|
}
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
static wchar_t *diagnostics_windows_utf8_to_wide(const char *value) {
|
|
int needed = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value, -1, NULL, 0);
|
|
wchar_t *wide = needed > 0 ? calloc((size_t)needed, sizeof(*wide)) : NULL;
|
|
if (!wide || MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, value, -1, wide, needed) <= 0) {
|
|
free(wide);
|
|
return NULL;
|
|
}
|
|
return wide;
|
|
}
|
|
|
|
static bool diagnostics_windows_current_user_sid(void **information_out, PSID *sid_out) {
|
|
*information_out = NULL;
|
|
*sid_out = NULL;
|
|
HANDLE token = NULL;
|
|
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) {
|
|
return false;
|
|
}
|
|
DWORD needed = 0;
|
|
(void)GetTokenInformation(token, TokenUser, NULL, 0, &needed);
|
|
void *information = needed > 0 ? calloc(1, needed) : NULL;
|
|
bool ok =
|
|
information && GetTokenInformation(token, TokenUser, information, needed, &needed) != 0;
|
|
(void)CloseHandle(token);
|
|
if (!ok) {
|
|
free(information);
|
|
return false;
|
|
}
|
|
PSID sid = ((TOKEN_USER *)information)->User.Sid;
|
|
if (!sid || !IsValidSid(sid)) {
|
|
free(information);
|
|
return false;
|
|
}
|
|
*information_out = information;
|
|
*sid_out = sid;
|
|
return true;
|
|
}
|
|
|
|
/* Owner is the current user AND the protected DACL grants access to that
|
|
* owner alone — the Windows equivalent of the POSIX 0700/0600 assertions. */
|
|
static bool diagnostics_windows_path_owner_private(const char *path, bool directory) {
|
|
void *user_information = NULL;
|
|
PSID user_sid = NULL;
|
|
wchar_t *wide_path = diagnostics_windows_utf8_to_wide(path);
|
|
DWORD flags = FILE_FLAG_OPEN_REPARSE_POINT | (directory ? FILE_FLAG_BACKUP_SEMANTICS : 0);
|
|
HANDLE handle = wide_path
|
|
? CreateFileW(wide_path, READ_CONTROL,
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
|
|
OPEN_EXISTING, flags, NULL)
|
|
: INVALID_HANDLE_VALUE;
|
|
PSID owner = NULL;
|
|
PACL dacl = NULL;
|
|
PSECURITY_DESCRIPTOR descriptor = NULL;
|
|
SECURITY_DESCRIPTOR_CONTROL control = 0;
|
|
DWORD revision = 0;
|
|
bool ok = handle != INVALID_HANDLE_VALUE &&
|
|
diagnostics_windows_current_user_sid(&user_information, &user_sid) &&
|
|
GetSecurityInfo(handle, SE_FILE_OBJECT,
|
|
OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION, &owner, NULL,
|
|
&dacl, NULL, &descriptor) == ERROR_SUCCESS &&
|
|
owner && IsValidSid(owner) && EqualSid(owner, user_sid) &&
|
|
GetSecurityDescriptorControl(descriptor, &control, &revision) != 0 &&
|
|
(control & SE_DACL_PROTECTED) != 0 && dacl && dacl->AceCount == 1;
|
|
if (ok) {
|
|
void *ace = NULL;
|
|
ok = GetAce(dacl, 0, &ace) != 0 &&
|
|
((ACE_HEADER *)ace)->AceType == ACCESS_ALLOWED_ACE_TYPE &&
|
|
EqualSid((PSID)&((ACCESS_ALLOWED_ACE *)ace)->SidStart, user_sid);
|
|
}
|
|
if (descriptor) {
|
|
(void)LocalFree(descriptor);
|
|
}
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
(void)CloseHandle(handle);
|
|
}
|
|
free(wide_path);
|
|
free(user_information);
|
|
return ok;
|
|
}
|
|
|
|
static bool diagnostics_windows_output_contract(const char *path) {
|
|
/* Mirrors production's diag_win_handle_valid: files are secured by their
|
|
* owner-private parent directory; the file itself must be a plain local
|
|
* regular file — no reparse, no extra links. Owner/DACL are asserted on
|
|
* the directory, where the security boundary actually lives (file owners
|
|
* follow the machine's default-owner policy, e.g. Administrators on
|
|
* GitHub-runner-class images). */
|
|
wchar_t *wide_path = diagnostics_windows_utf8_to_wide(path);
|
|
HANDLE handle = wide_path
|
|
? CreateFileW(wide_path, GENERIC_READ,
|
|
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
|
|
OPEN_EXISTING, FILE_FLAG_OPEN_REPARSE_POINT, NULL)
|
|
: INVALID_HANDLE_VALUE;
|
|
BY_HANDLE_FILE_INFORMATION information;
|
|
bool ok = handle != INVALID_HANDLE_VALUE && GetFileType(handle) == FILE_TYPE_DISK &&
|
|
GetFileInformationByHandle(handle, &information) != 0 &&
|
|
(information.dwFileAttributes &
|
|
(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_REPARSE_POINT)) == 0 &&
|
|
information.nNumberOfLinks == 1;
|
|
if (handle != INVALID_HANDLE_VALUE) {
|
|
(void)CloseHandle(handle);
|
|
}
|
|
free(wide_path);
|
|
return ok;
|
|
}
|
|
|
|
TEST(diagnostics_outputs_are_owner_private_windows) {
|
|
ASSERT_EQ(cbm_setenv("CBM_DIAGNOSTICS", "1", 1), 0);
|
|
ASSERT_TRUE(cbm_diag_start());
|
|
diagnostics_paths_t outputs;
|
|
bool captured = diagnostics_capture_paths(&outputs);
|
|
bool snapshot_emitted = captured && diagnostics_wait_for_file(outputs.snapshot, 1000);
|
|
bool trajectory_emitted = captured && diagnostics_wait_for_file(outputs.trajectory, 1000);
|
|
bool secure = captured && snapshot_emitted && trajectory_emitted &&
|
|
diagnostics_windows_path_owner_private(outputs.directory, true) &&
|
|
diagnostics_windows_output_contract(outputs.snapshot) &&
|
|
diagnostics_windows_output_contract(outputs.trajectory);
|
|
cbm_diag_stop();
|
|
ASSERT_EQ(cbm_unsetenv("CBM_DIAGNOSTICS"), 0);
|
|
if (captured) {
|
|
diagnostics_cleanup_outputs(&outputs);
|
|
}
|
|
ASSERT_TRUE(secure);
|
|
PASS();
|
|
}
|
|
#endif
|
|
|
|
SUITE(diagnostics) {
|
|
#ifndef _WIN32
|
|
RUN_TEST(diagnostics_rejects_predictable_tmp_symlinks);
|
|
#endif
|
|
RUN_TEST(diagnostics_stop_interrupts_periodic_wait);
|
|
#ifndef _WIN32
|
|
RUN_TEST(diagnostics_outputs_are_owner_private_regular_files);
|
|
RUN_TEST(diagnostics_placement_honors_tmpdir_with_spaces);
|
|
#endif
|
|
#ifdef _WIN32
|
|
RUN_TEST(diagnostics_outputs_are_owner_private_windows);
|
|
#endif
|
|
RUN_TEST(diagnostics_discovery_record_survives_suppressed_log_level);
|
|
RUN_TEST(diagnostics_stalled_writer_cannot_retain_shutdown);
|
|
RUN_TEST(diagnostics_soak_discovers_daemon_emitted_paths);
|
|
}
|