All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junyoung Jang <graypanda.inzag@gmail.com>
To: Eric Dumazet <edumazet@kernel.org>, Neal Cardwell <ncardwell@google.com>
Cc: Junyoung Jang <graypanda.inzag@gmail.com>,
	Gwanhyun Lee <gwanhyunlee@gmail.com>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	"David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	John Fastabend <john.fastabend@gmail.com>,
	Jakub Sitnicki <jakub@cloudflare.com>,
	Jiayuan Chen <jiayuan.chen@linux.dev>,
	netdev@vger.kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
Date: Wed, 25 Nov 2026 08:02:27 +0900	[thread overview]
Message-ID: <20261124230228.3868519-1-graypanda.inzag@gmail.com> (raw)

A sockmap SK_SKB verdict can remove receive skbs through tcp_read_skb()
without advancing tp->copied_seq. tcp_inq() then reports unread bytes
while sk_receive_queue is empty. Deleting the socket's sockmap entry
does not repair the sequence mismatch.

TCP_ZEROCOPY_RECEIVE uses tcp_inq() before inspecting the native receive
queue. A small request enters tcp_zc_handle_leftover(), where
tcp_recv_skb() can return NULL. A page-sized request can reach the
mapping loop with the same empty queue and dereference a NULL skb.

Check tcp_recv_skb() before using its result in both paths. Also stop
before advancing beyond the last skb in sk_receive_queue. Neither
check consumes data in the sockmap ingress queue.

A custom LLM agent developed by the author found this issue. The author
manually reviewed the root cause and tested the reproducer and fix.
On net/main with KASAN, a privileged SOCKMAP SK_PASS reproducer
triggered NULL dereferences with both small and page-sized requests.
A map-delete variant also triggered the small-request fault. All
completed without a KASAN report after this change. A non-sockmap TCP
baseline still copied one byte. An adapted tcp_mmap -z selftest, with
only its transfer size reduced from 32 GiB to 64 MiB, passed before
and after the fix.

Fixes: e5c6de5fa025 ("bpf, sockmap: Incorrectly handling copied_seq")
Co-developed-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Gwanhyun Lee <gwanhyunlee@gmail.com>
Signed-off-by: Junyoung Jang <graypanda.inzag@gmail.com>
---
 net/ipv4/tcp.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
index 418842a2dfd9..fa3b4bcbc726 100644
--- a/net/ipv4/tcp.c
+++ b/net/ipv4/tcp.c
@@ -2072,6 +2072,8 @@ static int tcp_zc_handle_leftover(struct tcp_zerocopy_receive *zc,
 		offset = *seq - TCP_SKB_CB(skb)->seq;
 	} else {
 		skb = tcp_recv_skb(sk, *seq, &offset);
+		if (!skb)
+			return 0;
 		if (TCP_SKB_CB(skb)->has_rxtstamp) {
 			tcp_update_recv_tstamps(skb, tss);
 			zc->msg_flags |= TCP_CMSG_TS;
@@ -2274,10 +2276,14 @@ static int tcp_zerocopy_receive(struct sock *sk,
 			if (skb) {
 				if (zc->recv_skip_hint > 0)
 					break;
+				if (skb_queue_is_last(&sk->sk_receive_queue, skb))
+					break;
 				skb = skb->next;
 				offset = seq - TCP_SKB_CB(skb)->seq;
 			} else {
 				skb = tcp_recv_skb(sk, seq, &offset);
+				if (!skb)
+					break;
 			}
 
 			if (!skb_frags_readable(skb))

base-commit: 37fa509e43ba59d7c329025436e8ea64bdf089d7
-- 
2.43.0


             reply	other threads:[~2026-11-24 23:02 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 23:02 Junyoung Jang [this message]
2026-11-24 23:28 ` [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap Alexei Starovoitov
2026-11-25 10:03   ` Junyoung Jang
2026-11-26 12:53   ` Junyoung Jang
2026-11-25 23:02 ` sashiko-bot
2026-11-28 23:20 ` netdev-bot+sashiko

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=20261124230228.3868519-1-graypanda.inzag@gmail.com \
    --to=graypanda.inzag@gmail.com \
    --cc=bpf@vger.kernel.org \
    --cc=davem@davemloft.net \
    --cc=edumazet@kernel.org \
    --cc=gwanhyunlee@gmail.com \
    --cc=horms@kernel.org \
    --cc=jakub@cloudflare.com \
    --cc=jiayuan.chen@linux.dev \
    --cc=john.fastabend@gmail.com \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ncardwell@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.