Skip to contentSkip to conversation

net: fix unreadable net_iov skb handling across networking paths

PATCH In review Octavian Purdila 2 participants 15 messages Dec 3 – Dec 4

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

cover net: fix unreadable net_iov skb handling across networking paths

Device-memory TCP and io_uring zcrx bind RX queues to page_pool memory providers whose net_iov fragments cannot be mapped or read by the CPU. While standard TCP devmem traffic uses flow steering onto isolated RX queues, several networking paths either propagate stale skb metadata, mix readable and unreadable fragments across frag_list, or attempt to map or copy payload fragments on the CPU when unexpected protocol traffic or socket options interact with unreadable skbs:

1. GRO fragment reuse and pull failure (patches 1-2): napi_frags_skb() calls napi_reuse_skb() while fragments remain attached on Ethernet header pull failure and napi_gro_frags() dereferences the resulting NULL skb; napi_reuse_skb() leaves stale skb->unreadable and skb->pp_recycle state on cached napi->skb heads.

2. IP fragment reassembly and generic skb helpers (patches 3-4): IP fragment reassembly permits mixing readable and unreadable fragments in the same queue and does not propagate head->unreadable to the frag_list wrapper skb, and skb_to_sgvec(), skb_send_sock_locked(), sockmap verdict processing, and strparser assume CPU-readable fragments.

3. kTLS RX and TCP-MD5 / TCP-AO (patches 5-6): kTLS tls_strp decryption and software TCP-MD5 / TCP-AO payload hashing attempt to access unreadable net_iov fragments on the CPU.

Patches 1-6 fix these paths by freeing failed napi_frags_skb() skbs, resetting cached GRO skb state, rejecting mixed-readability IP fragments, and rejecting unreadable fragments cleanly before CPU page mapping occurs.

Patches 7-8 add KUnit regression tests in net_test covering the napi_gro_frags, IP defrag, scatterlist, skmsg, and strparser paths and verifying page_pool and dmabuf binding reference counts remain balanced. Patches 9-10 extend devmem.py with end-to-end protocol regression tests for kTLS RX and TCP-MD5 / TCP-AO RX and TX rejection.

Verified with net_core KUnit tests in QEMU and end-to-end devmem.py runs over ConnectX-7 (mlx5_core) 400GbE hardware.

Octavian Purdila (10):
net: gro: free skb on napi_frags_skb() header pull failure
net: gro: reset unreadable and pp_recycle in napi_reuse_skb
inet: frag: reject fragments with mixed readability in reassembly
net: skbuff, skmsg, strparser: reject unreadable frags
tls: reject unreadable skbs in tls_strp
tcp: reject unreadable skbs on TCP-MD5 and TCP-AO
net: kunit: test unreadable net_iov napi_gro_frags
net: kunit: test unreadable net_iov defrag and skb helpers
selftests: drv-net: devmem: test unreadable kTLS RX
selftests: drv-net: devmem: test unreadable skbs on TCP-MD5 and TCP-AO

 include/net/inet_frag.h                       |   1 +
 include/net/tcp.h                             |  33 +-
 net/core/gro.c                                |   7 +-
 net/core/net_test.c                           | 628 +++++++++++++++++-
 net/core/skbuff.c                             |   8 +
 net/core/skmsg.c                              |  10 +
 net/ipv4/inet_fragment.c                      |  13 +-
 net/ipv4/tcp.c                                |  32 +-
 net/ipv4/tcp_ao.c                             |  29 +-
 net/ipv4/tcp_ipv4.c                           |  13 +-
 net/ipv4/tcp_output.c                         |   9 +-
 net/ipv6/tcp_ipv6.c                           |  10 +-
 net/strparser/strparser.c                     |   5 +
 net/tls/tls_strp.c                            |  24 +-
 tools/testing/selftests/drivers/net/hw/config |   2 +
 .../selftests/drivers/net/hw/devmem.py        |  17 +-
 .../selftests/drivers/net/hw/devmem_lib.py    | 445 ++++++++++++-
 .../drivers/net/hw/lib/py/__init__.py         |  11 +-
 18 files changed, 1247 insertions(+), 50 deletions(-)

base-commit: 90ae4042bd9f35a9098d14b1a12a43df9b326e2b

Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#1 lore raw

PATCH 1/10 net: gro: free skb on napi_frags_skb() header pull failure

When skb_gro_header_slow() fails to pull the Ethernet header in napi_frags_skb() (for instance on an unreadable net_iov fragment without a linear header, or a short/malformed packet), the skb still has its attached fragments (nr_frags > 0, data_len > 0). Calling napi_reuse_skb() on this error path caches the dirty skb back onto napi->skb without releasing or clearing its fragments, leaking attached fragments and corrupting length metadata for the next napi_get_frags() call. In addition, napi_gro_frags() passes the resulting NULL skb straight into trace_napi_gro_frags_entry() and dev_gro_receive(), dereferencing a NULL pointer.

Free the skb with kfree_skb() in napi_frags_skb() on header-pull failure so attached fragments are released cleanly with their original recycling state intact, and return GRO_CONSUMED in napi_gro_frags() when napi_frags_skb() returns NULL.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes f55720680080 ("gro: Fix page ref count for skbs freed normally")
Fixes 1d11fa696733 ("net-gro: remove GRO_DROP")
Assisted-by LLM
---
 net/core/gro.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
