inet: release newly acquired port on inet_csk_listen_start() hash error
Subsystems (3)
PATCH 1/1 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(). On error, after transitioning back to TCP_CLOSE and before purging the Fast Open RST queue, clear inet->inet_sport and call inet_put_port(sk) if get_port() newly bound the socket. Sockets that were explicitly bound before listen() retain their user-bound port.
This issue was discovered with an AI-assisted code review tool (ARTIST) and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without kernel modifications or error injection; a standalone reproducer is available upon maintainers' request. An earlier revision of this fix was verified against the reproducer in QEMU/KVM; this version, rebased onto commit 342e62fd34c2 ("tcp: purge Fast Open RST queue when listen() fails"), was checked with checkpatch.pl --strict.
--- net/ipv4/inet_connection_sock.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/ipv4/inet_connection_sock.c +6 -0
@@ -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) {
@@ -1398,6 +1400,10 @@ int inet_csk_listen_start(struct sock *sk) }inet_sk_set_state (sk,TCP_CLOSE ); +if (!bound && icsk->icsk_bind_hash) { + inet->inet_sport =0 ; +inet_put_port (sk); + }/* A Fast Open child accepted before a previous shutdown() of this * listener might have queued its request in rskq_rst_head while
Signature · 1 line
2.56.1.809.ga0b61035b5-goog
Kuniyuki Iwashima
reviewerQuoted 23 lines · Dominik Maier
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 settingSOCK_BINDPORT_LOCK. Ifsk->sk_prot->hash()then fails (for example, when a prior failedconnect()lefticsk->unhashed_stateset toTCP_SYN_SENTsoinet_hash()returns-EINVAL),inet_csk_listen_start()resetssk_statetoTCP_CLOSEand returns the error without releasing the newly acquired port.Because
inet->inet_numremains non-zero whileSOCK_BINDPORT_LOCKis unset, a subsequent Fast Open connect on a dual-stackAF_INET6socket can updatesk_v6_rcv_saddrviatcp_v4_connect()without rehashing the bhash2 entry. Disconnecting withAF_UNSPECthen 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_hashwas already populated beforeget_port(). On error, after transitioning back toTCP_CLOSEand before purging the Fast Open RST queue, clearinet->inet_sportand callinet_put_port(sk)ifget_port()newly bound the socket. Sockets that were explicitly bound beforelisten()retain their user-bound port.
Quoted 7 lines
This issue was discovered with an AI-assisted code review tool (ARTIST) and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without kernel modifications or error injection; a standalone reproducer is available upon maintainers' request. An earlier revision of this fix was verified against the reproducer in QEMU/KVM; this version, rebased onto commit
342e62fd34c2("tcp: purge Fast Open RST queue whenlisten()fails"), was checked with checkpatch.pl --strict.
Please move the last paragraph under ---.
Quoted 2 lines
Fixes 8cc3aef0cb19 ("tcp: Do not allow buggy transitions between ehash and lhash2.")
This is not needed.
Quoted 28 lines
Fixes 28044fc1d495 ("net: Add a bhash2 table hashed by port and address")Signed-off-by Dominik Maier <[email protected]>--- net/ipv4/inet_connection_sock.c | 6 ++++++ 1 file changed, 6 insertions(+)net/ipv4/inet_connection_sock.c +2
-0@@ -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) {
net/ipv4/inet_connection_sock.c int inet_csk_listen_start(struct sock *sk) · line 1406
Quoted 7 lines
net/ipv4/inet_connection_sock.c +4
-0@@ -1398,6 +1400,10 @@ int inet_csk_listen_start(struct sock *sk) }inet_sk_set_state (sk,TCP_CLOSE ); +if (!bound && icsk->icsk_bind_hash) { + inet->inet_sport =0 ; +inet_put_port (sk); + }
Please move this to the block above after ->hash() failure and call inet_put_port() before clearing inet_sport.
---
pw-bot: cr