POSIX CPU Timers TOCTOU race (CVE-2025-38352)

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks

此页面记录了 Linux/Android POSIX CPU timers 中的一个 TOCTOU 竞争条件,该条件可以破坏定时器状态并导致内核崩溃,在某些情况下可被引导为提权。

  • 受影响组件:kernel/time/posix-cpu-timers.c
  • 原语:任务退出时的 expiry 与 deletion 竞争
  • 配置敏感:CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path)

内部机制快速回顾(与利用相关)

  • 三个 CPU 时钟通过 cpu_clock_sample() 驱动定时器的记账:
  • CPUCLOCK_PROF: utime + stime
  • CPUCLOCK_VIRT: utime only
  • CPUCLOCK_SCHED: task_sched_runtime()
  • 定时器创建时将定时器关联到 task/pid 并初始化 timerqueue 节点:
static int posix_cpu_timer_create(struct k_itimer *new_timer) {
struct pid *pid;
rcu_read_lock();
pid = pid_for_clock(new_timer->it_clock, false);
if (!pid) { rcu_read_unlock(); return -EINVAL; }
new_timer->kclock = &clock_posix_cpu;
timerqueue_init(&new_timer->it.cpu.node);
new_timer->it.cpu.pid = get_pid(pid);
rcu_read_unlock();
return 0;
}
  • Arming 会将条目插入到 per-base timerqueue 中,并可能更新 next-expiry cache:
static void arm_timer(struct k_itimer *timer, struct task_struct *p) {
struct posix_cputimer_base *base = timer_base(timer, p);
struct cpu_timer *ctmr = &timer->it.cpu;
u64 newexp = cpu_timer_getexpires(ctmr);
if (!cpu_timer_enqueue(&base->tqhead, ctmr)) return;
if (newexp < base->nextevt) base->nextevt = newexp;
}
  • 快速路径避免昂贵的处理,除非缓存的到期时间表明可能触发:
static inline bool fastpath_timer_check(struct task_struct *tsk) {
struct posix_cputimers *pct = &tsk->posix_cputimers;
if (!expiry_cache_is_inactive(pct)) {
u64 samples[CPUCLOCK_MAX];
task_sample_cputime(tsk, samples);
if (task_cputimers_expired(samples, pct))
return true;
}
return false;
}
  • 过期处理会收集已过期的 timers,标记它们为 firing,将它们从队列中移除;实际交付被延迟:
#define MAX_COLLECTED 20
static u64 collect_timerqueue(struct timerqueue_head *head,
struct list_head *firing, u64 now) {
struct timerqueue_node *next; int i = 0;
while ((next = timerqueue_getnext(head))) {
struct cpu_timer *ctmr = container_of(next, struct cpu_timer, node);
u64 expires = cpu_timer_getexpires(ctmr);
if (++i == MAX_COLLECTED || now < expires) return expires;
ctmr->firing = 1;                           // critical state
rcu_assign_pointer(ctmr->handling, current);
cpu_timer_dequeue(ctmr);
list_add_tail(&ctmr->elist, firing);
}
return U64_MAX;
}

两种到期处理模式

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: 到期通过 target task 上的 task_work 延迟处理
  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: 到期在 IRQ 上下文中直接处理
void run_posix_cpu_timers(void) {
struct task_struct *tsk = current;
__run_posix_cpu_timers(tsk);
}
#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK
static inline void __run_posix_cpu_timers(struct task_struct *tsk) {
if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled)) return;
tsk->posix_cputimers_work.scheduled = true;
task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME);
}
#else
static inline void __run_posix_cpu_timers(struct task_struct *tsk) {
lockdep_posixtimer_enter();
handle_posix_cpu_timers(tsk);                  // IRQ-context path
lockdep_posixtimer_exit();
}
#endif

在 IRQ-context 路径中,firing list 在 sighand 之外被处理。

static void handle_posix_cpu_timers(struct task_struct *tsk) {
struct k_itimer *timer, *next; unsigned long flags, start;
LIST_HEAD(firing);
if (!lock_task_sighand(tsk, &flags)) return;   // may fail on exit
do {
start = READ_ONCE(jiffies); barrier();
check_thread_timers(tsk, &firing);
check_process_timers(tsk, &firing);
} while (!posix_cpu_timers_enable_work(tsk, start));
unlock_task_sighand(tsk, &flags);              // race window opens here
list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) {
int cpu_firing;
spin_lock(&timer->it_lock);
list_del_init(&timer->it.cpu.elist);
cpu_firing = timer->it.cpu.firing;         // read then reset
timer->it.cpu.firing = 0;
if (likely(cpu_firing >= 0)) cpu_timer_fire(timer);
rcu_assign_pointer(timer->it.cpu.handling, NULL);
spin_unlock(&timer->it_lock);
}
}

Root cause: TOCTOU between IRQ-time expiry and concurrent deletion under task exit 前提条件

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK is disabled(使用 IRQ 路径)
  • 目标任务正在退出但尚未完全回收
  • 另一个线程并发地对同一定时器调用 posix_cpu_timer_del()

