[mono] mono_dl_fallback_unregister frees the list head but keeps the dangling head pointer
I maintainer di solito rispondono entro 1 giorno
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 75/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- c
- Ambito
- backend, operating-systems
Direzione di ricerca
Leggi src/mono/mono/utils/mono-dl.c, in particolare mono_dl_fallback_unregister() e il ciclo di fallback in mono_dl_open_full(). Riproduci l’use-after-free con l’harness allegato e la configurazione di AddressSanitizer, quindi verifica che la rimozione del nodo iniziale lasci la lista vuota e che una chiamata a mono_dl_open() non riuscita restituisca NULL senza segnalare errori.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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.
-
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=dlfallback-harness cmake --build <abs>/asan-obj --target dlfallback-harness -
Run it:
ASAN_OPTIONS=detect_leaks=0:detect_stack_use_after_return=0:suppressions=<abs>/mono-asan.supp <abs>/asan-obj/dlfallback-harness headThe
headscene needs no runtime initialisation.
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, 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). - 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.
- Lingua principale
- C#
- Stelle
- 18.5k
- Fork
- 5.8k
- Merge medio
- 2g 8h
- PR unite (30g)
- 597
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di dotnet/runtime
-
area-Debugger-mono untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
dotnet/runtime#138494 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
area-AssemblyLoader-mono untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/runtime#138493 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
JsonConsoleFormatter throws on NaN and infinite double and float values, failing the log callForse già presa @Mohamedadel-prog l’ha presa 1 giorno fa. Apertaarea-Extensions-Logging untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
dotnet/runtime#138418 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
area-System.IO untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
dotnet/runtime#138417 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
area-Codegen-AOT-mono untriaged
Difficoltà 2/5 1-3 ore Idoneità per principianti 64/100
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di dotnet/runtime
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
microsoft/sqltoolsservice#2851 ·
I maintainer di solito rispondono entro 1 giorno
-
enhancement
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno
-
needs-triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
elsa-workflows/elsa-foundation#6322 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
mohebifar/made-in-iran#273 ·
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with TakeForse già presa @Mohamedadel-prog l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
dotnet/efcore#39848 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni