From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.174 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795968767; b=aKeSVtJ+4ai2H5ju1axRcFVrLWR4hLt/5jJhUPDfAPhCUfbbwC4yNGmRa/v+Wb1dtrKM Re6rMaTKDwB+X1sgVXM4jfnFd+66Y3LgTTlCNSE9Ve/PqXGVvOuACR+hdYMCVwAkyy2Iu yNf1ZUCRI+0pD17hB10JyKVaxUjkGNQlwH8Il1xAvaEN9L6ffSkGmJbRFgQQZ5EI36Ws5 57x6O2GOzAmcni5sNVDQ6V1Iz/0d7ngFlmGnkoowng5xBSmbI1XnYydv+6FaHbzlq9fLB Qwpw52k4jEtpAOjTuuFgwWI+aTtebnRvZFuQ5HKI8QF0S4B7zd9nGcYH72Et0mgUYqg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795968767; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=PoaIMXS7lUjGzcDL9yXPGryF1HpvALJUYEoom4dTDsI=; b=j5EDdTT5Z8gWz1us13g27/dpOYAzd3iYakxd20KUMYa0EzzuM6l86dHa50exMVCrjug7 AYzDadcz++soHmp/ht4mdU1LrvRfACN3reasRi+iTg4JmdbYRh6mc9kjzFxv+WemAv86d pmMVT20xibs18GrAuY10ztLilU4KV//BaEMVtEQun8wN+WRxI6NKOej8m4e2XURoX5gMl L/kNLMgH7vVCUiYhZZkZpzOyBHocjqh31n4LAEFzPfK6c3XxolA1G4XORJlA0xdjSu7Nc xDDkQ0DS/PYj+kMi2BIM5WZ2GIJlQlM08tUCQbLSbZHeLQN4U4uG4z8ZvrccxK72h3g== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Tt2b5wb5; arc=none smtp.remote-ip=74.125.82.174 Received: from mail-dy1-f174.google.com (mail-dy1-f174.google.com [74.125.82.174]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF401606A323 for ; Sun, 29 Nov 2026 16:12:46 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF401606A323 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=Tt2b5wb5 Received: by mail-dy1-f174.google.com with SMTP id 5a478bee46e88-38644aedba1so16030287eec.0 for ; Sun, 29 Nov 2026 08:12:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795968766; x=1796573566; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=PoaIMXS7lUjGzcDL9yXPGryF1HpvALJUYEoom4dTDsI=; b=Tt2b5wb5ikzhQPtekXqe+xem8N8JWVplr+MyP79HbN2HHlAPu0ehrKi/ER34INpspF ArRLRe8JpHbfXZZIYEb5OxDzr+2Rpnf/tTWtSuFobt0VtpW/CILVWODTcke+volIfeAj QPA0jBDnSdlPQMu3TlMXvornOR8vlIHlVpJrzWYnjQMCEl+o030CxvTSVQ9kZRH0ai3J JWOAZulOwdw2ZkyuLSzFjuMQjAmS31Epk0MEr0rF4aRMKqXx1Klc4E1ih4F/MMUd+/QS fbJTQxo0nx6a7kWii0AAzRb5AbGRm6rye5h+lSkU9oDhbs1zMO6bAWMdnxPoW2tXuMfI e/AQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795968766; x=1796573566; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=PoaIMXS7lUjGzcDL9yXPGryF1HpvALJUYEoom4dTDsI=; b=UZEWcWJCRXBBPVNdv4qOMaJZBlG3v4APIAl4HIxPtZEIGUsd7IDJhvRA4I8t6G5wUo SAPVFdY3N1v4GkI6BCMmL01tD4MnNIGZw50JWQAgEOjznv1zmspYrzeV5SKCqQjCgHBh 7fPET8wg+iQKJ511SX6Sd8W1lpaymMdoYOWuLRHcIzq00tZzADjbzsh8wb84/IybwZgZ OigSiOGHJ/SXfbx6FHTlSOtq6WzzB/iFYhgV/vHtM9DTCzQCgHGtExJQc0uoQhGJFAxN Z9hgyFSENXZzs4XX5Y2Uv8AIIroUye3R82BIxRkp0fTGVNwMb7ew3ZC773GPnHfoRxgO Dpzg== X-Forwarded-Encrypted: i=1; AL+hErHSR7xZGIJdu7yMucNwt5uQo2BX1WWhzlG2L2ZiaeNe0gho8MJOueVSYaHGhfgviym4kZiedTQ=@vger.kernel.org X-Gm-Message-State: APuU4Z/iTAhVsaAaJdVt/N2WAzbSdAw6vQfKwhuLL+s2zX49GE2H+4FT 8KDc+2pZNLHbeAfhsJIwT+LlCa4orXFXHw923HGq/QUbeWQBu9c6HUAX X-Gm-Gg: AZFdTZcVdaYsSVc3vrBeGbQplV22ETd5dP3xTtiN29hcqWfT3M/HmXH/aqQZzFzlzjR 6MupWdcWH2+kW9dV/iRHDZbjb8IWEFVZw0aGJu+OIfr7zhmCqFmTKBx8VizmkRA3iSzMBua2Fxv G5/jaUmlwZmu7SFMNMSHMg7j4JCe9axkMMbs0c/oLEn98y/TFkIu3thWNyxpDVNFSQimgWpSx9L JteFIOI5rtQdhYh53JepLwfYcgDMkXjI6Y5eWVIUhK7IcJzqkvi/nz7HUmLEGIXb2/yx1qYlBkq 4es6RZhhdMPw/ydb1xfKLE0BUzKVe6CLkzPoPRFdYllXw6sXgNrozWf3dgcYuu2QvEYTEYxusbr swchuAtOlzO63kt7H/AkbzTU0Cx/V03AbLFDwagp7Z/4GtRaeMxhAt17gYxK7v7hD8nxOHX5Cz+ qXg9w36lritdeeGfNGqy4HA+N/H92XtN9dnXGB5rMoMDFiMH1EgtzoOX6Z4F9TrrYf4/mx0Y5AW 0KXiUa5htq69dLkBAhepCIcvDSqe07QCAVgQGvDyYCMc6gSTxgbDgDsHVKzJLwkQGhNMsVWd8jn 1KrlZlfHdylFGuFZoJ8uc+dPgPUWWgDUoK6g+/UDE3BRNDXlvCZh6gqq+qs1RI1Ab0qVFVMahwI vAA== X-Received: by 2002:a05:7301:d587:b0:381:d92e:ecab with SMTP id 5a478bee46e88-3856f25884emr28096451eec.11.1795968765942; Sun, 29 Nov 2026 08:12:45 -0800 (PST) Received: from Terra.tail0ac356.ts.net ([198.176.50.208]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-385666e7f59sm96677539eec.6.2026.11.29.08.12.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 08:12:45 -0800 (PST) From: Weiming Shi To: Pablo Neira Ayuso , Florian Westphal , Phil Sutter , "David S . Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman Cc: netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Xiang Mei , Weiming Shi , co+685321905e91e206@bugs.sh Subject: [PATCH net] netfilter: nf_queue: do not queue BPF syncookie request sockets Date: Sun, 29 Nov 2026 08:12:29 -0800 Message-ID: <20261129161228.4185092-2-bestswngs@gmail.com> X-Mailer: git-send-email 2.56.1 X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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. Fixes: e472f88891ab ("bpf: tcp: Support arbitrary SYN Cookie.") Reported-by: Signed-off-by: Weiming Shi 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