260 lines
12 KiB
C
260 lines
12 KiB
C
/* Isolated diagnostic variant: discover SPNEGO principal on the fixed local
|
|
* authenticated endpoint, then at most one MS-W32T Opnum6. No fallback/trust.
|
|
* Actual Microsoft-generated MIDL is reused. SEH stays entirely inside C.
|
|
*/
|
|
#define _WIN32_WINNT 0x0a00
|
|
#include <windows.h>
|
|
#include <rpc.h>
|
|
#include <rpcasync.h>
|
|
#include <stdint.h>
|
|
#include <stddef.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");
|
|
/* Diagnostic-only numeric ABI. No strings, names, pointers or remote arrays
|
|
* leave C. A zero native return is meaningful only when return_present is1.
|
|
* Stages:10 compose,20 binding,30 auth,40 call-timeout,50 arena,
|
|
* 60 invoking,61 returned,62 raw numeric status,70 local guard,80 mapped,
|
|
* 90 contained RPC/SEH exception. Never used by the clock trust evaluator.
|
|
*/
|
|
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, "Rust numeric diagnostic ABI");
|
|
|
|
/* Diagnostic source variant only. No strings/parameters leave this function. */
|
|
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(RmmClockEeRecord) == 20, "numeric EEInfo record ABI");
|
|
_Static_assert(sizeof(RmmClockEeInfo) == 104, "bounded four-record EEInfo ABI");
|
|
static void capture_eeinfo(RmmClockEeInfo *out) {
|
|
RPC_ERROR_ENUM_HANDLE enumeration = {0};
|
|
out->capture_attempted = 1;
|
|
out->start_status = RpcErrorStartEnumeration(&enumeration);
|
|
if (out->start_status != RPC_S_OK) return;
|
|
for (unsigned i = 0; i < 4; ++i) {
|
|
RPC_EXTENDED_ERROR_INFO info = {0};
|
|
info.Version = RPC_EEINFO_VERSION;
|
|
info.Flags = 0;
|
|
info.NumberOfParameters = MaxNumberOfEEInfoParams;
|
|
out->next_status = RpcErrorGetNextRecord(&enumeration, FALSE, &info);
|
|
if (out->next_status != RPC_S_OK) break;
|
|
RmmClockEeRecord *record = &out->records[out->count++];
|
|
record->generating_component = info.GeneratingComponent;
|
|
record->status = info.Status;
|
|
record->detection_location = info.DetectionLocation;
|
|
record->flags = info.Flags;
|
|
record->number_of_parameters = info.NumberOfParameters;
|
|
/* ComputerName and Parameters are neither copied nor dereferenced.
|
|
* CopyStrings=FALSE: RPC owns all borrowed fields until EndEnumeration. */
|
|
}
|
|
out->limit_reached = out->count == 4;
|
|
out->end_status = RpcErrorEndEnumeration(&enumeration);
|
|
}
|
|
|
|
/* Numeric protocol-path observations, never a server-identity assertion. */
|
|
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, "numeric SPNEGO path ABI");
|
|
|
|
static uint32_t check_principal(RPC_WSTR value, RmmClockAuthTrace *auth) {
|
|
if (!value) return ERROR_INVALID_DATA;
|
|
auth->principal_present = 1;
|
|
int high = 0;
|
|
for (unsigned i = 0; i <= 512; ++i) {
|
|
unsigned unit = value[i];
|
|
if (!unit) {
|
|
auth->principal_units = i;
|
|
/* Preserve a returned non-NULL empty string; the SSP decides its meaning. */
|
|
if (high) return ERROR_INVALID_NAME;
|
|
auth->principal_validated = 1;
|
|
return ERROR_SUCCESS;
|
|
}
|
|
if (i == 512 || unit < 0x20 || (unit >= 0x7f && unit <= 0x9f)) return ERROR_INVALID_NAME;
|
|
if (high) {
|
|
if (unit < 0xdc00 || unit > 0xdfff) return ERROR_INVALID_NAME;
|
|
high = 0;
|
|
} else if (unit >= 0xd800 && unit <= 0xdbff) high = 1;
|
|
else if (unit >= 0xdc00 && unit <= 0xdfff) return ERROR_INVALID_NAME;
|
|
}
|
|
return ERROR_INVALID_NAME;
|
|
}
|
|
|
|
static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out, RmmClockTrace *trace, RmmClockEeInfo *eeinfo, RmmClockAuthTrace *auth) {
|
|
RMM_W32TIME_STATUS *status = NULL;
|
|
unsigned long result = 0;
|
|
Arena arena = {0};
|
|
if (trace) trace->stage = 50;
|
|
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 {
|
|
if (trace) trace->stage = 60;
|
|
auth->status_query_invocations = 1;
|
|
result = RmmW32TimeQueryStatus(binding, &status);
|
|
if (trace) {
|
|
trace->stage = 61; trace->native_rpc_return_present = 1;
|
|
trace->native_rpc_return = result;
|
|
}
|
|
if (!result) {
|
|
if (trace) {
|
|
trace->stage = 62; trace->status_present = status != NULL;
|
|
if (status) {
|
|
trace->ul_size = status->ulSize; trace->c_entries = status->cEntries;
|
|
trace->p_entries_present = status->pEntries != NULL;
|
|
}
|
|
}
|
|
if (!status || status->ulSize != sizeof(*status) || status->cEntries || status->pEntries) {
|
|
if (trace) trace->stage = 70;
|
|
result = RPC_X_BAD_STUB_DATA;
|
|
}
|
|
else {
|
|
if (trace) trace->stage = 80;
|
|
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();
|
|
if (eeinfo) capture_eeinfo(eeinfo); /* before any further RPC call */
|
|
if (trace) {trace->stage = 90; trace->rpc_exception = result;}
|
|
} RpcEndExcept
|
|
release_allocations(&arena);
|
|
TlsSetValue(arena_index, NULL);
|
|
}
|
|
return result;
|
|
}
|
|
static uint32_t pipe_query(RmmClockStatus *out, RmmClockTrace *trace, RmmClockEeInfo *eeinfo, RmmClockAuthTrace *auth) {
|
|
RPC_WSTR text = NULL;
|
|
RPC_BINDING_HANDLE binding = NULL;
|
|
RPC_WSTR principal = NULL;
|
|
RPC_SECURITY_QOS qos = {0};
|
|
unsigned long result;
|
|
memset(out, 0, sizeof(*out));
|
|
memset(auth, 0, sizeof(*auth));
|
|
auth->authn_service = RPC_C_AUTHN_GSS_NEGOTIATE;
|
|
auth->authn_level = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
|
|
auth->impersonation_type = RPC_C_IMP_LEVEL_IDENTIFY;
|
|
if (trace) trace->stage = 10;
|
|
result = RpcStringBindingComposeW(NULL, (RPC_WSTR)L"ncacn_np", NULL,
|
|
(RPC_WSTR)L"\\pipe\\W32TIME_ALT", NULL, &text);
|
|
if (result) return result;
|
|
if (trace) trace->stage = 20;
|
|
result = RpcBindingFromStringBindingW(text, &binding);
|
|
RpcStringFreeW(&text);
|
|
if (result) return result;
|
|
/* Bound the management discovery as well as Opnum6. RpcMgmt allocates its
|
|
* returned string internally: the 512-unit check bounds inspection/use,
|
|
* not RPC runtime allocations made before the API returns. */
|
|
if (trace) trace->stage = 40;
|
|
result = RpcBindingSetOption(binding, RPC_C_OPT_CALL_TIMEOUT, 1500);
|
|
if (!result) {
|
|
if (trace) trace->stage = 25;
|
|
RpcTryExcept {
|
|
auth->discovery_invocations = 1;
|
|
result = RpcMgmtInqServerPrincNameW(binding, RPC_C_AUTHN_GSS_NEGOTIATE, &principal);
|
|
auth->discovery_return_present = 1;
|
|
auth->discovery_status = result;
|
|
if (result && eeinfo) {auth->eeinfo_origin = 1; capture_eeinfo(eeinfo);}
|
|
} RpcExcept(RpcExceptionFilter(RpcExceptionCode())) {
|
|
result = RpcExceptionCode();
|
|
auth->discovery_status = result;
|
|
if (eeinfo) {auth->eeinfo_origin = 1; capture_eeinfo(eeinfo);}
|
|
if (trace) {trace->stage = 91; trace->rpc_exception = result;}
|
|
} RpcEndExcept
|
|
}
|
|
if (!result) {
|
|
if (trace) trace->stage = 26;
|
|
result = check_principal(principal, auth);
|
|
}
|
|
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;
|
|
if (!result) {
|
|
if (trace) trace->stage = 30;
|
|
/* The returned principal is ONLY a diagnostic authentication input,
|
|
* never an independently trusted service/account/SID assertion. */
|
|
result = RpcBindingSetAuthInfoExW(binding, principal, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
|
|
RPC_C_AUTHN_GSS_NEGOTIATE, NULL, RPC_C_AUTHZ_NONE, &qos);
|
|
if (result && eeinfo) {auth->eeinfo_origin = 3; capture_eeinfo(eeinfo);}
|
|
}
|
|
if (!result) {
|
|
result = invoke_status(binding, out, trace, eeinfo, auth);
|
|
if (eeinfo && eeinfo->capture_attempted) auth->eeinfo_origin = 2;
|
|
}
|
|
/* Preserve the original discovery/auth/query result before cleanup RPC. */
|
|
if (principal) {
|
|
auth->principal_free_attempted = 1;
|
|
auth->principal_free_status = RpcStringFreeW(&principal);
|
|
}
|
|
auth->binding_free_attempted = 1;
|
|
auth->binding_free_status = RpcBindingFree(&binding);
|
|
return result;
|
|
}
|
|
/* At most one authenticated status query after exactly one discovery. */
|
|
uint32_t rmm_clock_query_trace_spnego_empty(RmmClockTrace *trace, RmmClockEeInfo *eeinfo, RmmClockAuthTrace *auth) {
|
|
RmmClockStatus ignored = {0};
|
|
memset(trace, 0, sizeof(*trace));
|
|
memset(eeinfo, 0, sizeof(*eeinfo));
|
|
return pipe_query(&ignored, trace, eeinfo, auth);
|
|
}
|