Skip to content

[Bug] lwp/signal: stale thread pointer cached in sig_dispatch_thr[] is never invalidated on thread exit, leading to use-after-free and a fully controlled kernel indirect call #12091

Backlog

Description

@XueDugu

RT-Thread Version: master @ 5d1fb66 (v5.4.0-dev). Introduced in 58e0ddf (2023-07-19, PR #7828); first affected release v5.0.2; all releases v5.0.2 – v5.3.0 and current master are affected (verified by reading components/lwp/lwp_signal.c at each tag).

Affected area: Kernel — components/lwp signal delivery (struct lwp_signal.sig_dispatch_thr[], lwp_signal.c)

Hardware-BSP vendor: QEMU

Architecture: ARM 32-bit (Cortex-A9), RT-Thread Smart (RT_USING_SMART=y, default for modern Smart BSPs)

Board and hardware details: qemu-vexpress-a9, stock QEMU (no device-model patches), arm-linux-musleabi gcc 7.3.0 toolchain, kernel 5.3.1 build of master @ 5d1fb66

Develop Toolchain: arm-linux-musleabi-gcc (RT-Thread Smart userland), scons BSP build

Describe the bug:

struct lwp_signal caches a raw thread pointer per signal number in sig_dispatch_thr[_LWP_NSIG]. _signal_find_catcher() writes it on every process-directed delivery (lwp_signal.c:945) and dereferences it on the next delivery (lwp_signal.c:913 → _sigismember(&candidate->signal.sigset_mask, signo) at :869). No thread-exit path ever invalidates this cache: the whole tree touches sig_dispatch_thr only in lwp_signal_init()'s memset (:512) and the :913/:945 read/write pair. Thread teardown (lwp_thread_exit → _thread_exit → rt_thread_delete → rt_free) frees the 320-byte struct rt_thread while the cache keeps pointing at it, and lwp_thread_signal_detach() only clears the exiting thread's own sigqueue.

After the cached thread is freed, the next kill(pid, <same signo>) dereferences the dangling pointer (use-after-free, CWE-416). The freed 320-byte kernel chunk can be deterministically reclaimed with fully attacker-controlled content by an unprivileged thread of the same process via mount("tmpfs") + write() (_dfs_tmpfs_write does rt_realloc(NULL, 320) and copies the user buffer verbatim). Crafting the fake rt_thread with:

  • parent.type = RT_Object_Class_Thread (offset 16, passes the type assert in the wakeup path)
  • sched_thread_ctx.stat = 0x04 (offset 64 — "suspended, interruptible" selects the wakeup branch of _do_signal_wakeup, lwp_signal.c:820)
  • signal.sigset_mask = 0 (offset 196 — target signal looks unblocked)
  • wakeup_handle.func (offset 236) and .user_data (offset 240) = attacker values

makes rt_thread_wakeup() (src/thread.c:1169) execute:

func = thread->wakeup_handle.func;         /* attacker-controlled */
thread->wakeup_handle.func = RT_NULL;
...
ret = func(thread->wakeup_handle.user_data, thread);

i.e. one kernel-privilege indirect call with both arguments attacker-controlled (PC and r0 fully controlled; CWE-416 → CWE-822).

Reproduction (PoC runs as an unprivileged RT-Smart user process; official syscalls only — sigprocmask, clone, kill, exit, open/write, mount):

  1. sigprocmask: block SIGUSR1 in the main thread.
  2. clone(): create an in-process thread T that does not block SIGUSR1 (the pthread path — joins lwp->t_grp).
  3. kill(getpid, SIGUSR1) → _signal_find_catcher caches T as the SIGUSR1 catcher.
  4. T calls exit() → rt_thread_delete(T) frees the object.
  5. mount tmpfs + twelve open/write of 320 attacker-chosen bytes groom the freed slot.
  6. kill(getpid, SIGUSR1) again → UAF read → rt_thread_wakeup indirect call.

Expected: a thread's exit must invalidate (or ref-count) any sig_dispatch_thr[] entry pointing to it; delivery must never dereference a freed rt_thread.
Actual (banner variant, reproduced 4/4 independent runs):

[4] kill again -> dereference freed dispatch cache
[signal-dispatch-pwn] kernel executed rt_kprintf with attacker args

The kernel executed the attacker-chosen function (rt_kprintf) with the attacker-supplied argument (a string located in the attacker's user pages). Crash variant with func = 0xdeadbe41:

prefetch abort:
r00:0xdeadbeef r03:0xdeadbe41
pc :0xdeadbe44

GDB evidence: a hardware read-watchpoint on the freed slot's wakeup_handle.func fires at rt_thread_wakeup (thread.c:1169) reading the attacker value, with backtrace
rt_thread_wakeup ← _do_signal_wakeup (lwp_signal.c:820) ← _siginfo_deliver_to_lwp (lwp_signal.c:903) ← lwp_signal_kill (lwp_signal.c:1038).
Control groups: with the thread kept alive delivery stays normal (kernel survives 3 kills); with the thread exited but no groom the second kill reads benign data and the kernel survives — both recorded.

Other additional context:

  • CVSS 3.1: AV:L/AC:L/PR:L/UI:N/S:U/C:H/I:H/A:H = 7.8 (High). Prerequisites declared explicitly: local unprivileged RT-Smart user process; default BSP configuration; stock QEMU; no info-leak prerequisite, no second process needed.
  • CWE-416 (use-after-free) leading to CWE-822 (untrusted pointer dereference) / indirect call.
  • Root cause is distinct from all previously reported lwp issues: it is a lifetime/UAF bug in the signal dispatch cache, not an index-bounds bug. GitHub issue/PR search for sig_dispatch returns nothing; NVD lwp-family CVEs (CVE-2025-5865/5866/5867/5868/5869/1115/6693 etc.) cover other code paths (sys_select/sendto/recvfrom/sigprocmask how/device syscalls); issue [Bug] lwp: sys_sigaction missing signal-number range check allows out-of-bounds read/write in kernel heap (RT-Smart) #11882 (sigaction signo OOB) fixed the registration face only — this cache lifetime bug is unrelated to any incomplete fix.
  • Suggested fix: in the thread teardown path (_thread_exit/rt_thread_delete), walk the process's sig_dispatch_thr[] and NULL the entries that point at the exiting thread (the array is only 64 entries); alternatively hold a reference on the cached catcher and validate it in _signal_find_catcher.

Activity

  1. github-actions commented on Nov 27, 2026

    @github-actions

    如需认领并处理此 Issue,请评论 /claim。

    To claim and work on this issue, comment /claim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions