* [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
@ 2026-11-24 23:02 Junyoung Jang
2026-11-24 23:28 ` Alexei Starovoitov
2026-11-25 23:02 ` sashiko-bot
0 siblings, 2 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-24 23:02 UTC (permalink / raw)
To: Eric Dumazet, Neal Cardwell
Cc: Junyoung Jang, Gwanhyun Lee, Kuniyuki Iwashima, David S. Miller,
Jakub Kicinski, Paolo Abeni, Simon Horman, John Fastabend,
Jakub Sitnicki, Jiayuan Chen, netdev, bpf, linux-kernel
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
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
2026-11-24 23:02 [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap Junyoung Jang
@ 2026-11-24 23:28 ` Alexei Starovoitov
2026-11-25 10:03 ` Junyoung Jang
2026-11-26 12:53 ` Junyoung Jang
2026-11-25 23:02 ` sashiko-bot
1 sibling, 2 replies; 5+ messages in thread
From: Alexei Starovoitov @ 2026-11-24 23:28 UTC (permalink / raw)
To: Junyoung Jang, Eric Dumazet, Neal Cardwell
Cc: Gwanhyun Lee, Kuniyuki Iwashima, David S. Miller, Jakub Kicinski,
Paolo Abeni, Simon Horman, John Fastabend, Jakub Sitnicki,
Jiayuan Chen, netdev, bpf, linux-kernel
On Tue, Nov 24, 2026 at 03:02 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
> 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.
This should be fixed on bpf side. Other readers trust copied_seq too:
reader with stale copied_seq
--------------------------------- ----------------------------
tcp_recvmsg_locked() WARN "TCP recvmsg seq # bug"
tcp_read_sock() frees new skbs
tcp_collapse() BUG_ON(offset < 0)
tls_strp_load_anchor_with_queue() WARN_ON_ONCE(!first)
pw-bot: cr
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
2026-11-24 23:28 ` Alexei Starovoitov
@ 2026-11-25 10:03 ` Junyoung Jang
2026-11-26 12:53 ` Junyoung Jang
1 sibling, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-25 10:03 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Eric Dumazet, Neal Cardwell, Gwanhyun Lee, Kuniyuki Iwashima,
David S. Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
John Fastabend, Jakub Sitnicki, Jiayuan Chen, netdev, bpf,
linux-kernel
Thank you for the review and for pointing out the other paths affected
by a stale copied_seq.
I agree that this should be fixed on the sockmap side. I will look into
e5c6de5fa025 later today and prepare a v2. In the meantime, if the
sockmap maintainers have a preferred approach in mind, please let me
know.
On Wed, Nov 25, 2026 at 8:28 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Nov 24, 2026 at 03:02 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
> > 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.
>
> This should be fixed on bpf side. Other readers trust copied_seq too:
>
> reader with stale copied_seq
> --------------------------------- ----------------------------
> tcp_recvmsg_locked() WARN "TCP recvmsg seq # bug"
> tcp_read_sock() frees new skbs
> tcp_collapse() BUG_ON(offset < 0)
> tls_strp_load_anchor_with_queue() WARN_ON_ONCE(!first)
>
>
> pw-bot: cr
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
2026-11-24 23:02 [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap Junyoung Jang
2026-11-24 23:28 ` Alexei Starovoitov
@ 2026-11-25 23:02 ` sashiko-bot
1 sibling, 0 replies; 5+ messages in thread
From: sashiko-bot @ 2026-11-25 23:02 UTC (permalink / raw)
To: Junyoung Jang; +Cc: bpf
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] tcp: out-of-bounds read and skb drop in tcp_zerocopy_receive()
on seq gap
--
commit 51c17d06cb86cc681cbd2093e298086ab3b52896
Author: Junyoung Jang <graypanda.inzag@gmail.com>
tcp: avoid NULL skb dereference in zerocopy receive with sockmap
This commit adds NULL checks after tcp_recv_skb() and stops at the last
skb in sk_receive_queue in tcp_zc_handle_leftover() and
tcp_zerocopy_receive() to prevent NULL pointer dereferences when sockmap
removes skbs without advancing tp->copied_seq.
> diff --git a/net/ipv4/tcp.c b/net/ipv4/tcp.c
> index 418842a2dfd98..fa3b4bcbc726a 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))
[Severity: High]
What happens in tcp_recv_skb() when sk_receive_queue is not empty after
tcp_read_skb() has left tp->copied_seq behind?
If a later segment or FIN arrives (for instance, after the socket is
removed from the sockmap), TCP_SKB_CB(skb)->seq is ahead of seq:
net/ipv4/tcp.c:tcp_recv_skb() {
while ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) {
offset = seq - TCP_SKB_CB(skb)->seq;
...
if (offset < skb->len || (TCP_SKB_CB(skb)->tcp_flags & TCPHDR_FIN)) {
*off = offset;
return skb;
}
tcp_eat_recv_skb(sk, skb);
}
}
Because tcp_recv_skb() does not check before(seq, TCP_SKB_CB(skb)->seq)
like tcp_recvmsg_locked() does, offset underflows u32. For non-FIN
skbs, offset < skb->len is false and tcp_eat_recv_skb() silently drops
and frees all unread data in sk_receive_queue.
If the skb carries TCPHDR_FIN, tcp_recv_skb() returns the non-NULL skb
with the underflowed u32 offset, bypassing the new !skb checks. In both
the small-request and page-sized paths, tcp_zc_handle_leftover() passes
this offset through tcp_copy_straggler_data() -> skb_copy_datagram_msg()
-> __skb_datagram_iter(), where converting offset to a negative signed
int causes simple_copy_to_iter() to read out-of-bounds kernel memory
before skb->data and copy it to userspace.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261124230228.3868519-1-graypanda.inzag@gmail.com?part=1
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
2026-11-24 23:28 ` Alexei Starovoitov
2026-11-25 10:03 ` Junyoung Jang
@ 2026-11-26 12:53 ` Junyoung Jang
1 sibling, 0 replies; 5+ messages in thread
From: Junyoung Jang @ 2026-11-26 12:53 UTC (permalink / raw)
To: Alexei Starovoitov
Cc: Eric Dumazet, Neal Cardwell, Gwanhyun Lee, Kuniyuki Iwashima,
David S. Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
John Fastabend, Jakub Sitnicki, Jiayuan Chen, netdev, bpf,
linux-kernel
Hi Alexei,
I noticed Kyuwon Cho already reported the ingress_msg copied_seq issue
last week ("bpf, sockmap: data left in ingress_msg is dropped without
moving copied_seq, and TLS RX then warns"), so I'll drop this patch to
avoid duplicating work.
Thanks,
Junyoung
On Wed, Nov 25, 2026 at 8:28 AM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> On Tue, Nov 24, 2026 at 03:02 PM Junyoung Jang <graypanda.inzag@gmail.com> wrote:
> > 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.
>
> This should be fixed on bpf side. Other readers trust copied_seq too:
>
> reader with stale copied_seq
> --------------------------------- ----------------------------
> tcp_recvmsg_locked() WARN "TCP recvmsg seq # bug"
> tcp_read_sock() frees new skbs
> tcp_collapse() BUG_ON(offset < 0)
> tls_strp_load_anchor_with_queue() WARN_ON_ONCE(!first)
>
>
> pw-bot: cr
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-26 12:53 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 23:02 [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap Junyoung Jang
2026-11-24 23:28 ` Alexei Starovoitov
2026-11-25 10:03 ` Junyoung Jang
2026-11-26 12:53 ` Junyoung Jang
2026-11-25 23:02 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox