inet: release newly acquired port on inet_csk_listen_start() hash error
When inet_csk_listen_start() is called on a socket that has not been
explicitly bound, sk->sk_prot->get_port() allocates an ephemeral port
and links the socket into bhash and bhash2 without setting
SOCK_BINDPORT_LOCK. If sk->sk_prot->hash() then fails (for example, when
a prior failed connect() left icsk->unhashed_state set to TCP_SYN_SENT
so inet_hash() returns -EINVAL), inet_csk_listen_start() resets sk_state
to TCP_CLOSE and returns the error without releasing the newly acquired
port.
Because inet->inet_num remains non-zero while SOCK_BINDPORT_LOCK is
unset, a subsequent Fast Open connect on a dual-stack AF_INET6 socket
can update sk_v6_rcv_saddr via tcp_v4_connect() without rehashing the
bhash2 entry. Disconnecting with AF_UNSPEC then invokes
__inet_put_port(), which locks the new address's bhash2 bucket while
unlinking and freeing tb2 from the original bhash2 bucket without
holding its spinlock.
Record whether icsk->icsk_bind_hash was already populated before
get_port(). If sk->sk_prot->hash() fails on a newly bound socket, call
inet_put_port(sk) and clear inet->inet_sport before transitioning back
to TCP_CLOSE. Explicitly bound sockets retain their port.
Fixes: 28044fc1d495 ("net: Add a bhash2 table hashed by port and address")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
Reviewed-by: Jiayuan Chen <jiayuan.chen@linux.dev>
Reviewed-by: Kuniyuki Iwashima <kuniyu@google.com>
Link: https://patch.msgid.link/20261124180600.71138-1-dmnk@google.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 file changed