Skip to content

Commit aa90491

Browse files
committed
fix(cli): stage Windows install transactions under a per-user root
The activation transaction and the release-descriptor probe validate the full ancestor chain of everything they stage: every path component must have a trusted owner and grant mutation rights only to trusted identities. That walk is deliberate TOCTOU defense and stays strict. The staging directory, however, came from the process temp root, which routinely lives in a tree that legitimately fails the walk through no fault of ours: under msys2 TMP resolves inside the msys install tree (C:\msys64\tmp), and on GitHub runners inside the runner work directory, both of which carry broad inherited grants such as Authenticated Users modify on an upper component. The staged pair then failed validation ("launcher descriptor candidate is unsafe" / the smoke Phase 8 --version probe refusal) even though the staged files themselves were private. Add cli_windows_staging_root(): stage install and update transactions under %LOCALAPPDATA%\codebase-memory-mcp, whose ancestor chain is user-owned end to end, so the strict walk passes by construction instead of by environment luck. Falls back to USERPROFILE\AppData\Local and finally the process temp root when the environment is degraded. Also make the staging observable where it previously failed blind: cli_windows_stage_private_file reports which transaction step failed with the OS error code, and cbm_mkdtemp warns once if the private security descriptor cannot be applied and it falls back to default directory security. A blind boolean here cost several blind CI round-trips. win.sh: scp targets need native C:/ paths on Windows OpenSSH. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 343582a commit aa90491

3 files changed

Lines changed: 81 additions & 12 deletions

File tree

