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

Đang mở Phù hợp với người mới
#138,497 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
75/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
c
Lĩnh vực
backend, operating-systems

Hướng nghiên cứu

Đọc src/mono/mono/utils/mono-dl.c, đặc biệt là mono_dl_fallback_unregister() và vòng lặp fallback trong mono_dl_open_full(). Tái hiện lỗi use-after-free bằng harness đính kèm và cấu hình AddressSanitizer, sau đó xác minh rằng việc hủy đăng ký phần tử đầu danh sách khiến danh sách trống và một lần gọi mono_dl_open() thất bại sẽ trả về NULL mà không báo lỗi.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C#
Star
18.5k
Fork
5.8k
Merge trung bình
2 ngày 8 giờ
Pull request đã merge (30 ngày)
597

Chuẩn bị môi trường

Mở trong Codespaces

Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của dotnet/runtime

Tất cả issue của dotnet/runtime

Issue tương tự

Thêm issue về C#

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.