smoke-ambient.ps1 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <#
  2. Install-time ambient write-deny lifecycle smoke (`ambient.rs`).
  3. Asserts the v2 install stamps Windows' stock world-writable system
  4. dirs `(D;OICI;WriteDeny)` for the sandbox SID, that the deny holds
  5. for a sandboxed child (write denied, read allowed), that a session
  6. deny stamp + restore on the same path preserves the ambient floor
  7. (the `ambient_denies` fold-in at the recompose chokepoint), and
  8. that uninstall removes it all.
  9. Self-contained: installs under a fixed test-only sublayer GUID
  10. (distinct from the other smoke scripts). NOTE the ambient stamps
  11. themselves are machine-global (one state DB), so this script runs
  12. its uninstall in `finally` — a later smoke script's install
  13. re-stamps them for itself.
  14. #>
  15. param(
  16. [Parameter(Mandatory = $true, Position = 0)]
  17. [string] $Exe
  18. )
  19. $ErrorActionPreference = 'Stop'
  20. # Fixed test-only sublayer; referenced by cleanup.ps1.
  21. $Sublayer = '9e4b2d7a-6c05-4f18-8a3d-2b9e7c1f5a0d'
  22. $PortRange = '60080-60089'
  23. function Run { param([string[]] $argv)
  24. & $Exe @argv
  25. if ($LASTEXITCODE -ne 0) {
  26. throw "srt-win $($argv -join ' ') exited $LASTEXITCODE"
  27. }
  28. }
  29. function J { param([string[]] $argv) Run $argv | ConvertFrom-Json }
  30. function Stdin { param([string[]] $argv, [string] $json)
  31. $raw = $json | & $Exe @argv 2>&1 | Out-String
  32. Write-Host -NoNewline $raw
  33. if ($LASTEXITCODE -ne 0) {
  34. throw "srt-win $($argv -join ' ') exited ${LASTEXITCODE}: $raw"
  35. }
  36. }
  37. function RunCapture { param([string[]] $argv)
  38. $raw = & $Exe @argv 2>&1 | Out-String
  39. return [pscustomobject]@{ exit = $LASTEXITCODE; raw = $raw }
  40. }
  41. # A sandboxed write to $probe must fail AND leave no file behind.
  42. function Assert-WriteDenied { param([string] $msg)
  43. & $Exe exec --quiet -- $cmd /c "echo p > `"$probe`"" 2>&1 | Out-Null
  44. if ($LASTEXITCODE -eq 0 -or (Test-Path $probe)) {
  45. Remove-Item $probe -Force -ea SilentlyContinue
  46. throw $msg
  47. }
  48. }
  49. $cmd = Join-Path $env:SystemRoot 'System32\cmd.exe'
  50. $env:SANDBOX_RUNTIME_WIN_DEBUG = '1'
  51. Write-Host "smoke-ambient: sublayer=$Sublayer exe=$Exe"
  52. try { Start-Service seclogon -ea Stop } catch {
  53. Write-Host "smoke-ambient: WARNING: Start-Service seclogon: $_"
  54. }
  55. try {
  56. # ── AM1: install stamps the ambient list ─────────────────────────
  57. $inst = RunCapture @('install','--sublayer-guid',$Sublayer,'--proxy-port-range',$PortRange)
  58. Write-Host -NoNewline $inst.raw
  59. if ($inst.exit -ne 0) { throw "install exited $($inst.exit): $($inst.raw)" }
  60. if ($inst.raw -notmatch 'ambient write-deny stamped on (\d+) system path') {
  61. throw 'AM1: install output missing the ambient-stamp line'
  62. }
  63. $us = J @('user','status')
  64. $sbSid = $us.marker_user_sid
  65. if (-not $sbSid) { throw 'setup marker missing user_sid' }
  66. $st = J @('status','--sublayer-guid',$Sublayer)
  67. if (-not $st.ambient.paths -or $st.ambient.paths.Count -lt 3) {
  68. throw "AM1: status.ambient.paths has $($st.ambient.paths.Count) entries (want >= 3)"
  69. }
  70. $absent = @($st.ambient.paths | Where-Object { -not $_.present })
  71. if ($absent.Count -gt 0) {
  72. throw "AM1: recorded-but-absent ambient ACEs: $($absent.path -join ', ')"
  73. }
  74. Write-Host "AM1 ok: $($st.ambient.paths.Count) ambient denies recorded + present"
  75. # ── AM2: sandboxed write to %ProgramData% denied; read allowed ───
  76. $probe = Join-Path $env:ProgramData "srt-ambient-smoke-$([guid]::NewGuid().ToString('N')).txt"
  77. Assert-WriteDenied 'AM2: sandboxed write into %ProgramData% succeeded (must be denied)'
  78. & $Exe exec --quiet -- $cmd /c "dir `"$env:ProgramData`" > nul" 2>&1 | Out-Null
  79. if ($LASTEXITCODE -ne 0) { throw 'AM2: sandboxed READ of %ProgramData% failed (deny must be write-only)' }
  80. Write-Host 'AM2 ok: %ProgramData% write denied, read allowed'
  81. # ── AM3: session stamp + restore preserves the ambient floor ─────
  82. $json = @{ denyRead = @(); denyWrite = @($env:ProgramData) } | ConvertTo-Json
  83. Stdin @('acl','stamp','--holder-pid',$PID,'--sandbox-user-sid',$sbSid) $json
  84. Run @('acl','restore','--holder-pid',$PID,'--sandbox-user-sid',$sbSid,'--json')
  85. Assert-WriteDenied 'AM3: ambient deny lost after session stamp+restore round-trip'
  86. Write-Host 'AM3 ok: ambient floor survives session stamp+restore'
  87. # ── AM4: --keep-user uninstall keeps the floor ───────────────────
  88. # Ambient stamps key on the ACCOUNT, not the sublayer: tearing down
  89. # one sublayer with --keep-user (what test suites do around a
  90. # possibly-shared machine) must not strip the floor.
  91. $un = RunCapture @('uninstall','--sublayer-guid',$Sublayer,'--keep-user')
  92. Write-Host -NoNewline $un.raw
  93. if ($un.exit -ne 0) { throw "uninstall --keep-user exited $($un.exit): $($un.raw)" }
  94. if ($un.raw -match 'ambient write-deny removed') {
  95. throw 'AM4: --keep-user uninstall removed the ambient stamps (must keep them)'
  96. }
  97. $st2 = J @('status','--sublayer-guid',$Sublayer)
  98. if ($st2.ambient.paths.Count -lt 3) {
  99. throw "AM4: only $($st2.ambient.paths.Count) ambient rows survive --keep-user uninstall"
  100. }
  101. Assert-WriteDenied 'AM4: ambient deny lost after --keep-user uninstall'
  102. Write-Host 'AM4 ok: --keep-user uninstall keeps the ambient floor'
  103. # ── AM5: full uninstall removes the stamps with the account ──────
  104. $un2 = RunCapture @('uninstall','--sublayer-guid',$Sublayer)
  105. Write-Host -NoNewline $un2.raw
  106. if ($un2.exit -ne 0) { throw "uninstall exited $($un2.exit): $($un2.raw)" }
  107. if ($un2.raw -notmatch 'ambient write-deny removed') {
  108. throw 'AM5: full uninstall output missing the ambient-removal line'
  109. }
  110. $st3 = J @('status','--sublayer-guid',$Sublayer)
  111. if ($st3.ambient.paths.Count -ne 0) {
  112. throw "AM5: $($st3.ambient.paths.Count) ambient rows survive full uninstall"
  113. }
  114. # The account is gone, so assert ACE removal via icacls, not exec.
  115. $pdAcl = icacls $env:ProgramData | Out-String
  116. if ($pdAcl -match [regex]::Escape($sbSid) -or $pdAcl -match 'srt-sandbox') {
  117. throw 'AM5: sandbox-SID ACE still on %ProgramData% after full uninstall'
  118. }
  119. Write-Host 'AM5 ok: full uninstall removed the ambient denies'
  120. }
  121. finally {
  122. & $Exe uninstall --sublayer-guid $Sublayer 2>&1 | Out-Null
  123. }
  124. Write-Host 'smoke-ambient: PASS (AM1/AM2/AM3/AM4/AM5)'