[Bug] [Smart] mm: rt_page_bits() never clamps to RT_PAGE_MAX_ORDER — oversized shmget(size) indexes buddy lists out of bounds, groomable to arbitrary kernel write and kernel code execution

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

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start with components/mm/mm_page.c, especially rt_page_bits() and the _pages_alloc/__pages_alloc paths; trace how the order reaches the buddy-list index. Check the RT-Smart shmget() path for size bounds, then reproduce with the stated 4 MiB+1 case on the QEMU AArch64 setup. Done means oversized requests fail before page-list access and valid allocations still work.

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

描述

RT-Thread Version

master fbdb9711db8be41fe84f92a2f983c048c11f9a47 (2026-11-30 tip). Introduced by 7450ef6c4d71 (2023-01-09, RT-Smart MM layer #6809). First affected release: v5.0.0; verified still present on v5.0.0–v5.3.0 and current master, and on branches v5.0.x / v5.1.x / lts-v5.2.x / v4.0.x / lts-v4.1.x (rt_page_bits() has no order clamp in any of them).

Affected area

Memory management — components/mm/mm_page.c (RT-Smart MMU page allocator)

Hardware-BSP vendor

QEMU

Architecture

AArch64 (ARM)

Board and hardware details

qemu-virt64-aarch64, QEMU virt machine, 1 CPU, 128 MiB, RT-Smart (RT_USING_SMART=y + System V shm), virtio-blk rootfs, GICv2. Reproduced on a kernel rebuilt from current master tip.

Develop Toolchain

aarch64-linux-musleabi gcc 7.3 (scons), QEMU 9.2.3

Describe the bug

rt_page_bits() (components/mm/mm_page.c:453) returns a page order computed from a caller-supplied size with only a lower clamp (bit < 0 → 0). It never rejects orders above RT_PAGE_MAX_ORDER (11). _pages_alloc/__pages_alloc use the returned size_bits directly as a buddy-list index (_get_pgls_head(agr_pgls, affid, size_bits) → agr_pgls[size_bits]), and the only guards are RT_ASSERT(size_bits < RT_PAGE_MAX_ORDER) — compiled out in release builds. shmget() passes an unbounded user size into this path, so an unprivileged RT-Smart process indexes the static buddy-list array out of bounds with a fully controlled offset.

Two concrete outcomes, both reproduced:

  1. DoS: shmget(key, 4MiB+1) → size_bits=11 → page_list_low[11] aliases page_list_high[0] (NULL) → _page_remove(NULL, ...) reads p->pre at 0x8:
shmget: mode=attack size=0x400001
Data abort
fault addr = 0x0000000000000008
abort caused by read instruction
  1. Arbitrary write → kernel code execution: shmget(key, 1<<49) → size_bits=37 → page_list_low[37] lands exactly on the adjacent static _last_alloc pointer. The PoC first allocates two legal 4 KiB shm segments so _last_alloc points at a user-mapped page, then forges a struct rt_page header there (pre/next attacker-chosen). The oversized shmget makes _page_remove() run the list unlink on the forged page — two arbitrary kernel writes — and returns the forged page as a valid allocation. The PoC uses the writes to redirect rt_object_take_hook into an AArch64 stub placed inside the controlled page, then triggers the hook with a public sem_take() syscall. GDB on the rebuilt master-tip kernel:
[oversized shmget] _last_alloc(OOB slot)=0xffff0000045e0000 pointee:
0xffff0000045e0000:	0x4148434b44455f4d	0xffff0000002ba078     <- attacker-written fake rt_page header

[CRAFT-HIT] arbitrary kernel write: rt_object_put_hook now = 0x4148434b44455f4d
#0 _page_remove (page_head=0xffff0000001f7f70 <_last_alloc>, p=0xffff0000045e0000, size_bits=37) mm_page.c:548
#1 __pages_alloc (agr_pgls=0xffff0000001f7e48 <page_list_low>, size_bits=37, ...)
#5 rt_pages_alloc_ext (size_bits=37, ...)
#6 _lwp_shmget (size=562949953421312)

[PWN-1] sem-take hook armed: rt_object_take_hook = 0xffff0000045e0100 (attacker kernel page)
[PWN-2] EXECUTION at attacker-controlled kernel page 0xffff0000045e0100:
=> 0xffff0000045e0100:  b 0xffff0000045e0120          <- attacker shellcode bytes, kernel mode
[PWN-body] attacker shellcode BODY reached at 0x45e0120:
   mov x0, #0xa078 / movk x0, #0x2ba, lsl #16 / movk x0, #0xffff, lsl #48
   mov x1, #0x5f4d / movk ...                              <- stub builds & writes marker

Steps to reproduce (all official POSIX APIs from an unprivileged process: shmget/shmat/sem_create/sem_take):

  1. Build qemu-virt64-aarch64 with RT-Smart enabled (stock Smart configuration).
  2. Run the attached PoC modes from msh:
    • control (size=4 MiB): returns normally, system alive;
    • attack (size=4 MiB+1): kernel Data abort at fault address 0x8, reproducible on every boot;
    • craft (size=1<<49, groomed): _page_remove performs attacker-directed writes into kernel globals;
    • pwn (size=1<<49, groomed + stub): rt_object_take_hook is overwritten with the address of an attacker page and the kernel fetches and executes attacker bytes there.

Note for anyone re-running: on current master rt_mutex_take() no longer calls rt_object_take_hook — the hook trigger in the reproducer uses sem_create/sem_take (src/ipc.c:642).

Expected: shmget() with a size that maps to an order ≥ RT_PAGE_MAX_ORDER must fail with an error before any page-list indexing (the same clamp rt_page_bits() callers need everywhere).

Actual: the out-of-range order indexes static kernel state out of bounds; with small grooming this yields arbitrary kernel writes and kernel-mode execution of attacker bytes from an unprivileged user process.

Other additional context
  • CVSS 3.1: AV:L/AC:L/PR:L/UI:N/S:C/C:H/I:H/A:H = 8.8 (Critical). Preconditions stated explicitly: RT-Smart (MMU) build with System V shm reachable by an unprivileged local process — the default shape for RT-Smart products.
  • CWE-190 (integer/range error on the page order) → CWE-787 (OOB write via buddy-list unlink) → CWE-822/CD (untrusted pointer as kernel instruction source).
  • Dedup check (2026-12-02): GitHub issue/PR search for shmget / rt_page_bits / page-order overflow returns no prior report; not covered by any of the existing mm-related issues. All maintenance branches (v5.0.x, v5.1.x, lts-v5.2.x, v4.0.x, lts-v4.1.x) still contain the unclamped rt_page_bits().
  • Fix suggestion (two lines): reject orders past the array in rt_pages_alloc paths — e.g. clamp/fail in rt_page_bits() itself (if (bit >= RT_PAGE_MAX_ORDER) return -RT_ERROR; semantics for callers) and turn the RT_ASSERT(size_bits < RT_PAGE_MAX_ORDER) guards at mm_page.c:484/615 into unconditional runtime checks that return an allocation failure. shmget() should additionally bound size to the shm size limit before calling the page allocator.
主要语言
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 摘要。