Skip to content

Fix MoveDistribution VHD ownership restore to reuse the resolved file handle#41131

Merged
benhillis merged 1 commit into
masterfrom
user/benhill/movedistribution-vhd-owner-handle
Jul 22, 2026
Merged

Fix MoveDistribution VHD ownership restore to reuse the resolved file handle#41131
benhillis merged 1 commit into
masterfrom
user/benhill/movedistribution-vhd-owner-handle

Conversation

@benhillis

Copy link
Copy Markdown
Member

When moving a distro's VHD via wsl --manage --move , MoveDistribution restores the VHD's original owner after the move by re-opening the destination path by name as SYSTEM with WRITE_OWNER.

This changes setVhdOwner to instead open the destination impersonated as the caller with READ_CONTROL, then use ReOpenFile to upgrade that same handle to WRITE_OWNER while running as SYSTEM with SE_RESTORE_NAME, rather than re-resolving the path by name. ReOpenFile reuses the existing file object instead of walking the path again.

Also moves the rollback scope_exit registration earlier so a failure in setVhdOwner while moving to the new path correctly triggers rollback of the move and registry changes.

No behavior change for existing tests (MoveVhdOwnership, MoveVhdWithAdminOwner both pass unchanged)."

… handle

setVhdOwner now opens the destination impersonated with READ_CONTROL, then uses ReOpenFile to upgrade the same handle to WRITE_OWNER while running as SYSTEM, instead of re-opening the path by name as SYSTEM. This avoids a second path resolution under elevated privileges. Also moves the rollback scope_exit registration earlier so a failure in setVhdOwner while moving to the new path correctly triggers rollback.
@benhillis
benhillis requested a review from a team as a code owner July 21, 2026 21:33
Copilot AI review requested due to automatic review settings July 21, 2026 21:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines LxssUserSessionImpl::MoveDistribution() to restore a moved distro VHD’s original owner using a handle-preserving flow: open the destination VHD while impersonating the caller, then upgrade that same file object to WRITE_OWNER under SYSTEM via ReOpenFile, avoiding a second path walk as SYSTEM. It also adjusts rollback ordering so failures during ownership restoration correctly revert the move before registry updates can strand the distro registration.

Changes:

  • Update VHD ownership restoration to open as the caller (READ_CONTROL) and then ReOpenFile to WRITE_OWNER under SYSTEM with SE_RESTORE_NAME, reusing the resolved file object.
  • Register the rollback scope_exit before attempting ownership restoration, ensuring rollback occurs if setVhdOwner() fails.
  • Expand share flags used during ownership restoration to include FILE_SHARE_DELETE for the involved handles.

@benhillis
benhillis merged commit d92c398 into master Jul 22, 2026
12 checks passed
@benhillis
benhillis deleted the user/benhill/movedistribution-vhd-owner-handle branch July 22, 2026 21:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants