Windows: ICorDebugProcess::Detach closes the debugger's handle in the debuggee twice, so a handle the debuggee opens in between is closed
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
- Bereich
- devtools, operating-systems
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
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:
- Attach.
LocalEventChannel::Initduplicates the debugger's process handle into the debuggee (SYNCHRONIZE), and keeps the value in mscordbi's copy of the control block (m_rightSideProcessHandle). - 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. - Detach, in mscordbi.
CordbProcess::Detach→Neuter→LocalEventChannel::Delete→RemoteHANDLE::CloseInRemoteProcess. This callsDuplicateHandle(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:
- It waits for a debugger, then finds which of its handle values refer to the debugger's process (with
CompareObjectHandles). - As soon as
Debugger.IsAttachedbecomes false, it creates 64 events. - 300 ms later it checks them with
GetHandleInformation. It never closes them.
- It waits for a debugger, then finds which of its handle values refer to the debugger's process (with
- The debugger:
- It attaches managed-only: dbgshim's
CreateDebuggingInterfaceFromVersionEx, thenICorDebug::DebugActiveProcess(pid, FALSE). - It waits until the debuggee has found the handle, then stops the process and calls
ICorDebugProcess::Detach. - 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.
- It attaches managed-only: dbgshim's
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::FinishInitializeIPCChannelWorkerfirst close debuggee's RC thread ntdll!NtClose←coreclr!Debugger::HandleIPCEvent←HandleIPCEventWrapper←DebuggerRCThread::HandleRSEAreuse, 0.08–0.26 ms later debuggee's main thread ntdll!NtCreateEvent← the debuggee'sMainsecond close, 32–93 ms after the first debugger ntdll!NtDuplicateObject←mscordbi!LocalEventChannel::Delete←CordbProcess::Neuter←CordbProcess::DetachThe 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-armrunner (26200, arm64)31 of 40 GitHub's windows-latestrunner (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 beforeCloseInRemoteProcess, 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
Startet den Dev-Container des Projekts im Browser, mit Ihrem eigenen GitHub-Konto.
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus dotnet/runtime
-
area-VM-meta-mono untriaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
Maintainer antworten meist innerhalb von 1 Tag
-
area-Debugger-mono untriaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
dotnet/runtime#138494 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
area-AssemblyLoader-mono untriaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
dotnet/runtime#138493 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log callEvtl. vergeben @Mohamedadel-prog hat das vor 2 Tagen übernommen. Offenarea-Extensions-Logging untriaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 82/100
dotnet/runtime#138418 · 2 Kommentare ·
Maintainer antworten meist innerhalb von 1 Tag
-
area-System.IO untriaged
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
dotnet/runtime#138417 · 1 Kommentar ·
Maintainer antworten meist innerhalb von 1 Tag
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
Maintainer antworten meist innerhalb von 1 Tag
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
juandresrodca/AdminForge#69 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Keep PublicAPI.Unshipped.txt sorted, with a guard, so concurrent API PRs stop conflicting on itOffenchore small tooling
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 79/100
BattletechModders/ModTek#256 · 1 Kommentar ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100
Facepunch/sbox-public#12484 ·
Maintainer antworten meist innerhalb von 2 Tagen