Technical Root Cause Analysis
1. Functions, Structs, and Fields Involved
2. Root Cause & Concurrency Mechanism
In struct sock, the wait queue pointer sk_wq is declared with the __rcu attribute:
union {
struct socket_wq __rcu *sk_wq;
/* private: */
struct socket_wq *sk_wq_raw;
/* public: */
};
Socket wakeup callbacks (such as unix_write_space(), sock_def_write_space(), sock_def_readable(), and sock_def_wakeup()) access sk->sk_wq locklessly under rcu_read_lock() using rcu_dereference(sk->sk_wq) without acquiring sk->sk_callback_lock.
In sock_orphan() (include/net/sock.h):
static inline void sock_orphan(struct sock *sk)
{
write_lock_bh(&sk->sk_callback_lock);
sock_set_flag(sk, SOCK_DEAD);
sk_set_socket(sk, NULL);
sk->sk_wq = NULL;
write_unlock_bh(&sk->sk_callback_lock);
}
sock_orphan() updates sk->sk_wq using a plain, unmarked C store (sk->sk_wq = NULL;). While sk_set_socket() immediately preceding it explicitly uses WRITE_ONCE(), and other socket initialization/grafting helpers (sock_graft(), sock_init_data_uid(), sk_clone()) use rcu_assign_pointer() or RCU_INIT_POINTER(), sock_orphan() fails to use RCU_INIT_POINTER(sk->sk_wq, NULL).
Holding write_lock_bh(&sk->sk_callback_lock) does not protect against concurrent readers because RCU readers of sk->sk_wq do not take sk_callback_lock. Consequently, under the Linux Kernel Memory Model (LKMM), an unmarked write races with a marked read (rcu_dereference()).
3. Trigger Flow
-
CPU 0 (Task 8407):
- Closes an AF_UNIX socket (Socket B) via
sys_close() -> sock_close() -> __sock_release() -> unix_release() -> unix_release_sock().
- At
af_unix.c:678, it calls sock_orphan(sk) on Socket B.
- Inside
sock_orphan(), it performs the plain write sk->sk_wq = NULL; at sock.h:2145.
-
CPU 1 (Task 8405):
- Closes peer AF_UNIX socket (Socket A) in
unix_release_sock().
- While purging remaining unread buffers from Socket A's
sk_receive_queue at af_unix.c:726, it calls kfree_skb_reason(skb, SKB_DROP_REASON_SOCKET_CLOSE).
- The queued
skb was sent by Socket B, so dropping the packet triggers its destructor unix_wfree() -> sock_wfree() on Socket B.
sock_wfree() calls sk_write_space(sk) -> unix_write_space(sk) on Socket B.
- In
unix_write_space(), line 603 dereferences sk->sk_wq with wq = rcu_dereference(sk->sk_wq);.
-
Sanitizer Detection:
KCSAN detects a concurrent marked read (rcu_dereference() -> READ_ONCE()) on CPU 1 and an unmarked write (sk->sk_wq = NULL) on CPU 0 to the exact same pointer address (sk->sk_wq).
4. Remediation
In sock_orphan() (include/net/sock.h), sk->sk_wq = NULL; should be replaced with:
RCU_INIT_POINTER(sk->sk_wq, NULL);
This applies WRITE_ONCE() to the assignment, preventing compiler store-tearing and ensuring consistency with RCU reader expectations and LKMM concurrency rules.