tcp: Clear sk_send_head after purging the write queue

Denis Andzakovic discovered a potential use-after-free in older kernel
versions, using syzkaller.  tcp_write_queue_purge() frees all skbs in
the TCP write queue and can leave sk->sk_send_head pointing to freed
memory.  tcp_disconnect() clears that pointer after calling
tcp_write_queue_purge(), but tcp_connect() does not.  It is
(surprisingly) possible to add to the write queue between
disconnection and reconnection, so this needs to be done in both

This bug was introduced by backports of commit 7f582b248d0a ("tcp:
purge write queue in tcp_connect_init()") and does not exist upstream
because of earlier changes in commit 75c119afe14f ("tcp: implement
rb-tree based retransmit queue").  The latter is a major change that's
not suitable for stable.

Reported-by: Denis Andzakovic <>
Bisected-by: Salvatore Bonaccorso <>
Fixes: 7f582b248d0a ("tcp: purge write queue in tcp_connect_init()")
Cc: <> # before 4.15
Cc: Eric Dumazet <>
Signed-off-by: Ben Hutchings <>
Signed-off-by: Greg Kroah-Hartman <>
Bug: 143009752
(cherry picked from commit e99e7745d03fc50ba7c5b7c91c17294fee2d5991)
Signed-off-by: Matthias Maennich <>

Change-Id: Ie325f18e7fe1c327a45e149034b90d2e74922263
1 file changed