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

未关闭
#12,111 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
c

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
C
星标
12.4k
派生
5.5k
平均合并
3 天 13 小时
30 天内合并 PR
73

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

RT-Thread/rt-thread 的其他 Issue

查看 RT-Thread/rt-thread 的全部 Issue

相似的 Issue

更多 C Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。