/* Isolated SPNEGO diagnostic. Original MIDL remains unchanged. Read the own * SYSTEM token first; one management discovery, then at most one status query. * No clock setter, token impersonation, NTP/ACL/provider or product gate. */ #define _WIN32_WINNT 0x0a00 #define UNICODE 1 #include #include #include #include #include typedef struct { uint32_t stage, native_rpc_return, native_rpc_return_present, rpc_exception; uint32_t status_present, ul_size, c_entries, p_entries_present; } RmmClockTrace; _Static_assert(sizeof(RmmClockTrace) == 32, "same diagnostic trace ABI"); typedef struct { uint32_t generating_component, status, detection_location, flags; int32_t number_of_parameters; } RmmClockEeRecord; typedef struct { uint32_t capture_attempted, start_status, next_status, end_status, count, limit_reached; RmmClockEeRecord records[4]; } RmmClockEeInfo; _Static_assert(sizeof(RmmClockEeInfo) == 104, "same four-record numeric EEInfo ABI"); typedef struct { uint32_t discovery_invocations, discovery_status, discovery_return_present; uint32_t principal_present, principal_units, principal_validated; uint32_t status_query_invocations, authn_service, authn_level, impersonation_type; uint32_t principal_free_attempted, principal_free_status; uint32_t binding_free_attempted, binding_free_status, eeinfo_origin; } RmmClockAuthTrace; _Static_assert(sizeof(RmmClockAuthTrace) == 60, "same numeric auth trace ABI"); typedef struct { uint32_t query_status, system_user, token_type, thread_query_status, thread_present; uint32_t lookup_status, privileges_status, privilege_present, privilege_attributes; uint32_t privilege_enabled, privilege_default, privilege_removed; } RmmTokenProbe; uint32_t rmm_clock_query_trace_spnego_empty(RmmClockTrace *trace, RmmClockEeInfo *eeinfo, RmmClockAuthTrace *auth); static WCHAR service_name[] = L"RmmClockSpnegoEmptyDiagnostic"; static WCHAR case_id[37]; static SERVICE_STATUS_HANDLE status_handle; static SERVICE_STATUS service_status; static DWORD WINAPI control(DWORD event, DWORD type, void *data, void *context) { (void)event; (void)type; (void)data; (void)context; return ERROR_CALL_NOT_IMPLEMENTED; } static DWORD own_system_token(RmmTokenProbe *probe) { HANDLE token = NULL; HANDLE thread = NULL; DWORD needed = 0, result = ERROR_SUCCESS; _Alignas(8) BYTE storage[4096]; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) { probe->query_status = GetLastError(); return probe->query_status; } if (!GetTokenInformation(token, TokenUser, storage, sizeof(storage), &needed)) result = GetLastError(); else if (needed < sizeof(TOKEN_USER) || needed > sizeof(storage) || !IsWellKnownSid(((TOKEN_USER *)storage)->User.Sid, WinLocalSystemSid)) result = ERROR_ACCESS_DENIED; else probe->system_user = 1; if (!result) { TOKEN_TYPE type = 0; if (!GetTokenInformation(token, TokenType, &type, sizeof(type), &needed)) result = GetLastError(); else { probe->token_type = (uint32_t)type; if (needed != sizeof(type) || type != TokenPrimary) result = ERROR_ACCESS_DENIED; } } if (!result) { if (OpenThreadToken(GetCurrentThread(), TOKEN_QUERY, TRUE, &thread)) { probe->thread_present = 1; CloseHandle(thread); result = ERROR_ACCESS_DENIED; /* never impersonate/revert to continue */ } else { probe->thread_query_status = GetLastError(); if (probe->thread_query_status != ERROR_NO_TOKEN) result = probe->thread_query_status; } } if (!result) { LUID target = {0}; if (!LookupPrivilegeValueW(NULL, SE_SYSTEMTIME_NAME, &target)) { probe->lookup_status = GetLastError();result = probe->lookup_status; } else { needed = 0; if (GetTokenInformation(token, TokenPrivileges, NULL, 0, &needed) || GetLastError() != ERROR_INSUFFICIENT_BUFFER || needed < sizeof(DWORD) || needed > sizeof(storage)) { probe->privileges_status = ERROR_INVALID_DATA;result = ERROR_INVALID_DATA; } else if (!GetTokenInformation(token, TokenPrivileges, storage, sizeof(storage), &needed)) { probe->privileges_status = GetLastError();result = probe->privileges_status; } else { TOKEN_PRIVILEGES *privileges = (TOKEN_PRIVILEGES *)storage; size_t count = privileges->PrivilegeCount; if (count > 256 || needed > sizeof(storage) || offsetof(TOKEN_PRIVILEGES, Privileges) + count * sizeof(LUID_AND_ATTRIBUTES) > needed) { probe->privileges_status = ERROR_INVALID_DATA;result = ERROR_INVALID_DATA; } else for (size_t i = 0; i < count; ++i) { const LUID_AND_ATTRIBUTES *p = &privileges->Privileges[i]; if (p->Luid.LowPart != target.LowPart || p->Luid.HighPart != target.HighPart) continue; if (probe->privilege_present) {result = ERROR_INVALID_DATA;break;} probe->privilege_present = 1; probe->privilege_attributes = p->Attributes; probe->privilege_enabled = !!(p->Attributes & SE_PRIVILEGE_ENABLED); probe->privilege_default = !!(p->Attributes & SE_PRIVILEGE_ENABLED_BY_DEFAULT); probe->privilege_removed = !!(p->Attributes & SE_PRIVILEGE_REMOVED); } } } } probe->query_status = result; CloseHandle(token); return result; } #define TOKEN_FIELDS "\"query_status\":%lu,\"system_user\":%lu,\"token_type\":%lu,\"thread_query_status\":%lu,\"thread_present\":%lu,\"lookup_status\":%lu,\"privileges_status\":%lu,\"privilege_present\":%lu,\"privilege_attributes\":%lu,\"privilege_enabled\":%lu,\"privilege_default\":%lu,\"privilege_removed\":%lu" #define TOKEN_VALUES(p) (unsigned long)(p)->query_status,(unsigned long)(p)->system_user,(unsigned long)(p)->token_type,(unsigned long)(p)->thread_query_status,(unsigned long)(p)->thread_present,(unsigned long)(p)->lookup_status,(unsigned long)(p)->privileges_status,(unsigned long)(p)->privilege_present,(unsigned long)(p)->privilege_attributes,(unsigned long)(p)->privilege_enabled,(unsigned long)(p)->privilege_default,(unsigned long)(p)->privilege_removed static DWORD write_new(const WCHAR *suffix, const char *bytes, DWORD length) { WCHAR path[256];DWORD written = 0, result = ERROR_SUCCESS; int count = swprintf(path, sizeof(path) / sizeof(path[0]), L"C:\\ProgramData\\RustDiscoveryBootstrap\\clock-spnego-empty-trace-%ls\\%ls", case_id, suffix); if (count <= 0 || count >= (int)(sizeof(path) / sizeof(path[0]))) return ERROR_INVALID_PARAMETER; HANDLE file = CreateFileW(path, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL); if (file == INVALID_HANDLE_VALUE) return GetLastError(); if (!WriteFile(file, bytes, length, &written, NULL) || written != length) result = ERROR_WRITE_FAULT; else if (!FlushFileBuffers(file)) result = GetLastError(); CloseHandle(file);return result; } static DWORD write_token(const RmmTokenProbe *token) { char report[1024]; int count = snprintf(report, sizeof(report), "{" TOKEN_FIELDS "}\n", TOKEN_VALUES(token)); if (count <= 0 || count >= (int)sizeof(report)) return ERROR_BUFFER_OVERFLOW; return write_new(L"token.json", report, (DWORD)count); } static DWORD write_result(uint32_t rpc, const RmmClockTrace *trace, const RmmClockEeInfo *eeinfo, const RmmClockAuthTrace *auth, const RmmTokenProbe *token) { char id[37], report[4096]; for (unsigned i = 0; i < 37; ++i) id[i] = (char)case_id[i]; int count = snprintf(report, sizeof(report), "{\"schema_version\":1,\"profile\":\"clock-system-spnego-empty-v1\",\"case_id\":\"%s\"," "\"token_system\":true,\"read_only\":true,\"diagnostic_only\":true,\"query_invocations\":%lu," "\"clock_qualified\":false,\"adapter_qualified\":false,\"product_gate_open\":false,\"rpc_code\":%lu," "\"trace\":{\"stage\":%lu,\"native_rpc_return\":%lu,\"native_rpc_return_present\":%lu," "\"rpc_exception\":%lu,\"status_present\":%lu,\"ul_size\":%lu,\"c_entries\":%lu,\"p_entries_present\":%lu}}\n", id, (unsigned long)auth->status_query_invocations, (unsigned long)rpc, (unsigned long)trace->stage, (unsigned long)trace->native_rpc_return, (unsigned long)trace->native_rpc_return_present, (unsigned long)trace->rpc_exception, (unsigned long)trace->status_present, (unsigned long)trace->ul_size, (unsigned long)trace->c_entries, (unsigned long)trace->p_entries_present); if (count <= 0 || count >= (int)sizeof(report)) return ERROR_BUFFER_OVERFLOW; if (count < 3 || eeinfo->count > 4) return ERROR_INVALID_DATA; /* Replace only the final root-object closure; original trace stays intact. */ size_t used = (size_t)count - 2; int appended = snprintf(report + used, sizeof(report) - used, ",\"eeinfo\":{\"capture_attempted\":%lu,\"start_status\":%lu,\"next_status\":%lu," "\"end_status\":%lu,\"count\":%lu,\"limit_reached\":%lu,\"records\":[", (unsigned long)eeinfo->capture_attempted, (unsigned long)eeinfo->start_status, (unsigned long)eeinfo->next_status, (unsigned long)eeinfo->end_status, (unsigned long)eeinfo->count, (unsigned long)eeinfo->limit_reached); if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW; used += (size_t)appended; for (unsigned i = 0; i < eeinfo->count; ++i) { const RmmClockEeRecord *record = &eeinfo->records[i]; appended = snprintf(report + used, sizeof(report) - used, "%s{\"generating_component\":%lu,\"status\":%lu,\"detection_location\":%lu," "\"flags\":%lu,\"number_of_parameters\":%ld}", i ? "," : "", (unsigned long)record->generating_component, (unsigned long)record->status, (unsigned long)record->detection_location, (unsigned long)record->flags, (long)record->number_of_parameters); if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW; used += (size_t)appended; } appended = snprintf(report + used, sizeof(report) - used, "]},\"token\":{" TOKEN_FIELDS "},\"auth\":{" "\"discovery_invocations\":%lu,\"discovery_status\":%lu,\"discovery_return_present\":%lu," "\"principal_present\":%lu,\"principal_units\":%lu,\"principal_validated\":%lu," "\"status_query_invocations\":%lu,\"authn_service\":%lu,\"authn_level\":%lu,\"impersonation_type\":%lu," "\"principal_free_attempted\":%lu,\"principal_free_status\":%lu,\"binding_free_attempted\":%lu," "\"binding_free_status\":%lu,\"eeinfo_origin\":%lu}}\n", TOKEN_VALUES(token), (unsigned long)auth->discovery_invocations, (unsigned long)auth->discovery_status, (unsigned long)auth->discovery_return_present, (unsigned long)auth->principal_present, (unsigned long)auth->principal_units, (unsigned long)auth->principal_validated, (unsigned long)auth->status_query_invocations, (unsigned long)auth->authn_service, (unsigned long)auth->authn_level, (unsigned long)auth->impersonation_type, (unsigned long)auth->principal_free_attempted, (unsigned long)auth->principal_free_status, (unsigned long)auth->binding_free_attempted, (unsigned long)auth->binding_free_status, (unsigned long)auth->eeinfo_origin); if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW; used += (size_t)appended; count = (int)used; return write_new(L"result.json", report, (DWORD)count); } static void WINAPI run_service(DWORD argc, WCHAR **argv) { (void)argc; (void)argv; status_handle = RegisterServiceCtrlHandlerExW(service_name, control, NULL); if (!status_handle) return; ZeroMemory(&service_status, sizeof(service_status)); service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS; service_status.dwCurrentState = SERVICE_RUNNING; service_status.dwControlsAccepted = 0; if (!SetServiceStatus(status_handle, &service_status)) return; RmmTokenProbe token = {0}; DWORD result = own_system_token(&token); DWORD token_write = write_token(&token); /* durable numeric witness before RPC */ if (!result) result = token_write; if (!result) { RmmClockTrace trace = {0}; RmmClockEeInfo eeinfo = {0}; RmmClockAuthTrace auth = {0}; uint32_t rpc = rmm_clock_query_trace_spnego_empty(&trace, &eeinfo, &auth); result = write_result(rpc, &trace, &eeinfo, &auth, &token); } service_status.dwCurrentState = SERVICE_STOPPED; service_status.dwWin32ExitCode = result; SetServiceStatus(status_handle, &service_status); } int wmain(int argc, WCHAR **argv) { if (argc != 3 || wcscmp(argv[1], L"--service") || wcslen(argv[2]) != 36) return ERROR_INVALID_PARAMETER; for (unsigned i = 0; i < 36; ++i) { WCHAR value = argv[2][i]; if (i == 8 || i == 13 || i == 18 || i == 23) { if (value != L'-') return ERROR_INVALID_PARAMETER; } else if (!((value >= L'0' && value <= L'9') || (value >= L'a' && value <= L'f'))) return ERROR_INVALID_PARAMETER; case_id[i] = value; } case_id[36] = 0; SERVICE_TABLE_ENTRYW table[] = {{service_name, run_service}, {NULL, NULL}}; if (!StartServiceCtrlDispatcherW(table)) return (int)GetLastError(); return 0; }