net/core/gro.c +4 -1
index 1241b23d66efa..fb730017fd3ba 100644
@@ -776,7 +776,7 @@ static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
 		if (unlikely(!eth)) {
 			net_warn_ratelimited("%s: dropping impossible skb from %s\n",
 					     __func__, napi->dev->name);
-			napi_reuse_skb(napi, skb);
+			kfree_skb(skb);
 			return NULL;
 		}
 	} else {
@@ -805,6 +805,9 @@ gro_result_t napi_gro_frags(struct napi_struct *napi)
 	gro_result_t ret;
 	struct sk_buff *skb = napi_frags_skb(napi);
 
+	if (unlikely(!skb))
+		return GRO_CONSUMED;
+
 	trace_napi_gro_frags_entry(skb);
 
 	ret = napi_frags_finish(napi, skb, dev_gro_receive(&napi->gro, skb));
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#3 lore raw

PATCH 2/10 net: gro: reset unreadable and pp_recycle in napi_reuse_skb

In reply to Octavian Purdila

Drivers using napi_get_frags() and napi_gro_frags() cache and reuse the fragmentless head sk_buff on napi->skb whenever incoming fragments merge into an existing GRO flow. When an unreadable or page_pool-backed skb is cached and later reused for a normal readable packet (for instance across queue reconfiguration or shared NAPI instances), leaving skb->unreadable and skb->pp_recycle set on napi->skb leaks stale state onto subsequent packets, corrupting readability checks and causing skb_gro_receive() to refuse GRO merges due to mismatched pp_recycle state.

Reset skb->unreadable and skb->pp_recycle in napi_reuse_skb().

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes 6a5bcd84e886 ("page_pool: Allow drivers to hint on SKB recycling")
Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 net/core/gro.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/gro.c +2 -0
index fb730017fd3ba..c1d07bca1b4eb 100644
@@ -697,6 +697,8 @@ static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
 
 	skb->encapsulation = 0;
 	skb->ip_summed = CHECKSUM_NONE;
+	skb->unreadable = 0;
+	skb->pp_recycle = 0;
 
 	shinfo = skb_shinfo(skb);
 	shinfo->gso_type = 0;
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#5 lore raw

PATCH 3/10 inet: frag: reject fragments with mixed readability in reassembly

In reply to Octavian Purdila

A reassembled skb keeps the head fragment's unreadable bit while chaining subsequent fragments onto frag_list. Upper-layer socket receive paths dispatch based on skb_frags_readable() of the head skb (for example, tcp_recvmsg_locked() routes to skb_copy_datagram_msg() vs. tcp_recvmsg_dmabuf()), and require uniform readability across all frag_list entries. Mixing readable and unreadable fragments in the same reassembly queue (for example, when fragments split across a devmem queue and a normal queue, or when a size-threshold spill places only larger fragments in unreadable netmem) produces a mixed-readability reassembled skb that fails on delivery with -EFAULT or -ENODEV.

Reject a fragment in inet_frag_queue_insert() when its readability differs from the fragments already in the queue, mirroring IPFRAG_MIXED_SECPATH, while continuing to allow uniformly unreadable fragments to reassemble. Also propagate head->unreadable to the wrapper skb allocated by inet_frag_reasm_prepare() when the first fragment already carries a frag_list.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 include/net/inet_frag.h  |  1 +
 net/ipv4/inet_fragment.c | 13 ++++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
include/net/inet_frag.h +1 -0
index c1fb419a9cf03..668cb042b36b3 100644
@@ -175,6 +175,7 @@ extern const u8 ip_frag_ecn_table[16];
 #define IPFRAG_OVERLAP	2
 #define IPFRAG_MIXED_SECPATH	3
 #define IPFRAG_TOOBIG	4
+#define IPFRAG_MIXED_READABILITY	5
 
 /* Max sum of the truesize of fragments in a queue. This keeps the sums
  * done in inet_frag_reasm_prepare() and inet_frag_reasm_finish() well
net/ipv4/inet_fragment.c +10 -3
index f330d0a398233..caf07cf1a411c 100644
@@ -444,9 +444,15 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
 {
 	struct sk_buff *last = q->fragments_tail;
 
-	/* The reassembled skb keeps the first fragment's secpath. */
-	if (last && !secpath_equal(last, skb))
-		return IPFRAG_MIXED_SECPATH;
+	/* The reassembled skb keeps the first fragment's secpath and
+	 * readability state.
+	 */
+	if (last) {
+		if (!secpath_equal(last, skb))
+			return IPFRAG_MIXED_SECPATH;
+		if (skb_frags_readable(last) != skb_frags_readable(skb))
+			return IPFRAG_MIXED_READABILITY;
+	}
 
 	if (skb->truesize > INET_FRAG_MAX_TRUESIZE - q->truesize)
 		return IPFRAG_TOOBIG;
@@ -639,6 +645,7 @@ void *inet_frag_reasm_prepare(struct inet_frag_queue *q, struct sk_buff *skb,
 		head->truesize += clone->truesize;
 		clone->csum = 0;
 		clone->ip_summed = head->ip_summed;
+		clone->unreadable = head->unreadable;
 		add_frag_mem_limit(q->fqdir, clone->truesize);
 		skb_shinfo(head)->frag_list = clone;
 		nextp = &clone->next;
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#7 lore raw

PATCH 4/10 net: skbuff, skmsg, strparser: reject unreadable frags

In reply to Octavian Purdila

Scatterlist and sockmap/strparser consumers assume skb fragments are backed by CPU-accessible struct pages. If an skb carrying unreadable net_iov fragments reaches these paths (for instance via a devmem RX queue or after attaching a socket with queued devmem data), walking its fragments dereferences NULL page pointers or livelocks in delayed work retrying skb_linearize() on non-EMSGSIZE errors.

Fail fast with -EFAULT on unreadable skbs in scatterlist conversion, __skb_send_sock(), sockmap verdict processing, and strparser, and propagate non-EMSGSIZE enqueue errors while clearing SK_PSOCK_TX_ENABLED to stop further pass-verdict ingress queuing onto the faulted socket.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 net/core/skbuff.c         |  8 ++++++++
 net/core/skmsg.c          | 10 ++++++++++
 net/strparser/strparser.c |  5 +++++
 3 files changed, 23 insertions(+)
net/core/skbuff.c +8 -0
index 8ac7ad1378a12..d20d08682a132 100644
@@ -3380,6 +3380,11 @@ static int __skb_send_sock(struct sock *sk, struct sk_buff *skb, int offset,
 	if (!len)
 		goto out;
 
+	if (unlikely(!skb_frags_readable(skb)) && skb_shinfo(skb)->nr_frags) {
+		ret = -EFAULT;
+		goto error;
+	}
+
 	/* Make offset relative to start of frags */
 	offset -= skb_headlen(skb);
 
@@ -5391,6 +5396,9 @@ __skb_to_sgvec(struct sk_buff *skb, struct scatterlist *sg, int offset, int len,
 		offset += copy;
 	}
 
+	if (unlikely(!skb_frags_readable(skb)) && skb_shinfo(skb)->nr_frags)
+		return -EFAULT;
+
 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 		int end;
 
net/core/skmsg.c +10 -0
index 8a3b8536c0c21..f56393b43a462 100644
@@ -560,6 +560,9 @@ static int sk_psock_skb_ingress_enqueue(struct sk_buff *skb,
 	 */
 	num_sge = skb_to_sgvec(skb, msg->sg.data, off, len);
 	if (num_sge < 0) {
+		if (num_sge != -EMSGSIZE)
+			return num_sge;
+
 		/* skb linearize may fail with ENOMEM, but lets simply try again
 		 * later if this happens. Under memory pressure we don't want to
 		 * drop the skb. We need to linearize the skb so that the mapping
@@ -1015,6 +1018,13 @@ static int sk_psock_verdict_apply(struct sk_psock *psock, struct sk_buff *skb,
 	    skb_bpf_redirect_fetch(skb) == psock->sk)
 		verdict = __SK_PASS;
 
+	if (verdict != __SK_DROP && unlikely(!skb_frags_readable(skb))) {
+		sk_psock_report_error(psock, EFAULT);
+		sk_psock_clear_state(psock, SK_PSOCK_TX_ENABLED);
+		err = -EFAULT;
+		goto out_free;
+	}
+
 	switch (verdict) {
 	case __SK_PASS:
 		err = -EIO;
net/strparser/strparser.c +5 -0
index 9c1559d7923f2..e030eda7509cb 100644
@@ -99,6 +99,11 @@ static int strp_recv(read_descriptor_t *desc, struct sk_buff *orig_skb,
 	if (strp->paused)
 		return 0;
 
+	if (unlikely(!skb_frags_readable(orig_skb))) {
+		strp_parser_err(strp, -EFAULT, desc);
+		return 0;
+	}
+
 	head = strp->skb_head;
 	if (head) {
 		/* Message already in progress */
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#8 lore raw

PATCH 5/10 tls: reject unreadable skbs in tls_strp

In reply to Octavian Purdila

Software kTLS receive parses and decrypts TLS records on the CPU, assuming queued fragments are readable struct pages. If TLS_RX is configured on a socket with unreadable net_iov data in its receive queue (or receiving from a devmem RX queue), tls_strp either anchors unreadable skbs for CPU decryption or triggers warnings and advances skb lengths over uninitialized memory when skb_copy_bits() fails.

Reject unreadable skbs in tls_strp queue validation and abort stream parsing cleanly with -EFAULT if unreadable fragments are encountered.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying devmem.py selftests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 net/tls/tls_strp.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
net/tls/tls_strp.c +22 -2
index 53d3256d0114a..a9e49384a1692 100644
@@ -45,6 +45,11 @@ tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
 	struct sk_buff *skb;
 	int i, err;
 
+	if (unlikely(!skb_frags_readable(in_skb))) {
+		tls_strp_abort_strp(strp, -EFAULT);
+		return NULL;
+	}
+
 	skb = alloc_skb_with_frags(0, len, TLS_PAGE_ORDER,
 				   &err, strp->sk->sk_allocation);
 	if (!skb)
@@ -53,9 +58,13 @@ tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
 
-		WARN_ON_ONCE(skb_copy_bits(in_skb, offset,
+		if (unlikely(skb_copy_bits(in_skb, offset,
 					   skb_frag_address(frag),
-					   skb_frag_size(frag)));
+					   skb_frag_size(frag)))) {
+			kfree_skb(skb);
+			tls_strp_abort_strp(strp, -EFAULT);
+			return NULL;
+		}
 		offset += skb_frag_size(frag);
 	}
 
@@ -349,6 +358,12 @@ static int tls_strp_copyin(read_descriptor_t *desc, struct sk_buff *in_skb,
 	if (strp->msg_ready)
 		return 0;
 
+	if (unlikely(!skb_frags_readable(in_skb))) {
+		tls_strp_abort_strp(strp, -EFAULT);
+		desc->error = -EFAULT;
+		return 0;
+	}
+
 	skb = strp->anchor;
 	if (!skb->len)
 		skb_copy_decrypted(skb, in_skb);
@@ -438,6 +453,8 @@ static bool tls_strp_check_queue_ok(struct tls_strparser *strp,
 	u32 seq;
 
 	first = skb_shinfo(strp->anchor)->frag_list;
+	if (unlikely(!skb_frags_readable(first)))
+		return false;
 	skb = first;
 	seq = TCP_SKB_CB(first)->seq;
 
@@ -449,6 +466,9 @@ static bool tls_strp_check_queue_ok(struct tls_strparser *strp,
 		remaining -= skb->len;
 		skb = skb->next;
 
+		if (unlikely(!skb_frags_readable(skb)))
+			return false;
+
 		if (TCP_SKB_CB(skb)->seq != seq)
 			return false;
 		if (skb_cmp_decrypted(first, skb))
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#10 lore raw

PATCH 6/10 tcp: reject unreadable skbs on TCP-MD5 and TCP-AO

In reply to Octavian Purdila

Software TCP-MD5 and TCP-AO compute packet digests on the CPU by mapping skb fragments via skb_frag_foreach_page() and kmap_local_page(), which dereferences NULL page pointers on unreadable net_iov fragments on both receive and transmit. In addition, tcp_ao_verify_hash() ignored tcp_ao_hash_skb() errors and compared the incoming option against a zeroed buffer.

Fail signature payload hashing with -EFAULT on unreadable skbs and check hash helper errors across TCP-MD5 and TCP-AO receive and reset paths. On transmit, disallow devmem sends in tcp_sendmsg_locked() when TCP-MD5 or TCP-AO is active on the socket, reject enabling TCP-MD5 or TCP-AO keys while unreadable skbs are queued on the write or retransmit queues, and keep a defensive guard in __tcp_transmit_skb().

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying devmem.py selftests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Fixes bd61848900bf ("net: devmem: Implement TX path")
Assisted-by LLM
---
 include/net/tcp.h     | 33 ++++++++++++++++++++++++---------
 net/ipv4/tcp.c        | 32 +++++++++++++++++++++++++-------
 net/ipv4/tcp_ao.c     | 29 ++++++++++++++++++++---------
 net/ipv4/tcp_ipv4.c   | 13 +++++++++----
 net/ipv4/tcp_output.c |  9 ++++++++-
 net/ipv6/tcp_ipv6.c   | 10 ++++++----
 6 files changed, 92 insertions(+), 34 deletions(-)
include/net/tcp.h +24 -9
index 869e780fcb482..b189236e89dc9 100644
@@ -2104,8 +2104,8 @@ struct tcp6_pseudohdr {
 	__be32		protocol;	/* including padding */
 };
 
-void tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
-			 const struct sock *sk, const struct sk_buff *skb);
+int tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
+			const struct sock *sk, const struct sk_buff *skb);
 int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 		   int family, u8 prefixlen, int l3index, u8 flags,
 		   const u8 *newkey, u8 newkeylen);
@@ -2165,8 +2165,8 @@ static inline void tcp_md5_destruct_sock(struct sock *sk)
 #endif
 
 struct md5_ctx;
-void tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
-			   unsigned int header_len);
+int tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
+			  unsigned int header_len);
 void tcp_md5_hash_key(struct md5_ctx *ctx, const struct tcp_md5sig_key *key);
 
 /* From tcp_fastopen.c */
@@ -2358,6 +2358,21 @@ static inline bool tcp_rtx_and_write_queues_empty(const struct sock *sk)
 	return tcp_rtx_queue_empty(sk) && tcp_write_queue_empty(sk);
 }
 
+static inline bool tcp_tx_has_unreadable_skb(const struct sock *sk)
+{
+	struct sk_buff *skb;
+
+	skb_queue_walk(&sk->sk_write_queue, skb) {
+		if (!skb_frags_readable(skb))
+			return true;
+	}
+	skb_rbtree_walk(skb, &sk->tcp_rtx_queue) {
+		if (!skb_frags_readable(skb))
+			return true;
+	}
+	return false;
+}
+
 static inline void tcp_add_write_queue_tail(struct sock *sk, struct sk_buff *skb)
 {
 	__skb_queue_tail(&sk->sk_write_queue, skb);
@@ -2549,7 +2564,7 @@ struct tcp_sock_af_ops {
 #ifdef CONFIG_TCP_MD5SIG
 	struct tcp_md5sig_key	*(*md5_lookup) (const struct sock *sk,
 						const struct sock *addr_sk);
-	void		(*calc_md5_hash)(char *location,
+	int		(*calc_md5_hash)(char *location,
 					 const struct tcp_md5sig_key *md5,
 					 const struct sock *sk,
 					 const struct sk_buff *skb);
@@ -2577,10 +2592,10 @@ struct tcp_request_sock_ops {
 #ifdef CONFIG_TCP_MD5SIG
 	struct tcp_md5sig_key *(*req_md5_lookup)(const struct sock *sk,
 						 const struct sock *addr_sk);
-	void		(*calc_md5_hash) (char *location,
-					  const struct tcp_md5sig_key *md5,
-					  const struct sock *sk,
-					  const struct sk_buff *skb);
+	int		(*calc_md5_hash)(char *location,
+					 const struct tcp_md5sig_key *md5,
+					 const struct sock *sk,
+					 const struct sk_buff *skb);
 #endif
 #ifdef CONFIG_TCP_AO
 	struct tcp_ao_key *(*ao_lookup)(const struct sock *sk,
net/ipv4/tcp.c +25 -7
index 80de376a9d2ef..f0ab18b589872 100644
@@ -1226,6 +1226,16 @@ int tcp_sendmsg_locked(struct sock *sk, struct msghdr *msg, size_t size)
 		/* 'common' sending to sendq */
 	}
 
+	if (unlikely(binding)) {
+		struct tcp_key key;
+
+		tcp_get_current_key(sk, &key);
+		if (tcp_key_is_md5(&key) || tcp_key_is_ao(&key)) {
+			err = -EOPNOTSUPP;
+			goto out_err;
+		}
+	}
+
 	if (sockc_err) {
 		err = sockc_err;
 		goto out_err;
@@ -4891,8 +4901,8 @@ int tcp_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
 }
 
 #ifdef CONFIG_TCP_MD5SIG
-void tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
-			   unsigned int header_len)
+int tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
+			  unsigned int header_len)
 {
 	const unsigned int head_data_len = skb_headlen(skb) > header_len ?
 					   skb_headlen(skb) - header_len : 0;
@@ -4900,6 +4910,9 @@ void tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
 	struct sk_buff *frag_iter;
 	unsigned int i;
 
+	if (unlikely(!skb_frags_readable(skb)))
+		return -EFAULT;
+
 	md5_update(ctx, (const u8 *)tcp_hdr(skb) + header_len, head_data_len);
 
 	for (i = 0; i < shi->nr_frags; ++i) {
@@ -4916,8 +4929,12 @@ void tcp_md5_hash_skb_data(struct md5_ctx *ctx, const struct sk_buff *skb,
 		}
 	}
 
-	skb_walk_frags(skb, frag_iter)
-		tcp_md5_hash_skb_data(ctx, frag_iter, 0);
+	skb_walk_frags(skb, frag_iter) {
+		if (tcp_md5_hash_skb_data(ctx, frag_iter, 0))
+			return -EFAULT;
+	}
+
+	return 0;
 }
 
 void tcp_md5_hash_key(struct md5_ctx *ctx,
@@ -4945,6 +4962,7 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
 	const struct tcp_sock *tp = tcp_sk(sk);
 	struct tcp_md5sig_key *key;
 	u8 newhash[16];
+	int err;
 
 	key = tcp_md5_do_lookup(sk, l3index, saddr, family);
 	if (!key) {
@@ -4958,10 +4976,10 @@ tcp_inbound_md5_hash(const struct sock *sk, const struct sk_buff *skb,
 	 * IPv4-mapped case.
 	 */
 	if (family == AF_INET)
-		tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
+		err = tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
 	else
-		tp->af_specific->calc_md5_hash(newhash, key, NULL, skb);
-	if (crypto_memneq(hash_location, newhash, 16)) {
+		err = tp->af_specific->calc_md5_hash(newhash, key, NULL, skb);
+	if (err || crypto_memneq(hash_location, newhash, 16)) {
 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPMD5FAILURE);
 		trace_tcp_hash_md5_mismatch(sk, skb);
 		return SKB_DROP_REASON_TCP_MD5FAILURE;
net/ipv4/tcp_ao.c +20 -9
index 857b8684e01d9..34c69dfcedd04 100644
@@ -679,9 +679,9 @@ int tcp_ao_hash_hdr(unsigned short int family, char *ao_hash,
 	return 1;
 }
 
-static void tcp_ao_hash_skb_data(struct tcp_ao_mac_ctx *mac_ctx,
-				 const struct sk_buff *skb,
-				 unsigned int header_len)
+static int tcp_ao_hash_skb_data(struct tcp_ao_mac_ctx *mac_ctx,
+				const struct sk_buff *skb,
+				unsigned int header_len)
 {
 	const unsigned int head_data_len = skb_headlen(skb) > header_len ?
 					   skb_headlen(skb) - header_len : 0;
@@ -689,6 +689,9 @@ static void tcp_ao_hash_skb_data(struct tcp_ao_mac_ctx *mac_ctx,
 	struct sk_buff *frag_iter;
 	unsigned int i;
 
+	if (unlikely(!skb_frags_readable(skb)))
+		return -EFAULT;
+
 	tcp_ao_mac_update(mac_ctx, (const u8 *)tcp_hdr(skb) + header_len,
 			  head_data_len);
 
@@ -706,8 +709,12 @@ static void tcp_ao_hash_skb_data(struct tcp_ao_mac_ctx *mac_ctx,
 		}
 	}
 
-	skb_walk_frags(skb, frag_iter)
-		tcp_ao_hash_skb_data(mac_ctx, frag_iter, 0);
+	skb_walk_frags(skb, frag_iter) {
+		if (tcp_ao_hash_skb_data(mac_ctx, frag_iter, 0))
+			return -EFAULT;
+	}
+
+	return 0;
 }
 
 int tcp_ao_hash_skb(unsigned short int family,
@@ -726,7 +733,8 @@ int tcp_ao_hash_skb(unsigned short int family,
 	tcp_ao_hash_header(&mac_ctx, th,
 			   !!(key->keyflags & TCP_AO_KEYF_EXCLUDE_OPT),
 			   ao_hash, hash_offset, tcp_ao_maclen(key));
-	tcp_ao_hash_skb_data(&mac_ctx, skb, th->doff << 2);
+	if (tcp_ao_hash_skb_data(&mac_ctx, skb, th->doff << 2))
+		goto clear_hash;
 	tcp_ao_mac_final(&mac_ctx, hash_buf);
 
 	memcpy(ao_hash, hash_buf, tcp_ao_maclen(key));
@@ -970,9 +978,9 @@ tcp_ao_verify_hash(const struct sock *sk, const struct sk_buff *skb,
 	}
 
 	/* XXX: make it per-AF callback? */
-	tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key,
-			(phash - (u8 *)th), sne);
-	if (crypto_memneq(phash, hash_buf, maclen)) {
+	if (tcp_ao_hash_skb(family, hash_buf, key, sk, skb, traffic_key,
+			    (phash - (u8 *)th), sne) ||
+	    crypto_memneq(phash, hash_buf, maclen)) {
 		NET_INC_STATS(sock_net(sk), LINUX_MIB_TCPAOBAD);
 		atomic64_inc(&info->counters.pkt_bad);
 		atomic64_inc(&key->pkt_bad);
@@ -1665,6 +1673,9 @@ static int tcp_ao_add_cmd(struct sock *sk, unsigned short int family,
 	if (IS_ERR(ao_info))
 		return PTR_ERR(ao_info);
 
+	if (tcp_tx_has_unreadable_skb(sk))
+		return -EOPNOTSUPP;
+
 	if (!ao_info) {
 		ao_info = tcp_ao_alloc_info(GFP_KERNEL);
 		if (!ao_info)
net/ipv4/tcp_ipv4.c +9 -4
index 8aa52376cf0aa..2f2b187a1ed85 100644
@@ -817,8 +817,8 @@ static void tcp_v4_send_reset(const struct sock *sk, struct sk_buff *skb,
 		if (!key)
 			goto out;
 
-		tcp_v4_md5_hash_skb(newhash, key, NULL, skb);
-		if (crypto_memneq(md5_hash_location, newhash, 16))
+		if (tcp_v4_md5_hash_skb(newhash, key, NULL, skb) ||
+		    crypto_memneq(md5_hash_location, newhash, 16))
 			goto out;
 	}
 
@@ -1399,6 +1399,9 @@ int tcp_md5_do_add(struct sock *sk, const union tcp_md5_addr *addr,
 {
 	struct tcp_sock *tp = tcp_sk(sk);
 
+	if (tcp_tx_has_unreadable_skb(sk))
+		return -EOPNOTSUPP;
+
 	if (!rcu_dereference_protected(tp->md5sig_info, lockdep_sock_is_held(sk))) {
 		if (fips_enabled) {
 			pr_warn_once("TCP-MD5 support is disabled due to FIPS\n");
@@ -1576,7 +1579,7 @@ tcp_v4_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
 	md5_final(&ctx, md5_hash);
 }
 
-noinline_for_stack void
+noinline_for_stack int
 tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
 		    const struct sock *sk, const struct sk_buff *skb)
 {
@@ -1595,9 +1598,11 @@ tcp_v4_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
 
 	md5_init(&ctx);
 	tcp_v4_md5_hash_headers(&ctx, daddr, saddr, th, skb->len);
-	tcp_md5_hash_skb_data(&ctx, skb, th->doff << 2);
+	if (tcp_md5_hash_skb_data(&ctx, skb, th->doff << 2))
+		return -EFAULT;
 	tcp_md5_hash_key(&ctx, key);
 	md5_final(&ctx, md5_hash);
+	return 0;
 }
 
 #endif
net/ipv4/tcp_output.c +8 -1
index 043838d996404..6749ec5873dfd 100644
@@ -1606,6 +1606,14 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
 
 	BUG_ON(!skb || !tcp_skb_pcount(skb));
 	tp = tcp_sk(sk);
+	tcp_get_current_key(sk, &key);
+	if (unlikely(!skb_frags_readable(skb) &&
+		     (tcp_key_is_md5(&key) || tcp_key_is_ao(&key)))) {
+		if (!clone_it)
+			kfree_skb(skb);
+		return -EOPNOTSUPP;
+	}
+
 	prior_wstamp = tp->tcp_wstamp_ns;
 	tp->tcp_wstamp_ns = max(tp->tcp_wstamp_ns, tp->tcp_clock_cache);
 	skb_set_delivery_time(skb, tp->tcp_wstamp_ns, SKB_CLOCK_MONOTONIC);
@@ -1631,7 +1639,6 @@ static int __tcp_transmit_skb(struct sock *sk, struct sk_buff *skb,
 	tcb = TCP_SKB_CB(skb);
 	memset(&opts.cleared, 0, sizeof(opts.cleared));
 
-	tcp_get_current_key(sk, &key);
 	if (unlikely(tcb->tcp_flags & TCPHDR_SYN)) {
 		tcp_options_size = tcp_syn_options(sk, skb, &opts, &key);
 	} else {
net/ipv6/tcp_ipv6.c +6 -4
index 5c7ed38f1f56a..61bfdd5108740 100644
@@ -751,7 +751,7 @@ tcp_v6_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
 	md5_final(&ctx, md5_hash);
 }
 
-static noinline_for_stack void
+static noinline_for_stack int
 tcp_v6_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
 		    const struct sock *sk, const struct sk_buff *skb)
 {
@@ -770,9 +770,11 @@ tcp_v6_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
 
 	md5_init(&ctx);
 	tcp_v6_md5_hash_headers(&ctx, daddr, saddr, th, skb->len);
-	tcp_md5_hash_skb_data(&ctx, skb, th->doff << 2);
+	if (tcp_md5_hash_skb_data(&ctx, skb, th->doff << 2))
+		return -EFAULT;
 	tcp_md5_hash_key(&ctx, key);
 	md5_final(&ctx, md5_hash);
+	return 0;
 }
 #endif
 
@@ -1083,8 +1085,8 @@ static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb,
 			goto out;
 		key.type = TCP_KEY_MD5;
 
-		tcp_v6_md5_hash_skb(newhash, key.md5_key, NULL, skb);
-		if (crypto_memneq(md5_hash_location, newhash, 16))
+		if (tcp_v6_md5_hash_skb(newhash, key.md5_key, NULL, skb) ||
+		    crypto_memneq(md5_hash_location, newhash, 16))
 			goto out;
 	}
 #endif
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#11 lore raw

PATCH 7/10 net: kunit: test unreadable net_iov napi_gro_frags

In reply to Octavian Purdila

Add KUnit regression tests in net_test covering unreadable net_iov skb handling on napi_frags_skb() header-pull failure and NAPI GRO fragment reuse in napi_reuse_skb(), verifying that pull failures free the skb without crashing or leaking references, and that unreadable and pp_recycle flags are cleared on reuse with balanced page_pool and binding reference counts.

Assisted-by LLM
---
 net/core/net_test.c | 246 +++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 245 insertions(+), 1 deletion(-)
net/core/net_test.c +245 -1
index 47f5dc0fa5cf6..b939cc93e9050 100644
@@ -515,9 +515,13 @@ static void ip_tunnel_flags_test_run(struct kunit *test)
 
 /* Netmem & SKB fragment refcounting */
 
+#include <linux/etherdevice.h>
 #include <linux/skbuff_ref.h>
-#include <net/netmem.h>
+#include <net/gro.h>
+#include <net/ip.h>
+#include <net/netdev_lock.h>
 #include <net/netdev_rx_queue.h>
+#include <net/netmem.h>
 #include <net/page_pool/helpers.h>
 #include <net/page_pool/memory_provider.h>
 #include <net/tcp.h>
@@ -1720,6 +1724,244 @@ static void netmem_ref_test_tcp_devmem_recv(struct kunit *test)
 	page_pool_destroy(pool);
 	percpu_ref_exit(&binding.ref);
 }
+
+struct netmem_unreadable_env {
+	struct net_devmem_dmabuf_binding binding;
+	struct page_pool *pool;
+	struct net_iov niov;
+	netmem_ref niov_nm;
+	long base_ref;
+};
+
+static void netmem_unreadable_env_cleanup(struct netmem_unreadable_env *env)
+{
+	netmem_clear_pp_magic(env->niov_nm);
+	netmem_set_pp(env->niov_nm, NULL);
+	page_pool_destroy(env->pool);
+	percpu_ref_exit(&env->binding.ref);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(netmem_unreadable_env_cleanup_action,
+			    netmem_unreadable_env_cleanup,
+			    struct netmem_unreadable_env *);
+
+static struct netmem_unreadable_env *
+netmem_unreadable_env_init(struct kunit *test)
+{
+	struct page_pool_params pp_params = {
+		.order = 0,
+		.pool_size = 8,
+		.nid = NUMA_NO_NODE,
+	};
+	struct netmem_unreadable_env *env;
+	int ret;
+
+	env = kunit_kzalloc(test, sizeof(*env), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, env);
+
+	ret = percpu_ref_init(&env->binding.ref, dummy_percpu_ref_release,
+			      PERCPU_REF_INIT_ATOMIC, GFP_KERNEL);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	env->pool = page_pool_create(&pp_params);
+	if (IS_ERR(env->pool)) {
+		percpu_ref_exit(&env->binding.ref);
+		KUNIT_ASSERT_FALSE(test, IS_ERR(env->pool));
+	}
+
+	env->binding.area.niovs = &env->niov;
+	net_iov_init(&env->niov, &env->binding.area, NET_IOV_DMABUF);
+	env->niov_nm = net_iov_to_netmem(&env->niov);
+	netmem_set_pp(env->niov_nm, env->pool);
+	netmem_or_pp_magic(env->niov_nm, PP_SIGNATURE);
+	atomic_long_set(&env->niov.desc.pp_ref_count, 2);
+	env->base_ref = atomic_long_read(&env->binding.ref.data->count);
+
+	ret = kunit_add_action_or_reset(test,
+					netmem_unreadable_env_cleanup_action,
+					env);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	return env;
+}
+
+static void netmem_unreadable_add_frag(struct sk_buff *skb,
+				       struct netmem_unreadable_env *env,
+				       u32 off, u32 len)
+{
+	skb->pp_recycle = 1;
+	skb_add_rx_frag_netmem(skb, 0, env->niov_nm, off, len, PAGE_SIZE);
+	napi_pp_get_page(env->niov_nm);
+}
+
+static void netmem_unreadable_env_check(struct kunit *test,
+					struct netmem_unreadable_env *env)
+{
+	KUNIT_EXPECT_EQ(test, atomic_long_read(&env->niov.desc.pp_ref_count),
+			2L);
+	KUNIT_EXPECT_EQ(test, atomic_long_read(&env->binding.ref.data->count),
+			env->base_ref);
+}
+
+struct gro_test_napi_ctx {
+	struct net_device *dev;
+	struct napi_struct napi;
+};
+
+static int dummy_napi_poll(struct napi_struct *napi, int budget)
+{
+	return 0;
+}
+
+static void gro_test_napi_cleanup(struct gro_test_napi_ctx *ctx)
+{
+	netdev_lock(ctx->dev);
+	__netif_napi_del_locked(&ctx->napi);
+	netdev_unlock(ctx->dev);
+	free_netdev(ctx->dev);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(gro_test_napi_cleanup_action,
+			    gro_test_napi_cleanup,
+			    struct gro_test_napi_ctx *);
+
+static struct gro_test_napi_ctx *alloc_gro_test_napi(struct kunit *test)
+{
+	struct gro_test_napi_ctx *ctx;
+	int ret;
+
+	ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, ctx);
+
+	ctx->dev = alloc_etherdev(0);
+	KUNIT_ASSERT_NOT_NULL(test, ctx->dev);
+	ctx->dev->features |= NETIF_F_GRO;
+	netif_napi_add_weight(ctx->dev, &ctx->napi, dummy_napi_poll, 64);
+
+	ret = kunit_add_action_or_reset(test, gro_test_napi_cleanup_action,
+					ctx);
+	KUNIT_ASSERT_EQ(test, ret, 0);
+
+	return ctx;
+}
+
+static void fill_tcp4_headers(struct sk_buff *skb, __be32 saddr, __be32 daddr,
+			      __be16 sport, __be16 dport, u32 seq, u32 ack_seq,
+			      unsigned int payload_len)
+{
+	struct ethhdr *eth;
+	struct tcphdr *th;
+	struct iphdr *iph;
+
+	skb_reset_mac_header(skb);
+	eth = skb_put_zero(skb, sizeof(*eth));
+	eth->h_proto = htons(ETH_P_IP);
+
+	skb_set_network_header(skb, skb->len);
+	iph = skb_put_zero(skb, sizeof(*iph));
+	iph->version = 4;
+	iph->ihl = 5;
+	iph->ttl = 64;
+	iph->protocol = IPPROTO_TCP;
+	iph->saddr = saddr;
+	iph->daddr = daddr;
+	iph->tot_len = htons(sizeof(*iph) + sizeof(*th) + payload_len);
+	ip_send_check(iph);
+
+	skb_set_transport_header(skb, skb->len);
+	th = skb_put_zero(skb, sizeof(*th));
+	th->source = sport;
+	th->dest = dport;
+	th->seq = htonl(seq);
+	th->ack_seq = htonl(ack_seq);
+	th->doff = sizeof(*th) / 4;
+	th->ack = 1;
+	th->window = htons(65535);
+
+	skb->protocol = htons(ETH_P_IP);
+	skb->ip_summed = CHECKSUM_UNNECESSARY;
+}
+
+static void netmem_unreadable_test_napi_pull_fail(struct kunit *test)
+{
+	struct netmem_unreadable_env *env = netmem_unreadable_env_init(test);
+	struct gro_test_napi_ctx *ctx = alloc_gro_test_napi(test);
+	struct napi_struct *napi = &ctx->napi;
+	gro_result_t ret = GRO_NORMAL;
+	bool has_cached = true;
+	struct sk_buff *skb;
+
+	/* Attach only an unreadable net_iov without linear ETH_HLEN so
+	 * napi_frags_skb() fails header pull: verify napi_gro_frags() returns
+	 * GRO_CONSUMED without crashing, caching a dirty skb, or leaking the
+	 * net_iov reference. napi_get_frags() and napi_gro_frags() require
+	 * BH-disabled NAPI context.
+	 */
+	local_bh_disable();
+	skb = napi_get_frags(napi);
+	if (skb) {
+		netmem_unreadable_add_frag(skb, env, 0, 64);
+		ret = napi_gro_frags(napi);
+		has_cached = !!napi->skb;
+	}
+	local_bh_enable();
+
+	KUNIT_ASSERT_NOT_NULL(test, skb);
+	KUNIT_EXPECT_EQ(test, ret, GRO_CONSUMED);
+	KUNIT_EXPECT_FALSE(test, has_cached);
+
+	kunit_release_action(test, gro_test_napi_cleanup_action, ctx);
+	netmem_unreadable_env_check(test, env);
+}
+
+static void netmem_unreadable_test_napi_reuse(struct kunit *test)
+{
+	struct netmem_unreadable_env *env = netmem_unreadable_env_init(test);
+	struct gro_test_napi_ctx *ctx = alloc_gro_test_napi(test);
+	struct napi_struct *napi = &ctx->napi;
+	gro_result_t ret1 = GRO_NORMAL, ret2 = GRO_NORMAL;
+	bool readable = false, pp_recycle = true;
+	struct sk_buff *skb;
+
+	/* Merge two unreadable pp_recycle segments via napi_gro_frags() so
+	 * napi_reuse_skb() caches the second skb on napi->skb, then verify
+	 * the cached skb has unreadable and pp_recycle cleared.
+	 * napi_get_frags() and napi_gro_frags() require BH-disabled context.
+	 */
+	local_bh_disable();
+	skb = napi_get_frags(napi);
+	if (!skb)
+		goto out_bh;
+	fill_tcp4_headers(skb, htonl(0xc0000201), htonl(0xc0000202),
+			  htons(10000), htons(20000), 1000, 1, 64);
+	netmem_unreadable_add_frag(skb, env, 0, 64);
+	ret1 = napi_gro_frags(napi);
+
+	skb = napi_get_frags(napi);
+	if (!skb)
+		goto out_bh;
+	fill_tcp4_headers(skb, htonl(0xc0000201), htonl(0xc0000202),
+			  htons(10000), htons(20000), 1064, 1, 64);
+	netmem_unreadable_add_frag(skb, env, 64, 64);
+	ret2 = napi_gro_frags(napi);
+
+	skb = napi_get_frags(napi);
+	if (skb) {
+		readable = skb_frags_readable(skb);
+		pp_recycle = skb->pp_recycle;
+	}
+out_bh:
+	local_bh_enable();
+
+	KUNIT_ASSERT_NOT_NULL(test, skb);
+	KUNIT_EXPECT_EQ(test, ret1, GRO_HELD);
+	KUNIT_EXPECT_EQ(test, ret2, GRO_MERGED_FREE);
+	KUNIT_EXPECT_TRUE(test, readable);
+	KUNIT_EXPECT_FALSE(test, pp_recycle);
+
+	kunit_release_action(test, gro_test_napi_cleanup_action, ctx);
+	netmem_unreadable_env_check(test, env);
+}
 #endif
 #endif
 
@@ -1789,6 +2031,8 @@ static struct kunit_case net_test_cases[] = {
 	KUNIT_CASE(netmem_ref_test_try_coalesce),
 #ifdef CONFIG_INET
 	KUNIT_CASE(netmem_ref_test_tcp_devmem_recv),
+	KUNIT_CASE(netmem_unreadable_test_napi_pull_fail),
+	KUNIT_CASE(netmem_unreadable_test_napi_reuse),
 #endif
 #endif
 	{ },
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#12 lore raw

PATCH 8/10 net: kunit: test unreadable net_iov defrag and skb helpers

In reply to Octavian Purdila

Add KUnit regression tests in net_test covering mixed-readability rejection and uniformly unreadable reassembly across IPv4 and Netfilter IPv6 fragment reassembly, scatterlist conversion via skb_to_sgvec(), skb_send_sock_locked(), and strparser stream aborting, verifying that page_pool and binding reference counts remain balanced.

Assisted-by LLM
---
 net/core/net_test.c | 382 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 382 insertions(+)
net/core/net_test.c +382 -0
index b939cc93e9050..8da6c5ab4430c 100644
@@ -516,14 +516,23 @@ static void ip_tunnel_flags_test_run(struct kunit *test)
 /* Netmem & SKB fragment refcounting */
 
 #include <linux/etherdevice.h>
+#include <linux/ipv6.h>
+#include <linux/scatterlist.h>
 #include <linux/skbuff_ref.h>
 #include <net/gro.h>
 #include <net/ip.h>
+#if IS_REACHABLE(CONFIG_NF_DEFRAG_IPV6)
+#include <net/ipv6_frag.h>
+#include <net/netfilter/ipv6/nf_defrag_ipv6.h>
+#endif
 #include <net/netdev_lock.h>
 #include <net/netdev_rx_queue.h>
 #include <net/netmem.h>
 #include <net/page_pool/helpers.h>
 #include <net/page_pool/memory_provider.h>
+#if IS_REACHABLE(CONFIG_STREAM_PARSER)
+#include <net/strparser.h>
+#endif
 #include <net/tcp.h>
 #include "devmem.h"
 #include "netmem_priv.h"
@@ -1962,6 +1971,377 @@ static void netmem_unreadable_test_napi_reuse(struct kunit *test)
 	kunit_release_action(test, gro_test_napi_cleanup_action, ctx);
 	netmem_unreadable_env_check(test, env);
 }
+
+static struct sk_buff *alloc_ipv4_frag_skb(u16 id, u16 frag_off)
+{
+	struct sk_buff *skb;
+	struct iphdr *iph;
+
+	skb = alloc_skb(64, GFP_KERNEL);
+	if (!skb)
+		return NULL;
+
+	skb->dev = init_net.loopback_dev;
+	skb_reset_network_header(skb);
+	iph = skb_put_zero(skb, sizeof(*iph));
+	iph->version = 4;
+	iph->ihl = 5;
+	iph->ttl = 64;
+	iph->id = htons(id);
+	iph->protocol = IPPROTO_UDP;
+	iph->saddr = htonl(0x7f000001);
+	iph->daddr = htonl(0x7f000001);
+	iph->frag_off = htons(frag_off);
+	iph->tot_len = htons(sizeof(*iph) + 16);
+	ip_send_check(iph);
+	return skb;
+}
+
+#if IS_REACHABLE(CONFIG_NF_DEFRAG_IPV6)
+static struct sk_buff *alloc_ipv6_frag_skb(u32 id, u16 frag_off)
+{
+	struct frag_hdr *fhdr;
+	struct ipv6hdr *ip6h;
+	struct sk_buff *skb;
+
+	skb = alloc_skb(128, GFP_KERNEL);
+	if (!skb)
+		return NULL;
+
+	skb->dev = init_net.loopback_dev;
+	skb->protocol = htons(ETH_P_IPV6);
+	skb_reset_network_header(skb);
+	ip6h = skb_put_zero(skb, sizeof(*ip6h));
+	ip6h->version = 6;
+	ip6h->nexthdr = NEXTHDR_FRAGMENT;
+	ip6h->hop_limit = 64;
+	ip6h->saddr.s6_addr[15] = 1;
+	ip6h->daddr.s6_addr[15] = 1;
+	ip6h->payload_len = htons(sizeof(*fhdr) + 16);
+	skb_set_transport_header(skb, skb->len);
+	fhdr = skb_put_zero(skb, sizeof(*fhdr));
+	fhdr->nexthdr = IPPROTO_UDP;
+	fhdr->frag_off = htons(frag_off);
+	fhdr->identification = htonl(id);
+	return skb;
+}
+#endif
+
+static void netmem_unreadable_test_ip_defrag(struct kunit *test)
+{
+	struct netmem_unreadable_env *env = netmem_unreadable_env_init(test);
+	struct sk_buff *frag1, *frag2, *child;
+	int err;
+
+	/* IPv4: readable first fragment + unreadable second fragment must fail
+	 * and tear down the fragment queue.
+	 */
+	frag1 = alloc_ipv4_frag_skb(0x4242, IP_MF);
+	frag2 = alloc_ipv4_frag_skb(0x4242, 16 >> 3);
+	if (!frag1 || !frag2) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv4 frags");
+	}
+	skb_put_zero(frag1, 16);
+	netmem_unreadable_add_frag(frag2, env, 0, 16);
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag1, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	if (err != -EINPROGRESS) {
+		if (!err)
+			consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 ip_defrag failed: %d", err);
+	}
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag2, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	KUNIT_EXPECT_EQ(test, err, -EINVAL);
+	if (!err)
+		consume_skb(frag2);
+
+	/* IPv4: unreadable first fragment + readable second fragment must fail
+	 * and release the queued unreadable fragment cleanly.
+	 */
+	frag1 = alloc_ipv4_frag_skb(0x4243, IP_MF);
+	frag2 = alloc_ipv4_frag_skb(0x4243, 16 >> 3);
+	if (!frag1 || !frag2) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv4 frags");
+	}
+	netmem_unreadable_add_frag(frag1, env, 0, 16);
+	skb_put_zero(frag2, 16);
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag1, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	if (err != -EINPROGRESS) {
+		if (!err)
+			consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 ip_defrag failed: %d", err);
+	}
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag2, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	KUNIT_EXPECT_EQ(test, err, -EINVAL);
+	if (!err)
+		consume_skb(frag2);
+
+	/* IPv4: uniformly unreadable fragments reassemble cleanly. */
+	frag1 = alloc_ipv4_frag_skb(0x4244, IP_MF);
+	frag2 = alloc_ipv4_frag_skb(0x4244, 16 >> 3);
+	if (!frag1 || !frag2) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv4 frags");
+	}
+	netmem_unreadable_add_frag(frag1, env, 0, 16);
+	netmem_unreadable_add_frag(frag2, env, 0, 16);
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag1, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	if (err != -EINPROGRESS) {
+		if (!err)
+			consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 ip_defrag failed: %d", err);
+	}
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag2, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	KUNIT_EXPECT_EQ(test, err, 0);
+	if (!err) {
+		KUNIT_EXPECT_FALSE(test, skb_frags_readable(frag2));
+		KUNIT_EXPECT_EQ(test, frag2->len, sizeof(struct iphdr) + 32);
+		consume_skb(frag2);
+	}
+
+	/* IPv4: uniformly unreadable first fragment that already carries a
+	 * frag_list propagates unreadable to the wrapper skb.
+	 */
+	frag1 = alloc_ipv4_frag_skb(0x4245, IP_MF);
+	frag2 = alloc_ipv4_frag_skb(0x4245, 16 >> 3);
+	child = alloc_skb(0, GFP_KERNEL);
+	if (!frag1 || !frag2 || !child) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		consume_skb(child);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv4 frags");
+	}
+	netmem_unreadable_add_frag(child, env, 0, 16);
+	skb_shinfo(frag1)->frag_list = child;
+	frag1->data_len = child->len;
+	frag1->len += child->len;
+	frag1->truesize += child->truesize;
+	frag1->unreadable = 1;
+	netmem_unreadable_add_frag(frag2, env, 0, 16);
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag1, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	if (err != -EINPROGRESS) {
+		if (!err)
+			consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 ip_defrag failed: %d", err);
+	}
+
+	local_bh_disable();
+	err = ip_defrag(&init_net, frag2, IP_DEFRAG_LOCAL_DELIVER);
+	local_bh_enable();
+	KUNIT_EXPECT_EQ(test, err, 0);
+	if (!err) {
+		struct sk_buff *fl = skb_shinfo(frag2)->frag_list;
+
+		KUNIT_EXPECT_FALSE(test, skb_frags_readable(frag2));
+		KUNIT_EXPECT_NOT_NULL(test, fl);
+		if (fl)
+			KUNIT_EXPECT_FALSE(test, skb_frags_readable(fl));
+		consume_skb(frag2);
+	}
+
+#if IS_REACHABLE(CONFIG_NF_DEFRAG_IPV6)
+	/* Netfilter IPv6: mixed readability must fail and kill the queue. */
+	frag1 = alloc_ipv6_frag_skb(0x4242, IP6_MF);
+	frag2 = alloc_ipv6_frag_skb(0x4242, 16);
+	if (!frag1 || !frag2) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv6 frags");
+	}
+	netmem_unreadable_add_frag(frag1, env, 0, 16);
+	skb_put_zero(frag2, 16);
+
+	err = nf_ct_frag6_gather(&init_net, frag1, 0);
+	if (err != -EINPROGRESS) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 nf_ct_frag6_gather failed: %d",
+				     err);
+	}
+
+	err = nf_ct_frag6_gather(&init_net, frag2, 0);
+	KUNIT_EXPECT_EQ(test, err, -EINVAL);
+	if (err != -EINPROGRESS)
+		consume_skb(frag2);
+
+	/* Netfilter IPv6: uniformly unreadable fragments reassemble cleanly. */
+	frag1 = alloc_ipv6_frag_skb(0x4243, IP6_MF);
+	frag2 = alloc_ipv6_frag_skb(0x4243, 16);
+	if (!frag1 || !frag2) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "failed to allocate IPv6 frags");
+	}
+	netmem_unreadable_add_frag(frag1, env, 0, 16);
+	netmem_unreadable_add_frag(frag2, env, 0, 16);
+
+	err = nf_ct_frag6_gather(&init_net, frag1, 0);
+	if (err != -EINPROGRESS) {
+		consume_skb(frag1);
+		consume_skb(frag2);
+		KUNIT_FAIL_AND_ABORT(test, "frag1 nf_ct_frag6_gather failed: %d",
+				     err);
+	}
+
+	err = nf_ct_frag6_gather(&init_net, frag2, 0);
+	KUNIT_EXPECT_EQ(test, err, 0);
+	if (!err)
+		KUNIT_EXPECT_FALSE(test, skb_frags_readable(frag2));
+	if (err != -EINPROGRESS)
+		consume_skb(frag2);
+#endif
+
+	netmem_unreadable_env_check(test, env);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(sock_release_action, sock_release, struct socket *);
+KUNIT_DEFINE_ACTION_WRAPPER(consume_skb_action, consume_skb, struct sk_buff *);
+
+#if IS_REACHABLE(CONFIG_STREAM_PARSER)
+struct test_strp_ctx {
+	struct strparser strp;
+	struct socket *sock;
+};
+
+static void test_strp_ctx_cleanup(struct test_strp_ctx *ctx)
+{
+	strp_stop(&ctx->strp);
+	strp_done(&ctx->strp);
+	ctx->sock->sk->sk_state = TCP_CLOSE;
+	sock_release(ctx->sock);
+}
+
+KUNIT_DEFINE_ACTION_WRAPPER(test_strp_cleanup_act, test_strp_ctx_cleanup,
+			    struct test_strp_ctx *);
+
+static int dummy_strp_parse(struct strparser *strp, struct sk_buff *skb)
+{
+	return 64;
+}
+
+static void dummy_strp_rcv(struct strparser *strp, struct sk_buff *skb)
+{
+	kfree_skb(skb);
+}
+#endif
+
+static void netmem_unreadable_test_sg_and_strp(struct kunit *test)
+{
+	struct netmem_unreadable_env *env = netmem_unreadable_env_init(test);
+	struct scatterlist sg[4];
+	struct socket *tx_sock;
+	struct sk_buff *skb;
+	int ret;
+
+	skb = alloc_skb(32, GFP_KERNEL);
+	KUNIT_ASSERT_NOT_NULL(test, skb);
+	skb_put_zero(skb, 8);
+	netmem_unreadable_add_frag(skb, env, 0, 64);
+	KUNIT_ASSERT_EQ(test,
+			kunit_add_action_or_reset(test, consume_skb_action,
+						  skb),
+			0);
+
+	/* Linear prefix is readable; spanning into unreadable frag fails. */
+	sg_init_table(sg, ARRAY_SIZE(sg));
+	KUNIT_EXPECT_EQ(test, skb_to_sgvec(skb, sg, 0, 8), 1);
+	sg_init_table(sg, ARRAY_SIZE(sg));
+	KUNIT_EXPECT_EQ(test, skb_to_sgvec(skb, sg, 0, skb->len), -EFAULT);
+
+	KUNIT_ASSERT_EQ(test,
+			sock_create_kern(&init_net, AF_INET, SOCK_DGRAM,
+					 IPPROTO_UDP, &tx_sock),
+			0);
+	KUNIT_ASSERT_EQ(test,
+			kunit_add_action_or_reset(test, sock_release_action,
+						  tx_sock),
+			0);
+	lock_sock(tx_sock->sk);
+	ret = skb_send_sock_locked(tx_sock->sk, skb, 8, 64);
+	release_sock(tx_sock->sk);
+	KUNIT_EXPECT_EQ(test, ret, -EFAULT);
+	kunit_release_action(test, sock_release_action, tx_sock);
+	kunit_release_action(test, consume_skb_action, skb);
+
+#if IS_REACHABLE(CONFIG_STREAM_PARSER)
+	{
+		static const struct strp_callbacks cb = {
+			.parse_msg = dummy_strp_parse,
+			.rcv_msg = dummy_strp_rcv,
+		};
+		struct test_strp_ctx *ctx;
+
+		ctx = kunit_kzalloc(test, sizeof(*ctx), GFP_KERNEL);
+		KUNIT_ASSERT_NOT_NULL(test, ctx);
+		KUNIT_ASSERT_EQ(test,
+				sock_create_kern(&init_net, AF_INET,
+						 SOCK_STREAM, IPPROTO_TCP,
+						 &ctx->sock),
+				0);
+		ctx->sock->sk->sk_state = TCP_ESTABLISHED;
+		tcp_sk(ctx->sock->sk)->copied_seq = 0;
+		tcp_sk(ctx->sock->sk)->rcv_nxt = 64;
+		ret = strp_init(&ctx->strp, ctx->sock->sk, &cb);
+		if (ret) {
+			ctx->sock->sk->sk_state = TCP_CLOSE;
+			sock_release(ctx->sock);
+			KUNIT_ASSERT_EQ(test, ret, 0);
+		}
+		ret = kunit_add_action_or_reset(test, test_strp_cleanup_act,
+						ctx);
+		KUNIT_ASSERT_EQ(test, ret, 0);
+
+		skb = alloc_skb(0, GFP_KERNEL);
+		KUNIT_ASSERT_NOT_NULL(test, skb);
+		netmem_unreadable_add_frag(skb, env, 0, 64);
+		TCP_SKB_CB(skb)->seq = 0;
+		TCP_SKB_CB(skb)->end_seq = 64;
+		skb_queue_tail(&ctx->sock->sk->sk_receive_queue, skb);
+
+		local_bh_disable();
+		bh_lock_sock(ctx->sock->sk);
+		strp_data_ready(&ctx->strp);
+		bh_unlock_sock(ctx->sock->sk);
+		local_bh_enable();
+
+		KUNIT_EXPECT_TRUE(test, ctx->strp.stopped);
+		KUNIT_EXPECT_EQ(test, READ_ONCE(ctx->sock->sk->sk_err), EFAULT);
+
+		kunit_release_action(test, test_strp_cleanup_act, ctx);
+	}
+#endif
+
+	netmem_unreadable_env_check(test, env);
+}
 #endif
 #endif
 
@@ -2033,6 +2413,8 @@ static struct kunit_case net_test_cases[] = {
 	KUNIT_CASE(netmem_ref_test_tcp_devmem_recv),
 	KUNIT_CASE(netmem_unreadable_test_napi_pull_fail),
 	KUNIT_CASE(netmem_unreadable_test_napi_reuse),
+	KUNIT_CASE(netmem_unreadable_test_ip_defrag),
+	KUNIT_CASE(netmem_unreadable_test_sg_and_strp),
 #endif
 #endif
 	{ },
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#13 lore raw

PATCH 9/10 selftests: drv-net: devmem: test unreadable kTLS RX

In reply to Octavian Purdila

Add an end-to-end devmem.py protocol regression test covering unreadable net_iov skbs on kTLS TLS_RX decryption, verifying that configuring TLS_RX on a socket with queued unreadable devmem fragments rejects the record cleanly with EFAULT.

Bind the devmem RX queue directly from Python rather than via ncdevmem so the test owns the TCP socket and can install TLS_RX while unreadable devmem fragments are already queued.

Assisted-by LLM
---
 .../selftests/drivers/net/hw/devmem.py        |   9 +-
 .../selftests/drivers/net/hw/devmem_lib.py    | 210 +++++++++++++++++-
 .../drivers/net/hw/lib/py/__init__.py         |  11 +-
 3 files changed, 221 insertions(+), 9 deletions(-)
tools/testing/selftests/drivers/net/hw/devmem.py +7 -2
index 82c11ffc4add7..d15118060d261 100755
@@ -3,7 +3,7 @@
 
 from os import path
 from devmem_lib import (setup_test, run_rx, run_tx, run_tx_chunks, run_rx_hds,
-                        run_rx_large_niov)
+                        run_rx_large_niov, run_rx_tls)
 from lib.py import ksft_run, ksft_exit, ksft_disruptive
 from lib.py import NetDrvEpEnv
 
@@ -36,12 +36,17 @@ def check_rx_large_niov(cfg) -> None:
     run_rx_large_niov(cfg)
 
 
+def check_rx_tls(cfg) -> None:
+    """Run the unreadable kTLS RX test."""
+    run_rx_tls(cfg)
+
+
 def main() -> None:
     """Run the devmem test cases."""
     with NetDrvEpEnv(__file__) as cfg:
         setup_test(cfg, path.abspath(path.dirname(__file__) + "/ncdevmem"))
         ksft_run([check_rx, check_tx, check_tx_chunks, check_rx_hds,
-                  check_rx_large_niov],
+                  check_rx_large_niov, check_rx_tls],
                  args=(cfg,))
     ksft_exit()
 
tools/testing/selftests/drivers/net/hw/devmem_lib.py +208 -2

223 lines not on this page · raw message

tools/testing/selftests/drivers/net/hw/lib/py/__init__.py +6 -5
index 4077f5f1a1be3..1f7df889726aa 100644
@@ -29,8 +29,9 @@ try:
     from net.lib.py import KsftSkipEx, KsftFailEx, KsftXfailEx
     from net.lib.py import ksft_disruptive, ksft_exit, ksft_pr, ksft_run, \
         ksft_setup, ksft_variants, KsftNamedVariant
-    from net.lib.py import ksft_eq, ksft_ge, ksft_in, ksft_is, ksft_lt, \
-        ksft_ne, ksft_not_in, ksft_raises, ksft_true, ksft_gt, ksft_not_none
+    from net.lib.py import ksft_busy_wait, ksft_eq, ksft_ge, ksft_in, \
+        ksft_is, ksft_lt, ksft_ne, ksft_not_in, ksft_raises, ksft_true, \
+        ksft_gt, ksft_not_none
     from drivers.net.lib.py import set_ethtool_feat
     from drivers.net.lib.py import GenerateTraffic, Remote, Iperf3Runner
     from drivers.net.lib.py import NetDrvEnv, NetDrvEpEnv, NetDrvContEnv
@@ -46,9 +47,9 @@ try:
                "KsftSkipEx", "KsftFailEx", "KsftXfailEx",
                "ksft_disruptive", "ksft_exit", "ksft_pr", "ksft_run",
                "ksft_setup", "ksft_variants", "KsftNamedVariant",
-               "ksft_eq", "ksft_ge", "ksft_in", "ksft_is", "ksft_lt",
-               "ksft_ne", "ksft_not_in", "ksft_raises", "ksft_true", "ksft_gt",
-               "ksft_not_none", "ksft_not_none",
+               "ksft_busy_wait", "ksft_eq", "ksft_ge", "ksft_in", "ksft_is",
+               "ksft_lt", "ksft_ne", "ksft_not_in", "ksft_raises", "ksft_true",
+               "ksft_gt", "ksft_not_none", "ksft_not_none",
                "NetDrvEnv", "NetDrvEpEnv", "NetDrvContEnv", "GenerateTraffic",
                "Remote", "Iperf3Runner", "set_ethtool_feat"]
 except ModuleNotFoundError as e:
Signature · 1 line
2.56.1.915.g69d5ec7298-goog
#14 lore raw

PATCH 10/10 selftests: drv-net: devmem: test unreadable skbs on TCP-MD5 and TCP-AO

In reply to Octavian Purdila

Add an end-to-end devmem.py regression test covering TCP-MD5 and TCP-AO sockets, verifying that devmem TX sends on authenticated sockets and adding TCP-MD5 or TCP-AO keys with unreadable TX skbs queued are both rejected with EOPNOTSUPP, and incoming authenticated payload segments landing in unreadable net_iov fragments are dropped while incrementing TCPMD5Failure and TCPAOBad.

Assisted-by LLM
---
 tools/testing/selftests/drivers/net/hw/config |   2 +
 .../selftests/drivers/net/hw/devmem.py        |  12 +-
 .../selftests/drivers/net/hw/devmem_lib.py    | 245 +++++++++++++++++-
 3 files changed, 252 insertions(+), 7 deletions(-)
tools/testing/selftests/drivers/net/hw/config +2 -0
index 081155bf7847f..e63d2bf5a2953 100644
@@ -22,6 +22,8 @@ CONFIG_NET_IPIP=y
 CONFIG_NETKIT=y
 CONFIG_NET_SCH_INGRESS=y
 CONFIG_SYNC_FILE=y
+CONFIG_TCP_AO=y
+CONFIG_TCP_MD5SIG=y
 CONFIG_TLS=y
 CONFIG_TLS_DEVICE=y
 CONFIG_UDMABUF=y
tools/testing/selftests/drivers/net/hw/devmem.py +9 -3
index d15118060d261..d7e3915e4ee36 100755
@@ -3,8 +3,8 @@
 
 from os import path
 from devmem_lib import (setup_test, run_rx, run_tx, run_tx_chunks, run_rx_hds,
-                        run_rx_large_niov, run_rx_tls)
-from lib.py import ksft_run, ksft_exit, ksft_disruptive
+                        run_rx_large_niov, run_rx_tls, run_tcp_auth)
+from lib.py import ksft_run, ksft_exit, ksft_disruptive, ksft_variants
 from lib.py import NetDrvEpEnv
 
 
@@ -41,12 +41,18 @@ def check_rx_tls(cfg) -> None:
     run_rx_tls(cfg)
 
 
+@ksft_variants(["md5", "ao"])
+def check_tcp_auth(cfg, mode) -> None:
+    """Run the TCP-MD5 or TCP-AO unreadable RX/TX test."""
+    run_tcp_auth(cfg, mode)
+
+
 def main() -> None:
     """Run the devmem test cases."""
     with NetDrvEpEnv(__file__) as cfg:
         setup_test(cfg, path.abspath(path.dirname(__file__) + "/ncdevmem"))
         ksft_run([check_rx, check_tx, check_tx_chunks, check_rx_hds,
-                  check_rx_large_niov, check_rx_tls],
+                  check_rx_large_niov, check_rx_tls, check_tcp_auth],
                  args=(cfg,))
     ksft_exit()
 
tools/testing/selftests/drivers/net/hw/devmem_lib.py +241 -4

282 lines not on this page · raw message

Signature · 1 line
2.56.1.915.g69d5ec7298-goog
Reviewed-by on v1 1/10
#2 lore raw

Re: [PATCH net 01/10] net: gro: free skb on napi_frags_skb() header pull failure

In reply to Octavian Purdila
Quoted 17 lines · Octavian Purdila

When skb_gro_header_slow() fails to pull the Ethernet header in napi_frags_skb() (for instance on an unreadable net_iov fragment without a linear header, or a short/malformed packet), the skb still has its attached fragments (nr_frags > 0, data_len > 0). Calling napi_reuse_skb() on this error path caches the dirty skb back onto napi->skb without releasing or clearing its fragments, leaking attached fragments and corrupting length metadata for the next napi_get_frags() call. In addition, napi_gro_frags() passes the resulting NULL skb straight into trace_napi_gro_frags_entry() and dev_gro_receive(), dereferencing a NULL pointer.

Free the skb with kfree_skb() in napi_frags_skb() on header-pull failure so attached fragments are released cleanly with their original recycling state intact, and return GRO_CONSUMED in napi_gro_frags() when napi_frags_skb() returns NULL.

Quoted 8 lines

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes f55720680080 ("gro: Fix page ref count for skbs freed normally")
Fixes 1d11fa696733 ("net-gro: remove GRO_DROP")
Assisted-by LLM
Signature · 2 lines
Thanks,
Mina
Reviewed-by on v1 2/10
#4 lore raw

Re: [PATCH net 02/10] net: gro: reset unreadable and pp_recycle in napi_reuse_skb

In reply to Octavian Purdila
Quoted 29 lines · Octavian Purdila

Drivers using napi_get_frags() and napi_gro_frags() cache and reuse the fragmentless head sk_buff on napi->skb whenever incoming fragments merge into an existing GRO flow. When an unreadable or page_pool-backed skb is cached and later reused for a normal readable packet (for instance across queue reconfiguration or shared NAPI instances), leaving skb->unreadable and skb->pp_recycle set on napi->skb leaks stale state onto subsequent packets, corrupting readability checks and causing skb_gro_receive() to refuse GRO merges due to mismatched pp_recycle state.

Reset skb->unreadable and skb->pp_recycle in napi_reuse_skb().

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes 6a5bcd84e886 ("page_pool: Allow drivers to hint on SKB recycling")
Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 net/core/gro.c | 2 ++
 1 file changed, 2 insertions(+)
net/core/gro.c +0 -0
index fb730017fd3ba..c1d07bca1b4eb 100644

net/core/gro.c static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) · line 702

