@@ -26,13 +26,6 @@ enum {
2626 CBM_DAEMON_IPC_RETRY_INTERVAL_MS = 10 ,
2727 CBM_DAEMON_IPC_COORDINATION_CLEANUP_MS = 500 ,
2828 CBM_DAEMON_IPC_WINDOWS_RENDEZVOUS_RETRY_MS = 250 ,
29- /* After ConnectNamedPipe the server must see the client's first bytes
30- * before it can impersonate it (ImpersonateNamedPipeClient fails with
31- * ERROR_CANNOT_IMPERSONATE until data has been read from the pipe). The
32- * client always sends its HELLO request immediately after connecting, so
33- * this only bounds how long a connect-but-never-send peer is tolerated;
34- * generous for starved CI runners, still finite. */
35- CBM_DAEMON_IPC_FIRST_FRAME_TIMEOUT_MS = 15000 ,
3629};
3730
3831/* Last private-namespace validation refusal, set at the exact failing check.
@@ -3364,14 +3357,6 @@ struct cbm_daemon_ipc_connection {
33643357 HANDLE handle ;
33653358 atomic_bool poisoned ;
33663359 cbm_daemon_ipc_pipe_role_t role ;
3367- /* First frame header, read by the accept path BEFORE impersonating the
3368- * client: ImpersonateNamedPipeClient needs a COMPLETED read on the pipe,
3369- * not merely buffered data (PeekNamedPipe is insufficient). connection_
3370- * read_full drains these bytes before touching the pipe again, so the
3371- * runtime's frame parser still sees an intact HELLO. Server role only. */
3372- unsigned char prime [CBM_DAEMON_FRAME_HEADER_SIZE ];
3373- size_t prime_len ;
3374- size_t prime_off ;
33753360};
33763361
33773362struct cbm_daemon_ipc_startup_lock {
@@ -3846,14 +3831,6 @@ static win_generation_address_t *win_endpoint_generation_snapshot(
38463831 const cbm_daemon_ipc_endpoint_t * endpoint );
38473832static int win_legacy_pipe_probe (const cbm_daemon_ipc_endpoint_t * endpoint );
38483833
3849- static bool win_token_is_current_user (win_security_t * security , HANDLE token ) {
3850- PSID token_sid = NULL ;
3851- void * token_user = win_token_user_query (security , token , & token_sid );
3852- bool equal = token_user && token_sid && security -> equal_sid (token_sid , security -> user_sid );
3853- free (token_user );
3854- return equal ;
3855- }
3856-
38573834static uint32_t win_sid_read_u32_le (const uint8_t * bytes ) {
38583835 return (uint32_t )bytes [0 ] | ((uint32_t )bytes [1 ] << 8U ) | ((uint32_t )bytes [2 ] << 16U ) |
38593836 ((uint32_t )bytes [3 ] << 24U );
@@ -5154,64 +5131,69 @@ static int win_overlapped_wait(HANDLE handle, OVERLAPPED *overlapped, DWORD time
51545131}
51555132
51565133static bool win_pipe_client_is_current_user (HANDLE pipe ) {
5157- win_security_t security ;
5158- if (!win_security_init (& security )) {
5159- cbm_log_warn ("daemon.accept.client_identity" , "step" , "security_init" );
5160- win_security_destroy (& security );
5134+ /* Validate the connected client by its process token, resolved from the
5135+ * pipe's CLIENT process id - the mirror of the client-side server check
5136+ * (win_pipe_server_is_current_user via GetNamedPipeServerProcessId). This
5137+ * needs neither impersonation nor a prior read on the pipe, so it works at
5138+ * accept time without changing accept's contract. ImpersonateNamedPipeClient
5139+ * was unusable here: it fails with ERROR_CANNOT_IMPERSONATE until the server
5140+ * has completed a read, which accept must not require. */
5141+ HMODULE kernel = GetModuleHandleW (L"kernel32.dll" );
5142+ get_named_pipe_server_process_id_fn get_client_pid =
5143+ kernel ? (get_named_pipe_server_process_id_fn )GetProcAddress (kernel ,
5144+ "GetNamedPipeClientProcessId" )
5145+ : NULL ;
5146+ if (!get_client_pid ) {
5147+ cbm_log_warn ("daemon.accept.client_identity" , "step" , "pid_fn_missing" );
51615148 return false;
51625149 }
5163- if (!security .impersonate_named_pipe_client (pipe )) {
5164- /* ERROR_CANNOT_IMPERSONATE (1368) here means the client has not yet
5165- * written to the pipe - i.e. the daemon validated identity before the
5166- * client sent its first frame, or the accept loop fired on a pipe with
5167- * no real client behind it (the runner spin symptom). */
5150+ ULONG process_id = 0 ;
5151+ if (!get_client_pid (pipe , & process_id ) || process_id == 0 ) {
51685152 char error_text [16 ];
51695153 (void )snprintf (error_text , sizeof (error_text ), "%lu" , (unsigned long )GetLastError ());
5170- cbm_log_warn ("daemon.accept.client_identity" , "step" , "impersonate " , "error" , error_text );
5171- win_security_destroy ( & security );
5154+ cbm_log_warn ("daemon.accept.client_identity" , "step" , "client_pid_query " , "error" ,
5155+ error_text );
51725156 return false;
51735157 }
5174- HANDLE token = NULL ;
5175- bool opened = security .open_thread_token (GetCurrentThread (), TOKEN_QUERY , TRUE, & token ) != 0 ;
5176- if (!opened ) {
5158+ HANDLE process = OpenProcess (PROCESS_QUERY_LIMITED_INFORMATION , FALSE, process_id );
5159+ if (!process ) {
5160+ process = OpenProcess (PROCESS_QUERY_INFORMATION , FALSE, process_id );
5161+ }
5162+ if (!process ) {
5163+ char pid_text [16 ];
51775164 char error_text [16 ];
5165+ (void )snprintf (pid_text , sizeof (pid_text ), "%lu" , (unsigned long )process_id );
51785166 (void )snprintf (error_text , sizeof (error_text ), "%lu" , (unsigned long )GetLastError ());
5179- cbm_log_warn ("daemon.accept.client_identity" , "step" , "thread_token" , "error" , error_text );
5180- }
5181- bool same_user = opened && win_token_is_current_user (& security , token );
5182- if (opened && !same_user ) {
5183- /* Impersonation succeeded but the client's account SID does not match
5184- * the daemon's. Classify the impersonated identity: an anonymous or
5185- * service SID means the pipe carried no real user context. */
5186- PSID client_sid = NULL ;
5187- void * client_user = win_token_user_query (& security , token , & client_sid );
5188- const char * sid_class =
5189- !client_sid ? "unreadable"
5190- : security .is_well_known_sid (client_sid , WinAnonymousSid ) ? "anonymous"
5191- : security .is_well_known_sid (client_sid , WinLocalSystemSid ) ? "system"
5192- : security .is_well_known_sid (client_sid , WinBuiltinAdministratorsSid ) ? "administrators"
5193- : "other_account" ;
5194- cbm_log_warn ("daemon.accept.client_identity" , "step" , "sid_mismatch" , "client" , sid_class );
5195- free (client_user );
5167+ cbm_log_warn ("daemon.accept.client_identity" , "step" , "process_open" , "pid" , pid_text ,
5168+ "error" , error_text );
5169+ return false;
5170+ }
5171+ win_security_t security ;
5172+ bool initialized = win_security_init (& security );
5173+ HANDLE token = NULL ;
5174+ bool opened = initialized && security .open_process_token (process , TOKEN_QUERY , & token ) != 0 ;
5175+ PSID token_sid = NULL ;
5176+ void * token_user = opened ? win_token_user_query (& security , token , & token_sid ) : NULL ;
5177+ bool same_user =
5178+ token_user && token_sid && security .equal_sid (token_sid , security .user_sid ) != 0 ;
5179+ if (!same_user ) {
5180+ const char * step = !initialized ? "security_init"
5181+ : !opened ? "token_open"
5182+ : !token_sid ? "token_query"
5183+ : "sid_mismatch" ;
5184+ cbm_log_warn ("daemon.accept.client_identity" , "step" , step );
51965185 }
5186+ free (token_user );
51975187 if (token ) {
51985188 (void )CloseHandle (token );
51995189 }
5200- bool reverted = security .revert_to_self () != 0 ;
5201- win_security_destroy (& security );
5202- if (!reverted ) {
5203- /* Continuing this daemon thread while it still impersonates an
5204- * untrusted pipe client would corrupt every subsequent access check. */
5205- (void )TerminateProcess (GetCurrentProcess (), ERROR_ACCESS_DENIED );
5206- abort ();
5190+ if (initialized ) {
5191+ win_security_destroy (& security );
52075192 }
5193+ (void )CloseHandle (process );
52085194 return same_user ;
52095195}
52105196
5211- /* Defined below; the accept path pre-reads the first frame header through it. */
5212- static int connection_read_full (cbm_daemon_ipc_connection_t * connection , void * buffer ,
5213- size_t length , uint64_t deadline_ms );
5214-
52155197int cbm_daemon_ipc_accept (cbm_daemon_ipc_listener_t * listener , uint32_t timeout_ms ,
52165198 cbm_daemon_ipc_connection_t * * connection_out ) {
52175199 if (connection_out ) {
@@ -5262,28 +5244,9 @@ int cbm_daemon_ipc_accept(cbm_daemon_ipc_listener_t *listener, uint32_t timeout_
52625244 connection -> handle = listener -> pipe ;
52635245 atomic_init (& connection -> poisoned , false);
52645246 connection -> role = CBM_DAEMON_IPC_PIPE_ROLE_ACCEPTED_SERVER ;
5265- connection -> prime_len = 0 ;
5266- connection -> prime_off = 0 ;
5267- /* Read the client's first frame header before impersonating: the client
5268- * sends its HELLO immediately after connecting, and a COMPLETED read is
5269- * what ImpersonateNamedPipeClient requires (ERROR_CANNOT_IMPERSONATE
5270- * otherwise - PeekNamedPipe availability alone is not enough). The header
5271- * is retained in the connection prime buffer and drained by
5272- * connection_read_full before the runtime's own read. */
5273- if (connection_read_full (connection , connection -> prime , CBM_DAEMON_FRAME_HEADER_SIZE ,
5274- ipc_deadline_after (CBM_DAEMON_IPC_FIRST_FRAME_TIMEOUT_MS )) != 1 ) {
5275- cbm_log_warn ("daemon.accept.client_rejected" , "stage" , "first_frame" );
5276- free (connection );
5277- (void )DisconnectNamedPipe (listener -> pipe );
5278- (void )CloseHandle (listener -> pipe );
5279- listener -> pipe = INVALID_HANDLE_VALUE ;
5280- return -1 ;
5281- }
5282- connection -> prime_len = CBM_DAEMON_FRAME_HEADER_SIZE ;
5247+ /* Validate the client's identity by its process token (no impersonation,
5248+ * no read) so accept keeps its immediate-return contract. */
52835249 if (!win_pipe_client_is_current_user (listener -> pipe )) {
5284- /* The daemon-side twin of the client's server_identity log: without
5285- * it a server-initiated disconnect is indistinguishable from a
5286- * client-side failure in the field. */
52875250 cbm_log_warn ("daemon.accept.client_rejected" , "stage" , "client_identity" );
52885251 free (connection );
52895252 (void )DisconnectNamedPipe (listener -> pipe );
@@ -5532,8 +5495,6 @@ cbm_daemon_ipc_connection_t *cbm_daemon_ipc_connect(const cbm_daemon_ipc_endpoin
55325495 connection -> handle = pipe ;
55335496 atomic_init (& connection -> poisoned , false);
55345497 connection -> role = CBM_DAEMON_IPC_PIPE_ROLE_CONNECTED_CLIENT ;
5535- connection -> prime_len = 0 ;
5536- connection -> prime_off = 0 ;
55375498 return connection ;
55385499}
55395500
@@ -5621,15 +5582,6 @@ static int connection_read_full(cbm_daemon_ipc_connection_t *connection, void *b
56215582 return -1 ;
56225583 }
56235584 size_t offset = 0 ;
5624- /* Drain any bytes pre-read before impersonation (the first frame header)
5625- * ahead of the pipe, so the runtime's frame parser sees an intact stream. */
5626- if (connection -> prime_off < connection -> prime_len ) {
5627- size_t available = connection -> prime_len - connection -> prime_off ;
5628- size_t take = available < length ? available : length ;
5629- memcpy (buffer , connection -> prime + connection -> prime_off , take );
5630- connection -> prime_off += take ;
5631- offset = take ;
5632- }
56335585 while (offset < length ) {
56345586 DWORD chunk = length - offset > MAXDWORD ? MAXDWORD : (DWORD )(length - offset );
56355587 DWORD transferred = 0 ;
0 commit comments