[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

Aperta
#12,110 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

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
36/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
c, operating-systems

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
C
Stelle
12.4k
Fork
5.5k
Merge medio
3g 13h
PR unite (30g)
73

Preparare l'ambiente

Apri in Codespaces

Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RT-Thread/rt-thread

Tutte le issue di RT-Thread/rt-thread

Issue simili

Altre issue su C

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.