๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #6875

Re: [PATCH net v2 1/2] inet: reject unhashed sockets during established lookups

#6875
From: Eric Dumazet
Date: Fri, 23 Oct 2026 16:39
208 lines
17428 bytes
Le ven. 23 oct. 2026 ร  07:26, Hyunwoo Kim <imv4bel@gmail.com> a รฉcrit :
>
> TCP established ehash lookups are lockless. The socket caches involved
> use SLAB_TYPESAFE_BY_RCU, so memory can be reused before an RCU reader
> that retained its old address has finished.
>
> After a tuple match, __inet_lookup_established() and
> __inet6_lookup_established() acquire a reference and repeat the tuple
> check. They do not check whether the new object is hashed. If the
> address is reused for a socket with the same tuple, the lookup can
> acquire a reference to the new object and return it while its nulls node
> is unhashed.
>
> A PoC that drives reuse with a SYN cookie request socket reproduced the
> crash. The new request socket is unhashed and has a NULL rsk_listener.
> A lookup that had loaded the address of the old request socket acquired
> the reference published by tcp_get_cookie_sock() and returned the new
> object. tcp_v4_rcv() then read sk_state through the NULL listener.
>
> The resulting crash was:
>
>   BUG: kernel NULL pointer dereference, address: 0000000000000012
>   #PF: supervisor read access in kernel mode
>   #PF: error_code(0x0000) - not-present page
>   ...
>   Oops: Oops: 0000 [#1] SMP NOPTI
>   CPU: 4 UID: 0 PID: 0 Comm: swapper/4 Not tainted 7.3.0-00591-g0e135910deec #1 PREEMPT(lazy)
>   ...
>   RIP: 0010:tcp_v4_rcv+0x44e/0x1280
>   ...
>   R10: 0000000000000001 R11: 00000000024ca8c0 R12: 0000000000000000
>   ...
>   CR2: 0000000000000012 CR3: 0000000103d50004 CR4: 0000000000770ef0
>   ...
>   Call Trace:
>    <IRQ>
>    ? raw_local_deliver+0xae/0x240
>    ip_protocol_deliver_rcu+0x2d/0x180
>    ip_local_deliver_finish+0x84/0x100
>    ip_sublist_rcv_finish+0x5f/0x70
>    ip_sublist_rcv+0x12a/0x1d0
>    ip_list_rcv+0xfd/0x130
>    __netif_receive_skb_list_core+0x223/0x250
>    netif_receive_skb_list_internal+0x1d2/0x2c0
>    ? try_fill_recv+0x39/0x7b0
>    napi_complete_done+0x74/0x170
>    virtnet_poll+0x445/0x600
>    __napi_poll+0x27/0x1e0
>    net_rx_action+0x2fe/0x3b0
>    handle_softirqs+0xc2/0x2c0
>    __irq_exit_rcu+0x74/0xd0
>    common_interrupt+0x7f/0x90
>    </IRQ>
>   ...
>   Kernel panic - not syncing: Fatal exception in interrupt
>
> After taking a reference, check whether the nulls node is unhashed in
> both IPv4 and IPv6 established lookups. If it is unhashed or the tuple
> no longer matches, drop the reference and restart the lookup.
>
> Lookups racing with the TIME_WAIT hashdance, or with a request being
> replaced by its child, now restart and find the new socket instead of
> returning the old one.
>
> Request sockets that were never inserted into ehash can have a nonzero
> refcount, and a stale lookup can hold a transient reference. Therefore,
> when SYN cookie, Fast Open, and request migration paths fail after
> setting a nonzero refcount, release the references held by those paths
> with reqsk_put() instead of freeing the request directly.
>
> Fixes: 079096f103fa ("tcp/dccp: install syn_recv requests into ehash table")
> Cc: stable@vger.kernel.org
> Signed-off-by: Hyunwoo Kim <imv4bel@gmail.com>
> ---
>  net/ipv4/inet_connection_sock.c | 10 +++++++---
>  net/ipv4/inet_hashtables.c      |  5 +++--
>  net/ipv4/syncookies.c           |  2 ++
>  net/ipv4/tcp_input.c            |  4 +++-
>  net/ipv6/inet6_hashtables.c     |  4 +++-
>  5 files changed, 18 insertions(+), 7 deletions(-)
>
> diff --git a/net/ipv4/inet_connection_sock.c b/net/ipv4/inet_connection_sock.c
> index 6a30f1138454..cff02f4c5ac8 100644
> --- a/net/ipv4/inet_connection_sock.c
> +++ b/net/ipv4/inet_connection_sock.c
> @@ -1136,10 +1136,14 @@ static void reqsk_timer_handler(struct timer_list *t)
>          */
>         if (nreq) {
>                 __NET_INC_STATS(net, LINUX_MIB_TCPMIGRATEREQFAILURE);
> +               /* The timer was not armed, drop its reference here. */
> +               reqsk_put(nreq);
>  no_ownership:
>                 reqsk_migrate_reset(nreq);
>                 reqsk_queue_removed(queue, nreq);
> -               __reqsk_free(nreq);
> +               /* Drop the ehash and temporary references. */

Lets change this comment to :
/* Drop the temporary reference and the one reserved for ehash. */

Reviewed-by: Eric Dumazet <edumazet@kernel.org>

> +               reqsk_put(nreq);
> +               reqsk_put(nreq);
>         }
>
>  drop:
> @@ -1447,7 +1451,7 @@ struct sock *inet_csk_complete_hashdance(struct sock *sk, struct sock *child,
>
>                         __NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMIGRATEREQFAILURE);
>                         reqsk_migrate_reset(nreq);
> -                       __reqsk_free(nreq);
> +                       reqsk_put(nreq);
>                 } else if (inet_csk_reqsk_queue_add(sk, req, child)) {
>                         return child;
>                 }
> @@ -1502,7 +1506,7 @@ void inet_csk_listen_stop(struct sock *sk)
>                                         __NET_INC_STATS(sock_net(nsk),
>                                                         LINUX_MIB_TCPMIGRATEREQFAILURE);
>                                         reqsk_migrate_reset(nreq);
> -                                       __reqsk_free(nreq);
> +                                       reqsk_put(nreq);
>                                 }
>                                 rcu_read_unlock();
>
> diff --git a/net/ipv4/inet_hashtables.c b/net/ipv4/inet_hashtables.c
> index ba0faa9ae2bb..58d54965036a 100644
> --- a/net/ipv4/inet_hashtables.c
> +++ b/net/ipv4/inet_hashtables.c
> @@ -552,8 +552,9 @@ struct sock *__inet_lookup_established(const struct net *net,
>                 if (likely(inet_match(net, sk, acookie, ports, dif, sdif))) {
>                         if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
>                                 goto out;
> -                       if (unlikely(!inet_match(net, sk, acookie,
> -                                                ports, dif, sdif))) {
> +                       if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) ||
> +                                    !inet_match(net, sk, acookie, ports, dif,
> +                                                sdif))) {
>                                 sock_gen_put(sk);
>                                 goto begin;
>                         }
> diff --git a/net/ipv4/syncookies.c b/net/ipv4/syncookies.c
> index 73e129768184..644030561537 100644
> --- a/net/ipv4/syncookies.c
> +++ b/net/ipv4/syncookies.c
> @@ -218,6 +218,8 @@ struct sock *tcp_get_cookie_sock(struct sock *sk, struct sk_buff *skb,
>
>                 bh_unlock_sock(child);
>                 sock_put(child);
> +               reqsk_put(req);
> +               return NULL;
>         }
>         __reqsk_free(req);
>
> diff --git a/net/ipv4/tcp_input.c b/net/ipv4/tcp_input.c
> index 99baf14afdfd..3583867487d7 100644
> --- a/net/ipv4/tcp_input.c
> +++ b/net/ipv4/tcp_input.c
> @@ -7777,7 +7777,9 @@ int tcp_conn_request(struct request_sock_ops *rsk_ops,
>                 if (!inet_csk_reqsk_queue_add(sk, req, fastopen_sk)) {
>                         bh_unlock_sock(fastopen_sk);
>                         sock_put(fastopen_sk);
> -                       goto drop_and_free;
> +                       /* tcp_disconnect() already dropped one reqsk ref. */
> +                       reqsk_put(req);
> +                       goto drop;
>                 }
>                 READ_ONCE(sk->sk_data_ready)(sk);
>                 bh_unlock_sock(fastopen_sk);
> diff --git a/net/ipv6/inet6_hashtables.c b/net/ipv6/inet6_hashtables.c
> index b111b51d69fc..aa5fa1dd062f 100644
> --- a/net/ipv6/inet6_hashtables.c
> +++ b/net/ipv6/inet6_hashtables.c
> @@ -108,7 +108,9 @@ struct sock *__inet6_lookup_established(const struct net *net,
>                 if (unlikely(!refcount_inc_not_zero(&sk->sk_refcnt)))
>                         goto out;
>
> -               if (unlikely(!inet6_match(net, sk, saddr, daddr, ports, dif, sdif))) {
> +               if (unlikely(hlist_nulls_unhashed_lockless(&sk->sk_nulls_node) ||
> +                            !inet6_match(net, sk, saddr, daddr, ports, dif,
> +                                         sdif))) {
>                         sock_gen_put(sk);
>                         goto begin;
>                 }
> --
> 2.43.0
>
Technical Details
Message-ID: <CAL4Wiio_6_a3-w9RqtC3Qpe=NXqtgBwqmSiKVbfbuPK60kSN5g@mail.gmail.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351153-linux-kernel-netdev=dewy.ru
References: <20261023052542.3505250-1-imv4bel@gmail.com> <20261023052542.3505250-2-imv4bel@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-351153-linux-kernel-netdev=dewy.ru
From: edumazet@kernel.org (Eric Dumazet)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v2 1/2] inet: reject unhashed sockets during
 established lookups
Date: Fri, 23 Oct 2026 18:39:41 +0200
Organization: Usenet Network
Message-ID: <CAL4Wiio_6_a3-w9RqtC3Qpe=NXqtgBwqmSiKVbfbuPK60kSN5g@mail.gmail.com>
References: <20261023052542.3505250-1-imv4bel@gmail.com> <20261023052542.3505250-2-imv4bel@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Injection-Info: usenet.network;
	logging-data="553362"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:3IO4LP5szmQXpbZryymwqNY6uLk= sha256:wmsr0Gt8qpcB2Ta07/EOfiaARRfC3X+Q5md/gaCTn6U=
	sha1:T64eahMpZBjVHkt9eL42fgJwOtk= sha256:OTuurPCoTkDkwa1n8cPV3Wd5zrnuzk3El66sE3vxLVU=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id A0468111525; Fri, 23 Oct 2026 19:42:40 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.739
X-Spam-Status: No, score=-3.739 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.339, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id pzWtlanJr9JE;
	Fri, 23 Oct 2026 19:42:33 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-351153-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 0B66F111516; Fri, 23 Oct 2026 19:42:25 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id EA37E38228; Fri, 23 Oct 2026 16:39:55 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1792773595;
	b=xT8VlLpr+BZz+WRzT4w6OAyof88mRWqpvTWAda/jmeGfEyJ7o+VUI+vrrTdyhErDJWlz
	 2imQn6bEWjzVUqGAcqqVwVNuJiiQl6wl8Q8Evqcb50MayJn0iehXNjPH/HkUQd0GC0qcN
	 8BGwB4+5waE90Tzk7QmNVq+oKv4jPK0AwcWtJDnmxh0Uab6qQROgu/ZMlz5ANW+hwwfkr
	 hitliZGj4wFZocuHTRJgi4ob3Qxx+JqIvcR8huXLlSZkJgaJjjtv2B081qdOX1KoALwlK
	 mzvq3sa6Stpevya9zJ96a82YvBDE8OriltCsLZG/9EDuTKuN76E9KQBK4pCIaa68R/A==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792773595;
	h=X-Original-To:Received:Received:DKIM-Signature:Received:
	 X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:X-Mailing-List:
	 List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe:
	 List-Owner:Precedence:MIME-Version:References:In-Reply-To:From:Date:
	 X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:Subject:To:Cc:
	 Content-Type:Content-Transfer-Encoding;
	bh=eLoXF0D1za+hi3iGHkuVmbEpDQaUboIZtZF11tK6egI=;
	b=EVsbmbBBakJMdv89D7j2uaqStN/N5rW1sQHwDbm8HSbIZZVOlOPNzVFyVJh2hqIXP7nz
	 8mg0fz+rYsZQvBq0mpBUcwF6G5DBfD4HRdE4Ov2KiHeXNAu830MclNkjy9O6fpz3YMFnp
	 66a3ADmWWfk8Lz5zwv1slGeEB02GoHgoH+HeQcUF5IknCp4J0EGq5BHWMFyW85KbcooIe
	 Di5hxDiVpO/YSuAn7AvbhJ1PA9G6PRrXA8oZADDoadV90bKu1lHs62ERonlFcVaPHuvII
	 PYaE8fcOV9hEhv2nTJUtxZNPXRWaH/TpTB7Wqut3AK5Brc1FDdriOyVGWYvsuipq4xQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LF1HFsFR;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id B021E606A325;
	Fri, 23 Oct 2026 16:39:55 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LF1HFsFR
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1792773594;
	b=p9sh93hcw1/ImGq6hoyYFii823Jpr1tEE8E/2V2GiDnsqhustPWoRh5Q0N+gk3wMUJBh
	 uMiWs1OlaRCJhLsYGgj1REuQQwbq+zfF5q9NS6T0VDfL3pjz93r7/e0ndCqkDPbzaZAAI
	 VtY0/l5UmnBh8tfO2/GQVe+9hCjFgagSWpjVJrTEOwW0X2lyvaaexKxTN+ZXvf2FcZNqI
	 54SYnmAYP/I4kNPpSLk4i6ODbSISisQ42t07TH3naIR34ru63y7RAAdRIYLvIRuC4h/Q1
	 wZiEMOekhHhexrhCZHstjM0G/w+M1OrL8Rp9YvqyQRGtXqBdEw/bx3CyrUkE2rimKTw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1792773594;
	h=Received:DKIM-Signature:Received:X-Forwarded-Encrypted:
	 X-Gm-Message-State:X-Received:MIME-Version:References:In-Reply-To:
	 From:Date:X-Gmail-Original-Message-ID:X-Gm-Features:Message-ID:
	 Subject:To:Cc:Content-Type:Content-Transfer-Encoding;
	bh=eLoXF0D1za+hi3iGHkuVmbEpDQaUboIZtZF11tK6egI=;
	b=fAMVoS4iaX2UOh4d3qLxsQqikIqbNYzXJveQTYtVlt+N3eM2n55Tsma0ahTkJDqk905k
	 G1mPz9W9V9MS6G8G0YcTJLrQgHlWrpCWViz3Ayc4+NaobK6LQpN1oxleNPKQy5z9ZNOk1
	 fa7l4Fp1SI2TUEZy42tNyH7NYKBgZNhjheVBYIJRiPviRTexVCmFnIhnqQSWOTWW2/KMl
	 ApVWn9IPWhiNFhoOM8jN6yrIKGXuLIY47uBizeuB3uIYdKFf4TOEVEbI3KFFw/+qXSFqR
	 cruE6FUiozyW77aONnstc78s0cEZaah2z+NfrmpXACAv/PfxoA/ilEMp+Cfl3Ce1vDg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=LF1HFsFR;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8AF00606A31B
	for <netdev@vger.kernel.org>; Fri, 23 Oct 2026 16:39:54 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1C1611F031E2
	for <netdev@vger.kernel.org>; Fri, 23 Oct 2026 16:39:54 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1792773594;
	bh=eLoXF0D1za+hi3iGHkuVmbEpDQaUboIZtZF11tK6egI=;
	h=References:In-Reply-To:From:Date:Subject:To:Cc;
	b=LF1HFsFRgVR247xGn5yUnHwE7uAVqBmIEP0ehd03WjbzyKMHRbR7nUibe7KnT0+0u
	 k6neao6LWEzpGPDlY92HwlAYOZDtR/jo9mXgpkgKFWi8R1zxibvxkEef1R07zndnJI
	 gDEyIyCenitIQR3q7E4jntodUP9Ix/Z3t45+PSN0Lay1HcHh3PcjHrGY3Hz7wJEgzr
	 1pKIwxBz8mlaNBZ6bql63JOLjkuI1CbiUSOEXqsFzFzP3Tu+R9mKtERMmcH64dKvbJ
	 0flHtdX9GhlaldIbj6XQ1aEUcSvXE+IQWxuaPfwD6516jrep/WbuZArZNZff9ltSdh
	 VuSDkUpuAjNEw==
X-Received: by mail-oo1-f49.google.com with SMTP id 006d021491bc7-70059e4dab6so715635eaf.3
        for <netdev@vger.kernel.org>; Fri, 23 Oct 2026 09:39:54 -0700 (PDT)
X-Forwarded-Encrypted: i=1; AKwUvBxMWJ/NtMB07KmkyQXTPiA3Ow/ljHSyOjghWoXhGscucPUFhH7TNfcBQXcgjZS7TQIaYJNZ654=@vger.kernel.org
X-Gm-Message-State: AFq9FYIJXLHxvhy/C7pKHF4UViEeFD4bEqdsN51prvlivSP+Pi1RuHsy
	sYyoakuDi5YWJeT6fvqv62L3WpUMOmyn4AvQGn2BMerkLueet2ZdRpzfCXZpk7nAtvGWo3oKFzX
	xvkg9dLXMMrkQGbNXqAmOr0K5vTEynaE=
X-Received: by 2002:a05:6820:1612:b0:707:27c7:86d9 with SMTP id
 006d021491bc7-70b88ab80e7mr1916749eaf.48.1792773593385; Fri, 23 Oct 2026
 09:39:53 -0700 (PDT)
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list
X-In-Reply-To: <20261023052542.3505250-2-imv4bel@gmail.com>
X-Gmail-Original-Message-ID: <CAL4Wiio_6_a3-w9RqtC3Qpe=NXqtgBwqmSiKVbfbuPK60kSN5g@mail.gmail.com>
X-Gm-Features: AclHuK8_oAEk4nDhnB9KV9J3Amo3OdhyF8APHYjDQSj_Ehe4KSD5B3ictQTU8Vs
X-Cc: ncardwell@google.com, kuniyu@google.com, davem@davemloft.net, 
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org, martin.lau@linux.dev, 
	john.fastabend@gmail.com, weiwan@google.com, sd@queasysnail.net, 
	jiayuan.chen@linux.dev, netdev@vger.kernel.org, stable@vger.kernel.org