Skip to content

Commit a9bf9c7

Browse files
q2venPaolo Abeni
authored andcommitted
af_unix: Annotate data-race of sk->sk_state in unix_stream_connect().
As small optimisation, unix_stream_connect() prefetches the client's sk->sk_state without unix_state_lock() and checks if it's TCP_CLOSE. Later, sk->sk_state is checked again under unix_state_lock(). Let's use READ_ONCE() for the first check and TCP_CLOSE directly for the second check. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent eb0718f commit a9bf9c7

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

net/unix/af_unix.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,7 +1481,6 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
14811481
struct sk_buff *skb = NULL;
14821482
long timeo;
14831483
int err;
1484-
int st;
14851484

14861485
err = unix_validate_addr(sunaddr, addr_len);
14871486
if (err)
@@ -1571,9 +1570,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
15711570
15721571
Well, and we have to recheck the state after socket locked.
15731572
*/
1574-
st = sk->sk_state;
1575-
1576-
switch (st) {
1573+
switch (READ_ONCE(sk->sk_state)) {
15771574
case TCP_CLOSE:
15781575
/* This is ok... continue with connect */
15791576
break;
@@ -1588,7 +1585,7 @@ static int unix_stream_connect(struct socket *sock, struct sockaddr *uaddr,
15881585

15891586
unix_state_lock_nested(sk, U_LOCK_SECOND);
15901587

1591-
if (sk->sk_state != st) {
1588+
if (sk->sk_state != TCP_CLOSE) {
15921589
unix_state_unlock(sk);
15931590
unix_state_unlock(other);
15941591
sock_put(other);

0 commit comments

Comments
 (0)