BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Junyoung Jang" <graypanda.inzag@gmail.com>
Cc: bpf@vger.kernel.org
Subject: Re: [PATCH net] tcp: avoid NULL skb dereference in zerocopy receive with sockmap
Date: Wed, 25 Nov 2026 23:02:41 +0000	[thread overview]
Message-ID: <sashiko-outbox-234348@kernel.org> (raw)
In-Reply-To: <20261124230228.3868519-1-graypanda.inzag@gmail.com>

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

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

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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-25 23:02 ` sashiko-bot [this message]

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=sashiko-outbox-234348@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=graypanda.inzag@gmail.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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