205 lines
10 KiB
C
205 lines
10 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 <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);
|
|
/* Older MinGW headers stop at V3. This is the documented x64 V5 SDK layout,
|
|
* not a fallback to an older authentication contract. MSVC also checks it
|
|
* directly against its native SDK declaration. Windows x64 is the only target.
|
|
* https://learn.microsoft.com/windows/win32/api/rpcdce/ns-rpcdce-rpc_security_qos_v5_w
|
|
*/
|
|
typedef struct {
|
|
unsigned long Version, Capabilities, IdentityTracking, ImpersonationType;
|
|
unsigned long AdditionalSecurityInfoType;
|
|
union { RPC_HTTP_TRANSPORT_CREDENTIALS_W *HttpCredentials; } u;
|
|
void *Sid;
|
|
unsigned int EffectiveOnly;
|
|
void *ServerSecurityDescriptor;
|
|
} RmmRpcQosV5;
|
|
_Static_assert(sizeof(RmmRpcQosV5) == 56, "x64 RPC QOS V5 ABI");
|
|
_Static_assert(offsetof(RmmRpcQosV5, Sid) == offsetof(RPC_SECURITY_QOS_V3_W, Sid), "SDK QOS prefix ABI");
|
|
_Static_assert(offsetof(RmmRpcQosV5, ServerSecurityDescriptor) == 48, "x64 server descriptor ABI");
|
|
#ifdef _MSC_VER
|
|
_Static_assert(sizeof(RmmRpcQosV5) == sizeof(RPC_SECURITY_QOS_V5_W), "native SDK QOS V5 ABI");
|
|
_Static_assert(offsetof(RmmRpcQosV5, ServerSecurityDescriptor) == offsetof(RPC_SECURITY_QOS_V5_W, ServerSecurityDescriptor), "native SDK descriptor ABI");
|
|
#endif
|
|
|
|
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.
|
|
*/
|
|
static uint32_t alpc_query(RmmClockStatus *out, unsigned control) {
|
|
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];
|
|
_Alignas(8) BYTE service_sid[SECURITY_MAX_SID_SIZE];
|
|
_Alignas(8) BYTE acl_buffer[sizeof(ACL) + sizeof(ACCESS_ALLOWED_ACE) + SECURITY_MAX_SID_SIZE];
|
|
SECURITY_DESCRIPTOR descriptor;
|
|
WCHAR domain[64];
|
|
DWORD domain_size = sizeof(domain) / sizeof(domain[0]);
|
|
DWORD service_sid_size = sizeof(service_sid);
|
|
SID_NAME_USE sid_type;
|
|
DWORD sid_size = sizeof(expected_sid);
|
|
RmmRpcQosV5 qos = {0};
|
|
/* An account-only SID19 binding also accepts other LocalService
|
|
* processes, including our network agent. The client-owned V5
|
|
* descriptor therefore requires the separate W32Time service SID.
|
|
* No descriptor/identity is obtained from endpoint metadata.
|
|
* Controls are fixed read-only diagnostics, never production modes.
|
|
*/
|
|
if (!CreateWellKnownSid(control == 1 ? WinLocalSystemSid : WinLocalServiceSid,
|
|
NULL, expected_sid, &sid_size)) result = GetLastError();
|
|
if (!result && !LookupAccountNameW(NULL,
|
|
control == 2 ? L"NT SERVICE\\RmmDiscoveryWindows" : L"NT SERVICE\\W32Time",
|
|
service_sid, &service_sid_size, domain, &domain_size, &sid_type)) result = GetLastError();
|
|
if (!result && (!IsValidSid(service_sid) || service_sid_size > sizeof(service_sid))) result = ERROR_INVALID_SID;
|
|
if (!result && !InitializeAcl((PACL)acl_buffer, sizeof(acl_buffer), ACL_REVISION)) result = GetLastError();
|
|
if (!result && !AddAccessAllowedAce((PACL)acl_buffer, ACL_REVISION, GENERIC_ALL, service_sid)) result = GetLastError();
|
|
if (!result && !InitializeSecurityDescriptor(&descriptor, SECURITY_DESCRIPTOR_REVISION)) result = GetLastError();
|
|
if (!result && !SetSecurityDescriptorDacl(&descriptor, TRUE, (PACL)acl_buffer, FALSE)) result = GetLastError();
|
|
if (!result) {
|
|
qos.Version = 5; /* RPC_C_SECURITY_QOS_VERSION_5 */
|
|
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;
|
|
qos.EffectiveOnly = TRUE;
|
|
qos.ServerSecurityDescriptor = &descriptor;
|
|
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;
|
|
}
|
|
uint32_t rmm_clock_alpc_query(RmmClockStatus *out) { return alpc_query(out, 0); }
|
|
uint32_t rmm_clock_wrong_account(RmmClockStatus *out) { return alpc_query(out, 1); }
|
|
uint32_t rmm_clock_wrong_service(RmmClockStatus *out) { return alpc_query(out, 2); }
|