Skip to content

Commit f517455

Browse files
committed
fix(win): reconcile the C suite with the managed-launcher contract
The first full Windows x64 CI run of this branch (earlier contract-step walls had always aborted the leg) surfaced 40 failures: the managed launcher's fail-closed install/update/uninstall gates and the suite's POSIX-modeled activation tests were both new in this branch and had never been reconciled, and they hid three real production bugs. Production fixes: - ui: config publishes with POSIX rename semantics. MoveFileExW's legacy replace cannot supersede a destination a reader holds open (the name lingers until the last handle closes, even with FILE_SHARE_DELETE), so a config save racing an open reader failed. The temp file is opened with DELETE access and renamed in-hand (bare-path target, NUL-padded rename record) before close, falling back to MoveFileExW on any rename-by-handle failure so non-NTFS config homes keep the previous behavior. - subprocess(win): the child's redirected log handle now shares write access. POSIX places no lock on the redirected log, so a second producer may append while the child runs; the missing FILE_SHARE_WRITE mandatory-locked every other writer out, which the index-supervisor callback-isolation test legitimately exercises. - cli(win): the portable uninstall path composes the managed binary name with its .exe suffix; it could never find (or remove) the installed binary before. - activation: security-predicate refusals are now diagnosable. The transaction's predicates refuse without a usable OS last-error, which collapsed five distinct refusal classes into one blind "status -3, os 0" at every caller. Each refusing predicate records what refused — predicate, untrusted SID, and object path — first-wins so the deepest cause survives, and the staging error line appends it. This is what identified every Windows refusal in this batch, including the real install-failure class on drive-root-rooted payload directories (Authenticated Users holds inherited Modify there; profile-rooted directories do not). Windows test dispatch (release binaries unchanged): - Test builds (CBM_CLI_ENABLE_TEST_API, the existing test-object macro) compile the portable activation flow on Windows, reachable solely through the installed activation test seam and announced loudly on stderr; release binaries compile exclusively the managed transaction, so the dispatch polarity fails closed. The suite's install/update/ uninstall calls route through a seam-if-absent wrapper, keeping the shared activation and agent-config semantics unit-covered on Windows; POSIX arms keep exercising the real activation machinery. - Two new release-gate tests assert the fail-closed contract with the seam OFF: a portable payload may not self-update/self-uninstall, and install refuses to adopt or replace a foreign file at the canonical launcher path even under --force. - Test-bug fixes the first valid Windows runs exposed: the version- cohort crash test reused one conflict record across the probe and the turnover acquire (whose entry memset erased the probe's detail before the assertions read it); the activation staged-path assertion now checks containment rather than a separator (the \\?\ namespace performs no forward-slash translation); the ui atomic test captures, cleans up, then asserts so a failure cannot poison later tests; the PATH-visibility guard is POSIX-only (Windows configures PATH through the user registry, not a shell rc). Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 6ab5e3f commit f517455

9 files changed

Lines changed: 435 additions & 99 deletions

File tree

‎src/cli/activation_transaction.c‎

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define WIN32_LEAN_AND_MEAN
1818
#endif
1919
#include <aclapi.h>
20+
#include <sddl.h>
2021
#include <windows.h>
2122
#else
2223
#include <fcntl.h>
@@ -78,6 +79,37 @@ struct cbm_activation_transaction {
7879
#endif
7980
};
8081

82+
/* The transaction's security predicates refuse by returning false without a
83+
* usable OS last-error, so every caller used to report the same blind
84+
* "status -3, os 0" for five distinct refusal classes (ancestor open,
85+
* owner, DACL, target snapshot, staged create). The first predicate that
86+
* refuses records what refused here; callers append the note to their error
87+
* line. First-wins so the deepest (root) refusal survives the unwind.
88+
* Single-threaded by contract: CLI activation runs on one thread. */
89+
static char g_activation_refusal_note[512];
90+
91+
/* The validated object's path, set by each validator for the duration of its
92+
* predicate calls so the note can say WHAT was refused, not just why. Points
93+
* into the caller's storage; never dereferenced after the validator returns. */
94+
static const char *g_activation_refusal_object = NULL;
95+
96+
static void activation_refusal_clear(void) {
97+
g_activation_refusal_note[0] = '\0';
98+
}
99+
100+
static void activation_note_refusal(const char *predicate, unsigned long os_error) {
101+
if (g_activation_refusal_note[0] != '\0') {
102+
return;
103+
}
104+
(void)snprintf(g_activation_refusal_note, sizeof(g_activation_refusal_note), "%s (os %lu)%s%s",
105+
predicate, os_error, g_activation_refusal_object ? " at " : "",
106+
g_activation_refusal_object ? g_activation_refusal_object : "");
107+
}
108+
109+
const char *cbm_activation_transaction_refusal_note(void) {
110+
return g_activation_refusal_note;
111+
}
112+
81113
#ifdef _WIN32
82114
typedef HANDLE activation_native_file_t;
83115
#define ACTIVATION_INVALID_FILE INVALID_HANDLE_VALUE
@@ -382,6 +414,9 @@ static bool activation_windows_owner_is_trusted(HANDLE handle) {
382414
bool trusted = result == ERROR_SUCCESS && owner && IsValidSid(owner) &&
383415
(EqualSid(owner, user_sid) || IsWellKnownSid(owner, WinLocalSystemSid) ||
384416
IsWellKnownSid(owner, WinBuiltinAdministratorsSid));
417+
if (!trusted) {
418+
activation_note_refusal("owner-not-trusted", result);
419+
}
385420
if (descriptor) {
386421
(void)LocalFree(descriptor);
387422
}
@@ -400,6 +435,9 @@ static bool activation_windows_owner_is_current(HANDLE handle) {
400435
DWORD result = GetSecurityInfo(handle, SE_FILE_OBJECT, OWNER_SECURITY_INFORMATION, &owner, NULL,
401436
NULL, NULL, &descriptor);
402437
bool same = result == ERROR_SUCCESS && owner && IsValidSid(owner) && EqualSid(owner, user_sid);
438+
if (!same) {
439+
activation_note_refusal("owner-not-current-user", result);
440+
}
403441
if (descriptor) {
404442
(void)LocalFree(descriptor);
405443
}
@@ -482,9 +520,21 @@ static bool activation_windows_acl_check(HANDLE handle, DWORD tolerated_untruste
482520
* launcher ancestry validators). */
483521
IsWellKnownSid(sid, WinCreatorOwnerRightsSid));
484522
if (!trusted) {
523+
char label[128];
524+
LPSTR sid_text = NULL;
525+
(void)snprintf(label, sizeof(label), "acl-grants-cross-account-mutation to %s",
526+
ConvertSidToStringSidA(sid, &sid_text) && sid_text ? sid_text
527+
: "unparsable-sid");
528+
if (sid_text) {
529+
(void)LocalFree(sid_text);
530+
}
531+
activation_note_refusal(label, 0UL);
485532
secure = false;
486533
}
487534
}
535+
if (!secure) {
536+
activation_note_refusal("acl-unreadable-or-malformed", result);
537+
}
488538
if (descriptor) {
489539
(void)LocalFree(descriptor);
490540
}
@@ -560,13 +610,18 @@ static HANDLE activation_windows_directory_open_no_reparse(const char *directory
560610
CreateFileW(prefixed, FILE_READ_ATTRIBUTES | READ_CONTROL,
561611
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING,
562612
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, NULL);
613+
DWORD open_error = handle == INVALID_HANDLE_VALUE ? GetLastError() : ERROR_SUCCESS;
563614
prefixed[boundary + 4U] = saved;
564615
BY_HANDLE_FILE_INFORMATION information;
565616
bool valid = handle != INVALID_HANDLE_VALUE && GetFileType(handle) == FILE_TYPE_DISK &&
566617
GetFileInformationByHandle(handle, &information) &&
567618
(information.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0 &&
568619
(information.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) == 0;
569620
if (!valid) {
621+
activation_note_refusal(handle == INVALID_HANDLE_VALUE
622+
? "ancestor-component-open"
623+
: "ancestor-component-not-plain-directory",
624+
open_error);
570625
if (handle != INVALID_HANDLE_VALUE) {
571626
(void)CloseHandle(handle);
572627
}
@@ -592,6 +647,7 @@ static HANDLE activation_windows_directory_open_no_reparse(const char *directory
592647
* copy's build-fingerprint validation. */
593648
static bool activation_source_directory_secure(const char *directory,
594649
activation_file_identity_t *identity_out) {
650+
g_activation_refusal_object = directory;
595651
HANDLE handle = activation_windows_directory_open_no_reparse(directory);
596652
BY_HANDLE_FILE_INFORMATION information;
597653
bool open_ok = handle != INVALID_HANDLE_VALUE &&
@@ -605,12 +661,14 @@ static bool activation_source_directory_secure(const char *directory,
605661
if (handle != INVALID_HANDLE_VALUE) {
606662
(void)CloseHandle(handle);
607663
}
664+
g_activation_refusal_object = NULL;
608665
return acl_ok;
609666
}
610667

611668
static bool activation_directory_secure(const char *directory, int *unused,
612669
activation_file_identity_t *identity_out) {
613670
(void)unused;
671+
g_activation_refusal_object = directory;
614672
HANDLE handle = activation_windows_directory_open_no_reparse(directory);
615673
BY_HANDLE_FILE_INFORMATION information;
616674
bool open_ok = handle != INVALID_HANDLE_VALUE &&
@@ -623,6 +681,7 @@ static bool activation_directory_secure(const char *directory, int *unused,
623681
if (handle != INVALID_HANDLE_VALUE) {
624682
(void)CloseHandle(handle);
625683
}
684+
g_activation_refusal_object = NULL;
626685
return acl_ok;
627686
}
628687

@@ -853,6 +912,7 @@ static activation_create_status_t activation_private_file_create(
853912
free(wide);
854913
return ACTIVATION_CREATE_ERROR;
855914
}
915+
g_activation_refusal_object = path;
856916
SetLastError(ERROR_SUCCESS);
857917
HANDLE file = CreateFileW(wide, GENERIC_READ | GENERIC_WRITE | READ_CONTROL | DELETE,
858918
FILE_SHARE_READ, &security.attributes, CREATE_NEW,
@@ -865,13 +925,19 @@ static activation_create_status_t activation_private_file_create(
865925
activation_windows_owner_is_current(file) && activation_windows_acl_secure(file);
866926
activation_windows_security_destroy(&security);
867927
if (!valid) {
928+
if (file == INVALID_HANDLE_VALUE && error != ERROR_FILE_EXISTS &&
929+
error != ERROR_ALREADY_EXISTS) {
930+
activation_note_refusal("staged-file-create", error);
931+
}
932+
g_activation_refusal_object = NULL;
868933
if (file != INVALID_HANDLE_VALUE) {
869934
(void)CloseHandle(file);
870935
}
871936
return error == ERROR_FILE_EXISTS || error == ERROR_ALREADY_EXISTS
872937
? ACTIVATION_CREATE_EXISTS
873938
: ACTIVATION_CREATE_ERROR;
874939
}
940+
g_activation_refusal_object = NULL;
875941
*file_out = file;
876942
return ACTIVATION_CREATE_OK;
877943
#else
@@ -1171,6 +1237,7 @@ static cbm_activation_transaction_status_t activation_transaction_prepare(
11711237
const char *target_path, activation_action_t action,
11721238
cbm_activation_transaction_t **transaction_out) {
11731239
*transaction_out = NULL;
1240+
activation_refusal_clear();
11741241
cbm_activation_transaction_t *transaction = calloc(1, sizeof(*transaction));
11751242
if (!transaction) {
11761243
return CBM_ACTIVATION_TRANSACTION_NO_MEMORY;
@@ -1203,8 +1270,7 @@ static cbm_activation_transaction_status_t activation_transaction_prepare(
12031270
}
12041271
if (!activation_entry_snapshot(transaction, transaction->target_path, transaction->target_name,
12051272
&transaction->target_existed, &transaction->target_identity)) {
1206-
#ifdef _WIN32
1207-
#endif
1273+
activation_note_refusal("target-entry-snapshot", 0UL);
12081274
activation_transaction_destroy(transaction);
12091275
return CBM_ACTIVATION_TRANSACTION_IO;
12101276
}

‎src/cli/activation_transaction.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,11 @@ const char *cbm_activation_transaction_deferred_path(
8585

8686
const char *cbm_activation_transaction_status_message(cbm_activation_transaction_status_t status);
8787

88+
/* Which security predicate refused the most recent transaction, as
89+
* "predicate (os N)", or "" when nothing refused since the last prepare.
90+
* The predicates refuse without a usable OS last-error, so this is the only
91+
* way a caller can say WHY staging failed. Reset by every prepare/stage
92+
* entry; single-threaded like the rest of the transaction API. */
93+
const char *cbm_activation_transaction_refusal_note(void);
94+
8895
#endif /* CBM_ACTIVATION_TRANSACTION_H */

0 commit comments

Comments
 (0)