Skip to content

Commit e1d09c2

Browse files
q2venkuba-moo
authored andcommitted
af_unix: Fix data races around sk->sk_shutdown.
KCSAN found a data race around sk->sk_shutdown where unix_release_sock() and unix_shutdown() update it under unix_state_lock(), OTOH unix_poll() and unix_dgram_poll() read it locklessly. We need to annotate the writes and reads with WRITE_ONCE() and READ_ONCE(). BUG: KCSAN: data-race in unix_poll / unix_release_sock write to 0xffff88800d0f8aec of 1 bytes by task 264 on cpu 0: unix_release_sock+0x75c/0x910 net/unix/af_unix.c:631 unix_release+0x59/0x80 net/unix/af_unix.c:1042 __sock_release+0x7d/0x170 net/socket.c:653 sock_close+0x19/0x30 net/socket.c:1397 __fput+0x179/0x5e0 fs/file_table.c:321 ____fput+0x15/0x20 fs/file_table.c:349 task_work_run+0x116/0x1a0 kernel/task_work.c:179 resume_user_mode_work include/linux/resume_user_mode.h:49 [inline] exit_to_user_mode_loop kernel/entry/common.c:171 [inline] exit_to_user_mode_prepare+0x174/0x180 kernel/entry/common.c:204 __syscall_exit_to_user_mode_work kernel/entry/common.c:286 [inline] syscall_exit_to_user_mode+0x1a/0x30 kernel/entry/common.c:297 do_syscall_64+0x4b/0x90 arch/x86/entry/common.c:86 entry_SYSCALL_64_after_hwframe+0x72/0xdc read to 0xffff88800d0f8aec of 1 bytes by task 222 on cpu 1: unix_poll+0xa3/0x2a0 net/unix/af_unix.c:3170 sock_poll+0xcf/0x2b0 net/socket.c:1385 vfs_poll include/linux/poll.h:88 [inline] ep_item_poll.isra.0+0x78/0xc0 fs/eventpoll.c:855 ep_send_events fs/eventpoll.c:1694 [inline] ep_poll fs/eventpoll.c:1823 [inline] do_epoll_wait+0x6c4/0xea0 fs/eventpoll.c:2258 __do_sys_epoll_wait fs/eventpoll.c:2270 [inline] __se_sys_epoll_wait fs/eventpoll.c:2265 [inline] __x64_sys_epoll_wait+0xcc/0x190 fs/eventpoll.c:2265 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x3b/0x90 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x72/0xdc value changed: 0x00 -> 0x03 Reported by Kernel Concurrency Sanitizer on: CPU: 1 PID: 222 Comm: dbus-broker Not tainted 6.3.0-rc7-02330-gca6270c12e20 #2 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 Fixes: 3c73419 ("af_unix: fix 'poll for write'/ connected DGRAM sockets") Fixes: 1da177e ("Linux-2.6.12-rc2") Reported-by: syzbot <[email protected]> Signed-off-by: Kuniyuki Iwashima <[email protected]> Reviewed-by: Eric Dumazet <[email protected]> Reviewed-by: Michal Kubiak <[email protected]> Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 679ed00 commit e1d09c2

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

net/unix/af_unix.c

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
603603
/* Clear state */
604604
unix_state_lock(sk);
605605
sock_orphan(sk);
606-
sk->sk_shutdown = SHUTDOWN_MASK;
606+
WRITE_ONCE(sk->sk_shutdown, SHUTDOWN_MASK);
607607
path = u->path;
608608
u->path.dentry = NULL;
609609
u->path.mnt = NULL;
@@ -628,7 +628,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
628628
if (sk->sk_type == SOCK_STREAM || sk->sk_type == SOCK_SEQPACKET) {
629629
unix_state_lock(skpair);
630630
/* No more writes */
631-
skpair->sk_shutdown = SHUTDOWN_MASK;
631+
WRITE_ONCE(skpair->sk_shutdown, SHUTDOWN_MASK);
632632
if (!skb_queue_empty(&sk->sk_receive_queue) || embrion)
633633
WRITE_ONCE(skpair->sk_err, ECONNRESET);
634634
unix_state_unlock(skpair);
@@ -3008,7 +3008,7 @@ static int unix_shutdown(struct socket *sock, int mode)
30083008
++mode;
30093009

30103010
unix_state_lock(sk);
3011-
sk->sk_shutdown |= mode;
3011+
WRITE_ONCE(sk->sk_shutdown, sk->sk_shutdown | mode);
30123012
other = unix_peer(sk);
30133013
if (other)
30143014
sock_hold(other);
@@ -3028,7 +3028,7 @@ static int unix_shutdown(struct socket *sock, int mode)
30283028
if (mode&SEND_SHUTDOWN)
30293029
peer_mode |= RCV_SHUTDOWN;
30303030
unix_state_lock(other);
3031-
other->sk_shutdown |= peer_mode;
3031+
WRITE_ONCE(other->sk_shutdown, other->sk_shutdown | peer_mode);
30323032
unix_state_unlock(other);
30333033
other->sk_state_change(other);
30343034
if (peer_mode == SHUTDOWN_MASK)
@@ -3160,16 +3160,18 @@ static __poll_t unix_poll(struct file *file, struct socket *sock, poll_table *wa
31603160
{
31613161
struct sock *sk = sock->sk;
31623162
__poll_t mask;
3163+
u8 shutdown;
31633164

31643165
sock_poll_wait(file, sock, wait);
31653166
mask = 0;
3167+
shutdown = READ_ONCE(sk->sk_shutdown);
31663168

31673169
/* exceptional events? */
31683170
if (READ_ONCE(sk->sk_err))
31693171
mask |= EPOLLERR;
3170-
if (sk->sk_shutdown == SHUTDOWN_MASK)
3172+
if (shutdown == SHUTDOWN_MASK)
31713173
mask |= EPOLLHUP;
3172-
if (sk->sk_shutdown & RCV_SHUTDOWN)
3174+
if (shutdown & RCV_SHUTDOWN)
31733175
mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
31743176

31753177
/* readable? */
@@ -3203,19 +3205,21 @@ static __poll_t unix_dgram_poll(struct file *file, struct socket *sock,
32033205
struct sock *sk = sock->sk, *other;
32043206
unsigned int writable;
32053207
__poll_t mask;
3208+
u8 shutdown;
32063209

32073210
sock_poll_wait(file, sock, wait);
32083211
mask = 0;
3212+
shutdown = READ_ONCE(sk->sk_shutdown);
32093213

32103214
/* exceptional events? */
32113215
if (READ_ONCE(sk->sk_err) ||
32123216
!skb_queue_empty_lockless(&sk->sk_error_queue))
32133217
mask |= EPOLLERR |
32143218
(sock_flag(sk, SOCK_SELECT_ERR_QUEUE) ? EPOLLPRI : 0);
32153219

3216-
if (sk->sk_shutdown & RCV_SHUTDOWN)
3220+
if (shutdown & RCV_SHUTDOWN)
32173221
mask |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
3218-
if (sk->sk_shutdown == SHUTDOWN_MASK)
3222+
if (shutdown == SHUTDOWN_MASK)
32193223
mask |= EPOLLHUP;
32203224

32213225
/* readable? */

0 commit comments

Comments
 (0)