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 race 条件を記述しており、タイマー状態を破損させ kernel をクラッシュさせる可能性があり、場合によっては privilege escalation に利用され得ます。

  • 影響を受けるコンポーネント: kernel/time/posix-cpu-timers.c
  • プリミティブ: expiry vs deletion race under task exit
  • 設定依存: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQコンテキストのexpiryパス)

簡易内部要約(exploitation に関連)

  • 3つのCPUクロックが cpu_clock_sample() を介してタイマーのアカウンティングを駆動する:
  • CPUCLOCK_PROF: utime + stime
  • CPUCLOCK_VIRT: utime only
  • CPUCLOCK_SCHED: task_sched_runtime()
  • タイマー作成時に timer を task/pid に紐付け、timerqueue ノードを初期化する:
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;
}
  • Arming は 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;
}
  • 期限切れ処理は期限切れのタイマーを収集し、発火としてマークし、キューから移動します。実際の配信は遅延されます:
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 コンテキスト経路では、発火リストは 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 パスが使用されている)
  • 対象タスクは終了中だが完全には回収されていない
  • 別スレッドが同じタイマーに対して同時に posix_cpu_timer_del() を呼ぶ

Sequence

  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() を介して state を取得できず、timer->it.cpu.firing をチェックする通常のインフライトガードをスキップする可能性がある。削除は発火していないかのように進行し、expiry が処理されている間に状態を破壊してクラッシュや未定義動作(UB)を引き起こす。

Why TASK_WORK mode is safe by design

  • 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) and rationale

  • current タスクが終了中であれば早期にリターンを追加し、すべての処理をゲートする:
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 を見逃して expiry 処理とレースする可能性のあるウィンドウが排除されます。

Impact

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

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 タイマをアタッチします(スレッド単位またはプロセス全体のクロック):
  • For per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, ...)
  • For process-wide: 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");
}
  • 別の sibling thread から、ターゲット thread が終了するのと同時に同じ timer を削除する:
c
void *deleter(void *arg) {
for (;;) (void)timer_delete(t);     // hammer delete in a loop
}
  • レースを助長する要因: 高いスケジューラのティックレート、CPU負荷、スレッドの退出/再作成を繰り返すサイクル。クラッシュは通常、unlock_task_sighand() 直後のタスク検索/ロックに失敗したために posix_cpu_timer_del() が発火を見逃すときに発生します。

検出とハードニング

  • 緩和策: 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 に依存して進行中の期限切れを検出する; このチェックは exit/reap 中にタスクの検索/ロックが失敗した場合にスキップされる

エクスプロイト研究の注意事項

  • 公開された挙動は信頼性の高い kernel crash primitive です; これを privilege escalation に変えるには通常、この要約の範囲外でオブジェクト寿命や write-what-where のような追加の制御可能なオーバーラップが必要になります。任意の PoC はシステムを不安定にする可能性があるため、emulators/VMs 上でのみ実行してください。

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