Netdev List
 help / color / mirror / Atom feed
From: Kuniyuki Iwashima <kuniyu@google.com>
To: Eric Dumazet <edumazet@kernel.org>,
	Neal Cardwell <ncardwell@google.com>,
	 "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	 Willem de Bruijn <willemb@google.com>
Cc: Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	 Kuniyuki Iwashima <kuni1840@gmail.com>,
	netdev@vger.kernel.org
Subject: [PATCH v1 net] tcp: Don't queue skb to listener's backlog.
Date: Sat,  5 Dec 2026 20:28:54 +0900	[thread overview]
Message-ID: <20261205112906.1410872-1-kuniyu@google.com> (raw)

When SYN processing races with shutdown() for a listener
transitioning to TCP_CLOSE followed by listen(), skb could be
put into the backlog and processed later.

Since the cited commit, tcp_add_backlog() calls tcp_cleanup_skb()
and strips skb_dst(skb) to avoid costly refcounting.

However, icsk->icsk_af_ops->conn_request() requires skb_dst()
for broadcast/multicast checks, triggering NULL ptr deref below. [0]

Let's drop skb for (ex-)listener in tcp_add_backlog().

Note that the listener is not designed to use backlog (e.g.
rcu_dereference() in tcp_v[46]_cookie_check()) so we cannot
drop skb in ->conn_request() by checking !skb_dst(skb).

We can remove RCU/BH protection around ->conn_request() later
in net-next, which was added by commit 449809a66c1d ("tcp/dccp:
block BH for SYN processing") and commit 1ad98e9d1bdf ("tcp/dccp:
fix lockdep issue when SYN is backlogged").

[0]:
Oops: general protection fault, probably for non-canonical address 0xdffffc000000001b: 0000 [#1] SMP KASAN NOPTI
KASAN: null-ptr-deref in range [0x00000000000000d8-0x00000000000000df]
CPU: 24 UID: 0 PID: 972 Comm: a Not tainted 7.4.0-rc5-00307-ge9d78fc6741a #11 PREEMPT(lazy)
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.17.0-debian-1.17.0-1 04/01/2014
RIP: 0010:tcp_v6_conn_request (./include/net/ip6_route.h:276)
Code: 89 f8 48 c1 e8 03 42 80 3c 20 00 74 08 4c 89 ff e8 a3 bf b5 fd 4d 8b 3f 49 83 e7 fe 49 81 c7 d8 00 00 00 4c 89 f8 48 c1 e8 03 <42> 0f b6 04 20 84 c0 0f 85 ea 01 00 00 41 83 3f 00 78 4f 4c 8d b3
RSP: 0018:ffff88811320fcc0 EFLAGS: 00010206
RAX: 000000000000001b RBX: ffff8881145e1f80 RCX: dffffc0000000000
RDX: ffff8881105e2b64 RSI: ffff888110528368 RDI: ffff8881145e1f80
RBP: ffff8881145e1f92 R08: ffff8881145e2057 R09: 0000000000000000
R10: ffff8881145e2048 R11: ffffffff83ddfdd0 R12: dffffc0000000000
R13: ffff8881105e2a40 R14: ffff888110528368 R15: 00000000000000d8
FS:  00007f7edb9c16c0(0000) GS:ffff888193398000(0000) knlGS:0000000000000000
CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007f7edb9c0f78 CR3: 000000010be3a002 CR4: 0000000000370ef0
Call Trace:
 <TASK>
 tcp_rcv_state_process (net/ipv4/tcp_input.c:7292)
 tcp_v6_do_rcv (net/ipv6/tcp_ipv6.c:1656)
 __release_sock (./include/net/sock.h:1195)
 release_sock (net/core/sock.c:3946)
 inet_listen (net/ipv4/af_inet.c:251)
 __x64_sys_listen (net/socket.c:2001)
 do_syscall_64 (arch/x86/entry/syscall_64.c:61 arch/x86/entry/syscall_64.c:89)
 entry_SYSCALL_64_after_hwframe (arch/x86/entry/entry_64.S:121)
RIP: 0033:0x7f7edcac05eb
Code: 14 25 28 00 00 00 75 0c 48 81 c4 a0 00 00 00 5b 41 5c 5d c3 e8 56 b2 00 00 66 0f 1f 44 00 00 f3 0f 1e fa b8 32 00 00 00 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 8b 0d dd 77 0f 00 f7 d8 64 89 01 48
RSP: 002b:00007f7edb9c0de8 EFLAGS: 00000246 ORIG_RAX: 0000000000000032
RAX: ffffffffffffffda RBX: 00007f7edb9c1ce4 RCX: 00007f7edcac05eb
RDX: 000000000000002a RSI: 0000000000000080 RDI: 0000000000000006
RBP: 00007f7edb9c0e50 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000021
R13: 00007ffcacd6f3a0 R14: 0000000000000000 R15: 00007ffcacd6f4a7
 </TASK>
Modules linked in:

Fixes: ade9628ed049 ("tcp: drop dst in tcp_add_backlog()")
Signed-off-by: Kuniyuki Iwashima <kuniyu@google.com>
---
Found by LLM
---
 net/ipv4/tcp_ipv4.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/net/ipv4/tcp_ipv4.c b/net/ipv4/tcp_ipv4.c
index 8aa52376cf0a..b32609989d2a 100644
--- a/net/ipv4/tcp_ipv4.c
+++ b/net/ipv4/tcp_ipv4.c
@@ -1931,6 +1931,11 @@ enum skb_drop_reason tcp_add_backlog(struct sock *sk, struct sk_buff *skb)
 
 	tcp_cleanup_skb(skb);
 
+	if (unlikely((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))) {
+		bh_unlock_sock(sk);
+		return SKB_DROP_REASON_TCP_CLOSE;
+	}
+
 	if (unlikely(tcp_checksum_complete(skb))) {
 		bh_unlock_sock(sk);
 		trace_tcp_bad_csum(skb);
-- 
2.56.1.915.g69d5ec7298-goog


                 reply	other threads:[~2026-12-05 11:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261205112906.1410872-1-kuniyu@google.com \
    --to=kuniyu@google.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuni1840@gmail.com \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox