Files
rust-discovery/crates/agent-windows/native/clock_rpc.c
T
2026-11-14 02:39:39 +01:00

157 lines
7.3 KiB
C

/* Only the authenticated local MS-W32T status operation is reachable here.
* NDR code is generated by WIDL; SEH must terminate inside C, never in Rust.
*/
#define _WIN32_WINNT 0x0a00
#include <windows.h>
#include <rpc.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "w32time.h"
/* Documented public SDK API; older MinGW headers omit its declaration. */
int RPC_ENTRY RpcExceptionFilter(unsigned long exception_code);
typedef struct Allocation { struct Allocation *next; size_t size; } Allocation;
typedef struct { Allocation *head; size_t total; } Arena;
/* Explicit Windows TLS also works on threads created by SCM. Clang static TLS
* in the mixed Rust/MinGW PE did not initialize such a thread's storage. */
static INIT_ONCE arena_once = INIT_ONCE_STATIC_INIT;
static DWORD arena_index = TLS_OUT_OF_INDEXES;
static BOOL CALLBACK initialize_arena(PINIT_ONCE once, PVOID parameter, PVOID *context) {
(void)once; (void)parameter; (void)context;
arena_index = TlsAlloc();
return arena_index != TLS_OUT_OF_INDEXES;
}
void *__RPC_USER MIDL_user_allocate(size_t size) {
Arena *arena = TlsGetValue(arena_index);
if (!arena || size > 65536 || arena->total > 65536 - size) return NULL;
Allocation *p = calloc(1, sizeof(*p) + size);
if (!p) return NULL;
p->size = size; p->next = arena->head; arena->head = p; arena->total += size;
return p + 1;
}
void __RPC_USER MIDL_user_free(void *memory) {
Arena *arena = TlsGetValue(arena_index);
if (!memory || !arena) return;
Allocation **link = &arena->head;
while (*link && (*link + 1) != memory) link = &(*link)->next;
if (*link) { Allocation *p = *link; *link = p->next; arena->total -= p->size; free(p); }
}
static void release_allocations(Arena *arena) {
while (arena->head) MIDL_user_free(arena->head + 1);
}
/* Stable numeric ABI, no returned peer/source strings or remotely sized data. */
typedef struct {
uint32_t leap, stratum, state, flags, last_result, size;
uint64_t last_sync, sync_age;
int64_t root_delay;
uint64_t root_dispersion;
int64_t phase;
} RmmClockStatus;
_Static_assert(sizeof(RmmClockStatus) == 64, "Rust numeric clock ABI");
_Static_assert(sizeof(RMM_W32TIME_STATUS) == 120, "MS-W32T x64 structure ABI");
static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out) {
RMM_W32TIME_STATUS *status = NULL;
unsigned long result = 0;
Arena arena = {0};
if (!result && !InitOnceExecuteOnce(&arena_once, initialize_arena, NULL, NULL)) result = ERROR_NOT_ENOUGH_MEMORY;
if (!result && TlsGetValue(arena_index)) result = ERROR_BUSY;
if (!result && !TlsSetValue(arena_index, &arena)) result = GetLastError();
if (!result) {
RpcTryExcept {
result = RmmW32TimeQueryStatus(binding, &status);
if (!result) {
if (!status || status->ulSize != sizeof(*status) || status->cEntries || status->pEntries)
result = RPC_X_BAD_STUB_DATA;
else {
out->leap = status->eLeapIndicator; out->stratum = status->nStratum;
out->state = status->ulLcState; out->flags = status->ulTSFlags;
out->last_result = status->eLastSyncResult; out->size = status->ulSize;
out->last_sync = status->qwLastSyncTicks; out->sync_age = status->tpTimeLastGoodSync;
out->root_delay = status->toRootDelay; out->root_dispersion = status->tpRootDispersion;
out->phase = status->toSysPhaseOffset;
}
}
} RpcExcept(RpcExceptionFilter(RpcExceptionCode())) {
result = RpcExceptionCode();
} RpcEndExcept
release_allocations(&arena);
TlsSetValue(arena_index, NULL);
}
return result;
}
uint32_t rmm_clock_query(RmmClockStatus *out) {
RPC_WSTR text = NULL;
RPC_BINDING_HANDLE binding = NULL;
RPC_SECURITY_QOS qos = {0};
unsigned long result;
memset(out, 0, sizeof(*out));
result = RpcStringBindingComposeW(NULL, (RPC_WSTR)L"ncacn_np", NULL,
(RPC_WSTR)L"\\pipe\\W32TIME_ALT", NULL, &text);
if (result) return result;
result = RpcBindingFromStringBindingW(text, &binding);
RpcStringFreeW(&text);
if (result) return result;
qos.Version = RPC_C_SECURITY_QOS_VERSION;
qos.Capabilities = RPC_C_QOS_CAPABILITIES_DEFAULT;
qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
qos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, &qos);
if (!result) result = RpcBindingSetOption(binding, RPC_C_OPT_CALL_TIMEOUT, 1500);
if (!result) result = invoke_status(binding, out);
RpcBindingFree(&binding);
return result;
}
/* Kernel-authenticated ALPC only. No automatic fallback to named pipes.
* The expected SID is fixed locally, never adopted from endpoint metadata.
*/
uint32_t rmm_clock_alpc_query(RmmClockStatus *out) {
RPC_IF_ID id = {0};
RPC_EP_INQ_HANDLE inquiry = NULL;
RPC_STATUS result;
memset(out, 0, sizeof(*out));
result = UuidFromStringW((RPC_WSTR)L"8fb6d884-2388-11d0-8c35-00c04fda2795", &id.Uuid);
if (result) return result;
id.VersMajor = 4; id.VersMinor = 1;
result = RpcMgmtEpEltInqBegin(NULL, RPC_C_EP_MATCH_BY_IF, &id, RPC_C_VERS_EXACT, NULL, &inquiry);
if (result) return result;
for (unsigned i=0; i<16; ++i) {
RPC_IF_ID actual = {0};
RPC_BINDING_HANDLE binding = NULL;
UUID object = {0};
RPC_WSTR annotation = NULL, text = NULL, protocol = NULL;
result = RpcMgmtEpEltInqNextW(inquiry, &actual, &binding, &object, &annotation);
if (annotation) RpcStringFreeW(&annotation);
if (result) break;
result = RpcBindingToStringBindingW(binding, &text);
if (!result) result = RpcStringBindingParseW(text, NULL, &protocol, NULL, NULL, NULL);
int local = !result && !wcscmp((wchar_t *)protocol, L"ncalrpc");
if (protocol) RpcStringFreeW(&protocol);
if (text) RpcStringFreeW(&text);
if (result) {RpcBindingFree(&binding);break;}
if (local) {
_Alignas(8) BYTE expected_sid[SECURITY_MAX_SID_SIZE];
DWORD sid_size = sizeof(expected_sid);
RPC_SECURITY_QOS_V3_W qos = {0};
if (!CreateWellKnownSid(WinLocalServiceSid, NULL, expected_sid, &sid_size)) result = GetLastError();
if (!result) {
qos.Version = RPC_C_SECURITY_QOS_VERSION_3;
qos.Capabilities = RPC_C_QOS_CAPABILITIES_MUTUAL_AUTH;
qos.IdentityTracking = RPC_C_QOS_IDENTITY_STATIC;
qos.ImpersonationType = RPC_C_IMP_LEVEL_IDENTIFY;
qos.Sid = expected_sid;
result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, (RPC_SECURITY_QOS *)&qos);
if (!result) result = invoke_status(binding, out);
}
RpcBindingFree(&binding);
break; /* no retry, alternate identity, endpoint or weaker transport */
}
RpcBindingFree(&binding);
result = EPT_S_NOT_REGISTERED;
}
RpcMgmtEpEltInqDone(&inquiry);
return result == RPC_X_NO_MORE_ENTRIES ? EPT_S_NOT_REGISTERED : result;
}