🇰🇷 한글 설명
커널의 z_waitq_head() 헬퍼 함수가 SMP 시스템에서 스케줄러 스핀락(_sched_spinlock)을 적절히 획득하지 않고 대기 큐의 헤드를 읽어 발생하는 레이스 컨디션 취약점입니다. 이로 인해 타이머 만료 핸들러와 스레드 중단 과정이 상호 배타적이지 않게 되어, 권한이 없는 사용자가 커널 메모리의 Out-of-bounds 및 Use-after-free 쓰기를 유발할 수 있습니다.
💡 쉬운 설명
여러 개의 CPU가 동시에 작동할 때, 커널이 데이터를 안전하게 보호하는 잠금 장치를 제대로 사용하지 않아 발생하는 문제입니다. 공격자가 이 틈을 이용해 시스템의 중요한 정보를 망가뜨리거나 컴퓨터를 강제로 종료시킬 수 있습니다.
🇺🇸 원문 (English)
The kernel helper z_waitq_head() (kernel/include/wait_q.h) reads the head of a thread wait queue, but on SMP systems it was called from several places without holding the scheduler spinlock _sched_spinlock that serializes wait-queue mutation. The consequential call site is z_timer_expiration_handler() in kernel/timer.c, which held only the timer subsystem lock timer_lock while reading timer->wait_q and then unpending, setting the return value of, and readying the thread it obtained. halt_thread() in kernel/sched.c removes an aborted thread from its wait queue while holding only _sched_spinlock, so the two paths are not mutually exclusive.
When the last waiter is removed on another CPU inside that window, sys_dlist_peek_head() can return the list sentinel — a pointer into the k_timer object itself — typed as a struct k_thread * (with CONFIG_WAITQ_SCALABLE, rb_get_min() can return an arbitrary node of a tree being restructured), or it can return a pointer to a thread that has already been unpended and, for dynamically allocated threads, freed. The handler then calls unpend_thread_no_timeout(), which performs a sys_dlist_remove() through pointers read from that object, and arch_thread_return_value_set(), which writes unconditionally through the pointer.
The result is out-of-bounds and use-after-free writes in supervisor context, plus scheduler state corruption; a milder outcome is a thread that has re-pended elsewhere being woken with a success return value from an unrelated blocking call. All of the primitives needed to drive the interleaving — k_timer_start(), k_timer_status_sync(), k_thread_create() and k_thread_abort() — are system calls, so with CONFIG_USERSPACE enabled an unprivileged user-mode thread can attempt the race against kernel-owned data.
Exploitation requires a multi-CPU build (CONFIG_MP_MAX_NUM_CPUS greater than one; the affected region is a no-op on uniprocessor targets), local code execution, and winning a narrow timing window, so the practical outcome is most often a kernel crash rather than controlled corruption. The fix splits the helper into z_waitq_head_locked() for callers that already hold the scheduler spinlock and a z_waitq_head() that acquires it, and brings the timer expiration handler's head read, unpend and ready under a single LOCK_SCHED_SPINLOCK region.
📍 영향받는 시스템
CONFIG_MP_MAX_NUM_CPUS가 1보다 큰 멀티코어 환경의 커널 (CONFIG_USERSPACE 활성화 시)
⚔️ 공격 벡터
로컬 권한을 가진 사용자가 시스템 콜(k_timer_start, k_thread_create 등)을 이용해 정교한 타이밍의 레이스 컨디션 공격 수행
🛡️ 대응 방법
z_waitq_head() 함수를 잠금 방식에 따라 분리하고, 타이머 만료 핸들러의 동작을 단일 LOCK_SCHED_SPINLOCK 영역 내에서 실행되도록 커널 패치 적용
🔧 검증 코드
명령어 결과에서 MP_MAX_NUM_CPUS가 1보다 크고 CONFIG_USERSPACE가 y로 설정되어 있다면 취약한 환경일 가능성이 높습니다.
uname -a && grep -E 'CONFIG_MP_MAX_NUM_CPUS|CONFIG_USERSPACE' /boot/config-$(uname -r) 2>/dev/null