[libdl] dlmodule: 32-bit section-size sum overflow in dlmodule_load_relocated_object() -> heap overflow (follow-up to #11898)
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 35/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- c
- Ambito
- embedded-iot, security
Direzione di ricerca
Start with components/libc/posix/libdl/dlelf.c, especially dlmodule_load_relocated_object() around the section-size accumulation and copy loop; then read dlmodule.c around dlmodule_load() to trace how the file buffer is sized. Use the three malformed ELF inputs described in the issue to check the overflow and bounds cases, and verify benign ARM ET_REL and ET_DYN modules still load. Done means malformed inputs are rejected without sanitizer errors.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Summary
dlmodule_load_relocated_object() sizes the module image by adding the sh_size of every loadable
section into an rt_ubase_t (32 bits on ARM/RISC-V32/i386) without an overflow check, allocates that
many bytes, and then copies or zero-fills every section with its full sh_size. A relocatable module
whose section sizes sum past 2^32 gets a small allocation and the loader writes far past it. Section
offsets/sizes are also never compared with the file length, so rt_memcpy() reads past the file buffer.
The writes happen inside dlmodule_load(), before the relocation-target checks of PR #12016 run and
before any module code is executed.
Tested commit: master fbdb9711db8be41fe84f92a2f983c048c11f9a47 (2026-12-01). The loader files are
byte-identical to 332ac026 and contain PR #12016 (df2e0bffec).
Root cause (components/libc/posix/libdl/dlelf.c, master)
437 for (index = 0; index < elf_module->e_shnum; index ++)
440 if (IS_PROG(shdr[index]) && IS_AX(shdr[index])) module_size += shdr[index].sh_size; /* text /
446 if (IS_PROG(shdr[index]) && IS_ALLOC(shdr[index])) module_size += shdr[index].sh_size; / rodata /
451 if (IS_PROG(shdr[index]) && IS_AW(shdr[index])) module_size += shdr[index].sh_size; / data /
456 if (IS_NOPROG(shdr[index]) && IS_AW(shdr[index])) module_size += shdr[index].sh_size; / bss /
483 module_size += RT_ALIGN(dlmodule_common_block(...), sizeof(rt_ubase_t)); / also unchecked /
491 module_size += common_align - 1; / also unchecked /
500 module->mem_space = rt_malloc(module_size); / wrapped, small */
518 rt_memset(ptr, 0, module_size);
527 rt_memcpy(ptr, (rt_uint8_t )elf_module + shdr[index].sh_offset, shdr[index].sh_size); / text /
538 rt_memcpy(ptr, ... , shdr[index].sh_size); / rodata /
549 rt_memcpy(ptr, ... , shdr[index].sh_size); / data /
560 rt_memset(ptr, 0, shdr[index].sh_size); / bss */
module_ptr is the whole file read into a heap buffer of exactly the file length (dlmodule.c:513-516),
and sh_offset + sh_size is never checked against that length.
Affected versions
The unchecked sum has existed since the module loader was added: 5211e7ff8f "add module feature"
(2010-04-11, src/module.c, first tag v1.2.0), moved to components/libc/libdl/dlelf.c by efe452313
(2018-08-30, v3.1.0) and later to components/libc/posix/libdl/. Present in v5.3.0 (99428a1e7f,
2026-09-10) and master fbdb971. PR #12016 does not cover it: its checks run after the copy loop.
Reproduction
Harness: the unmodified dlelf.c dlmodule.c dlsym.c dlopen.c dlclose.c arch/arm.c compiled for ARM32
with arm-linux-gnueabihf-gcc -fsanitize=address,undefined; a shim maps rt_malloc/rt_free/rt_memcpy/
rt_memset/... and the module symbol table to host functions; main() calls
dlopen(argv[1]) -> dlsym("main") -> dlclose(). Run under qemu-arm (user mode). Inputs are made from a
benign ARM relocatable module (m1_arm.o) by changing one field:
bss_memset_write.o a .rodata section with sh_size = 0xffffffff -> 32-bit sum wraps to 203 bytes
negative_size_memset.o .bss sh_size = 0xfffffffe
memcpy_huge_sh_size.o a .data section with sh_size = 0x1000000 (16 MiB, larger than the 1.5 KiB file)
=== bss_memset_write.o
ERROR: AddressSanitizer: heap-buffer-overflow on address 0x43503f8b
WRITE of size 128 at 0x43503f8b thread T0
#1 rt_memset harness/shim.c:25
#2 dlmodule_load_relocated_object rtt/components/libc/posix/libdl/dlelf.c:560
#3 dlmodule_load rtt/components/libc/posix/libdl/dlmodule.c:556
#4 rt_dlopen rtt/components/libc/posix/libdl/dlopen.c:74
0x43503f8b is located 0 bytes after 203-byte region [0x43503ec0,0x43503f8b)
#2 dlmodule_load_relocated_object rtt/components/libc/posix/libdl/dlelf.c:500
=== negative_size_memset.o
ERROR: AddressSanitizer: negative-size-param: (size=-2)
#2 dlmodule_load_relocated_object rtt/components/libc/posix/libdl/dlelf.c:560
=== memcpy_huge_sh_size.o
ERROR: AddressSanitizer: memcpy-param-overlap: memory ranges [0x4350186c,0x4450186c) and [0x43303720, 0x44303720) overlap
#2 dlmodule_load_relocated_object rtt/components/libc/posix/libdl/dlelf.c:549
0x43303bf0 is located 0 bytes after 1392-byte region [0x43303680,0x43303bf0) <- the file buffer (dlmodule.c:513)
No loader error message is printed before the crash. 8 benign modules (ARM ET_REL and ET_DYN) load
without any sanitizer report.
Impact
- Kernel heap overflow of attacker-chosen length with zero bytes or attacker-controlled file contents
at module-load time, in the context of the thread calling dlopen() / dlmodule_exec() (msh executes
*.mo files found on the filesystem, components/finsh/msh.c:312). This may allow arbitrary code
execution in the kernel. - Out-of-bounds read of the file buffer (sh_offset + sh_size unchecked), up to 4 GiB.
- The corruption happens during dlopen() of a malformed or truncated module, before its constructors
run and before any check of the loaded image can take place; it is in the input validation that
#12016 started to add (its review asked for sh_offset/sh_size validation against the file size as a
follow-up).
Suggested fix (3 files, +89/-16; I can open a PR)
- dlmodule_size_add(): overflow-checked accumulation of every term of module_size (section sizes,
counted once per copy exactly as the copy loop does, the common block and its alignment padding). - dlmodule_in_image(): [sh_offset, sh_offset + sh_size) of every section that is copied, and of symbol
tables, must lie inside the file; same for the section/program header tables and for PT_LOAD
[p_offset, p_offset + p_filesz) in dlmodule_load_shared_object(). - dlmodule_load() / dlmodule_load_custom() pass the file length to the two loaders (0 = unknown for a
custom ops->load() that does not report a length; the size-overflow checks still apply there).
With this change all three inputs are rejected ("Module: section 6 out of image", "Module: section 5
size overflow", "Module: section 3 out of image") with no sanitizer report, and all benign modules
load. On a 3,000-file sample of a 42,460-file mutation corpus, 63 files that crashed no longer crash,
no file gains a sanitizer report, and 11 files that were accepted are now rejected (all have a
segment, section or header table outside the file, or a common-block alignment that overflows).
Not addressed (read-only): unterminated string tables reaching strcmp/strlen, symbol/relocation table
reads at dlelf.c:250/337/469/689.
Classification: CWE-190 leading to CWE-122/CWE-787; CWE-125 for the file-buffer read.
Suggested CVSS 3.1: CVSS:3.1/AV:L/AC:L/PR:N/UI:R/S:U/C:N/I:H/A:H = 7.1 (a user or operator loads a
crafted module file).
Reporter: Jeongwon Jang (SWSEC Lab, Chungnam National University), [email protected]
Discovery and analysis: this issue was found with automated tools: a structure-aware ELF fuzzer
(Exefuzzer, an AFL++ custom mutator), AFL++, and LLM-assisted source review and target-specific
mutation scripts. The results above were reproduced by re-running the attached reproducers; the
analysis and this write-up were prepared with the assistance of these tools, and I reviewed them.
Disclosure: I have not shared this issue with anyone else. Please let me know your preferred
disclosure timeline; I am happy to test a fix.
- Lingua principale
- C
- Stelle
- 12.4k
- Fork
- 5.5k
- Merge medio
- 3g 13h
- PR unite (30g)
- 73
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 RT-Thread/rt-thread
-
[Bug] Out-of-bounds stack read and callback signature mismatch in TM4C123 ADC driver (drv_adc.c)Apertabug Component component: drivers
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
RT-Thread/rt-thread#12118 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
BSP BSP: NXP bug Component component: drivers
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
RT-Thread/rt-thread#12117 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
BSP BSP: GD32 bug Component component: drivers
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
RT-Thread/rt-thread#12115 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
-
Arch: ARM/AArch64 BSP BSP: GD32 bug Component component: drivers
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
RT-Thread/rt-thread#12109 · 5 commenti ·
I maintainer di solito rispondono entro 2 giorni
-
action bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
RT-Thread/rt-thread#12106 · 1 commento ·
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di RT-Thread/rt-thread
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
signalwire/freeswitch#3329 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
Status: Opened
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
espressif/esp-protocols#1224 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 83/100
I maintainer di solito rispondono entro 1 giorno
-
bot-triaged module: bfloat16 module: cuda module: half module: numerical-stability triage review
Difficoltà 2/5 1-3 ore Idoneità per principianti 77/100
I maintainer di solito rispondono entro 1 giorno