]> git.baikalelectronics.ru Git - kernel.git/commit
net: stream: purge sk_error_queue in sk_stream_kill_queues()
authorEric Dumazet <edumazet@google.com>
Fri, 16 Dec 2022 16:29:17 +0000 (16:29 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 18 Jan 2023 10:41:33 +0000 (11:41 +0100)
commitce5f796a0e2297b52b67fbb06e47ceadd242702c
tree1b670cf2a8a37dcaeb3610f9255d2888ce3bea9d
parent8fa3c55cc8e185ad99c8060e5aab78e502ebdbf1
net: stream: purge sk_error_queue in sk_stream_kill_queues()

[ Upstream commit 6ea88d67f035e37152a5c2317a665d2b0c5c2045 ]

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: 2d3e867815a5 ("net: stream: don't purge sk_error_queue in sk_stream_kill_queues()")
Reported-by: Changheon Lee <darklight2357@icloud.com>
Signed-off-by: Eric Dumazet <edumazet@google.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
net/core/stream.c