🇰🇷 한글 설명
kernel/queue.c의 z_queue_node_peek() 함수가 큐의 스핀락을 획득하지 않은 상태에서 노드를 참조하여 발생하는 Use-After-Free 취약점입니다. 동시성 환경에서 k_queue_get()이 노드를 해제하는 동안 peek 작업이 수행되면, 이미 해제된 메모리의 데이터를 읽거나 잘못된 포인터를 반환하여 시스템 충돌이나 메모리 오염을 유발할 수 있습니다.
💡 쉬운 설명
데이터를 확인하는 과정에서 안전장치(잠금)를 사용하지 않아, 다른 작업이 데이터를 삭제하는 순간에 잘못된 정보를 읽게 되는 문제입니다. 이로 인해 시스템이 갑자기 멈추거나 중요한 정보가 유출될 위험이 있습니다.
🇺🇸 원문 (English)
The kernel queue helper z_queue_node_peek() in kernel/queue.c dereferences a node taken from a queue's data_q list, reading the node's flag byte and, for items enqueued via k_queue_alloc_append/alloc_prepend, the data pointer of an internally allocated alloc_node struct. The implementations of z_impl_k_queue_peek_head() and z_impl_k_queue_peek_tail() performed this read-and-dereference without holding the queue's spinlock, while every other accessor of the same list — including k_queue_get(), which unlinks a node and k_free()s its backing alloc_node — operates under that lock.
Because peek was unsynchronized, a concurrent k_queue_get() on the same queue (on an SMP build, or under preemption/ISR concurrency) can free the node between the moment peek obtains the node pointer and the moment it dereferences it. The peek then reads flag bits and a data pointer out of freed, potentially re-allocated heap memory and returns a stale or dangling pointer to its caller. k_fifo and k_lifo are thin wrappers over k_queue, so this affects buffer queues used throughout the net_buf, Bluetooth, USB, and networking subsystems; the peek operations are also system calls reachable from CONFIG_USERSPACE threads.
The consequences are a use-after-free read that can leak stale heap contents (one pointer word) and, when the returned dangling pointer is subsequently consumed as a live buffer, a dereference that can crash the system or corrupt memory. Exploitation requires winning a small race window with local access (e.g. a userspace process racing k_queue_peek_* against k_queue_get on a shared queue, or two CPUs), so practical impact is bounded and of low severity.
The fix wraps both peek implementations with k_spin_lock/k_spin_unlock on the queue lock, making the read-and-dereference atomic with respect to the concurrent unlink-and-free and bringing peek into line with the rest of the queue's locking discipline.
📍 영향받는 시스템
net_buf, Bluetooth, USB, networking 서브시스템을 사용하는 커널 기반 시스템
⚔️ 공격 벡터
로컬 권한을 가진 사용자가 사용자 공간 스레드를 통해 k_queue_peek 관련 시스템 호출을 실행하여 경합 조건(Race Condition) 유도
🛡️ 대응 방법
z_impl_k_queue_peek_head() 및 z_impl_k_queue_peek_tail() 구현부에 k_spin_lock 및 k_spin_unlock을 적용하여 원자성 보장
🔧 검증 코드
명령어 결과에 k_spin_lock이 포함되지 않은 peek 함수 구현 코드가 출력되면 취약한 버전입니다.
grep -r "z_impl_k_queue_peek_head" kernel/queue.c | grep -v "k_spin_lock"