Skip to content

mingw: guard process tree traversal against PID reuse - #6474

Closed
huong8373tt-beep wants to merge 1 commit into
git-for-windows:mainfrom
huong8373tt-beep:slai/guard-windows-process-tree-pid-reuse
Closed

huong8373tt-beep wants to merge 1 commit into
git-for-windows:mainfrom
huong8373tt-beep:slai/guard-windows-process-tree-pid-reuse

Conversation

@huong8373tt-beep

Copy link
Copy Markdown

Summary

Fixes #6435.

terminate_process_tree() previously expanded the cleanup tree using only the parent PID recorded in a Toolhelp snapshot. A PID can be reused after its original process exits, so an unrelated long-running orphan could retain a dead parent PID that is later assigned to a Git child. The fallback cleanup path could then terminate that unrelated process.

Validate every candidate before adding it to the cleanup tree:

  • re-check its live parent PID from its process handle using NtQueryInformationProcess(ProcessBasicInformation);
  • require the candidate creation time to be no earlier than its accepted parent;
  • retain a handle for each accepted node until termination completes, so accepted PIDs cannot be reused while the tree is being walked.

If a candidate cannot be opened with the query and terminate rights needed for this validation, it is not added to the descendant tree. The explicitly targeted root process still follows the existing termination fallback.

Validation

  • make -j2 DEVELOPER=1 NO_RUST=1 git.exe t/unit-tests/bin/unit-tests.exe on Windows/MINGW64
  • t/unit-tests/bin/unit-tests.exe -r mingw
    • 4 passed, 1 expected UCRT skip
  • git diff --check

The new Windows unit coverage verifies creation-time ordering and live parent identity for a real child process, including rejection when the claimed parent timestamp is later than the child.

Artificial intelligence assistance disclosure

Slai Agent Runtime assisted with reproduction, implementation, and test execution; the account owner is responsible for this submission.

dscho added a commit that referenced this pull request Nov 25, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
@dscho

dscho commented Nov 25, 2026

Copy link
Copy Markdown
Member

Closing, as it is superseded by #6493.

@dscho dscho closed this Nov 25, 2026
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 26, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 27, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 29, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
gitforwindowshelper Bot pushed a commit that referenced this pull request Nov 30, 2026
Windows can reuse the PID of an orphan's dead parent for a process Git
is cleaning up. Our process-tree traversal currently matches only the
parent PID, so it can mistake that unrelated orphan and its descendants
for processes Git should terminate.

Let's exclude reused PIDs by ensuring that the process creation time
matches the one obtained together with the PID when the process tree was
enumerated, by switching to `NtQuerySystemInformation()` to obtain
creation times alongside parent PIDs. Candidates must not predate their
claimed parents, and their recorded creation times must precede the
snapshot-start cutoff. When opening a descendant for termination, its
creation time must match the captured value, so a reused PID does not
select a different process.

This should also fix a frequent flake in VFS for Git's Functional Tests.

This supersedes the process-tree cleanup proposal in the following PR:
#6474. That approach opens
candidates during discovery and retains a handle for every accepted
descendant. Here, discovery opens no process handles, and each
descendant is opened only once, at termination. Handle usage stays
bounded by the root plus one temporary handle rather than growing with
the tree. The independent growable-storage change is a separate commit.

This fixes #6435.
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.

terminate_process_tree() kills unrelated processes when a PID has been reused (no creation-time check)

2 participants