过程

  1. update_process_times() 在 IRQ 上下文中为正在退出的任务触发 run_posix_cpu_timers()。
  2. collect_timerqueue() 将 ctmr->firing = 1 并把定时器移到临时的 firing 列表。
  3. handle_posix_cpu_timers() 通过 unlock_task_sighand() 释放 sighand,以在锁外交付定时器。
  4. 在 unlock 之后立即,正在退出的任务可能被回收;一个兄弟线程会执行 posix_cpu_timer_del()。
  5. 在这段窗口期内,posix_cpu_timer_del() 可能无法通过 cpu_timer_task_rcu()/lock_task_sighand() 获取状态,从而跳过检查 timer->it.cpu.firing 的正常在飞(in-flight)保护。删除会以未在 firing 的方式继续,导致在处理到期时破坏状态,进而引起崩溃/未定义行为 (UB)。

为什么 TASK_WORK 模式在设计上是安全的

  • 在 CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y 下,到期会被延迟到 task_work;exit_task_work 在 exit_notify 之前运行,因此不会出现与回收的 IRQ-time 重叠。
  • 即便如此,如果任务已经在退出,task_work_add() 会失败;对 exit_state 的门控使两种模式保持一致。

Fix (Android common kernel) and rationale

  • 如果 current task 正在退出,则添加一个早期返回以阻止所有处理:
// kernel/time/posix-cpu-timers.c (Android common kernel commit 157f357d50b5038e5eaad0b2b438f923ac40afeb)
if (tsk->exit_state)
return;
  • 这会阻止对即将退出的任务进入 handle_posix_cpu_timers(),消除了 posix_cpu_timer_del() 可能错过 it.cpu.firing 并与到期处理发生竞态的窗口。

Impact

  • 在并发到期/删除期间对定时器结构的内核内存破坏可能导致立即崩溃(DoS),并且由于可以对任意内核状态进行操控,是通向权限提升的重要原语。

Triggering the bug (safe, reproducible conditions) Build/config

  • Ensure CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n and use a kernel without the exit_state gating fix.

Runtime strategy

  • 针对一个即将退出的线程并向其附加一个 CPU timer(每线程或进程范围的时钟):
  • For per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, …)
  • For process-wide: timer_create(CLOCK_PROCESS_CPUTIME_ID, …)
  • Arm with a very short initial expiration and small interval to maximize IRQ-path entries:
static timer_t t;
static void setup_cpu_timer(void) {
struct sigevent sev = {0};
sev.sigev_notify = SIGEV_SIGNAL;    // delivery type not critical for the race
sev.sigev_signo = SIGUSR1;
if (timer_create(CLOCK_THREAD_CPUTIME_ID, &sev, &t)) perror("timer_create");
struct itimerspec its = {0};
its.it_value.tv_nsec = 1;           // fire ASAP
its.it_interval.tv_nsec = 1;        // re-fire
if (timer_settime(t, 0, &its, NULL)) perror("timer_settime");
}
  • 从 sibling thread 并发删除相同的 timer,在 target thread 退出时:
void *deleter(void *arg) {
for (;;) (void)timer_delete(t);     // hammer delete in a loop
}
  • 竞态放大因素:高调度器时钟频率、CPU 负载、重复的线程退出/重建循环。崩溃通常在 posix_cpu_timer_del() 因为在 unlock_task_sighand() 之后立即 task 查找/锁定失败而跳过检测到触发时出现。

检测与加固

  • 缓解:应用 exit_state 保护;尽可能启用 CONFIG_POSIX_CPU_TIMERS_TASK_WORK。
  • 可观测性:在 unlock_task_sighand()/posix_cpu_timer_del() 周围添加 tracepoints/WARN_ONCE;当观察到 it.cpu.firing==1 且 cpu_timer_task_rcu()/lock_task_sighand() 失败时发出告警;监视任务退出时的 timerqueue 不一致情况。

审计重点(供审核者)

  • update_process_times() → run_posix_cpu_timers() (IRQ)
  • __run_posix_cpu_timers() 选择 (TASK_WORK vs IRQ 路径)
  • collect_timerqueue(): 设置 ctmr->firing 并移动节点
  • handle_posix_cpu_timers(): 在触发循环前释放 sighand
  • posix_cpu_timer_del(): 依赖 it.cpu.firing 来检测正在进行的过期;当任务在退出/回收期间查找/锁定失败时,该检查会被跳过

漏洞利用研究备注

  • 已披露的行为是一个可靠的内核崩溃原语;将其转化为提权通常需要额外的可控重叠(对象生命周期或 write-what-where 影响),超出本摘要范围。将任何 PoC 视为可能破坏系统的,并仅在模拟器/VM 中运行。

另见

Ksmbd Streams Xattr Oob Write Cve 2025 37947

参考资料

Tip

学习和实践 AWS 黑客技术:HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE) 学习和实践 Azure 黑客技术:HackTricks Training Azure Red Team Expert (AzRTE)

支持 HackTricks