From 45ce6223708130b388d78d0d35ad27176da33cb4 Mon Sep 17 00:00:00 2001 From: Kevin Janssen Date: Sun, 15 Nov 2026 21:51:58 +0100 Subject: [PATCH] Check the pinned Windows profile unit and installed PowerShell host --- .../windows/Test-LocalServiceProfileBuild.ps1 | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 deploy/windows/Test-LocalServiceProfileBuild.ps1 diff --git a/deploy/windows/Test-LocalServiceProfileBuild.ps1 b/deploy/windows/Test-LocalServiceProfileBuild.ps1 new file mode 100644 index 0000000..b9c8c48 --- /dev/null +++ b/deploy/windows/Test-LocalServiceProfileBuild.ps1 @@ -0,0 +1,100 @@ +#requires -Version 5.1 +#requires -RunAsAdministrator +[CmdletBinding()] +param() +$ErrorActionPreference = 'Stop' + +function Assert-PrivateUnitPath([string]$Path, [bool]$Directory) { + $item = Get-Item -LiteralPath $Path -Force + if ($item.PSIsContainer -ne $Directory -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint)) { + throw 'Unexpected unit path type' + } + $acl = Get-Acl -LiteralPath $Path + $trusted = @('S-1-5-18', 'S-1-5-32-544') + $sidType = [Security.Principal.SecurityIdentifier] + if ($acl.GetOwner($sidType).Value -notin $trusted) { throw 'Unexpected unit path owner' } + $rules = @($acl.GetAccessRules($true, $true, $sidType)) + if (!$rules.Count) { throw 'Missing unit path ACL' } + foreach ($rule in $rules) { + if ($rule.AccessControlType -ne 'Allow' -or $rule.IdentityReference.Value -notin $trusted) { + throw 'Unexpected unit path ACL' + } + } +} + +$bootstrap = 'C:\ProgramData\RustDiscoveryBootstrap' +$unit = Join-Path $PSScriptRoot 'rmm_agent_windows-9b3e91d827c6ff16.exe' +$unitHash = '7d1715dc105c1eb482405511e1b3abf4437c448e186e73923c43dd3c7938710e' +$test = 'runtime::tests::remote_signed_requires_explicit_profile_jobs_and_binding' +if ((Split-Path -Parent $PSScriptRoot) -ne $bootstrap) { throw 'Private bootstrap directory required' } +Assert-PrivateUnitPath $bootstrap $true +Assert-PrivateUnitPath $PSScriptRoot $true +Assert-PrivateUnitPath $PSCommandPath $false +Assert-PrivateUnitPath $unit $false +foreach ($entry in (Get-ChildItem -LiteralPath $PSScriptRoot -Force)) { + if ($entry.Name -notin @([IO.Path]::GetFileName($PSCommandPath), [IO.Path]::GetFileName($unit))) { + throw 'Unit staging directory must contain only the runner and the unit executable' + } +} +$system32 = [Environment]::SystemDirectory +$shell = Join-Path $system32 'WindowsPowerShell\v1.0\powershell.exe' +if (![Environment]::Is64BitProcess -or [Diagnostics.Process]::GetCurrentProcess().MainModule.FileName -ne $shell -or + $PSVersionTable.PSEdition -ne 'Desktop' -or $PSVersionTable.PSVersion.Major -ne 5 -or $PSVersionTable.PSVersion.Minor -ne 1) { + throw 'Run with the installed 64-bit Windows PowerShell 5.1' +} +$policies = [ordered]@{} +foreach ($scope in 'MachinePolicy','UserPolicy','Process','CurrentUser','LocalMachine') { + $policies[$scope] = [string](Microsoft.PowerShell.Security\Get-ExecutionPolicy -Scope $scope) +} + +$handle = [IO.File]::Open($unit, 'Open', 'Read', 'Read') +try { + $hasher = [Security.Cryptography.SHA256]::Create() + try { $actual = [BitConverter]::ToString($hasher.ComputeHash($handle)).Replace('-','').ToLowerInvariant() } + finally { $hasher.Dispose() } + if ($actual -cne $unitHash -or $handle.Length -ne 18608199) { throw 'Unexpected unit executable' } + $start = New-Object Diagnostics.ProcessStartInfo + $start.FileName = $unit + $start.Arguments = "--exact $test --test-threads=1 --color never" + $start.WorkingDirectory = $PSScriptRoot + $start.UseShellExecute = $false + $start.CreateNoWindow = $true + $start.RedirectStandardOutput = $true + $start.RedirectStandardError = $true + $start.EnvironmentVariables.Clear() + $windows = Split-Path -Parent $system32 + $start.EnvironmentVariables['SystemRoot'] = $windows + $start.EnvironmentVariables['WINDIR'] = $windows + $start.EnvironmentVariables['SystemDrive'] = [IO.Path]::GetPathRoot($windows).TrimEnd('\') + $start.EnvironmentVariables['PATH'] = $system32 + $proc = [Diagnostics.Process]::Start($start) + try { + $stdout = $proc.StandardOutput.ReadToEndAsync() + $stderr = $proc.StandardError.ReadToEndAsync() + if (!$proc.WaitForExit(30000)) { + $proc.Kill() + throw 'Fixed unit test timed out' + } + if (!$stdout.Wait(2000) -or !$stderr.Wait(2000) -or $proc.ExitCode -ne 0 -or $stderr.Result.Length -gt 0) { + throw 'Fixed unit test failed' + } + $lines = @($stdout.Result -split '\r?\n' | Where-Object { $_ }) + if ($lines.Count -ne 3 -or $lines[0] -cne 'running 1 test' -or $lines[1] -cne "test $test ... ok" -or + $lines[2] -cnotmatch '^test result: ok\. 1 passed; 0 failed; 0 ignored; 0 measured; (\d+) filtered out; finished in \d+(?:\.\d+)?s$') { + throw 'Unexpected test result or filter count' + } + $filtered = [int]$Matches[1] + } finally { $proc.Dispose() } +} finally { $handle.Dispose() } + +[ordered]@{ + observed_at = [DateTimeOffset]::UtcNow.ToString('o') + unit_sha256 = $actual + test_name = $test + passed = 1 + filtered_out = $filtered + powershell_path = $shell + powershell_sha256 = (Get-FileHash -LiteralPath $shell -Algorithm SHA256).Hash.ToLowerInvariant() + powershell_version = $PSVersionTable.PSVersion.ToString() + admin_host_policy_scopes = $policies +} | ConvertTo-Json -Depth 3 -Compress