Skip to content

Commit e0c8bcc

Browse files
Eric Dumazetdavem330
authored andcommitted
net: stream: purge sk_error_queue in sk_stream_kill_queues()
Changheon Lee reported TCP socket leaks, with a nice repro. It seems we leak TCP sockets with the following sequence: 1) SOF_TIMESTAMPING_TX_ACK is enabled on the socket. Each ACK will cook an skb put in error queue, from __skb_tstamp_tx(). __skb_tstamp_tx() is using skb_clone(), unless SOF_TIMESTAMPING_OPT_TSONLY was also requested. 2) If the application is also using MSG_ZEROCOPY, then we put in the error queue cloned skbs that had a struct ubuf_info attached to them. Whenever an struct ubuf_info is allocated, sock_zerocopy_alloc() does a sock_hold(). As long as the cloned skbs are still in sk_error_queue, socket refcount is kept elevated. 3) Application closes the socket, while error queue is not empty. Since tcp_close() no longer purges the socket error queue, we might end up with a TCP socket with at least one skb in error queue keeping the socket alive forever. This bug can be (ab)used to consume all kernel memory and freeze the host. We need to purge the error queue, with proper synchronization against concurrent writers. Fixes: 24bcbe1 ("net: stream: don't purge sk_error_queue in sk_stream_kill_queues()") Reported-by: Changheon Lee <[email protected]> Signed-off-by: Eric Dumazet <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent d83b950 commit e0c8bcc

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

net/core/stream.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,12 @@ void sk_stream_kill_queues(struct sock *sk)
196196
/* First the read buffer. */
197197
__skb_queue_purge(&sk->sk_receive_queue);
198198

199+
/* Next, the error queue.
200+
* We need to use queue lock, because other threads might
201+
* add packets to the queue without socket lock being held.
202+
*/
203+
skb_queue_purge(&sk->sk_error_queue);
204+
199205
/* Next, the write queue. */
200206
WARN_ON_ONCE(!skb_queue_empty(&sk->sk_write_queue));
201207

0 commit comments

Comments
 (0)