| 1 | #requires -Version 5.1 |
| 2 | $ErrorActionPreference = 'Stop' |
| 3 | $source = Join-Path $PSScriptRoot '../packaging/windows' |
| 4 | $root = Join-Path ([IO.Path]::GetTempPath()) ('delete-race-' + [guid]::NewGuid().ToString('N')) |
| 5 | New-Item -ItemType Directory -Path $root | Out-Null |
| 6 | try { |
| 7 | Add-Type -Path (Join-Path $source 'verified-file-deletion.cs') |
| 8 | $tokens = $null |
| 9 | $errors = $null |
| 10 | $ast = [Management.Automation.Language.Parser]::ParseFile((Join-Path $source 'installation-files.ps1'), [ref]$tokens, [ref]$errors) |
| 11 | if ($errors.Count) { throw 'The production installation helper has syntax errors.' } |
| 12 | $definitions = $ast.FindAll({ param($node) $node -is [Management.Automation.Language.FunctionDefinitionAst] }, $true) |
| 13 | foreach ($definition in $definitions) { |
| 14 | if ($definition.Name -in @('Open-ReadLocked', 'Stream-Hash', 'Initialize-VerifiedDeletion', 'Remove-MatchingFile')) { |
| 15 | . ([scriptblock]::Create($definition.Extent.Text)) |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | $script:originalHash = (Get-Item function:Stream-Hash).ScriptBlock |
| 20 | $script:replacePath = Join-Path $root 'program.dll' |
| 21 | $script:renamedPath = Join-Path $root 'original-renamed.dll' |
| 22 | [IO.File]::WriteAllText($script:replacePath, 'verified old program') |
| 23 | $expected = (Get-FileHash $script:replacePath).Hash |
| 24 | function Stream-Hash([IO.Stream]$Stream) { |
| 25 | $hash = & $script:originalHash $Stream |
| 26 | [IO.File]::Move($script:replacePath, $script:renamedPath) |
| 27 | [IO.File]::WriteAllText($script:replacePath, 'new personal data') |
| 28 | return $hash |
| 29 | } |
| 30 | Remove-MatchingFile $root 'program.dll' $expected |
| 31 | $replacementRetained = [IO.File]::ReadAllText($script:replacePath) -eq 'new personal data' |
| 32 | $verifiedObjectDeleted = !(Test-Path $script:renamedPath) |
| 33 | |
| 34 | $outside = Join-Path $root 'outside' |
| 35 | New-Item -ItemType Directory -Path $outside | Out-Null |
| 36 | [IO.File]::WriteAllText((Join-Path $outside 'owned.dll'), 'external target') |
| 37 | $link = Join-Path $root 'linked' |
| 38 | & cmd.exe /c mklink /J $link $outside | Out-Null |
| 39 | if ($LASTEXITCODE -ne 0) { throw 'Could not create the isolated junction fixture.' } |
| 40 | $reparseDenied = $false |
| 41 | try { |
| 42 | $stream = [CageVerifiedFile]::Open($root, 'linked/owned.dll') |
| 43 | $stream.Dispose() |
| 44 | } catch { $reparseDenied = $true } |
| 45 | $externalRetained = [IO.File]::ReadAllText((Join-Path $outside 'owned.dll')) -eq 'external target' |
| 46 | [pscustomobject]@{ |
| 47 | ReplacementRetained = $replacementRetained |
| 48 | VerifiedObjectDeleted = $verifiedObjectDeleted |
| 49 | ReparseTraversalDenied = $reparseDenied |
| 50 | ExternalTargetRetained = $externalRetained |
| 51 | PowerShell = $PSVersionTable.PSVersion.ToString() |
| 52 | } | ConvertTo-Json -Compress |
| 53 | if (!$replacementRetained -or !$verifiedObjectDeleted -or !$reparseDenied -or !$externalRetained) { |
| 54 | throw 'Verified-handle deletion postconditions failed.' |
| 55 | } |
| 56 | } finally { |
| 57 | if (Test-Path -LiteralPath (Join-Path $root 'linked')) { [IO.Directory]::Delete((Join-Path $root 'linked')) } |
| 58 | Remove-Item -LiteralPath $root -Recurse -Force |
| 59 | } |