Skip to content

Commit 5c0b241

Browse files
authored
Merge pull request DeusData#2275 from DeusData/fix/win-endpoint-dir-create-race
fix(daemon): losing the runtime-directory creation race is not a failure on Windows
2 parents 1160fa3 + a041794 commit 5c0b241

3 files changed

Lines changed: 217 additions & 7 deletions

File tree

‎src/daemon/ipc.c‎

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,31 @@ static void ipc_startup_gate_run(void) {
214214
}
215215
#endif
216216

217+
#ifdef _WIN32
218+
static cbm_daemon_ipc_win_directory_create_hook_fn g_win_directory_create_hook_for_test;
219+
static void *g_win_directory_create_hook_context_for_test;
220+
#endif
221+
222+
void cbm_daemon_ipc_win_directory_create_hook_set_for_test(
223+
cbm_daemon_ipc_win_directory_create_hook_fn hook, void *context) {
224+
#ifdef _WIN32
225+
g_win_directory_create_hook_context_for_test = context;
226+
g_win_directory_create_hook_for_test = hook;
227+
#else
228+
(void)hook;
229+
(void)context;
230+
#endif
231+
}
232+
233+
#ifdef _WIN32
234+
static void ipc_win_directory_create_hook_run(const wchar_t *path) {
235+
cbm_daemon_ipc_win_directory_create_hook_fn hook = g_win_directory_create_hook_for_test;
236+
if (hook) {
237+
hook(path, g_win_directory_create_hook_context_for_test);
238+
}
239+
}
240+
#endif
241+
217242
bool cbm_daemon_ipc_windows_legacy_names(const char *canonical_runtime_parent,
218243
const char *instance_key,
219244
char pipe_out[CBM_DAEMON_IPC_WINDOWS_NAME_CAP],
@@ -3994,6 +4019,16 @@ static char *wide_to_utf8(const wchar_t *value) {
39944019
return utf8;
39954020
}
39964021

4022+
/* Record "<path>: <message>" as the validation detail, with the wide path
4023+
* rendered as UTF-8. One place owns the conversion buffer, so every refusal on
4024+
* the directory walk can name its component without each call site carrying
4025+
* its own allocation. */
4026+
static void ipc_validation_detail_set_for_path(const wchar_t *path, const char *message) {
4027+
char *path_utf8 = wide_to_utf8(path);
4028+
ipc_validation_detail_set("%s: %s", path_utf8 ? path_utf8 : "<path>", message);
4029+
free(path_utf8);
4030+
}
4031+
39974032
static wchar_t *wide_copy(const wchar_t *value) {
39984033
if (!value) {
39994034
return NULL;
@@ -4861,13 +4896,28 @@ static bool win_runtime_directory_secure(const wchar_t *runtime_dir) {
48614896
return false;
48624897
}
48634898
bool created = CreateDirectoryW(runtime_dir, &security.directory_attributes) != 0;
4864-
if (!created && GetLastError() != ERROR_ALREADY_EXISTS) {
4899+
DWORD create_error = created ? ERROR_SUCCESS : GetLastError();
4900+
if (!created && create_error != ERROR_ALREADY_EXISTS) {
4901+
char message[96];
4902+
(void)snprintf(message, sizeof(message),
4903+
"could not create the directory (Windows error %lu)",
4904+
(unsigned long)create_error);
4905+
ipc_validation_detail_set_for_path(runtime_dir, message);
48654906
win_security_destroy(&security);
48664907
return false;
48674908
}
48684909
DWORD attributes = GetFileAttributesW(runtime_dir);
48694910
if (attributes == INVALID_FILE_ATTRIBUTES || (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0 ||
48704911
(attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
4912+
/* What already exists at this path is adopted only if it is a plain
4913+
* directory. Say which rule refused it: the walk in front of this
4914+
* function tolerates losing a creation race, so this check is what
4915+
* stands between that tolerance and a planted file or junction. */
4916+
const char *rule = attributes == INVALID_FILE_ATTRIBUTES ? "cannot be inspected"
4917+
: (attributes & FILE_ATTRIBUTE_DIRECTORY) == 0
4918+
? "exists but is not a directory"
4919+
: "is a reparse point (junction or symlink)";
4920+
ipc_validation_detail_set_for_path(runtime_dir, rule);
48714921
win_security_destroy(&security);
48724922
return false;
48734923
}
@@ -5021,8 +5071,35 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) {
50215071
DWORD attributes = GetFileAttributesW(path);
50225072
if (attributes == INVALID_FILE_ATTRIBUTES) {
50235073
DWORD error = GetLastError();
5024-
ok = (error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND) &&
5025-
CreateDirectoryW(path, &security.directory_attributes) != 0;
5074+
bool absent = error == ERROR_FILE_NOT_FOUND || error == ERROR_PATH_NOT_FOUND;
5075+
if (absent) {
5076+
ipc_win_directory_create_hook_run(path);
5077+
/* Several processes first-starting together all observe
5078+
* this component absent; one CreateDirectoryW wins and
5079+
* the rest get ERROR_ALREADY_EXISTS. Losing that race is
5080+
* not a failure -- the directory this process wanted now
5081+
* exists, exactly as if it had been there all along. It
5082+
* is NOT trusted for that: an ancestor still goes through
5083+
* win_directory_component_secure() below, and the final
5084+
* component through win_runtime_directory_secure(), which
5085+
* refuses a non-directory or reparse point and enforces
5086+
* owner and DACL. The POSIX walk tolerates EEXIST at the
5087+
* same point for the same reason. */
5088+
if (CreateDirectoryW(path, &security.directory_attributes) == 0) {
5089+
error = GetLastError();
5090+
absent = error == ERROR_ALREADY_EXISTS;
5091+
}
5092+
}
5093+
ok = absent;
5094+
if (!ok) {
5095+
/* Never a bare "(endpoint)": name the component and the
5096+
* Windows error that refused it. */
5097+
char message[96];
5098+
(void)snprintf(message, sizeof(message),
5099+
"could not create or inspect the directory (Windows error %lu)",
5100+
(unsigned long)error);
5101+
ipc_validation_detail_set_for_path(path, message);
5102+
}
50265103
}
50275104
/* Ancestors are observe-only and must already be secure. The
50285105
* final current-user directory is intentionally handled below by
@@ -5034,10 +5111,7 @@ static bool win_private_directory_tree_secure(const wchar_t *directory_path) {
50345111
* at this walk position; the helper set the inner rule. */
50355112
char inner[384];
50365113
(void)snprintf(inner, sizeof(inner), "%s", ipc_validation_detail_buffer);
5037-
char *component_utf8 = wide_to_utf8(path);
5038-
ipc_validation_detail_set(
5039-
"%s: %s", component_utf8 ? component_utf8 : "<component>", inner);
5040-
free(component_utf8);
5114+
ipc_validation_detail_set_for_path(path, inner);
50415115
}
50425116
}
50435117
}

‎src/daemon/ipc_internal.h‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,15 @@ void cbm_daemon_ipc_posix_record_write_failure_set_for_test(int errno_value);
110110
typedef void (*cbm_daemon_ipc_startup_gate_fn)(void *context);
111111
void cbm_daemon_ipc_startup_gate_set_for_test(cbm_daemon_ipc_startup_gate_fn gate, void *context);
112112

113+
/* Deterministic-interleaving seam for the Windows private-directory walk:
114+
* fires after a path component was observed ABSENT and before this process
115+
* tries to create it. A test plays the concurrent process that wins that
116+
* creation, which pins the cold-start race (N processes first-starting
117+
* against a runtime directory that does not exist yet) by construction
118+
* instead of by thread timing. `path` is the NUL-terminated component path at
119+
* the walk position. No-op off Windows. */
120+
typedef void (*cbm_daemon_ipc_win_directory_create_hook_fn)(const wchar_t *path, void *context);
121+
void cbm_daemon_ipc_win_directory_create_hook_set_for_test(
122+
cbm_daemon_ipc_win_directory_create_hook_fn hook, void *context);
123+
113124
#endif /* CBM_DAEMON_IPC_INTERNAL_H */

‎tests/test_daemon_ipc.c‎

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,129 @@ TEST(daemon_ipc_windows_private_directory_ace_is_inheritable) {
882882
PASS();
883883
}
884884

885+
typedef struct {
886+
int calls;
887+
bool plant_file; /* false: play a racer that creates the DIRECTORY first */
888+
bool planted;
889+
} ipc_test_win_create_racer_t;
890+
891+
/* Plays the concurrent process that wins the creation of a path component the
892+
* walk has just observed absent. Runs inside the walk, at the exact point the
893+
* race lives, so the interleaving is pinned by construction: no threads, no
894+
* timing. */
895+
static void ipc_test_win_create_racer(const wchar_t *path, void *context) {
896+
ipc_test_win_create_racer_t *racer = context;
897+
racer->calls++;
898+
if (racer->calls != 1) {
899+
return;
900+
}
901+
if (racer->plant_file) {
902+
HANDLE file =
903+
CreateFileW(path, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
904+
racer->planted = file != INVALID_HANDLE_VALUE;
905+
if (racer->planted) {
906+
(void)CloseHandle(file);
907+
}
908+
} else {
909+
racer->planted = CreateDirectoryW(path, NULL) != 0;
910+
}
911+
}
912+
913+
/* Cold-start race (test-windows-guards section_cold_storm, 2026-09-21; the
914+
* same thing a user gets when a host launches several MCP servers on its very
915+
* first run): N processes first-start against a runtime directory that does
916+
* not exist yet. All of them observe the final component absent, one
917+
* CreateDirectoryW wins, and every other process gets ERROR_ALREADY_EXISTS.
918+
* The walk treated that as a failure and recorded no detail, so the losers
919+
* exited with a bare "secure CLI coordination could not be created
920+
* (endpoint)". Losing the creation race is not a failure: the directory this
921+
* process wanted now exists, and the owner / DACL / not-a-reparse-point checks
922+
* that follow are exactly the ones a pre-existing directory gets. The POSIX
923+
* walk has always tolerated EEXIST here. */
924+
TEST(daemon_ipc_windows_private_directory_survives_lost_creation_race) {
925+
char parent[TEST_PATH_CAP] = {0};
926+
char cache[TEST_PATH_CAP] = {0};
927+
bool paths_ok = false;
928+
bool secured = false;
929+
bool owner_only = false;
930+
ipc_test_win_create_racer_t racer = {0};
931+
932+
if (ipc_test_parent_new(parent, "win-create-race")) {
933+
int written = snprintf(cache, sizeof(cache), "%s/cache", parent);
934+
paths_ok = written > 0 && written < (int)sizeof(cache);
935+
}
936+
if (paths_ok) {
937+
cbm_daemon_ipc_win_directory_create_hook_set_for_test(ipc_test_win_create_racer, &racer);
938+
secured = cbm_daemon_ipc_private_directory_secure(cache);
939+
cbm_daemon_ipc_win_directory_create_hook_set_for_test(NULL, NULL);
940+
}
941+
if (secured) {
942+
/* The racer created the directory with an inherited DACL; the winner's
943+
* directory must still end up owner-only, like any adopted one. */
944+
PACL dacl = NULL;
945+
PSECURITY_DESCRIPTOR descriptor = NULL;
946+
ACL_SIZE_INFORMATION information;
947+
memset(&information, 0, sizeof(information));
948+
if (GetNamedSecurityInfoA(cache, SE_FILE_OBJECT, DACL_SECURITY_INFORMATION, NULL, NULL,
949+
&dacl, NULL, &descriptor) == ERROR_SUCCESS &&
950+
dacl &&
951+
GetAclInformation(dacl, &information, sizeof(information), AclSizeInformation)) {
952+
owner_only = information.AceCount == 1;
953+
}
954+
if (descriptor) {
955+
(void)LocalFree(descriptor);
956+
}
957+
}
958+
959+
(void)RemoveDirectoryA(cache);
960+
ipc_test_remove_flat_dir(parent);
961+
962+
ASSERT_TRUE(paths_ok);
963+
/* The seam sat on the path and the racer really won: without these two the
964+
* test could pass without ever exercising the race. */
965+
ASSERT_EQ(racer.calls, 1);
966+
ASSERT_TRUE(racer.planted);
967+
ASSERT_TRUE(secured);
968+
ASSERT_TRUE(owner_only);
969+
PASS();
970+
}
971+
972+
/* The tolerance above must not admit anything: when what appeared at the path
973+
* is NOT a directory, the walk still refuses -- and it says why. A bare
974+
* "(endpoint)" is what four reporters in #1533/#1574 were left with; every
975+
* refusal on this path names the component and the rule. */
976+
TEST(daemon_ipc_windows_private_directory_refuses_and_names_a_planted_file) {
977+
char parent[TEST_PATH_CAP] = {0};
978+
char cache[TEST_PATH_CAP] = {0};
979+
char detail[512] = {0};
980+
bool paths_ok = false;
981+
bool refused = false;
982+
ipc_test_win_create_racer_t racer = {.plant_file = true};
983+
984+
if (ipc_test_parent_new(parent, "win-create-race-file")) {
985+
int written = snprintf(cache, sizeof(cache), "%s/cache", parent);
986+
paths_ok = written > 0 && written < (int)sizeof(cache);
987+
}
988+
if (paths_ok) {
989+
cbm_daemon_ipc_win_directory_create_hook_set_for_test(ipc_test_win_create_racer, &racer);
990+
refused = !cbm_daemon_ipc_private_directory_secure(cache);
991+
cbm_daemon_ipc_win_directory_create_hook_set_for_test(NULL, NULL);
992+
const char *why = cbm_daemon_ipc_validation_detail();
993+
(void)snprintf(detail, sizeof(detail), "%s", why ? why : "");
994+
}
995+
996+
(void)DeleteFileA(cache);
997+
ipc_test_remove_flat_dir(parent);
998+
999+
ASSERT_TRUE(paths_ok);
1000+
ASSERT_EQ(racer.calls, 1);
1001+
ASSERT_TRUE(racer.planted);
1002+
ASSERT_TRUE(refused);
1003+
ASSERT_NOT_NULL(strstr(detail, "cache"));
1004+
ASSERT_NOT_NULL(strstr(detail, "not a directory"));
1005+
PASS();
1006+
}
1007+
8851008
TEST(daemon_ipc_windows_private_directory_allows_add_subdirectory_only_ancestor) {
8861009
char parent[TEST_PATH_CAP] = {0};
8871010
char add_only[TEST_PATH_CAP] = {0};
@@ -5361,6 +5484,8 @@ SUITE(daemon_ipc) {
53615484
RUN_TEST(daemon_ipc_windows_default_endpoint_ignores_temp_environment);
53625485
RUN_TEST(daemon_ipc_windows_private_directory_rejects_untrusted_ancestor_acl);
53635486
RUN_TEST(daemon_ipc_windows_private_directory_ace_is_inheritable);
5487+
RUN_TEST(daemon_ipc_windows_private_directory_survives_lost_creation_race);
5488+
RUN_TEST(daemon_ipc_windows_private_directory_refuses_and_names_a_planted_file);
53645489
RUN_TEST(daemon_ipc_windows_private_directory_allows_add_subdirectory_only_ancestor);
53655490
RUN_TEST(daemon_ipc_windows_sid_trust_accepts_local_admin_rejects_foreign_500);
53665491
RUN_TEST(daemon_ipc_windows_legacy_bridge_covers_handoff_and_lifetime);

0 commit comments

Comments
 (0)