[mono] mono_assembly_foreach() iterates the live list instead of the copy it makes
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
- 78/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
Bắt đầu tại src/mono/mono/metadata/assembly.c, ở mono_assembly_foreach() (các dòng 3034–3049), và đọc cách hàm sao chép rồi duyệt loaded_assemblies. Cách tái hiện sử dụng một harness AddressSanitizer có callback đóng một assembly; hãy dùng nó để xác nhận việc truy cập nút đã được giải phóng. Công việc hoàn tất khi quá trình liệt kê duyệt danh sách đã sao chép và harness chạy xong mà không gặp lỗi use-after-free đã được báo cáo.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Description
mono_assembly_foreach() (src/mono/mono/metadata/assembly.c:3034-3049) copies the global loaded_assemblies list under the assemblies lock — the comment says it is done "to avoid calling the callback inside the lock" — but then walks the live list instead of the copy. When the callback releases the last reference of the assembly it was called with, mono_assembly_close() unlinks and frees the list node that g_list_foreach() is currently visiting, and the loop reads that freed node to advance.
Reproduction Steps
Attached: harness.c (uses the public embedding API only), Poc.cs, Target.cs, and the files needed to build an ASan-instrumented runtime.
The harness initialises the runtime, loads a small assembly Target.dll that nothing else references (so its reference count is 1), and then calls:
mono_assembly_foreach (close_cb, NULL);
close_cb calls mono_assembly_close() once for Target.dll and returns.
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.
-
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.projThe 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=0is baked intomono-sgenbyasan-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). -
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=assembly_foreach_poc cmake --build <abs>/asan-obj --target assembly_foreach_poc -
Compile
Poc.csandTarget.cstoPoc.dllandTarget.dll(libraries from a Mono build of the same tree onMONO_PATH), then run:MONO_PATH=<mono assemblies> LD_LIBRARY_PATH=<dir with libSystem.*.so> \ ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp \ <abs>/asan-obj/assembly_foreach_poc close <abs>/Poc.dll <abs>/Target.dll
Expected behavior
The enumeration finishes, and the callback is called once for every assembly that was loaded when the enumeration started.
Actual behavior
ASan reports a heap-use-after-free read of the list node (asan.txt):
ERROR: AddressSanitizer: heap-use-after-free
READ of size 8
#0 monoeg_g_list_foreach src/mono/mono/eglib/glist.c:275:16
#1 mono_assembly_foreach src/mono/mono/metadata/assembly.c:3047:2
freed by thread T0 here:
#1 monoeg_g_list_free_1 src/mono/mono/eglib/glist.c:72:2
#2 monoeg_g_list_remove src/mono/mono/eglib/glist.c:124:2
#3 mono_assembly_close_except_image_pools src/mono/mono/metadata/assembly.c:2955:22
Without ASan the run is usually silent, because the freed 24-byte node still holds its old next pointer. When the node has been reused the process gets a SIGSEGV in g_list_foreach. A no-op callback on the same assemblies is clean on both builds.
Regression?
No response
Known Workarounds
No response
Configuration
- .NET 10.0.15 (tag
v10.0.15, commita3788b71495457354b64ca8040b6ded5ff6ca4f9), Mono runtime built from source with the attached hook, linux-x64, clang 18, Release flags plus-fsanitize=address. main(commit5f8157f1522a) has the same code at the same lines; the same report was also reproduced onmain@bdd9b750b70.- v11.0.0 (tag
v11.0.0): same code (sources compared; not run).
Other information
Root cause
src/mono/mono/metadata/assembly.c:3043-3049 — identical on main and release/10.0:
mono_assemblies_lock ();
copy = g_list_copy (loaded_assemblies);
mono_assemblies_unlock ();
g_list_foreach (loaded_assemblies, func, user_data);
g_list_free (copy);
The callback runs outside the lock and may unload an assembly. Releasing the last reference unlinks the node from the live list (assembly.c:2954-2957):
mono_assemblies_lock ();
loaded_assemblies = g_list_remove (loaded_assemblies, assembly);
loaded_assembly_count--;
mono_assemblies_unlock ();
g_list_remove() frees the node, which is the one g_list_foreach() has cached in its loop variable.
mono_assembly_foreach is public embedding API (MONO_API_FUNCTION in src/native/public/mono/metadata/details/assembly-functions.h:27). The only in-tree caller is mono_precompile_assemblies() (src/mono/mono/mini/mini-runtime.c:5395), whose callback does not release assemblies, which is why the standalone runtime is not affected.
- 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
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.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/runtime
-
area-VM-meta-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
area-Debugger-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
dotnet/runtime#138494 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log callCó thể đã có người làm @Mohamedadel-prog đã nhận 1 ngày trước. Đang mởarea-Extensions-Logging untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
dotnet/runtime#138418 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-System.IO untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/runtime#138417 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-Codegen-AOT-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 64/100
Maintainer thường phản hồi trong vòng 1 ngày
Tất cả issue của dotnet/runtime
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/sqltoolsservice#2851 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
elsa-workflows/elsa-foundation#6322 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
mohebifar/made-in-iran#273 ·
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with TakeCó thể đã có người làm @Mohamedadel-prog đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
dotnet/efcore#39848 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày