[PATCH net-next v6 6/7] net: prevent mixing frags from different memory providers or bindings

From
Mina Almasry <hidden>
Date
2026-11-20 20:14:18
Reply to
Mina Almasry, 2026-11-20
Prevent coalescing, segmenting, or shifting skb fragments from
incompatible memory providers or different devmem bindings.

Add skb_mem_provider_compat() with an inline fast path checking only
skb_frags_readable() on both skbs (unreadable lives in
struct_group(headers, ...) alongside header flags already loaded in GRO
and TCP coalescing/collapse paths) and an out-of-line slow path
__skb_mem_provider_compat() comparing the net_iov provider instance
(net_iov_owner() for devmem bindings, niov->desc.pp for other net_iov
providers such as io_uring zcrx). Enforce compatibility across
skb_try_coalesce(), skb_gro_receive(), skb_gro_receive_list(),
skb_segment(), and tcp_skb_can_collapse(), and add a
DEBUG_NET_WARN_ON_ONCE() check in skb_shift() matching the existing
pp_recycle and decrypted assertions.

This issue is theoretical and was found by manual code inspection and
LLM review; it was not triggered in production. Verified with the
net_core KUnit test suite; not tested on real hardware.

Fixes: 65249feb6b3d ("net: add support for skbs with unreadable frags")
Signed-off-by: Mina Almasry <redacted>
---
 include/linux/skbuff.h | 18 ++++++++++++++++
 include/net/tcp.h      |  2 +-
 net/core/gro.c         |  6 ++++++
 net/core/skbuff.c      | 49 +++++++++++++++++++++++++++++++++++++++++-
 4 files changed, 73 insertions(+), 2 deletions(-)
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 0c69b66af8f0..979aab031096 100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -1890,6 +1890,24 @@ static inline bool skb_frags_readable(const struct sk_buff *skb)
 	return !skb->unreadable;
 }
 
