Qualify Windows IPC client runtime and prepare bounded native providers
Rust development slice / linux (push) Failing after 7s

This commit is contained in:
2026-11-14 03:53:48 +01:00
parent 2bf0183e68
commit 9d6a6f0eb6
24 changed files with 3880 additions and 30 deletions
+274
View File
@@ -0,0 +1,274 @@
#requires -Version 5.1
<#!
Native development build only. No tool installation, download, service change,
enrollment, RMM job, reboot, certificate-store or persistent policy modification.
Invoke this file in the authorized bootstrap process with RemoteSigned; GPO
precedence is retained. This is no policy exception for the RMM job executor.
Preflight creates a private receipt; Build requires its explicitly selected
source/tool hashes and creates a NEW private case. A failed case is never reused.
!#>
[CmdletBinding()]
param(
[ValidateSet('Preflight','Build')][string]$Mode='Preflight',
[Parameter(Mandatory=$true)][string]$SourceRoot,
[string]$EvidenceRoot=(Join-Path $env:USERPROFILE 'RmmNativeBuilds'),
[string]$ExpectedPreflight,
[string]$RustupExe,
[string]$RustupHome,
[string]$CargoHome,
[string]$ScriptFile=$PSCommandPath,
[ValidatePattern('^10\.0\.26100\.0$')][string]$SdkVersion='10.0.26100.0',
[ValidateRange(10,100)][int]$MinimumFreeGiB=20,
[Guid]$CaseId=([Guid]::NewGuid())
)
Set-StrictMode -Version Latest
$ErrorActionPreference='Stop'
$Target='x86_64-pc-windows-msvc'
$Toolchain='1.99.1-x86_64-pc-windows-msvc'
$case=$null
$report=[ordered]@{schema_version=1;case_id=$CaseId.ToString();mode=$Mode;completed=$false;stage='initial';
target=$Target;toolchain=$Toolchain;tool_installation=$false;services_changed=$false;reboot_requested=$false;
jobs_created=$false;clock_qualified=$false;unit_tests_executed=$false;abi_probe_executed=$false}
$savedEnvironment=@{}
function Require([bool]$Condition,[string]$Code) {if (-not $Condition) {throw $Code}}
function Hash([string]$Path) {return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()}
function No-Reparse([string]$Path) {
$item=Get-Item -LiteralPath $Path -Force
while ($null -ne $item) {
Require (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'BUILD_REPARSE_PATH'
if ($item -is [IO.FileInfo]) {$item=$item.Directory} else {$item=$item.Parent}
}
}
function Protected-BuildRoot([string]$Path) {
$acl=Get-Acl -LiteralPath $Path
$me=[Security.Principal.WindowsIdentity]::GetCurrent().User.Value
$trusted=@($me,'S-1-5-18','S-1-5-32-544')
Require ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'BUILD_ROOT_OWNER'
$write=[Security.AccessControl.FileSystemRights]::Write -bor [Security.AccessControl.FileSystemRights]::Delete -bor
[Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles -bor
[Security.AccessControl.FileSystemRights]::ChangePermissions -bor [Security.AccessControl.FileSystemRights]::TakeOwnership
foreach ($ace in $acl.Access) {
if ($ace.AccessControlType -eq 'Allow' -and ($ace.FileSystemRights -band $write) -ne 0) {
Require ($ace.IdentityReference.Translate([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'UNTRUSTED_BUILD_ROOT_WRITER'
}
}
}
function New-PrivateDirectory([string]$Path) {
Require (-not (Test-Path -LiteralPath $Path)) 'BUILD_CASE_ALREADY_EXISTS'
[void][IO.Directory]::CreateDirectory($Path)
$acl=New-Object Security.AccessControl.DirectorySecurity
$acl.SetAccessRuleProtection($true,$false)
$me=[Security.Principal.WindowsIdentity]::GetCurrent().User
$acl.SetOwner($me)
foreach ($sid in @($me.Value,'S-1-5-18','S-1-5-32-544') | Select-Object -Unique) {
$rule=New-Object Security.AccessControl.FileSystemAccessRule(
(New-Object Security.Principal.SecurityIdentifier($sid)),
[Security.AccessControl.FileSystemRights]::FullControl,
([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [Security.AccessControl.InheritanceFlags]::ObjectInherit),
[Security.AccessControl.PropagationFlags]::None,[Security.AccessControl.AccessControlType]::Allow)
$acl.AddAccessRule($rule)
}
Set-Acl -LiteralPath $Path -AclObject $acl
No-Reparse $Path
}
function Write-New([string]$Path,[string]$Value) {
$stream=[IO.File]::Open($Path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None)
try {$bytes=(New-Object Text.UTF8Encoding($false)).GetBytes($Value);$stream.Write($bytes,0,$bytes.Length);$stream.Flush($true)}
finally {$stream.Dispose()}
}
function Receipt([string]$Name) {Write-New (Join-Path $case $Name) ($report | ConvertTo-Json -Depth 50)}
function Set-BuildEnvironment([string]$Name,[string]$Value) {
if (-not $savedEnvironment.ContainsKey($Name)) {$savedEnvironment[$Name]=[Environment]::GetEnvironmentVariable($Name,'Process')}
[Environment]::SetEnvironmentVariable($Name,$Value,'Process')
}
function Microsoft-Tool([string]$Path) {
No-Reparse $Path
$sig=Get-AuthenticodeSignature -LiteralPath $Path
Require ($sig.Status -eq 'Valid' -and $null -ne $sig.SignerCertificate -and
$sig.SignerCertificate.Subject -match 'Microsoft Corporation') 'MICROSOFT_TOOL_SIGNATURE_INVALID'
return [ordered]@{path=$Path;sha256=(Hash $Path);signature_status=[string]$sig.Status;
signer_thumbprint=$sig.SignerCertificate.Thumbprint;file_version=(Get-Item -LiteralPath $Path).VersionInfo.FileVersion}
}
function Source-Files([string]$Root) {
$files=@(Get-Item -LiteralPath (Join-Path $Root 'Cargo.toml'),(Join-Path $Root 'Cargo.lock'),(Join-Path $Root 'rust-toolchain.toml'))
foreach ($name in @('crates','proto')) {
$files+=Get-ChildItem -LiteralPath (Join-Path $Root $name) -Recurse -File -Force |
Where-Object {$_.Extension -in @('.rs','.toml','.c','.h','.idl','.proto')}
}
$files+=Get-Item -LiteralPath (Join-Path $Root 'deploy/windows/msvc/clock_abi_probe.c'),(Join-Path $Root 'deploy/windows/Build-NativeMsvc.ps1')
$result=[ordered]@{}
foreach ($item in $files | Sort-Object FullName -Unique) {
No-Reparse $item.FullName
$relative=$item.FullName.Substring($Root.Length+1).Replace('\','/')
Require ($relative -notmatch '(^|/)(credentials|devstate|target|\.git|node_modules)(/|$)') 'NON_SOURCE_BUILD_INPUT'
$result[$relative]=Hash $item.FullName
}
return $result
}
function Same-Map($Expected,$Actual) {
$keys=@($Expected.PSObject.Properties | ForEach-Object {$_.Name})
if ($Expected -is [Collections.IDictionary]) {$keys=@($Expected.Keys)}
Require ($keys.Count -eq $Actual.Count) 'SOURCE_OR_TOOL_COUNT_CHANGED'
foreach ($key in $keys) {
$old=if ($Expected -is [Collections.IDictionary]) {$Expected[$key]} else {$Expected.$key}
Require ($Actual.Contains($key) -and $old -eq $Actual[$key]) 'SOURCE_OR_TOOL_HASH_CHANGED'
}
}
function Quoted([string]$Value) {
Require ($Value -notmatch '["\x00\r\n]') 'NATIVE_ARGUMENT_ENCODING'
return '"'+($Value -replace '(\\+)$','$1$1')+'"'
}
function Invoke-BuildTool([string]$Exe,[string[]]$Arguments,[string]$Label,[int]$Seconds=1800,[bool]$AllowFailure=$false) {
$out=Join-Path $case ($Label+'.stdout');$err=Join-Path $case ($Label+'.stderr')
$quoted=@($Arguments | ForEach-Object {Quoted $_}) -join ' '
$process=Start-Process -FilePath $Exe -ArgumentList $quoted -WorkingDirectory $case -PassThru -WindowStyle Hidden -RedirectStandardOutput $out -RedirectStandardError $err
Write-New (Join-Path $case ($Label+'.started.json')) (@{pid=$process.Id;executable_sha256=(Hash $Exe);label=$Label} | ConvertTo-Json)
if (-not $process.WaitForExit($Seconds*1000)) {
# A compiler/build-script timeout is retained for inspection. No blind
# retry, process-tree kill, service action or installation rollback.
throw 'NATIVE_BUILD_TIMEOUT_PROCESS_STATE_RETAINED'
}
$process.Refresh()
Write-New (Join-Path $case ($Label+'.result.json')) (@{exit_code=$process.ExitCode;stdout_sha256=(Hash $out);stderr_sha256=(Hash $err)} | ConvertTo-Json)
if (-not $AllowFailure) {Require ($process.ExitCode -eq 0) ('BUILD_TOOL_FAILED_'+$Label)}
return $out
}
try {
Require ($env:OS -eq 'Windows_NT' -and [Environment]::Is64BitProcess -and [Environment]::Is64BitOperatingSystem) 'NATIVE_WINDOWS_AMD64_REQUIRED'
Require ($SourceRoot -match '^[A-Za-z]:\\' -and $EvidenceRoot -match '^[A-Za-z]:\\') 'LOCAL_BUILD_DRIVE_REQUIRED'
$SourceRoot=(Get-Item -LiteralPath $SourceRoot).FullName.TrimEnd('\');No-Reparse $SourceRoot
Require ($ScriptFile -eq (Join-Path $SourceRoot 'deploy/windows/Build-NativeMsvc.ps1')) 'RUNNER_FROM_EXACT_SOURCE_BUNDLE_REQUIRED'
Require ($SourceRoot -notmatch '(?i)\\ProgramData\\RustDiscovery|\\Program Files\\RustDiscovery') 'NO_CLIENT_STATE_AS_SOURCE'
if (-not (Test-Path -LiteralPath $EvidenceRoot)) {[void][IO.Directory]::CreateDirectory($EvidenceRoot)}
$EvidenceRoot=(Get-Item -LiteralPath $EvidenceRoot).FullName.TrimEnd('\');No-Reparse $EvidenceRoot
Protected-BuildRoot $EvidenceRoot
Require (-not $EvidenceRoot.StartsWith($SourceRoot+'\',[StringComparison]::OrdinalIgnoreCase) -and
$EvidenceRoot -notmatch '(?i)\\ProgramData\\RustDiscovery|\\Program Files\\RustDiscovery') 'BUILD_EVIDENCE_OUTSIDE_SOURCE_AND_RUNTIME'
$case=Join-Path $EvidenceRoot $CaseId.ToString();New-PrivateDirectory $case
$report.source_root=$SourceRoot;$report.source_files=Source-Files $SourceRoot
Require ([IO.File]::ReadAllText((Join-Path $SourceRoot 'crates/agent-windows/src/clock.rs')) -match
'(?m)^pub const ADAPTER_QUALIFIED: bool = false;\r?$') 'CLOCK_GATE_MUST_REMAIN_CLOSED'
$report.runner_sha256=Hash $ScriptFile
$drive=New-Object IO.DriveInfo([IO.Path]::GetPathRoot($case));$report.free_bytes=$drive.AvailableFreeSpace
$missing=New-Object Collections.Generic.List[string]
if ($drive.AvailableFreeSpace -lt $MinimumFreeGiB*1GB) {$missing.Add('FREE_DISK_BELOW_BOUND')}
foreach ($name in @('CL','_CL_','LINK','_LINK_','RUSTFLAGS','CARGO_ENCODED_RUSTFLAGS','RUSTC_WRAPPER','RUSTC_WORKSPACE_WRAPPER')) {
Require ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name,'Process'))) 'UNEXPECTED_BUILD_FLAG_OR_WRAPPER'
}
foreach ($name in [Environment]::GetEnvironmentVariables('Process').Keys) {
if ($name -match '^(CFLAGS|CXXFLAGS|CPPFLAGS|LDFLAGS)(_|$)|^CARGO_(BUILD|TARGET_.+)_(RUSTFLAGS|RUSTC_WRAPPER|RUNNER)$') {
Require ([string]::IsNullOrEmpty([Environment]::GetEnvironmentVariable($name,'Process'))) 'UNEXPECTED_NATIVE_BUILD_OVERRIDE'
}
}
$tools=[ordered]@{};$hashes=[ordered]@{}
$tools.cmd=Microsoft-Tool (Join-Path ([Environment]::SystemDirectory) 'cmd.exe')
$vswhere=Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe'
if (-not (Test-Path -LiteralPath $vswhere)) {$missing.Add('VISUAL_STUDIO_BUILD_TOOLS')}
else {
$tools.vswhere=Microsoft-Tool $vswhere
$instances=@(& $vswhere -products '*' -version '[17.14,19.0)' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -latest -format json | ConvertFrom-Json)
Require ($LASTEXITCODE -eq 0) 'VSWHERE_QUERY_FAILED'
if ($instances.Count -ne 1) {$missing.Add('MSVC_AMD64_WORKLOAD')}
else {
$instance=$instances[0];$report.visual_studio_version=$instance.installationVersion
$batch=Join-Path $instance.installationPath 'VC/Auxiliary/Build/vcvarsall.bat';No-Reparse $batch
Require ($batch -notmatch '[&|<>^%"!\r\n]') 'DEVELOPER_ENVIRONMENT_PATH'
$report.vcvarsall_sha256=Hash $batch
$environment=@(& $tools.cmd.path /d /s /c ('""'+$batch+'" amd64 '+$SdkVersion+' >nul && set"'))
Require ($LASTEXITCODE -eq 0) 'MSVC_SDK_ENVIRONMENT_UNAVAILABLE'
$allowed=@('PATH','INCLUDE','LIB','LIBPATH','WindowsSdkDir','WindowsSDKVersion','VCToolsInstallDir','VSCMD_ARG_TGT_ARCH')
foreach ($line in $environment) {if ($line -match '^([^=]+)=(.*)$' -and $Matches[1] -in $allowed) {Set-BuildEnvironment $Matches[1] $Matches[2]}}
Require ($env:WindowsSDKVersion.TrimEnd('\') -eq $SdkVersion -and $env:VSCMD_ARG_TGT_ARCH -eq 'x64') 'EXACT_AMD64_SDK_ENVIRONMENT'
foreach ($name in @('cl','link','lib','dumpbin')) {
$path=Join-Path $env:VCToolsInstallDir ('bin/Hostx64/x64/'+$name+'.exe');$tools[$name]=Microsoft-Tool $path
}
$midl=Join-Path $env:WindowsSdkDir ('bin/'+$SdkVersion+'/x64/midl.exe');$tools.midl=Microsoft-Tool $midl
$report.sdk_version=$SdkVersion
}
}
$rustup=if ([string]::IsNullOrEmpty($RustupExe)) {Get-Command rustup.exe -CommandType Application -ErrorAction SilentlyContinue}
else {Require (-not [string]::IsNullOrEmpty($RustupHome) -and -not [string]::IsNullOrEmpty($CargoHome)) 'ISOLATED_RUST_HOMES_REQUIRED';
No-Reparse $RustupExe;Protected-BuildRoot $RustupHome;Protected-BuildRoot $CargoHome;
Set-BuildEnvironment 'RUSTUP_HOME' $RustupHome;Set-BuildEnvironment 'CARGO_HOME' $CargoHome;@{Source=(Get-Item -LiteralPath $RustupExe).FullName}}
$report.rustup_home=[Environment]::GetEnvironmentVariable('RUSTUP_HOME','Process')
$report.cargo_home=[Environment]::GetEnvironmentVariable('CARGO_HOME','Process')
if ($null -eq $rustup) {$missing.Add('RUSTUP_AND_PINNED_MSVC_TOOLCHAIN')}
else {
No-Reparse $rustup.Source;$tools.rustup=@{path=$rustup.Source;sha256=(Hash $rustup.Source)}
foreach ($name in @('rustc','cargo','rustdoc','clippy-driver','rustfmt')) {
$label='rustup-which-'+$name
$output=Invoke-BuildTool $rustup.Source @('which','--toolchain',$Toolchain,$name) $label 30 $true
$query=Get-Content -LiteralPath (Join-Path $case ($label+'.result.json')) -Raw | ConvertFrom-Json
$path=@(Get-Content -LiteralPath $output)
if ($query.exit_code -ne 0 -or $path.Count -ne 1 -or -not (Test-Path -LiteralPath $path[0])) {$missing.Add('PINNED_RUST_TOOL_'+$name);continue}
No-Reparse $path[0];$tools[$name]=@{path=$path[0];sha256=(Hash $path[0])}
}
if ($tools.Contains('rustc')) {
$version=@(Get-Content -LiteralPath (Invoke-BuildTool $tools.rustc.path @('-vV') 'rust-version' 30))
Require ('release: 1.99.1' -in $version -and 'host: x86_64-pc-windows-msvc' -in $version) 'PINNED_NATIVE_RUST_VERSION'
$report.rust_version=$version
}
}
foreach ($name in $tools.Keys) {$hashes[$name]=$tools[$name].sha256}
$report.tools=$tools;$report.tool_hashes=$hashes;$report.missing_tools=@($missing);$report.ready=($missing.Count -eq 0)
$report.stage='preflight';Receipt 'preflight.json'
if ($Mode -eq 'Preflight') {Write-Output ('PREFLIGHT ready='+$report.ready+' case='+$CaseId);exit $(if ($report.ready) {0} else {3})}
Require $report.ready 'NATIVE_BUILD_PREREQUISITES_MISSING'
Require (-not [string]::IsNullOrEmpty($ExpectedPreflight)) 'EXPLICIT_PREFLIGHT_RECEIPT_REQUIRED'
No-Reparse $ExpectedPreflight;$expected=Get-Content -LiteralPath $ExpectedPreflight -Raw | ConvertFrom-Json
Require ($expected.ready -eq $true -and $expected.target -eq $Target -and $expected.toolchain -eq $Toolchain -and
$expected.runner_sha256 -eq $report.runner_sha256 -and $expected.rustup_home -eq $report.rustup_home -and
$expected.cargo_home -eq $report.cargo_home) 'EXPECTED_PREFLIGHT_BINDING'
Same-Map $expected.source_files $report.source_files;Same-Map $expected.tool_hashes $hashes
$report.expected_preflight_sha256=Hash $ExpectedPreflight
$snapshot=Join-Path $case 'source';[void][IO.Directory]::CreateDirectory($snapshot)
foreach ($relative in $report.source_files.Keys) {
$destination=Join-Path $snapshot $relative;[void][IO.Directory]::CreateDirectory([IO.Path]::GetDirectoryName($destination))
[IO.File]::Copy((Join-Path $SourceRoot $relative),$destination,$false)
Require ((Hash $destination) -eq $report.source_files[$relative]) 'IMMUTABLE_SOURCE_COPY_CHANGED'
}
Same-Map $expected.source_files (Source-Files $SourceRoot)
Set-BuildEnvironment 'RUSTUP_TOOLCHAIN' $Toolchain;Set-BuildEnvironment 'RUSTC' $tools.rustc.path;Set-BuildEnvironment 'RUSTDOC' $tools.rustdoc.path
Set-BuildEnvironment 'CARGO_TARGET_DIR' (Join-Path $case 'target');Set-BuildEnvironment 'RMM_MIDL_EXE' $tools.midl.path
Set-BuildEnvironment 'CC_x86_64_pc_windows_msvc' $tools.cl.path;Set-BuildEnvironment 'AR_x86_64_pc_windows_msvc' $tools.lib.path
Set-BuildEnvironment 'CARGO_TARGET_X86_64_PC_WINDOWS_MSVC_LINKER' $tools.link.path
$abi=Join-Path $case 'abi';[void][IO.Directory]::CreateDirectory($abi)
[void](Invoke-BuildTool $tools.midl.path @('/nologo','/env','amd64','/Oicf','/robust','/Zp8','/client','stub','/server','none','/h','w32time.h','/cstub','w32time_c.c','/out',$abi,(Join-Path $snapshot 'crates/agent-windows/native/w32time.idl')) 'midl' 60)
$probe=Join-Path $abi 'clock_abi_probe.exe'
[void](Invoke-BuildTool $tools.cl.path @('/nologo','/std:c11','/Zp8','/W4','/WX','/TC',('/I'+$abi),(Join-Path $snapshot 'deploy/windows/msvc/clock_abi_probe.c'),('/Fo'+(Join-Path $abi 'clock_abi_probe.obj')),('/Fe'+$probe),'/link','rpcrt4.lib') 'abi-compile' 60)
$abiOutput=Invoke-BuildTool $probe @() 'abi-seh' 20
$abiResult=Get-Content -LiteralPath $abiOutput -Raw | ConvertFrom-Json
Require ($abiResult.amd64 -eq $true -and $abiResult.numeric_size -eq 64 -and $abiResult.wire_size -eq 120 -and
$abiResult.seh_caught -eq 1722 -and $abiResult.network_called -eq $false) 'NATIVE_MIDL_ABI_OR_SEH_FAILED'
$report.abi_seh_verified=$true;$report.abi_probe_executed=$true;$report.abi_probe_sha256=Hash $probe
$report.generated_rpc=@{header_sha256=(Hash (Join-Path $abi 'w32time.h'));client_sha256=(Hash (Join-Path $abi 'w32time_c.c'))}
$manifest=Join-Path $snapshot 'Cargo.toml'
[void](Invoke-BuildTool $tools.cargo.path @('clippy','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--all-targets','--','-D','warnings') 'clippy')
[void](Invoke-BuildTool $tools.cargo.path @('build','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--bins','--examples','--release') 'release')
[void](Invoke-BuildTool $tools.cargo.path @('test','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-windows','--features','system-helper','--lib','--no-run','--message-format=json') 'windows-unit-link')
[void](Invoke-BuildTool $tools.cargo.path @('test','--manifest-path',$manifest,'--locked','--offline','--target',$Target,'-p','rmm-agent-core','--tests','--no-run','--message-format=json') 'core-unit-link')
$artifacts=[ordered]@{}
foreach ($name in @('rmm-agent-windows','rmm-helper-windows')) {
$path=Join-Path $env:CARGO_TARGET_DIR ($Target+'/release/'+$name+'.exe')
$pe=[IO.File]::ReadAllBytes($path);$offset=[BitConverter]::ToInt32($pe,0x3c)
Require ($pe.Length -gt ($offset+24) -and [BitConverter]::ToUInt16($pe,0) -eq 0x5a4d -and [BitConverter]::ToUInt32($pe,$offset) -eq 0x4550 -and
[BitConverter]::ToUInt16($pe,$offset+4) -eq 0x8664) 'NATIVE_AMD64_PE_REQUIRED'
$dependencies=Invoke-BuildTool $tools.dumpbin.path @('/nologo','/dependents',$path) ($name+'-dependents') 30
$artifacts[$name]=@{path=$path;sha256=(Hash $path);size_bytes=$pe.Length;authenticode_status=[string](Get-AuthenticodeSignature -LiteralPath $path).Status;
dependents_sha256=(Hash $dependencies)}
}
Same-Map $expected.source_files (Source-Files $SourceRoot)
foreach ($name in $tools.Keys) {Require ((Hash $tools[$name].path) -eq $hashes[$name]) 'BUILD_TOOL_CHANGED_DURING_BUILD'}
$report.artifacts=$artifacts;$report.source_before_after_equal=$true;$report.completed=$true;$report.stage='built_not_deployed';Receipt 'result.json'
Write-Output ('PASS native MSVC source build '+$CaseId+'; no runtime installation, clock qualification or RMM job')
} catch {
$report.stage='incomplete';$report.error_class=$_.Exception.GetType().Name
$report.error_code=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'BUILD_SETUP_OR_TOOL_FAILED'}
if ($null -ne $case) {try {Receipt ('error-'+[Guid]::NewGuid().ToString()+'.json')} catch {}}
Write-Output ('NATIVE_MSVC_INCOMPLETE case='+$CaseId+'; private receipt retained; no automatic retry')
exit 2
} finally {
foreach ($name in $savedEnvironment.Keys) {[Environment]::SetEnvironmentVariable($name,$savedEnvironment[$name],'Process')}
}
+281
View File
@@ -0,0 +1,281 @@
#requires -Version 5.1
<#!
Explicit development-tool bootstrap; no RMM jobs, daemon changes, OS update
command, trust import, persistent execution-policy/PATH modification or reboot.
The already authorized development bootstrap uses process RemoteSigned only;
Machine/User/GPO scopes are never changed and GPOs retain precedence.
Prepare downloads pinned public inputs only. InstallVs/InstallRust each start
one fixed demand-only task; Read never retries. Worker result is durable before
the task exits. A lost reply, timeout, 3010 or failure never restarts installation.
!#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][ValidateSet('Prepare','InstallVs','InstallRust','Read','WorkerVs','WorkerRust')][string]$Phase,
[string]$RootDirectory=(Join-Path $env:USERPROFILE 'RmmNativeTools'),
[string]$CaseDirectory,
[Guid]$CaseId=([Guid]::NewGuid()),
[string]$ScriptFile=$PSCommandPath
)
Set-StrictMode -Version Latest
$ErrorActionPreference='Stop'
$VsVersion='18.11.12405.450'
$VsDirectory='C:\RmmBuildTools'
$Toolchain='1.99.1-x86_64-pc-windows-msvc'
$RustupSha256='38027d51d69a5d05493627a0634dbea52d6cd793c8f072d2373d3f4163eec7db'
$downloads=@(
@{name='vs_BuildTools.exe';sha256='386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4';max=16MB;
url='https://download.visualstudio.microsoft.com/download/pr/2d4073f5-2775-4a70-8c1d-b50ae62795a7/386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4/vs_BuildTools.exe'},
@{name='rustup-init.exe';sha256=$RustupSha256;max=32MB;
url='https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe'},
@{name='rustup-init.exe.sha256';sha256=$null;max=4096;
url='https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe.sha256'},
@{name='rust-toolchain.toml.sha256';sha256=$null;max=4096;
url='https://static.rust-lang.org/dist/channel-rust-1.99.1.toml.sha256'}
)
$case=$null
function Require([bool]$Value,[string]$Code) {if (-not $Value) {throw $Code}}
function Hash([string]$Path) {return (Get-FileHash -LiteralPath $Path -Algorithm SHA256).Hash.ToLowerInvariant()}
function Hash-Text([string]$Value) {
$sha=[Security.Cryptography.SHA256]::Create()
try {return ([BitConverter]::ToString($sha.ComputeHash([Text.Encoding]::UTF8.GetBytes($Value)))).Replace('-','').ToLowerInvariant()}
finally {$sha.Dispose()}
}
function No-Reparse([string]$Path) {
$item=Get-Item -LiteralPath $Path -Force
while ($null -ne $item) {
Require (($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -eq 0) 'SETUP_REPARSE_PATH'
if ($item -is [IO.FileInfo]) {$item=$item.Directory} else {$item=$item.Parent}
}
}
function Protected-Directory([string]$Path) {
No-Reparse $Path;$acl=Get-Acl -LiteralPath $Path
$trusted=@([Security.Principal.WindowsIdentity]::GetCurrent().User.Value,'S-1-5-18','S-1-5-32-544')
Require ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'SETUP_DIRECTORY_OWNER'
$write=[Security.AccessControl.FileSystemRights]::Write -bor [Security.AccessControl.FileSystemRights]::Delete -bor
[Security.AccessControl.FileSystemRights]::ChangePermissions -bor [Security.AccessControl.FileSystemRights]::TakeOwnership -bor
[Security.AccessControl.FileSystemRights]::DeleteSubdirectoriesAndFiles
foreach ($ace in $acl.Access) {
if ($ace.AccessControlType -eq 'Allow' -and ($ace.FileSystemRights -band $write) -ne 0) {
Require ($ace.IdentityReference.Translate([Security.Principal.SecurityIdentifier]).Value -in $trusted) 'UNTRUSTED_SETUP_DIRECTORY_WRITER'
}
}
}
function New-Private([string]$Path) {
Require (-not (Test-Path -LiteralPath $Path)) 'SETUP_CASE_ALREADY_EXISTS'
[void][IO.Directory]::CreateDirectory($Path)
$acl=New-Object Security.AccessControl.DirectorySecurity;$acl.SetAccessRuleProtection($true,$false)
$acl.SetOwner((New-Object Security.Principal.SecurityIdentifier('S-1-5-32-544')))
foreach ($sid in @('S-1-5-18','S-1-5-32-544')) {
$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule(
(New-Object Security.Principal.SecurityIdentifier($sid)),[Security.AccessControl.FileSystemRights]::FullControl,
([Security.AccessControl.InheritanceFlags]::ContainerInherit -bor [Security.AccessControl.InheritanceFlags]::ObjectInherit),
[Security.AccessControl.PropagationFlags]::None,[Security.AccessControl.AccessControlType]::Allow)))
}
Set-Acl -LiteralPath $Path -AclObject $acl;Protected-Directory $Path
}
function Write-New([string]$Path,$Value) {
$raw=(New-Object Text.UTF8Encoding($false)).GetBytes(($Value | ConvertTo-Json -Depth 30))
$stream=[IO.File]::Open($Path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None)
try {$stream.Write($raw,0,$raw.Length);$stream.Flush($true)} finally {$stream.Dispose()}
}
function Read-Json([string]$Name) {No-Reparse (Join-Path $case $Name);return Get-Content -LiteralPath (Join-Path $case $Name) -Raw | ConvertFrom-Json}
function Microsoft-Signature([string]$Path) {
No-Reparse $Path;$sig=Get-AuthenticodeSignature -LiteralPath $Path
Require ($sig.Status -eq 'Valid' -and $null -ne $sig.SignerCertificate -and $sig.SignerCertificate.Subject -match 'Microsoft Corporation') 'SETUP_MICROSOFT_SIGNATURE'
return @{status=[string]$sig.Status;thumbprint=$sig.SignerCertificate.Thumbprint;sha256=(Hash $Path)}
}
function Download($Item) {
$uri=[Uri]$Item.url;$redirects=0
while ($true) {
Require ($uri.Scheme -eq 'https' -and $uri.Host -in @('download.visualstudio.microsoft.com','static.rust-lang.org')) 'SETUP_DOWNLOAD_ORIGIN'
$request=[Net.HttpWebRequest]::Create($uri);$request.AllowAutoRedirect=$false;$request.UseDefaultCredentials=$false
$request.Timeout=30000;$request.ReadWriteTimeout=30000
$response=$request.GetResponse()
try {
if ([int]$response.StatusCode -in @(301,302,303,307,308)) {
Require ($redirects -lt 3) 'SETUP_DOWNLOAD_REDIRECT_LIMIT';$redirects++
$uri=New-Object Uri($uri,$response.Headers['Location']);continue
}
Require ([int]$response.StatusCode -eq 200) 'SETUP_DOWNLOAD_HTTP_STATUS'
$stream=[IO.File]::Open((Join-Path $case $Item.name),[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::None)
$inputStream=$response.GetResponseStream()
try {
$buffer=New-Object byte[] 65536;$size=0
while (($count=$inputStream.Read($buffer,0,$buffer.Length)) -gt 0) {$size+=$count;Require ($size -le $Item.max) 'SETUP_DOWNLOAD_SIZE';$stream.Write($buffer,0,$count)}
$stream.Flush($true)
} finally {$inputStream.Dispose();$stream.Dispose()}
Require ($null -eq $Item.sha256 -or (Hash (Join-Path $case $Item.name)) -eq $Item.sha256) 'SETUP_DOWNLOAD_HASH'
return @{name=$Item.name;url=$Item.url;final_url=$uri.AbsoluteUri;sha256=(Hash (Join-Path $case $Item.name));bytes=$size}
} finally {$response.Dispose()}
}
}
function Facts {
$os=Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion'
$policies=@(Get-ExecutionPolicy -List | Sort-Object Scope | ForEach-Object {$_.Scope.ToString()+':'+$_.ExecutionPolicy.ToString()})
$services=@{}
foreach ($name in @('W32Time','RmmDiscoveryWindows','RmmDiscoverySystemHelper')) {
$s=Get-CimInstance Win32_Service -Filter ("Name='"+$name+"'")
$services[$name]=if ($null -eq $s) {$null} else {@{state=$s.State;start_mode=$s.StartMode;pid=$s.ProcessId}}
}
$roots=@(Get-ChildItem Cert:\LocalMachine\Root | ForEach-Object {$_.Thumbprint} | Sort-Object)
$publishers=@(Get-ChildItem Cert:\LocalMachine\TrustedPublisher | ForEach-Object {$_.Thumbprint} | Sort-Object)
return @{boot_ticks=(Get-CimInstance Win32_OperatingSystem).LastBootUpTime.ToUniversalTime().Ticks;
os_build=[string]$os.CurrentBuildNumber;os_ubr=[int]$os.UBR;policies=$policies;services=$services;
root_trust_sha256=(Hash-Text ($roots -join ','));publisher_trust_sha256=(Hash-Text ($publishers -join ','));
machine_path_sha256=(Hash-Text ([Environment]::GetEnvironmentVariable('Path','Machine')));
reboot_pending=((Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Component Based Servicing\RebootPending') -or
(Test-Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'))}
}
function Stable-Facts($Before,$After) {
foreach ($key in @('boot_ticks','os_build','os_ubr','root_trust_sha256','publisher_trust_sha256','machine_path_sha256')) {Require ($Before.$key -eq $After.$key) ('SETUP_BOUNDARY_CHANGED_'+$key.ToUpperInvariant())}
Require (($Before.policies -join '|') -eq ($After.policies -join '|')) 'SETUP_EXECUTION_POLICY_CHANGED'
foreach ($name in @('W32Time','RmmDiscoveryWindows','RmmDiscoverySystemHelper')) {
$old=$Before.services.$name;$new=$After.services.$name
Require (($null -eq $old) -eq ($null -eq $new)) 'SETUP_SERVICE_PRESENCE_CHANGED'
if ($null -ne $old) {Require ($old.state -eq $new.state -and $old.start_mode -eq $new.start_mode -and $old.pid -eq $new.pid) 'SETUP_SERVICE_CHANGED'}
}
}
function Validate-Inputs($Prepared) {
Require ((Hash $ScriptFile) -eq $Prepared.script_sha256 -and $Prepared.vs_version -eq $VsVersion -and $Prepared.toolchain -eq $Toolchain) 'SETUP_SOURCE_PLAN_CHANGED'
foreach ($item in $Prepared.downloads) {Require ((Hash (Join-Path $case $item.name)) -eq $item.sha256) 'PREPARED_INSTALLER_CHANGED'}
$sig=Microsoft-Signature (Join-Path $case 'vs_BuildTools.exe')
Require ($sig.thumbprint -eq $Prepared.vs_signer.thumbprint) 'PREPARED_SIGNER_CHANGED'
}
function Verify-Vs {
$where=Join-Path ${env:ProgramFiles(x86)} 'Microsoft Visual Studio/Installer/vswhere.exe'
[void](Microsoft-Signature $where)
$instances=@(& $where -products Microsoft.VisualStudio.Product.BuildTools -version '[18.11,18.12)' -requires Microsoft.VisualStudio.Component.VC.Tools.x86.x64 -format json | ConvertFrom-Json)
Require ($LASTEXITCODE -eq 0 -and $instances.Count -eq 1 -and $instances[0].installationVersion -eq $VsVersion -and $instances[0].installationPath -eq $VsDirectory) 'INSTALLED_VS_VERSION_OR_TARGET'
$sdk=Join-Path ${env:ProgramFiles(x86)} 'Windows Kits/10/bin/10.0.26100.0/x64/midl.exe'
return @{version=$instances[0].installationVersion;path=$VsDirectory;sdk_midl=(Microsoft-Signature $sdk)}
}
function Worker([string]$Kind,$Prepared) {
Require ([Security.Principal.WindowsIdentity]::GetCurrent().User.Value -eq 'S-1-5-18') 'SETUP_FIXED_WORKER_SYSTEM_ACCOUNT'
$intent=Read-Json ($Kind+'-start-intent.json')
Require ($intent.prepared_sha256 -eq (Hash (Join-Path $case 'prepared.json'))) 'SETUP_FIXED_ONCE_INTENT'
Write-New (Join-Path $case ($Kind+'-worker-once.json')) @{kind=$Kind;pid=$PID;prepared_sha256=$intent.prepared_sha256}
$result=[ordered]@{kind=$Kind;completed=$false;reboot_required=$false;stage='worker_entered';rmm_jobs_created=$false;reboot_requested=$false}
$saved=@{}
try {
Validate-Inputs $Prepared;$before=Facts;Stable-Facts $Prepared.before $before
Require (-not $before.reboot_pending) 'SETUP_PREEXISTING_REBOOT_PENDING'
if ($Kind -eq 'vs') {
Require (-not (Test-Path -LiteralPath $VsDirectory)) 'EXISTING_VS_TARGET_NO_RETRY'
$exe=Join-Path $case 'vs_BuildTools.exe'
# Standard Microsoft acquisition/verification only. No custom
# catalog, channel, checksum override or signature-bypass option.
$args=@('--wait','--quiet','--norestart','--noUpdateInstaller','--installPath',$VsDirectory,
'--add','Microsoft.VisualStudio.Workload.VCTools','--add','Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
'--add','Microsoft.VisualStudio.Component.Windows11SDK.26100')
} else {
$vs=Read-Json 'vs-result.json';Require ($vs.completed -eq $true -and $vs.reboot_required -eq $false) 'VS_MUST_COMPLETE_WITHOUT_REBOOT_FIRST'
[void](Verify-Vs)
foreach ($name in @('RUSTUP_HOME','CARGO_HOME')) {
$saved[$name]=[Environment]::GetEnvironmentVariable($name,'Process')
$path=Join-Path $case $name.ToLowerInvariant();Require (-not (Test-Path -LiteralPath $path)) 'EXISTING_RUST_HOME_NO_RETRY'
[Environment]::SetEnvironmentVariable($name,$path,'Process')
}
foreach ($pair in @(@('RUSTUP_DIST_SERVER','https://static.rust-lang.org'),@('RUSTUP_UPDATE_ROOT','https://static.rust-lang.org/rustup'))) {
$saved[$pair[0]]=[Environment]::GetEnvironmentVariable($pair[0],'Process')
[Environment]::SetEnvironmentVariable($pair[0],$pair[1],'Process')
}
$exe=Join-Path $case 'rustup-init.exe'
$args=@('-y','--no-modify-path','--no-update-default-toolchain','--profile','minimal',
'--default-host','x86_64-pc-windows-msvc','--default-toolchain','1.99.1','--component','clippy,rustfmt')
}
$arguments=@($args | ForEach-Object {Require ($_ -notmatch '["\r\n\x00]') 'SETUP_ARGUMENT_ENCODING';'"'+($_ -replace '(\\+)$','$1$1')+'"'}) -join ' '
$process=Start-Process -FilePath $exe -ArgumentList $arguments -PassThru -WindowStyle Hidden -RedirectStandardOutput (Join-Path $case ($Kind+'.stdout')) -RedirectStandardError (Join-Path $case ($Kind+'.stderr'))
Write-New (Join-Path $case ($Kind+'-installer-process.json')) @{pid=$process.Id;start_ticks=$process.StartTime.ToUniversalTime().Ticks;exe_sha256=(Hash $exe)}
Require ($process.WaitForExit(3600000)) 'SETUP_TIMEOUT_EFFECT_UNKNOWN_NO_RETRY';$process.Refresh();$result.exit_code=$process.ExitCode
$result.reboot_required=($process.ExitCode -in @(3010,1641))
$after=Facts;$result.after=$after;$result.reboot_required=($result.reboot_required -or $after.reboot_pending);Stable-Facts $Prepared.before $after
if ($process.ExitCode -eq 3010 -or $after.reboot_pending) {$result.stage='blocked_reboot_required';$result.reboot_required=$true}
elseif ($process.ExitCode -eq 1641) {$result.stage='unexpected_reboot_initiated';$result.reboot_required=$true}
else {
Require ($process.ExitCode -eq 0) 'SETUP_INSTALLER_FAILED_NO_RETRY'
if ($Kind -eq 'vs') {$result.installed=Verify-Vs}
else {
$rustup=Join-Path $env:CARGO_HOME 'bin/rustup.exe';$rustc=@(& $rustup which --toolchain $Toolchain rustc)
Require ($LASTEXITCODE -eq 0 -and $rustc.Count -eq 1) 'PINNED_RUSTC_MISSING'
$version=@(& $rustc[0] -vV)
Require ($LASTEXITCODE -eq 0 -and 'release: 1.99.1' -in $version -and 'host: x86_64-pc-windows-msvc' -in $version) 'INSTALLED_RUST_VERSION_OR_HOST'
$result.installed=@{rustup_exe=$rustup;rustup_sha256=(Hash $rustup);rustc_sha256=(Hash $rustc[0]);rustup_home=$env:RUSTUP_HOME;cargo_home=$env:CARGO_HOME;version=$version}
}
$result.stage='installed_verified';$result.completed=$true
}
} catch {
$result.error_class=$_.Exception.GetType().Name;$result.error_code=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'SETUP_WORKER_FAILED'}
} finally {
foreach ($name in $saved.Keys) {[Environment]::SetEnvironmentVariable($name,$saved[$name],'Process')}
Write-New (Join-Path $case ($Kind+'-result.json')) $result
Disable-ScheduledTask -TaskName ('RmmNativeTools-'+$Kind+'-'+$Prepared.case_id) -ErrorAction SilentlyContinue | Out-Null
}
}
try {
Require ($env:OS -eq 'Windows_NT' -and [Environment]::Is64BitProcess) 'SETUP_NATIVE_WINDOWS_X64'
Require ((Get-ExecutionPolicy -Scope Process) -eq 'RemoteSigned') 'AUTHORIZED_BOOTSTRAP_PROCESS_REMOTESIGNED_REQUIRED'
$principal=New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
Require ($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) 'SETUP_ADMIN_REQUIRED'
Require (-not [string]::IsNullOrEmpty($ScriptFile)) 'EXPLICIT_PINNED_SCRIPT_FILE_REQUIRED'
$ScriptFile=(Get-Item -LiteralPath $ScriptFile).FullName;No-Reparse $ScriptFile
if ($Phase -eq 'Prepare') {
Require ($RootDirectory -match '^[A-Za-z]:\\') 'LOCAL_TOOL_ROOT_REQUIRED'
if (-not (Test-Path -LiteralPath $RootDirectory)) {[void][IO.Directory]::CreateDirectory($RootDirectory)}
Protected-Directory $RootDirectory;$case=Join-Path $RootDirectory $CaseId.ToString();New-Private $case
Require (-not (Test-Path -LiteralPath $VsDirectory)) 'EXISTING_VS_TARGET'
$before=Facts;Require (-not $before.reboot_pending) 'SETUP_PREEXISTING_REBOOT_PENDING'
$drive=New-Object IO.DriveInfo([IO.Path]::GetPathRoot($case));Require ($drive.AvailableFreeSpace -ge 30GB) 'SETUP_DISK_BELOW_30GIB'
$savedTls=[Net.ServicePointManager]::SecurityProtocol
try {[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;$downloaded=@($downloads | ForEach-Object {Download $_})}
finally {[Net.ServicePointManager]::SecurityProtocol=$savedTls}
$signer=Microsoft-Signature (Join-Path $case 'vs_BuildTools.exe')
$version=(Get-Item -LiteralPath (Join-Path $case 'vs_BuildTools.exe')).VersionInfo
Require ($version.ProductVersion -eq '18.11.4 LTSC' -and $version.FileVersion -eq $VsVersion) 'PINNED_VS_BOOTSTRAPPER_VERSION'
Require ((Get-Content -LiteralPath (Join-Path $case 'rustup-init.exe.sha256') -Raw).Trim().Split(' ')[0] -eq $RustupSha256) 'RUSTUP_OFFICIAL_CHECKSUM'
Require ((Get-Content -LiteralPath (Join-Path $case 'rust-toolchain.toml.sha256') -Raw).Trim().Split(' ')[0] -eq 'fb152be80975c194aa5fc684b2aec3a6e14e2b855efe2faa4e5f8ae6e590f463') 'RUST_TOOLCHAIN_DISTRIBUTION_PIN'
[IO.File]::Copy($ScriptFile,(Join-Path $case 'Install-NativeBuildTools.ps1'),$false)
$prepared=@{schema_version=1;case_id=$CaseId.ToString();script_sha256=(Hash $ScriptFile);before=$before;downloads=$downloaded;
vs_signer=$signer;vs_version=$VsVersion;toolchain=$Toolchain;free_bytes=$drive.AvailableFreeSpace;installers_executed=$false}
Write-New (Join-Path $case 'prepared.json') $prepared
Write-Output ('PREPARED '+$case+'; downloads verified, no installation');exit 0
}
Require (-not [string]::IsNullOrEmpty($CaseDirectory)) 'EXPLICIT_RETAINED_CASE_REQUIRED'
$case=(Get-Item -LiteralPath $CaseDirectory).FullName;Protected-Directory $case;$prepared=Read-Json 'prepared.json'
Require (([Guid]$prepared.case_id).ToString() -eq (Split-Path $case -Leaf)) 'SAME_PREPARED_CASE'
Validate-Inputs $prepared
if ($Phase -in @('WorkerVs','WorkerRust')) {Worker $(if ($Phase -eq 'WorkerVs') {'vs'} else {'rust'}) $prepared;exit 0}
if ($Phase -eq 'Read') {
$results=@{}
foreach ($kind in @('vs','rust')) {
$name=$kind+'-result.json';$task=Get-ScheduledTask -TaskName ('RmmNativeTools-'+$kind+'-'+$prepared.case_id) -ErrorAction SilentlyContinue
$results[$kind]=@{result=$(if (Test-Path -LiteralPath (Join-Path $case $name)) {Read-Json $name} else {$null});task_state=$(if ($null -eq $task) {'absent'} else {[string]$task.State})}
}
$facts=Facts;$stable=$true;$boundaryError=$null
try {Stable-Facts $prepared.before $facts} catch {$stable=$false;$boundaryError='BOOTSTRAP_BOUNDARY_CHANGED'}
Write-Output (@{case_id=$prepared.case_id;results=$results;facts=$facts;read_only=$true;boundary_verified=$stable;boundary_error=$boundaryError} | ConvertTo-Json -Depth 30);exit 0
}
$kind=if ($Phase -eq 'InstallVs') {'vs'} else {'rust'}
$before=Facts;Stable-Facts $prepared.before $before;Require (-not $before.reboot_pending) 'SETUP_REBOOT_PENDING'
if ($kind -eq 'rust') {$vs=Read-Json 'vs-result.json';Require ($vs.completed -eq $true -and $vs.reboot_required -eq $false) 'VS_FIRST_NO_REBOOT'}
$intentPath=Join-Path $case ($kind+'-start-intent.json')
Write-New $intentPath @{kind=$kind;prepared_sha256=(Hash (Join-Path $case 'prepared.json'));script_sha256=$prepared.script_sha256}
$taskName='RmmNativeTools-'+$kind+'-'+$prepared.case_id
Require ($null -eq (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)) 'EXISTING_TOOL_TASK_NO_RETRY'
$workerFile=Join-Path $case 'Install-NativeBuildTools.ps1';$quotedFile=$workerFile.Replace("'","''");$quotedCase=$case.Replace("'","''")
$workerPhase=if ($kind -eq 'vs') {'WorkerVs'} else {'WorkerRust'}
$command="`$p='$quotedFile';if((Get-FileHash -LiteralPath `$p -Algorithm SHA256).Hash.ToLowerInvariant() -ne '$($prepared.script_sha256)'){throw 'WORKER_HASH'};& `$p -Phase $workerPhase -CaseDirectory '$quotedCase'"
$encoded=[Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($command))
$powershell=Join-Path ([Environment]::SystemDirectory) 'WindowsPowerShell/v1.0/powershell.exe';[void](Microsoft-Signature $powershell)
$action=New-ScheduledTaskAction -Execute $powershell -Argument ('-NoProfile -NonInteractive -ExecutionPolicy RemoteSigned -EncodedCommand '+$encoded)
$settings=New-ScheduledTaskSettingsSet -MultipleInstances IgnoreNew -RestartCount 0 -ExecutionTimeLimit (New-TimeSpan -Minutes 90) -DisallowHardTerminate -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries
$user=New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType ServiceAccount -RunLevel Highest
Register-ScheduledTask -TaskName $taskName -Action $action -Principal $user -Settings $settings | Out-Null
# Deliberately no trigger or automatic restart: exactly this one demand call.
Start-ScheduledTask -TaskName $taskName
Write-New (Join-Path $case ($kind+'-task-started.json')) @{task_name=$taskName;source_sha256=$prepared.script_sha256;demand_only=$true;automatic_retry=$false}
Write-Output ('STARTED '+$kind+'; read retained case, never repeat install')
} catch {
$errorCode=if ($_.Exception.Message -match '^[A-Z0-9_]{1,96}$') {$_.Exception.Message} else {'NATIVE_TOOL_BOOTSTRAP_FAILED'}
if ($null -ne $case) {try {Write-New (Join-Path $case ('bootstrap-error-'+[Guid]::NewGuid().ToString()+'.json')) @{phase=$Phase;error_class=$_.Exception.GetType().Name;error_code=$errorCode}} catch {}}
Write-Output ('NATIVE_TOOL_SETUP_INCOMPLETE '+$errorCode+'; retain case and readback, no automatic retry');exit 2
}
+2 -2
View File
@@ -45,7 +45,7 @@ def powershell(script, payload=None, timeout=60, remote_signed=False):
UPLOAD = r'''
$ErrorActionPreference='Stop'
$p=([Console]::In.ReadLine() | ConvertFrom-Json)
if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'}
if ($p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|(Install-PassiveClient|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'}
$root='C:\ProgramData\RustDiscoveryBootstrap'
if (!(Test-Path -LiteralPath $root)) {
New-Item -ItemType Directory -Path $root | Out-Null
@@ -68,7 +68,7 @@ if (Test-Path -LiteralPath $path) {throw 'Artifact already exists'}
VERIFY_UPLOAD = r'''
$ErrorActionPreference='Stop'
$p=([Console]::In.ReadLine() | ConvertFrom-Json)
if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|Install-PassiveClient\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'}
if ($p.sha256 -notmatch '^[0-9a-f]{64}$' -or $p.name -notmatch '^((rmm-agent-windows|windows-tests-(lib|bin|acl)|windows-core-(contracts|reconcile|helper-reconcile))\.exe|(Install-PassiveClient|Build-MsiFixture|Build-NativeMsvc|Install-NativeBuildTools)\.ps1|PassiveStateProbe\.exe|WindowsTlsProbe\.exe|RuntimeStateProbe\.exe)$') {throw 'Artifact allowlist'}
$path='C:\ProgramData\RustDiscoveryBootstrap\'+$p.sha256+'\'+$p.name
$hash=(Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()
if ($hash -cne $p.sha256) {throw 'Artifact hash'}
+222
View File
@@ -0,0 +1,222 @@
# Native Windows MSVC build
Prepared 2026-11-14; **not executed on the VM**. No compiler installation,
Windows update, restart, trust-store change, daemon replacement or RMM job was
performed by this preparation. Existing Cloud identities/journals are untouched.
`Build-NativeMsvc.ps1` is a development build runner, not an installer. Run it in
64-bit Windows PowerShell5.1 from a source bundle containing the exact repository
sources and this script. `Preflight` inventories installed tools and records
private source/tool hashes. `Build` requires an explicitly selected successful
preflight receipt, creates a different private case, and builds an isolated
source copy. It never reuses a partial build case.
## Required native tools
- Rust `1.99.1-x86_64-pc-windows-msvc`, including `cargo`, `rustc`, `rustdoc`,
`clippy-driver`, `rustfmt`, supplied by the official rustup installation.
- Installed Visual Studio C++ tools in the selected range `[17.14,19.0)`;
VS2022 or VS2026 is selected by its signed `vswhere.exe`. The actual product,
compiler version, signer thumbprints and tool hashes are recorded. The current
official [VS release history](https://learn.microsoft.com/en-us/visualstudio/releases/2026/release-history)
includes VS2026; the runner does not download a mutable latest bootstrapper.
- The x64 `cl.exe`, `link.exe`, `lib.exe`, `dumpbin.exe`, and Windows SDK
`10.0.26100.0` x64 `midl.exe`. Microsoft signatures must be valid. The selected
SDK is explicit through `vcvarsall.bat amd64 10.0.26100.0`; see Microsoft's
[native build environment](https://learn.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=msvc-170).
- At least20GiB available on the evidence/build volume by default. This is the
runner's conservative admission threshold, not a measured installation size.
- The locked Cargo dependencies must already be available in the build user's
registry cache. Builds are `--locked --offline`. A missing crate is a failed
prerequisite, never a reason for an automatic online retry. An explicit
development preparation can populate the public dependencies with
`cargo +1.99.1-x86_64-pc-windows-msvc fetch --locked --target x86_64-pc-windows-msvc`.
Do not transfer an entire Cargo home containing credentials or configuration.
The parent's actual read-only VM report
`devstate/windows-client/native-msvc-tools-readonly-20261114.json` now records
103300169728 free bytes, x64, no `vswhere`, no `rustup`, no SDK directories,
W32Time stopped and all execution-policy scopes undefined. No installation was
performed by this preparation. No extra private key, server token or production
signing certificate is needed for source build.
[Rust's MSVC target contract](https://doc.rust-lang.org/rustc/platform-support/windows-msvc.html)
defines native Windows support, Microsoft calling conventions and PE/COFF output.
## Explicit tool-setup plan, not executed
If preflight reports missing C++ tools, first select a **fixed-version** Build
Tools bootstrapper from Microsoft's release history, retain its download URL,
SHA-256, ProductVersion, valid Microsoft Authenticode signature and thumbprint.
Verify the intended absent/new install directory and sufficient disk before
starting setup. Reuse a suitable existing installation rather than updating it.
The exact required component IDs are:
```text
Microsoft.VisualStudio.Workload.VCTools
Microsoft.VisualStudio.Component.VC.Tools.x86.x64
Microsoft.VisualStudio.Component.Windows11SDK.26100
```
The planned bootstrapper arguments are:
```text
--wait --passive --norestart --installPath C:\RmmBuildTools
--add Microsoft.VisualStudio.Workload.VCTools
--add Microsoft.VisualStudio.Component.VC.Tools.x86.x64
--add Microsoft.VisualStudio.Component.Windows11SDK.26100
```
There is no `--force`, repair, OS update, automatic retry or reboot. Persist the
actual setup process/result before returning a bootstrap reply. `0` completes
the setup operation; `3010` means installation requires a restart **before use**,
so stop without building, clearing the flag or implicitly rebooting. `1641`
means a reboot was initiated and violates this no-restart plan. `1618`/`1001`
mean another installation is active; preserve the case and inspect, never kill
the other installer or blindly rerun. A lost SSH reply permits only process/log
readback of that same setup. The build runner itself never launches setup.
[Microsoft component IDs](https://learn.microsoft.com/en-us/visualstudio/install/workload-component-id-vs-build-tools?view=visualstudio),
[parameters and actual exit-code semantics](https://learn.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022).
## Build invocation and evidence
Planned, not executed:
```powershell
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Preflight -SourceRoot C:\Source\rust-discovery
# Review that exact private preflight.json, then use a NEW build case:
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Build -SourceRoot C:\Source\rust-discovery `
-ExpectedPreflight C:\Users\Administrator\RmmNativeBuilds\<preflight-id>\preflight.json
```
The runner imports only selected developer environment values, rejects caller
compiler flags/wrappers, pins the native compiler/linker/MIDL paths, and keeps
each process's stdout/stderr private. It copies only explicit source extensions
under `crates/` and `proto/`, root Cargo/toolchain files and the MSVC runner/probe;
no `credentials/`, `devstate/`, client key material, repository metadata or local
server state. A protected build root and new protected case prevent unrelated
service users from changing the copied inputs. Source/tool hashes must match
before and after the build.
Before Cargo, Microsoft MIDL generates the real IDL with `/env amd64 /Oicf
/robust /Zp8`; `win64` is intentionally avoided because MIDL documents it as
IA64. The native C probe asserts Microsoft LLP64, numeric Rust ABI64 bytes,
wire-status ABI120 bytes and explicit field offsets. It raises/catches only a
local RPC exception1722 using the same SDK SEH macros/filter; it does not query
W32Time, contact RPC or claim trusted time. There is no vendored WIDL stub or
SEH workaround. [MIDL AMD64/packing](https://learn.microsoft.com/en-us/windows/win32/midl/-env),
[robust NDR checking](https://learn.microsoft.com/en-us/windows/win32/midl/-robust),
[C11 assertions](https://learn.microsoft.com/en-us/cpp/c-language/static-assert-c?view=msvc-170),
[SDK RPC SEH](https://learn.microsoft.com/en-us/windows/win32/rpc/rpctryexcept).
The product build script adds only the native MSVC branch, preserving the GNU
WIDL/clang branch and the C exception boundary. Both still compile the actual
clock sources and preserve their static assertions; SDK or compiler failure is
fatal. The Clock readiness gate stays unqualified. The separate native
service-SID authentication candidate is not qualified by an ABI or build pass.
Cargo then checks Windows Clippy with warnings denied, builds agent/helper and
examples in release, and **links without running** Windows/core unit tests.
The runner records AMD64 PE headers, artifact hashes, generated RPC hashes,
dependency listings and actual Authenticode status. Development output may be
unsigned; this is no signing/installer/production-PKI acceptance. Native unit
execution, LocalService/SYSTEM behavior and a preserving daemon deployment
remain separate explicit gates. A timeout retains its exact process/evidence;
no process-tree kill or retry is implied.
Locally executed preparation checks: PowerShell AST parse; isolated compilation
and Clippy of `build.rs` using locked-version `cc1.11.1`; rustfmt/source checks.
These checks do not prove that Microsoft MIDL, MSVC, the ABI probe or the final
agent/helper builds have run successfully on Windows.
## Concrete native tool bootstrap, prepared separately
`Install-NativeBuildTools.ps1` supplies separate `Prepare`, `InstallVs`,
`InstallRust`, and read-only `Read` phases. It is prepared, **not natively
executed** by this work. Both install calls consume create-only start intents
before launching their fixed worker. Each worker runs in its own protected,
demand-only Task Scheduler entry as SYSTEM, without a trigger, automatic retry
or autostart; a second worker entry refuses before installation. This keeps a
long setup independent of the40s SSH/bootstrap reply limit. Results, actual
installer PID and exit code are retained, and the task is disabled after its
worker result. Lost reply/timeout permits readback only. These are explicitly
authorized development-tool bootstrap tasks, never WebUI/RMM jobs or a generic
SYSTEM execution provider. [Task registration](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/register-scheduledtask?view=windowsserver2025-ps),
[fixed service-account principal](https://learn.microsoft.com/en-us/powershell/module/scheduledtasks/new-scheduledtaskprincipal?view=windowsserver2025-ps).
Inputs are fixed official TLS URLs with ordinary certificate/name validation:
| Input | Pinned SHA-256 |
|---|---|
| Microsoft Build Tools18.11.4 LTSC bootstrapper, file version18.11.12405.450 | `386b5d9a3d6576eab3772c1ecaa7c107ded38146a9c483f41679c194ae1e63b4` |
| rustup1.29.3 Windows x64 MSVC bootstrapper | `38027d51d69a5d05493627a0634dbea52d6cd793c8f072d2373d3f4163eec7db` |
| Rust1.99.1 official distribution manifest | `fb152be80975c194aa5fc684b2aec3a6e14e2b855efe2faa4e5f8ae6e590f463` |
The Microsoft EXE additionally requires valid Microsoft Authenticode and the
exact product/file version. Rustup is checked against its exact
[official archived checksum](https://static.rust-lang.org/rustup/archive/1.29.3/x86_64-pc-windows-msvc/rustup-init.exe.sha256),
with [official archive/install options](https://rust-lang.github.io/rustup/installation/other.html).
Its homes are private case subdirectories; `--no-modify-path`,
`--no-update-default-toolchain`, minimal profile and exactly clippy/rustfmt keep
the developer's default Rust installation/profile/PATH untouched. The fixed
Rust distribution origin is set only for that worker process. Using isolated
homes is an [official rustup option](https://rust-lang.github.io/rustup/installation/index.html).
VS setup uses only `--wait --quiet --norestart --noUpdateInstaller`, the new
fixed target `C:\RmmBuildTools`, and the three components above. It relies on the
official installer's standard Microsoft metadata/package verification. No
custom channel/catalog, checksum override, certificate import or weaker
verification flag is supplied. Installed VS version must then match exactly
18.11.12405.450 and SDK26100 must be present; drift remains incomplete. Setup's
required transitive development dependencies remain Microsoft's responsibility;
this is no general production-installer trust acceptance. No Windows Update
command is invoked. Boot/OS build, machine trust-store fingerprints, machine
PATH, execution policies, W32Time and both exact RMM service states/PIDs are
observed before/after. Detected changes, unexpected setup version,3010/1641 or
restart need stop completion without resetting/undoing or retrying anything.
Scope decision2026-11-14: an initial **custom manual catalog-bundle** design was
rejected. Its channel declared catalog size31851991 and hash
`ef56e369d92855a9667cd03602019375f42b3cd2145041df31cacc071a40e1af`;
the exact official URL instead returned18733589 bytes with hash
`805d16fd14a370cbeb6568179950826ba0cdc6e2cffa44936fddc408ef05cba3`.
Those inputs and rejected source remain privately retained; no mismatching
catalog was accepted or installed. The extra manual bundle was our design,
not an AGENTS/product requirement. The selected replacement uses the supported,
independently hash/Authenticode-pinned official EXE and its own verification,
not a checksum exception or undocumented JSON-signature workaround.
Microsoft documents [standard acquisition and fixed-version bootstrappers](https://learn.microsoft.com/en-us/visualstudio/install/use-command-line-parameters-to-install-visual-studio?view=vs-2022).
Planned, not executed:
```powershell
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Prepare
# Review that exact printed private case and its prepared.json first:
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase InstallVs -CaseDirectory <case>
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Read -CaseDirectory <case>
# Continue only after verified VS exit0 and no restart need:
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase InstallRust -CaseDirectory <same-case>
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Install-NativeBuildTools.ps1 -Phase Read -CaseDirectory <same-case>
```
This uses the already authorized **development/bootstrap-only** process option
`-ExecutionPolicy RemoteSigned`, as in the previous Windows dev-access and
passive-client bootstrap. The worker uses that same process option and invokes
the hash-checked `.ps1` file normally with `& $p`; it never executes file content
through `ScriptBlock::Create`. A small encoded hash launcher is only transport
for that ordinary file invocation. There is no `Set-ExecutionPolicy`, registry
policy write, Machine/User/GPO change or Bypass option. Persistent scopes stay
unchanged; process scopes are consistently RemoteSigned and GPO precedence is
retained. This permission does **not** decide or authorize the separate RMM
PowerShell job executor's still-unqualified policy contract.
After actual successful setup, pass the recorded private homes to the builder:
```powershell
powershell.exe -NoProfile -ExecutionPolicy RemoteSigned -File .\deploy\windows\Build-NativeMsvc.ps1 -Mode Preflight -SourceRoot C:\Source\rust-discovery `
-RustupExe <case>\cargo_home\bin\rustup.exe -RustupHome <case>\rustup_home -CargoHome <case>\cargo_home
```
Use those same arguments with `-Mode Build -ExpectedPreflight <actual-receipt>`.
Populate the isolated public Cargo cache explicitly before an offline build;
there is no automatic fetch/retry in the builder. Native setup, native MIDL/ABI,
unit execution and deployment results are still outstanding until actually run.
+40
View File
@@ -0,0 +1,40 @@
/* Pure native ABI + local SEH qualification. No RPC/network/time query. */
#define _WIN32_WINNT 0x0a00
#include <windows.h>
#include <rpc.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include "w32time.h"
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(void *) == 8, "native AMD64 pointers");
_Static_assert(sizeof(unsigned long) == 4, "Microsoft LLP64");
_Static_assert(sizeof(RmmClockStatus) == 64, "Rust numeric clock ABI");
_Static_assert(offsetof(RmmClockStatus, last_sync) == 24, "Rust FILETIME offset");
_Static_assert(offsetof(RmmClockStatus, phase) == 56, "Rust phase offset");
_Static_assert(sizeof(RMM_W32TIME_STATUS) == 120, "MS-W32T x64 wire structure");
_Static_assert(offsetof(RMM_W32TIME_STATUS, qwLastSyncTicks) == 24, "MS-W32T FILETIME offset");
_Static_assert(offsetof(RMM_W32TIME_STATUS, toSysPhaseOffset) == 64, "MS-W32T phase offset");
_Static_assert(offsetof(RMM_W32TIME_STATUS, pEntries) == 112, "MS-W32T array pointer offset");
int main(void) {
unsigned long caught = 0;
/* Exercise the same SDK SEH macros/filter as clock_rpc.c. The exception
originates locally; it must terminate in C and never cross into Rust. */
RpcTryExcept {
RpcRaiseException(RPC_S_SERVER_UNAVAILABLE);
} RpcExcept(RpcExceptionFilter(RpcExceptionCode())) {
caught = RpcExceptionCode();
} RpcEndExcept
if (caught != RPC_S_SERVER_UNAVAILABLE) return 1;
printf("{\"amd64\":true,\"numeric_size\":64,\"wire_size\":120,\"seh_caught\":%lu,\"network_called\":false}\n", caught);
return 0;
}
+257
View File
@@ -0,0 +1,257 @@
#!/usr/bin/env python3
"""New passive IPC-client runtime qualification/upgrade; historical V1 stays intact.
No pin/helper service is installed here. Native unit tests, actual runtime change
and later real LocalService-to-SYSTEM IPC are distinct acceptance steps.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import shutil
import subprocess
import uuid
import runtime_upgrade as previous
import client_bootstrap as cb
import transport_acceptance as base
import auto_transport_acceptance as auto
require=cb.contract.require
write=previous.write
digest=previous.digest
STATE=cb.EVIDENCE/'runtime-feature-upgrade'
CURRENT=cb.EVIDENCE/'current-runtime.json'
TARGET=previous.TARGET
IMAGE=previous.IMAGE
OLD='f64e81155a4d86089a9dd7e6ec42ffba50b939442386c3187a28769a4de1aa0b'
ORIGINAL='31e360e50b177b4d5729d24e436d4f25aeed7b1a7bd35f2a129841ba1abffd37'
def replace_once(source, old, new):
require(source.count(old)==1,'exact historical source transform, no fuzzy replacement')
return source.replace(old,new,1)
def upgrade_source():
source=replace_once(previous.UPGRADE,"$p.old_sha256 -cne '"+ORIGINAL+"'","$p.old_sha256 -cne '"+OLD+"'")
source=replace_once(source,r"$folder='C:\ProgramData\RustDiscoveryBootstrap\runtime-'+$p.id",r"$folder='C:\ProgramData\RustDiscoveryBootstrap\runtime-feature-'+$p.id")
source=replace_once(source,r"$active='C:\ProgramData\RustDiscoveryBootstrap\runtime-active-intent.json'",r"$active='C:\ProgramData\RustDiscoveryBootstrap\runtime-feature-active-intent.json'")
source=replace_once(source,"function Snapshot([string]$expected) {",r'''function Snapshot([string]$expected) {
if ((Test-Path -LiteralPath 'C:\ProgramData\RustDiscoverySystem') -or (Test-Path -LiteralPath 'C:\Program Files\RustDiscovery\rmm-helper-windows.exe') -or $null -ne (Get-Service -Name 'RmmDiscoverySystemHelper' -ErrorAction SilentlyContinue)) {throw 'Complete passive feature upgrade before any helper/pin installation'}''')
return source
UPGRADE=upgrade_source()
PREPARE=base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1','RuntimeFeatureUpgrade.ps1')
LAUNCH=base.LAUNCH_SCRIPT.replace('TransportAcceptance.ps1','RuntimeFeatureUpgrade.ps1').replace('RmmTransportPayload','RmmRuntimePayload')
def source_files():
files=previous.source_files()
# Clippy --all-targets also compiles native_acl and EVERY example. Pin
# those, all nested modules and each local crate's optional build script.
for crate in ('agent-windows','agent-core','domain','protocol'):
root=cb.ROOT/'crates'/crate
for path in root.rglob('*'):
if path.is_file() and path.suffix in ('.rs','.c','.h','.idl','.toml'):
relative=str(path.relative_to(cb.ROOT));files[relative]=digest(path)
for path in ('deploy/windows/runtime_upgrade.py','deploy/windows/runtime_feature_upgrade.py'):
files[path]=digest(cb.ROOT/path)
return dict(sorted(files.items()))
def stable(manifest,current_sources=True):
require(manifest['schema_version']==1 and manifest['architecture']==TARGET and manifest['default_features'] is True and manifest['system_helper_feature'] is True and manifest['system_helper_client_feature'] is True,'explicit optional IPC-client feature profile')
if current_sources:
require(source_files()==manifest['source_files'],'feature build sources unchanged')
require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$',(cb.ROOT/'crates/agent-windows/src/clock.rs').read_text(),re.M) is not None,'production clock gate must remain closed')
require(manifest['checks']['production_clock_qualified'] is False,'compiled production clock stayed unqualified')
for item in manifest['artifacts'].values():require(digest(item['path'])==item['sha256'],'actual retained linked artifact hash')
def build():
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True)
folder=STATE/('build-'+str(uuid.uuid4()));folder.mkdir(mode=0o700)
before=source_files();write(folder,'source-before.json',before)
require(re.search(r'^pub const ADAPTER_QUALIFIED: bool = false;$',(cb.ROOT/'crates/agent-windows/src/clock.rs').read_text(),re.M) is not None,'production clock gate must remain closed')
target=cb.ROOT/'devstate/windows-runtime-feature/target';target.mkdir(mode=0o700,parents=True,exist_ok=True)
script='cargo clippy --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --all-targets -- -D warnings && cargo build --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --bin rmm-agent-windows --example runtime_state --release && cargo test --locked --offline --target '+TARGET+' -p rmm-agent-windows --features system-helper --lib --bin rmm-agent-windows --no-run --message-format=json'
raw=previous.command(['docker','run','--rm','--network','none','--read-only','--tmpfs','/tmp:rw,size=512m','-v',str(cb.ROOT)+':/src:ro','-v',str(target)+':/target','-e','CARGO_TARGET_DIR=/target','--entrypoint','/bin/sh',IMAGE,'-c',script])
require(source_files()==before,'no source drift during feature build')
units={}
for line in raw.decode().splitlines():
try:item=json.loads(line)
except ValueError:continue
if item.get('reason')=='compiler-artifact' and 'rmm-agent-windows' in item.get('package_id',''):
require('system-helper' in item.get('features',[]),'actual compiled feature profile')
if item.get('profile',{}).get('test') and item.get('executable'):
kinds=item['target']['kind'];key='lib' if 'lib' in kinds else 'bin' if 'bin' in kinds else None
if key:units[key]=target/Path(item['executable']).relative_to('/target')
require(set(units)=={'lib','bin'},'new feature lib and binary unit links')
artifacts={}
for key,path,name,release in [('agent',target/TARGET/'release/rmm-agent-windows.exe','rmm-agent-windows.exe',True),('probe',target/TARGET/'release/examples/runtime_state.exe','RuntimeStateProbe.exe',True),('lib',units['lib'],'windows-tests-lib.exe',False),('bin',units['bin'],'windows-tests-bin.exe',False)]:
out=folder/name;shutil.copyfile(path,out);out.chmod(0o600);original=digest(path)
if key in ('lib','bin'):
previous.command(['docker','run','--rm','--network','none','--read-only','-v',str(folder.resolve())+':/artifacts','--entrypoint','x86_64-w64-mingw32-strip',IMAGE,'--strip-debug','/artifacts/'+name])
require(digest(path)==original,'retain original unit link')
data=out.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','actual Windows AMD64 artifact')
artifacts[key]=dict(path=str(out.resolve()),name=name,sha256=digest(out),bytes=len(data),release_build=release,original_link_sha256=original)
require(source_files()==before,'source unchanged through artifact preservation')
manifest=dict(schema_version=1,architecture=TARGET,default_features=True,system_helper_feature=True,system_helper_client_feature=True,source_files=before,artifacts=artifacts,checks=dict(cross_clippy_D_warnings=True,release_build=True,windows_unit_link=True,production_clock_qualified=False),native_vm_tested=False)
write(folder,'manifest.json',manifest)
print(json.dumps(dict(manifest=str((folder/'manifest.json').resolve()),artifacts={k:v['sha256'] for k,v in artifacts.items()},native_vm_tested=False)))
def qualify(manifest_path):
manifest=cb.contract.protected_json(manifest_path);stable(manifest)
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True)
folder=STATE/('qualification-'+str(uuid.uuid4()));folder.mkdir(mode=0o700)
write(folder,'intent.json',dict(manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],test_runner_sha256=hashlib.sha256(previous.TESTS.encode()).hexdigest(),system_helper_client_feature=True))
before=auto.scopes();flags=auto.status();write(folder,'server-before.json',dict(counts=before,flags=flags))
reports={}
for key,count in [('lib',21),('bin',6)]:
item=manifest['artifacts'][key];previous.upload(item)
report=cb.powershell(previous.TESTS,dict(name=item['name'],sha256=item['sha256']),timeout=150)
write(folder,key+'-native.json',report)
names=re.findall(r'^(.+): test\r?$',report['listed']['stdout'],re.M)
summary=re.search(r'test result: ok\. (\d+) passed; 0 failed; 0 ignored; 0 measured; 0 filtered out;',report['result']['stdout'])
require(report['listed']['exit_code']==report['result']['exit_code']==0 and summary is not None and len(names)==int(summary.group(1))==count,'all exact 21+6 new native units, none skipped or filtered')
reports[key]=dict(artifact_sha256=item['sha256'],passed=len(names),test_names=names,native_context='bootstrap_administrator_unit_tests')
stable(manifest)
after=auto.scopes();after_flags=auto.status();require(before==after and flags==after_flags and auto.flags_disabled(flags),'native units have no jobs/policy/fault effect')
write(folder,'result.json',dict(verified=True,completed=True,native_unit_tests=reports,manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),source_files=manifest['source_files'],server_counts=after,server_flags=flags,no_jobs=True,clock_adapter_qualified=False,system_helper_client_feature=True,local_service_runtime_test=False,native_pipe_peer_test=False))
print(json.dumps(dict(verified=True,qualification=str((folder/'result.json').resolve()),native_tests={k:v['passed'] for k,v in reports.items()},native_pipe_peer_test=False)))
def qualified(manifest_path,qualification_path,current_sources=True):
manifest=cb.contract.protected_json(manifest_path);stable(manifest,current_sources)
proof=cb.contract.protected_json(qualification_path)
require(proof.get('verified') is True and proof.get('completed') is True and proof.get('system_helper_client_feature') is True and proof['manifest_path']==str(manifest_path.resolve()) and proof['manifest_sha256']==digest(manifest_path) and proof['source_files']==manifest['source_files'] and proof.get('no_jobs') is True and proof.get('clock_adapter_qualified') is False,'new source-bound native feature qualification')
for key,count in [('lib',21),('bin',6)]:
require(proof['native_unit_tests'][key]['passed']==count and proof['native_unit_tests'][key]['artifact_sha256']==manifest['artifacts'][key]['sha256'],'exact new compiled native unit association')
return manifest,proof
def prior_runtime():
index=cb.contract.protected_json(CURRENT)
require(index.get('type')=='rmm.windows.current-runtime' and index.get('binary_sha256')==OLD and index.get('agent_id')==base.AGENT and index.get('asset_id')==base.ASSET and index.get('transport')=='auto' and index.get('windows_jobs_enabled') is False and index.get('clock_adapter_qualified') is False and not index.get('system_helper_client_feature',False),'accepted original f64 passive runtime')
path=Path(index['evidence']['path']).resolve()
require(path.is_relative_to(cb.EVIDENCE.resolve()) and digest(path)==index['evidence']['sha256'],'exact accepted old runtime evidence')
proof=cb.contract.protected_json(path)
require(proof.get('verified') is True and proof.get('completed') is True and proof.get('restored') is False and proof.get('no_jobs') is True and proof['native']['binary_sha256']==OLD,'actual old passive runtime, not a historical transport artifact')
return index,proof
def stage_source():
raw=UPGRADE.encode();sha=hashlib.sha256(raw).hexdigest();value=cb.powershell(PREPARE,dict(sha256=sha))
if not value['already_uploaded']:
folder=STATE/'source';folder.mkdir(mode=0o700,parents=True,exist_ok=True);path=folder/(sha+'.ps1')
if not path.exists():
fd=os.open(path,os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW,0o600)
with os.fdopen(fd,'wb') as stream:stream.write(raw);stream.flush();os.fsync(stream.fileno())
require(digest(path)==sha,'exact feature source bytes')
prefix=cb.contract.protected_json(cb.ROOT/'credentials/windows-dev-access.json')['ssh_prefix']
args=['sftp']+[part for part in prefix[1:-1] if part!='-T']+['-b','-',prefix[-1]]
target='/C:/ProgramData/RustDiscoveryBootstrap/'+sha+'/RuntimeFeatureUpgrade.ps1'
result=subprocess.run(args,input=('put '+json.dumps(str(path.resolve()))+' '+json.dumps(target)+'\n').encode(),capture_output=True,timeout=60)
require(result.returncode==0 and cb.powershell(PREPARE,dict(sha256=sha))['already_uploaded'],'exact staged feature source')
return sha
def remote(intent,action):
require(intent['source_sha256']==hashlib.sha256(UPGRADE.encode()).hexdigest(),'immutable exact feature source')
return cb.powershell(LAUNCH,dict(sha256=intent['source_sha256'],payload=dict(intent['remote_payload'],action=action)),timeout=100,remote_signed=True)
def upgrade(manifest_path,qualification_path):
manifest,proof=qualified(manifest_path,qualification_path);index,old=prior_runtime()
os.umask(0o077);STATE.mkdir(mode=0o700,parents=True,exist_ok=True);ident=str(uuid.uuid4());folder=STATE/ident;folder.mkdir(mode=0o700)
for key in ('agent','probe'):previous.upload(manifest['artifacts'][key])
source=stage_source()
payload=dict(id=ident,old_sha256=OLD,new_sha256=manifest['artifacts']['agent']['sha256'],probe_sha256=manifest['artifacts']['probe']['sha256'],config_sha256=old['native']['config_sha256'])
intent=dict(id=ident,type='passive_feature_runtime_upgrade',started_at=cb.contract.now().isoformat(),source_sha256=source,manifest_path=str(manifest_path.resolve()),manifest_sha256=digest(manifest_path),qualification_path=str(qualification_path.resolve()),qualification_sha256=digest(qualification_path),remote_payload=payload,previous_runtime_index=index,previous_runtime_index_sha256=digest(CURRENT),no_jobs=True,no_helper_install=True,no_reboot=True)
write(folder,'intent.json',intent)
before=dict(native=remote(intent,'state'),central=base.observe(ident),counts=auto.scopes(),server=auto.status())
require(before['counts']==proof['server_counts'] and before['server']==proof['server_flags'] and auto.flags_disabled(before['server']),'unchanged jobs/policies since native units')
require(before['native']['service']['state']=='Running' and before['native']['journal']['pending_inventory_events']==0,'settled retained actual original runtime')
for field in ('binary_sha256','binary_acl_sha256','config_sha256','config_acl_sha256','public_hashes','identity_acls','private_key_metadata'):
require(before['native'][field]==old['native'][field],'accepted old runtime identity/config/ACL continuity')
require(digest(CURRENT)==intent['previous_runtime_index_sha256'],'current runtime index unchanged before mutation')
write(folder,'before.json',before);write(folder,'enter-intent.json',dict(id=ident,stop_swap_start_once=True,source_sha256=source))
try:write(folder,'entered.json',remote(intent,'enter'))
except Exception as error:
write(folder,'enter-reply-incomplete.json',dict(error_type=type(error).__name__,continuation='finish/readback only; never repeat enter or start'));raise
print('Passive feature runtime transition '+ident,flush=True);finish(ident)
def load_case(ident):
require(str(uuid.UUID(ident))==ident,'canonical existing feature case')
folder=STATE/ident;intent=cb.contract.protected_json(folder/'intent.json')
require(intent['id']==ident and intent['type']=='passive_feature_runtime_upgrade' and digest(intent['manifest_path'])==intent['manifest_sha256'] and digest(intent['qualification_path'])==intent['qualification_sha256'],'same original immutable feature case and qualification')
manifest,_=qualified(Path(intent['manifest_path']),Path(intent['qualification_path']),current_sources=False)
return folder,intent,manifest
def publish(folder,result_name,intent,binary,restored):
result=folder/result_name
value=dict(schema_version=1,type='rmm.windows.current-runtime',case_id=folder.name,agent_id=base.AGENT,asset_id=base.ASSET,binary_sha256=binary,transport='auto',evidence=dict(path=str(result.resolve()),sha256=digest(result)),windows_jobs_enabled=False,clock_adapter_qualified=False,system_helper_client_feature=not restored)
actual=cb.contract.protected_json(CURRENT)
if actual==value:return
require(digest(CURRENT)==intent['previous_runtime_index_sha256'] or actual.get('case_id')==folder.name,'CAS current runtime: never overwrite another transition')
write(folder,'previous-index-'+str(uuid.uuid4())+'.json',actual)
temporary=CURRENT.with_name('.feature-runtime-'+str(uuid.uuid4())+'.json');write(temporary.parent,temporary.name,value);os.replace(temporary,CURRENT)
def finish(ident):
folder,intent,manifest=load_case(ident);restored=(folder/'rollback-intent.json').exists()
before=cb.contract.protected_json(folder/'before.json');observed=remote(intent,'finish');write(folder,'finish-readback-'+str(uuid.uuid4())+'.json',observed)
expected=OLD if restored else manifest['artifacts']['agent']['sha256']
require(observed.get('completed') is True and observed.get('read_only') is True and observed.get('restored')==restored and observed['native']['binary_sha256']==expected,'actual selected runtime after fresh readback, never historical success replay')
started=cb.contract.protected_json(folder/'rollback-intent.json')['started_at'] if restored else intent['started_at']
online=base.wait_mode(ident,'wss',cb.contract.timestamp(started),before['central']['device'])
after=remote(intent,'finish');write(folder,'after-readback-'+str(uuid.uuid4())+'.json',after)
require(after.get('restored')==restored and after['native']['binary_sha256']==expected,'same actual selected runtime after native heartbeat wait')
native=after['native'];require(native['journal']['pending_inventory_events']==0 and native['journal']['sequence']>before['native']['journal']['sequence'],'two actual WSS heartbeats/sequence advance and concrete original telemetry ACK')
device=base.read_device();denied=base.foreign_denied()
require(cb.contract.timestamp(device['observed_at'])>=cb.contract.timestamp(intent['started_at']),'actual fresh stored inventory of retained identity')
counts=auto.scopes();flags=auto.status();require(counts==before['counts'] and flags==before['server'] and auto.flags_disabled(flags),'Windows/Linux job counts and server policies/hooks unchanged')
result_name='rollback-completed.json' if restored else 'result.json';path=folder/result_name
value=dict(verified=True,completed=True,restored=restored,id=ident,native=native,online=online,stored_inventory=device,foreign_denied=denied,manifest_path=intent['manifest_path'],manifest_sha256=intent['manifest_sha256'],qualification_sha256=intent['qualification_sha256'],counts=counts,server=flags,no_jobs=True,no_reboot=True,no_clock_change=True,windows_jobs_enabled=False,clock_adapter_qualified=False,system_helper_client_feature=not restored,native_pipe_peer_test=False,configuration_unchanged=True,private_key_read=False,transport='auto',preferred_wire='wss')
if path.exists():require(cb.contract.protected_json(path).get('verified') is True,'retain original completed result')
else:write(folder,result_name,value)
publish(folder,result_name,intent,expected,restored)
print(json.dumps(dict(verified=True,id=ident,system_helper_client_feature=not restored,native_pipe_peer_test=False,windows_jobs_enabled=False)))
def rollback(ident):
folder,intent,_=load_case(ident);before=cb.contract.protected_json(folder/'before.json')
require(auto.scopes()==before['counts'] and auto.status()==before['server'],'no intervening jobs/policy changes before explicit rollback')
write(folder,'rollback-intent.json',dict(id=ident,old_binary_sha256=OLD,started_at=cb.contract.now().isoformat(),start_once=True,no_jobs=True))
try:value=remote(intent,'rollback')
except Exception as error:
write(folder,'rollback-reply-incomplete.json',dict(error_type=type(error).__name__,readback_required=True,no_repeated_start=True));raise
write(folder,'rollback-result.json',value)
require(value.get('restored') is True and value['native']['binary_sha256']==OLD,'actual original f64 binary restored')
finish(ident)
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('phase',choices=['build-local','qualify-native','upgrade-passive','finish','rollback'])
parser.add_argument('--manifest',type=Path);parser.add_argument('--qualification',type=Path);parser.add_argument('--id')
args=parser.parse_args()
if args.phase=='build-local':require(not(args.manifest or args.qualification or args.id),'fresh immutable feature build');build()
elif args.phase=='qualify-native':require(args.manifest and not(args.qualification or args.id),'one new feature manifest');qualify(args.manifest)
elif args.phase=='upgrade-passive':require(args.manifest and args.qualification and not args.id,'same new native feature qualification');upgrade(args.manifest,args.qualification)
else:require(args.id and not(args.manifest or args.qualification),'same original feature case only');{'finish':finish,'rollback':rollback}[args.phase](args.id)
if __name__=='__main__':
try:main()
except Exception as error:
print('WINDOWS_FEATURE_RUNTIME_INCOMPLETE:'+type(error).__name__)
raise SystemExit(2) from None
+390
View File
@@ -0,0 +1,390 @@
#!/usr/bin/env python3
"""Create-once passive SYSTEM helper bootstrap. Never creates or approves a job.
Deployment order: accepted feature-agent upgrade first, this helper second. The
running LocalService agent discovers the protected pin without another restart.
Only its own kernel-authenticated Probe can prove the positive pipe exchange.
A lost mutation reply permits readback only, never a repeated install/start.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
import uuid
import client_bootstrap as cb
import transport_acceptance as base
require = cb.contract.require
STATE = cb.EVIDENCE / 'system-helper-bootstrap'
AGENT_ID = '63ea15b2-0643-403d-b5b4-cba1288dbea3'
ASSET_ID = 'a176520e-8a91-4e93-a78b-8e7620f430e0'
HELPER_SHA = '921cf1822f128c3e37ca4c0250f08309a14d760453fa3b56a589229c6614353e'
UNIT_SHA = 'd98bbd7c96b78f13b3590bc507961821788672326dd2b690050e81e258e64b45'
QUALIFICATION = cb.EVIDENCE / 'helper-unit-qualification/38b0fe75-f6a5-4ad3-ab2d-7f3acf7e9cbd/result.json'
OWN_SOURCES = {
'crates/agent-windows/src/helper_ipc.rs': '8163e8029b7dfe256d82995036f351c6e241c8e1e184f34129c15ac205052552',
'crates/agent-windows/src/helper_system.rs': '07908fae9f834f9bd728a10885596d2d997d5e26a5712254de35b9e47dfa7226',
'crates/agent-windows/src/bin/rmm-helper-windows.rs': 'e87c39bee18dc9375287e4f490f70d6bbd76629884564f76804f2bd9152358a4',
}
SCRIPT = r'''
$ErrorActionPreference='Stop'
$p=$global:RmmSystemHelperPayload
if ($null -eq $p -or [string]([guid]$p.id) -cne $p.id -or $p.action -notin @('install','start','readback')) {throw 'Fixed passive helper intent'}
foreach($n in @('helper_sha256','agent_sha256','agent_acl_sha256','config_sha256','config_acl_sha256','source_sha256','policy_sha256','pin_sha256')) {if ($p.$n -notmatch '^[0-9a-f]{64}$') {throw 'Exact bootstrap hashes'}}
if ($p.helper_sha256 -cne '921cf1822f128c3e37ca4c0250f08309a14d760453fa3b56a589229c6614353e') {throw 'Native-unit-qualified helper only'}
$name='RmmDiscoverySystemHelper';$agentName='RmmDiscoveryWindows'
$root='C:\ProgramData\RustDiscoverySystem';$state=Join-Path $root 'state'
$settingsPath=Join-Path $root 'helper.json';$pinPath=Join-Path $root 'agent-pin.json'
$binary='C:\Program Files\RustDiscovery\rmm-helper-windows.exe'
$agentBinary='C:\Program Files\RustDiscovery\rmm-agent-windows.exe'
$agentConfig='C:\ProgramData\RustDiscovery\config.json'
$bootstrap='C:\ProgramData\RustDiscoveryBootstrap'
$folder=Join-Path $bootstrap ('system-helper-'+$p.id)
$active=Join-Path $bootstrap 'system-helper-active-intent.json'
$uploaded=Join-Path (Join-Path $bootstrap $p.helper_sha256) 'rmm-helper-windows.exe'
$helperCommand='"C:\Program Files\RustDiscovery\rmm-helper-windows.exe" --service'
$agentCommand='"C:\Program Files\RustDiscovery\rmm-agent-windows.exe" --service --config "C:\ProgramData\RustDiscovery\config.json"'
$sys='S-1-5-18';$admin='S-1-5-32-544'
$agentSid=(New-Object Security.Principal.NTAccount('NT SERVICE',$agentName)).Translate([Security.Principal.SecurityIdentifier]).Value
function Sha([byte[]]$data) {
$h=[Security.Cryptography.SHA256]::Create()
try {return ([BitConverter]::ToString($h.ComputeHash($data))).Replace('-','').ToLowerInvariant()} finally {$h.Dispose()}
}
function Hash([string]$path) {return (Get-FileHash -LiteralPath $path -Algorithm SHA256).Hash.ToLowerInvariant()}
function Sid([string]$name) {return (New-Object Security.Principal.NTAccount('NT SERVICE',$name)).Translate([Security.Principal.SecurityIdentifier]).Value}
function Acl-Hash([string]$path) {return Sha ([Text.Encoding]::UTF8.GetBytes((Get-Acl -LiteralPath $path).Sddl))}
function Check([string]$path,[bool]$directory,[string[]]$readers,[string[]]$writers) {
$item=Get-Item -LiteralPath $path -Force
if ($item.PSIsContainer -ne $directory -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) {throw 'Protected regular path'}
$acl=Get-Acl -LiteralPath $path
if ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sys,$admin)) {throw 'Protected owner'}
if ($directory -and !$acl.AreAccessRulesProtected) {throw 'Explicit protected directory DACL required'}
$rules=@($acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier]));if (!$rules.Count) {throw 'Empty DACL'}
$seen=@{}
foreach($r in $rules) {
$id=$r.IdentityReference.Value;$rights=[int64]$r.FileSystemRights
if ($r.AccessControlType -ne 'Allow') {throw 'Unexpected deny or inherited DACL'}
if ($id -in @($sys,$admin)+$writers) {if ($rights -ne 2032127) {throw 'Exact writer rights'}}
elseif ($id -in $readers) {if (($rights -band (-bnot 1179817)) -ne 0 -or ($rights -band 131241) -ne 131241) {throw 'Reader must have read-execute only'}}
else {throw 'Unlisted principal'}
$seen[$id]=$true
}
foreach($id in @($sys,$admin)+$readers+$writers) {if (!$seen[$id]) {throw 'Missing intended DACL principal'}}
}
function Protect([string]$path,[bool]$directory,[string[]]$readers,[string[]]$writers) {
if ($directory) {$acl=New-Object Security.AccessControl.DirectorySecurity} else {$acl=New-Object Security.AccessControl.FileSecurity}
$acl.SetAccessRuleProtection($true,$false);$acl.SetOwner((New-Object Security.Principal.SecurityIdentifier($admin)))
$inherit='None';if ($directory) {$inherit='ContainerInherit,ObjectInherit'}
foreach($id in @($sys,$admin)+$writers) {$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($id)),'FullControl',$inherit,'None','Allow')))}
foreach($id in $readers) {$acl.AddAccessRule((New-Object Security.AccessControl.FileSystemAccessRule((New-Object Security.Principal.SecurityIdentifier($id)),'ReadAndExecute',$inherit,'None','Allow')))}
Set-Acl -LiteralPath $path -AclObject $acl;Check $path $directory $readers $writers
}
function Write-New([string]$path,$value) {
$bytes=[Text.Encoding]::UTF8.GetBytes(($value | ConvertTo-Json -Depth 24 -Compress))
$f=[IO.File]::Open($path,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read)
try {$f.Write($bytes,0,$bytes.Length);$f.Flush($true)} finally {$f.Dispose()}
}
function Agent-State {
$service=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoveryWindows'" -OperationTimeoutSec 10
if ($service.State -cne 'Running' -or $service.StartName -cne 'NT AUTHORITY\LocalService' -or $service.PathName -cne $agentCommand -or $service.StartMode -cne 'Auto') {throw 'Original running LocalService agent'}
if ((Hash $agentBinary) -cne $p.agent_sha256 -or (Hash $agentConfig) -cne $p.config_sha256 -or (Acl-Hash $agentBinary) -cne $p.agent_acl_sha256 -or (Acl-Hash $agentConfig) -cne $p.config_acl_sha256) {throw 'Current accepted passive feature agent and ACLs'}
$cfg=Get-Content -LiteralPath $agentConfig -Raw | ConvertFrom-Json
if ((($cfg.PSObject.Properties.Name|Sort-Object)-join ',') -cne 'ca_file,connect_address,enrollment_token_file,server_name,state_directory,transport' -or $cfg.transport -cne 'auto' -or $cfg.server_name -cne 'discovery.ease-technologies.de') {throw 'Unchanged six-field passive auto configuration'}
$ident=Get-Content -LiteralPath 'C:\ProgramData\RustDiscovery\state\identity.json' -Raw | ConvertFrom-Json
if ($ident.agent_id -cne $p.binding.agent_id -or $ident.asset_id -cne $p.binding.asset_id -or $ident.agent_id -cne '63ea15b2-0643-403d-b5b4-cba1288dbea3' -or $ident.asset_id -cne 'a176520e-8a91-4e93-a78b-8e7620f430e0') {throw 'Original enrolled identity'}
$proc=Get-CimInstance Win32_Process -Filter ('ProcessId='+$service.ProcessId) -OperationTimeoutSec 10
$owner=(Invoke-CimMethod -InputObject $proc -MethodName GetOwnerSid -OperationTimeoutSec 10).Sid
if ($owner -cne 'S-1-5-19' -or $proc.ExecutablePath -cne $agentBinary) {throw 'Actual agent LocalService process'}
return [ordered]@{process_id=$service.ProcessId;creation=$proc.CreationDate.ToUniversalTime().ToString('o');owner_sid=$owner;binary_sha256=(Hash $agentBinary);config_sha256=(Hash $agentConfig);config_acl_sha256=(Acl-Hash $agentConfig);agent_id=$ident.agent_id;asset_id=$ident.asset_id}
}
function Same-Agent($before,$after) {
foreach($field in @('process_id','creation','owner_sid','binary_sha256','config_sha256','config_acl_sha256','agent_id','asset_id')) {if ($before.$field -cne $after.$field) {throw 'No agent restart or identity/configuration mutation allowed'}}
}
function Peer([string]$service,[string]$sid,[string]$user,[string]$path,[string]$hash) {
return [ordered]@{service_name=$service;service_sid=$sid;user_sid=$user;binary=$path;binary_sha256=$hash}
}
function Configurations([string]$helperSid) {
$binding=[ordered]@{tenant_id=$p.binding.tenant_id;asset_id=$p.binding.asset_id;agent_id=$p.binding.agent_id;enrollment_generation=[long]$p.binding.enrollment_generation;assignment_version=[long]$p.binding.assignment_version;public_key_hex=$p.binding.public_key_hex}
$agent=Peer $agentName $agentSid 'S-1-5-19' $agentBinary $p.agent_sha256
$helper=Peer $name $helperSid $sys $binary $p.helper_sha256
$settings=[ordered]@{schema_version=1;binding=$binding;agent=$agent;helper=$helper;allowed_services=@();max_runtime_seconds=30;enable_effects=$false;allow_reboot=$false}
# JSON.NET/PowerShell may escape strings differently from serde. The policy
# hash comes from exact Python canonical settings bytes, verified below.
$expected=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($p.settings_base64)) | ConvertFrom-Json
$actual=$settings|ConvertTo-Json -Depth 24 -Compress
if (($expected|ConvertTo-Json -Depth 24 -Compress) -cne $actual) {throw 'Exact fixed passive public settings'}
$settingsBytes=[Convert]::FromBase64String($p.settings_base64)
if ((Sha $settingsBytes) -cne $p.policy_sha256) {throw 'Canonical public policy hash'}
$pin=[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($p.pin_base64)) | ConvertFrom-Json
if ($pin.schema_version -ne 1 -or $pin.policy_sha256 -cne $p.policy_sha256 -or ($pin.binding|ConvertTo-Json -Compress) -cne ($binding|ConvertTo-Json -Compress) -or ($pin.agent|ConvertTo-Json -Compress) -cne ($agent|ConvertTo-Json -Compress) -or ($pin.helper|ConvertTo-Json -Compress) -cne ($helper|ConvertTo-Json -Compress)) {throw 'Exact reciprocal public pin'}
return $settingsBytes
}
function Read-Intent {
Check $folder $true @() @()
$intent=Get-Content -LiteralPath (Join-Path $folder 'intent.json') -Raw | ConvertFrom-Json
foreach($field in @('id','helper_sha256','agent_sha256','agent_acl_sha256','config_sha256','config_acl_sha256','source_sha256','policy_sha256','pin_sha256','helper_service_sid','settings_base64','pin_base64')) {if ($intent.payload.$field -cne $p.$field) {throw 'Exact existing passive helper intent'}}
$live=Get-Content -LiteralPath $active -Raw | ConvertFrom-Json
if ($live.id -cne $p.id) {throw 'Another helper bootstrap is active'}
return $intent
}
function Existing-State($intent) {
$agent=Agent-State;Same-Agent $intent.agent $agent
$service=Get-CimInstance Win32_Service -Filter "Name='RmmDiscoverySystemHelper'" -OperationTimeoutSec 10
if ($null -eq $service) {return [ordered]@{service_exists=$false;agent=$agent;root_exists=(Test-Path -LiteralPath $root);binary_exists=(Test-Path -LiteralPath $binary);no_jobs=$true;native_pipe_peer_test=$false}}
$helperSid=Sid $name
if ((Get-ItemPropertyValue -LiteralPath ('HKLM:\SYSTEM\CurrentControlSet\Services\'+$name) -Name ServiceSidType) -ne 1) {throw 'Unrestricted own helper service SID required'}
if ($service.StartName -notin @('LocalSystem','NT AUTHORITY\SYSTEM') -or $service.PathName -cne $helperCommand -or $service.StartMode -cne 'Manual' -or $service.ServiceType -cne 'Own Process') {throw 'Fixed SYSTEM own-process service configuration'}
Check $root $true @($agentSid,$helperSid) @();Check $state $true @() @($helperSid)
foreach($path in @($settingsPath,$pinPath,$binary)) {Check $path $false @($agentSid,$helperSid) @()}
if ((Hash $binary) -cne $p.helper_sha256 -or (Hash $settingsPath) -cne $p.policy_sha256 -or (Hash $pinPath) -cne $p.pin_sha256) {throw 'Exact retained helper image and public policy'}
[void](Configurations $helperSid)
$owner=$null
if ($service.State -eq 'Running') {
$proc=Get-CimInstance Win32_Process -Filter ('ProcessId='+$service.ProcessId) -OperationTimeoutSec 10
$owner=(Invoke-CimMethod -InputObject $proc -MethodName GetOwnerSid -OperationTimeoutSec 10).Sid
if ($owner -cne $sys -or $proc.ExecutablePath -cne $binary) {throw 'Actual own SYSTEM helper process'}
}
$ledger=Join-Path $state 'ledger.sqlite';$lock=Join-Path $state 'executor.lock'
foreach($path in @($ledger,$lock)) {if (Test-Path -LiteralPath $path) {Check $path $false @() @($helperSid)}}
$probePath='C:\ProgramData\RustDiscovery\state\helper-probe.json';$probe=$null
if (Test-Path -LiteralPath $probePath) {
$item=Get-Item -LiteralPath $probePath -Force
if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -or $item.Length -gt 8192) {throw 'Bounded current agent IPC witness'}
$acl=Get-Acl -LiteralPath $probePath
if ($acl.GetOwner([Security.Principal.SecurityIdentifier]).Value -notin @($sys,$admin,'S-1-5-19',$agentSid)) {throw 'Own agent IPC witness owner'}
foreach($rule in $acl.GetAccessRules($true,$true,[Security.Principal.SecurityIdentifier])) {if ($rule.AccessControlType -ne 'Allow' -or $rule.IdentityReference.Value -notin @($sys,$admin,'S-1-5-19',$agentSid)) {throw 'Own agent IPC witness DACL'}}
$probe=Get-Content -LiteralPath $probePath -Raw | ConvertFrom-Json
}
return [ordered]@{service_exists=$true;service_state=$service.State;process_id=$service.ProcessId;owner_sid=$owner;helper_service_sid=$helperSid;helper_sha256=(Hash $binary);policy_sha256=(Hash $settingsPath);pin_sha256=(Hash $pinPath);agent=$agent;enable_effects=$false;allow_reboot=$false;allowed_services=@();ledger_exists=(Test-Path -LiteralPath $ledger);executor_lock_exists=(Test-Path -LiteralPath $lock);private_state_acl_sha256=(Acl-Hash $state);agent_probe=$probe;no_jobs=$true;native_system_service_running=($service.State -eq 'Running');native_pipe_peer_test=$false;clock_adapter_qualified=$false}
}
Check $bootstrap $true @() @()
if ($p.binding.agent_id -cne '63ea15b2-0643-403d-b5b4-cba1288dbea3' -or $p.binding.asset_id -cne 'a176520e-8a91-4e93-a78b-8e7620f430e0' -or $p.binding.public_key_hex -notmatch '^[0-9a-f]{64}$' -or $p.binding.enrollment_generation -ne 1 -or $p.binding.assignment_version -le 0) {throw 'Public signing binding, never private enrollment material'}
if ($p.action -eq 'readback') { $intent=Read-Intent;Existing-State $intent | ConvertTo-Json -Depth 24 -Compress;exit 0 }
if ($p.action -eq 'install') {
if ((Test-Path -LiteralPath $folder) -or (Test-Path -LiteralPath $active) -or (Test-Path -LiteralPath $root) -or (Test-Path -LiteralPath $binary) -or $null -ne (Get-Service -Name $name -ErrorAction SilentlyContinue)) {throw 'Unknown existing helper state: readback only, never adopt or reset a ledger'}
Check 'C:\Program Files\RustDiscovery' $true @($agentSid) @()
$before=Agent-State
Check $uploaded $false @() @()
if ((Hash $uploaded) -cne $p.helper_sha256) {throw 'Exact qualified upload'}
# SID derivation is deterministic even before CreateService; no service effects
# are needed to prepare reciprocal configuration.
$helperSid=$p.helper_service_sid
if ($helperSid -notmatch '^S-1-5-80-([0-9]+-){4}[0-9]+$') {throw 'Fixed deterministic helper service SID'}
$settingsBytes=Configurations $helperSid
New-Item -ItemType Directory -Path $folder | Out-Null;Protect $folder $true @() @()
Write-New $active @{id=$p.id;source_sha256=$p.source_sha256;no_jobs=$true}
Write-New (Join-Path $folder 'intent.json') @{payload=$p;agent=$before;no_jobs=$true}
Write-New (Join-Path $folder 'install-attempt.json') @{install_once=$true;no_jobs=$true;enable_effects=$false}
# Every mutation follows the retained intent. No existing path is overwritten.
New-Item -ItemType Directory -Path $root | Out-Null;Protect $root $true @($agentSid,$helperSid) @()
New-Item -ItemType Directory -Path $state | Out-Null;Protect $state $true @() @($helperSid)
$source=[IO.File]::Open($uploaded,[IO.FileMode]::Open,[IO.FileAccess]::Read,[IO.FileShare]::Read)
try {$target=[IO.File]::Open($binary,[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read);try {$source.CopyTo($target);$target.Flush($true)} finally {$target.Dispose()}} finally {$source.Dispose()}
Protect $binary $false @($agentSid,$helperSid) @()
foreach($pair in @(@($settingsPath,$p.settings_base64),@($pinPath,$p.pin_base64))) {
$bytes=[Convert]::FromBase64String($pair[1]);$file=[IO.File]::Open($pair[0],[IO.FileMode]::CreateNew,[IO.FileAccess]::Write,[IO.FileShare]::Read)
try {$file.Write($bytes,0,$bytes.Length);$file.Flush($true)} finally {$file.Dispose()}
Protect $pair[0] $false @($agentSid,$helperSid) @()
}
New-Service -Name $name -BinaryPathName $helperCommand -DisplayName 'RustDiscovery SYSTEM Helper' -StartupType Manual | Out-Null
$sc=Join-Path $env:WINDIR 'System32\sc.exe'
& $sc sidtype $name unrestricted | Out-Null;if ($LASTEXITCODE -ne 0) {throw 'Own helper service SID'}
if ((Sid $name) -cne $helperSid) {throw 'Actual SCM service SID differs from pin'}
$after=Existing-State @{agent=$before}
if ($after.service_state -cne 'Stopped' -or $after.ledger_exists -or $after.executor_lock_exists) {throw 'Installed but deliberately not yet started'}
Write-New (Join-Path $folder 'installed.json') $after
$after|ConvertTo-Json -Depth 24 -Compress;exit 0
}
$intent=Read-Intent
if (!(Test-Path -LiteralPath (Join-Path $folder 'installed.json'))) {throw 'Complete exact installation receipt required'}
$before=Existing-State $intent
if ($before.service_state -cne 'Stopped' -or $before.ledger_exists -or $before.executor_lock_exists) {throw 'First passive start only; preserve any existing state'}
# Never replay Start-Service after a timeout/lost reply or service failure.
Write-New (Join-Path $folder 'start-attempt.json') @{start_once=$true;no_jobs=$true;enable_effects=$false;allow_reboot=$false}
Start-Service -Name $name
(Get-Service -Name $name).WaitForStatus([ServiceProcess.ServiceControllerStatus]::Running,[TimeSpan]::FromSeconds(20))
$after=Existing-State $intent
if ($after.service_state -cne 'Running' -or !$after.ledger_exists -or !$after.executor_lock_exists) {throw 'Running own SYSTEM service with retained private ledger/lock required'}
Write-New (Join-Path $folder 'started.json') $after
$after|ConvertTo-Json -Depth 24 -Compress
'''
PREPARE = base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1', 'SystemHelperBootstrap.ps1')
LAUNCH = base.LAUNCH_SCRIPT.replace('TransportAcceptance.ps1', 'SystemHelperBootstrap.ps1').replace('RmmTransportPayload', 'RmmSystemHelperPayload')
UPLOAD_PREPARE = base.PREPARE_SCRIPT.replace('TransportAcceptance.ps1', 'rmm-helper-windows.exe')
def sha(path):
return hashlib.sha256(Path(path).read_bytes()).hexdigest()
def canonical(value):
return json.dumps(value, ensure_ascii=False, separators=(',', ':')).encode()
def store(folder, name, value):
path = folder / name
fd = os.open(path, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_NOFOLLOW, 0o600)
with os.fdopen(fd, 'wb') as out:
out.write(json.dumps(value, indent=2).encode());out.flush();os.fsync(out.fileno())
return path
def private_dir(path):
path.mkdir(mode=0o700, parents=True, exist_ok=True)
require(not path.is_symlink() and path.stat().st_mode & 0o077 == 0, 'private bootstrap evidence directory')
def service_sid(name):
# MS-LSAT 3.1.1.1.2 Configurable Translation Database:
# https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-lsat/252cb6b0-2edc-4403-9d75-5b2786ba859e
# SERVICE SID: SHA-1 of uppercase UTF-16LE service name, five
# little-endian 32-bit subauthorities. Native LookupAccountName verifies it.
import struct
digest = hashlib.sha1(name.upper().encode('utf-16le')).digest()
return 'S-1-5-80-' + '-'.join(str(n) for n in struct.unpack('<5I', digest))
def qualified_helper(artifact):
proof = cb.contract.protected_json(QUALIFICATION)
require(proof.get('verified') is True and proof.get('passed') == 21 and proof.get('binary_sha256') == UNIT_SHA and proof.get('no_jobs') is True and proof.get('native_system_helper_service_test') is False and proof.get('native_pipe_peer_test') is False, 'actual 21-unit qualification, no invented service/pipe proof')
require(sha(artifact) == HELPER_SHA, 'exact associated deployable helper artifact; unit executable is different')
for path, digest in OWN_SOURCES.items():
require(proof['source_files'].get(path) == digest and sha(cb.ROOT / path) == digest, 'qualified helper codec/SCM sources unchanged')
return {'path': str(QUALIFICATION), 'sha256': sha(QUALIFICATION), 'unit_binary_sha256': UNIT_SHA, 'helper_binary_sha256': HELPER_SHA}
def current_runtime():
index_path = cb.EVIDENCE / 'current-runtime.json'
index = cb.contract.protected_json(index_path)
require(index.get('type') == 'rmm.windows.current-runtime' and index.get('schema_version') == 1 and index.get('agent_id') == AGENT_ID and index.get('asset_id') == ASSET_ID and index.get('transport') == 'auto' and index.get('windows_jobs_enabled') is False and index.get('clock_adapter_qualified') is False, 'accepted retained passive runtime index')
evidence = Path(index['evidence']['path']).resolve()
require(evidence.is_relative_to(cb.EVIDENCE.resolve()) and sha(evidence) == index['evidence']['sha256'], 'exact current-runtime evidence')
result = cb.contract.protected_json(evidence)
require(result.get('verified') is True and result.get('completed') is True and result.get('restored') is False and result.get('no_jobs') is True, 'actual accepted current runtime')
require(index.get('system_helper_client_feature') is True and result.get('system_helper_client_feature') is True, 'accepted optional IPC client must precede helper install; old passive f64 is not an IPC client')
native = result['native']
require(native['binary_sha256'] == index['binary_sha256'] and re.fullmatch('[0-9a-f]{64}', native['config_sha256']), 'actual current binary/configuration association')
return index, {'path': str(index_path), 'sha256': sha(index_path), 'evidence': index['evidence']}, native
def binding_file(path):
value = cb.contract.protected_json(path)
fields = ('tenant_id','asset_id','agent_id','enrollment_generation','assignment_version','public_key_hex')
require(set(value) == set(fields) and value['agent_id'] == AGENT_ID and value['asset_id'] == ASSET_ID and value['enrollment_generation'] == 1 and type(value['assignment_version']) is int and value['assignment_version'] > 0 and re.fullmatch('[0-9a-f]{64}', value['public_key_hex']), 'public signing binding, never tokens or private keys')
require(str(uuid.UUID(value['tenant_id'])) == value['tenant_id'] and uuid.UUID(value['tenant_id']).int != 0, 'explicit tenant binding')
return {field:value[field] for field in fields}
def configurations(binding, agent_sha):
helper_sid = service_sid('RmmDiscoverySystemHelper')
agent = dict(service_name='RmmDiscoveryWindows',service_sid=service_sid('RmmDiscoveryWindows'),user_sid='S-1-5-19',binary=r'C:\Program Files\RustDiscovery\rmm-agent-windows.exe',binary_sha256=agent_sha)
helper = dict(service_name='RmmDiscoverySystemHelper',service_sid=helper_sid,user_sid='S-1-5-18',binary=r'C:\Program Files\RustDiscovery\rmm-helper-windows.exe',binary_sha256=HELPER_SHA)
settings = dict(schema_version=1,binding=binding,agent=agent,helper=helper,allowed_services=[],max_runtime_seconds=30,enable_effects=False,allow_reboot=False)
raw = canonical(settings);policy = hashlib.sha256(raw).hexdigest()
pin = dict(schema_version=1,binding=binding,policy_sha256=policy,agent=agent,helper=helper)
return raw, canonical(pin), helper_sid, policy
def transfer(path, name, digest, preparation):
status = cb.powershell(preparation, {'sha256':digest})
if not status['already_uploaded']:
prefix = cb.contract.protected_json(cb.ROOT / 'credentials/windows-dev-access.json')['ssh_prefix']
args = ['sftp'] + [part for part in prefix[1:-1] if part != '-T'] + ['-b','-',prefix[-1]]
target = '/C:/ProgramData/RustDiscoveryBootstrap/' + digest + '/' + name
result = subprocess.run(args,input=('put '+json.dumps(str(path.resolve()))+' '+json.dumps(target)+'\n').encode(),capture_output=True,timeout=120)
require(result.returncode == 0, 'fixed bootstrap SFTP failed')
require(cb.powershell(preparation, {'sha256':digest})['already_uploaded'], 'exact immutable staged artifact')
return digest
def prepare(artifact, binding_path):
# All inputs checked and durable locally before the first remote mutation.
qualification = qualified_helper(artifact)
index, runtime, native = current_runtime()
binding = binding_file(binding_path)
raw, pin, helper_sid, policy = configurations(binding, index['binary_sha256'])
import base64
ident = str(uuid.uuid4());private_dir(STATE);folder = STATE / ident;folder.mkdir(mode=0o700)
source_sha = hashlib.sha256(SCRIPT.encode()).hexdigest()
payload = dict(id=ident,helper_sha256=HELPER_SHA,agent_sha256=index['binary_sha256'],agent_acl_sha256=native['binary_acl_sha256'],config_sha256=native['config_sha256'],config_acl_sha256=native['config_acl_sha256'],source_sha256=source_sha,binding=binding,helper_service_sid=helper_sid,policy_sha256=policy,pin_sha256=hashlib.sha256(pin).hexdigest(),settings_base64=base64.b64encode(raw).decode(),pin_base64=base64.b64encode(pin).decode())
store(folder,'intent.json',dict(schema_version=1,type='rmm.windows.passive-system-helper',payload=payload,qualification=qualification,current_runtime=runtime,source_file_sha256=sha(__file__),no_jobs=True,helper_enable_effects=False,allow_reboot=False))
source = folder / 'SystemHelperBootstrap.ps1'
fd = os.open(source, os.O_CREAT|os.O_EXCL|os.O_WRONLY|os.O_NOFOLLOW, 0o600)
with os.fdopen(fd,'wb') as out:out.write(SCRIPT.encode());out.flush();os.fsync(out.fileno())
store(folder,'upload-attempt.json',{'helper_sha256':HELPER_SHA,'source_sha256':source_sha})
transfer(artifact, 'rmm-helper-windows.exe', HELPER_SHA, UPLOAD_PREPARE)
transfer(source, 'SystemHelperBootstrap.ps1', source_sha, PREPARE)
store(folder,'prepared.json',{'prepared':True,'source_sha256':source_sha,'helper_sha256':HELPER_SHA,'no_service_changes':True})
return {'case_id':ident,'prepared':True,'no_service_changes':True}
def run(ident, action):
require(str(uuid.UUID(ident)) == ident, 'canonical bootstrap case')
folder = STATE / ident
intent = cb.contract.protected_json(folder / 'intent.json')
require(intent['source_file_sha256'] == sha(__file__) and intent['payload']['source_sha256'] == hashlib.sha256(SCRIPT.encode()).hexdigest(), 'exact prepared runner/source; never silently rebase case')
require(cb.contract.protected_json(folder / 'prepared.json')['prepared'] is True, 'completed source/artifact upload')
index_path = Path(intent['current_runtime']['path'])
require(sha(index_path) == intent['current_runtime']['sha256'], 'accepted runtime index unchanged during bootstrap')
payload = dict(intent['payload'], action='readback' if action == 'verify-peer' else action)
if action not in ('readback','verify-peer'):
store(folder, action+'-attempt.json', {'action_once':action,'source_sha256':payload['source_sha256'],'no_jobs':True})
else:
# Immutable readbacks can be repeated safely with fresh evidence UUIDs.
store(folder,'readback-intent-'+str(uuid.uuid4())+'.json',{'read_only':True,'no_jobs':True})
result = cb.powershell(LAUNCH, {'sha256':payload['source_sha256'],'payload':payload}, timeout=90, remote_signed=True)
store(folder,action+'-result-'+str(uuid.uuid4())+'.json',result)
if action == 'verify-peer':
verify_peer(result, payload)
receipt={'verified':True,'case_id':ident,'native_system_helper_service_test':True,'native_pipe_peer_test':True,'source':'actual_local_service_runtime','agent_process_id':result['agent']['process_id'],'helper_process_id':result['process_id'],'challenge':result['agent_probe']['challenge'],'policy_sha256':payload['policy_sha256'],'helper_sha256':HELPER_SHA,'clock_adapter_qualified':False,'system_capability_advertised':False,'no_jobs':True,'native':result}
store(folder,'peer-verified-'+str(uuid.uuid4())+'.json',receipt)
return receipt
return {'case_id':ident,'action':action,'native':result,'no_jobs':True,'native_pipe_peer_test':False}
def verify_peer(result, payload):
"""The SSH caller only reads a fresh receipt written by the real service.
PeerProof/require_self inside the accepted Agent SDK and helper bind kernel
pipe PIDs, SCM PIDs, users/service SIDs, creation/aliveness and pinned images.
An Admin process that directly opens the pipe cannot emit this success.
"""
from datetime import datetime, timezone
require(result.get('native_system_service_running') is True and result.get('owner_sid') == 'S-1-5-18' and result.get('enable_effects') is False and result.get('allow_reboot') is False and result.get('allowed_services') == [] and result.get('ledger_exists') is True and result.get('executor_lock_exists') is True, 'actual passive SYSTEM helper with private persistent files')
probe=result.get('agent_probe') or {}
require(probe.get('schema_version') == 1 and probe.get('source') == 'actual_local_service_runtime' and probe.get('agent_process_id') == result['agent']['process_id'] and probe.get('agent_service') == 'RmmDiscoveryWindows' and probe.get('agent_id') == AGENT_ID and probe.get('asset_id') == ASSET_ID, 'actual current LocalService agent wrote the pipe receipt')
require(probe.get('authenticated_pipe_exchange') is True and probe.get('client_service_pid_verified_by_helper') is True and probe.get('verified_helper_user_sid') == 'S-1-5-18' and probe.get('verified_helper_service_sid') == payload['helper_service_sid'] and probe.get('helper_binary_sha256') == HELPER_SHA and probe.get('policy_sha256') == payload['policy_sha256'], 'reciprocal exact role/image/policy pipe proof')
require(probe.get('helper_clock_qualified') is False and probe.get('helper_ready') is False and probe.get('system_capability_advertised') is False and probe.get('no_jobs') is True, 'pipe connectivity cannot open the clock or a job capability')
observed=datetime.fromisoformat(probe['observed_at'].replace('Z','+00:00'))
# Freshness here is only acceptance telemetry, never a trusted job clock.
require(0 <= (datetime.now(timezone.utc)-observed).total_seconds() <= 90, 'fresh actual agent receipt; wait for normal periodic probe')
require(str(uuid.UUID(probe['challenge'])) == probe['challenge'], 'actual challenge UUID')
for name in ('ledger_file_id','executor_lock_file_id'):
value=probe.get(name) or {}
require(type(value.get('volume_serial')) is int and value['volume_serial'] > 0 and isinstance(value.get('file_id'),list) and len(value['file_id']) == 16 and all(type(n) is int and 0 <= n <= 255 for n in value['file_id']) and any(value['file_id']), 'actual helper-held private Windows file identity')
def main():
parser=argparse.ArgumentParser(description=__doc__)
parser.add_argument('action',choices=['prepare','install','start','readback','verify-peer'])
parser.add_argument('--artifact',type=Path)
parser.add_argument('--public-binding',type=Path)
parser.add_argument('--case')
args=parser.parse_args()
if args.action == 'prepare':
require(args.artifact is not None and args.public_binding is not None, 'prepare requires exact artifact and protected PUBLIC binding')
result=prepare(args.artifact,args.public_binding)
else:
require(args.case is not None, 'explicit existing bootstrap case required')
result=run(args.case,args.action)
print(json.dumps(result))
if __name__ == '__main__':
main()
@@ -0,0 +1,49 @@
"""Focused source-only guards for the new passive feature-upgrade namespace."""
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import uuid
import runtime_feature_upgrade as feature
class FeatureUpgradeGuards(unittest.TestCase):
def test_historical_source_derivation_is_exact_and_owns_new_namespace(self):
source=feature.UPGRADE
self.assertIn("$p.old_sha256 -cne '"+feature.OLD+"'",source)
self.assertNotIn("$p.old_sha256 -cne '"+feature.ORIGINAL+"'",source)
self.assertIn("\\runtime-feature-active-intent.json'",source)
self.assertNotIn("\\runtime-active-intent.json'",source)
self.assertIn("\\runtime-feature-'+$p.id",source)
self.assertIn('Complete passive feature upgrade before any helper/pin installation',source)
self.assertIn('Original six-field passive configuration',source)
with self.assertRaises(Exception):feature.replace_once('same same','same','changed')
def test_old_default_profile_cannot_claim_new_feature_qualification(self):
with self.assertRaises(Exception):
feature.stable(dict(schema_version=1,architecture=feature.TARGET,default_features=True,system_helper_feature=False,system_helper_client_feature=False),current_sources=False)
def test_restore_then_finish_cannot_publish_historical_new_runtime(self):
with tempfile.TemporaryDirectory() as temporary:
folder=Path(temporary);ident=str(uuid.uuid4())
feature.write(folder,'rollback-intent.json',{'started_at':'2026-11-14T00:00:00+00:00'})
feature.write(folder,'before.json',{'fixture':True})
manifest={'artifacts':{'agent':{'sha256':'11'*32}}}
native={'completed':True,'read_only':True,'restored':False,'native':{'binary_sha256':'11'*32}}
with patch.object(feature,'load_case',return_value=(folder,{},manifest)),patch.object(feature,'remote',return_value=native),patch.object(feature,'publish') as publish:
with self.assertRaises(Exception):feature.finish(ident)
publish.assert_not_called()
def test_current_runtime_cas_rejects_another_transition(self):
with tempfile.TemporaryDirectory() as temporary:
folder=Path(temporary);current=folder/'index.json'
feature.write(folder,'index.json',{'case_id':str(uuid.uuid4())})
feature.write(folder,'result.json',{'verified':True})
with patch.object(feature,'CURRENT',current):
with self.assertRaises(Exception):
feature.publish(folder,'result.json',{'previous_runtime_index_sha256':'00'*32},'11'*32,False)
self.assertEqual(set(path.name for path in folder.iterdir()),{'index.json','result.json'})
if __name__=='__main__':unittest.main()
@@ -0,0 +1,56 @@
"""Focused local bootstrap guards. No SSH, service or RMM job is executed."""
import copy
from datetime import datetime, timezone, timedelta
import hashlib
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch
import uuid
import system_helper_bootstrap as helper
class BootstrapGuards(unittest.TestCase):
def test_passive_public_policy_uses_exact_roles_and_empty_effect_allowlist(self):
import json
binding=dict(tenant_id=str(uuid.uuid4()),asset_id=helper.ASSET_ID,agent_id=helper.AGENT_ID,enrollment_generation=1,assignment_version=1,public_key_hex='11'*32)
raw,pin,sid,policy=helper.configurations(binding,'22'*32)
settings=json.loads(raw)
self.assertEqual(settings['allowed_services'],[])
self.assertIs(settings['enable_effects'],False)
self.assertIs(settings['allow_reboot'],False)
self.assertEqual(settings['agent']['user_sid'],'S-1-5-19')
self.assertEqual(settings['helper']['user_sid'],'S-1-5-18')
self.assertEqual(settings['helper']['service_sid'],sid)
self.assertEqual(hashlib.sha256(raw).hexdigest(),policy)
self.assertEqual(json.loads(pin)['policy_sha256'],policy)
# MS-LSAT documented ALG vector confirms ordering and byte endianness.
self.assertEqual(helper.service_sid('ALG'),'S-1-5-80-2387347252-3645287876-2469496166-3824418187-3586569773')
def test_lost_install_reply_cannot_replay_mutation(self):
with tempfile.TemporaryDirectory() as temporary:
state=Path(temporary);ident=str(uuid.uuid4());folder=state/ident;folder.mkdir(mode=0o700)
index=helper.store(state,'index.json',{'fixture':True})
helper.store(folder,'intent.json',{'source_file_sha256':helper.sha(helper.__file__),'payload':{'id':ident,'source_sha256':hashlib.sha256(helper.SCRIPT.encode()).hexdigest()},'current_runtime':{'path':str(index),'sha256':helper.sha(index)}})
helper.store(folder,'prepared.json',{'prepared':True})
with patch.object(helper,'STATE',state),patch.object(helper.cb,'powershell',side_effect=RuntimeError('simulated lost SSH reply')) as rpc:
with self.assertRaisesRegex(RuntimeError,'simulated lost'):
helper.run(ident,'install')
with self.assertRaises(FileExistsError):
helper.run(ident,'install')
self.assertEqual(rpc.call_count,1)
self.assertTrue((folder/'install-attempt.json').exists())
def test_admin_or_stale_or_ready_receipt_is_not_positive_pipe_acceptance(self):
payload={'helper_service_sid':helper.service_sid('RmmDiscoverySystemHelper'),'policy_sha256':'33'*32}
probe=dict(schema_version=1,source='actual_local_service_runtime',agent_process_id=100,agent_service='RmmDiscoveryWindows',agent_id=helper.AGENT_ID,asset_id=helper.ASSET_ID,authenticated_pipe_exchange=True,client_service_pid_verified_by_helper=True,verified_helper_user_sid='S-1-5-18',verified_helper_service_sid=payload['helper_service_sid'],helper_binary_sha256=helper.HELPER_SHA,policy_sha256=payload['policy_sha256'],helper_clock_qualified=False,helper_ready=False,system_capability_advertised=False,no_jobs=True,observed_at=datetime.now(timezone.utc).isoformat(),challenge=str(uuid.uuid4()),ledger_file_id={'volume_serial':1,'file_id':[1]*16},executor_lock_file_id={'volume_serial':1,'file_id':[2]*16})
result=dict(native_system_service_running=True,owner_sid='S-1-5-18',enable_effects=False,allow_reboot=False,allowed_services=[],ledger_exists=True,executor_lock_exists=True,agent={'process_id':100},agent_probe=probe)
helper.verify_peer(result,payload)
for field,value in [('source','administrator_ssh_probe'),('agent_process_id',999),('helper_ready',True),('helper_clock_qualified',True),('policy_sha256','44'*32),('observed_at',(datetime.now(timezone.utc)-timedelta(seconds=100)).isoformat())]:
bad=copy.deepcopy(result);bad['agent_probe'][field]=value
with self.assertRaises(Exception,msg=field):helper.verify_peer(bad,payload)
if __name__ == '__main__':
unittest.main()