Quoted 6 lines
net/core/gro.c +2 -0
@@ -697,6 +697,8 @@ static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
 
        skb->encapsulation = 0;
        skb->ip_summed = CHECKSUM_NONE;
+       skb->unreadable = 0;
+       skb->pp_recycle = 0;
 

LGTM for napi_reuse_skb() — definitely needed (e.g. gve_rx_dqo can reuse napi_get_frags() on copy-on-demand without marking pp_recycle).

Side note while we're in gro.c: AFAICT skb_gro_receive() (and gro_list_prepare()) only checks p->pp_recycle != skb->pp_recycle and doesn't check skb_frags_readable(p) != skb_frags_readable(skb). If a shared NAPI or driver ever puts readable pp_recycle=1 and unreadable pp_recycle=1 segments of the same flow through GRO, they could still get merged into a mixed-readability skb/frag_list (which also matters for patch 5). Probably worth a separate check in skb_gro_receive() too.

Signature · 2 lines
Thanks,
Mina
#6 lore raw

Re: [PATCH net 03/10] inet: frag: reject fragments with mixed readability in reassembly

In reply to Octavian Purdila
Quoted 54 lines · Octavian Purdila

A reassembled skb keeps the head fragment's unreadable bit while chaining subsequent fragments onto frag_list. Upper-layer socket receive paths dispatch based on skb_frags_readable() of the head skb (for example, tcp_recvmsg_locked() routes to skb_copy_datagram_msg() vs. tcp_recvmsg_dmabuf()), and require uniform readability across all frag_list entries. Mixing readable and unreadable fragments in the same reassembly queue (for example, when fragments split across a devmem queue and a normal queue, or when a size-threshold spill places only larger fragments in unreadable netmem) produces a mixed-readability reassembled skb that fails on delivery with -EFAULT or -ENODEV.

