Skip to content

Commit 26bfb8b

Browse files
q2venPaolo Abeni
authored andcommitted
af_unix: Set sk->sk_state under unix_state_lock() for truly disconencted peer.
When a SOCK_DGRAM socket connect()s to another socket, the both sockets' sk->sk_state are changed to TCP_ESTABLISHED so that we can register them to BPF SOCKMAP. When the socket disconnects from the peer by connect(AF_UNSPEC), the state is set back to TCP_CLOSE. Then, the peer's state is also set to TCP_CLOSE, but the update is done locklessly and unconditionally. Let's say socket A connect()ed to B, B connect()ed to C, and A disconnects from B. After the first two connect()s, all three sockets' sk->sk_state are TCP_ESTABLISHED: $ ss -xa Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess u_dgr ESTAB 0 0 @A 641 * 642 u_dgr ESTAB 0 0 @b 642 * 643 u_dgr ESTAB 0 0 @C 643 * 0 And after the disconnect, B's state is TCP_CLOSE even though it's still connected to C and C's state is TCP_ESTABLISHED. $ ss -xa Netid State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess u_dgr UNCONN 0 0 @A 641 * 0 u_dgr UNCONN 0 0 @b 642 * 643 u_dgr ESTAB 0 0 @C 643 * 0 In this case, we cannot register B to SOCKMAP. So, when a socket disconnects from the peer, we should not set TCP_CLOSE to the peer if the peer is connected to yet another socket, and this must be done under unix_state_lock(). Note that we use WRITE_ONCE() for sk->sk_state as there are many lockless readers. These data-races will be fixed in the following patches. Fixes: 83301b5 ("af_unix: Set TCP_ESTABLISHED for datagram sockets too") Signed-off-by: Kuniyuki Iwashima <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent b0c9a26 commit 26bfb8b

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

net/unix/af_unix.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,6 @@ static void unix_dgram_disconnected(struct sock *sk, struct sock *other)
570570
sk_error_report(other);
571571
}
572572
}
573-
other->sk_state = TCP_CLOSE;
574573
}
575574

576575
static void unix_sock_destructor(struct sock *sk)
@@ -1424,8 +1423,15 @@ static int unix_dgram_connect(struct socket *sock, struct sockaddr *addr,
14241423

14251424
unix_state_double_unlock(sk, other);
14261425

1427-
if (other != old_peer)
1426+
if (other != old_peer) {
14281427
unix_dgram_disconnected(sk, old_peer);
1428+
1429+
unix_state_lock(old_peer);
1430+
if (!unix_peer(old_peer))
1431+
WRITE_ONCE(old_peer->sk_state, TCP_CLOSE);
1432+
unix_state_unlock(old_peer);
1433+
}
1434+
14291435
sock_put(old_peer);
14301436
} else {
14311437
unix_peer(sk) = other;

0 commit comments

Comments
 (0)