Skip to contentSkip to conversation

ipv4: validate header geometry before fragmentation

PATCH In review Lin Jiapeng 2 participants 2 messages Dec 2

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

PATCH v2 1/1 ipv4: validate header geometry before fragmentation

From: Lin Jiapeng <[email protected]>

Commit 6785011f8b16 ("ipv4: validate checksum_start before completing checksum") added a checksum-start check before skb_checksum_help(), but did not validate the IPv4 header it reads. TencentOS Corvus AI found an OVS fragmentation BUG with a PUSH_MPLS/TRUNC reproducer, and review of the first fix identified related checksum-order and network-offset cases.

That fix was incomplete. ip_do_fragment() completes CHECKSUM_PARTIAL before validating the fixed header or IHL, while its fragment helpers require skb->data to point to a complete linear IPv4 header. OVS prepare_frag() can also copy and pull more L2 bytes than remain after TRUNC. The resulting state->left underflow makes ip_frag_next() copy beyond the logical linear header and then hit BUG(). The enabled detector configuration records the failing path:
kernel BUG at net/ipv4/ip_output.c:725

  Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:ip_frag_next+0xa70/0xb10
  Call Trace:
   ip_do_fragment+0xdf0/0x1a30
   do_output+0x33c/0x5c0
   do_execute_actions+0x5d8e/0x76d0
   ovs_packet_cmd_execute+0xd57/0x14b0
  Kernel panic - not syncing: Fatal exception in interrupt

Validate the network offset, fixed header and IHL before checksum completion, retain the validated header length, and reload iph after the checksum helper. In OVS, reject negative or unpullable L2 offsets before prepare_frag() copies or pulls them. Valid packets continue through the existing checksum and fragmentation paths.

Reproduce with CONFIG_USER_NS, CONFIG_NET_NS, CONFIG_OPENVSWITCH and CONFIG_MPLS enabled. An unprivileged process creates a user+network namespace, gains CAP_NET_ADMIN only there, creates an OVS datapath, and executes a 60-byte IPv4 frame with PUSH_MPLS x4, TRUNC(45), OUTPUT and MRU=28. Boot with oops=panic panic=1 panic_on_warn=1; no race is required. The unfixed kernel panics in ip_frag_next(), while the fixed kernel drops the malformed packet and still fragments plain IPv4 with TRUNC(45) and MRU=28.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---

v1 -> v2:
- validate the fixed IPv4 header and IHL before CHECKSUM_PARTIAL
- require skb->data == skb_network_header() for the fragment helpers
- reload iph after skb_checksum_help()
- reject invalid OVS L2 geometry before prepare_frag()
- describe the pre-BUG read and remove the incorrect KASAN dependency
- include the panic/oops call trace for localization
- credit Paulos Yibelo with Suggested-by

v1: https://lore.kernel.org/netdev/[email protected]/

base-commit: 0d39bf81267861742b80d5f1c52e8a63e9ca0ca1

 net/ipv4/ip_output.c      | 27 +++++++++++++++++++--------
 net/openvswitch/actions.c |  7 +++++++
 2 files changed, 26 insertions(+), 8 deletions(-)
net/ipv4/ip_output.c +19 -8
index d42d776af273..365b9c8b9c9b 100644
@@ -770,21 +770,33 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	struct ip_frag_state state;
 	int err = 0;
 
-	/* for offloaded checksums cleanup checksum before fragmentation */
+	/*
+	 * Point into the IP datagram header. The fragmentation helpers use
+	 * skb->data as the header base, so reject any other network offset.
+	 */
+	if (unlikely(skb_network_offset(skb) != 0 ||
+		     skb_headlen(skb) < sizeof(struct iphdr))) {
+		err = -EINVAL;
+		goto fail;
+	}
+
+	iph = ip_hdr(skb);
+	hlen = iph->ihl * 4;
+	if (unlikely(hlen < sizeof(*iph) || hlen > skb_headlen(skb))) {
+		err = -EINVAL;
+		goto fail;
+	}
+
+	/* Complete offloaded checksums only after validating the IP header. */
 	if (skb->ip_summed == CHECKSUM_PARTIAL) {
 		err = ip_check_csum_start(skb);
 		if (!err)
 			err = skb_checksum_help(skb);
 		if (err)
 			goto fail;
+		iph = ip_hdr(skb);
 	}
 
-	/*
-	 *	Point into the IP datagram header.
-	 */
-
-	iph = ip_hdr(skb);
-
 	mtu = ip_skb_dst_mtu(sk, skb);
 	if (IPCB(skb)->frag_max_size && IPCB(skb)->frag_max_size < mtu)
 		mtu = IPCB(skb)->frag_max_size;
@@ -793,7 +805,6 @@ int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
 	 *	Setup starting values.
 	 */
 
