fix(win/nvfbcwrp): restore backup owner only after install succeeds

- Defer the backup owner/DACL restore out of Move-Genuine into a new Invoke-FinalizeOwner that runs only after every target was applied, so a later failure still finds the Administrators grant needed to move a backup back.
- Set AddedAdminGrant only after Unlock-DLL succeeds, so a failed takeown or grant is never treated as a grant that must be removed.
- Report a finalize failure as a red warning while keeping exit code 0, since rolling back a completed install would leave the system worse.
- README.md manual restore now includes the takeown/icacls commands needed to regain access to the Administrators-owned backups.
- Target: win/nvfbcwrp
This commit is contained in:
ram/haidar
2026-11-14 20:17:20 +07:00
parent 310ffd9895
commit 2a61b61073
2 changed files with 46 additions and 22 deletions

View File

@@ -346,15 +346,19 @@ Function Test-WrapperPe {
}
# Moves the genuine DLL to its backup name. Never overwrites an existing backup.
# Known limit (F-7): an interrupt (Ctrl+C) after taking ownership in Unlock-DLL and before
# Restore-Owner has finished (the window is between the unlock and the end of the restore) can
# leave NvFBC64_.dll / NvFBC_.dll owned by Administrators, possibly with an Administrators
# FullControl entry. The DLL contents are intact. A re-run does not restore owner/ACL, because the
# move then succeeds without UnauthorizedAccess. Fix by hand if desired (see README.md).
# Order: move, verify the move, restore owner/ACL on the moved backup, then verify the hash, so the
# unrestored window is as short as possible. Rollback records are kept correct at each step.
# Owner/ACL: when Move-Item is refused (UnauthorizedAccess), Unlock-DLL takes ownership of the
# ORIGINAL file and grants Administrators FullControl, and the move is retried. The owner restore of
# the BACKUP is deliberately NOT done here. It is recorded in $Deferred and runs in
# Invoke-FinalizeOwner only after every target was applied. Until then the backup keeps the
# Administrators grant, so a rollback can always move it back. A rollback restores owner/DACL on the
# ORIGINAL path, after the move back (see Undo-Changes, 'Acl').
# Known limit (F-7): an interrupt (Ctrl+C) after the unlock and before Invoke-FinalizeOwner has
# finished can leave NvFBC64_.dll / NvFBC_.dll owned by Administrators, possibly with an
# Administrators FullControl entry. The DLL contents are intact. A re-run does not restore owner/ACL,
# because the move then succeeds without UnauthorizedAccess. Fix by hand if desired (see README.md).
# Order: move, verify the move, verify the hash, then record the finalize step.
Function Move-Genuine {
param($Target, $Undo)
param($Target, $Undo, $Deferred)
$from = $Target.Original
$to = $Target.Backup
$aclEntry = $null
@@ -363,19 +367,21 @@ Function Move-Genuine {
}
catch [System.UnauthorizedAccessException] {
Write-Host "[$($Target.Name)] Access denied; taking ownership of $from ..."
# Everything needed to revert is recorded BEFORE the unlock. Rollback runs Restore-Owner on
# the original path after the file has been moved back, so a half-done unlock is undone too.
# Everything needed to revert is recorded BEFORE the unlock. AddedAdminGrant starts as $false and
# is set only after Unlock-DLL has succeeded, so a failed takeown or grant is never "undone" as a grant.
$ownerSid = Get-OwnerSid -Path $from
$hadAdminAce = Test-ExplicitAdminAce -Path $from
$aclEntry = [pscustomobject]@{
Kind = 'Acl'
Path = $from
Path = $from # original path: rollback runs Restore-Owner here, after the move back
Backup = $to # finalize runs Restore-Owner here, after all targets were applied
OwnerSid = $ownerSid
AddedAdminGrant = (-not $hadAdminAce) # never remove an Administrators ACE that was already there
AddedAdminGrant = $false
Done = $false
}
$Undo.Add($aclEntry)
Unlock-DLL -Path $from
$aclEntry.AddedAdminGrant = (-not $hadAdminAce) # never remove an Administrators ACE that was already there
Move-Item -LiteralPath $from -Destination $to
}
# Recorded right after the move, so rollback moves the backup back even if a later step fails.
@@ -384,17 +390,10 @@ Function Move-Genuine {
if ((Test-Path -LiteralPath $from) -or -not (Test-Path -LiteralPath $to -PathType Leaf)) {
throw "Move verification failed: $from -> $to"
}
if ($aclEntry) {
# Owner/ACL go back on the moved BACKUP, not on the original path. Runs before the hash check
# so the window with the Administrators ownership is as short as possible.
$r = Restore-Owner -Path $to -OwnerSid $aclEntry.OwnerSid -RemoveAdminGrant $aclEntry.AddedAdminGrant
if ($r -eq 'failed') { throw "Could not remove the Administrators grant from the backup $to" }
if ($r -eq 'full') { $aclEntry.Done = $true }
else { $aclEntry.AddedAdminGrant = $false } # partial: the grant is already gone; rollback retries only the owner
}
if ((Get-Sha256 -Path $to) -ne $Target.OriginalHash) {
throw "Hash mismatch after moving the genuine DLL to $to"
}
if ($aclEntry) { $Deferred.Add($aclEntry) } # restored by Invoke-FinalizeOwner, success path only
Write-Host "[$($Target.Name)] OK: moved $from -> $to (SHA-256 verified)"
}
@@ -480,6 +479,28 @@ Function Show-AclNotes {
foreach ($n in $script:AclNotes) { Write-Host " $n" -ForegroundColor Yellow }
}
# Final phase. Runs ONLY after every target was applied successfully, so no rollback can follow it.
# Restores the owner and DACL of each backup that Move-Genuine took ownership of, using the saved
# owner SID and the grant flag recorded there. Statuses from Restore-Owner:
# 'full' / 'partial' normal. 'partial' (owner not set back, typically TrustedInstaller) is a NOTE.
# 'failed' the Administrators grant could not be removed. A red WARNING is shown and
# the run still exits 0: rolling back a completed install would be worse. The
# backup keeps an extra Administrators FullControl entry until fixed by hand.
# Never throws, never rolls back.
Function Invoke-FinalizeOwner {
param($Deferred)
foreach ($entry in $Deferred) {
$r = Restore-Owner -Path $entry.Backup -OwnerSid $entry.OwnerSid -RemoveAdminGrant $entry.AddedAdminGrant
if ($r -eq 'full') { $entry.Done = $true }
elseif ($r -eq 'partial') { $entry.AddedAdminGrant = $false } # the grant is already gone
elseif ($r -eq 'failed') {
$msg = "WARNING: $($entry.Backup) still has an Administrators entry. The install is complete and the exit code stays 0. Remove the Administrators entry from that file by hand."
Write-Host $msg -ForegroundColor Red
$script:AclNotes.Add($msg)
}
}
}
Function Invoke-Patch {
$scriptDir = $PSScriptRoot
Write-Host 'Starting NVFBC DLL patch...'
@@ -507,6 +528,7 @@ Function Invoke-Patch {
# Apply. Any failure rolls back everything done in this run, for both architectures.
$undo = New-Object -TypeName System.Collections.Generic.List[object]
$deferred = New-Object -TypeName System.Collections.Generic.List[object] # owner/DACL restores for the finalize step
try {
foreach ($t in $targets) {
switch ($t.Action) {
@@ -514,7 +536,7 @@ Function Invoke-Patch {
Write-Host "[$($t.Name)] OK: already patched ($($t.Original) is the wrapper, $($t.Backup) kept). Skipped."
}
'Install' {
Move-Genuine -Target $t -Undo $undo
Move-Genuine -Target $t -Undo $undo -Deferred $deferred
Install-Wrapper -Target $t -Undo $undo
}
'ResumeInstall' {
@@ -538,6 +560,8 @@ Function Invoke-Patch {
return 1
}
# Every target is applied. Only now are the backups' owner/DACL restored (success path only).
Invoke-FinalizeOwner -Deferred $deferred
Show-AclNotes
Write-Host 'NVFBC patch complete.'
return 0

View File

@@ -14,7 +14,7 @@ Wrapper for `NvFBC64.dll` library which injects private keys into `NvFBC_CreateE
This procedure has to be repeated after any driver reinstall/update, so keep your copies of `nvfbcwrp64.dll` and `nvfbcwrp32.dll` files.
You can use the [PowerShell script](Patch.ps1) and [Patch Launcher](RunPatch.bat) to quickly apply patch. Put `nvfbcwrp64.dll` and `nvfbcwrp32.dll` in the same folder as `Patch.ps1` first. The script asks for administrator rights itself, renames the genuine DLLs to `NvFBC64_.dll` and `NvFBC_.dll` (steps 2-4), and installs the wrappers (steps 5-6). It is safe to run again. It refuses, and changes nothing, when the file at the original path exists and is not the wrapper while a `NvFBC64_.dll` or `NvFBC_.dll` backup exists. If the original is missing and a genuine backup exists, it completes the installation. If the wrappers are already in place, it changes nothing, except that it removes a leftover `NvFBC64.dll.nvfbcwrp-tmp` or `NvFBC.dll.nvfbcwrp-tmp` file from an interrupted run. The refusal message says which case applies: (a) after a driver update the original may be a new genuine NVIDIA DLL; first verify it (File Properties > Details: Company `NVIDIA Corporation`, Product `NVIDIA Frame Buffer Capture Library`). Only if the driver was just installed or updated and NvFBC works, or the size and SHA-256 of the file match the driver package, move the old backup to another folder (do not delete it) and run again. (b) if the original is an old or partial copy of the wrapper, delete only that original file and keep the backup. If the backup itself is the wrapper, the message says that the wrapper copy is the file to delete (and that the original must be kept if it is genuine). Before any change, the script also checks that the file it would treat as genuine has Company `NVIDIA Corporation`; if it does not (for example an old wrapper build or a damaged file), it refuses, changes nothing, and asks you to reinstall the NVIDIA driver. If you are sure the file is genuine, run it again with `-SkipGenuineCheck`. If a file cannot be read at all (for example another program holds it open), the script refuses with a read error and asks you to close programs that use NVIDIA components and run again; it does not suggest reinstalling the driver. If anything fails, the script rolls back its changes. If the script was interrupted (for example with Ctrl+C) before the genuine DLL was moved, run it again. If it was interrupted after the move, run it again: it resumes and finishes the installation. If it was interrupted after the script took ownership of the genuine DLL and before the owner/ACL restore had finished, `NvFBC64_.dll` or `NvFBC_.dll` may remain owned by Administrators, possibly with an Administrators FullControl entry. The DLL itself is intact, and a re-run does not restore the owner or ACL. Fix it by hand if you want the original owner and ACL back. To restore manually, close all applications that use NvFBC, open an elevated (Administrator) prompt, delete `%WINDIR%\system32\NvFBC64.dll` and `%WINDIR%\SysWOW64\NvFBC.dll`, then rename `NvFBC64_.dll` back to `NvFBC64.dll` and `NvFBC_.dll` back to `NvFBC.dll`.
You can use the [PowerShell script](Patch.ps1) and [Patch Launcher](RunPatch.bat) to quickly apply patch. Put `nvfbcwrp64.dll` and `nvfbcwrp32.dll` in the same folder as `Patch.ps1` first. The script asks for administrator rights itself, renames the genuine DLLs to `NvFBC64_.dll` and `NvFBC_.dll` (steps 2-4), and installs the wrappers (steps 5-6). It is safe to run again. It refuses, and changes nothing, when the file at the original path exists and is not the wrapper while a `NvFBC64_.dll` or `NvFBC_.dll` backup exists. If the original is missing and a genuine backup exists, it completes the installation. If the wrappers are already in place, it changes nothing, except that it removes a leftover `NvFBC64.dll.nvfbcwrp-tmp` or `NvFBC.dll.nvfbcwrp-tmp` file from an interrupted run. The refusal message says which case applies: (a) after a driver update the original may be a new genuine NVIDIA DLL; first verify it (File Properties > Details: Company `NVIDIA Corporation`, Product `NVIDIA Frame Buffer Capture Library`). Only if the driver was just installed or updated and NvFBC works, or the size and SHA-256 of the file match the driver package, move the old backup to another folder (do not delete it) and run again. (b) if the original is an old or partial copy of the wrapper, delete only that original file and keep the backup. If the backup itself is the wrapper, the message says that the wrapper copy is the file to delete (and that the original must be kept if it is genuine). Before any change, the script also checks that the file it would treat as genuine has Company `NVIDIA Corporation`; if it does not (for example an old wrapper build or a damaged file), it refuses, changes nothing, and asks you to reinstall the NVIDIA driver. If you are sure the file is genuine, run it again with `-SkipGenuineCheck`. If a file cannot be read at all (for example another program holds it open), the script refuses with a read error and asks you to close programs that use NVIDIA components and run again; it does not suggest reinstalling the driver. If anything fails, the script rolls back its changes. If the script was interrupted (for example with Ctrl+C) before the genuine DLL was moved, run it again. If it was interrupted after the move, run it again: it resumes and finishes the installation. If it was interrupted after the script took ownership of the genuine DLL and before the owner/ACL restore had finished, `NvFBC64_.dll` or `NvFBC_.dll` may remain owned by Administrators, possibly with an Administrators FullControl entry. The DLL itself is intact, and a re-run does not restore the owner or ACL. Fix it by hand if you want the original owner and ACL back. To restore manually, close all applications that use NvFBC and open an elevated (Administrator) prompt. After a successful run, `NvFBC64_.dll` and `NvFBC_.dll` are owned by Administrators and have no Administrators entry, so an elevated prompt cannot rename or delete them until you take ownership and grant Administrators Full Control: `takeown /F "%WINDIR%\system32\NvFBC64_.dll" /A`, `icacls "%WINDIR%\system32\NvFBC64_.dll" /grant *S-1-5-32-544:F`, `takeown /F "%WINDIR%\SysWOW64\NvFBC_.dll" /A`, `icacls "%WINDIR%\SysWOW64\NvFBC_.dll" /grant *S-1-5-32-544:F`. Then delete only the wrappers `%WINDIR%\system32\NvFBC64.dll` and `%WINDIR%\SysWOW64\NvFBC.dll`, and rename `NvFBC64_.dll` back to `NvFBC64.dll` and `NvFBC_.dll` back to `NvFBC.dll`.
## Advanced Usage