@@ -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+
8851008TEST (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