Reject a fragment in inet_frag_queue_insert() when its readability differs from the fragments already in the queue, mirroring IPFRAG_MIXED_SECPATH, while continuing to allow uniformly unreadable fragments to reassemble. Also propagate head->unreadable to the wrapper skb allocated by inet_frag_reasm_prepare() when the first fragment already carries a frag_list.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying net_test KUnit tests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 include/net/inet_frag.h  |  1 +
 net/ipv4/inet_fragment.c | 13 ++++++++++---
 2 files changed, 11 insertions(+), 3 deletions(-)
include/net/inet_frag.h +1 -0
index c1fb419a9cf03..668cb042b36b3 100644
@@ -175,6 +175,7 @@ extern const u8 ip_frag_ecn_table[16];
 #define IPFRAG_OVERLAP 2
 #define IPFRAG_MIXED_SECPATH   3
 #define IPFRAG_TOOBIG  4
+#define IPFRAG_MIXED_READABILITY       5
 
 /* Max sum of the truesize of fragments in a queue. This keeps the sums
  * done in inet_frag_reasm_prepare() and inet_frag_reasm_finish() well
net/ipv4/inet_fragment.c +0 -3
index f330d0a398233..caf07cf1a411c 100644
@@ -444,9 +444,15 @@ int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb,
 {
        struct sk_buff *last = q->fragments_tail;
 
-       /* The reassembled skb keeps the first fragment's secpath. */
-       if (last && !secpath_equal(last, skb))
-               return IPFRAG_MIXED_SECPATH;

