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

未关闭
#138,451 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
44/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
cpp, csharp

调研方向

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 内容生成。

描述

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.

主要语言
C#
星标
18.5k
派生
5.8k
平均合并
2 天 8 小时
30 天内合并 PR
597

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/runtime 的其他 Issue

查看 dotnet/runtime 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。