[libdl] dlmodule: 32-bit section-size sum overflow in dlmodule_load_relocated_object() -> heap overflow (follow-up to #11898)

Ouverte
#12,111 1 commentaire 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 2 jours

Personne n'a encore pris cette issue.

Évaluation

Difficulté
4/5
Temps estimé
3-5 jours
Accessibilité débutants
35/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
c

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

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.

Langage dominant
C
Étoiles
12.4k
Forks
5.5k
Merge moyen
3 j 13 h
PR mergées (30 j)
73

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de RT-Thread/rt-thread

Toutes les issues de RT-Thread/rt-thread

Issues similaires

Plus d'issues C

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.