sk->sk_wq is an RCU pointer: sock_graft() sets it with rcu_assign_pointer() and readers such as unix_write_space() load it with rcu_dereference(), possibly while the socket is being closed. When an AF_UNIX socket is closed while its peer frees skbs charged to it, sock_orphan() clears the pointer with a plain store and KCSAN reports: BUG: KCSAN: data-race in unix_release_sock / unix_write_space read (marked) to 0xffff888137738100 of 8 bytes by task 20918 on cpu 0: unix_write_space+0xa6/0x170 net/unix/af_unix.c:603 sock_wfree+0x282/0x400 net/core/sock.c:2749 unix_wfree+0x1d/0x30 net/unix/af_unix.c:2019 skb_release_head_state+0x27b/0x280 net/core/skbuff.c:1167 skb_release_all net/core/skbuff.c:1187 [inline] __kfree_skb+0x18/0x180 net/core/skbuff.c:1203 sk_skb_reason_drop+0xd6/0x270 net/core/skbuff.c:1242 kfree_skb_reason include/linux/skbuff.h:1330 [inline] unix_release_sock+0x66f/0x790 net/unix/af_unix.c:759 write to 0xffff888137738100 of 8 bytes by task 20919 on cpu 1: sock_orphan include/net/sock.h:2145 [inline] unix_release_sock+0x238/0x790 net/unix/af_unix.c:711 The race has no functional effect. Readers handle a NULL sk_wq (skwq_has_sleeper() and sock_wake_async() check for it), and a reader that still sees the old pointer is safe because the socket_wq is part of the socket inode, which is freed after an RCU grace period. The plain store still lets the compiler tear it, and KCSAN reports it. Clear the pointer with RCU_INIT_POINTER(), which stores it with WRITE_ONCE(), as sk_clone() and sock_init_data_uid() already do. Fixes: 43815482370c ("net: sock_def_readable() and friends RCU conversion") Reported-by: syzbot+c2e012bd497ee37068b0@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=c2e012bd497ee37068b0 Signed-off-by: Jiayi Zhang --- The syzbot report is in syzbot's moderation queue and was not sent to the lists; the Closes: link shows it. Tested in QEMU with a test program that closes both ends of AF_UNIX socket pairs at the same time after queueing messages (KCSAN report filter set to these functions): without the patch 5 of 5 instances reported this race on sk->sk_wq within 10 minutes; with the patch none of 5 did. The net selftests were not run. With the patch, 3 of the 5 instances reported another pair of accesses under the same title: sock_set_flag(sk, SOCK_DEAD) in sock_orphan(), a non-atomic __set_bit() on sk->sk_flags, against the test of SOCK_FASYNC in sk_wake_async_rcu(). syzbot shows that pair in 3 of the 35 crashes of this bug; the other 32 are the sk->sk_wq race fixed here. This patch does not change it. mptcp has the same plain store of sk_wq in net/mptcp/protocol.c; that would be a separate patch. AI tools assisted with the analysis, the code and the changelog. include/net/sock.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/net/sock.h b/include/net/sock.h index 3144efa..669608d 100644 --- a/include/net/sock.h +++ b/include/net/sock.h @@ -2142,7 +2142,7 @@ 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; + RCU_INIT_POINTER(sk->sk_wq, NULL); write_unlock_bh(&sk->sk_callback_lock); } -- 2.53.0