Article View: linux.kernel.netdev
Article #24589Re: [PATCH net] netfilter: nf_queue: do not queue BPF syncookie request sockets
From: Pablo Neira Ayuso
Date: Mon, 30 Nov 2026 11:48
Date: Mon, 30 Nov 2026 11:48
85 lines
12334 bytes
12334 bytes
On Sun, Nov 29, 2026 at 08:12:29AM -0800, Weiming Shi wrote:
> bpf_sk_assign_tcp_reqsk() allocates a request socket with
> inet_reqsk_alloc(ops, sk, false), stores the listener in
> req->rsk_listener without taking a reference, and attaches it to the
> skb with skb->destructor = sock_pfree. Because neither the request
> socket (rsk_refcnt == 0) nor req->rsk_listener is refcounted, the
> listener pointer is only valid within the ingress RCU read-side
> critical section.
>
> __nf_queue() lets the skb outlive that critical section while waiting
> for a userspace verdict. Its prefetched-socket check only handles
> !sk_is_refcounted(sk), which is false for TCP_NEW_SYN_RECV because
> sk_fullsock() is false for request sockets, and
> nf_queue_entry_get_refs() only pins state->sk (NULL at prerouting).
> If the listener is closed and its RCU grace period elapses while the
> packet is queued, reinjection returns the freed req->rsk_listener from
> skb_steal_sock() into inet_steal_sock() and tcp_v[46]_rcv():
>
> BUG: KASAN: slab-use-after-free in tcp_v4_rcv (include/net/sock.h:2295
> include/net/inet_hashtables.h:458 include/net/inet_hashtables.h:496
> net/ipv4/tcp_ipv4.c:2121)
> Read of size 1 at addr ffff888025542812 by task exploit/194
> Call Trace:
> tcp_v4_rcv (net/ipv4/tcp_ipv4.c:2121)
> ip_protocol_deliver_rcu (net/ipv4/ip_input.c:207)
> ip_local_deliver_finish (net/ipv4/ip_input.c:241)
> ip_local_deliver (net/ipv4/ip_input.c:262)
> ip_rcv_finish (net/ipv4/ip_input.c:492)
> nfqnl_reinject (net/netfilter/nfnetlink_queue.c:544)
> nfqnl_recv_verdict (net/netfilter/nfnetlink_queue.c:1905)
> ...
> __x64_sys_sendto (net/socket.c:2286)
>
> Reject prefetched unrefcounted TCP_NEW_SYN_RECV request sockets in
> __nf_queue() with -ENOTCONN before the !sk_is_refcounted() check so
> nf_queue() drops the skb. Calling skb_orphan() instead would discard
> the validated request socket and cause tcp_v[46]_rcv() to fail
> cookie_tcp_check() and send a spurious RST on reinjection. Matching
> on !refcount_read(&sk->sk_refcnt) instead of inet_reqsk(sk)->syncookie
> also avoids a data race against reqsk_timer_handler() updating
> num_timeout in the same bitfield byte on hashed request sockets.
Cc: Nikolay.
I have seen working on this. I am not sure if this will require more
checks in more spots (and more follow up incremental patches), which
would be better to avoid.
> Fixes: e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.")
> Reported-by: <co+685321905e91e206@bugs.sh>
> Signed-off-by: Weiming Shi <bestswngs@gmail.com>
> Assisted-by: LLM
> ---
> Note: this conflicts with "[PATCH RFC bpf-next 1/4] net: track references
> owned by prefetched socket assignments"
> (20261124151244.1423343-2-razor@blackwall.org), which rewrites the
> skb_sk_is_prefetched() block in __nf_queue() (patch 2/4 of that series,
> 20261124151244.1423343-3-razor@blackwall.org, addresses the sibling
> skb_do_redirect() escape). This check needs to stay outside the
> !sk_is_refcounted(skb->sk) branch. If pinning req->rsk_listener on
> struct nf_queue_entry is preferred over dropping these packets, happy
> to send a v2 with that approach instead.
>
> net/netfilter/nf_queue.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/net/netfilter/nf_queue.c b/net/netfilter/nf_queue.c
> index 166d05e994ac..ee9d2ee49855 100644
> --- a/net/netfilter/nf_queue.c
> +++ b/net/netfilter/nf_queue.c
> @@ -198,6 +198,10 @@ static int __nf_queue(struct sk_buff *skb, const struct nf_hook_state *state,
> if (skb_sk_is_prefetched(skb)) {
> struct sock *sk = skb->sk;
>
> + if (READ_ONCE(sk->sk_state) == TCP_NEW_SYN_RECV &&
> + !refcount_read(&sk->sk_refcnt))
> + return -ENOTCONN;
> +
> if (!sk_is_refcounted(sk)) {
> if (!refcount_inc_not_zero(&sk->sk_refcnt))
> return -ENOTCONN;
> --
> 2.56.1
>
Technical Details
Message-ID:
<aw1ieNBG88kMLaob@chamomile>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374145-linux-kernel-netdev=dewy.ruReferences:
<20261129161228.4185092-2-bestswngs@gmail.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374145-linux-kernel-netdev=dewy.ru From: pablo@netfilter.org (Pablo Neira Ayuso) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH net] netfilter: nf_queue: do not queue BPF syncookie request sockets Date: Mon, 30 Nov 2026 12:48:08 +0100 Organization: Usenet Network Message-ID: <aw1ieNBG88kMLaob@chamomile> References: <20261129161228.4185092-2-bestswngs@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Injection-Info: usenet.network; logging-data="1416503"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:u8OU+6GwmR/xo/Cl8zFQ7YWEQoA= sha256:yQby16B9Dk4Bf9mDCZ21wdRNpUeKkuJZgIGIIwYvexI= sha1:ToP5XmnBFamlOM8c86TJ+eoAdhM= sha256:xLv+uYScEGipcokpHsv2/aXg3U0YRVAWzV/RQAaglE8= Content-Disposition: inline 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 526C0125FDE; Mon, 30 Nov 2026 14:48:39 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -3.051 X-Spam-Status: No, score=-3.051 tagged_above=-9999 required=6.31 tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, 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=netfilter.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 PWyWHxG4H8Aj; Mon, 30 Nov 2026 14:48:36 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-374145-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=netfilter.org X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 2AB5E125FDD; Mon, 30 Nov 2026 14:48:28 +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 AF2A72949B; Mon, 30 Nov 2026 11:48:25 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=fail; t=1796039305; b=kJK83JZCQk6xH5BXbL2b4brOF15Mzp43QnUblAzE1otTKTom8bTBSRrtqd/uE+QAbWEZ GMRrSJa39CkLRvqQmD7YRYHQSTcogV821/A9b7mN+BrIrXJwG6aa4mrcCfioltmxjJHtS wrZDpezr5YWsdF+WSDcfE27qAIs8RA7LaEcrwRIJ5+AXe2NRsTEKkzoerq5NN4n7FtrYD /ONKh2pNMTj2AuTDNjKwXyzUFI3n8WfM4eqUnakTui9lCVmNK+fiAMSxKiADRxS1NLK1a mtXC2CHULultCubwZK5/NGcvcvCBXlI9CNDrC3gqpns3r4rjbvR6HLS8kVAY189NvLQ== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039305; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Received:Date: From:To:Cc:Subject:Message-ID:References:X-Mailing-List:List-Id: List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner: Precedence:MIME-Version:Content-Type:Content-Disposition:In-Reply-To; bh=2h0EeiKQJ4FtQHuyABcPacYvHHK8At6ckj816AI0fR8=; b=S6azvPutW7joIRwVjidXRLgDsEvKoWubM52u5OhG4BBCHLnrDgI/z3fssk/+5EUcJw4A v35MAQ5zvkZshvrW70PfX5chOIZlp94q783yQGcWrXOXNNj0ea643MfJXdQHAcC9k0JkW 48fQagoar8DyjZOHkFDP6RUqRJmRLjbvATDpF5cPp0rYqd4suV14yFIQw7+UncQW0CTzE 4NFj5ce9CsGVkOVEwtUEpTVrVVmVddfIbMbSJci/AKEge3ZDaKqSjuqY/0X2OKhO0ksTB wSmiaYb6Hiz8eJewfCptwApAKDGpyPVR/izclxP+Qzpyst6Xs23oPDca7cPVBrPob0Q== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=iJzDAsOh; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; arc=fail 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 7250D606A32D; Mon, 30 Nov 2026 11:48:25 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=iJzDAsOh X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=217.70.190.124 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796039294; b=SYzR5+2mi8Y4RclyEm6Z0jTkShAErdMekJZwrBL6CvDMATJTOimr8hxCq6Z7ft0qUGlw dTNtpjuo4ezG5AXy5EHR0HBZsX1ofGECcYT3Dczu007TZ+hbFV2z/C3Yh3HMgwzXSr4SZ +6k7QekzxOwVu5mzC8KlfZXS4TYZEUB3xBcTZmd4e2DPXCTfiXPtE8eBntVjQpKq1PSmy dx4osUmso5vL46uMOwpjf+PfNKYBrXjA+gCnrogTYoMsmgHsBhgoT1rJPLm3BrlwHvDnc Swn9kbArb7qenMRrBkyLI325aP+LeG1dM7HRySBlPCEUF75iNwJUqydKiWI3bzi4N7w== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796039294; h=DMARC-Filter:DKIM-Signature:Received:Date:From:To:Cc:Subject: Message-ID:References:MIME-Version:Content-Type:Content-Disposition: In-Reply-To:X-Spam-Status:X-Spam-Checker-Version; bh=2h0EeiKQJ4FtQHuyABcPacYvHHK8At6ckj816AI0fR8=; b=SsstsBmncWAgDr9P7cpzdtJR14AQlCSn5DdFFLFlEzRVB/SHJEJb8BUiy2pkCokYKOOz dwiqox8DI6H76eqpcC7GvWDUXyLUQW4iHskQzAzKow0gYyvozLa81HeLsXcWeh4iKW7me gmcKfxKoiV/mBWyhPE8QRdG2Me2RaV+sQmwEHTQL3N5Z3Z8K5buQbARO/D1+IQmjEgxii ct7fmd8HoNrZd+pXFoemS9VEP6Axg8U1hA2tfDEG1HFXaez9+19rOu27KyWVMxsA4X+mU yHR/31DqhP9Jy/wUcV3SEQfqsngtRX/unr4huRUmYHZsI+UIVsIhuM4wX5tdgmZ8djQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=netfilter.org; spf=pass smtp.mailfrom=netfilter.org; dkim=pass header.d=netfilter.org header.i=@netfilter.org header.a=rsa-sha256 header.s=2025 header.b=iJzDAsOh; arc=none smtp.remote-ip=217.70.190.124 X-Received: from mail.netfilter.org (mail.netfilter.org [217.70.190.124]) (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 UTF8SMTPS id 210BB606A31C; Mon, 30 Nov 2026 11:48:13 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 210BB606A31C X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=netfilter.org X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=netfilter.org X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=netfilter.org; s=2025; t=1796039291; bh=2h0EeiKQJ4FtQHuyABcPacYvHHK8At6ckj816AI0fR8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=iJzDAsOhZY3iDds9L3JksVbQ6l2HXjQUUaPJgsIyLMGAHPzNclt336lRnWqHQNWnb MExlwPv6aIHTIT3shAcXDxZMGQpe1jUNBQGqLGW4c/aU+VUt6g4rsy5s4JiK3fGQhy Yl4R4F4Nd9+4NjADX5jTb2yIAMf4dnsjuAb9VL49pUuVEmG1J+gSzRR6vXp+ElZJAj 8jpr/7BZ8HIT8psJFcCNyU3Wzy0UGO7N0hS8IhKySI56USC8ZwAvyN/EJNLIclZGpm UNWIDpEUGgcZPdnnyeEQW5xwyPI2CHmN76xOZdDYlljsKLHfv0bAeDk1+Y/Mtyoi5P DK07gwpM8ANEw== X-Received: from netfilter.org (mail-agni [217.70.190.124]) by mail.netfilter.org (Postfix) with UTF8SMTPSA id 739C16005E; Mon, 30 Nov 2026 12:48:11 +0100 (CET) X-Cc: Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>, "David S . Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>, netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Xiang Mei <xmei5@asu.edu>, co+685321905e91e206@bugs.sh, Nikolay Aleksandrov <razor@blackwall.org> 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: <20261129161228.4185092-2-bestswngs@gmail.com>