🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
1 messages
1 total messages Started by Jiayuan Chen Thu, 26 Nov 2026 01:33
Re: [PATCH net v2] inet: release newly acquired port on inet_csk_listen_start() hash error
#22843
Author: Jiayuan Chen
Date: Thu, 26 Nov 2026 01:33
79 lines
11243 bytes

On 11/25/26 2:06 AM, Dominik Maier wrote:
> 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>


> ---
> v2:
> - Move port release inside the !err block after ->hash() fails and call
>   inet_put_port() before clearing inet_sport (Kuniyuki Iwashima).
> - Drop the extra Fixes: 8cc3aef0cb19 tag and move the testing note below
>   --- (Kuniyuki Iwashima).
> - v1: https://lore.kernel.org/netdev/20261119192133.2632936-1-dmnk@google.com/
>
> Found with AI-assisted code review; reproduced and tested in QEMU on
> top of net/main.
>  net/ipv4/inet_connection_sock.c | 7 +++++++
>  1 file changed, 7 insertions(+)
>
> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index 9a3b05edb62b..b8df2a05572b 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
> @@ -1369,6 +1369,7 @@ int inet_csk_listen_start(struct sock *sk)
>  {
>  	struct inet_connection_sock *icsk = inet_csk(sk);
>  	struct inet_sock *inet = inet_sk(sk);
> +	bool bound;
>  	int err;
>  
>  	err = inet_ulp_can_listen(sk);
> @@ -1385,6 +1386,7 @@ int inet_csk_listen_start(struct sock *sk)
>  	 * It is OK, because this socket enters to hash table only
>  	 * after validation is complete.
>  	 */
> +	bound = !!icsk->icsk_bind_hash;
>  	inet_sk_state_store(sk, TCP_LISTEN);
>  	err = sk->sk_prot->get_port(sk, inet->inet_num);
>  	if (!err) {
> @@ -1395,6 +1397,11 @@ int inet_csk_listen_start(struct sock *sk)
>  
>  		if (likely(!err))
>  			return 0;
> +
> +		if (!bound) {
> +			inet_put_port(sk);
> +			inet->inet_sport = 0;
> +		}
>  	}
>  
>  	inet_sk_set_state(sk, TCP_CLOSE);
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads