Windows: ICorDebugProcess::Detach closes the debugger's handle in the debuggee twice, so a handle the debuggee opens in between is closed
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
- 难度
- 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 小时
- 30 天内合并 PR
- 597
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 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 天内回复
-
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 天内回复