Skip to contentSkip to conversation

tcp: avoid NULL skb dereference in zerocopy receive with sockmap

PATCH In review Junyoung Jang 2 participants 2 messages Nov 24

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tcp: avoid NULL skb dereference in zerocopy receive with sockmap

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")
---
 net/ipv4/tcp.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/ipv4/tcp.c +6 -0
index 418842a2dfd9..fa3b4bcbc726 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw
Quoted 3 lines · Junyoung Jang

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