[mono] mono_dl_fallback_unregister frees the list head but keeps the dangling head pointer

未关闭 适合新手
#138,497 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c

调研方向

阅读 src/mono/mono/utils/mono-dl.c,特别是 mono_dl_fallback_unregister() 和 mono_dl_open_full() 中的回退循环。使用附带的 harness 和 AddressSanitizer 配置复现 use-after-free,然后验证注销链表头节点后链表为空,并且失败的 mono_dl_open() 会返回 NULL,且不报告错误。

由索引模型根据 Issue 内容生成。

描述

area-VM-meta-mono untriaged
Description

mono_dl_fallback_unregister() (src/mono/mono/utils/mono-dl.c:435-445) removes a handler from the process-global fallback_handlers list but discards the new head returned by g_slist_remove(). When the handler being unregistered is the first entry of the list, the freed node stays in fallback_handlers, and the next mono_dl_open*() whose dlopen() fails walks the fallback loop through that freed node.

Reproduction Steps

Attached: harness.c (a small C program that only calls the public embedding API), plus the files needed to build it into an ASan-instrumented runtime.

MonoDlFallbackHandler *h = mono_dl_fallback_register (poc_load, poc_symbol, poc_close, user_data);
mono_dl_fallback_unregister (h);   /* h was the head: the head node is freed, the global head is dangling */
mono_dl_open ("libdlfallback_missing.so", MONO_DL_LAZY, &error);   /* dlopen fails -> fallback loop */
Build (Mono runtime with AddressSanitizer, Linux x64, tag v10.0.15)

The Mono CMake build has no sanitizer switch, so the flags are injected with a CMake hook. All files are attached; <abs> stands for absolute paths on your machine.

  1. Build the runtime (asan-flags.cmake, asan-default-options.c):

    ./build.sh -restore -build -c release /p:RuntimeFlavor=Mono /p:KeepNativeSymbols=true \
      "/p:MonoObjDir=<abs>/asan-obj/" "/p:RuntimeBinDir=<abs>/asan-bin/" \
      "/p:CMakeArgs=-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=<abs>/asan-flags.cmake" \
      -projects <repo>/src/mono/mono.proj
    

    The instrumented runtime is <abs>/asan-obj/out/bin/mono-sgen. Run every ASan binary of this build with
    ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp
    (detect_stack_use_after_return=0 is baked into mono-sgen by asan-default-options.c, but a separately linked harness needs it from the environment: Mono's GC-suspend code is not compatible with ASan's fake stack).

  2. Add the harness as an extra executable linked into that runtime (extra-targets.cmake, harness.c):

    cmake -S <repo>/src/mono -B <abs>/asan-obj \
      "-DCMAKE_PROJECT_TOP_LEVEL_INCLUDES=<abs>/asan-flags.cmake;<abs>/extra-targets.cmake" \
      -DMONO_VERIFY_HARNESS_SRC=<abs>/harness.c -DMONO_VERIFY_HARNESS_NAME=dlfallback-harness
    cmake --build <abs>/asan-obj --target dlfallback-harness
    
  3. Run it:

    ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp <abs>/asan-obj/dlfallback-harness head
    

    The head scene needs no runtime initialisation.

attachment.zip

Expected behavior

mono_dl_open() on a library that does not exist returns NULL; the fallback handler list is empty after the only handler was unregistered.

Actual behavior

ASan reports a heap-use-after-free read of the freed list node in the fallback loop (asan.txt):

ERROR: AddressSanitizer: heap-use-after-free
READ of size 8
    #0 mono_dl_open_full          src/mono/mono/utils/mono-dl.c:156:69
freed by thread T0 here:
    #1 monoeg_g_slist_free_1     src/mono/mono/eglib/gslist.c:42:2
    #2 monoeg_g_slist_remove     src/mono/mono/eglib/gslist.c:225:3
    #3 mono_dl_fallback_unregister src/mono/mono/utils/mono-dl.c:443:2
previously allocated by thread T0 here:
    #1 monoeg_malloc             src/mono/mono/eglib/gmem.c:120:8
    #3 mono_dl_fallback_register src/mono/mono/utils/mono-dl.c:428:22

Without ASan the same program crashes deterministically with SIGSEGV in mono_dl_open_full (the call never returns): the freed 16-byte node has been reused by libc's dlopen() error handling, so the garbage handler pointer is dereferenced at mono-dl.c:158. With the harness's module-symbol scene the runtime makes an indirect call through a function-pointer slot read from the freed handler (mono_dl_symbol(), mono-dl.c:206).

Regression?

No response

Known Workarounds

No response

Configuration
  • .NET 10.0.15 (tag v10.0.15, commit a3788b71495457354b64ca8040b6ded5ff6ca4f9), Mono runtime built from source with the attached hook, linux-x64, clang 18, Release flags plus -fsanitize=address.
  • main (commit 5f8157f1522a) has the same code at the same lines; the same report was also reproduced on main@bdd9b750b70.
  • v11.0.0 (tag v11.0.0): same code (sources compared; not run).
  • Not specific to this configuration as far as I can tell; other allocators change what the freed node contains, not the dangling head.
Other information
Root cause

g_slist_remove() returns the new head of the list, because removing the first element replaces the head. mono_dl_fallback_unregister() ignores that return value, so fallback_handlers keeps pointing at the node that g_slist_remove() just freed:

void
mono_dl_fallback_unregister (MonoDlFallbackHandler *handler)
{
        GSList *found;

        found = g_slist_find (fallback_handlers, handler);
        if (found == NULL)
                return;

        g_slist_remove (fallback_handlers, handler);
        g_free (handler);
}

src/mono/mono/utils/mono-dl.c:434-445 — identical on main and release/10.0. The consumer is the fallback loop of mono_dl_open_full() (mono-dl.c:153-158), and with a surviving handler also mono_dl_symbol() (mono-dl.c:206) and mono_dl_close() (mono-dl.c:239).

In the tree, the only caller of mono_dl_fallback_register() is the WebAssembly browser runtime (src/mono/browser/runtime/runtime.c:299); nothing in the tree calls mono_dl_fallback_unregister(). Both are exported embedding API (MONO_API_FUNCTION in src/native/public/mono/utils/details/mono-dl-fallback-functions.h:9 and :11), so any native host can reach this.

主要语言
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 摘要。