Windows: ICorDebugProcess::Detach closes the debugger's handle in the debuggee twice, so a handle the debuggee opens in between is closed

Offen
#138,451 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
44/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
cpp, csharp

Rechercherichtung

Start with src/coreclr/debug/di/localeventchannel.cpp, especially LocalEventChannel::Init and Delete, then trace the detach paths in src/coreclr/debug/ee/debugger.cpp, src/coreclr/debug/ee/rcthread.cpp, and src/coreclr/debug/di/process.cpp. Run the supplied debuggee/debugger reproduction on Windows and confirm that the handle value is closed once in the debuggee and again by mscordbi after reuse. Done means a detach cannot close a handle subsequently created by the debuggee, with the Windows reproduction and relevant debugger tests passing.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

area-Diagnostics-coreclr untriaged
Description

On Windows, a managed-only detach closes one handle value in the debuggee twice. If the debuggee opens a handle between the two closes and gets that value, the second close closes the debuggee's own new handle.

The value is that of the debugger's process handle, which mscordbi duplicates into the debuggee at attach. Here is the sequence, with links to main at 198cfaf:

  1. Attach. LocalEventChannel::Init duplicates the debugger's process handle into the debuggee (SYNCHRONIZE), and keeps the value in mscordbi's copy of the control block (m_rightSideProcessHandle).
  2. Detach, in the debuggee. DB_IPCE_DETACH_FROM_PROCESS → DebuggerRCThread::RightSideDetach → CloseIPCHandles → RemoteHANDLE::Close. This closes the handle and sets the field to NULL. The debuggee then replies and resumes its threads.
  3. Detach, in mscordbi. CordbProcess::Detach → Neuter → LocalEventChannel::Delete → RemoteHANDLE::CloseInRemoteProcess. This calls DuplicateHandle(debuggee, value, ..., DUPLICATE_CLOSE_SOURCE) with the value from mscordbi's copy.

As far as we can read, nothing refreshes mscordbi's copy between steps 2 and 3. UpdateRightSideDCB runs only on the timeout and failure paths during a detach, and the detach reply is read from the receive buffer, not the control block. So the second close uses a value the debuggee already closed. By then the debuggee is running again, and Windows reuses freed handle values quickly.

Reproduction Steps

We hit this while hardening our debugger's detach on Windows. Our reproduction is a pair of file-based apps in our repository (MIT):

  • The debuggee:
    1. It waits for a debugger, then finds which of its handle values refer to the debugger's process (with CompareObjectHandles).
    2. As soon as Debugger.IsAttached becomes false, it creates 64 events.
    3. 300 ms later it checks them with GetHandleInformation. It never closes them.
  • The debugger:
    1. It attaches managed-only: dbgshim's CreateDebuggingInterfaceFromVersionEx, then ICorDebug::DebugActiveProcess(pid, FALSE).
    2. It waits until the debuggee has found the handle, then stops the process and calls ICorDebugProcess::Detach.
    3. It goes through our own ICorDebug wrapper. Between the attach and the detach it only continues the attach's callbacks, and it sets no breakpoints.

To run it: clone the repository, then run dotnet run --no-cache 119-detach-closes-a-handle-smoke.cs --sessions 40 in poc/drhook-engine on Windows (.NET 10 SDK).

We have not tried other debuggers. Nothing in the code is specific to our client, so we would expect any mscordbi-based debugger that detaches to show the same thing.

Expected behavior

A detach closes only the handle mscordbi put into the debuggee, and closes it once. The debuggee's own handles are untouched.

Actual behavior
  • The debuggee's new handles, 40 detaches.

    Outcome Detaches
    Exactly one of the 64 new events invalid, always the one with the value of the debugger's handle 30
    A new event invalid with any other value 0
    No new event invalid 10
  • A handle trace, 20 detaches (Windows Performance Recorder's built-in Handle profile; the probe that reads it). In all 15 that lost a handle, the events on that value in the debuggee's handle table were the same:

    Step Thread Stack (kernel frames left out)
    attach debugger ntdll!NtDuplicateObject ← mscordbi!LocalEventChannel::Init ← CordbProcess::FinishInitializeIPCChannelWorker
    first close debuggee's RC thread ntdll!NtClose ← coreclr!Debugger::HandleIPCEvent ← HandleIPCEventWrapper ← DebuggerRCThread::HandleRSEA
    reuse, 0.08–0.26 ms later debuggee's main thread ntdll!NtCreateEvent ← the debuggee's Main
    second close, 32–93 ms after the first debugger ntdll!NtDuplicateObject ← mscordbi!LocalEventChannel::Delete ← CordbProcess::Neuter ← CordbProcess::Detach

    The second close's object is the debuggee's new event. In the 5 detaches that lost nothing, the value was not reused before mscordbi's close.

  • What it costs a debuggee is whatever that handle was for. In 1,200 detaches from a debuggee that creates threads continuously, we saw the following. They are consistent with this close, but we did not trace each one:

    • a thread whose wait never ended, leaving the process alive after the detach (4);
    • a write to a just-opened file failing with "The handle is invalid" (9);
    • the runtime stopping with "Internal CLR error" (0x80131506) (4).
Regression?

We don't know. We have run only .NET 10.0.15. The code involved is the same in main.

Known Workarounds

We know of none from the debugger's side. Both closes happen inside ICorDebugProcess::Detach and the debuggee's runtime. A debuggee that opens no handle in the moment after the detach is not affected.

Configuration
  • .NET 10.0.15, for both the debuggee and the debugger (mscordbi from the same runtime).

  • The reproduction's 40 detaches, on three machines:

    Machine Detaches that lost the handle
    Windows 11 (26200) arm64, a VM with 4 cores 30 of 40
    GitHub's windows-11-arm runner (26200, arm64) 31 of 40
    GitHub's windows-latest runner (26100, x64) 13 of 40

    On x64 the debuggee's new events took the freed value less often. No other handle was lost on any of them.

  • Linux and macOS talk to the debugger over the transport (FEATURE_DBGIPC_TRANSPORT_VM), where neither close is compiled.

Other information
  • A possible fix, which we have not tried. In LocalEventChannel::Delete, re-read the debuggee's field before CloseInRemoteProcess, and skip the close when it is NULL: the debuggee sets it to NULL when it closes the handle. mscordbi's close would still be needed when the debuggee never took the handle over, as after a failed attach (CleanupHalfBakedLeftSide).
  • Not a security issue, as far as we can see. The debugger already has full access to the debuggee.
  • More detail. Our full write-up, with the records, is here.

Thanks for taking a look. We're happy to run a fix against the reproduction.

Vorherrschende Sprache
C#
Sterne
18.5k
Forks
5.8k
Ø Merge
2 T. 8 Std.
Gemergte PRs (30 T.)
597

Entwicklungsumgebung

In Codespaces öffnen

Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus dotnet/runtime

Alle Issues in dotnet/runtime

Ähnliche Issues

Weitere Issues zu C#

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.