net/ipv4/inet_fragment.c int inet_frag_queue_insert(struct inet_frag_queue *q, struct sk_buff *skb, · line 456

Quoted 10 lines
net/ipv4/inet_fragment.c +9 -0
+       /* The reassembled skb keeps the first fragment's secpath and
+        * readability state.
+        */
+       if (last) {
+               if (!secpath_equal(last, skb))
+                       return IPFRAG_MIXED_SECPATH;
+               if (skb_frags_readable(last) != skb_frags_readable(skb))
+                       return IPFRAG_MIXED_READABILITY;
+       }
 

I think this regresses io_uring zcrx for fragmented TCP datagrams? Unlike tcp_recvmsg_locked() / tcp_recvmsg_dmabuf(), io_zcrx_recv_skb() -> io_zcrx_recv_frag() actually handles readability per-frag (it falls back to io_zcrx_copy_frag() when !skb_frag_is_net_iov(frag)) and walks frag_list recursively. I don't think we want to regress that.

Signature · 2 lines
Thanks,
Mina
#9 lore raw

Re: [PATCH net 05/10] tls: reject unreadable skbs in tls_strp

In reply to Octavian Purdila
Quoted 27 lines · Octavian Purdila

Software kTLS receive parses and decrypts TLS records on the CPU, assuming queued fragments are readable struct pages. If TLS_RX is configured on a socket with unreadable net_iov data in its receive queue (or receiving from a devmem RX queue), tls_strp either anchors unreadable skbs for CPU decryption or triggers warnings and advances skb lengths over uninitialized memory when skb_copy_bits() fails.

Reject unreadable skbs in tls_strp queue validation and abort stream parsing cleanly with -EFAULT if unreadable fragments are encountered.

This issue was found by code inspection while developing per-socket devmem RX and reproduced and verified with the accompanying devmem.py selftests.

Fixes 65249feb6b3d ("net: add support for skbs with unreadable frags")
Assisted-by LLM
---
 net/tls/tls_strp.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)
net/tls/tls_strp.c +0 -0
index 53d3256d0114a..a9e49384a1692 100644

net/tls/tls_strp.c tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb, · line 52

Quoted 8 lines
net/tls/tls_strp.c +5 -0
@@ -45,6 +45,11 @@ tls_strp_skb_copy(struct tls_strparser *strp, struct sk_buff *in_skb,
        struct sk_buff *skb;
        int i, err;
 
+       if (unlikely(!skb_frags_readable(in_skb))) {
+               tls_strp_abort_strp(strp, -EFAULT);
+               return NULL;
+       }
+

Honestly I think these explicity checks in relatively high-level functions, I don't like.

I think in this case for example the real problem is that skb_copy_bits is failing. Lets make this call site handle a generic failure instead of explicitily checking readability.

Probably something similar should happen in all the other callers. I think the call site should detect some skb helper failing, not checking explicitly for readability.

Signature · 2 lines
Thanks,
Mina