‎src/cli/cli.c‎

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9477,11 +9477,31 @@ static bool cli_windows_stage_private_file(
94779477
cbm_activation_transaction_status_t status =
94789478
cbm_activation_transaction_stage_file(target, source, &transaction);
94799479
cli_binary_validator_t validator = {{0}};
9480-
bool ready = status == CBM_ACTIVATION_TRANSACTION_OK && transaction &&
9481-
cli_activation_transaction_expected_build(transaction, &validator) &&
9482-
cli_activation_transaction_commit_validated(transaction, &validator,
9483-
CLI_OCTAL_PERM) == CLI_OK &&
9484-
cli_activation_transaction_finalize_close(&transaction) == CLI_OK;
9480+
/* Each transaction step reports separately: a directory-validation
9481+
* refusal, a build-identity mismatch, and a commit failure are three
9482+
* different bugs, and the caller's single boolean previously collapsed
9483+
* them into one blind "staging failed" with no OS error code. */
9484+
bool ready = status == CBM_ACTIVATION_TRANSACTION_OK && transaction;
9485+
if (!ready) {
9486+
(void)fprintf(stderr, "error: staging transaction open failed (status %d, os %lu)\n",
9487+
(int)status, (unsigned long)GetLastError());
9488+
}
9489+
if (ready && !cli_activation_transaction_expected_build(transaction, &validator)) {
9490+
(void)fprintf(stderr, "error: staged copy build-identity validation failed (os %lu)\n",
9491+
(unsigned long)GetLastError());
9492+
ready = false;
9493+
}
9494+
if (ready && cli_activation_transaction_commit_validated(transaction, &validator,
9495+
CLI_OCTAL_PERM) != CLI_OK) {
9496+
(void)fprintf(stderr, "error: staging transaction commit failed (os %lu)\n",
9497+
(unsigned long)GetLastError());
9498+
ready = false;
9499+
}
9500+
if (ready && cli_activation_transaction_finalize_close(&transaction) != CLI_OK) {
9501+
(void)fprintf(stderr, "error: staging transaction finalize failed (os %lu)\n",
9502+
(unsigned long)GetLastError());
9503+
ready = false;
9504+
}
94859505
(void)cli_activation_transaction_abort(&transaction);
94869506
if (ready && fingerprint_out) {
94879507
(void)snprintf(fingerprint_out, CBM_DAEMON_BUILD_FINGERPRINT_SIZE, "%s",
@@ -9513,6 +9533,37 @@ static bool cli_windows_stage_private_bytes(
95139533
return ready;
95149534
}
95159535

9536+
/* Per-user staging root for Windows install/update transactions. The
9537+
* activation transaction and the release-descriptor probe validate the FULL
9538+
* ancestor chain of everything they stage (trusted owner and no foreign
9539+
* mutation grant on any path component). The process temp root routinely
9540+
* fails that walk through no fault of ours: under msys2, TMP resolves
9541+
* inside the msys install tree (C:\msys64\tmp), and on GitHub runners
9542+
* inside the runner work directory, both of which carry broad inherited
9543+
* grants such as Authenticated Users modify on an upper component. The
9544+
* profile's AppData\Local chain is user-owned end to end, so staging there
9545+
* satisfies the strict walk by construction instead of by environment
9546+
* luck. */
9547+
static const char *cli_windows_staging_root(char root_out[CLI_BUF_1K]) {
9548+
char base[CLI_BUF_1K];
9549+
if (!cbm_safe_getenv("LOCALAPPDATA", base, sizeof(base), NULL) || !base[0]) {
9550+
char profile[CLI_BUF_1K];
9551+
if (!cbm_safe_getenv("USERPROFILE", profile, sizeof(profile), NULL) || !profile[0]) {
9552+
return cbm_tmpdir();
9553+
}
9554+
int base_length = snprintf(base, sizeof(base), "%s/AppData/Local", profile);
9555+
if (base_length <= 0 || (size_t)base_length >= sizeof(base)) {
9556+
return cbm_tmpdir();
9557+
}
9558+
}
9559+
int written = snprintf(root_out, CLI_BUF_1K, "%s/codebase-memory-mcp", base);
9560+
if (written <= 0 || written >= CLI_BUF_1K) {
9561+
return cbm_tmpdir();
9562+
}
9563+
(void)_mkdir(root_out);
9564+
return root_out;
9565+
}
9566+
95169567
static bool cli_windows_prepare_install_pair(const wchar_t *launcher_source,
95179568
const wchar_t *payload_source,
95189569
const char expected_payload_sha256[65],
@@ -9521,8 +9572,9 @@ static bool cli_windows_prepare_install_pair(const wchar_t *launcher_source,
95219572
char directory_out[CLI_BUF_1K]) {
95229573
char *launcher_source_utf8 = cbm_wide_to_utf8(launcher_source);
95239574
char *payload_source_utf8 = cbm_wide_to_utf8(payload_source);
9524-
int directory_length =
9525-
snprintf(directory_out, CLI_BUF_1K, "%s/cbm-win-install-XXXXXX", cbm_tmpdir());
9575+
char staging_root[CLI_BUF_1K];
9576+
int directory_length = snprintf(directory_out, CLI_BUF_1K, "%s/cbm-win-install-XXXXXX",
9577+
cli_windows_staging_root(staging_root));
95269578
bool ready = launcher_source_utf8 && payload_source_utf8 && directory_length > 0 &&
95279579
directory_length < CLI_BUF_1K && cbm_mkdtemp(directory_out);
95289580
char launcher_target[CLI_BUF_1K];
@@ -9622,8 +9674,9 @@ static bool cli_windows_prepare_update_pair(const cbm_windows_release_pair_t *pa
96229674
pair->payload_len <= 0 || !expected_payload_sha256) {
96239675
return false;
96249676
}
9625-
int directory_length =
9626-
snprintf(directory_out, CLI_BUF_1K, "%s/cbm-win-update-XXXXXX", cbm_tmpdir());
9677+
char staging_root[CLI_BUF_1K];
9678+
int directory_length = snprintf(directory_out, CLI_BUF_1K, "%s/cbm-win-update-XXXXXX",
9679+
cli_windows_staging_root(staging_root));
96279680
bool ready =
96289681
directory_length > 0 && directory_length < CLI_BUF_1K && cbm_mkdtemp(directory_out);
96299682
char launcher_target[CLI_BUF_1K];

‎src/foundation/compat.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,22 @@ char *cbm_mkdtemp(char *tmpl) {
129129
}
130130
if (!_mktemp(buf))
131131
return NULL;
132-
if (!win_mkdtemp_private_create(buf) && _mkdir(buf) != 0)
133-
return NULL;
132+
if (!win_mkdtemp_private_create(buf)) {
133+
/* One-time note: every private-namespace validation downstream
134+
* depends on the explicit descriptor, so a silent fallback turns
135+
* into unexplained owner/DACL refusals far from this call site. */
136+
static bool fallback_reported;
137+
DWORD create_error = GetLastError();
138+
if (_mkdir(buf) != 0)
139+
return NULL;
140+
if (!fallback_reported) {
141+
fallback_reported = true;
142+
(void)fprintf(stderr,
143+
"warning: private temp-directory descriptor unavailable "
144+
"(os %lu); using default directory security\n",
145+
(unsigned long)create_error);
146+
}
147+
}
134148
/* Normalize to forward slashes. Callers embed this path in JSON repo_path
135149
* (where "\t"/"\a" are invalid escapes → index fails) and pass it to git -C.
136150
* Windows file APIs accept forward slashes, so the created dir is unaffected. */

‎test-infrastructure/vm/win.sh‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ sh)
6161
;;
6262
push-file)
6363
[ $# -eq 2 ] || { echo "usage: win.sh push-file <local-path> <vm-path>" >&2; exit 2; }
64-
scp -i "$KEY" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$1" "${USER_}@${HOST}:$2"
64+
# Windows OpenSSH resolves scp targets natively: use C:/... not /c/...
65+
dest="${2/#\/c\//C:\/}"
66+
scp -i "$KEY" -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "$1" "${USER_}@${HOST}:${dest}"
6567
;;
6668
asan-build)
6769
# x86_64 (CI's exact arch) with ASan, runs under Windows-on-ARM emulation.

0 commit comments

Comments
 (0)