Merge native Windows clock diagnostics with actual denied-query results
Rust development slice / linux (push) Failing after 7s
Rust development slice / linux (push) Failing after 7s
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local-only SYSTEM trace diagnostic build; no VM and no product source edits."""
|
||||
import base64
|
||||
import difflib
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-eeinfo-trace'
|
||||
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
||||
BASELINE = ROOT / 'devstate/windows-clock-midl/diagnostic-builds/181c3a1c-8324-41fd-9b7c-aa8010262912/qualification.json'
|
||||
BASELINE_SHA = '2e0de3acf72ab5c3a8e0baa54e8643d086ea7c03d1b38fd89b6bfc00e45f8cbb'
|
||||
NATIVE = ROOT / 'devstate/windows-clock-midl/native-cases/149bafe2-3469-4580-b133-988812bcc3db'
|
||||
READBACK = NATIVE / 'readback-059c15ef-0f31-41aa-ad1d-ea6b774280d7.json'
|
||||
READBACK_SHA = '0dc2373825f7f8f224cda04ecd5c521dd3c37a6e712bc3ed83bc4fa6d0d89c8c'
|
||||
|
||||
|
||||
def require(value, message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def write(path, raw):
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'wb') as output:
|
||||
output.write(raw); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
def save(path, value):
|
||||
write(path, (json.dumps(value, indent=2) + '\n').encode())
|
||||
|
||||
|
||||
def original_sources():
|
||||
require(sha(BASELINE) == BASELINE_SHA, 'original b1d3 qualification')
|
||||
value = json.loads(BASELINE.read_bytes())
|
||||
require(len(value['source_files']) == 131 and value['artifact']['sha256'] == 'b1d3da4f393ddf20bc8e48a9894ee314666f2ac3613ef97f5c228e3d089ca560', 'unchanged complete original131 source set')
|
||||
for name, digest in value['source_files'].items():
|
||||
path = ROOT / name
|
||||
require(not path.is_symlink() and path.is_file() and sha(path) == digest, 'original131 input changed')
|
||||
return value
|
||||
|
||||
|
||||
def own_sources():
|
||||
return {str((HERE / name).relative_to(ROOT)):sha(HERE / name) for name in ('clock_rpc_eeinfo.c','system_trace_eeinfo.c','build.py','native.py')}
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
baseline = original_sources(); before = own_sources()
|
||||
require(sha(READBACK) == READBACK_SHA, 'actual Microsoft codegen readback')
|
||||
receipt = json.loads(READBACK.read_bytes())
|
||||
require(receipt['readback_only'] is True and receipt['result']['complete'] is True
|
||||
and receipt['result']['run']['exit_code'] == 0 and receipt['result']['no_rpc'] is True,
|
||||
'actual completed MIDL codegen')
|
||||
folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700, parents=True)
|
||||
source = folder / 'source'; source.mkdir(mode=0o700)
|
||||
generated = {}
|
||||
for name, digest in baseline['generated_inputs'].items():
|
||||
path = NATIVE / ('native-' + name); raw = path.read_bytes()
|
||||
item = next(item for item in receipt['files'] if item['name'] == name)
|
||||
require(sha(path) == item['sha256'] == digest and len(raw) == item['bytes']
|
||||
and base64.b64decode(item['base64'], validate=True) == raw, 'same actual unchanged Microsoft output')
|
||||
write(source / name, raw); generated[name] = digest
|
||||
baseline_wrapper = ROOT / 'crates/agent-windows/native/clock_rpc.c'
|
||||
require(sha(baseline_wrapper) == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8','same original C/Auth wrapper')
|
||||
wrapper = HERE / 'clock_rpc_eeinfo.c'
|
||||
write(source / 'clock_rpc.c', wrapper.read_bytes()); write(source / 'system_trace.c', (HERE / 'system_trace_eeinfo.c').read_bytes())
|
||||
delta = ''.join(difflib.unified_diff(baseline_wrapper.read_text().splitlines(True),wrapper.read_text().splitlines(True),fromfile='original-clock_rpc.c',tofile='diagnostic-clock_rpc_eeinfo.c'))
|
||||
write(folder / 'wrapper-source.diff',delta.encode())
|
||||
save(folder / 'intent.json', dict(image=IMAGE, original131=baseline['source_files'], baseline_sha256=BASELINE_SHA,
|
||||
actual_codegen_readback_sha256=READBACK_SHA, generated=generated, own_sources=before,
|
||||
wrapper_sha256=sha(wrapper), original_wrapper_sha256=sha(baseline_wrapper), wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'), own_system_token_only=True, exactly_one_trace_call=True, no_vm=True))
|
||||
output = folder / 'output'; output.mkdir(mode=0o700)
|
||||
checks = {}
|
||||
def run(name, args):
|
||||
command = ['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=128m',
|
||||
'-v',str(source)+':/src:ro','-v',str(output)+':/out','--entrypoint',args[0],IMAGE,*args[1:]]
|
||||
done = subprocess.run(command,capture_output=True,timeout=120)
|
||||
write(folder / (name+'.stdout'),done.stdout); write(folder / (name+'.stderr'),done.stderr)
|
||||
checks[name] = dict(exit_code=done.returncode,command=command,stdout_sha256=sha(folder / (name+'.stdout')),stderr_sha256=sha(folder / (name+'.stderr')))
|
||||
save(folder / (name+'.json'),checks[name]); require(done.returncode == 0,'local diagnostic compile/check failure: '+name)
|
||||
return done.stdout
|
||||
flags = ['--target=x86_64-w64-windows-gnu','-fms-extensions','-Wall','-Wextra','-Werror','-D_M_AMD64=100','-I/src','-O2']
|
||||
run('main',['clang',*flags,'-c','/src/system_trace.c','-o','/out/system_trace.o'])
|
||||
run('wrapper',['clang',*flags,'-c','/src/clock_rpc.c','-o','/out/clock_rpc.o'])
|
||||
run('actual-midl',['clang',*flags,'-Wno-error=missing-braces','-Wno-error=unused-const-variable','-c','/src/w32time_c.c','-o','/out/w32time_c.o'])
|
||||
run('link',['x86_64-w64-mingw32-gcc','-static-libgcc','-municode','/out/system_trace.o','/out/clock_rpc.o','/out/w32time_c.o',
|
||||
'-lrpcrt4','-ladvapi32','-lkernel32','-lmincore','-o','/out/ClockSystemEeInfoTrace.linked.exe'])
|
||||
raw_linked = output / 'ClockSystemEeInfoTrace.linked.exe'
|
||||
linked = output / 'ClockSystemEeInfoTrace.original.exe'; delivery = output / 'ClockSystemEeInfoTrace.exe'
|
||||
# Retain a caller-owned private exact copy; Docker's root-owned raw link is
|
||||
# kept too. No chmod/chown of unrelated objects or source mutation.
|
||||
shutil.copyfile(raw_linked,linked); linked.chmod(0o600)
|
||||
shutil.copyfile(linked,delivery); delivery.chmod(0o600)
|
||||
run('strip',['x86_64-w64-mingw32-strip','--strip-debug','/out/ClockSystemEeInfoTrace.exe'])
|
||||
imports = sorted(set(re.findall(r'DLL Name:\s+(\S+)',run('imports',['x86_64-w64-mingw32-objdump','-p','/out/ClockSystemEeInfoTrace.exe']).decode())))
|
||||
require(imports and {name.lower() for name in imports}.issubset({'kernel32.dll','msvcrt.dll','advapi32.dll','rpcrt4.dll','api-ms-win-core-realtime-l1-1-2.dll'}),'reviewed system DLL imports only')
|
||||
data = delivery.read_bytes(); offset = int.from_bytes(data[60:64],'little')
|
||||
require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86' and len(data) < 1048576,'bounded standalone C x64 PE')
|
||||
spec = importlib.util.spec_from_file_location('system_trace_native',HERE / 'native.py'); native = importlib.util.module_from_spec(spec); spec.loader.exec_module(native)
|
||||
script = folder / 'native.ps1'; write(script,native.SCRIPT.encode())
|
||||
parser = "$e=$null;$t=$null;[System.Management.Automation.Language.Parser]::ParseFile($args[0],[ref]$t,[ref]$e)|Out-Null;if($e.Count){$e|ForEach-Object{$_.Message};exit 1};'AST_PASS'"
|
||||
parser_path = folder / 'parse-native.ps1'; write(parser_path,parser.encode())
|
||||
parsed = subprocess.run(['/tmp/rmm-powershell-parser/pwsh','-NoLogo','-NoProfile','-NonInteractive','-File',str(parser_path),str(script)],capture_output=True,timeout=15)
|
||||
write(folder / 'powershell-parser.stdout',parsed.stdout); write(folder / 'powershell-parser.stderr',parsed.stderr)
|
||||
require(parsed.returncode == 0 and b'AST_PASS' in parsed.stdout and not parsed.stderr,'actual PowerShell AST')
|
||||
command_chars = len(native.encoded_command())
|
||||
require(command_chars < 32767,'Windows encoded command length')
|
||||
require(original_sources()['source_files'] == baseline['source_files'] and own_sources() == before,'all original131 and new sources unchanged')
|
||||
manifest = dict(schema_version=1,profile='clock-system-eeinfo-v1',image=IMAGE,baseline_sha256=BASELINE_SHA,
|
||||
original131=baseline['source_files'],own_sources=before,wrapper_sha256=sha(wrapper),original_wrapper_sha256=sha(baseline_wrapper),wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'),generated=generated,
|
||||
actual_codegen_readback_sha256=READBACK_SHA,checks=checks,script_sha256=hashlib.sha256(native.SCRIPT.encode()).hexdigest(),
|
||||
powershell_ast_passed=True,encoded_command_characters=command_chars,
|
||||
artifact=dict(path=str(delivery),sha256=sha(delivery),bytes=len(data),imports=imports),
|
||||
linked=dict(path=str(linked),sha256=sha(linked)),raw_linked_sha256=sha(raw_linked),native_executed=False,clock_qualified=False,
|
||||
adapter_qualified=False,product_gate_open=False,no_vm=True,no_rmm_jobs=True)
|
||||
save(folder / 'manifest.json',manifest)
|
||||
print(json.dumps(dict(manifest=str(folder / 'manifest.json'),manifest_sha256=sha(folder / 'manifest.json'),artifact=manifest['artifact'],
|
||||
original131_unchanged=True,powershell_ast=True,command_characters=command_chars,native_executed=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_LOCAL_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,291 @@
|
||||
/* 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 <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);
|
||||
/* 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");
|
||||
/* 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);
|
||||
}
|
||||
|
||||
static uint32_t invoke_status(RPC_BINDING_HANDLE binding, RmmClockStatus *out, RmmClockTrace *trace, RmmClockEeInfo *eeinfo) {
|
||||
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;
|
||||
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) {
|
||||
RPC_WSTR text = NULL;
|
||||
RPC_BINDING_HANDLE binding = NULL;
|
||||
RPC_SECURITY_QOS qos = {0};
|
||||
unsigned long result;
|
||||
memset(out, 0, sizeof(*out));
|
||||
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;
|
||||
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 (trace) trace->stage = 30;
|
||||
result = RpcBindingSetAuthInfoExW(binding, NULL, RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
|
||||
RPC_C_AUTHN_WINNT, NULL, RPC_C_AUTHZ_NONE, &qos);
|
||||
if (!result) {
|
||||
if (trace) trace->stage = 40;
|
||||
result = RpcBindingSetOption(binding, RPC_C_OPT_CALL_TIMEOUT, 1500);
|
||||
}
|
||||
if (!result) result = invoke_status(binding, out, trace, eeinfo);
|
||||
RpcBindingFree(&binding);
|
||||
return result;
|
||||
}
|
||||
uint32_t rmm_clock_query(RmmClockStatus *out) { return pipe_query(out, NULL, NULL); }
|
||||
uint32_t rmm_clock_query_trace(RmmClockTrace *trace) {
|
||||
RmmClockStatus ignored = {0};
|
||||
memset(trace, 0, sizeof(*trace));
|
||||
return pipe_query(&ignored, trace, NULL);
|
||||
}
|
||||
/* 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, NULL, NULL);
|
||||
}
|
||||
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); }
|
||||
|
||||
/* Exactly one original authenticated pipe query; EEInfo is observation only. */
|
||||
uint32_t rmm_clock_query_trace_eeinfo(RmmClockTrace *trace, RmmClockEeInfo *eeinfo) {
|
||||
RmmClockStatus ignored = {0};
|
||||
memset(trace, 0, sizeof(*trace));
|
||||
memset(eeinfo, 0, sizeof(*eeinfo));
|
||||
return pipe_query(&ignored, trace, eeinfo);
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Parent-only native SYSTEM trace bootstrap: prepare, once start, read, cleanup.
|
||||
|
||||
Readback is strictly read-only. Cleanup is a separate explicit phase and may
|
||||
remove only this exact stopped diagnostic service. Never changes W32Time/RMM.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-eeinfo-trace'
|
||||
ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
||||
|
||||
|
||||
def require(value,message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def pairs(items):
|
||||
result = {}
|
||||
for name,value in items:
|
||||
require(name not in result,'duplicate JSON key'); result[name] = value
|
||||
return result
|
||||
|
||||
|
||||
def private(path,directory=False):
|
||||
item = Path(path).lstat()
|
||||
require(item.st_uid == os.getuid() and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600)
|
||||
and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1),'private owned path')
|
||||
|
||||
|
||||
def load(path):
|
||||
private(path); require(Path(path).stat().st_size <= 2*1024*1024,'bounded private JSON')
|
||||
return json.loads(Path(path).read_bytes(),object_pairs_hook=pairs)
|
||||
|
||||
|
||||
def save(path,value):
|
||||
private(path.parent,True)
|
||||
fd = os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as output:
|
||||
json.dump(value,output,indent=2); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
SCRIPT = r'''
|
||||
$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
$id=[guid]::ParseExact($p.id,'D').ToString()
|
||||
if($id-cne$p.id-or$p.profile-cne'clock-system-eeinfo-v1'-or$p.action-notin@('prepare','start','readback','cleanup')-or$p.binary_sha256-notmatch'^[a-f0-9]{64}$'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed SYSTEM diagnostic request'}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap';$name='RmmClockSystemEeInfoDiagnostic';$case=Join-Path $root ('clock-eeinfo-trace-'+$id);$exe=Join-Path $case 'ClockSystemEeInfoTrace.exe';$command='"'+$exe+'" --service '+$id
|
||||
function Test-RmmTracePrivate($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private object type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private object owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private ACL'}}}
|
||||
function Write-RmmTraceNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
|
||||
function Get-RmmTraceJson($path){Test-RmmTracePrivate $path $false;if((Get-Item -LiteralPath $path).Length-gt16384){throw 'Small diagnostic receipt'};return(Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)}
|
||||
function Get-RmmTraceService{return(Get-CimInstance Win32_Service -Filter "Name='RmmClockSystemEeInfoDiagnostic'" -OperationTimeoutSec 5)}
|
||||
function Test-RmmTraceService($s){if($s-and($s.PathName-cne$command-or$s.StartName-cne'LocalSystem'-or$s.StartMode-cne'Manual')){throw 'Only own exact temporary service'}}
|
||||
if($p.action-eq'readback'-and!(Test-Path -LiteralPath $case)){@{id=$id;status='remote_case_absent';read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $root $true
|
||||
if($p.action-eq'prepare'){if((Test-Path -LiteralPath $case)-or(Get-RmmTraceService)){throw 'Existing case/service retained'};New-Item -ItemType Directory -Path $case|Out-Null;Test-RmmTracePrivate $case $true;Write-RmmTraceNew (Join-Path $case 'intent.json') $p;@{id=$id;prepared=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $case $true;$old=Get-RmmTraceJson (Join-Path $case 'intent.json')
|
||||
foreach($field in @('id','profile','binary_sha256','manifest_sha256','script_sha256','binary_bytes')){if($old.$field-cne$p.$field){throw 'Original exact diagnostic intent'}}
|
||||
Test-RmmTracePrivate $exe $false;if((Get-Item -LiteralPath $exe).Length-ne$p.binary_bytes-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.binary_sha256){throw 'Exact diagnostic executable'}
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s
|
||||
if($p.action-eq'start'){
|
||||
if($s-or(Test-Path -LiteralPath (Join-Path $case 'start-intent.json'))){throw 'No repeated start; original readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'start-intent.json') @{id=$id;command=$command;binary_sha256=$p.binary_sha256;create_once=$true}
|
||||
New-Service -Name $name -BinaryPathName $command -StartupType Manual|Out-Null
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s){throw 'Own service creation missing'}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-created.json') @{id=$id;command=$command;start_name=$s.StartName}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-start-intent.json') @{start_once=$true}
|
||||
Start-Service -Name $name
|
||||
$deadline=[DateTime]::UtcNow.AddSeconds(20)
|
||||
do{$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s-or$s.State-eq'Stopped'){break};Start-Sleep -Milliseconds 100}while([DateTime]::UtcNow-lt$deadline)
|
||||
}
|
||||
if($p.action-eq'cleanup'){
|
||||
if(!$s){if(!(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'))){throw 'Missing service without bound cleanup'};@{id=$id;cleanup_verified=$true;already_absent=$true;read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
if($s.State-cne'Stopped'){throw 'Cleanup only own bound stopped service'}
|
||||
$created=Get-RmmTraceJson (Join-Path $case 'service-created.json');if($created.id-cne$id-or$created.command-cne$command-or$created.start_name-cne'LocalSystem'){throw 'Original service creation binding'}
|
||||
if(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json')){throw 'Cleanup already attempted; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-intent.json') @{id=$id;command=$command;delete_once=$true}
|
||||
& "$env:WINDIR\System32\sc.exe" delete $name|Out-Null;if($LASTEXITCODE-ne0){throw 'Own stopped service deletion incomplete'}
|
||||
if(Get-RmmTraceService){throw 'Own service still registered; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-completed.json') @{id=$id;service_absent=$true}
|
||||
@{id=$id;cleanup_verified=$true;already_absent=$false;read_only=$false}|ConvertTo-Json -Compress;exit 0
|
||||
}
|
||||
$report=$null;$result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){$report=Get-RmmTraceJson $result}
|
||||
$cleaned=$false;$cleanupReceipt=Join-Path $case 'cleanup-completed.json';if(Test-Path -LiteralPath $cleanupReceipt){$c=Get-RmmTraceJson $cleanupReceipt;if($c.id-cne$id-or$c.service_absent-ne$true){throw 'Bound cleanup receipt'};$cleaned=$true}
|
||||
@{id=$id;service_present=[bool]$s;service_state=$(if($s){$s.State}else{$null});service_exit_code=$(if($s){$s.ExitCode}else{$null});start_intent_present=(Test-Path -LiteralPath (Join-Path $case 'service-start-intent.json'));cleanup_intent_present=(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'));cleanup_completed=$cleaned;report=$report;read_only=($p.action-eq'readback');no_rmm_jobs=$true;no_w32time_change=$true;no_agent_change=$true}|ConvertTo-Json -Depth 12 -Compress
|
||||
'''
|
||||
|
||||
|
||||
def encoded_command():
|
||||
script = 'try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}'
|
||||
return 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(script.encode('utf-16le')).decode()
|
||||
|
||||
|
||||
def manifest(path):
|
||||
path = path.absolute(); private(path.parent,True)
|
||||
require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}',path.parent.name),'fixed local build manifest')
|
||||
value = load(path)
|
||||
require(value['profile'] == 'clock-system-eeinfo-v1' and value['schema_version'] == 1
|
||||
and value['native_executed'] is False and value['clock_qualified'] is False and value['product_gate_open'] is False
|
||||
and value['powershell_ast_passed'] is True and value['encoded_command_characters'] == len(encoded_command()) < 32767
|
||||
and value['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(),'same reviewed nonqualifying diagnostic source')
|
||||
require(value['original_wrapper_sha256'] == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8'
|
||||
and sha(path.parent / 'wrapper-source.diff') == value['wrapper_diff_sha256'],'same exact observational source delta')
|
||||
for name,digest in value['own_sources'].items():require(sha(ROOT / name) == digest,'same original local driver source')
|
||||
for name in ('artifact','linked'):
|
||||
item=value[name];p=Path(item['path']);require(p.parent == path.parent / 'output','fixed retained PE path');private(p);require(sha(p) == item['sha256'],'retained exact diagnostic PE')
|
||||
require(Path(value['artifact']['path']).stat().st_size == value['artifact']['bytes'],'exact PE size')
|
||||
return value
|
||||
|
||||
|
||||
def access(root):
|
||||
require(root == ACCESS_ROOT and root.resolve() == root,'existing Main access root only')
|
||||
sys.path.insert(0,str(ROOT / 'deploy'))
|
||||
import vm_access_check
|
||||
config = vm_access_check.load_config(root / 'credentials/server.conf')
|
||||
require(config['address'] == '88.99.80.202' and config['username'] == 'root' and config.get('port','22') == '22','fixed existing cloud reverse jump')
|
||||
record=load(root / 'credentials/windows-dev-access.json');expiry=dt.datetime.fromisoformat(record['expires_at'])
|
||||
require(expiry.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expiry,'current existing access only')
|
||||
private(root / 'credentials',True);private(root / 'credentials/windows-dev.known_hosts')
|
||||
key=str(Path(config['ssh_key_path']).expanduser())
|
||||
jump=['ssh','-F','/dev/null','-T','-p','22','-i',key,'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','ForwardAgent=no','-o','IdentitiesOnly=yes','-W','127.0.0.1:22240','root@88.99.80.202']
|
||||
prefix=['ssh','-F','/dev/null','-T','-i',key,'-o','ProxyCommand='+shlex.join(jump),'-o','HostKeyAlias=rmm-windows-dev','-o','UserKnownHostsFile='+str(root / 'credentials/windows-dev.known_hosts'),'-o','GlobalKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','rmm-dev@127.0.0.1']
|
||||
require(record['ssh_prefix'] == prefix,'original pinned transport only')
|
||||
return prefix
|
||||
|
||||
|
||||
def run_transport(args,data,folder):
|
||||
try:
|
||||
done=subprocess.run(args,input=data,capture_output=True,timeout=50)
|
||||
save(folder / ('transport-'+str(uuid.uuid4())+'.json'),dict(returncode=done.returncode,stdout_b64=base64.b64encode(done.stdout).decode(),stderr_b64=base64.b64encode(done.stderr).decode()))
|
||||
require(done.returncode == 0,'transport incomplete; original readback only')
|
||||
return done.stdout
|
||||
except subprocess.TimeoutExpired as error:
|
||||
save(folder / ('transport-timeout-'+str(uuid.uuid4())+'.json'),dict(timed_out=True,stdout_b64=base64.b64encode(error.stdout or b'').decode(),stderr_b64=base64.b64encode(error.stderr or b'').decode()))
|
||||
raise RuntimeError('lost reply; no repeated operation') from None
|
||||
|
||||
|
||||
def rpc(prefix,payload,folder):
|
||||
value=json.loads(run_transport(prefix+[encoded_command()],(json.dumps(payload)+'\n').encode(),folder).decode('utf-8-sig'),object_pairs_hook=pairs)
|
||||
require(isinstance(value,dict) and 'bootstrap_error' not in value,'remote diagnostic incomplete')
|
||||
return value
|
||||
|
||||
|
||||
def check_report(value,ident):
|
||||
require(value['id'] == ident and value['no_rmm_jobs'] is True and value['no_w32time_change'] is True and value['no_agent_change'] is True,'exact nonproduct diagnostic case')
|
||||
report=value.get('report')
|
||||
if report is None:return False
|
||||
require(report['schema_version'] == 1 and report['profile'] == 'clock-system-eeinfo-v1' and report['case_id'] == ident
|
||||
and report['token_system'] is True and report['read_only'] is True and report['diagnostic_only'] is True
|
||||
and report['query_invocations'] == 1 and report['clock_qualified'] is False and report['adapter_qualified'] is False
|
||||
and report['product_gate_open'] is False,'actual SYSTEM token and single nonqualifying trace')
|
||||
fields={'stage','native_rpc_return','native_rpc_return_present','rpc_exception','status_present','ul_size','c_entries','p_entries_present'}
|
||||
require(set(report['trace']) == fields and type(report['rpc_code']) is int and 0 <= report['rpc_code'] < 2**32
|
||||
and all(type(n) is int and 0 <= n < 2**32 for n in report['trace'].values()),'bounded numeric trace only')
|
||||
ee=report['eeinfo']
|
||||
require(set(ee) == {'capture_attempted','start_status','next_status','end_status','count','limit_reached','records'}
|
||||
and all(type(ee[k]) is int and 0 <= ee[k] < 2**32 for k in ('capture_attempted','start_status','next_status','end_status','count','limit_reached'))
|
||||
and ee['capture_attempted'] in (0,1) and 0 <= ee['count'] <= 4 and len(ee['records']) == ee['count']
|
||||
and ee['limit_reached'] == int(ee['count'] == 4),'four-record numeric EEInfo budget')
|
||||
record_fields={'generating_component','status','detection_location','flags','number_of_parameters'}
|
||||
for record in ee['records']:
|
||||
require(set(record) == record_fields and all(type(n) is int and 0 <= n < 2**32 for n in record.values())
|
||||
and record['number_of_parameters'] <= 4,'numeric EEInfo fields only; no parameter contents')
|
||||
if report['trace']['stage'] == 90:require(ee['capture_attempted'] == 1 and report['rpc_code'] == report['trace']['rpc_exception'],'original exception code preserved before immediate capture')
|
||||
return (value['service_state'] == 'Stopped' and value['service_exit_code'] == 0) or (value['service_present'] is False and value['cleanup_completed'] is True)
|
||||
|
||||
|
||||
def execute(action,path,root,ident):
|
||||
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True);private(STATE,True)
|
||||
value=manifest(path);prefix=access(root)
|
||||
if action == 'prepare':
|
||||
require(not ident,'new diagnostic case only');ident=str(uuid.uuid4());folder=STATE / ident;folder.mkdir(mode=0o700)
|
||||
payload=dict(id=ident,profile='clock-system-eeinfo-v1',manifest_sha256=sha(path),binary_sha256=value['artifact']['sha256'],binary_bytes=value['artifact']['bytes'],script_sha256=value['script_sha256'])
|
||||
save(folder / 'intent.json',dict(payload=payload,manifest_path=str(path.absolute()),access_root=str(root)))
|
||||
print(json.dumps(dict(case_id=ident,prepare_once=True)),flush=True)
|
||||
prepared=rpc(prefix,dict(payload,action='prepare'),folder);save(folder / 'prepared-remote.json',prepared);require(prepared == dict(id=ident,prepared=True),'new private remote case')
|
||||
save(folder / 'upload-intent.json',dict(binary_sha256=payload['binary_sha256'],upload_once=True))
|
||||
args=['sftp']+[p for p in prefix[1:-1] if p != '-T']+['-b','-',prefix[-1]]
|
||||
destination='/C:/ProgramData/RustDiscoveryBootstrap/clock-eeinfo-trace-'+ident+'/ClockSystemEeInfoTrace.exe'
|
||||
run_transport(args,('put '+json.dumps(value['artifact']['path'])+' '+json.dumps(destination)+'\n').encode(),folder)
|
||||
save(folder / 'upload-completed.json',dict(uploaded=True));print(json.dumps(dict(case_id=ident,prepared=True,service_started=False)));return
|
||||
require(str(uuid.UUID(ident)) == ident,'canonical original case')
|
||||
folder=STATE / ident;private(folder,True);original=load(folder / 'intent.json');payload=original['payload']
|
||||
require(original['manifest_path'] == str(path.absolute()) and original['access_root'] == str(root)
|
||||
and payload['manifest_sha256'] == sha(path) and payload['script_sha256'] == value['script_sha256'],'original same-case input tuple')
|
||||
if action in ('start','cleanup'):
|
||||
if action == 'start':require(load(folder / 'upload-completed.json')['uploaded'] is True,'completed original upload')
|
||||
save(folder / (action+'-intent.json'),dict(action=action,once=True))
|
||||
result=rpc(prefix,dict(payload,action=action),folder);save(folder / ('readback-'+str(uuid.uuid4())+'.json'),result)
|
||||
if action == 'cleanup':
|
||||
require(result.get('id') == ident and result.get('cleanup_verified') is True,'own stopped service cleanup only')
|
||||
print(json.dumps(dict(case_id=ident,cleanup_verified=True)));return
|
||||
if result.get('status'):
|
||||
require(action == 'readback' and result['status'] == 'remote_case_absent','read-only original partial status')
|
||||
print(json.dumps(dict(case_id=ident,complete=False,retry_allowed=False)));return
|
||||
complete=check_report(result,ident)
|
||||
if complete:
|
||||
destination=folder / 'diagnostic-result.json'
|
||||
receipt=dict(complete=True,manifest_sha256=sha(path),actual=result,clock_qualified=False,product_gate_open=False)
|
||||
if destination.exists():require(load(destination)['actual']['report'] == result['report'],'preserve original numeric report')
|
||||
else:save(destination,receipt)
|
||||
print(json.dumps(dict(case_id=ident,diagnostic_complete=complete,rpc_code=result['report']['rpc_code'] if complete else None,
|
||||
stage=result['report']['trace']['stage'] if complete else None,eeinfo_count=result['report']['eeinfo']['count'] if complete else None,clock_qualified=False,cleanup_separate=True)))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','start','readback','cleanup'))
|
||||
parser.add_argument('--manifest',type=Path,required=True);parser.add_argument('--access-root',type=Path,required=True);parser.add_argument('--id')
|
||||
parser.add_argument('--execute-native',action='store_true');args=parser.parse_args()
|
||||
require((args.action == 'readback' and not args.execute_native) or (args.action != 'readback' and args.execute_native),'explicit native effect phase; readback only reads')
|
||||
execute(args.action,args.manifest,args.access_root,args.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_NATIVE_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,135 @@
|
||||
/* Standalone diagnostic only. The original C/Auth/MIDL inputs are linked
|
||||
* unchanged. Exactly one trace query, only under this process's SYSTEM token.
|
||||
* No clock setter, token impersonation, NTP/ACL/provider or product gate. */
|
||||
#define _WIN32_WINNT 0x0a00
|
||||
#include <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
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");
|
||||
uint32_t rmm_clock_query_trace_eeinfo(RmmClockTrace *trace, RmmClockEeInfo *eeinfo);
|
||||
|
||||
static WCHAR service_name[] = L"RmmClockSystemEeInfoDiagnostic";
|
||||
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(void) {
|
||||
HANDLE token = NULL;
|
||||
DWORD needed = 0, result = ERROR_SUCCESS;
|
||||
_Alignas(8) BYTE storage[4096];
|
||||
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) return GetLastError();
|
||||
if (!GetTokenInformation(token, TokenUser, storage, sizeof(storage), &needed)) result = GetLastError();
|
||||
else if (needed > sizeof(storage) || !IsWellKnownSid(((TOKEN_USER *)storage)->User.Sid, WinLocalSystemSid)) result = ERROR_ACCESS_DENIED;
|
||||
CloseHandle(token);
|
||||
return result;
|
||||
}
|
||||
|
||||
static DWORD write_result(uint32_t rpc, const RmmClockTrace *trace, const RmmClockEeInfo *eeinfo) {
|
||||
WCHAR path[256];
|
||||
char id[37], report[4096];
|
||||
DWORD written = 0, result = ERROR_SUCCESS;
|
||||
for (unsigned i = 0; i < 37; ++i) id[i] = (char)case_id[i];
|
||||
int count = swprintf(path, sizeof(path) / sizeof(path[0]),
|
||||
L"C:\\ProgramData\\RustDiscoveryBootstrap\\clock-eeinfo-trace-%ls\\result.json", case_id);
|
||||
if (count <= 0 || count >= (int)(sizeof(path) / sizeof(path[0]))) return ERROR_INVALID_PARAMETER;
|
||||
count = snprintf(report, sizeof(report),
|
||||
"{\"schema_version\":1,\"profile\":\"clock-system-eeinfo-v1\",\"case_id\":\"%s\","
|
||||
"\"token_system\":true,\"read_only\":true,\"diagnostic_only\":true,\"query_invocations\":1,"
|
||||
"\"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)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, "]}}\n");
|
||||
if (appended <= 0 || (size_t)appended >= sizeof(report) - used) return ERROR_BUFFER_OVERFLOW;
|
||||
used += (size_t)appended;
|
||||
count = (int)used;
|
||||
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, report, (DWORD)count, &written, NULL) || written != (DWORD)count) result = ERROR_WRITE_FAULT;
|
||||
else if (!FlushFileBuffers(file)) result = GetLastError();
|
||||
CloseHandle(file);
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
DWORD result = own_system_token();
|
||||
if (!result) {
|
||||
RmmClockTrace trace = {0};
|
||||
RmmClockEeInfo eeinfo = {0};
|
||||
uint32_t rpc = rmm_clock_query_trace_eeinfo(&trace, &eeinfo); /* exactly one fixed call */
|
||||
result = write_result(rpc, &trace, &eeinfo);
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
[workspace]
|
||||
|
||||
[package]
|
||||
name = "rmm-clock-midl-diagnostic"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "AGPL-3.0-only"
|
||||
autobins = false
|
||||
|
||||
[[bin]]
|
||||
name = "clock-midl-readiness"
|
||||
path = "../../../crates/agent-windows/examples/clock_readiness.rs"
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.100"
|
||||
chrono = { version = "0.4.42", features = ["serde"] }
|
||||
serde = { version = "1.0.228", features = ["derive"] }
|
||||
serde_json = "1.0.145"
|
||||
uuid = { version = "1.18.1", features = ["v4", "serde"] }
|
||||
rmm-agent-core = { path = "../../../crates/agent-core" }
|
||||
|
||||
[build-dependencies]
|
||||
cc = "=1.11.1"
|
||||
sha2 = "0.10.9"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-sys = { version = "0.61.2", features = ["Win32_Foundation", "Win32_Security", "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Pipes", "Win32_System_SystemInformation", "Win32_System_Registry", "Win32_System_Memory", "Win32_System_Services", "Win32_System_Threading"] }
|
||||
@@ -0,0 +1,93 @@
|
||||
//! Separate diagnostic only: actual Microsoft outputs from native case 149bafe2.
|
||||
//! No generator execution/fallback, NDR edits, header-version overrides or
|
||||
//! modification of the product crate's build script/output directory.
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::{env, fs, path::Path};
|
||||
|
||||
fn pinned(path: &Path, expected: &str) {
|
||||
let bytes = fs::read(path).expect("missing pinned diagnostic input");
|
||||
assert_eq!(
|
||||
format!("{:x}", Sha256::digest(bytes)),
|
||||
expected,
|
||||
"diagnostic input changed: {}",
|
||||
path.display()
|
||||
);
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let here = env::current_dir().unwrap();
|
||||
let root = here.join("../../..");
|
||||
for (name, digest) in [
|
||||
(
|
||||
"examples/clock_readiness.rs",
|
||||
"77433fafc9891ada53571bc070e33b9c9998379b7dadf48c1e19dc3851e44fe7",
|
||||
),
|
||||
(
|
||||
"src/clock.rs",
|
||||
"08b983095015cc7ba08ce08e9436d1519bf720c5501dd7af8b8b3dbae56b356f",
|
||||
),
|
||||
(
|
||||
"native/clock_rpc.c",
|
||||
"74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8",
|
||||
),
|
||||
(
|
||||
"native/clock_endpoints.c",
|
||||
"85c563bc8824bc30f7e80ef36e3dae5548c1f7a94faf5e882e98f275ab9a55e7",
|
||||
),
|
||||
(
|
||||
"native/w32time.idl",
|
||||
"37da4acb622daf9acf1aa6f1218ff9f52e5a2def14e58d8d9dd8da65078b3c6c",
|
||||
),
|
||||
] {
|
||||
pinned(&root.join("crates/agent-windows").join(name), digest);
|
||||
}
|
||||
let generated = here.join("generated");
|
||||
pinned(
|
||||
&generated.join("w32time.h"),
|
||||
"db5dbe5b335eaa58a6512d4abfda44e7273f3aa8a43077dcb0719e4b97ee2bde",
|
||||
);
|
||||
pinned(
|
||||
&generated.join("w32time_c.c"),
|
||||
"d806adb431f16724c8afdea0c0751588a4bc6f82b6f0b8391d4d189d6a6f4a38",
|
||||
);
|
||||
if env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
|
||||
return;
|
||||
}
|
||||
assert_eq!(env::var("TARGET").as_deref(), Ok("x86_64-pc-windows-gnu"));
|
||||
// Microsoft guards its AMD64 stub using this architecture macro. Clang's
|
||||
// GNU target does not predefine it. This declares the actual selected x64
|
||||
// architecture; no SDK/RPCNDR version or _MSC_VER is fabricated.
|
||||
cc::Build::new()
|
||||
.compiler("clang")
|
||||
.flag("--target=x86_64-w64-windows-gnu")
|
||||
.flag("-fms-extensions")
|
||||
.flag("-Werror")
|
||||
.define("_M_AMD64", Some("100"))
|
||||
.include(&generated)
|
||||
.file(root.join("crates/agent-windows/native/clock_rpc.c"))
|
||||
.file(root.join("crates/agent-windows/native/clock_endpoints.c"))
|
||||
.compile("rmm_clock_rpc");
|
||||
// MIDL's unchanged descriptor initializer uses legal C brace elision for
|
||||
// MinGW's union field and emits two unused const tables. Keep these style
|
||||
// warnings visible only for this pinned generated unit; do not edit NDR
|
||||
// data, suppress type/security errors, or weaken wrapper diagnostics.
|
||||
cc::Build::new()
|
||||
.compiler("clang")
|
||||
.flag("--target=x86_64-w64-windows-gnu")
|
||||
.flag("-fms-extensions")
|
||||
.flag("-Werror")
|
||||
.flag("-Wno-error=missing-braces")
|
||||
.flag("-Wno-error=unused-const-variable")
|
||||
.define("_M_AMD64", Some("100"))
|
||||
.include(&generated)
|
||||
.file(generated.join("w32time_c.c"))
|
||||
.compile("rmm_clock_midl_stub");
|
||||
println!("cargo:rustc-link-lib=rpcrt4");
|
||||
println!("cargo:rustc-link-lib=advapi32");
|
||||
// The existing GNU readiness artifact imports both precise interrupt-time
|
||||
// getters through the Windows realtime API set. This SDK's kernel32 import
|
||||
// archive omits them; its mincore archive supplies those same OS imports.
|
||||
// No clock implementation, API fallback or generated code is substituted.
|
||||
println!("cargo:rustc-link-lib=mincore");
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local-only SPNEGO comparison: retain the returned non-NULL empty principal."""
|
||||
import base64
|
||||
import difflib
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-spnego-empty-trace'
|
||||
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
||||
BASELINE = ROOT / 'devstate/windows-clock-midl/diagnostic-builds/181c3a1c-8324-41fd-9b7c-aa8010262912/qualification.json'
|
||||
BASELINE_SHA = '2e0de3acf72ab5c3a8e0baa54e8643d086ea7c03d1b38fd89b6bfc00e45f8cbb'
|
||||
NATIVE = ROOT / 'devstate/windows-clock-midl/native-cases/149bafe2-3469-4580-b133-988812bcc3db'
|
||||
READBACK = NATIVE / 'readback-059c15ef-0f31-41aa-ad1d-ea6b774280d7.json'
|
||||
READBACK_SHA = '0dc2373825f7f8f224cda04ecd5c521dd3c37a6e712bc3ed83bc4fa6d0d89c8c'
|
||||
PARENT_MANIFEST = ROOT / 'devstate/windows-clock-spnego-trace/build-2ebcfe9c-1e2b-4a50-99c5-3370640aeb77/manifest.json'
|
||||
PARENT_SHA = 'f6b5cd23b1599c2db4fc803fd7232dfd3522dead840e492f659e4efd7a6be001'
|
||||
PARENT_RESULT = ROOT / 'devstate/windows-clock-spnego-trace/0f512772-3599-4c83-b877-41c74fe50fb0/diagnostic-result.json'
|
||||
PARENT_RESULT_SHA = '4c99b34b8c64f877dbbf0fe860d59499f1c054de7d036016e80b84581448cfc9'
|
||||
UNCHANGED_DIAGNOSTICS = {
|
||||
'deploy/windows/clock-system-trace/system_trace.c': '5be92130fda4dbb2a0078861f2b4ebd70bc3b7ecc3a65af0b426702cb69b772a',
|
||||
'deploy/windows/clock-system-trace/build.py': 'e66ef17ba8a442f594114e3fa8eec62c256f0a9b1d9ca0d4e5c821675e00f05f',
|
||||
'deploy/windows/clock-system-trace/native.py': 'fd46fc5167ac85808a4483f76e774e5e05e0d79f4a01def611a0c0b91414714e',
|
||||
'deploy/windows/clock-eeinfo-trace/clock_rpc_eeinfo.c': '2bf009c3118af34ae7945c7d67b4647f0b24a9db34fbeeb4b44299e69f7e9a68',
|
||||
'deploy/windows/clock-eeinfo-trace/system_trace_eeinfo.c': '09628d345592c767ebe136e40108e283e96bc37f749c5db93e62e327cb890301',
|
||||
'deploy/windows/clock-eeinfo-trace/build.py': '59a0c3f881b824d921d165ace4da1e5992b85ddbf9d1d5132404b588dd50f36c',
|
||||
'deploy/windows/clock-eeinfo-trace/native.py': 'cc71c230d4e6e30831eefd20de3d80848732e74d0ce077d79db29c3a1db33e8f',
|
||||
'deploy/windows/clock-spnego-trace/clock_rpc_spnego.c': '10882bb39d7f27cc7e42123f2d2d4b2dbfb7a4cf86ed5bc80767c5fba1a33147',
|
||||
'deploy/windows/clock-spnego-trace/system_trace_spnego.c': '74f96e1e3d55799c3b599abf81ad5e66e436b9fcbd3b7125d87fe102d303812c',
|
||||
'deploy/windows/clock-spnego-trace/build.py': 'b10cdc0ba340d9b209e40a22391606f763aed01669e0356322f817eca6f08e0c',
|
||||
'deploy/windows/clock-spnego-trace/native.py': 'b85773f8c65d99aa59ec7318a2edca38d205ad3da394f645220f63ba1914bfd0',
|
||||
}
|
||||
|
||||
|
||||
def require(value, message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def write(path, raw):
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'wb') as output:
|
||||
output.write(raw); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
def save(path, value):
|
||||
write(path, (json.dumps(value, indent=2) + '\n').encode())
|
||||
|
||||
|
||||
def original_sources():
|
||||
require(sha(PARENT_MANIFEST) == PARENT_SHA and sha(PARENT_RESULT) == PARENT_RESULT_SHA, 'original F6 and empty-principal case retained')
|
||||
require(sha(BASELINE) == BASELINE_SHA, 'original b1d3 qualification')
|
||||
value = json.loads(BASELINE.read_bytes())
|
||||
require(len(value['source_files']) == 131 and value['artifact']['sha256'] == 'b1d3da4f393ddf20bc8e48a9894ee314666f2ac3613ef97f5c228e3d089ca560', 'unchanged complete original131 source set')
|
||||
for name, digest in value['source_files'].items():
|
||||
path = ROOT / name
|
||||
require(not path.is_symlink() and path.is_file() and sha(path) == digest, 'original131 input changed')
|
||||
require(all(sha(ROOT/name)==digest for name,digest in UNCHANGED_DIAGNOSTICS.items()),'existing SYSTEM/WINNT/EEInfo diagnostic sources unchanged')
|
||||
return value
|
||||
|
||||
|
||||
def own_sources():
|
||||
return {str((HERE / name).relative_to(ROOT)):sha(HERE / name) for name in ('clock_rpc_spnego_empty.c','system_trace_spnego_empty.c','build.py','native.py')}
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
baseline = original_sources(); before = own_sources()
|
||||
require(sha(READBACK) == READBACK_SHA, 'actual Microsoft codegen readback')
|
||||
receipt = json.loads(READBACK.read_bytes())
|
||||
require(receipt['readback_only'] is True and receipt['result']['complete'] is True
|
||||
and receipt['result']['run']['exit_code'] == 0 and receipt['result']['no_rpc'] is True,
|
||||
'actual completed MIDL codegen')
|
||||
folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700, parents=True)
|
||||
source = folder / 'source'; source.mkdir(mode=0o700)
|
||||
generated = {}
|
||||
for name, digest in baseline['generated_inputs'].items():
|
||||
path = NATIVE / ('native-' + name); raw = path.read_bytes()
|
||||
item = next(item for item in receipt['files'] if item['name'] == name)
|
||||
require(sha(path) == item['sha256'] == digest and len(raw) == item['bytes']
|
||||
and base64.b64decode(item['base64'], validate=True) == raw, 'same actual unchanged Microsoft output')
|
||||
write(source / name, raw); generated[name] = digest
|
||||
baseline_wrapper = ROOT / 'crates/agent-windows/native/clock_rpc.c'
|
||||
require(sha(baseline_wrapper) == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8','same original C/Auth wrapper')
|
||||
wrapper = HERE / 'clock_rpc_spnego_empty.c'
|
||||
write(source / 'clock_rpc.c', wrapper.read_bytes()); write(source / 'system_trace.c', (HERE / 'system_trace_spnego_empty.c').read_bytes())
|
||||
delta = ''.join(difflib.unified_diff(baseline_wrapper.read_text().splitlines(True),wrapper.read_text().splitlines(True),fromfile='original-clock_rpc.c',tofile='diagnostic-clock_rpc_spnego_empty.c'))
|
||||
write(folder / 'wrapper-source.diff',delta.encode())
|
||||
parent_wrapper = ROOT / 'deploy/windows/clock-spnego-trace/clock_rpc_spnego.c'
|
||||
parent_delta = ''.join(difflib.unified_diff(parent_wrapper.read_text().splitlines(True), wrapper.read_text().splitlines(True),
|
||||
fromfile='unchanged-parent-spnego.c', tofile='new-spnego-empty.c'))
|
||||
write(folder / 'parent-wrapper-source.diff', parent_delta.encode())
|
||||
save(folder / 'intent.json', dict(image=IMAGE, original131=baseline['source_files'], baseline_sha256=BASELINE_SHA,
|
||||
actual_codegen_readback_sha256=READBACK_SHA, generated=generated, own_sources=before,
|
||||
wrapper_sha256=sha(wrapper), original_wrapper_sha256=sha(baseline_wrapper), wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'), unchanged_diagnostics=UNCHANGED_DIAGNOSTICS,
|
||||
parent_manifest_sha256=PARENT_SHA,parent_result_sha256=PARENT_RESULT_SHA,parent_wrapper_diff_sha256=sha(folder / 'parent-wrapper-source.diff'),
|
||||
own_system_token_only=True,token_query_only=True,max_principal_discoveries=1,max_status_queries=1,principal_identity_proven=False,returned_empty_principal_allowed=True,null_principal_allowed=False,no_vm=True))
|
||||
output = folder / 'output'; output.mkdir(mode=0o700)
|
||||
checks = {}
|
||||
def run(name, args):
|
||||
command = ['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=128m',
|
||||
'-v',str(source)+':/src:ro','-v',str(output)+':/out','--entrypoint',args[0],IMAGE,*args[1:]]
|
||||
done = subprocess.run(command,capture_output=True,timeout=120)
|
||||
write(folder / (name+'.stdout'),done.stdout); write(folder / (name+'.stderr'),done.stderr)
|
||||
checks[name] = dict(exit_code=done.returncode,command=command,stdout_sha256=sha(folder / (name+'.stdout')),stderr_sha256=sha(folder / (name+'.stderr')))
|
||||
save(folder / (name+'.json'),checks[name]); require(done.returncode == 0,'local diagnostic compile/check failure: '+name)
|
||||
return done.stdout
|
||||
flags = ['--target=x86_64-w64-windows-gnu','-fms-extensions','-Wall','-Wextra','-Werror','-D_M_AMD64=100','-I/src','-O2']
|
||||
run('main',['clang',*flags,'-c','/src/system_trace.c','-o','/out/system_trace.o'])
|
||||
run('wrapper',['clang',*flags,'-c','/src/clock_rpc.c','-o','/out/clock_rpc.o'])
|
||||
run('actual-midl',['clang',*flags,'-Wno-error=missing-braces','-Wno-error=unused-const-variable','-c','/src/w32time_c.c','-o','/out/w32time_c.o'])
|
||||
run('link',['x86_64-w64-mingw32-gcc','-static-libgcc','-municode','/out/system_trace.o','/out/clock_rpc.o','/out/w32time_c.o',
|
||||
'-lrpcrt4','-ladvapi32','-lkernel32','-lmincore','-o','/out/ClockSpnegoEmptyTrace.linked.exe'])
|
||||
raw_linked = output / 'ClockSpnegoEmptyTrace.linked.exe'
|
||||
linked = output / 'ClockSpnegoEmptyTrace.original.exe'; delivery = output / 'ClockSpnegoEmptyTrace.exe'
|
||||
# Retain a caller-owned private exact copy; Docker's root-owned raw link is
|
||||
# kept too. No chmod/chown of unrelated objects or source mutation.
|
||||
shutil.copyfile(raw_linked,linked); linked.chmod(0o600)
|
||||
shutil.copyfile(linked,delivery); delivery.chmod(0o600)
|
||||
run('strip',['x86_64-w64-mingw32-strip','--strip-debug','/out/ClockSpnegoEmptyTrace.exe'])
|
||||
imports = sorted(set(re.findall(r'DLL Name:\s+(\S+)',run('imports',['x86_64-w64-mingw32-objdump','-p','/out/ClockSpnegoEmptyTrace.exe']).decode())))
|
||||
require(imports and {name.lower() for name in imports}.issubset({'kernel32.dll','msvcrt.dll','advapi32.dll','rpcrt4.dll','api-ms-win-core-realtime-l1-1-2.dll'}),'reviewed system DLL imports only')
|
||||
require(imports == json.loads(PARENT_MANIFEST.read_bytes())['artifact']['imports'], 'same DLL imports as exact F6 comparison')
|
||||
data = delivery.read_bytes(); offset = int.from_bytes(data[60:64],'little')
|
||||
require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86' and len(data) < 1048576,'bounded standalone C x64 PE')
|
||||
spec = importlib.util.spec_from_file_location('system_trace_native',HERE / 'native.py'); native = importlib.util.module_from_spec(spec); spec.loader.exec_module(native)
|
||||
script = folder / 'native.ps1'; write(script,native.SCRIPT.encode())
|
||||
parser = "$e=$null;$t=$null;[System.Management.Automation.Language.Parser]::ParseFile($args[0],[ref]$t,[ref]$e)|Out-Null;if($e.Count){$e|ForEach-Object{$_.Message};exit 1};'AST_PASS'"
|
||||
parser_path = folder / 'parse-native.ps1'; write(parser_path,parser.encode())
|
||||
parsed = subprocess.run(['/tmp/rmm-powershell-parser/pwsh','-NoLogo','-NoProfile','-NonInteractive','-File',str(parser_path),str(script)],capture_output=True,timeout=15)
|
||||
write(folder / 'powershell-parser.stdout',parsed.stdout); write(folder / 'powershell-parser.stderr',parsed.stderr)
|
||||
require(parsed.returncode == 0 and b'AST_PASS' in parsed.stdout and not parsed.stderr,'actual PowerShell AST')
|
||||
command_chars = len(native.encoded_command())
|
||||
require(command_chars < 32767,'Windows encoded command length')
|
||||
require(original_sources()['source_files'] == baseline['source_files'] and own_sources() == before,'all original131 and new sources unchanged')
|
||||
manifest = dict(schema_version=1,profile='clock-system-spnego-empty-v1',image=IMAGE,baseline_sha256=BASELINE_SHA,
|
||||
original131=baseline['source_files'],own_sources=before,wrapper_sha256=sha(wrapper),original_wrapper_sha256=sha(baseline_wrapper),wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'),generated=generated,
|
||||
actual_codegen_readback_sha256=READBACK_SHA,unchanged_diagnostics=UNCHANGED_DIAGNOSTICS,token_query_only=True,max_principal_discoveries=1,max_status_queries=1,principal_identity_proven=False,
|
||||
parent_manifest_sha256=PARENT_SHA,parent_result_sha256=PARENT_RESULT_SHA,parent_wrapper_diff_sha256=sha(folder / 'parent-wrapper-source.diff'),returned_empty_principal_allowed=True,null_principal_allowed=False,
|
||||
checks=checks,script_sha256=hashlib.sha256(native.SCRIPT.encode()).hexdigest(),
|
||||
powershell_ast_passed=True,encoded_command_characters=command_chars,
|
||||
artifact=dict(path=str(delivery),sha256=sha(delivery),bytes=len(data),imports=imports),
|
||||
linked=dict(path=str(linked),sha256=sha(linked)),raw_linked_sha256=sha(raw_linked),native_executed=False,clock_qualified=False,
|
||||
adapter_qualified=False,product_gate_open=False,no_vm=True,no_rmm_jobs=True)
|
||||
save(folder / 'manifest.json',manifest)
|
||||
print(json.dumps(dict(manifest=str(folder / 'manifest.json'),manifest_sha256=sha(folder / 'manifest.json'),artifact=manifest['artifact'],
|
||||
original131_unchanged=True,powershell_ast=True,command_characters=command_chars,native_executed=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_LOCAL_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,259 @@
|
||||
/* 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);
|
||||
}
|
||||
@@ -0,0 +1,272 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Parent-only native SYSTEM trace bootstrap: prepare, once start, read, cleanup.
|
||||
|
||||
Readback is strictly read-only. Cleanup is a separate explicit phase and may
|
||||
remove only this exact stopped diagnostic service. Never changes W32Time/RMM.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-spnego-empty-trace'
|
||||
ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
||||
|
||||
|
||||
def require(value,message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def pairs(items):
|
||||
result = {}
|
||||
for name,value in items:
|
||||
require(name not in result,'duplicate JSON key'); result[name] = value
|
||||
return result
|
||||
|
||||
|
||||
def private(path,directory=False):
|
||||
item = Path(path).lstat()
|
||||
require(item.st_uid == os.getuid() and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600)
|
||||
and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1),'private owned path')
|
||||
|
||||
|
||||
def load(path):
|
||||
private(path); require(Path(path).stat().st_size <= 2*1024*1024,'bounded private JSON')
|
||||
return json.loads(Path(path).read_bytes(),object_pairs_hook=pairs)
|
||||
|
||||
|
||||
def save(path,value):
|
||||
private(path.parent,True)
|
||||
fd = os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as output:
|
||||
json.dump(value,output,indent=2); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
SCRIPT = r'''
|
||||
$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
$id=[guid]::ParseExact($p.id,'D').ToString()
|
||||
if($id-cne$p.id-or$p.profile-cne'clock-system-spnego-empty-v1'-or$p.action-notin@('prepare','start','readback','cleanup')-or$p.binary_sha256-notmatch'^[a-f0-9]{64}$'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed SYSTEM diagnostic request'}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap';$name='RmmClockSpnegoEmptyDiagnostic';$case=Join-Path $root ('clock-spnego-empty-trace-'+$id);$exe=Join-Path $case 'ClockSpnegoEmptyTrace.exe';$command='"'+$exe+'" --service '+$id
|
||||
function Test-RmmTracePrivate($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private object type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private object owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private ACL'}}}
|
||||
function Write-RmmTraceNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
|
||||
function Get-RmmTraceJson($path){Test-RmmTracePrivate $path $false;if((Get-Item -LiteralPath $path).Length-gt16384){throw 'Small diagnostic receipt'};return(Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)}
|
||||
function Get-RmmTraceService{return(Get-CimInstance Win32_Service -Filter "Name='RmmClockSpnegoEmptyDiagnostic'" -OperationTimeoutSec 5)}
|
||||
function Test-RmmTraceService($s){if($s-and($s.PathName-cne$command-or$s.StartName-cne'LocalSystem'-or$s.StartMode-cne'Manual')){throw 'Only own exact temporary service'}}
|
||||
if($p.action-eq'readback'-and!(Test-Path -LiteralPath $case)){@{id=$id;status='remote_case_absent';read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $root $true
|
||||
if($p.action-eq'prepare'){if((Test-Path -LiteralPath $case)-or(Get-RmmTraceService)){throw 'Existing case/service retained'};New-Item -ItemType Directory -Path $case|Out-Null;Test-RmmTracePrivate $case $true;Write-RmmTraceNew (Join-Path $case 'intent.json') $p;@{id=$id;prepared=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $case $true;$old=Get-RmmTraceJson (Join-Path $case 'intent.json')
|
||||
foreach($field in @('id','profile','binary_sha256','manifest_sha256','script_sha256','binary_bytes')){if($old.$field-cne$p.$field){throw 'Original exact diagnostic intent'}}
|
||||
Test-RmmTracePrivate $exe $false;if((Get-Item -LiteralPath $exe).Length-ne$p.binary_bytes-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.binary_sha256){throw 'Exact diagnostic executable'}
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s
|
||||
if($p.action-eq'start'){
|
||||
if($s-or(Test-Path -LiteralPath (Join-Path $case 'start-intent.json'))){throw 'No repeated start; original readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'start-intent.json') @{id=$id;command=$command;binary_sha256=$p.binary_sha256;create_once=$true}
|
||||
New-Service -Name $name -BinaryPathName $command -StartupType Manual|Out-Null
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s){throw 'Own service creation missing'}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-created.json') @{id=$id;command=$command;start_name=$s.StartName}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-start-intent.json') @{start_once=$true}
|
||||
Start-Service -Name $name
|
||||
$deadline=[DateTime]::UtcNow.AddSeconds(20)
|
||||
do{$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s-or$s.State-eq'Stopped'){break};Start-Sleep -Milliseconds 100}while([DateTime]::UtcNow-lt$deadline)
|
||||
}
|
||||
if($p.action-eq'cleanup'){
|
||||
if(!$s){if(!(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'))){throw 'Missing service without bound cleanup'};@{id=$id;cleanup_verified=$true;already_absent=$true;read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
if($s.State-cne'Stopped'){throw 'Cleanup only own bound stopped service'}
|
||||
$created=Get-RmmTraceJson (Join-Path $case 'service-created.json');if($created.id-cne$id-or$created.command-cne$command-or$created.start_name-cne'LocalSystem'){throw 'Original service creation binding'}
|
||||
if(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json')){throw 'Cleanup already attempted; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-intent.json') @{id=$id;command=$command;delete_once=$true}
|
||||
& "$env:WINDIR\System32\sc.exe" delete $name|Out-Null;if($LASTEXITCODE-ne0){throw 'Own stopped service deletion incomplete'}
|
||||
if(Get-RmmTraceService){throw 'Own service still registered; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-completed.json') @{id=$id;service_absent=$true}
|
||||
@{id=$id;cleanup_verified=$true;already_absent=$false;read_only=$false}|ConvertTo-Json -Compress;exit 0
|
||||
}
|
||||
$report=$null;$result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){$report=Get-RmmTraceJson $result}
|
||||
$token=$null;$tokenFile=Join-Path $case 'token.json';if(Test-Path -LiteralPath $tokenFile){$token=Get-RmmTraceJson $tokenFile}
|
||||
$cleaned=$false;$cleanupReceipt=Join-Path $case 'cleanup-completed.json';if(Test-Path -LiteralPath $cleanupReceipt){$c=Get-RmmTraceJson $cleanupReceipt;if($c.id-cne$id-or$c.service_absent-ne$true){throw 'Bound cleanup receipt'};$cleaned=$true}
|
||||
@{id=$id;service_present=[bool]$s;service_state=$(if($s){$s.State}else{$null});service_exit_code=$(if($s){$s.ExitCode}else{$null});start_intent_present=(Test-Path -LiteralPath (Join-Path $case 'service-start-intent.json'));cleanup_intent_present=(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'));cleanup_completed=$cleaned;report=$report;token_before_rpc=$token;read_only=($p.action-eq'readback');no_rmm_jobs=$true;no_w32time_change=$true;no_agent_change=$true}|ConvertTo-Json -Depth 12 -Compress
|
||||
'''
|
||||
|
||||
|
||||
def encoded_command():
|
||||
script = 'try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}'
|
||||
return 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(script.encode('utf-16le')).decode()
|
||||
|
||||
|
||||
def manifest(path):
|
||||
path = path.absolute(); private(path.parent,True)
|
||||
require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}',path.parent.name),'fixed local build manifest')
|
||||
value = load(path)
|
||||
require(value['profile'] == 'clock-system-spnego-empty-v1' and value['schema_version'] == 1
|
||||
and value['native_executed'] is False and value['clock_qualified'] is False and value['product_gate_open'] is False
|
||||
and value['powershell_ast_passed'] is True and value['encoded_command_characters'] == len(encoded_command()) < 32767
|
||||
and value['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(),'same reviewed nonqualifying diagnostic source')
|
||||
require(value['original_wrapper_sha256'] == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8'
|
||||
and sha(path.parent / 'wrapper-source.diff') == value['wrapper_diff_sha256'],'same exact observational source delta')
|
||||
require(value['token_query_only'] is True and value['max_principal_discoveries']==value['max_status_queries']==1
|
||||
and value['principal_identity_proven'] is False,'no privilege mutation/fallback or principal-as-identity claim')
|
||||
require(value['parent_manifest_sha256']=='f6b5cd23b1599c2db4fc803fd7232dfd3522dead840e492f659e4efd7a6be001'
|
||||
and value['parent_result_sha256']=='4c99b34b8c64f877dbbf0fe860d59499f1c054de7d036016e80b84581448cfc9'
|
||||
and value['returned_empty_principal_allowed'] is True and value['null_principal_allowed'] is False
|
||||
and sha(path.parent/'parent-wrapper-source.diff')==value['parent_wrapper_diff_sha256'],
|
||||
'exact F6 comparison, returned non-NULL string only; no fabricated target or fallback')
|
||||
for name,digest in value['unchanged_diagnostics'].items():require(sha(ROOT/name)==digest,'original SYSTEM/WINNT/EEInfo inputs retained')
|
||||
for name,digest in value['own_sources'].items():require(sha(ROOT / name) == digest,'same original local driver source')
|
||||
for name in ('artifact','linked'):
|
||||
item=value[name];p=Path(item['path']);require(p.parent == path.parent / 'output','fixed retained PE path');private(p);require(sha(p) == item['sha256'],'retained exact diagnostic PE')
|
||||
require(Path(value['artifact']['path']).stat().st_size == value['artifact']['bytes'],'exact PE size')
|
||||
return value
|
||||
|
||||
|
||||
def access(root):
|
||||
require(root == ACCESS_ROOT and root.resolve() == root,'existing Main access root only')
|
||||
sys.path.insert(0,str(ROOT / 'deploy'))
|
||||
import vm_access_check
|
||||
config = vm_access_check.load_config(root / 'credentials/server.conf')
|
||||
require(config['address'] == '88.99.80.202' and config['username'] == 'root' and config.get('port','22') == '22','fixed existing cloud reverse jump')
|
||||
record=load(root / 'credentials/windows-dev-access.json');expiry=dt.datetime.fromisoformat(record['expires_at'])
|
||||
require(expiry.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expiry,'current existing access only')
|
||||
private(root / 'credentials',True);private(root / 'credentials/windows-dev.known_hosts')
|
||||
key=str(Path(config['ssh_key_path']).expanduser())
|
||||
jump=['ssh','-F','/dev/null','-T','-p','22','-i',key,'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','ForwardAgent=no','-o','IdentitiesOnly=yes','-W','127.0.0.1:22240','root@88.99.80.202']
|
||||
prefix=['ssh','-F','/dev/null','-T','-i',key,'-o','ProxyCommand='+shlex.join(jump),'-o','HostKeyAlias=rmm-windows-dev','-o','UserKnownHostsFile='+str(root / 'credentials/windows-dev.known_hosts'),'-o','GlobalKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','rmm-dev@127.0.0.1']
|
||||
require(record['ssh_prefix'] == prefix,'original pinned transport only')
|
||||
return prefix
|
||||
|
||||
|
||||
def run_transport(args,data,folder):
|
||||
try:
|
||||
done=subprocess.run(args,input=data,capture_output=True,timeout=50)
|
||||
save(folder / ('transport-'+str(uuid.uuid4())+'.json'),dict(returncode=done.returncode,stdout_b64=base64.b64encode(done.stdout).decode(),stderr_b64=base64.b64encode(done.stderr).decode()))
|
||||
require(done.returncode == 0,'transport incomplete; original readback only')
|
||||
return done.stdout
|
||||
except subprocess.TimeoutExpired as error:
|
||||
save(folder / ('transport-timeout-'+str(uuid.uuid4())+'.json'),dict(timed_out=True,stdout_b64=base64.b64encode(error.stdout or b'').decode(),stderr_b64=base64.b64encode(error.stderr or b'').decode()))
|
||||
raise RuntimeError('lost reply; no repeated operation') from None
|
||||
|
||||
|
||||
def rpc(prefix,payload,folder):
|
||||
value=json.loads(run_transport(prefix+[encoded_command()],(json.dumps(payload)+'\n').encode(),folder).decode('utf-8-sig'),object_pairs_hook=pairs)
|
||||
require(isinstance(value,dict) and 'bootstrap_error' not in value,'remote diagnostic incomplete')
|
||||
return value
|
||||
|
||||
|
||||
def check_report(value,ident):
|
||||
require(value['id'] == ident and value['no_rmm_jobs'] is True and value['no_w32time_change'] is True and value['no_agent_change'] is True,'exact nonproduct diagnostic case')
|
||||
report=value.get('report')
|
||||
if report is None:return False
|
||||
require(report['schema_version'] == 1 and report['profile'] == 'clock-system-spnego-empty-v1' and report['case_id'] == ident
|
||||
and report['token_system'] is True and report['read_only'] is True and report['diagnostic_only'] is True
|
||||
and type(report['query_invocations']) is int and report['query_invocations'] in (0,1) and report['clock_qualified'] is False and report['adapter_qualified'] is False
|
||||
and report['product_gate_open'] is False,'actual SYSTEM token and single nonqualifying trace')
|
||||
token=report['token']
|
||||
require(set(token)=={'query_status','system_user','token_type','thread_query_status','thread_present','lookup_status','privileges_status','privilege_present','privilege_attributes','privilege_enabled','privilege_default','privilege_removed'}
|
||||
and all(type(v) is int and 0<=v<2**32 for v in token.values())
|
||||
and token==value['token_before_rpc'] and token['query_status']==token['lookup_status']==token['privileges_status']==0
|
||||
and token['system_user']==1 and token['token_type']==1 and token['thread_present']==0 and token['thread_query_status']==1008,
|
||||
'durable own SYSTEM primary token/privileges observed before RPC without impersonation')
|
||||
require(all(token[k] in (0,1) for k in ('privilege_present','privilege_enabled','privilege_default','privilege_removed'))
|
||||
and token['privilege_enabled']==int(bool(token['privilege_attributes']&2))
|
||||
and token['privilege_default']==int(bool(token['privilege_attributes']&1))
|
||||
and token['privilege_removed']==int(bool(token['privilege_attributes']&4))
|
||||
and (token['privilege_present'] or token['privilege_attributes']==0),'present/enabled privilege attributes remain observations only')
|
||||
auth=report['auth']
|
||||
require(set(auth)=={'discovery_invocations','discovery_status','discovery_return_present','principal_present','principal_units','principal_validated','status_query_invocations','authn_service','authn_level','impersonation_type','principal_free_attempted','principal_free_status','binding_free_attempted','binding_free_status','eeinfo_origin'}
|
||||
and all(type(v) is int and 0<=v<2**32 for v in auth.values())
|
||||
and auth['authn_service']==9 and auth['authn_level']==6 and auth['impersonation_type']==2
|
||||
and all(auth[k] in (0,1) for k in ('discovery_invocations','discovery_return_present','principal_present','principal_validated','status_query_invocations','principal_free_attempted','binding_free_attempted'))
|
||||
and auth['status_query_invocations']==report['query_invocations'] and auth['eeinfo_origin'] in (0,1,2,3)
|
||||
and auth['discovery_return_present']<=auth['discovery_invocations']
|
||||
and auth['principal_units']<=512,'bounded one-discovery/at-most-one-status SPNEGO Privacy Identify path')
|
||||
if auth['principal_validated']:
|
||||
require(auth['principal_present']==1 and 0<=auth['principal_units']<=512,'checked non-NULL bounded UTF16 principal, possibly empty')
|
||||
if auth['principal_present']:
|
||||
require(auth['principal_free_attempted']==1,'owned principal cleanup attempted')
|
||||
if auth['status_query_invocations']:
|
||||
require(auth['discovery_invocations']==auth['discovery_return_present']==auth['principal_present']==auth['principal_validated']==1
|
||||
and auth['discovery_status']==0 and 0<=auth['principal_units']<=512,'no status query without successful checked principal discovery')
|
||||
fields={'stage','native_rpc_return','native_rpc_return_present','rpc_exception','status_present','ul_size','c_entries','p_entries_present'}
|
||||
require(set(report['trace']) == fields and type(report['rpc_code']) is int and 0 <= report['rpc_code'] < 2**32
|
||||
and all(type(n) is int and 0 <= n < 2**32 for n in report['trace'].values()),'bounded numeric trace only')
|
||||
ee=report['eeinfo']
|
||||
require(set(ee) == {'capture_attempted','start_status','next_status','end_status','count','limit_reached','records'}
|
||||
and all(type(ee[k]) is int and 0 <= ee[k] < 2**32 for k in ('capture_attempted','start_status','next_status','end_status','count','limit_reached'))
|
||||
and ee['capture_attempted'] in (0,1) and 0 <= ee['count'] <= 4 and len(ee['records']) == ee['count']
|
||||
and ee['limit_reached'] == int(ee['count'] == 4),'four-record numeric EEInfo budget')
|
||||
require(ee['capture_attempted']==int(auth['eeinfo_origin']!=0),'numeric EEInfo origin matches capture')
|
||||
record_fields={'generating_component','status','detection_location','flags','number_of_parameters'}
|
||||
for record in ee['records']:
|
||||
require(set(record) == record_fields and all(type(n) is int and 0 <= n < 2**32 for n in record.values())
|
||||
and record['number_of_parameters'] <= 4,'numeric EEInfo fields only; no parameter contents')
|
||||
if report['trace']['stage'] in (90,91):
|
||||
require(ee['capture_attempted']==1 and report['rpc_code']==report['trace']['rpc_exception']
|
||||
and auth['eeinfo_origin']==(2 if report['trace']['stage']==90 else 1),'original exception code preserved before immediate capture')
|
||||
return (value['service_state'] == 'Stopped' and value['service_exit_code'] == 0) or (value['service_present'] is False and value['cleanup_completed'] is True)
|
||||
|
||||
|
||||
def execute(action,path,root,ident):
|
||||
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True);private(STATE,True)
|
||||
value=manifest(path);prefix=access(root)
|
||||
if action == 'prepare':
|
||||
require(not ident,'new diagnostic case only');ident=str(uuid.uuid4());folder=STATE / ident;folder.mkdir(mode=0o700)
|
||||
payload=dict(id=ident,profile='clock-system-spnego-empty-v1',manifest_sha256=sha(path),binary_sha256=value['artifact']['sha256'],binary_bytes=value['artifact']['bytes'],script_sha256=value['script_sha256'])
|
||||
save(folder / 'intent.json',dict(payload=payload,manifest_path=str(path.absolute()),access_root=str(root)))
|
||||
print(json.dumps(dict(case_id=ident,prepare_once=True)),flush=True)
|
||||
prepared=rpc(prefix,dict(payload,action='prepare'),folder);save(folder / 'prepared-remote.json',prepared);require(prepared == dict(id=ident,prepared=True),'new private remote case')
|
||||
save(folder / 'upload-intent.json',dict(binary_sha256=payload['binary_sha256'],upload_once=True))
|
||||
args=['sftp']+[p for p in prefix[1:-1] if p != '-T']+['-b','-',prefix[-1]]
|
||||
destination='/C:/ProgramData/RustDiscoveryBootstrap/clock-spnego-empty-trace-'+ident+'/ClockSpnegoEmptyTrace.exe'
|
||||
run_transport(args,('put '+json.dumps(value['artifact']['path'])+' '+json.dumps(destination)+'\n').encode(),folder)
|
||||
save(folder / 'upload-completed.json',dict(uploaded=True));print(json.dumps(dict(case_id=ident,prepared=True,service_started=False)));return
|
||||
require(str(uuid.UUID(ident)) == ident,'canonical original case')
|
||||
folder=STATE / ident;private(folder,True);original=load(folder / 'intent.json');payload=original['payload']
|
||||
require(original['manifest_path'] == str(path.absolute()) and original['access_root'] == str(root)
|
||||
and payload['manifest_sha256'] == sha(path) and payload['script_sha256'] == value['script_sha256'],'original same-case input tuple')
|
||||
if action in ('start','cleanup'):
|
||||
if action == 'start':require(load(folder / 'upload-completed.json')['uploaded'] is True,'completed original upload')
|
||||
save(folder / (action+'-intent.json'),dict(action=action,once=True))
|
||||
result=rpc(prefix,dict(payload,action=action),folder);save(folder / ('readback-'+str(uuid.uuid4())+'.json'),result)
|
||||
if action == 'cleanup':
|
||||
require(result.get('id') == ident and result.get('cleanup_verified') is True,'own stopped service cleanup only')
|
||||
print(json.dumps(dict(case_id=ident,cleanup_verified=True)));return
|
||||
if result.get('status'):
|
||||
require(action == 'readback' and result['status'] == 'remote_case_absent','read-only original partial status')
|
||||
print(json.dumps(dict(case_id=ident,complete=False,retry_allowed=False)));return
|
||||
complete=check_report(result,ident)
|
||||
if complete:
|
||||
destination=folder / 'diagnostic-result.json'
|
||||
receipt=dict(complete=True,manifest_sha256=sha(path),actual=result,clock_qualified=False,product_gate_open=False)
|
||||
if destination.exists():require(load(destination)['actual']['report'] == result['report'],'preserve original numeric report')
|
||||
else:save(destination,receipt)
|
||||
print(json.dumps(dict(case_id=ident,diagnostic_complete=complete,rpc_code=result['report']['rpc_code'] if complete else None,
|
||||
stage=result['report']['trace']['stage'] if complete else None,eeinfo_count=result['report']['eeinfo']['count'] if complete else None,clock_qualified=False,cleanup_separate=True)))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','start','readback','cleanup'))
|
||||
parser.add_argument('--manifest',type=Path,required=True);parser.add_argument('--access-root',type=Path,required=True);parser.add_argument('--id')
|
||||
parser.add_argument('--execute-native',action='store_true');args=parser.parse_args()
|
||||
require((args.action == 'readback' and not args.execute_native) or (args.action != 'readback' and args.execute_native),'explicit native effect phase; readback only reads')
|
||||
execute(args.action,args.manifest,args.access_root,args.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_NATIVE_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,233 @@
|
||||
/* 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 <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local-only bounded SYSTEM SPNEGO diagnostic; no VM or product source edits."""
|
||||
import base64
|
||||
import difflib
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-spnego-trace'
|
||||
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
||||
BASELINE = ROOT / 'devstate/windows-clock-midl/diagnostic-builds/181c3a1c-8324-41fd-9b7c-aa8010262912/qualification.json'
|
||||
BASELINE_SHA = '2e0de3acf72ab5c3a8e0baa54e8643d086ea7c03d1b38fd89b6bfc00e45f8cbb'
|
||||
NATIVE = ROOT / 'devstate/windows-clock-midl/native-cases/149bafe2-3469-4580-b133-988812bcc3db'
|
||||
READBACK = NATIVE / 'readback-059c15ef-0f31-41aa-ad1d-ea6b774280d7.json'
|
||||
READBACK_SHA = '0dc2373825f7f8f224cda04ecd5c521dd3c37a6e712bc3ed83bc4fa6d0d89c8c'
|
||||
UNCHANGED_DIAGNOSTICS = {
|
||||
'deploy/windows/clock-system-trace/system_trace.c': '5be92130fda4dbb2a0078861f2b4ebd70bc3b7ecc3a65af0b426702cb69b772a',
|
||||
'deploy/windows/clock-system-trace/build.py': 'e66ef17ba8a442f594114e3fa8eec62c256f0a9b1d9ca0d4e5c821675e00f05f',
|
||||
'deploy/windows/clock-system-trace/native.py': 'fd46fc5167ac85808a4483f76e774e5e05e0d79f4a01def611a0c0b91414714e',
|
||||
'deploy/windows/clock-eeinfo-trace/clock_rpc_eeinfo.c': '2bf009c3118af34ae7945c7d67b4647f0b24a9db34fbeeb4b44299e69f7e9a68',
|
||||
'deploy/windows/clock-eeinfo-trace/system_trace_eeinfo.c': '09628d345592c767ebe136e40108e283e96bc37f749c5db93e62e327cb890301',
|
||||
'deploy/windows/clock-eeinfo-trace/build.py': '59a0c3f881b824d921d165ace4da1e5992b85ddbf9d1d5132404b588dd50f36c',
|
||||
'deploy/windows/clock-eeinfo-trace/native.py': 'cc71c230d4e6e30831eefd20de3d80848732e74d0ce077d79db29c3a1db33e8f',
|
||||
}
|
||||
|
||||
|
||||
def require(value, message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def write(path, raw):
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'wb') as output:
|
||||
output.write(raw); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
def save(path, value):
|
||||
write(path, (json.dumps(value, indent=2) + '\n').encode())
|
||||
|
||||
|
||||
def original_sources():
|
||||
require(sha(BASELINE) == BASELINE_SHA, 'original b1d3 qualification')
|
||||
value = json.loads(BASELINE.read_bytes())
|
||||
require(len(value['source_files']) == 131 and value['artifact']['sha256'] == 'b1d3da4f393ddf20bc8e48a9894ee314666f2ac3613ef97f5c228e3d089ca560', 'unchanged complete original131 source set')
|
||||
for name, digest in value['source_files'].items():
|
||||
path = ROOT / name
|
||||
require(not path.is_symlink() and path.is_file() and sha(path) == digest, 'original131 input changed')
|
||||
require(all(sha(ROOT/name)==digest for name,digest in UNCHANGED_DIAGNOSTICS.items()),'existing SYSTEM/WINNT/EEInfo diagnostic sources unchanged')
|
||||
return value
|
||||
|
||||
|
||||
def own_sources():
|
||||
return {str((HERE / name).relative_to(ROOT)):sha(HERE / name) for name in ('clock_rpc_spnego.c','system_trace_spnego.c','build.py','native.py')}
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
baseline = original_sources(); before = own_sources()
|
||||
require(sha(READBACK) == READBACK_SHA, 'actual Microsoft codegen readback')
|
||||
receipt = json.loads(READBACK.read_bytes())
|
||||
require(receipt['readback_only'] is True and receipt['result']['complete'] is True
|
||||
and receipt['result']['run']['exit_code'] == 0 and receipt['result']['no_rpc'] is True,
|
||||
'actual completed MIDL codegen')
|
||||
folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700, parents=True)
|
||||
source = folder / 'source'; source.mkdir(mode=0o700)
|
||||
generated = {}
|
||||
for name, digest in baseline['generated_inputs'].items():
|
||||
path = NATIVE / ('native-' + name); raw = path.read_bytes()
|
||||
item = next(item for item in receipt['files'] if item['name'] == name)
|
||||
require(sha(path) == item['sha256'] == digest and len(raw) == item['bytes']
|
||||
and base64.b64decode(item['base64'], validate=True) == raw, 'same actual unchanged Microsoft output')
|
||||
write(source / name, raw); generated[name] = digest
|
||||
baseline_wrapper = ROOT / 'crates/agent-windows/native/clock_rpc.c'
|
||||
require(sha(baseline_wrapper) == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8','same original C/Auth wrapper')
|
||||
wrapper = HERE / 'clock_rpc_spnego.c'
|
||||
write(source / 'clock_rpc.c', wrapper.read_bytes()); write(source / 'system_trace.c', (HERE / 'system_trace_spnego.c').read_bytes())
|
||||
delta = ''.join(difflib.unified_diff(baseline_wrapper.read_text().splitlines(True),wrapper.read_text().splitlines(True),fromfile='original-clock_rpc.c',tofile='diagnostic-clock_rpc_spnego.c'))
|
||||
write(folder / 'wrapper-source.diff',delta.encode())
|
||||
save(folder / 'intent.json', dict(image=IMAGE, original131=baseline['source_files'], baseline_sha256=BASELINE_SHA,
|
||||
actual_codegen_readback_sha256=READBACK_SHA, generated=generated, own_sources=before,
|
||||
wrapper_sha256=sha(wrapper), original_wrapper_sha256=sha(baseline_wrapper), wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'), unchanged_diagnostics=UNCHANGED_DIAGNOSTICS,
|
||||
own_system_token_only=True,token_query_only=True,max_principal_discoveries=1,max_status_queries=1,principal_identity_proven=False,no_vm=True))
|
||||
output = folder / 'output'; output.mkdir(mode=0o700)
|
||||
checks = {}
|
||||
def run(name, args):
|
||||
command = ['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=128m',
|
||||
'-v',str(source)+':/src:ro','-v',str(output)+':/out','--entrypoint',args[0],IMAGE,*args[1:]]
|
||||
done = subprocess.run(command,capture_output=True,timeout=120)
|
||||
write(folder / (name+'.stdout'),done.stdout); write(folder / (name+'.stderr'),done.stderr)
|
||||
checks[name] = dict(exit_code=done.returncode,command=command,stdout_sha256=sha(folder / (name+'.stdout')),stderr_sha256=sha(folder / (name+'.stderr')))
|
||||
save(folder / (name+'.json'),checks[name]); require(done.returncode == 0,'local diagnostic compile/check failure: '+name)
|
||||
return done.stdout
|
||||
flags = ['--target=x86_64-w64-windows-gnu','-fms-extensions','-Wall','-Wextra','-Werror','-D_M_AMD64=100','-I/src','-O2']
|
||||
run('main',['clang',*flags,'-c','/src/system_trace.c','-o','/out/system_trace.o'])
|
||||
run('wrapper',['clang',*flags,'-c','/src/clock_rpc.c','-o','/out/clock_rpc.o'])
|
||||
run('actual-midl',['clang',*flags,'-Wno-error=missing-braces','-Wno-error=unused-const-variable','-c','/src/w32time_c.c','-o','/out/w32time_c.o'])
|
||||
run('link',['x86_64-w64-mingw32-gcc','-static-libgcc','-municode','/out/system_trace.o','/out/clock_rpc.o','/out/w32time_c.o',
|
||||
'-lrpcrt4','-ladvapi32','-lkernel32','-lmincore','-o','/out/ClockSystemSpnegoTrace.linked.exe'])
|
||||
raw_linked = output / 'ClockSystemSpnegoTrace.linked.exe'
|
||||
linked = output / 'ClockSystemSpnegoTrace.original.exe'; delivery = output / 'ClockSystemSpnegoTrace.exe'
|
||||
# Retain a caller-owned private exact copy; Docker's root-owned raw link is
|
||||
# kept too. No chmod/chown of unrelated objects or source mutation.
|
||||
shutil.copyfile(raw_linked,linked); linked.chmod(0o600)
|
||||
shutil.copyfile(linked,delivery); delivery.chmod(0o600)
|
||||
run('strip',['x86_64-w64-mingw32-strip','--strip-debug','/out/ClockSystemSpnegoTrace.exe'])
|
||||
imports = sorted(set(re.findall(r'DLL Name:\s+(\S+)',run('imports',['x86_64-w64-mingw32-objdump','-p','/out/ClockSystemSpnegoTrace.exe']).decode())))
|
||||
require(imports and {name.lower() for name in imports}.issubset({'kernel32.dll','msvcrt.dll','advapi32.dll','rpcrt4.dll','api-ms-win-core-realtime-l1-1-2.dll'}),'reviewed system DLL imports only')
|
||||
data = delivery.read_bytes(); offset = int.from_bytes(data[60:64],'little')
|
||||
require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86' and len(data) < 1048576,'bounded standalone C x64 PE')
|
||||
spec = importlib.util.spec_from_file_location('system_trace_native',HERE / 'native.py'); native = importlib.util.module_from_spec(spec); spec.loader.exec_module(native)
|
||||
script = folder / 'native.ps1'; write(script,native.SCRIPT.encode())
|
||||
parser = "$e=$null;$t=$null;[System.Management.Automation.Language.Parser]::ParseFile($args[0],[ref]$t,[ref]$e)|Out-Null;if($e.Count){$e|ForEach-Object{$_.Message};exit 1};'AST_PASS'"
|
||||
parser_path = folder / 'parse-native.ps1'; write(parser_path,parser.encode())
|
||||
parsed = subprocess.run(['/tmp/rmm-powershell-parser/pwsh','-NoLogo','-NoProfile','-NonInteractive','-File',str(parser_path),str(script)],capture_output=True,timeout=15)
|
||||
write(folder / 'powershell-parser.stdout',parsed.stdout); write(folder / 'powershell-parser.stderr',parsed.stderr)
|
||||
require(parsed.returncode == 0 and b'AST_PASS' in parsed.stdout and not parsed.stderr,'actual PowerShell AST')
|
||||
command_chars = len(native.encoded_command())
|
||||
require(command_chars < 32767,'Windows encoded command length')
|
||||
require(original_sources()['source_files'] == baseline['source_files'] and own_sources() == before,'all original131 and new sources unchanged')
|
||||
manifest = dict(schema_version=1,profile='clock-system-spnego-v1',image=IMAGE,baseline_sha256=BASELINE_SHA,
|
||||
original131=baseline['source_files'],own_sources=before,wrapper_sha256=sha(wrapper),original_wrapper_sha256=sha(baseline_wrapper),wrapper_diff_sha256=sha(folder / 'wrapper-source.diff'),generated=generated,
|
||||
actual_codegen_readback_sha256=READBACK_SHA,unchanged_diagnostics=UNCHANGED_DIAGNOSTICS,token_query_only=True,max_principal_discoveries=1,max_status_queries=1,principal_identity_proven=False,
|
||||
checks=checks,script_sha256=hashlib.sha256(native.SCRIPT.encode()).hexdigest(),
|
||||
powershell_ast_passed=True,encoded_command_characters=command_chars,
|
||||
artifact=dict(path=str(delivery),sha256=sha(delivery),bytes=len(data),imports=imports),
|
||||
linked=dict(path=str(linked),sha256=sha(linked)),raw_linked_sha256=sha(raw_linked),native_executed=False,clock_qualified=False,
|
||||
adapter_qualified=False,product_gate_open=False,no_vm=True,no_rmm_jobs=True)
|
||||
save(folder / 'manifest.json',manifest)
|
||||
print(json.dumps(dict(manifest=str(folder / 'manifest.json'),manifest_sha256=sha(folder / 'manifest.json'),artifact=manifest['artifact'],
|
||||
original131_unchanged=True,powershell_ast=True,command_characters=command_chars,native_executed=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_LOCAL_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,258 @@
|
||||
/* 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;
|
||||
if (!i || 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(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);
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Parent-only native SYSTEM trace bootstrap: prepare, once start, read, cleanup.
|
||||
|
||||
Readback is strictly read-only. Cleanup is a separate explicit phase and may
|
||||
remove only this exact stopped diagnostic service. Never changes W32Time/RMM.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-spnego-trace'
|
||||
ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
||||
|
||||
|
||||
def require(value,message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def pairs(items):
|
||||
result = {}
|
||||
for name,value in items:
|
||||
require(name not in result,'duplicate JSON key'); result[name] = value
|
||||
return result
|
||||
|
||||
|
||||
def private(path,directory=False):
|
||||
item = Path(path).lstat()
|
||||
require(item.st_uid == os.getuid() and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600)
|
||||
and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1),'private owned path')
|
||||
|
||||
|
||||
def load(path):
|
||||
private(path); require(Path(path).stat().st_size <= 2*1024*1024,'bounded private JSON')
|
||||
return json.loads(Path(path).read_bytes(),object_pairs_hook=pairs)
|
||||
|
||||
|
||||
def save(path,value):
|
||||
private(path.parent,True)
|
||||
fd = os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as output:
|
||||
json.dump(value,output,indent=2); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
SCRIPT = r'''
|
||||
$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
$id=[guid]::ParseExact($p.id,'D').ToString()
|
||||
if($id-cne$p.id-or$p.profile-cne'clock-system-spnego-v1'-or$p.action-notin@('prepare','start','readback','cleanup')-or$p.binary_sha256-notmatch'^[a-f0-9]{64}$'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed SYSTEM diagnostic request'}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap';$name='RmmClockSystemSpnegoDiagnostic';$case=Join-Path $root ('clock-spnego-trace-'+$id);$exe=Join-Path $case 'ClockSystemSpnegoTrace.exe';$command='"'+$exe+'" --service '+$id
|
||||
function Test-RmmTracePrivate($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private object type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private object owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private ACL'}}}
|
||||
function Write-RmmTraceNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
|
||||
function Get-RmmTraceJson($path){Test-RmmTracePrivate $path $false;if((Get-Item -LiteralPath $path).Length-gt16384){throw 'Small diagnostic receipt'};return(Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)}
|
||||
function Get-RmmTraceService{return(Get-CimInstance Win32_Service -Filter "Name='RmmClockSystemSpnegoDiagnostic'" -OperationTimeoutSec 5)}
|
||||
function Test-RmmTraceService($s){if($s-and($s.PathName-cne$command-or$s.StartName-cne'LocalSystem'-or$s.StartMode-cne'Manual')){throw 'Only own exact temporary service'}}
|
||||
if($p.action-eq'readback'-and!(Test-Path -LiteralPath $case)){@{id=$id;status='remote_case_absent';read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $root $true
|
||||
if($p.action-eq'prepare'){if((Test-Path -LiteralPath $case)-or(Get-RmmTraceService)){throw 'Existing case/service retained'};New-Item -ItemType Directory -Path $case|Out-Null;Test-RmmTracePrivate $case $true;Write-RmmTraceNew (Join-Path $case 'intent.json') $p;@{id=$id;prepared=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $case $true;$old=Get-RmmTraceJson (Join-Path $case 'intent.json')
|
||||
foreach($field in @('id','profile','binary_sha256','manifest_sha256','script_sha256','binary_bytes')){if($old.$field-cne$p.$field){throw 'Original exact diagnostic intent'}}
|
||||
Test-RmmTracePrivate $exe $false;if((Get-Item -LiteralPath $exe).Length-ne$p.binary_bytes-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.binary_sha256){throw 'Exact diagnostic executable'}
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s
|
||||
if($p.action-eq'start'){
|
||||
if($s-or(Test-Path -LiteralPath (Join-Path $case 'start-intent.json'))){throw 'No repeated start; original readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'start-intent.json') @{id=$id;command=$command;binary_sha256=$p.binary_sha256;create_once=$true}
|
||||
New-Service -Name $name -BinaryPathName $command -StartupType Manual|Out-Null
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s){throw 'Own service creation missing'}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-created.json') @{id=$id;command=$command;start_name=$s.StartName}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-start-intent.json') @{start_once=$true}
|
||||
Start-Service -Name $name
|
||||
$deadline=[DateTime]::UtcNow.AddSeconds(20)
|
||||
do{$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s-or$s.State-eq'Stopped'){break};Start-Sleep -Milliseconds 100}while([DateTime]::UtcNow-lt$deadline)
|
||||
}
|
||||
if($p.action-eq'cleanup'){
|
||||
if(!$s){if(!(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'))){throw 'Missing service without bound cleanup'};@{id=$id;cleanup_verified=$true;already_absent=$true;read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
if($s.State-cne'Stopped'){throw 'Cleanup only own bound stopped service'}
|
||||
$created=Get-RmmTraceJson (Join-Path $case 'service-created.json');if($created.id-cne$id-or$created.command-cne$command-or$created.start_name-cne'LocalSystem'){throw 'Original service creation binding'}
|
||||
if(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json')){throw 'Cleanup already attempted; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-intent.json') @{id=$id;command=$command;delete_once=$true}
|
||||
& "$env:WINDIR\System32\sc.exe" delete $name|Out-Null;if($LASTEXITCODE-ne0){throw 'Own stopped service deletion incomplete'}
|
||||
if(Get-RmmTraceService){throw 'Own service still registered; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-completed.json') @{id=$id;service_absent=$true}
|
||||
@{id=$id;cleanup_verified=$true;already_absent=$false;read_only=$false}|ConvertTo-Json -Compress;exit 0
|
||||
}
|
||||
$report=$null;$result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){$report=Get-RmmTraceJson $result}
|
||||
$token=$null;$tokenFile=Join-Path $case 'token.json';if(Test-Path -LiteralPath $tokenFile){$token=Get-RmmTraceJson $tokenFile}
|
||||
$cleaned=$false;$cleanupReceipt=Join-Path $case 'cleanup-completed.json';if(Test-Path -LiteralPath $cleanupReceipt){$c=Get-RmmTraceJson $cleanupReceipt;if($c.id-cne$id-or$c.service_absent-ne$true){throw 'Bound cleanup receipt'};$cleaned=$true}
|
||||
@{id=$id;service_present=[bool]$s;service_state=$(if($s){$s.State}else{$null});service_exit_code=$(if($s){$s.ExitCode}else{$null});start_intent_present=(Test-Path -LiteralPath (Join-Path $case 'service-start-intent.json'));cleanup_intent_present=(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'));cleanup_completed=$cleaned;report=$report;token_before_rpc=$token;read_only=($p.action-eq'readback');no_rmm_jobs=$true;no_w32time_change=$true;no_agent_change=$true}|ConvertTo-Json -Depth 12 -Compress
|
||||
'''
|
||||
|
||||
|
||||
def encoded_command():
|
||||
script = 'try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}'
|
||||
return 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(script.encode('utf-16le')).decode()
|
||||
|
||||
|
||||
def manifest(path):
|
||||
path = path.absolute(); private(path.parent,True)
|
||||
require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}',path.parent.name),'fixed local build manifest')
|
||||
value = load(path)
|
||||
require(value['profile'] == 'clock-system-spnego-v1' and value['schema_version'] == 1
|
||||
and value['native_executed'] is False and value['clock_qualified'] is False and value['product_gate_open'] is False
|
||||
and value['powershell_ast_passed'] is True and value['encoded_command_characters'] == len(encoded_command()) < 32767
|
||||
and value['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(),'same reviewed nonqualifying diagnostic source')
|
||||
require(value['original_wrapper_sha256'] == '74aceb6e2b70c5dd628fae069a842c356c20300b8c8ab12754258c78eaf295c8'
|
||||
and sha(path.parent / 'wrapper-source.diff') == value['wrapper_diff_sha256'],'same exact observational source delta')
|
||||
require(value['token_query_only'] is True and value['max_principal_discoveries']==value['max_status_queries']==1
|
||||
and value['principal_identity_proven'] is False,'no privilege mutation/fallback or principal-as-identity claim')
|
||||
for name,digest in value['unchanged_diagnostics'].items():require(sha(ROOT/name)==digest,'original SYSTEM/WINNT/EEInfo inputs retained')
|
||||
for name,digest in value['own_sources'].items():require(sha(ROOT / name) == digest,'same original local driver source')
|
||||
for name in ('artifact','linked'):
|
||||
item=value[name];p=Path(item['path']);require(p.parent == path.parent / 'output','fixed retained PE path');private(p);require(sha(p) == item['sha256'],'retained exact diagnostic PE')
|
||||
require(Path(value['artifact']['path']).stat().st_size == value['artifact']['bytes'],'exact PE size')
|
||||
return value
|
||||
|
||||
|
||||
def access(root):
|
||||
require(root == ACCESS_ROOT and root.resolve() == root,'existing Main access root only')
|
||||
sys.path.insert(0,str(ROOT / 'deploy'))
|
||||
import vm_access_check
|
||||
config = vm_access_check.load_config(root / 'credentials/server.conf')
|
||||
require(config['address'] == '88.99.80.202' and config['username'] == 'root' and config.get('port','22') == '22','fixed existing cloud reverse jump')
|
||||
record=load(root / 'credentials/windows-dev-access.json');expiry=dt.datetime.fromisoformat(record['expires_at'])
|
||||
require(expiry.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expiry,'current existing access only')
|
||||
private(root / 'credentials',True);private(root / 'credentials/windows-dev.known_hosts')
|
||||
key=str(Path(config['ssh_key_path']).expanduser())
|
||||
jump=['ssh','-F','/dev/null','-T','-p','22','-i',key,'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','ForwardAgent=no','-o','IdentitiesOnly=yes','-W','127.0.0.1:22240','root@88.99.80.202']
|
||||
prefix=['ssh','-F','/dev/null','-T','-i',key,'-o','ProxyCommand='+shlex.join(jump),'-o','HostKeyAlias=rmm-windows-dev','-o','UserKnownHostsFile='+str(root / 'credentials/windows-dev.known_hosts'),'-o','GlobalKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','rmm-dev@127.0.0.1']
|
||||
require(record['ssh_prefix'] == prefix,'original pinned transport only')
|
||||
return prefix
|
||||
|
||||
|
||||
def run_transport(args,data,folder):
|
||||
try:
|
||||
done=subprocess.run(args,input=data,capture_output=True,timeout=50)
|
||||
save(folder / ('transport-'+str(uuid.uuid4())+'.json'),dict(returncode=done.returncode,stdout_b64=base64.b64encode(done.stdout).decode(),stderr_b64=base64.b64encode(done.stderr).decode()))
|
||||
require(done.returncode == 0,'transport incomplete; original readback only')
|
||||
return done.stdout
|
||||
except subprocess.TimeoutExpired as error:
|
||||
save(folder / ('transport-timeout-'+str(uuid.uuid4())+'.json'),dict(timed_out=True,stdout_b64=base64.b64encode(error.stdout or b'').decode(),stderr_b64=base64.b64encode(error.stderr or b'').decode()))
|
||||
raise RuntimeError('lost reply; no repeated operation') from None
|
||||
|
||||
|
||||
def rpc(prefix,payload,folder):
|
||||
value=json.loads(run_transport(prefix+[encoded_command()],(json.dumps(payload)+'\n').encode(),folder).decode('utf-8-sig'),object_pairs_hook=pairs)
|
||||
require(isinstance(value,dict) and 'bootstrap_error' not in value,'remote diagnostic incomplete')
|
||||
return value
|
||||
|
||||
|
||||
def check_report(value,ident):
|
||||
require(value['id'] == ident and value['no_rmm_jobs'] is True and value['no_w32time_change'] is True and value['no_agent_change'] is True,'exact nonproduct diagnostic case')
|
||||
report=value.get('report')
|
||||
if report is None:return False
|
||||
require(report['schema_version'] == 1 and report['profile'] == 'clock-system-spnego-v1' and report['case_id'] == ident
|
||||
and report['token_system'] is True and report['read_only'] is True and report['diagnostic_only'] is True
|
||||
and type(report['query_invocations']) is int and report['query_invocations'] in (0,1) and report['clock_qualified'] is False and report['adapter_qualified'] is False
|
||||
and report['product_gate_open'] is False,'actual SYSTEM token and single nonqualifying trace')
|
||||
token=report['token']
|
||||
require(set(token)=={'query_status','system_user','token_type','thread_query_status','thread_present','lookup_status','privileges_status','privilege_present','privilege_attributes','privilege_enabled','privilege_default','privilege_removed'}
|
||||
and all(type(v) is int and 0<=v<2**32 for v in token.values())
|
||||
and token==value['token_before_rpc'] and token['query_status']==token['lookup_status']==token['privileges_status']==0
|
||||
and token['system_user']==1 and token['token_type']==1 and token['thread_present']==0 and token['thread_query_status']==1008,
|
||||
'durable own SYSTEM primary token/privileges observed before RPC without impersonation')
|
||||
require(all(token[k] in (0,1) for k in ('privilege_present','privilege_enabled','privilege_default','privilege_removed'))
|
||||
and token['privilege_enabled']==int(bool(token['privilege_attributes']&2))
|
||||
and token['privilege_default']==int(bool(token['privilege_attributes']&1))
|
||||
and token['privilege_removed']==int(bool(token['privilege_attributes']&4))
|
||||
and (token['privilege_present'] or token['privilege_attributes']==0),'present/enabled privilege attributes remain observations only')
|
||||
auth=report['auth']
|
||||
require(set(auth)=={'discovery_invocations','discovery_status','discovery_return_present','principal_present','principal_units','principal_validated','status_query_invocations','authn_service','authn_level','impersonation_type','principal_free_attempted','principal_free_status','binding_free_attempted','binding_free_status','eeinfo_origin'}
|
||||
and all(type(v) is int and 0<=v<2**32 for v in auth.values())
|
||||
and auth['authn_service']==9 and auth['authn_level']==6 and auth['impersonation_type']==2
|
||||
and all(auth[k] in (0,1) for k in ('discovery_invocations','discovery_return_present','principal_present','principal_validated','status_query_invocations','principal_free_attempted','binding_free_attempted'))
|
||||
and auth['status_query_invocations']==report['query_invocations'] and auth['eeinfo_origin'] in (0,1,2,3)
|
||||
and auth['discovery_return_present']<=auth['discovery_invocations']
|
||||
and auth['principal_units']<=512,'bounded one-discovery/at-most-one-status SPNEGO Privacy Identify path')
|
||||
if auth['principal_validated']:
|
||||
require(auth['principal_present']==1 and 1<=auth['principal_units']<=512,'checked nonempty bounded principal')
|
||||
if auth['principal_present']:
|
||||
require(auth['principal_free_attempted']==1,'owned principal cleanup attempted')
|
||||
if auth['status_query_invocations']:
|
||||
require(auth['discovery_invocations']==auth['discovery_return_present']==auth['principal_present']==auth['principal_validated']==1
|
||||
and auth['discovery_status']==0 and 1<=auth['principal_units']<=512,'no status query without successful checked principal discovery')
|
||||
fields={'stage','native_rpc_return','native_rpc_return_present','rpc_exception','status_present','ul_size','c_entries','p_entries_present'}
|
||||
require(set(report['trace']) == fields and type(report['rpc_code']) is int and 0 <= report['rpc_code'] < 2**32
|
||||
and all(type(n) is int and 0 <= n < 2**32 for n in report['trace'].values()),'bounded numeric trace only')
|
||||
ee=report['eeinfo']
|
||||
require(set(ee) == {'capture_attempted','start_status','next_status','end_status','count','limit_reached','records'}
|
||||
and all(type(ee[k]) is int and 0 <= ee[k] < 2**32 for k in ('capture_attempted','start_status','next_status','end_status','count','limit_reached'))
|
||||
and ee['capture_attempted'] in (0,1) and 0 <= ee['count'] <= 4 and len(ee['records']) == ee['count']
|
||||
and ee['limit_reached'] == int(ee['count'] == 4),'four-record numeric EEInfo budget')
|
||||
require(ee['capture_attempted']==int(auth['eeinfo_origin']!=0),'numeric EEInfo origin matches capture')
|
||||
record_fields={'generating_component','status','detection_location','flags','number_of_parameters'}
|
||||
for record in ee['records']:
|
||||
require(set(record) == record_fields and all(type(n) is int and 0 <= n < 2**32 for n in record.values())
|
||||
and record['number_of_parameters'] <= 4,'numeric EEInfo fields only; no parameter contents')
|
||||
if report['trace']['stage'] in (90,91):
|
||||
require(ee['capture_attempted']==1 and report['rpc_code']==report['trace']['rpc_exception']
|
||||
and auth['eeinfo_origin']==(2 if report['trace']['stage']==90 else 1),'original exception code preserved before immediate capture')
|
||||
return (value['service_state'] == 'Stopped' and value['service_exit_code'] == 0) or (value['service_present'] is False and value['cleanup_completed'] is True)
|
||||
|
||||
|
||||
def execute(action,path,root,ident):
|
||||
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True);private(STATE,True)
|
||||
value=manifest(path);prefix=access(root)
|
||||
if action == 'prepare':
|
||||
require(not ident,'new diagnostic case only');ident=str(uuid.uuid4());folder=STATE / ident;folder.mkdir(mode=0o700)
|
||||
payload=dict(id=ident,profile='clock-system-spnego-v1',manifest_sha256=sha(path),binary_sha256=value['artifact']['sha256'],binary_bytes=value['artifact']['bytes'],script_sha256=value['script_sha256'])
|
||||
save(folder / 'intent.json',dict(payload=payload,manifest_path=str(path.absolute()),access_root=str(root)))
|
||||
print(json.dumps(dict(case_id=ident,prepare_once=True)),flush=True)
|
||||
prepared=rpc(prefix,dict(payload,action='prepare'),folder);save(folder / 'prepared-remote.json',prepared);require(prepared == dict(id=ident,prepared=True),'new private remote case')
|
||||
save(folder / 'upload-intent.json',dict(binary_sha256=payload['binary_sha256'],upload_once=True))
|
||||
args=['sftp']+[p for p in prefix[1:-1] if p != '-T']+['-b','-',prefix[-1]]
|
||||
destination='/C:/ProgramData/RustDiscoveryBootstrap/clock-spnego-trace-'+ident+'/ClockSystemSpnegoTrace.exe'
|
||||
run_transport(args,('put '+json.dumps(value['artifact']['path'])+' '+json.dumps(destination)+'\n').encode(),folder)
|
||||
save(folder / 'upload-completed.json',dict(uploaded=True));print(json.dumps(dict(case_id=ident,prepared=True,service_started=False)));return
|
||||
require(str(uuid.UUID(ident)) == ident,'canonical original case')
|
||||
folder=STATE / ident;private(folder,True);original=load(folder / 'intent.json');payload=original['payload']
|
||||
require(original['manifest_path'] == str(path.absolute()) and original['access_root'] == str(root)
|
||||
and payload['manifest_sha256'] == sha(path) and payload['script_sha256'] == value['script_sha256'],'original same-case input tuple')
|
||||
if action in ('start','cleanup'):
|
||||
if action == 'start':require(load(folder / 'upload-completed.json')['uploaded'] is True,'completed original upload')
|
||||
save(folder / (action+'-intent.json'),dict(action=action,once=True))
|
||||
result=rpc(prefix,dict(payload,action=action),folder);save(folder / ('readback-'+str(uuid.uuid4())+'.json'),result)
|
||||
if action == 'cleanup':
|
||||
require(result.get('id') == ident and result.get('cleanup_verified') is True,'own stopped service cleanup only')
|
||||
print(json.dumps(dict(case_id=ident,cleanup_verified=True)));return
|
||||
if result.get('status'):
|
||||
require(action == 'readback' and result['status'] == 'remote_case_absent','read-only original partial status')
|
||||
print(json.dumps(dict(case_id=ident,complete=False,retry_allowed=False)));return
|
||||
complete=check_report(result,ident)
|
||||
if complete:
|
||||
destination=folder / 'diagnostic-result.json'
|
||||
receipt=dict(complete=True,manifest_sha256=sha(path),actual=result,clock_qualified=False,product_gate_open=False)
|
||||
if destination.exists():require(load(destination)['actual']['report'] == result['report'],'preserve original numeric report')
|
||||
else:save(destination,receipt)
|
||||
print(json.dumps(dict(case_id=ident,diagnostic_complete=complete,rpc_code=result['report']['rpc_code'] if complete else None,
|
||||
stage=result['report']['trace']['stage'] if complete else None,eeinfo_count=result['report']['eeinfo']['count'] if complete else None,clock_qualified=False,cleanup_separate=True)))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','start','readback','cleanup'))
|
||||
parser.add_argument('--manifest',type=Path,required=True);parser.add_argument('--access-root',type=Path,required=True);parser.add_argument('--id')
|
||||
parser.add_argument('--execute-native',action='store_true');args=parser.parse_args()
|
||||
require((args.action == 'readback' and not args.execute_native) or (args.action != 'readback' and args.execute_native),'explicit native effect phase; readback only reads')
|
||||
execute(args.action,args.manifest,args.access_root,args.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_NATIVE_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,233 @@
|
||||
/* 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 <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
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(RmmClockTrace *trace, RmmClockEeInfo *eeinfo, RmmClockAuthTrace *auth);
|
||||
|
||||
static WCHAR service_name[] = L"RmmClockSystemSpnegoDiagnostic";
|
||||
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-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-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(&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;
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local-only SYSTEM trace diagnostic build; no VM and no product source edits."""
|
||||
import base64
|
||||
import hashlib
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-system-trace'
|
||||
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
||||
BASELINE = ROOT / 'devstate/windows-clock-midl/diagnostic-builds/181c3a1c-8324-41fd-9b7c-aa8010262912/qualification.json'
|
||||
BASELINE_SHA = '2e0de3acf72ab5c3a8e0baa54e8643d086ea7c03d1b38fd89b6bfc00e45f8cbb'
|
||||
NATIVE = ROOT / 'devstate/windows-clock-midl/native-cases/149bafe2-3469-4580-b133-988812bcc3db'
|
||||
READBACK = NATIVE / 'readback-059c15ef-0f31-41aa-ad1d-ea6b774280d7.json'
|
||||
READBACK_SHA = '0dc2373825f7f8f224cda04ecd5c521dd3c37a6e712bc3ed83bc4fa6d0d89c8c'
|
||||
|
||||
|
||||
def require(value, message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def write(path, raw):
|
||||
path.parent.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'wb') as output:
|
||||
output.write(raw); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
def save(path, value):
|
||||
write(path, (json.dumps(value, indent=2) + '\n').encode())
|
||||
|
||||
|
||||
def original_sources():
|
||||
require(sha(BASELINE) == BASELINE_SHA, 'original b1d3 qualification')
|
||||
value = json.loads(BASELINE.read_bytes())
|
||||
require(len(value['source_files']) == 131 and value['artifact']['sha256'] == 'b1d3da4f393ddf20bc8e48a9894ee314666f2ac3613ef97f5c228e3d089ca560', 'unchanged complete original131 source set')
|
||||
for name, digest in value['source_files'].items():
|
||||
path = ROOT / name
|
||||
require(not path.is_symlink() and path.is_file() and sha(path) == digest, 'original131 input changed')
|
||||
return value
|
||||
|
||||
|
||||
def own_sources():
|
||||
return {str((HERE / name).relative_to(ROOT)):sha(HERE / name) for name in ('system_trace.c','build.py','native.py')}
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
baseline = original_sources(); before = own_sources()
|
||||
require(sha(READBACK) == READBACK_SHA, 'actual Microsoft codegen readback')
|
||||
receipt = json.loads(READBACK.read_bytes())
|
||||
require(receipt['readback_only'] is True and receipt['result']['complete'] is True
|
||||
and receipt['result']['run']['exit_code'] == 0 and receipt['result']['no_rpc'] is True,
|
||||
'actual completed MIDL codegen')
|
||||
folder = STATE / ('build-' + str(uuid.uuid4())); folder.mkdir(mode=0o700, parents=True)
|
||||
source = folder / 'source'; source.mkdir(mode=0o700)
|
||||
generated = {}
|
||||
for name, digest in baseline['generated_inputs'].items():
|
||||
path = NATIVE / ('native-' + name); raw = path.read_bytes()
|
||||
item = next(item for item in receipt['files'] if item['name'] == name)
|
||||
require(sha(path) == item['sha256'] == digest and len(raw) == item['bytes']
|
||||
and base64.b64decode(item['base64'], validate=True) == raw, 'same actual unchanged Microsoft output')
|
||||
write(source / name, raw); generated[name] = digest
|
||||
wrapper = ROOT / 'crates/agent-windows/native/clock_rpc.c'
|
||||
write(source / 'clock_rpc.c', wrapper.read_bytes()); write(source / 'system_trace.c', (HERE / 'system_trace.c').read_bytes())
|
||||
save(folder / 'intent.json', dict(image=IMAGE, original131=baseline['source_files'], baseline_sha256=BASELINE_SHA,
|
||||
actual_codegen_readback_sha256=READBACK_SHA, generated=generated, own_sources=before,
|
||||
wrapper_sha256=sha(wrapper), own_system_token_only=True, exactly_one_trace_call=True, no_vm=True))
|
||||
output = folder / 'output'; output.mkdir(mode=0o700)
|
||||
checks = {}
|
||||
def run(name, args):
|
||||
command = ['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=128m',
|
||||
'-v',str(source)+':/src:ro','-v',str(output)+':/out','--entrypoint',args[0],IMAGE,*args[1:]]
|
||||
done = subprocess.run(command,capture_output=True,timeout=120)
|
||||
write(folder / (name+'.stdout'),done.stdout); write(folder / (name+'.stderr'),done.stderr)
|
||||
checks[name] = dict(exit_code=done.returncode,command=command,stdout_sha256=sha(folder / (name+'.stdout')),stderr_sha256=sha(folder / (name+'.stderr')))
|
||||
save(folder / (name+'.json'),checks[name]); require(done.returncode == 0,'local diagnostic compile/check failure: '+name)
|
||||
return done.stdout
|
||||
flags = ['--target=x86_64-w64-windows-gnu','-fms-extensions','-Wall','-Wextra','-Werror','-D_M_AMD64=100','-I/src','-O2']
|
||||
run('main',['clang',*flags,'-c','/src/system_trace.c','-o','/out/system_trace.o'])
|
||||
run('wrapper',['clang',*flags,'-c','/src/clock_rpc.c','-o','/out/clock_rpc.o'])
|
||||
run('actual-midl',['clang',*flags,'-Wno-error=missing-braces','-Wno-error=unused-const-variable','-c','/src/w32time_c.c','-o','/out/w32time_c.o'])
|
||||
run('link',['x86_64-w64-mingw32-gcc','-static-libgcc','-municode','/out/system_trace.o','/out/clock_rpc.o','/out/w32time_c.o',
|
||||
'-lrpcrt4','-ladvapi32','-lkernel32','-lmincore','-o','/out/ClockSystemTrace.linked.exe'])
|
||||
raw_linked = output / 'ClockSystemTrace.linked.exe'
|
||||
linked = output / 'ClockSystemTrace.original.exe'; delivery = output / 'ClockSystemTrace.exe'
|
||||
# Retain a caller-owned private exact copy; Docker's root-owned raw link is
|
||||
# kept too. No chmod/chown of unrelated objects or source mutation.
|
||||
shutil.copyfile(raw_linked,linked); linked.chmod(0o600)
|
||||
shutil.copyfile(linked,delivery); delivery.chmod(0o600)
|
||||
run('strip',['x86_64-w64-mingw32-strip','--strip-debug','/out/ClockSystemTrace.exe'])
|
||||
imports = sorted(set(re.findall(r'DLL Name:\s+(\S+)',run('imports',['x86_64-w64-mingw32-objdump','-p','/out/ClockSystemTrace.exe']).decode())))
|
||||
require(imports and {name.lower() for name in imports}.issubset({'kernel32.dll','msvcrt.dll','advapi32.dll','rpcrt4.dll','api-ms-win-core-realtime-l1-1-2.dll'}),'reviewed system DLL imports only')
|
||||
data = delivery.read_bytes(); offset = int.from_bytes(data[60:64],'little')
|
||||
require(data[:2] == b'MZ' and data[offset:offset+6] == b'PE\0\0\x64\x86' and len(data) < 1048576,'bounded standalone C x64 PE')
|
||||
spec = importlib.util.spec_from_file_location('system_trace_native',HERE / 'native.py'); native = importlib.util.module_from_spec(spec); spec.loader.exec_module(native)
|
||||
script = folder / 'native.ps1'; write(script,native.SCRIPT.encode())
|
||||
parser = "$e=$null;$t=$null;[System.Management.Automation.Language.Parser]::ParseFile($args[0],[ref]$t,[ref]$e)|Out-Null;if($e.Count){$e|ForEach-Object{$_.Message};exit 1};'AST_PASS'"
|
||||
parser_path = folder / 'parse-native.ps1'; write(parser_path,parser.encode())
|
||||
parsed = subprocess.run(['/tmp/rmm-powershell-parser/pwsh','-NoLogo','-NoProfile','-NonInteractive','-File',str(parser_path),str(script)],capture_output=True,timeout=15)
|
||||
write(folder / 'powershell-parser.stdout',parsed.stdout); write(folder / 'powershell-parser.stderr',parsed.stderr)
|
||||
require(parsed.returncode == 0 and b'AST_PASS' in parsed.stdout and not parsed.stderr,'actual PowerShell AST')
|
||||
command_chars = len(native.encoded_command())
|
||||
require(command_chars < 32767,'Windows encoded command length')
|
||||
require(original_sources()['source_files'] == baseline['source_files'] and own_sources() == before,'all original131 and new sources unchanged')
|
||||
manifest = dict(schema_version=1,profile='clock-system-trace-v1',image=IMAGE,baseline_sha256=BASELINE_SHA,
|
||||
original131=baseline['source_files'],own_sources=before,wrapper_sha256=sha(wrapper),generated=generated,
|
||||
actual_codegen_readback_sha256=READBACK_SHA,checks=checks,script_sha256=hashlib.sha256(native.SCRIPT.encode()).hexdigest(),
|
||||
powershell_ast_passed=True,encoded_command_characters=command_chars,
|
||||
artifact=dict(path=str(delivery),sha256=sha(delivery),bytes=len(data),imports=imports),
|
||||
linked=dict(path=str(linked),sha256=sha(linked)),raw_linked_sha256=sha(raw_linked),native_executed=False,clock_qualified=False,
|
||||
adapter_qualified=False,product_gate_open=False,no_vm=True,no_rmm_jobs=True)
|
||||
save(folder / 'manifest.json',manifest)
|
||||
print(json.dumps(dict(manifest=str(folder / 'manifest.json'),manifest_sha256=sha(folder / 'manifest.json'),artifact=manifest['artifact'],
|
||||
original131_unchanged=True,powershell_ast=True,command_characters=command_chars,native_executed=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try: main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_LOCAL_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,222 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Parent-only native SYSTEM trace bootstrap: prepare, once start, read, cleanup.
|
||||
|
||||
Readback is strictly read-only. Cleanup is a separate explicit phase and may
|
||||
remove only this exact stopped diagnostic service. Never changes W32Time/RMM.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import datetime as dt
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shlex
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[3]
|
||||
HERE = Path(__file__).resolve().parent
|
||||
STATE = ROOT / 'devstate/windows-clock-system-trace'
|
||||
ACCESS_ROOT = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
||||
|
||||
|
||||
def require(value,message):
|
||||
if not value: raise RuntimeError(message)
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def pairs(items):
|
||||
result = {}
|
||||
for name,value in items:
|
||||
require(name not in result,'duplicate JSON key'); result[name] = value
|
||||
return result
|
||||
|
||||
|
||||
def private(path,directory=False):
|
||||
item = Path(path).lstat()
|
||||
require(item.st_uid == os.getuid() and stat.S_IMODE(item.st_mode) == (0o700 if directory else 0o600)
|
||||
and (stat.S_ISDIR(item.st_mode) if directory else stat.S_ISREG(item.st_mode) and item.st_nlink == 1),'private owned path')
|
||||
|
||||
|
||||
def load(path):
|
||||
private(path); require(Path(path).stat().st_size <= 2*1024*1024,'bounded private JSON')
|
||||
return json.loads(Path(path).read_bytes(),object_pairs_hook=pairs)
|
||||
|
||||
|
||||
def save(path,value):
|
||||
private(path.parent,True)
|
||||
fd = os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
|
||||
with os.fdopen(fd,'w') as output:
|
||||
json.dump(value,output,indent=2); output.flush(); os.fsync(output.fileno())
|
||||
|
||||
|
||||
SCRIPT = r'''
|
||||
$ErrorActionPreference='Stop';$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
$id=[guid]::ParseExact($p.id,'D').ToString()
|
||||
if($id-cne$p.id-or$p.profile-cne'clock-system-trace-v1'-or$p.action-notin@('prepare','start','readback','cleanup')-or$p.binary_sha256-notmatch'^[a-f0-9]{64}$'-or$p.manifest_sha256-notmatch'^[a-f0-9]{64}$'-or$p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed SYSTEM diagnostic request'}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap';$name='RmmClockSystemDiagnostic';$case=Join-Path $root ('clock-system-trace-'+$id);$exe=Join-Path $case 'ClockSystemTrace.exe';$command='"'+$exe+'" --service '+$id
|
||||
function Test-RmmTracePrivate($path,$dir){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$dir-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Private object type'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')){throw 'Private object owner'};$rules=@($a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if(!$rules.Count){throw 'Empty private ACL'};foreach($e in $rules){if($e.AccessControlType-ne'Allow'-or$e.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or($e.PropagationFlags-band[Security.AccessControl.PropagationFlags]::InheritOnly)){throw 'Broad private ACL'}}}
|
||||
function Write-RmmTraceNew($path,$value){$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try{$b=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 12 -Compress));$f.Write($b,0,$b.Length);$f.Flush($true)}finally{$f.Dispose()}}
|
||||
function Get-RmmTraceJson($path){Test-RmmTracePrivate $path $false;if((Get-Item -LiteralPath $path).Length-gt16384){throw 'Small diagnostic receipt'};return(Get-Content -LiteralPath $path -Raw|ConvertFrom-Json)}
|
||||
function Get-RmmTraceService{return(Get-CimInstance Win32_Service -Filter "Name='RmmClockSystemDiagnostic'" -OperationTimeoutSec 5)}
|
||||
function Test-RmmTraceService($s){if($s-and($s.PathName-cne$command-or$s.StartName-cne'LocalSystem'-or$s.StartMode-cne'Manual')){throw 'Only own exact temporary service'}}
|
||||
if($p.action-eq'readback'-and!(Test-Path -LiteralPath $case)){@{id=$id;status='remote_case_absent';read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $root $true
|
||||
if($p.action-eq'prepare'){if((Test-Path -LiteralPath $case)-or(Get-RmmTraceService)){throw 'Existing case/service retained'};New-Item -ItemType Directory -Path $case|Out-Null;Test-RmmTracePrivate $case $true;Write-RmmTraceNew (Join-Path $case 'intent.json') $p;@{id=$id;prepared=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
Test-RmmTracePrivate $case $true;$old=Get-RmmTraceJson (Join-Path $case 'intent.json')
|
||||
foreach($field in @('id','profile','binary_sha256','manifest_sha256','script_sha256','binary_bytes')){if($old.$field-cne$p.$field){throw 'Original exact diagnostic intent'}}
|
||||
Test-RmmTracePrivate $exe $false;if((Get-Item -LiteralPath $exe).Length-ne$p.binary_bytes-or(Get-FileHash -LiteralPath $exe -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.binary_sha256){throw 'Exact diagnostic executable'}
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s
|
||||
if($p.action-eq'start'){
|
||||
if($s-or(Test-Path -LiteralPath (Join-Path $case 'start-intent.json'))){throw 'No repeated start; original readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'start-intent.json') @{id=$id;command=$command;binary_sha256=$p.binary_sha256;create_once=$true}
|
||||
New-Service -Name $name -BinaryPathName $command -StartupType Manual|Out-Null
|
||||
$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s){throw 'Own service creation missing'}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-created.json') @{id=$id;command=$command;start_name=$s.StartName}
|
||||
Write-RmmTraceNew (Join-Path $case 'service-start-intent.json') @{start_once=$true}
|
||||
Start-Service -Name $name
|
||||
$deadline=[DateTime]::UtcNow.AddSeconds(20)
|
||||
do{$s=Get-RmmTraceService;Test-RmmTraceService $s;if(!$s-or$s.State-eq'Stopped'){break};Start-Sleep -Milliseconds 100}while([DateTime]::UtcNow-lt$deadline)
|
||||
}
|
||||
if($p.action-eq'cleanup'){
|
||||
if(!$s){if(!(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'))){throw 'Missing service without bound cleanup'};@{id=$id;cleanup_verified=$true;already_absent=$true;read_only=$true}|ConvertTo-Json -Compress;exit 0}
|
||||
if($s.State-cne'Stopped'){throw 'Cleanup only own bound stopped service'}
|
||||
$created=Get-RmmTraceJson (Join-Path $case 'service-created.json');if($created.id-cne$id-or$created.command-cne$command-or$created.start_name-cne'LocalSystem'){throw 'Original service creation binding'}
|
||||
if(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json')){throw 'Cleanup already attempted; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-intent.json') @{id=$id;command=$command;delete_once=$true}
|
||||
& "$env:WINDIR\System32\sc.exe" delete $name|Out-Null;if($LASTEXITCODE-ne0){throw 'Own stopped service deletion incomplete'}
|
||||
if(Get-RmmTraceService){throw 'Own service still registered; readback only'}
|
||||
Write-RmmTraceNew (Join-Path $case 'cleanup-completed.json') @{id=$id;service_absent=$true}
|
||||
@{id=$id;cleanup_verified=$true;already_absent=$false;read_only=$false}|ConvertTo-Json -Compress;exit 0
|
||||
}
|
||||
$report=$null;$result=Join-Path $case 'result.json';if(Test-Path -LiteralPath $result){$report=Get-RmmTraceJson $result}
|
||||
$cleaned=$false;$cleanupReceipt=Join-Path $case 'cleanup-completed.json';if(Test-Path -LiteralPath $cleanupReceipt){$c=Get-RmmTraceJson $cleanupReceipt;if($c.id-cne$id-or$c.service_absent-ne$true){throw 'Bound cleanup receipt'};$cleaned=$true}
|
||||
@{id=$id;service_present=[bool]$s;service_state=$(if($s){$s.State}else{$null});service_exit_code=$(if($s){$s.ExitCode}else{$null});start_intent_present=(Test-Path -LiteralPath (Join-Path $case 'service-start-intent.json'));cleanup_intent_present=(Test-Path -LiteralPath (Join-Path $case 'cleanup-intent.json'));cleanup_completed=$cleaned;report=$report;read_only=($p.action-eq'readback');no_rmm_jobs=$true;no_w32time_change=$true;no_agent_change=$true}|ConvertTo-Json -Depth 12 -Compress
|
||||
'''
|
||||
|
||||
|
||||
def encoded_command():
|
||||
script = 'try {\n'+SCRIPT+'\n} catch {[pscustomobject]@{bootstrap_error=$true;error_id=$_.FullyQualifiedErrorId;line=$_.InvocationInfo.ScriptLineNumber}|ConvertTo-Json -Compress;exit 1}'
|
||||
return 'powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand '+base64.b64encode(script.encode('utf-16le')).decode()
|
||||
|
||||
|
||||
def manifest(path):
|
||||
path = path.absolute(); private(path.parent,True)
|
||||
require(path.name == 'manifest.json' and path.parent.parent == STATE and re.fullmatch(r'build-[0-9a-f-]{36}',path.parent.name),'fixed local build manifest')
|
||||
value = load(path)
|
||||
require(value['profile'] == 'clock-system-trace-v1' and value['schema_version'] == 1
|
||||
and value['native_executed'] is False and value['clock_qualified'] is False and value['product_gate_open'] is False
|
||||
and value['powershell_ast_passed'] is True and value['encoded_command_characters'] == len(encoded_command()) < 32767
|
||||
and value['script_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(),'same reviewed nonqualifying diagnostic source')
|
||||
for name,digest in value['own_sources'].items():require(sha(ROOT / name) == digest,'same original local driver source')
|
||||
for name in ('artifact','linked'):
|
||||
item=value[name];p=Path(item['path']);require(p.parent == path.parent / 'output','fixed retained PE path');private(p);require(sha(p) == item['sha256'],'retained exact diagnostic PE')
|
||||
require(Path(value['artifact']['path']).stat().st_size == value['artifact']['bytes'],'exact PE size')
|
||||
return value
|
||||
|
||||
|
||||
def access(root):
|
||||
require(root == ACCESS_ROOT and root.resolve() == root,'existing Main access root only')
|
||||
sys.path.insert(0,str(ROOT / 'deploy'))
|
||||
import vm_access_check
|
||||
config = vm_access_check.load_config(root / 'credentials/server.conf')
|
||||
require(config['address'] == '88.99.80.202' and config['username'] == 'root' and config.get('port','22') == '22','fixed existing cloud reverse jump')
|
||||
record=load(root / 'credentials/windows-dev-access.json');expiry=dt.datetime.fromisoformat(record['expires_at'])
|
||||
require(expiry.utcoffset() is not None and dt.datetime.now(dt.timezone.utc) < expiry,'current existing access only')
|
||||
private(root / 'credentials',True);private(root / 'credentials/windows-dev.known_hosts')
|
||||
key=str(Path(config['ssh_key_path']).expanduser())
|
||||
jump=['ssh','-F','/dev/null','-T','-p','22','-i',key,'-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','ForwardAgent=no','-o','IdentitiesOnly=yes','-W','127.0.0.1:22240','root@88.99.80.202']
|
||||
prefix=['ssh','-F','/dev/null','-T','-i',key,'-o','ProxyCommand='+shlex.join(jump),'-o','HostKeyAlias=rmm-windows-dev','-o','UserKnownHostsFile='+str(root / 'credentials/windows-dev.known_hosts'),'-o','GlobalKnownHostsFile=/dev/null','-o','StrictHostKeyChecking=yes','-o','BatchMode=yes','-o','IdentitiesOnly=yes','-o','ForwardAgent=no','-o','ConnectTimeout=10','rmm-dev@127.0.0.1']
|
||||
require(record['ssh_prefix'] == prefix,'original pinned transport only')
|
||||
return prefix
|
||||
|
||||
|
||||
def run_transport(args,data,folder):
|
||||
try:
|
||||
done=subprocess.run(args,input=data,capture_output=True,timeout=50)
|
||||
save(folder / ('transport-'+str(uuid.uuid4())+'.json'),dict(returncode=done.returncode,stdout_b64=base64.b64encode(done.stdout).decode(),stderr_b64=base64.b64encode(done.stderr).decode()))
|
||||
require(done.returncode == 0,'transport incomplete; original readback only')
|
||||
return done.stdout
|
||||
except subprocess.TimeoutExpired as error:
|
||||
save(folder / ('transport-timeout-'+str(uuid.uuid4())+'.json'),dict(timed_out=True,stdout_b64=base64.b64encode(error.stdout or b'').decode(),stderr_b64=base64.b64encode(error.stderr or b'').decode()))
|
||||
raise RuntimeError('lost reply; no repeated operation') from None
|
||||
|
||||
|
||||
def rpc(prefix,payload,folder):
|
||||
value=json.loads(run_transport(prefix+[encoded_command()],(json.dumps(payload)+'\n').encode(),folder).decode('utf-8-sig'),object_pairs_hook=pairs)
|
||||
require(isinstance(value,dict) and 'bootstrap_error' not in value,'remote diagnostic incomplete')
|
||||
return value
|
||||
|
||||
|
||||
def check_report(value,ident):
|
||||
require(value['id'] == ident and value['no_rmm_jobs'] is True and value['no_w32time_change'] is True and value['no_agent_change'] is True,'exact nonproduct diagnostic case')
|
||||
report=value.get('report')
|
||||
if report is None:return False
|
||||
require(report['schema_version'] == 1 and report['profile'] == 'clock-system-trace-v1' and report['case_id'] == ident
|
||||
and report['token_system'] is True and report['read_only'] is True and report['diagnostic_only'] is True
|
||||
and report['query_invocations'] == 1 and report['clock_qualified'] is False and report['adapter_qualified'] is False
|
||||
and report['product_gate_open'] is False,'actual SYSTEM token and single nonqualifying trace')
|
||||
fields={'stage','native_rpc_return','native_rpc_return_present','rpc_exception','status_present','ul_size','c_entries','p_entries_present'}
|
||||
require(set(report['trace']) == fields and type(report['rpc_code']) is int and 0 <= report['rpc_code'] < 2**32
|
||||
and all(type(n) is int and 0 <= n < 2**32 for n in report['trace'].values()),'bounded numeric trace only')
|
||||
return (value['service_state'] == 'Stopped' and value['service_exit_code'] == 0) or (value['service_present'] is False and value['cleanup_completed'] is True)
|
||||
|
||||
|
||||
def execute(action,path,root,ident):
|
||||
os.umask(0o077);STATE.mkdir(mode=0o700,exist_ok=True);private(STATE,True)
|
||||
value=manifest(path);prefix=access(root)
|
||||
if action == 'prepare':
|
||||
require(not ident,'new diagnostic case only');ident=str(uuid.uuid4());folder=STATE / ident;folder.mkdir(mode=0o700)
|
||||
payload=dict(id=ident,profile='clock-system-trace-v1',manifest_sha256=sha(path),binary_sha256=value['artifact']['sha256'],binary_bytes=value['artifact']['bytes'],script_sha256=value['script_sha256'])
|
||||
save(folder / 'intent.json',dict(payload=payload,manifest_path=str(path.absolute()),access_root=str(root)))
|
||||
print(json.dumps(dict(case_id=ident,prepare_once=True)),flush=True)
|
||||
prepared=rpc(prefix,dict(payload,action='prepare'),folder);save(folder / 'prepared-remote.json',prepared);require(prepared == dict(id=ident,prepared=True),'new private remote case')
|
||||
save(folder / 'upload-intent.json',dict(binary_sha256=payload['binary_sha256'],upload_once=True))
|
||||
args=['sftp']+[p for p in prefix[1:-1] if p != '-T']+['-b','-',prefix[-1]]
|
||||
destination='/C:/ProgramData/RustDiscoveryBootstrap/clock-system-trace-'+ident+'/ClockSystemTrace.exe'
|
||||
run_transport(args,('put '+json.dumps(value['artifact']['path'])+' '+json.dumps(destination)+'\n').encode(),folder)
|
||||
save(folder / 'upload-completed.json',dict(uploaded=True));print(json.dumps(dict(case_id=ident,prepared=True,service_started=False)));return
|
||||
require(str(uuid.UUID(ident)) == ident,'canonical original case')
|
||||
folder=STATE / ident;private(folder,True);original=load(folder / 'intent.json');payload=original['payload']
|
||||
require(original['manifest_path'] == str(path.absolute()) and original['access_root'] == str(root)
|
||||
and payload['manifest_sha256'] == sha(path) and payload['script_sha256'] == value['script_sha256'],'original same-case input tuple')
|
||||
if action in ('start','cleanup'):
|
||||
if action == 'start':require(load(folder / 'upload-completed.json')['uploaded'] is True,'completed original upload')
|
||||
save(folder / (action+'-intent.json'),dict(action=action,once=True))
|
||||
result=rpc(prefix,dict(payload,action=action),folder);save(folder / ('readback-'+str(uuid.uuid4())+'.json'),result)
|
||||
if action == 'cleanup':
|
||||
require(result.get('id') == ident and result.get('cleanup_verified') is True,'own stopped service cleanup only')
|
||||
print(json.dumps(dict(case_id=ident,cleanup_verified=True)));return
|
||||
if result.get('status'):
|
||||
require(action == 'readback' and result['status'] == 'remote_case_absent','read-only original partial status')
|
||||
print(json.dumps(dict(case_id=ident,complete=False,retry_allowed=False)));return
|
||||
complete=check_report(result,ident)
|
||||
if complete:
|
||||
destination=folder / 'diagnostic-result.json'
|
||||
receipt=dict(complete=True,manifest_sha256=sha(path),actual=result,clock_qualified=False,product_gate_open=False)
|
||||
if destination.exists():require(load(destination)['actual']['report'] == result['report'],'preserve original numeric report')
|
||||
else:save(destination,receipt)
|
||||
print(json.dumps(dict(case_id=ident,diagnostic_complete=complete,rpc_code=result['report']['rpc_code'] if complete else None,
|
||||
stage=result['report']['trace']['stage'] if complete else None,clock_qualified=False,cleanup_separate=True)))
|
||||
|
||||
|
||||
def main():
|
||||
parser=argparse.ArgumentParser(description=__doc__);parser.add_argument('action',choices=('prepare','start','readback','cleanup'))
|
||||
parser.add_argument('--manifest',type=Path,required=True);parser.add_argument('--access-root',type=Path,required=True);parser.add_argument('--id')
|
||||
parser.add_argument('--execute-native',action='store_true');args=parser.parse_args()
|
||||
require((args.action == 'readback' and not args.execute_native) or (args.action != 'readback' and args.execute_native),'explicit native effect phase; readback only reads')
|
||||
execute(args.action,args.manifest,args.access_root,args.id)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:main()
|
||||
except Exception as error:
|
||||
print(json.dumps(dict(error='CLOCK_SYSTEM_TRACE_NATIVE_INCOMPLETE',error_class=type(error).__name__,details_suppressed=True)))
|
||||
raise SystemExit(2) from None
|
||||
@@ -0,0 +1,99 @@
|
||||
/* Standalone diagnostic only. The original C/Auth/MIDL inputs are linked
|
||||
* unchanged. Exactly one trace query, only under this process's SYSTEM token.
|
||||
* No clock setter, token impersonation, NTP/ACL/provider or product gate. */
|
||||
#define _WIN32_WINNT 0x0a00
|
||||
#include <windows.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <wchar.h>
|
||||
|
||||
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");
|
||||
uint32_t rmm_clock_query_trace(RmmClockTrace *trace);
|
||||
|
||||
static WCHAR service_name[] = L"RmmClockSystemDiagnostic";
|
||||
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(void) {
|
||||
HANDLE token = NULL;
|
||||
DWORD needed = 0, result = ERROR_SUCCESS;
|
||||
_Alignas(8) BYTE storage[4096];
|
||||
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &token)) return GetLastError();
|
||||
if (!GetTokenInformation(token, TokenUser, storage, sizeof(storage), &needed)) result = GetLastError();
|
||||
else if (needed > sizeof(storage) || !IsWellKnownSid(((TOKEN_USER *)storage)->User.Sid, WinLocalSystemSid)) result = ERROR_ACCESS_DENIED;
|
||||
CloseHandle(token);
|
||||
return result;
|
||||
}
|
||||
|
||||
static DWORD write_result(uint32_t rpc, const RmmClockTrace *trace) {
|
||||
WCHAR path[256];
|
||||
char id[37], report[1536];
|
||||
DWORD written = 0, result = ERROR_SUCCESS;
|
||||
for (unsigned i = 0; i < 37; ++i) id[i] = (char)case_id[i];
|
||||
int count = swprintf(path, sizeof(path) / sizeof(path[0]),
|
||||
L"C:\\ProgramData\\RustDiscoveryBootstrap\\clock-system-trace-%ls\\result.json", case_id);
|
||||
if (count <= 0 || count >= (int)(sizeof(path) / sizeof(path[0]))) return ERROR_INVALID_PARAMETER;
|
||||
count = snprintf(report, sizeof(report),
|
||||
"{\"schema_version\":1,\"profile\":\"clock-system-trace-v1\",\"case_id\":\"%s\","
|
||||
"\"token_system\":true,\"read_only\":true,\"diagnostic_only\":true,\"query_invocations\":1,"
|
||||
"\"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)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;
|
||||
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, report, (DWORD)count, &written, NULL) || written != (DWORD)count) result = ERROR_WRITE_FAULT;
|
||||
else if (!FlushFileBuffers(file)) result = GetLastError();
|
||||
CloseHandle(file);
|
||||
return result;
|
||||
}
|
||||
|
||||
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;
|
||||
DWORD result = own_system_token();
|
||||
if (!result) {
|
||||
RmmClockTrace trace = {0};
|
||||
uint32_t rpc = rmm_clock_query_trace(&trace); /* exactly one fixed call */
|
||||
result = write_result(rpc, &trace);
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Local-only, separate MIDL readiness crossbuild. Never accesses a VM.
|
||||
|
||||
Preserves all existing runners, product sources, generated outputs and evidence.
|
||||
Each invocation creates a fresh source snapshot and records failed checks too.
|
||||
"""
|
||||
import base64
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
PACKAGE = Path('deploy/windows/clock-midl-diagnostic')
|
||||
IMAGE = 'sha256:1b2fd985d2c1fcc3545ace602405dc68fa899201bd8a73fa319ff96377203fa9'
|
||||
CODEGEN = ROOT / 'devstate/windows-clock-midl/native-cases/149bafe2-3469-4580-b133-988812bcc3db'
|
||||
READBACK = 'readback-059c15ef-0f31-41aa-ad1d-ea6b774280d7.json'
|
||||
READBACK_SHA = '0dc2373825f7f8f224cda04ecd5c521dd3c37a6e712bc3ed83bc4fa6d0d89c8c'
|
||||
PINNED = {
|
||||
'w32time.h': 'db5dbe5b335eaa58a6512d4abfda44e7273f3aa8a43077dcb0719e4b97ee2bde',
|
||||
'w32time_c.c': 'd806adb431f16724c8afdea0c0751588a4bc6f82b6f0b8391d4d189d6a6f4a38',
|
||||
}
|
||||
|
||||
|
||||
def sha(raw):
|
||||
return hashlib.sha256(raw).hexdigest()
|
||||
|
||||
|
||||
def require(condition, message):
|
||||
if not condition:
|
||||
raise RuntimeError(message)
|
||||
|
||||
|
||||
def write_new(path, raw):
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with path.open('xb') as out:
|
||||
out.write(raw)
|
||||
out.flush()
|
||||
os.fsync(out.fileno())
|
||||
|
||||
|
||||
def save(path, value):
|
||||
write_new(path, (json.dumps(value, indent=2) + '\n').encode())
|
||||
|
||||
|
||||
def compiler_inputs():
|
||||
paths = [ROOT / name for name in ('Cargo.toml', 'Cargo.lock', 'rust-toolchain.toml')]
|
||||
paths += [p for p in (ROOT / 'crates').rglob('*') if p.is_file() and p.suffix in ('.rs', '.toml', '.c', '.idl')]
|
||||
paths += [p for p in (ROOT / 'proto').rglob('*') if p.is_file()]
|
||||
paths += [ROOT / PACKAGE / name for name in ('Cargo.toml', 'build.rs')]
|
||||
paths += [Path(__file__).resolve()]
|
||||
require(all(not p.is_symlink() for p in paths), 'compiler input symlink')
|
||||
return {str(p.relative_to(ROOT)): sha(p.read_bytes()) for p in sorted(paths)}
|
||||
|
||||
|
||||
def external_packages(lock):
|
||||
# Only compare Cargo-generated fixed string fields; never execute TOML.
|
||||
result = set()
|
||||
for block in lock.split('[[package]]')[1:]:
|
||||
fields = dict(re.findall(r'^(name|version|source|checksum) = "([^"\n]+)"$', block, re.M))
|
||||
if 'source' in fields:
|
||||
require(set(fields) == {'name', 'version', 'source', 'checksum'}, 'unrecognized external lock entry')
|
||||
result.add(tuple(fields[key] for key in ('name', 'version', 'source', 'checksum')))
|
||||
return result
|
||||
|
||||
|
||||
def main():
|
||||
os.umask(0o077)
|
||||
before = compiler_inputs()
|
||||
receipt_raw = (CODEGEN / READBACK).read_bytes()
|
||||
require(sha(receipt_raw) == READBACK_SHA, 'original native readback hash changed')
|
||||
receipt = json.loads(receipt_raw)
|
||||
result = receipt['result']
|
||||
require(receipt['readback_only'] is True and result['complete'] is True and result['no_rpc'] is True
|
||||
and result['run']['exit_code'] == 0 and result['run']['all_own_children_exited'] is True
|
||||
and result['clock_qualified'] is False, 'native codegen readback incomplete')
|
||||
ident = str(uuid.uuid4())
|
||||
case = ROOT / 'devstate/windows-clock-midl/diagnostic-builds' / ident
|
||||
case.mkdir(parents=True, mode=0o700)
|
||||
source = case / 'source'
|
||||
source.mkdir(mode=0o700)
|
||||
for name, digest in before.items():
|
||||
raw = (ROOT / name).read_bytes()
|
||||
require(sha(raw) == digest, 'source changed during snapshot')
|
||||
write_new(source / name, raw)
|
||||
for name, digest in PINNED.items():
|
||||
raw = (CODEGEN / ('native-' + name)).read_bytes()
|
||||
original = next(item for item in receipt['files'] if item['name'] == name)
|
||||
require(sha(raw) == digest == original['sha256'] and len(raw) == original['bytes']
|
||||
and base64.b64decode(original['base64'], validate=True) == raw, 'actual Microsoft output differs')
|
||||
write_new(source / PACKAGE / 'generated' / name, raw)
|
||||
write_new(source / PACKAGE / 'Cargo.lock', (ROOT / 'Cargo.lock').read_bytes())
|
||||
save(case / 'intent.json', dict(id=ident, image=IMAGE, native_case=str(CODEGEN), native_readback_sha256=READBACK_SHA,
|
||||
source_files=before, microsoft_generated=PINNED, no_vm=True, no_jobs=True,
|
||||
production_clock_qualified=False))
|
||||
print('Retained local MIDL diagnostic build ' + ident, flush=True)
|
||||
checks = {}
|
||||
|
||||
def run(name, args):
|
||||
command = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=512m',
|
||||
'-v', str(source) + ':/src:ro', '-v', str(case / 'target') + ':/target',
|
||||
'-v', str(case) + ':/evidence', '-e', 'CARGO_TARGET_DIR=/target',
|
||||
'--entrypoint', args[0], IMAGE] + args[1:]
|
||||
with (case / (name + '.log')).open('xb') as output:
|
||||
completed = subprocess.run(command, stdout=output, stderr=subprocess.STDOUT, timeout=600)
|
||||
checks[name] = dict(exit_code=completed.returncode, command=command, log_sha256=sha((case / (name + '.log')).read_bytes()))
|
||||
save(case / (name + '.check.json'), checks[name])
|
||||
require(completed.returncode == 0, 'local check failed: ' + name)
|
||||
print(name + ' PASS', flush=True)
|
||||
|
||||
manifest = '/src/' + str(PACKAGE / 'Cargo.toml')
|
||||
# Resolve only the new diagnostic leaf against the existing locked versions.
|
||||
# Snapshot manifest directory is temporarily a private writable mount for
|
||||
# this sole metadata step; the source worktree is never writable in Docker.
|
||||
writable = case / 'lock-resolution'
|
||||
shutil.copytree(source, writable)
|
||||
metadata_command = ['docker', 'run', '--rm', '--network', 'none', '--read-only', '--tmpfs', '/tmp:rw,size=128m',
|
||||
'-v', str(writable) + ':/src', '--entrypoint', 'cargo', IMAGE, 'metadata',
|
||||
'--manifest-path', manifest, '--offline', '--format-version', '1']
|
||||
with (case / 'lock-resolution.log').open('xb') as output:
|
||||
lock_result = subprocess.run(metadata_command, stdout=output, stderr=subprocess.STDOUT, timeout=120)
|
||||
require(lock_result.returncode == 0, 'diagnostic lock resolution failed')
|
||||
resolved = (writable / PACKAGE / 'Cargo.lock').read_bytes()
|
||||
require(external_packages(resolved.decode()).issubset(external_packages((ROOT / 'Cargo.lock').read_text())), 'new external dependency version')
|
||||
# Replace only our freshly copied diagnostic lock BEFORE build admission;
|
||||
# this is never an edit to generated NDR/header data or an OUT_DIR patch.
|
||||
(source / PACKAGE / 'Cargo.lock').write_bytes(resolved)
|
||||
save(case / 'resolved-lock.json', dict(sha256=sha(resolved), no_new_external_versions=True))
|
||||
run('fmt', ['cargo', 'fmt', '--manifest-path', manifest, '--', '--check'])
|
||||
run('clippy-windows', ['cargo', 'clippy', '--manifest-path', manifest, '--locked', '--offline', '--target', 'x86_64-pc-windows-gnu', '--all-targets', '--', '-D', 'warnings'])
|
||||
run('linux-clock-tests', ['cargo', 'test', '--manifest-path', manifest, '--locked', '--offline', '--bin', 'clock-midl-readiness', '--', '--test-threads=1'])
|
||||
require('test result: ok. 5 passed; 0 failed; 0 ignored;' in (case / 'linux-clock-tests.log').read_text(), 'actual five clock tests required')
|
||||
run('windows-test-link', ['cargo', 'build', '--manifest-path', manifest, '--locked', '--offline', '--target', 'x86_64-pc-windows-gnu', '--tests'])
|
||||
run('release-link', ['cargo', 'build', '--manifest-path', manifest, '--locked', '--offline', '--target', 'x86_64-pc-windows-gnu', '--release', '--bin', 'clock-midl-readiness'])
|
||||
run('artifact', ['sh', '-c', 'cp /target/x86_64-pc-windows-gnu/release/clock-midl-readiness.exe /evidence/WindowsClockReadiness.linked.exe && cp /evidence/WindowsClockReadiness.linked.exe /evidence/WindowsClockReadiness.exe && x86_64-w64-mingw32-strip /evidence/WindowsClockReadiness.exe && x86_64-w64-mingw32-objdump -p /evidence/WindowsClockReadiness.exe'])
|
||||
require(compiler_inputs() == before, 'source inputs changed during diagnostic build')
|
||||
executable = case / 'WindowsClockReadiness.exe'
|
||||
qualification = dict(id=ident, source_files=before, source_before_after_identical=True, image=IMAGE,
|
||||
native_codegen_case=CODEGEN.name, native_readback_sha256=READBACK_SHA,
|
||||
generated_inputs=PINNED, checks=checks, artifact=dict(path=str(executable), sha256=sha(executable.read_bytes()), bytes=executable.stat().st_size),
|
||||
linked_binary_sha256=sha((case / 'WindowsClockReadiness.linked.exe').read_bytes()),
|
||||
linux_clock_test_count=5, native_windows_execution=False, native_cloud_linux_execution=False,
|
||||
production_clock_qualified=False, no_vm=True, no_jobs=True)
|
||||
save(case / 'qualification.json', qualification)
|
||||
print(json.dumps(dict(id=ident, artifact=qualification['artifact'], qualification_sha256=sha((case / 'qualification.json').read_bytes()), no_vm=True, clock_qualified=False)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,279 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Package existing five portable clock units; only Root invokes native phases.
|
||||
|
||||
No build, Windows/NDR execution, RMM daemon, service, policy, DB or job action.
|
||||
Lost native replies permit readback of the same case only, never another run.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import hashlib
|
||||
import inspect
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path.insert(0, str(ROOT / 'deploy'))
|
||||
import remote_client_bootstrap as linux
|
||||
import vm_mvp_acceptance as contract
|
||||
|
||||
CASE = ROOT / 'devstate/windows-clock-midl/diagnostic-builds/181c3a1c-8324-41fd-9b7c-aa8010262912'
|
||||
QUALIFICATION_SHA = '2e0de3acf72ab5c3a8e0baa54e8643d086ea7c03d1b38fd89b6bfc00e45f8cbb'
|
||||
STATE = ROOT / 'devstate/windows-clock-midl/linux-native-packs'
|
||||
REMOTE = '/var/lib/rmm-clock-midl-portable-tests'
|
||||
TESTS = (
|
||||
'clock::tests::gate_requires_all_status_and_uncertainty_evidence',
|
||||
'clock::tests::production_clock_stays_closed_until_native_adapter_qualification',
|
||||
'clock::tests::suspend_closes_until_actual_post_resume_sync',
|
||||
'clock::tests::timeout_worker_stays_closed_without_a_second_call',
|
||||
'clock::tests::wall_step_and_overflow_fail_closed',
|
||||
)
|
||||
require = contract.require
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def support_sources():
|
||||
return {str(Path(module.__file__).resolve().relative_to(ROOT)): sha(module.__file__)
|
||||
for module in (linux, contract, linux.access)}
|
||||
|
||||
|
||||
def native_source_sha256():
|
||||
return hashlib.sha256(inspect.getsource(native_case).encode()).hexdigest()
|
||||
|
||||
|
||||
def save(folder, name, value):
|
||||
fd = os.open(folder / name, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'w') as out:
|
||||
json.dump(value, out, indent=2)
|
||||
out.write('\n')
|
||||
out.flush()
|
||||
os.fsync(out.fileno())
|
||||
|
||||
|
||||
def prepare():
|
||||
require(sha(CASE / 'qualification.json') == QUALIFICATION_SHA, 'original build qualification')
|
||||
qualified = contract.protected_json(CASE / 'qualification.json')
|
||||
require(qualified['linux_clock_test_count'] == 5 and qualified['no_vm'] is True, 'five local portable guards only')
|
||||
original = CASE / 'target/debug/deps/clock_midl_readiness-277aee462cb4f674'
|
||||
raw = original.read_bytes()
|
||||
require(raw[:6] == b'\x7fELF\x02\x01' and raw[18:20] == b'\x3e\x00', 'existing x64 Linux test executable')
|
||||
os.umask(0o077)
|
||||
STATE.mkdir(mode=0o700, parents=True, exist_ok=True)
|
||||
folder = STATE / str(uuid.uuid4())
|
||||
folder.mkdir(mode=0o700)
|
||||
binary = folder / 'clock-portable-tests'
|
||||
shutil.copyfile(original, binary)
|
||||
binary.chmod(0o600)
|
||||
require(sha(binary) == sha(original), 'byte-identical existing test executable')
|
||||
for name, args in (
|
||||
('elf', ['readelf', '--wide', '--program-headers', '--dynamic', '--version-info', str(binary)]),
|
||||
('list', ['docker', 'run', '--rm', '--network', 'none', '--read-only', '-v', str(binary) + ':/tests:ro',
|
||||
'--entrypoint', '/lib64/ld-linux-x86-64.so.2', qualified['image'], '/tests', '--list', '--format', 'terse']),
|
||||
):
|
||||
completed = subprocess.run(args, capture_output=True, timeout=15)
|
||||
require(completed.returncode == 0, 'local loader/list metadata failed')
|
||||
fd = os.open(folder / (name + '.log'), os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600)
|
||||
with os.fdopen(fd, 'wb') as out:
|
||||
out.write(completed.stdout)
|
||||
out.flush()
|
||||
os.fsync(out.fileno())
|
||||
listed = (folder / 'list.log').read_bytes()
|
||||
require(set(re.findall(rb'^(.+): test$', listed, re.M)) == {name.encode() for name in TESTS}, 'exact existing five-test list')
|
||||
info = (folder / 'elf.log').read_text()
|
||||
needed = re.findall(r'\(NEEDED\).*?\[([^\]]+)\]', info)
|
||||
glibc = sorted(set(re.findall(r'GLIBC_[0-9.]+', info)), key=lambda v: tuple(map(int, v[6:].split('.'))))
|
||||
require('/lib64/ld-linux-x86-64.so.2' in info and needed and glibc, 'actual ELF loader dependencies')
|
||||
manifest = dict(schema_version=1, type='rmm.clock.portable-unit-native-pack', original_build=str(CASE),
|
||||
qualification_sha256=QUALIFICATION_SHA, runner_sha256=sha(__file__),
|
||||
support_sources=support_sources(), native_case_sha256=native_source_sha256(),
|
||||
artifact=dict(path=str(binary), bytes=binary.stat().st_size, sha256=sha(binary)),
|
||||
original_test_executable=str(original), selected_tests=list(TESTS),
|
||||
interpreter='/lib64/ld-linux-x86-64.so.2', needed=needed, maximum_glibc=glibc[-1],
|
||||
list_sha256=sha(folder / 'list.log'), elf_sha256=sha(folder / 'elf.log'),
|
||||
compiled_new_code=False, stripped=False, native_vm_executed=False,
|
||||
linux_ndr_tested=False, windows_rpc_tested=False, product_clock_qualified=False)
|
||||
save(folder, 'manifest.json', manifest)
|
||||
print(json.dumps(dict(manifest=str(folder / 'manifest.json'), manifest_sha256=sha(folder / 'manifest.json'),
|
||||
artifact=manifest['artifact'], interpreter=manifest['interpreter'], needed=needed,
|
||||
maximum_glibc=glibc[-1], selected_tests=5, native_vm_executed=False)))
|
||||
|
||||
|
||||
def verified(path):
|
||||
manifest = contract.protected_json(path)
|
||||
require(manifest['runner_sha256'] == sha(__file__) and manifest['selected_tests'] == list(TESTS), 'frozen native pack runner and exact tests')
|
||||
require(manifest['support_sources'] == support_sources() and manifest['native_case_sha256'] == native_source_sha256(), 'frozen native transport/parser sources')
|
||||
require(manifest['qualification_sha256'] == QUALIFICATION_SHA == sha(CASE / 'qualification.json'), 'actual original build')
|
||||
require(manifest['compiled_new_code'] is False and manifest['stripped'] is False and manifest['native_vm_executed'] is False, 'existing portable harness only')
|
||||
binary = Path(manifest['artifact']['path'])
|
||||
require(sha(binary) == manifest['artifact']['sha256'] and binary.stat().st_size == manifest['artifact']['bytes']
|
||||
and sha(manifest['original_test_executable']) == sha(binary), 'unchanged actual original Linux harness')
|
||||
return manifest
|
||||
|
||||
|
||||
def native_case(payload):
|
||||
import resource
|
||||
import stat
|
||||
import time
|
||||
root = Path('/var/lib/rmm-clock-midl-portable-tests')
|
||||
ident = str(uuid.UUID(payload['id']))
|
||||
names = [
|
||||
'clock::tests::gate_requires_all_status_and_uncertainty_evidence',
|
||||
'clock::tests::production_clock_stays_closed_until_native_adapter_qualification',
|
||||
'clock::tests::suspend_closes_until_actual_post_resume_sync',
|
||||
'clock::tests::timeout_worker_stays_closed_without_a_second_call',
|
||||
'clock::tests::wall_step_and_overflow_fail_closed',
|
||||
]
|
||||
require(payload['id'] == ident and payload['action'] in ('prepare', 'run', 'read')
|
||||
and payload['tests'] == names and os.geteuid() == 0, 'fixed exact portable test case')
|
||||
digest = payload['binary_sha256']
|
||||
require(len(digest) == 64 and all(c in '0123456789abcdef' for c in digest), 'fixed binary hash')
|
||||
def safe(path, directory):
|
||||
value = path.lstat()
|
||||
require(value.st_uid == 0 and not value.st_mode & 0o077 and not stat.S_ISLNK(value.st_mode)
|
||||
and (stat.S_ISDIR(value.st_mode) if directory else stat.S_ISREG(value.st_mode)), 'private fixed test path')
|
||||
def write(path, value):
|
||||
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'w') as out:
|
||||
json.dump(value, out)
|
||||
out.flush()
|
||||
os.fsync(out.fileno())
|
||||
if payload['action'] == 'prepare' and not root.exists():
|
||||
root.mkdir(mode=0o700)
|
||||
safe(root, True)
|
||||
case = root / ident
|
||||
if payload['action'] == 'prepare':
|
||||
case.mkdir(mode=0o700)
|
||||
write(case / 'intent.json', payload)
|
||||
return dict(prepared=True)
|
||||
safe(case, True)
|
||||
safe(case / 'intent.json', False)
|
||||
intent = json.loads((case / 'intent.json').read_bytes())
|
||||
require(all(intent[key] == payload[key] for key in ('id', 'binary_sha256', 'manifest_sha256', 'tests', 'runner_sha256', 'support_sources', 'native_case_sha256')), 'original create-once intent')
|
||||
if payload['action'] == 'read':
|
||||
safe(case / 'result.json', False)
|
||||
return json.loads((case / 'result.json').read_bytes())
|
||||
binary = case / 'clock-portable-tests'
|
||||
safe(binary, False)
|
||||
require(hashlib.sha256(binary.read_bytes()).hexdigest() == digest, 'actual staged original test bytes')
|
||||
write(case / 'started.json', dict(binary_sha256=digest, tests=names, never_repeat_start=True))
|
||||
binary.chmod(0o500)
|
||||
temporary = case / 'tmp'
|
||||
temporary.mkdir(mode=0o700)
|
||||
env = {'PATH': '/usr/bin:/bin', 'LANG': 'C.UTF-8', 'TMPDIR': str(temporary), 'RUST_BACKTRACE': '0'}
|
||||
namespace = subprocess.run(['/usr/bin/unshare', '--net', '--', '/usr/bin/true'], env=env, capture_output=True, timeout=3)
|
||||
require(namespace.returncode == 0, 'network namespace required; no fallback')
|
||||
loader = subprocess.run(['/lib64/ld-linux-x86-64.so.2', '--list', str(binary)], env=env, capture_output=True, timeout=3)
|
||||
require(loader.returncode == 0 and len(loader.stdout) <= 16384 and len(loader.stderr) <= 16384, 'actual native loader compatibility')
|
||||
def limits():
|
||||
resource.setrlimit(resource.RLIMIT_CORE, (0, 0))
|
||||
resource.setrlimit(resource.RLIMIT_FSIZE, (1048576, 1048576))
|
||||
resource.setrlimit(resource.RLIMIT_CPU, (10, 10))
|
||||
results = []
|
||||
deadline = time.monotonic() + 20
|
||||
for number, name in enumerate(names):
|
||||
output = case / (str(number) + '-stdout.bin')
|
||||
errors = case / (str(number) + '-stderr.bin')
|
||||
timed_out = False
|
||||
with output.open('xb') as stdout, errors.open('xb') as stderr:
|
||||
output.chmod(0o600)
|
||||
errors.chmod(0o600)
|
||||
try:
|
||||
remaining = deadline - time.monotonic()
|
||||
require(remaining > 0, 'aggregate test deadline')
|
||||
child = subprocess.run(['/usr/bin/unshare', '--net', '--', str(binary), '--exact', name, '--test-threads=1'],
|
||||
cwd=case, env=env, stdin=subprocess.DEVNULL, stdout=stdout, stderr=stderr,
|
||||
timeout=min(5, remaining), preexec_fn=limits)
|
||||
code = child.returncode
|
||||
except subprocess.TimeoutExpired:
|
||||
timed_out = True
|
||||
code = None
|
||||
stdout.flush()
|
||||
stderr.flush()
|
||||
os.fsync(stdout.fileno())
|
||||
os.fsync(stderr.fileno())
|
||||
raw, err = output.read_bytes(), errors.read_bytes()
|
||||
require(len(raw) <= 1048576 and len(err) <= 1048576, 'bounded unit output')
|
||||
results.append(dict(name=name, exit_code=code, timed_out=timed_out, stdout_b64=base64.b64encode(raw).decode(), stderr_b64=base64.b64encode(err).decode()))
|
||||
if code != 0:
|
||||
break
|
||||
report = dict(id=ident, binary_sha256=digest, manifest_sha256=payload['manifest_sha256'], results=results,
|
||||
native_platform='linux', network_namespace=True, environment_cleared=True,
|
||||
loader_stdout_sha256=hashlib.sha256(loader.stdout).hexdigest(), linux_ndr_tested=False,
|
||||
windows_rpc_tested=False, product_clock_qualified=False, rmm_jobs_created=False)
|
||||
write(case / 'result.json', report)
|
||||
return report
|
||||
|
||||
|
||||
def native(path, access_root, ident=None):
|
||||
manifest = verified(path)
|
||||
linux.ROOT = linux.access.ROOT = access_root.resolve()
|
||||
os.umask(0o077)
|
||||
if ident is None:
|
||||
ident = str(uuid.uuid4())
|
||||
folder = STATE / ident
|
||||
folder.mkdir(mode=0o700)
|
||||
payload = dict(id=ident, binary_sha256=manifest['artifact']['sha256'], manifest_sha256=sha(path), tests=list(TESTS),
|
||||
runner_sha256=manifest['runner_sha256'], support_sources=manifest['support_sources'], native_case_sha256=manifest['native_case_sha256'])
|
||||
save(folder, 'intent.json', dict(payload=payload, manifest_path=str(path.resolve())))
|
||||
linux.rpc('linux-vm.conf', native_case, dict(payload, action='prepare'))
|
||||
cfg = linux.access.load_config(access_root / 'credentials/linux-vm.conf')
|
||||
args = ['sftp', '-F', '/dev/null', '-P', cfg.get('port', '22'), '-o', 'StrictHostKeyChecking=yes', '-o', 'BatchMode=yes',
|
||||
'-o', 'IdentitiesOnly=yes', '-o', 'ForwardAgent=no', '-o', 'ConnectTimeout=10', '-i', str(Path(cfg['ssh_key_path']).expanduser()),
|
||||
'-b', '-', cfg['username'] + '@' + cfg['address']]
|
||||
result = subprocess.run(args, input=('put ' + json.dumps(manifest['artifact']['path']) + ' ' + json.dumps(REMOTE + '/' + ident + '/clock-portable-tests') + '\n').encode(), capture_output=True, timeout=120)
|
||||
save(folder, 'upload.json', dict(returncode=result.returncode))
|
||||
require(result.returncode == 0, 'fixed portable binary upload')
|
||||
save(folder, 'run-intent.json', dict(execute_only_exact_five_portable_units=True))
|
||||
value = linux.rpc('linux-vm.conf', native_case, dict(payload, action='run'))
|
||||
else:
|
||||
ident = str(uuid.UUID(ident))
|
||||
folder = STATE / ident
|
||||
intent = contract.protected_json(folder / 'intent.json')
|
||||
payload = intent['payload']
|
||||
require(intent['manifest_path'] == str(path.resolve()) and payload['manifest_sha256'] == sha(path), 'original same native case')
|
||||
require(payload['runner_sha256'] == manifest['runner_sha256'] and payload['support_sources'] == manifest['support_sources']
|
||||
and payload['native_case_sha256'] == manifest['native_case_sha256'], 'original same native sources')
|
||||
value = linux.rpc('linux-vm.conf', native_case, dict(payload, action='read'))
|
||||
save(folder, 'readback-' + str(uuid.uuid4()) + '.json', value)
|
||||
require(value['binary_sha256'] == manifest['artifact']['sha256'] and value['manifest_sha256'] == sha(path)
|
||||
and len(value['results']) == 5 and value['product_clock_qualified'] is False, 'all five exact portable results')
|
||||
for result, name in zip(value['results'], TESTS, strict=True):
|
||||
require(result['name'] == name and result['exit_code'] == 0 and result['timed_out'] is False
|
||||
and b'1 passed; 0 failed; 0 ignored;' in base64.b64decode(result['stdout_b64'], validate=True), 'one exact native portable unit passed')
|
||||
proof = dict(verified=True, case_id=ident, native_platform='linux', selected_tests=5, manifest_sha256=sha(path), report=value)
|
||||
if (folder / 'passed.json').exists():
|
||||
require(contract.protected_json(folder / 'passed.json') == proof, 'unchanged original native proof')
|
||||
else:
|
||||
save(folder, 'passed.json', proof)
|
||||
print(json.dumps(dict(verified=True, case_id=ident, selected_native_portable_tests=5, linux_ndr_tested=False, product_clock_qualified=False)))
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument('action', choices=('prepare-local', 'native', 'readback'))
|
||||
parser.add_argument('--manifest', type=Path)
|
||||
parser.add_argument('--access-root', type=Path)
|
||||
parser.add_argument('--case')
|
||||
parser.add_argument('--execute-native-tests', action='store_true')
|
||||
args = parser.parse_args()
|
||||
if args.action == 'prepare-local':
|
||||
require(not (args.manifest or args.access_root or args.case or args.execute_native_tests), 'local packaging only')
|
||||
prepare()
|
||||
elif args.action == 'native':
|
||||
require(args.manifest and args.access_root and args.execute_native_tests and not args.case, 'Root explicitly starts one fixed native case')
|
||||
native(args.manifest, args.access_root)
|
||||
else:
|
||||
require(args.manifest and args.access_root and args.case and not args.execute_native_tests, 'same-case readback only')
|
||||
native(args.manifest, args.access_root, args.case)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
@@ -0,0 +1,263 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Exact Microsoft MIDL codegen only; no installer, service, RPC or RMM job.
|
||||
|
||||
Root owns all target calls. Lost replies use readback of the same intent only.
|
||||
"""
|
||||
import argparse
|
||||
import hashlib
|
||||
import importlib
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import subprocess
|
||||
import sys
|
||||
import uuid
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
ACCESS = Path('/home/kevin/Schreibtisch/Coding/rust-discovery')
|
||||
STATE = ROOT / 'devstate/windows-clock-midl/native-cases'
|
||||
TOOLS = ROOT / 'devstate/windows-clock-midl/6b7fb9b1-0bb7-4880-850b-e6812d3df1b1/tools'
|
||||
IDL = ROOT / 'crates/agent-windows/native/w32time.idl'
|
||||
PINS = {'midl.exe': ('7a2a717be64f755390ad8b04cb034549b2606acec646ec7cbe44c67516ccba83', 154048),
|
||||
'midlc.exe': ('34293d7bd256f2d43311d197f13bbb8a1ae26c92cc1605d9f35dd3ddfe264b65', 1366464),
|
||||
'w32time.idl': ('37da4acb622daf9acf1aa6f1218ff9f52e5a2def14e58d8d9dd8da65078b3c6c', 2004)}
|
||||
|
||||
# A fixed Codegen job. JOB_LIST is installed before CreateProcessW; no
|
||||
# suspended-process/late-Assign fallback. Child descendants join this same job.
|
||||
CS = r'''
|
||||
using System;using System.IO;using System.Text;using System.Runtime.InteropServices;
|
||||
public static class RmmMidlJob {
|
||||
[StructLayout(LayoutKind.Sequential)]struct SA{public uint cb;public IntPtr sd;[MarshalAs(UnmanagedType.Bool)]public bool inherit;}
|
||||
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]struct SI{public uint cb;public string reserved,desktop,title;public uint x,y,xs,ys,xc,yc,fill,flags;public ushort show,reserved2;public IntPtr reservedp,input,output,error;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct SIX{public SI si;public IntPtr attrs;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct PI{public IntPtr process,thread;public uint pid,tid;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct BASIC{public long processTime,jobTime;public uint flags;public UIntPtr minimum,maximum;public uint active;public UIntPtr affinity;public uint priority,scheduling;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct IO{public ulong a,b,c,d,e,f;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct LIMIT{public BASIC basic;public IO io;public UIntPtr processMemory,jobMemory,peakProcess,peakJob;}
|
||||
[StructLayout(LayoutKind.Sequential)]struct ACCOUNT{public long a,b,c,d;public uint faults,total,active,terminated;}
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern IntPtr CreateJobObjectW(IntPtr sa,string name);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool SetInformationJobObject(IntPtr job,int kind,ref LIMIT value,uint len);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool QueryInformationJobObject(IntPtr job,int kind,out ACCOUNT value,uint len,IntPtr needed);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool TerminateJobObject(IntPtr job,uint code);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool InitializeProcThreadAttributeList(IntPtr list,int count,uint flags,ref UIntPtr size);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool UpdateProcThreadAttribute(IntPtr list,uint flags,UIntPtr attribute,IntPtr value,UIntPtr len,IntPtr old,IntPtr needed);
|
||||
[DllImport("kernel32.dll")]static extern void DeleteProcThreadAttributeList(IntPtr list);
|
||||
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)]static extern IntPtr CreateFileW(string path,uint access,uint share,ref SA sa,uint disposition,uint flags,IntPtr template);
|
||||
[DllImport("kernel32.dll",CharSet=CharSet.Unicode,SetLastError=true)]static extern bool CreateProcessW(string app,StringBuilder command,IntPtr pa,IntPtr ta,bool inherit,uint flags,IntPtr environment,string cwd,ref SIX start,out PI pi);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool IsProcessInJob(IntPtr process,IntPtr job,out bool member);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern uint ResumeThread(IntPtr thread);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern uint WaitForSingleObject(IntPtr handle,uint ms);
|
||||
[DllImport("kernel32.dll",SetLastError=true)]static extern bool GetExitCodeProcess(IntPtr handle,out uint code);
|
||||
[DllImport("kernel32.dll")]static extern bool CloseHandle(IntPtr handle);
|
||||
static void Need(bool ok,string stage){if(!ok)throw new InvalidOperationException(stage+"_"+Marshal.GetLastWin32Error());}
|
||||
static IntPtr Output(string path,ref SA sa){var h=CreateFileW(path,0x40000000,1,ref sa,1,0,IntPtr.Zero);Need(h!=new IntPtr(-1),"OUTPUT_CREATE");return h;}
|
||||
public sealed class Result {public uint pid,exit_code,active_processes;public bool timed_out,output_exceeded,atomic_job_assignment,all_own_children_exited;}
|
||||
public static Result Run(string folder){
|
||||
Need(IntPtr.Size==8&&Marshal.SizeOf(typeof(SI))==104&&Marshal.SizeOf(typeof(SIX))==112&&Marshal.SizeOf(typeof(LIMIT))==144,"X64_ABI");
|
||||
IntPtr job=IntPtr.Zero,attrs=IntPtr.Zero,jobValue=IntPtr.Zero,handleValue=IntPtr.Zero,input=IntPtr.Zero,output=IntPtr.Zero,error=IntPtr.Zero;bool initialized=false;PI pi=new PI();
|
||||
try{
|
||||
job=CreateJobObjectW(IntPtr.Zero,null);Need(job!=IntPtr.Zero,"JOB_CREATE");
|
||||
LIMIT limit=new LIMIT();limit.basic.flags=0x2000|8;limit.basic.active=2;Need(SetInformationJobObject(job,9,ref limit,(uint)Marshal.SizeOf(typeof(LIMIT))),"JOB_LIMITS");
|
||||
SA sa=new SA();sa.cb=(uint)Marshal.SizeOf(typeof(SA));sa.inherit=true;
|
||||
input=CreateFileW("NUL",0x80000000,3,ref sa,3,0,IntPtr.Zero);Need(input!=new IntPtr(-1),"INPUT_CREATE");
|
||||
output=Output(Path.Combine(folder,"stdout.txt"),ref sa);error=Output(Path.Combine(folder,"stderr.txt"),ref sa);
|
||||
UIntPtr bytes=UIntPtr.Zero;InitializeProcThreadAttributeList(IntPtr.Zero,2,0,ref bytes);Need(bytes.ToUInt64()>0&&bytes.ToUInt64()<65536,"ATTRIBUTE_SIZE");
|
||||
attrs=Marshal.AllocHGlobal((int)bytes.ToUInt64());Need(InitializeProcThreadAttributeList(attrs,2,0,ref bytes),"ATTRIBUTE_INIT");initialized=true;
|
||||
jobValue=Marshal.AllocHGlobal(IntPtr.Size);Marshal.WriteIntPtr(jobValue,job);Need(UpdateProcThreadAttribute(attrs,0,new UIntPtr(0x2000d),jobValue,new UIntPtr((uint)IntPtr.Size),IntPtr.Zero,IntPtr.Zero),"ATOMIC_JOB_LIST");
|
||||
handleValue=Marshal.AllocHGlobal(3*IntPtr.Size);Marshal.WriteIntPtr(handleValue,0,input);Marshal.WriteIntPtr(handleValue,IntPtr.Size,output);Marshal.WriteIntPtr(handleValue,2*IntPtr.Size,error);
|
||||
Need(UpdateProcThreadAttribute(attrs,0,new UIntPtr(0x20002),handleValue,new UIntPtr((uint)(3*IntPtr.Size)),IntPtr.Zero,IntPtr.Zero),"HANDLE_LIST");
|
||||
SIX si=new SIX();si.si.cb=(uint)Marshal.SizeOf(typeof(SIX));si.attrs=attrs;si.si.flags=0x100;si.si.input=input;si.si.output=output;si.si.error=error;
|
||||
string exe=Path.Combine(folder,"input","midl.exe");
|
||||
var cmd=new StringBuilder("\""+exe+"\" /nologo /no_cpp /env amd64 /Oicf /robust /Zp8 /protocol dce /client stub /server none /h w32time.h /cstub w32time_c.c \""+Path.Combine(folder,"input","w32time.idl")+"\"");
|
||||
Need(CreateProcessW(exe,cmd,IntPtr.Zero,IntPtr.Zero,true,0x80000|0x8000000|4,IntPtr.Zero,Path.Combine(folder,"output"),ref si,out pi),"CODEGEN_CREATE");
|
||||
bool member;Need(IsProcessInJob(pi.process,job,out member)&&member,"ATOMIC_JOB_MEMBERSHIP");Need(ResumeThread(pi.thread)!=0xffffffff,"CODEGEN_RESUME");
|
||||
Result result=new Result();result.pid=pi.pid;result.atomic_job_assignment=true;
|
||||
var began=System.Diagnostics.Stopwatch.StartNew();
|
||||
while(true){
|
||||
uint waited=WaitForSingleObject(pi.process,25);Need(waited==0||waited==258,"CODEGEN_WAIT");
|
||||
foreach(string path in new[]{Path.Combine(folder,"stdout.txt"),Path.Combine(folder,"stderr.txt"),Path.Combine(folder,"output","w32time.h"),Path.Combine(folder,"output","w32time_c.c")})if(File.Exists(path)&&new FileInfo(path).Length>262144)result.output_exceeded=true;
|
||||
if(waited==0)break;
|
||||
if(began.ElapsedMilliseconds>=10000||result.output_exceeded){result.timed_out=began.ElapsedMilliseconds>=10000;Need(TerminateJobObject(job,0xdead),"OWN_JOB_TERMINATE");break;}
|
||||
}
|
||||
// One total two-second observation budget, not one timeout per child.
|
||||
var closing=System.Diagnostics.Stopwatch.StartNew();ACCOUNT account;
|
||||
do{Need(QueryInformationJobObject(job,1,out account,(uint)Marshal.SizeOf(typeof(ACCOUNT)),IntPtr.Zero),"JOB_ACCOUNTING");if(account.active==0)break;System.Threading.Thread.Sleep(25);}while(closing.ElapsedMilliseconds<2000);
|
||||
result.active_processes=account.active;result.all_own_children_exited=account.active==0;
|
||||
Need(GetExitCodeProcess(pi.process,out result.exit_code),"CODEGEN_EXIT_CODE");return result;
|
||||
}finally{
|
||||
// Closing only this unnamed job kills any remaining own compiler child.
|
||||
if(job!=IntPtr.Zero)CloseHandle(job);
|
||||
if(pi.thread!=IntPtr.Zero)CloseHandle(pi.thread);if(pi.process!=IntPtr.Zero)CloseHandle(pi.process);
|
||||
if(initialized)DeleteProcThreadAttributeList(attrs);if(attrs!=IntPtr.Zero)Marshal.FreeHGlobal(attrs);if(jobValue!=IntPtr.Zero)Marshal.FreeHGlobal(jobValue);if(handleValue!=IntPtr.Zero)Marshal.FreeHGlobal(handleValue);
|
||||
foreach(var h in new[]{input,output,error})if(h!=IntPtr.Zero&&h!=new IntPtr(-1))CloseHandle(h);
|
||||
}
|
||||
}
|
||||
}
|
||||
'''
|
||||
|
||||
SCRIPT = r'''$ErrorActionPreference='Stop'
|
||||
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
if([guid]::ParseExact($p.id,'D').ToString()-cne$p.id-or$p.action-notin@('prepare','admit','start','readback')){throw 'Fixed codegen case'}
|
||||
foreach($n in @('runner_sha256','script_sha256')){if($p.$n-notmatch'^[a-f0-9]{64}$'){throw 'Source binding'}}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap';$folder=Join-Path $root ('midl-codegen-'+$p.id)
|
||||
$inputsDir=Join-Path $folder 'input';$output=Join-Path $folder 'output'
|
||||
$pins=@(@('midl.exe','7a2a717be64f755390ad8b04cb034549b2606acec646ec7cbe44c67516ccba83',154048),@('midlc.exe','34293d7bd256f2d43311d197f13bbb8a1ae26c92cc1605d9f35dd3ddfe264b65',1366464),@('w32time.idl','37da4acb622daf9acf1aa6f1218ff9f52e5a2def14e58d8d9dd8da65078b3c6c',2004))
|
||||
function Check($path,[bool]$directory){
|
||||
$item=Get-Item -LiteralPath $path -Force;if($item.PSIsContainer-ne$directory-or($item.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Codegen regular private path'}
|
||||
$parent=$item.Parent;if(!$directory){$parent=$item.Directory};while($null-ne$parent){if($parent.Attributes-band[IO.FileAttributes]::ReparsePoint){throw 'Codegen ancestor reparse'};$parent=$parent.Parent}
|
||||
$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')-or!$a.Access.Count){throw 'Codegen private owner'}
|
||||
foreach($r in $a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or[int64]$r.FileSystemRights-ne2032127){throw 'Codegen exact controller ACL'}}
|
||||
}
|
||||
function Protect($path){$a=New-Object Security.AccessControl.DirectorySecurity;$a.SetAccessRuleProtection($true,$false);$a.SetOwner((New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544')));foreach($sid in @('S-1-5-18','S-1-5-32-544')){$a.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($sid)),'FullControl','ContainerInherit,ObjectInherit','None','Allow')))};Set-Acl -LiteralPath $path -AclObject $a;Check $path $true}
|
||||
function NewRecord($name,$value){$path=Join-Path $folder $name;$bytes=[Text.Encoding]::UTF8.GetBytes(($value|ConvertTo-Json -Depth 16 -Compress));$f=[IO.File]::Open($path,'CreateNew','Write','Read');try{$f.Write($bytes,0,$bytes.Length);$f.Flush($true)}finally{$f.Dispose()};Check $path $false}
|
||||
function Hash($path){return(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()}
|
||||
function Intent{
|
||||
Check $folder $true;Check $inputsDir $true;Check $output $true;Check (Join-Path $folder 'intent.json') $false
|
||||
$i=Get-Content -LiteralPath (Join-Path $folder 'intent.json') -Raw|ConvertFrom-Json
|
||||
if($i.id-cne$p.id-or$i.runner_sha256-cne$p.runner_sha256-or$i.script_sha256-cne$p.script_sha256){throw 'Immutable codegen intent'}
|
||||
}
|
||||
function VerifyInputs{
|
||||
$info=@();foreach($pin in $pins){$path=Join-Path $inputsDir $pin[0];Check $path $false;if((Get-Item -LiteralPath $path).Length-ne$pin[2]-or(Hash $path)-cne$pin[1]){throw 'Fixed codegen input bytes'}
|
||||
if($pin[0]-like'*.exe'){
|
||||
$sig=Get-AuthenticodeSignature -LiteralPath $path;if($sig.Status-ne'Valid'-or!$sig.SignerCertificate-or$sig.SignerCertificate.Subject-notmatch'(^|,\s*)O=Microsoft Corporation(,|$)'){throw 'Valid Microsoft Authenticode required'}
|
||||
$chain=New-Object Security.Cryptography.X509Certificates.X509Chain;try{$chain.ChainPolicy.RevocationMode='Online';$chain.ChainPolicy.RevocationFlag='ExcludeRoot';$chain.ChainPolicy.VerificationFlags='NoFlag';$chain.ChainPolicy.UrlRetrievalTimeout=[TimeSpan]::FromSeconds(2);if(!$chain.Build($sig.SignerCertificate)){throw 'Microsoft chain validation required'}}finally{$chain.Dispose()}
|
||||
$v=(Get-Item -LiteralPath $path).VersionInfo;if($v.FileMajorPart-ne8-or$v.FileMinorPart-ne1-or$v.FileBuildPart-ne628-or$v.FilePrivatePart-ne1){throw 'Exact pinned compiler version'}
|
||||
$info+=,[ordered]@{name=$pin[0];sha256=$pin[1];authenticode_status='Valid';signer_thumbprint=$sig.SignerCertificate.Thumbprint;chain_valid=$true;file_version='8.1.628.1'}
|
||||
}
|
||||
};return ,$info
|
||||
}
|
||||
Check $root $true
|
||||
if($p.action-ceq'prepare'){
|
||||
if(Test-Path -LiteralPath $folder){throw 'Existing case: readback only'}
|
||||
New-Item -ItemType Directory -Path $folder|Out-Null;Protect $folder
|
||||
foreach($path in @($inputsDir,$output)){New-Item -ItemType Directory -Path $path|Out-Null;Protect $path}
|
||||
NewRecord 'intent.json' @{id=$p.id;runner_sha256=$p.runner_sha256;script_sha256=$p.script_sha256;inputs=$pins;no_jobs=$true;no_rpc=$true;no_installation=$true}
|
||||
@{prepared=$true;id=$p.id;tool_executed=$false}|ConvertTo-Json -Compress;return
|
||||
}
|
||||
Intent
|
||||
if($p.action-ceq'admit'){$tools=VerifyInputs;NewRecord 'admitted.json' @{id=$p.id;tools=$tools;source_sha256=$pins[2][1];tool_executed=$false};@{admitted=$true;id=$p.id;tools=$tools;tool_executed=$false}|ConvertTo-Json -Depth 8 -Compress;return}
|
||||
if($p.action-ceq'readback'){
|
||||
$report=$null;$reportPath=Join-Path $folder 'result.json';if(Test-Path -LiteralPath $reportPath){Check $reportPath $false;$report=Get-Content -LiteralPath $reportPath -Raw|ConvertFrom-Json}
|
||||
$files=@();foreach($pair in @(@('stdout.txt',$folder),@('stderr.txt',$folder),@('w32time.h',$output),@('w32time_c.c',$output))){$path=Join-Path $pair[1] $pair[0];if(Test-Path -LiteralPath $path){Check $path $false;$size=(Get-Item -LiteralPath $path).Length;if($size-gt262144){throw 'Codegen output bound'};$files+=,@{name=$pair[0];bytes=$size;sha256=(Hash $path);base64=[Convert]::ToBase64String([IO.File]::ReadAllBytes($path))}}}
|
||||
@{id=$p.id;readback_only=$true;started=(Test-Path -LiteralPath (Join-Path $folder 'started.json'));result=$report;files=$files}|ConvertTo-Json -Depth 16 -Compress;return
|
||||
}
|
||||
if(Test-Path -LiteralPath (Join-Path $folder 'started.json')){throw 'No compiler restart: readback only'}
|
||||
Check (Join-Path $folder 'admitted.json') $false
|
||||
$tools=VerifyInputs
|
||||
$held=@();try{
|
||||
foreach($pin in $pins){$held+=,[IO.File]::Open((Join-Path $inputsDir $pin[0]),'Open','Read','Read')}
|
||||
$null=VerifyInputs
|
||||
Add-Type -TypeDefinition @'
|
||||
__CS__
|
||||
'@
|
||||
NewRecord 'started.json' @{id=$p.id;single_start_reserved=$true;atomic_job_list_required=$true;no_rpc=$true;no_jobs=$true}
|
||||
$run=$null
|
||||
try{$run=[RmmMidlJob]::Run($folder);$ok=($run.exit_code-eq0-and!$run.timed_out-and!$run.output_exceeded-and$run.all_own_children_exited)
|
||||
foreach($name in @('w32time.h','w32time_c.c')){$path=Join-Path $output $name;Check $path $false;if((Get-Item -LiteralPath $path).Length-gt262144){$ok=$false}}
|
||||
$other=@(Get-ChildItem -LiteralPath $output -Force|Where-Object{$_.Name-notin@('w32time.h','w32time_c.c')});if($other.Count){$ok=$false}
|
||||
NewRecord 'result.json' @{id=$p.id;complete=$ok;run=$run;tools=$tools;no_jobs=$true;no_rpc=$true;no_installation=$true;clock_qualified=$false}
|
||||
}catch{NewRecord 'result.json' @{id=$p.id;complete=$false;run=$run;error_class=$_.Exception.GetType().Name;error_line=$_.InvocationInfo.ScriptLineNumber;no_retry=$true;clock_qualified=$false};throw}
|
||||
@{id=$p.id;complete=$ok;tool_executed=$true;clock_qualified=$false}|ConvertTo-Json -Compress
|
||||
}finally{foreach($f in $held){$f.Dispose()}}
|
||||
'''.replace('__CS__', CS)
|
||||
|
||||
|
||||
STAGE = r'''$ErrorActionPreference='Stop'
|
||||
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
if($p.sha256-notmatch'^[a-f0-9]{64}$'-or$p.bytes-le0-or$p.bytes-gt32768){throw 'Fixed codegen source'}
|
||||
$root='C:\ProgramData\RustDiscoveryBootstrap'
|
||||
function Check($path,[bool]$directory){$i=Get-Item -LiteralPath $path -Force;if($i.PSIsContainer-ne$directory-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)){throw 'Regular controller source'};$a=Get-Acl -LiteralPath $path;if($a.GetOwner([Security.Principal.SecurityIdentifier]).Value-notin@('S-1-5-18','S-1-5-32-544')-or!$a.Access.Count){throw 'Source owner'};foreach($r in $a.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])){if($r.AccessControlType-ne'Allow'-or$r.IdentityReference.Value-notin@('S-1-5-18','S-1-5-32-544')-or[int64]$r.FileSystemRights-ne2032127){throw 'Source controller ACL'}}}
|
||||
Check $root $true
|
||||
$dir=Join-Path $root $p.sha256;if(!(Test-Path -LiteralPath $dir)){New-Item -ItemType Directory -Path $dir|Out-Null};Check $dir $true
|
||||
$path=Join-Path $dir 'MidlCodegen.ps1';$exists=Test-Path -LiteralPath $path
|
||||
if($exists){Check $path $false;if((Get-Item -LiteralPath $path).Length-ne$p.bytes-or(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.sha256){throw 'Immutable codegen source'}}
|
||||
@{already_uploaded=$exists}|ConvertTo-Json -Compress
|
||||
'''
|
||||
LAUNCH = r'''$ErrorActionPreference='Stop'
|
||||
$p=([Console]::In.ReadLine()|ConvertFrom-Json)
|
||||
if($p.script_sha256-notmatch'^[a-f0-9]{64}$'){throw 'Fixed codegen source hash'}
|
||||
$file='C:\ProgramData\RustDiscoveryBootstrap\'+$p.script_sha256+'\MidlCodegen.ps1'
|
||||
$i=Get-Item -LiteralPath $file -Force;if($i.PSIsContainer-or($i.Attributes-band[IO.FileAttributes]::ReparsePoint)-or(Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash.ToLowerInvariant()-cne$p.script_sha256){throw 'Immutable codegen source file'}
|
||||
$json=$p|ConvertTo-Json -Depth 8 -Compress
|
||||
$raw=$json|& "$env:WINDIR\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy RemoteSigned -File $file
|
||||
if($LASTEXITCODE-ne0){throw 'Exact codegen phase failed; retain case and readback only'}
|
||||
$raw
|
||||
'''
|
||||
|
||||
|
||||
def upload(cb, local, remote, folder, label):
|
||||
prefix=cb.contract.protected_json(cb.ROOT/'credentials/windows-dev-access.json')['ssh_prefix']
|
||||
batch='put '+json.dumps(str(local.resolve()))+' '+json.dumps(remote)+'\n'
|
||||
result=subprocess.run(['sftp']+[v for v in prefix[1:-1] if v!='-T']+['-b','-',prefix[-1]],input=batch.encode(),capture_output=True,timeout=40)
|
||||
save(folder,label+'.transfer.json',{'returncode':result.returncode,'stdout_sha256':hashlib.sha256(result.stdout).hexdigest(),'stderr_sha256':hashlib.sha256(result.stderr).hexdigest()})
|
||||
if result.returncode:raise RuntimeError('EXACT_CODEGEN_UPLOAD_FAILED_READBACK_ONLY')
|
||||
|
||||
|
||||
def stage_source(cb, folder, source, digest):
|
||||
meta={'sha256':digest,'bytes':source.stat().st_size}
|
||||
state=cb.powershell(STAGE,meta,timeout=40,remote_signed=True)
|
||||
if not state['already_uploaded']:
|
||||
upload(cb,source,'/C:/ProgramData/RustDiscoveryBootstrap/'+digest+'/MidlCodegen.ps1',folder,'source')
|
||||
if not cb.powershell(STAGE,meta,timeout=40,remote_signed=True)['already_uploaded']:raise RuntimeError('EXACT_SOURCE_READBACK_FAILED')
|
||||
|
||||
|
||||
def sha(path):
|
||||
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
|
||||
|
||||
|
||||
def save(folder, name, value):
|
||||
fd = os.open(folder/name, os.O_WRONLY|os.O_CREAT|os.O_EXCL|os.O_NOFOLLOW, 0o600)
|
||||
with os.fdopen(fd, 'w') as out:
|
||||
json.dump(value, out, indent=2);out.flush();os.fsync(out.fileno())
|
||||
|
||||
|
||||
def transport(access_root):
|
||||
if access_root.resolve() != ACCESS:
|
||||
raise RuntimeError('EXACT_EXISTING_ACCESS_ROOT_REQUIRED')
|
||||
sys.path.insert(0, str(ACCESS/'deploy/windows'))
|
||||
return importlib.import_module('client_bootstrap')
|
||||
|
||||
|
||||
def main():
|
||||
p=argparse.ArgumentParser(description=__doc__);p.add_argument('action',choices=('prepare','start','readback'));p.add_argument('--id');p.add_argument('--access-root',type=Path,default=ACCESS);a=p.parse_args()
|
||||
os.umask(0o077);cb=transport(a.access_root)
|
||||
script_sha=hashlib.sha256(SCRIPT.encode()).hexdigest();runner_sha=sha(__file__)
|
||||
if a.action=='prepare':
|
||||
if a.id:raise RuntimeError('FRESH_CREATE_ONLY_CASE_REQUIRED')
|
||||
local={'midl.exe':TOOLS/'midl.exe','midlc.exe':TOOLS/'midlc.exe','w32time.idl':IDL}
|
||||
for name,path in local.items():
|
||||
if path.is_symlink() or not path.is_file() or (sha(path),path.stat().st_size)!=PINS[name]:raise RuntimeError('EXACT_FIXED_INPUTS_REQUIRED')
|
||||
STATE.mkdir(parents=True,exist_ok=True,mode=0o700);folder=STATE/str(uuid.uuid4());folder.mkdir(mode=0o700)
|
||||
payload={'id':folder.name,'runner_sha256':runner_sha,'script_sha256':script_sha}
|
||||
save(folder,'intent.json',{'payload':payload,'inputs':PINS,'transport_sha256':sha(cb.__file__),'no_rpc':True,'no_jobs':True})
|
||||
save(folder,'prepare-attempt.json',{'once':True})
|
||||
source=folder/'MidlCodegen.ps1'
|
||||
with source.open('xb') as out:out.write(SCRIPT.encode());out.flush();os.fsync(out.fileno())
|
||||
stage_source(cb,folder,source,script_sha)
|
||||
result=cb.powershell(LAUNCH,dict(payload,action='prepare'),timeout=40,remote_signed=True);save(folder,'remote-prepared.json',result)
|
||||
for name,path in local.items():
|
||||
upload(cb,path,'/C:/ProgramData/RustDiscoveryBootstrap/midl-codegen-'+folder.name+'/input/'+name,folder,name)
|
||||
admitted=cb.powershell(LAUNCH,dict(payload,action='admit'),timeout=60,remote_signed=True);save(folder,'admitted.json',admitted)
|
||||
print(json.dumps({'case_id':folder.name,'prepared':True,'tool_executed':False}));return
|
||||
if not a.id or str(uuid.UUID(a.id))!=a.id:raise RuntimeError('EXACT_EXISTING_CASE_REQUIRED')
|
||||
folder=STATE/a.id;intent=cb.contract.protected_json(folder/'intent.json');payload=intent['payload']
|
||||
if payload['id']!=a.id or payload['runner_sha256']!=runner_sha or payload['script_sha256']!=script_sha or intent['transport_sha256']!=sha(cb.__file__):raise RuntimeError('IMMUTABLE_RUNNER_AND_TRANSPORT_REQUIRED')
|
||||
if a.action=='start':
|
||||
cb.contract.protected_json(folder/'admitted.json');save(folder,'start-attempt.json',{'once':True,'lost_reply_readback_only':True})
|
||||
result=cb.powershell(LAUNCH,dict(payload,action=a.action),timeout=60,remote_signed=True)
|
||||
save(folder,('start-result.json' if a.action=='start' else 'readback-'+str(uuid.uuid4())+'.json'),result)
|
||||
if a.action=='readback':
|
||||
for item in result['files']:
|
||||
import base64
|
||||
if item['name'] not in ('stdout.txt','stderr.txt','w32time.h','w32time_c.c'):raise RuntimeError('FIXED_OUTPUT_NAME_REQUIRED')
|
||||
raw=base64.b64decode(item['base64'],validate=True)
|
||||
if len(raw)>262144 or len(raw)!=item['bytes'] or hashlib.sha256(raw).hexdigest()!=item['sha256']:raise RuntimeError('EXACT_OUTPUT_BYTES_REQUIRED')
|
||||
dest=folder/('native-'+item['name'])
|
||||
if dest.exists():
|
||||
if sha(dest)!=item['sha256']:raise RuntimeError('RETAINED_OUTPUT_CHANGED')
|
||||
else:
|
||||
with dest.open('xb') as out:out.write(raw);out.flush();os.fsync(out.fileno())
|
||||
report=result.get('result') or result
|
||||
print(json.dumps({'case_id':a.id,'complete':report.get('complete',False),'readback_only':a.action=='readback','clock_qualified':False}))
|
||||
|
||||
if __name__=='__main__':main()
|
||||
Reference in New Issue
Block a user