-	hlen = iph->ihl * 4;
 	if (mtu < hlen + 8) {
 		err = -EMSGSIZE;
 		goto fail;
net/openvswitch/actions.c +7 -0
index 7691f262294d..b65304bb5ab8 100644
@@ -818,6 +818,13 @@ static void ovs_fragment(struct net *net, struct vport *vport,
 		goto err;
 	}
 
+	if (skb_network_offset(skb) < 0 ||
+	    skb_network_offset(skb) > skb_headlen(skb)) {
+		OVS_NLERR(1, "Invalid L2 header length for fragmentation");
+		kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_SMALL);
+		return;
+	}
+
 	if (key->eth.type == htons(ETH_P_IP)) {
 		struct rtable ovs_rt = { 0 };
 		unsigned long orig_dst;
maintainer
#1 lore raw
Quoted 62 lines · Lin Jiapeng

From: Lin Jiapeng <[email protected]>

Commit 6785011f8b16 ("ipv4: validate checksum_start before completing checksum") added a checksum-start check before skb_checksum_help(), but did not validate the IPv4 header it reads. TencentOS Corvus AI found an OVS fragmentation BUG with a PUSH_MPLS/TRUNC reproducer, and review of the first fix identified related checksum-order and network-offset cases.

That fix was incomplete. ip_do_fragment() completes CHECKSUM_PARTIAL before validating the fixed header or IHL, while its fragment helpers require skb->data to point to a complete linear IPv4 header. OVS prepare_frag() can also copy and pull more L2 bytes than remain after TRUNC. The resulting state->left underflow makes ip_frag_next() copy beyond the logical linear header and then hit BUG(). The enabled detector configuration records the failing path:
kernel BUG at net/ipv4/ip_output.c:725

  Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
  RIP: 0010:ip_frag_next+0xa70/0xb10
  Call Trace:
   ip_do_fragment+0xdf0/0x1a30
   do_output+0x33c/0x5c0
   do_execute_actions+0x5d8e/0x76d0
   ovs_packet_cmd_execute+0xd57/0x14b0
  Kernel panic - not syncing: Fatal exception in interrupt

Validate the network offset, fixed header and IHL before checksum completion, retain the validated header length, and reload iph after the checksum helper. In OVS, reject negative or unpullable L2 offsets before prepare_frag() copies or pulls them. Valid packets continue through the existing checksum and fragmentation paths.

Reproduce with CONFIG_USER_NS, CONFIG_NET_NS, CONFIG_OPENVSWITCH and CONFIG_MPLS enabled. An unprivileged process creates a user+network namespace, gains CAP_NET_ADMIN only there, creates an OVS datapath, and executes a 60-byte IPv4 frame with PUSH_MPLS x4, TRUNC(45), OUTPUT and MRU=28. Boot with oops=panic panic=1 panic_on_warn=1; no race is required. The unfixed kernel panics in ip_frag_next(), while the fixed kernel drops the malformed packet and still fragments plain IPv4 with TRUNC(45) and MRU=28.

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

---

v1 -> v2:
- validate the fixed IPv4 header and IHL before CHECKSUM_PARTIAL
- require skb->data == skb_network_header() for the fragment helpers
- reload iph after skb_checksum_help()
- reject invalid OVS L2 geometry before prepare_frag()
- describe the pre-BUG read and remove the incorrect KASAN dependency
- include the panic/oops call trace for localization
- credit Paulos Yibelo with Suggested-by

v1: https://lore.kernel.org/netdev/[email protected]/

base-commit: 0d39bf81267861742b80d5f1c52e8a63e9ca0ca1

 net/ipv4/ip_output.c      | 27 +++++++++++++++++++--------
 net/openvswitch/actions.c |  7 +++++++
 2 files changed, 26 insertions(+), 8 deletions(-)

[...]

Quoted 5 lines
net/openvswitch/actions.c +0 -0
index 7691f262294d..b65304bb5ab8 100644

net/openvswitch/actions.c static void ovs_fragment(struct net *net, struct vport *vport, · line 827

Quoted 10 lines
net/openvswitch/actions.c +7 -0
@@ -818,6 +818,13 @@ static void ovs_fragment(struct net *net, struct vport *vport,
 		goto err;
 	}
 
+	if (skb_network_offset(skb) < 0 ||
+	    skb_network_offset(skb) > skb_headlen(skb)) {
+		OVS_NLERR(1, "Invalid L2 header length for fragmentation");
+		kfree_skb_reason(skb, SKB_DROP_REASON_PKT_TOO_SMALL);
+		return;
+	}
+

This doesn't make a lot of sense to me. We should be able to trust the value in skb_network_offset(). It should not be set to negative in the first place.

Best regards, Ilya Maximets.