Windows: ICorDebugProcess::Detach closes the debugger's handle in the debuggee twice, so a handle the debuggee opens in between is closed
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 44/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
調査の方向性
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.
索引モデルが issue の本文から書いたものです。
説明
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.
- 主要言語
- C#
- スター
- 18.5k
- フォーク
- 5.8k
- 平均マージ
- 2日 8時間
- マージ済み PR(30日)
- 597
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dotnet/runtime のほかの issue
-
area-VM-meta-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
area-Debugger-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
dotnet/runtime#138494 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area-AssemblyLoader-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
dotnet/runtime#138493 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log call対応中かも @Mohamedadel-prog が 2 日前に担当しました。 オープンarea-Extensions-Logging untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
dotnet/runtime#138418 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
area-System.IO untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
dotnet/runtime#138417 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
juandresrodca/AdminForge#69 ·
メンテナーはふだん 1 日以内に返信
-
Keep PublicAPI.Unshipped.txt sorted, with a guard, so concurrent API PRs stop conflicting on itオープンchore small tooling
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
BattletechModders/ModTek#256 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
Facepunch/sbox-public#12484 ·
メンテナーはふだん 2 日以内に返信