Qualify enrolled Windows job clocks from the local time service
Rust development slice / linux (push) Failing after 15s
Rust development slice / linux (push) Failing after 15s
This commit is contained in:
@@ -0,0 +1,288 @@
|
||||
/* MS-W32T 2.2.17 / opnum 6 and MS-RPCE connection-oriented packet privacy.
|
||||
* Keep the status RPC on the same local pipe handle whose server PID we verify
|
||||
* against the Service Control Manager, before exchanging any auth tokens.
|
||||
* SSPI performs the NTLM exchange and protects the RPC headers and payloads.
|
||||
*/
|
||||
#define _WIN32_WINNT 0x0a00
|
||||
#define SECURITY_WIN32
|
||||
#include <windows.h>
|
||||
#include <rpc.h>
|
||||
#include <security.h>
|
||||
#include <string.h>
|
||||
#include "clock_status.h"
|
||||
|
||||
enum {
|
||||
RPC_HEADER = 16, CALL_HEADER = 24, SECURITY_TRAILER = 8, SIGNATURE = 16,
|
||||
FIRST_LAST = 3, HEADER_SIGN = 4, FRAME_LIMIT = 4280, TOKEN_LIMIT = 16384,
|
||||
STATUS_LAYOUT = 120, STATUS_FIXED_WIRE = 112, STATUS_SOURCE_PTR = 60,
|
||||
};
|
||||
enum { REQUEST = 0, RESPONSE = 2, BIND = 11, BIND_ACK = 12, AUTH3 = 16 };
|
||||
enum { BIND_CALL_ID = 1, STATUS_CALL_ID = 2, AUTH_CONTEXT_ID = 1, STATUS_OPNUM = 6 };
|
||||
|
||||
static DWORD w32time_pid(void) {
|
||||
SC_HANDLE manager = OpenSCManagerW(NULL, NULL, SC_MANAGER_CONNECT);
|
||||
if (!manager) return 0;
|
||||
SC_HANDLE service = OpenServiceW(manager, L"W32Time", SERVICE_QUERY_STATUS);
|
||||
SERVICE_STATUS_PROCESS status = {0};
|
||||
DWORD needed = 0;
|
||||
BOOL ok = service && QueryServiceStatusEx(service, SC_STATUS_PROCESS_INFO,
|
||||
(BYTE *)&status, sizeof status, &needed);
|
||||
if (service) CloseServiceHandle(service);
|
||||
CloseServiceHandle(manager);
|
||||
return ok && status.dwCurrentState == SERVICE_RUNNING ? status.dwProcessId : 0;
|
||||
}
|
||||
|
||||
static BOOL is_w32time(HANDLE pipe, DWORD expected_pid) {
|
||||
ULONG peer_pid = 0;
|
||||
return expected_pid != 0 && GetNamedPipeServerProcessId(pipe, &peer_pid)
|
||||
&& peer_pid == expected_pid && w32time_pid() == expected_pid;
|
||||
}
|
||||
|
||||
static DWORD pipe_io(HANDLE pipe, BYTE *buffer, DWORD capacity, BOOL writing,
|
||||
DWORD *count, ULONGLONG deadline) {
|
||||
ULONGLONG now = GetTickCount64();
|
||||
if (now >= deadline) return ERROR_TIMEOUT;
|
||||
OVERLAPPED io = {0};
|
||||
io.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
|
||||
if (!io.hEvent) return GetLastError();
|
||||
BOOL ok = writing ? WriteFile(pipe, buffer, capacity, NULL, &io)
|
||||
: ReadFile(pipe, buffer, capacity, NULL, &io);
|
||||
DWORD error = ok ? ERROR_SUCCESS : GetLastError();
|
||||
if (error == ERROR_IO_PENDING) {
|
||||
if (WaitForSingleObject(io.hEvent, (DWORD)(deadline - now)) != WAIT_OBJECT_0) {
|
||||
CancelIoEx(pipe, &io);
|
||||
/* Finish cancellation before releasing the stack buffer. */
|
||||
GetOverlappedResult(pipe, &io, count, TRUE);
|
||||
error = ERROR_TIMEOUT;
|
||||
} else {
|
||||
error = GetOverlappedResult(pipe, &io, count, FALSE)
|
||||
? ERROR_SUCCESS : GetLastError();
|
||||
}
|
||||
} else if (ok) {
|
||||
error = GetOverlappedResult(pipe, &io, count, FALSE)
|
||||
? ERROR_SUCCESS : GetLastError();
|
||||
}
|
||||
CloseHandle(io.hEvent);
|
||||
return error;
|
||||
}
|
||||
|
||||
static void put16(BYTE *p, uint16_t v) { memcpy(p, &v, sizeof v); }
|
||||
static void put32(BYTE *p, uint32_t v) { memcpy(p, &v, sizeof v); }
|
||||
static uint16_t get16(const BYTE *p) { uint16_t v; memcpy(&v, p, sizeof v); return v; }
|
||||
static uint32_t get32(const BYTE *p) { uint32_t v; memcpy(&v, p, sizeof v); return v; }
|
||||
static uint64_t get64(const BYTE *p) { uint64_t v; memcpy(&v, p, sizeof v); return v; }
|
||||
static int64_t get_i64(const BYTE *p) { int64_t v; memcpy(&v, p, sizeof v); return v; }
|
||||
static DWORD align4(DWORD offset) { return (offset + 3) & ~3u; }
|
||||
|
||||
static void write_header(BYTE *p, BYTE type, BYTE flags, WORD length, WORD auth_length, DWORD call) {
|
||||
memset(p, 0, RPC_HEADER);
|
||||
p[0] = 5;
|
||||
p[2] = type;
|
||||
p[3] = flags;
|
||||
p[4] = 0x10; /* little-endian NDR */
|
||||
put16(p + 8, length);
|
||||
put16(p + 10, auth_length);
|
||||
put32(p + 12, call);
|
||||
}
|
||||
|
||||
static void write_security_trailer(BYTE *p) {
|
||||
memset(p, 0, SECURITY_TRAILER);
|
||||
p[0] = RPC_C_AUTHN_WINNT;
|
||||
p[1] = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
|
||||
put32(p + 4, AUTH_CONTEXT_ID);
|
||||
}
|
||||
|
||||
static BOOL valid_security_trailer(const BYTE *p) {
|
||||
return p[0] == RPC_C_AUTHN_WINNT && p[1] == RPC_C_AUTHN_LEVEL_PKT_PRIVACY
|
||||
&& p[3] == 0 && get32(p + 4) == AUTH_CONTEXT_ID;
|
||||
}
|
||||
|
||||
static DWORD send_frame(HANDLE pipe, BYTE *frame, ULONGLONG deadline) {
|
||||
DWORD length = get16(frame + 8);
|
||||
DWORD count = 0;
|
||||
DWORD error = pipe_io(pipe, frame, length, TRUE, &count, deadline);
|
||||
return !error && count != length ? ERROR_INVALID_DATA : error;
|
||||
}
|
||||
|
||||
static DWORD read_frame(HANDLE pipe, BYTE *frame, DWORD *length, BYTE type, DWORD call,
|
||||
ULONGLONG deadline) {
|
||||
DWORD error = pipe_io(pipe, frame, FRAME_LIMIT, FALSE, length, deadline);
|
||||
if (error) return error;
|
||||
if (*length < RPC_HEADER || frame[0] != 5 || frame[1] != 0 || frame[2] != type
|
||||
|| (frame[3] & FIRST_LAST) != FIRST_LAST || get32(frame + 4) != 0x10
|
||||
|| get16(frame + 8) != *length || get32(frame + 12) != call) {
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static DWORD bind_status_rpc(HANDLE pipe, CredHandle *credentials, CtxtHandle *context,
|
||||
ULONGLONG deadline) {
|
||||
static const BYTE interface_v4_1[20] = {0x84,0xd8,0xb6,0x8f,0x88,0x23,0xd0,0x11,
|
||||
0x8c,0x35,0,0xc0,0x4f,0xda,0x27,0x95,4,0,1,0};
|
||||
static const BYTE ndr32_v2[20] = {0x04,0x5d,0x88,0x8a,0xeb,0x1c,0xc9,0x11,
|
||||
0x9f,0xe8,0x08,0,0x2b,0x10,0x48,0x60,2};
|
||||
const ULONG required = ISC_REQ_USE_DCE_STYLE | ISC_REQ_CONFIDENTIALITY
|
||||
| ISC_REQ_INTEGRITY | ISC_REQ_REPLAY_DETECT | ISC_REQ_SEQUENCE_DETECT;
|
||||
ULONG attributes = 0;
|
||||
BYTE out[TOKEN_LIMIT] = {0};
|
||||
BYTE reply[FRAME_LIMIT] = {0};
|
||||
SecBuffer token = {sizeof out - 80, SECBUFFER_TOKEN, out + 80};
|
||||
SecBufferDesc output = {SECBUFFER_VERSION, 1, &token};
|
||||
SECURITY_STATUS status = InitializeSecurityContextW(credentials, NULL, L"", required, 0,
|
||||
SECURITY_NATIVE_DREP, NULL, 0, context, &output, &attributes, NULL);
|
||||
if (status != SEC_I_CONTINUE_NEEDED) return status ? (DWORD)status : ERROR_INVALID_DATA;
|
||||
if (!token.cbBuffer || token.cbBuffer > sizeof out - 80) return ERROR_INVALID_DATA;
|
||||
|
||||
write_header(out, BIND, FIRST_LAST | HEADER_SIGN, (WORD)(80 + token.cbBuffer),
|
||||
(WORD)token.cbBuffer, BIND_CALL_ID);
|
||||
put16(out + 16, FRAME_LIMIT);
|
||||
put16(out + 18, FRAME_LIMIT);
|
||||
out[24] = 1; /* one bind context */
|
||||
out[30] = 1; /* one transfer syntax */
|
||||
memcpy(out + 32, interface_v4_1, sizeof interface_v4_1);
|
||||
memcpy(out + 52, ndr32_v2, sizeof ndr32_v2);
|
||||
write_security_trailer(out + 72);
|
||||
DWORD error = send_frame(pipe, out, deadline);
|
||||
if (error) return error;
|
||||
|
||||
DWORD length = 0;
|
||||
error = read_frame(pipe, reply, &length, BIND_ACK, BIND_CALL_ID, deadline);
|
||||
if (error) return error;
|
||||
DWORD challenge_length = get16(reply + 10);
|
||||
if (reply[3] != (FIRST_LAST | HEADER_SIGN) || length < 56
|
||||
|| !challenge_length || challenge_length > length - 56) return ERROR_INVALID_DATA;
|
||||
DWORD verdict = align4(26 + get16(reply + 24));
|
||||
DWORD trailer = length - SECURITY_TRAILER - challenge_length;
|
||||
if (verdict > trailer || trailer - verdict < 28 || reply[verdict] != 1
|
||||
|| get16(reply + verdict + 4) != 0 || get16(reply + verdict + 6) != 0
|
||||
|| memcmp(reply + verdict + 8, ndr32_v2, sizeof ndr32_v2)
|
||||
|| !valid_security_trailer(reply + trailer)) return ERROR_INVALID_DATA;
|
||||
|
||||
SecBuffer challenge = {challenge_length, SECBUFFER_TOKEN, reply + trailer + SECURITY_TRAILER};
|
||||
SecBufferDesc input = {SECBUFFER_VERSION, 1, &challenge};
|
||||
token.pvBuffer = out + 28;
|
||||
token.cbBuffer = sizeof out - 28;
|
||||
status = InitializeSecurityContextW(credentials, context, L"", required, 0,
|
||||
SECURITY_NATIVE_DREP, &input, 0, context, &output, &attributes, NULL);
|
||||
if (status == SEC_I_COMPLETE_NEEDED) status = CompleteAuthToken(context, &output);
|
||||
if (status) return (DWORD)status;
|
||||
if (!token.cbBuffer || token.cbBuffer > sizeof out - 28
|
||||
|| !(attributes & ISC_RET_CONFIDENTIALITY) || !(attributes & ISC_RET_INTEGRITY)) {
|
||||
return ERROR_INVALID_DATA;
|
||||
}
|
||||
|
||||
write_header(out, AUTH3, FIRST_LAST, (WORD)(28 + token.cbBuffer),
|
||||
(WORD)token.cbBuffer, BIND_CALL_ID);
|
||||
memset(out + RPC_HEADER, 0, 4);
|
||||
write_security_trailer(out + 20);
|
||||
return send_frame(pipe, out, deadline);
|
||||
}
|
||||
|
||||
static DWORD decode_status(const BYTE *body, DWORD length, RmmClockStatus *out) {
|
||||
/* Unique NDR32 status pointer, x64 MS-W32T layout ID, no auxiliary entries. */
|
||||
if (length < STATUS_FIXED_WIRE + 4 || !get32(body) || get32(body + 8) != STATUS_LAYOUT
|
||||
|| get32(body + 104) || get32(body + 108)) return RPC_X_BAD_STUB_DATA;
|
||||
DWORD result_offset = STATUS_FIXED_WIRE;
|
||||
if (get32(body + STATUS_SOURCE_PTR)) {
|
||||
/* Skip the optional conformant UTF-16 string without allocating or logging it. */
|
||||
if (length - result_offset < 16) return RPC_X_BAD_STUB_DATA;
|
||||
DWORD maximum = get32(body + result_offset);
|
||||
DWORD offset = get32(body + result_offset + 4);
|
||||
DWORD count = get32(body + result_offset + 8);
|
||||
if (offset || !count || count > maximum || count > (length - result_offset - 16) / 2)
|
||||
return RPC_X_BAD_STUB_DATA;
|
||||
result_offset += 12 + 2 * count;
|
||||
if (get16(body + result_offset - 2)) return RPC_X_BAD_STUB_DATA;
|
||||
result_offset = align4(result_offset);
|
||||
}
|
||||
if (length - 4 != result_offset) return RPC_X_BAD_STUB_DATA;
|
||||
DWORD result = get32(body + result_offset);
|
||||
if (result) return result;
|
||||
*out = (RmmClockStatus){
|
||||
.leap = get32(body + 12), .stratum = get32(body + 16), .size = STATUS_LAYOUT,
|
||||
.last_sync = get64(body + 32), .root_delay = get_i64(body + 40),
|
||||
.root_dispersion = get64(body + 48), .phase = get_i64(body + 64),
|
||||
.state = get32(body + 72), .flags = get32(body + 76),
|
||||
.last_result = get32(body + 88), .sync_age = get64(body + 96),
|
||||
};
|
||||
return ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static DWORD request_status(HANDLE pipe, CtxtHandle *context, ULONGLONG deadline,
|
||||
DWORD expected_pid, RmmClockStatus *out) {
|
||||
SecPkgContext_Sizes sizes = {0};
|
||||
SECURITY_STATUS security = QueryContextAttributesW(context, SECPKG_ATTR_SIZES, &sizes);
|
||||
if (security) return (DWORD)security;
|
||||
if (sizes.cbSecurityTrailer != SIGNATURE) return ERROR_INVALID_DATA;
|
||||
BYTE frame[CALL_HEADER + SECURITY_TRAILER + SIGNATURE] = {0};
|
||||
write_header(frame, REQUEST, FIRST_LAST, sizeof frame, SIGNATURE, STATUS_CALL_ID);
|
||||
put16(frame + 22, STATUS_OPNUM); /* only the read-only operation has an encoding */
|
||||
write_security_trailer(frame + CALL_HEADER);
|
||||
SecBuffer parts[] = {
|
||||
{CALL_HEADER, SECBUFFER_DATA | SECBUFFER_READONLY_WITH_CHECKSUM, frame},
|
||||
{0, SECBUFFER_DATA, frame + CALL_HEADER},
|
||||
{SECURITY_TRAILER, SECBUFFER_DATA | SECBUFFER_READONLY_WITH_CHECKSUM, frame + CALL_HEADER},
|
||||
{SIGNATURE, SECBUFFER_TOKEN, frame + CALL_HEADER + SECURITY_TRAILER},
|
||||
};
|
||||
SecBufferDesc message = {SECBUFFER_VERSION, 4, parts};
|
||||
security = EncryptMessage(context, 0, &message, 0);
|
||||
if (security) return (DWORD)security;
|
||||
if (parts[3].cbBuffer != SIGNATURE) return ERROR_INVALID_DATA;
|
||||
DWORD error = send_frame(pipe, frame, deadline);
|
||||
if (error) return error;
|
||||
|
||||
BYTE reply[FRAME_LIMIT] = {0};
|
||||
DWORD length = 0;
|
||||
error = read_frame(pipe, reply, &length, RESPONSE, STATUS_CALL_ID, deadline);
|
||||
if (error) return error;
|
||||
if (reply[3] != FIRST_LAST || get16(reply + 10) != SIGNATURE || get16(reply + 20) != 0
|
||||
|| length < sizeof frame) return ERROR_INVALID_DATA;
|
||||
DWORD trailer = length - SECURITY_TRAILER - SIGNATURE;
|
||||
BYTE padding = reply[trailer + 2];
|
||||
if (padding > 15 || padding > trailer - CALL_HEADER
|
||||
|| !valid_security_trailer(reply + trailer)) return ERROR_INVALID_DATA;
|
||||
parts[0].pvBuffer = reply;
|
||||
parts[1].pvBuffer = reply + CALL_HEADER;
|
||||
parts[1].cbBuffer = trailer - CALL_HEADER;
|
||||
parts[2].pvBuffer = reply + trailer;
|
||||
parts[3].pvBuffer = reply + trailer + SECURITY_TRAILER;
|
||||
ULONG qop = 0;
|
||||
security = DecryptMessage(context, &message, 0, &qop);
|
||||
if (security) return (DWORD)security;
|
||||
if (qop != 0) return ERROR_INVALID_DATA;
|
||||
if (!is_w32time(pipe, expected_pid)) return ERROR_ACCESS_DENIED;
|
||||
return decode_status(reply + CALL_HEADER, trailer - CALL_HEADER - padding, out);
|
||||
}
|
||||
|
||||
uint32_t rmm_local_clock_query(RmmClockStatus *out) {
|
||||
memset(out, 0, sizeof *out);
|
||||
DWORD expected_pid = w32time_pid();
|
||||
if (!expected_pid) return ERROR_SERVICE_NOT_ACTIVE;
|
||||
ULONGLONG deadline = GetTickCount64() + 750;
|
||||
HANDLE pipe = CreateFileW(L"\\\\.\\pipe\\W32TIME_ALT", FILE_READ_DATA | FILE_WRITE_DATA
|
||||
| FILE_WRITE_ATTRIBUTES | SYNCHRONIZE, 0, NULL, OPEN_EXISTING,
|
||||
FILE_FLAG_OVERLAPPED | SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION, NULL);
|
||||
if (pipe == INVALID_HANDLE_VALUE) return GetLastError();
|
||||
DWORD mode = PIPE_READMODE_MESSAGE;
|
||||
DWORD error;
|
||||
if (!is_w32time(pipe, expected_pid)) {
|
||||
error = ERROR_ACCESS_DENIED;
|
||||
} else if (!SetNamedPipeHandleState(pipe, &mode, NULL, NULL)) {
|
||||
error = GetLastError();
|
||||
} else {
|
||||
CredHandle credentials;
|
||||
CtxtHandle context;
|
||||
SecInvalidateHandle(&context);
|
||||
error = (DWORD)AcquireCredentialsHandleW(NULL, L"NTLM", SECPKG_CRED_OUTBOUND,
|
||||
NULL, NULL, NULL, NULL, &credentials, NULL);
|
||||
if (!error) {
|
||||
error = bind_status_rpc(pipe, &credentials, &context, deadline);
|
||||
if (!error) error = request_status(pipe, &context, deadline, expected_pid, out);
|
||||
if (SecIsValidHandle(&context)) DeleteSecurityContext(&context);
|
||||
FreeCredentialsHandle(&credentials);
|
||||
}
|
||||
}
|
||||
CloseHandle(pipe);
|
||||
return error;
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "w32time.h"
|
||||
#include "clock_status.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,
|
||||
@@ -61,15 +62,6 @@ void __RPC_USER MIDL_user_free(void *memory) {
|
||||
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.
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef RMM_CLOCK_STATUS_H
|
||||
#define RMM_CLOCK_STATUS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
/* Numeric Windows status only. No remote strings or pointers cross the ABI. */
|
||||
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");
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user