Skip to content

Commit 95d59a6

Browse files
q2venSasha Levin
authored andcommitted
af_unix: Annodate data-races around sk->sk_state for writers.
[ Upstream commit 942238f ] sk->sk_state is changed under unix_state_lock(), but it's read locklessly in many places. This patch adds WRITE_ONCE() on the writer side. We will add READ_ONCE() to the lockless readers in the following patches. Fixes: 83301b5 ("af_unix: Set TCP_ESTABLISHED for datagram sockets too") Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: Paolo Abeni <pabeni@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent c160486 commit 95d59a6

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

net/unix/af_unix.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static void unix_release_sock(struct sock *sk, int embrion)
604604
u->path.dentry = NULL;
605605
u->path.mnt = NULL;
606606
state = sk->sk_state;
607-
sk->sk_state = TCP_CLOSE;
607+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
608608

609609
skpair = unix_peer(sk);
610610
unix_peer(sk) = NULL;
@@ -726,7 +726,8 @@ static int unix_listen(struct socket *sock, int backlog)
726726
if (backlog > sk->sk_max_ack_backlog)
727727
wake_up_interruptible_all(&u->peer_wait);
728728
sk->sk_max_ack_backlog = backlog;
729-
sk->sk_state = TCP_LISTEN;
729+
WRITE_ONCE(sk->sk_state, TCP_LISTEN);
730+
730731
/* set credentials so connect can copy them */
731732
init_peercred(sk);
732733
err = 0;
@@ -1389,7 +1390,8 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
13891390
if (err)
13901391
goto out_unlock;
13911392

1392-
sk->sk_state = other->sk_state = TCP_ESTABLISHED;
1393+
WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
1394+
WRITE_ONCE(other->sk_state, TCP_ESTABLISHED);
13931395
} else {
13941396
/*
13951397
* 1003.1g breaking connected state with AF_UNSPEC
@@ -1406,7 +1408,7 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
14061408

14071409
unix_peer(sk) = other;
14081410
if (!other)
1409-
sk->sk_state = TCP_CLOSE;
1411+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
14101412
unix_dgram_peer_wake_disconnect_wakeup(sk, old_peer);
14111413

14121414
unix_state_double_unlock(sk, other);
@@ -1620,7 +1622,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
16201622
copy_peercred(sk, other);
16211623

16221624
sock->state = SS_CONNECTED;
1623-
sk->sk_state = TCP_ESTABLISHED;
1625+
WRITE_ONCE(sk->sk_state, TCP_ESTABLISHED);
16241626
sock_hold(newsk);
16251627

16261628
smp_mb__after_atomic(); /* sock_hold() does an atomic_inc() */
@@ -2015,7 +2017,7 @@ static int unix_dgram_sendmsg(struct socket *sock, struct msghdr *msg,
20152017
unix_peer(sk) = NULL;
20162018
unix_dgram_peer_wake_disconnect_wakeup(sk, other);
20172019

2018-
sk->sk_state = TCP_CLOSE;
2020+
WRITE_ONCE(sk->sk_state, TCP_CLOSE);
20192021
unix_state_unlock(sk);
20202022

20212023
unix_dgram_disconnected(sk, other);

0 commit comments

Comments
 (0)