+bool __skb_mem_provider_compat(const struct sk_buff *skb1,
+			       const struct sk_buff *skb2);
+
+/* Return true if the memory providers of @skb1 and @skb2 are compatible
+ * for coalescing. For any struct page-backed provider (the buddy allocator
+ * or a future provider handing out struct pages), coalescing is assumed
+ * feasible. For net_iov-backed providers, the provider instance pointer
+ * must match.
+ */
+static inline bool skb_mem_provider_compat(const struct sk_buff *skb1,
+					   const struct sk_buff *skb2)
+{
+	if (likely(skb_frags_readable(skb1) && skb_frags_readable(skb2)))
+		return true;
+
+	return __skb_mem_provider_compat(skb1, skb2);
+}
+
 static inline void skb_mark_not_on_list(struct sk_buff *skb)
 {
 	skb->next = NULL;
diff --git a/include/net/tcp.h b/include/net/tcp.h
index 0b896319aaf5..e2bb07a54a98 100644
--- a/include/net/tcp.h
+++ b/include/net/tcp.h
@@ -1285,7 +1285,7 @@ static inline bool tcp_skb_can_collapse(const struct sk_buff *to,
 		      mptcp_skb_can_collapse(to, from) &&
 		      !skb_cmp_decrypted(to, from) &&
 		      skb_pure_zcopy_same(to, from) &&
-		      skb_frags_readable(to) == skb_frags_readable(from));
+		      skb_mem_provider_compat(to, from));
 }
 
 static inline bool tcp_skb_can_collapse_rx(const struct sk_buff *to,
diff --git a/net/core/gro.c b/net/core/gro.c
index c093fe388dfe..6852b19e4840 100644
--- a/net/core/gro.c
+++ b/net/core/gro.c
@@ -109,6 +109,9 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 	if (p->pp_recycle != skb->pp_recycle)
 		return -ETOOMANYREFS;
 
+	if (!skb_mem_provider_compat(p, skb))
+		return -ETOOMANYREFS;
+
 	if (skb_zcopy(p) || skb_zcopy(skb))
 		return -ETOOMANYREFS;
 
@@ -233,6 +236,9 @@ int skb_gro_receive(struct sk_buff *p, struct sk_buff *skb)
 
 int skb_gro_receive_list(struct sk_buff *p, struct sk_buff *skb)
 {
+	if (!skb_mem_provider_compat(p, skb))
+		return -ETOOMANYREFS;
+
 	/* make sure to check flush flag and to not merge */
 	if (unlikely(p->len + skb->len >= 65536 ||
 		     NAPI_GRO_CB(skb)->flush))
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index aac61fbe419f..212d76ef9672 100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -4630,6 +4630,7 @@ int skb_shift(struct sk_buff *tgt, struct sk_buff *skb, int shiftlen)
 
 	DEBUG_NET_WARN_ON_ONCE(tgt->pp_recycle != skb->pp_recycle);
 	DEBUG_NET_WARN_ON_ONCE(skb_cmp_decrypted(tgt, skb));
+	DEBUG_NET_WARN_ON_ONCE(!skb_mem_provider_compat(tgt, skb));
 
 	todo = shiftlen;
 	from = 0;
@@ -5277,6 +5278,10 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
 				err = -EINVAL;
 				goto err;
 			}
+			if (!skb_mem_provider_compat(head_skb, list_skb)) {
+				err = -EINVAL;
+				goto err;
+			}
 
 			nskb = skb_clone(list_skb, GFP_ATOMIC);
 			if (unlikely(!nskb))
@@ -5398,6 +5403,11 @@ struct sk_buff *skb_segment(struct sk_buff *head_skb,
 					goto err;
 				}
 
+				if (!skb_mem_provider_compat(head_skb,
+							     list_skb)) {
+					err = -EINVAL;
+					goto err;
+				}
 				if (skb_shinfo(nskb)->nr_frags &&
 				    nskb->pp_recycle != list_skb->pp_recycle) {
 					nskb->data_len = pos - offset - hsize;
@@ -6588,6 +6598,43 @@ void kfree_skb_partial(struct sk_buff *skb, bool head_stolen)
 }
 EXPORT_SYMBOL(kfree_skb_partial);
 
+/* Return an opaque pointer identifying the memory provider instance of @skb's
+ * net_iov fragments:
+ * - For devmem (NET_IOV_DMABUF), net_iov_owner(niov) (&binding->area, 1:1 with
+ *   the binding across RX queues and TX).
+ * - For other net_iov providers (e.g., io_uring zcrx), niov->desc.pp (shared
+ *   across all areas of an io_zcrx_ifq).
+ */
+static void *skb_net_iov_provider(const struct sk_buff *skb)
+{
+	struct net_iov *niov;
+
+	if (!skb_shinfo(skb)->nr_frags)
+		return NULL;
+
+	niov = skb_frag_net_iov(&skb_shinfo(skb)->frags[0]);
+	if (!niov)
+		return NULL;
+
+	if (net_is_devmem_iov(niov))
+		return net_iov_owner(niov);
+
+	return niov->desc.pp;
+}
+
+bool __skb_mem_provider_compat(const struct sk_buff *skb1,
+			       const struct sk_buff *skb2)
+{
+	void *p1;
+
+	if (skb_frags_readable(skb1) != skb_frags_readable(skb2))
+		return false;
+
+	p1 = skb_net_iov_provider(skb1);
+	return p1 && p1 == skb_net_iov_provider(skb2);
+}
+EXPORT_SYMBOL(__skb_mem_provider_compat);
+
 /**
  * skb_try_coalesce - try to merge skb to prior one
  * @to: prior buffer
@@ -6620,7 +6667,7 @@ bool skb_try_coalesce(struct sk_buff *to, struct sk_buff *from,
 	 * belong to the same memory provider instance; do not coalesce skbs
 	 * that would mix them.
 	 */
-	if (skb_frags_readable(from) != skb_frags_readable(to))
+	if (!skb_mem_provider_compat(to, from))
 		return false;
 
 	if (len <= skb_tailroom(to) && skb_frags_readable(from)) {
-- 
2.56.1.750.gf70dc682cf-goog
Thread (8 messages) 8 messages, 1 author, last reply 1d ago
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help