smoke-kill.ps1 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <#
  2. Kill-chain invariant smoke (K1/K2): NO sandbox process survives
  3. the broker. Pins the ground-truthed probe result that the
  4. broker→runner `AssignProcessToJobObject` succeeds and the
  5. kill-on-close Job cascade reaps the whole tree — if a future
  6. Windows build (or a lockdown change) regresses the assign, these
  7. rows turn CI red instead of silently orphaning trees.
  8. K1: hard kill — `Stop-Process -Force` on the broker (no cleanup
  9. code runs) → broker→runner→cmd→PING all gone within 5s via
  10. the Job cascade.
  11. K2: graceful exit — the child finishes on its own, the broker
  12. returns normally (the always-armed TerminateProcess guard
  13. no-ops on the already-exited runner) → tree gone.
  14. Asserts on `Get-Process -Id … -ea SilentlyContinue` emptiness,
  15. NOT on Job membership or assign rc — the invariant is "no
  16. survivors", not "assigned to job".
  17. #>
  18. param(
  19. [Parameter(Mandatory = $true, Position = 0)]
  20. [string] $Exe
  21. )
  22. $ErrorActionPreference = 'Stop'
  23. # Fixed test-only sublayer; distinct from the other smoke scripts.
  24. # Referenced verbatim by the workflow's always()-cleanup step.
  25. $Sublayer = '4c1f7a2e-8b9d-4e3a-9f6c-1d7e5a0b8c4f'
  26. $PortRange = '60080-60089'
  27. function Run { param([string[]] $argv)
  28. & $Exe @argv
  29. if ($LASTEXITCODE -ne 0) {
  30. throw "srt-win $($argv -join ' ') exited $LASTEXITCODE"
  31. }
  32. }
  33. $cmd = Join-Path $env:SystemRoot 'System32\cmd.exe'
  34. Write-Host "smoke-kill: sublayer=$Sublayer exe=$Exe"
  35. try { Start-Service seclogon -ea Stop } catch {
  36. Write-Host "smoke-kill: WARNING: Start-Service seclogon: $_"
  37. }
  38. Run @('install','--sublayer-guid',$Sublayer,'--proxy-port-range',$PortRange)
  39. # ── Tree helpers ────────────────────────────────────────────────────
  40. # Identify sandbox procs by ParentProcessId chain, NOT GetOwner —
  41. # self-protect denies the owner query to a non-elevated caller.
  42. function Get-TreePids { param([int] $Root)
  43. # Single-snapshot BFS over Win32_Process on ParentProcessId.
  44. $all = Get-CimInstance Win32_Process |
  45. Select-Object ProcessId, ParentProcessId, Name
  46. $out = New-Object System.Collections.Generic.List[object]
  47. $seen = New-Object System.Collections.Generic.HashSet[int]
  48. $q = New-Object System.Collections.Generic.Queue[int]
  49. $q.Enqueue($Root); [void]$seen.Add($Root)
  50. $out.Add(($all | Where-Object { $_.ProcessId -eq $Root }))
  51. while ($q.Count -gt 0) {
  52. $p = $q.Dequeue()
  53. foreach ($c in $all | Where-Object { $_.ParentProcessId -eq $p }) {
  54. if ($seen.Add($c.ProcessId)) {
  55. $out.Add($c); $q.Enqueue($c.ProcessId)
  56. }
  57. }
  58. }
  59. return $out
  60. }
  61. function Assert-Gone { param([int[]] $Pids, [string] $Tag, [int] $WaitSec = 5)
  62. $deadline = [DateTime]::UtcNow.AddSeconds($WaitSec)
  63. do {
  64. $alive = @(Get-Process -Id $Pids -ea SilentlyContinue |
  65. Select-Object -Expand Id)
  66. if ($alive.Count -eq 0) {
  67. Write-Host ("${Tag} ok: all $($Pids.Count) tree PID(s) gone " +
  68. "within $WaitSec s")
  69. return
  70. }
  71. Start-Sleep -Milliseconds 200
  72. } while ([DateTime]::UtcNow -lt $deadline)
  73. throw ("${Tag}: $($alive.Count) survivor(s) after ${WaitSec}s: " +
  74. "$($alive -join ',') (full tree: $($Pids -join ','))")
  75. }
  76. # Launch a sandbox tree whose leaf pings for $PingCount probes
  77. # (~1s apart) and wait for the leaf, so the rows assert against a
  78. # fully-formed tree. `ping -n N` is the leaf (`timeout /t` aborts
  79. # under redirected stdin). Must use ProcessStartInfo.ArgumentList
  80. # (per-element quoting) — `Start-Process -ArgumentList` space-joins
  81. # without quoting, splitting the `cmd /c "…"` payload so `>nul`
  82. # reaches ping as a hostname.
  83. function Start-Tree {
  84. param([int] $PingCount = 300)
  85. $psi = [System.Diagnostics.ProcessStartInfo]::new()
  86. $psi.FileName = $Exe
  87. $psi.UseShellExecute = $false
  88. foreach ($a in @('exec','--quiet','--',
  89. $cmd,'/c',"ping -n $PingCount 127.0.0.1 >nul")) {
  90. [void]$psi.ArgumentList.Add($a)
  91. }
  92. $p = [System.Diagnostics.Process]::Start($psi)
  93. $bpid = $p.Id
  94. # Poll until the leaf (`PING.EXE`) is present — a depth count
  95. # alone can be inflated by conhost.exe before the leaf spawns.
  96. # 30s, not 10s: the leaf is two hops away (broker → seclogon
  97. # logon → cmd → ping), and the first CreateProcessWithLogonW on a
  98. # cold runner can take well over 10s building the sandbox user's
  99. # profile — the recurring x86-64 "leaf never appeared" flake.
  100. for ($i = 0; $i -lt 120; $i++) {
  101. Start-Sleep -Milliseconds 250
  102. if ($p.HasExited) {
  103. throw "Start-Tree: broker exited early with code $($p.ExitCode) before the leaf spawned (broker=$bpid)"
  104. }
  105. $t = @(Get-TreePids $bpid)
  106. if (($t | Where-Object { $_.Name -match '^PING\.EXE$' })) {
  107. $pids = @($t | Select-Object -Expand ProcessId)
  108. $names = ($t | ForEach-Object { "$($_.ProcessId)=$($_.Name)" }) -join ','
  109. Write-Host "smoke-kill: tree = $names (broker=$bpid)"
  110. return [pscustomobject]@{ broker = $bpid; proc = $p; pids = $pids }
  111. }
  112. }
  113. $t = @(Get-TreePids $bpid)
  114. $names = ($t | ForEach-Object { "$($_.ProcessId)=$($_.Name)" }) -join ','
  115. try { $p.Kill($true) } catch { }
  116. throw "Start-Tree: PING.EXE leaf never appeared in 30s (broker=$bpid, partial tree=[$names])"
  117. }
  118. try {
  119. # ── K1: hard-kill broker → whole tree gone ────────────────────────
  120. $t = Start-Tree
  121. Stop-Process -Id $t.broker -Force
  122. Assert-Gone -Pids $t.pids -Tag 'K1' -WaitSec 5
  123. # ── K2: graceful broker exit → whole tree gone ────────────────────
  124. # Short leaf (~7s): the child finishes, the runner returns its
  125. # exit code, the broker's armed guard no-ops on the already-dead
  126. # runner and the broker exits cleanly. Nothing may linger.
  127. $t = Start-Tree -PingCount 8
  128. if (-not $t.proc.WaitForExit(30000)) {
  129. try { $t.proc.Kill($true) } catch { }
  130. throw 'K2: broker did not exit within 30s of a ~7s child'
  131. }
  132. Write-Host "K2: broker exited (code=$($t.proc.ExitCode))"
  133. Assert-Gone -Pids $t.pids -Tag 'K2' -WaitSec 5
  134. }
  135. finally {
  136. & $Exe uninstall --sublayer-guid $Sublayer 2>&1 | Out-Null
  137. }
  138. Write-Host 'smoke-kill: PASS (K1/K2)'