Skip to contentSkip to conversation

ip_gre: validate csum_start only on pull

PATCH Applied Willem de Bruijn 4 participants 5 messages Sep 5, 2021 – Sep 6, 2021

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

PATCH 1/1 ip_gre: validate csum_start only on pull

From: Willem de Bruijn <[email protected]>

The GRE tunnel device can pull existing outer headers in ipge_xmit. This is a rare path, apparently unique to this device. The below commit ensured that pulling does not move skb->data beyond csum_start.

But it has a false positive if ip_summed is not CHECKSUM_PARTIAL and thus csum_start is irrelevant.

Refine to exclude this. At the same time simplify and strengthen the test.

Simplify, by moving the check next to the offending pull, making it more self documenting and removing an unnecessary branch from other code paths.

Strengthen, by also ensuring that the transport header is correct and therefore the inner headers will be after skb_reset_inner_headers. The transport header is set to csum_start in skb_partial_csum_set.

---
 net/ipv4/ip_gre.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/ipv4/ip_gre.c +6 -3
index 177d26d8fb9c..0fe6c936dc54 100644
@@ -473,8 +473,6 @@ static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
 
 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
 {
-	if (csum && skb_checksum_start(skb) < skb->data)
-		return -EINVAL;
 	return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
 }
 
@@ -632,15 +630,20 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
 	}
 
 	if (dev->header_ops) {
+		const int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
 		if (skb_cow_head(skb, 0))
 			goto free_skb;
 
 		tnl_params = (const struct iphdr *)skb->data;
 
+		if (pull_len > skb_transport_offset(skb))
+			goto free_skb;
+
 		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
 		 * to gre header.
 		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		skb_pull(skb, pull_len);
 		skb_reset_mac_header(skb);
 	} else {
 		if (skb_cow_head(skb, dev->needed_headroom))
Signature · 1 line
2.33.0.153.gba50c8fa24-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 66 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

The GRE tunnel device can pull existing outer headers in ipge_xmit. This is a rare path, apparently unique to this device. The below commit ensured that pulling does not move skb->data beyond csum_start.

But it has a false positive if ip_summed is not CHECKSUM_PARTIAL and thus csum_start is irrelevant.

Refine to exclude this. At the same time simplify and strengthen the test.

Simplify, by moving the check next to the offending pull, making it more self documenting and removing an unnecessary branch from other code paths.

Strengthen, by also ensuring that the transport header is correct and therefore the inner headers will be after skb_reset_inner_headers. The transport header is set to csum_start in skb_partial_csum_set.

---
 net/ipv4/ip_gre.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)
net/ipv4/ip_gre.c +6 -4
index 177d26d8fb9c..0fe6c936dc54 100644
@@ -473,8 +473,6 @@ static void __gre_xmit(struct sk_buff *skb, struct net_device *dev,
 
 static int gre_handle_offloads(struct sk_buff *skb, bool csum)
 {
-       if (csum && skb_checksum_start(skb) < skb->data)
-               return -EINVAL;
        return iptunnel_handle_offloads(skb, csum ? SKB_GSO_GRE_CSUM : SKB_GSO_GRE);
 }
 
@@ -632,15 +630,20 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
        }
 
        if (dev->header_ops) {
+               const int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
                if (skb_cow_head(skb, 0))
                        goto free_skb;
 
                tnl_params = (const struct iphdr *)skb->data;
 
+               if (pull_len > skb_transport_offset(skb))
+                       goto free_skb;
+
                /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
                 * to gre header.
                 */
-               skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+               skb_pull(skb, pull_len);
                skb_reset_mac_header(skb);
        } else {
                if (skb_cow_head(skb, dev->needed_headroom))
--
Quoted 2 lines

2.33.0.153.gba50c8fa24-goog

Looks good to me.

#2 lore raw
Quoted 3 lines · Alexander Duyck

Looks good to me.

Thanks Willem and Alex! Applied the patch to my tree. Will let you know tomorrow morning after regression is complete (though I'm sure it's fine).

#3 lore raw
In reply to Willem de Bruijn
This patch was applied to netdev/net.git (refs/heads/master):

Hello:

This patch was applied to netdev/net.git (refs/heads/master):

Quoted 10 lines · On Sun, 5 Sep 2021 11:21:09 -0400 you wrote:

From: Willem de Bruijn <[email protected]>

The GRE tunnel device can pull existing outer headers in ipge_xmit. This is a rare path, apparently unique to this device. The below commit ensured that pulling does not move skb->data beyond csum_start.

But it has a false positive if ip_summed is not CHECKSUM_PARTIAL and thus csum_start is irrelevant.

[...]

Here is the summary with links:
- [net] ip_gre: validate csum_start only on pull

    https://git.kernel.org/netdev/net/c/8a0ed250f911

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html

Tested-by on v1 1/1
#4 lore raw
In reply to Willem de Bruijn
Quoted 20 lines · Willem de Bruijn

From: Willem de Bruijn <[email protected]>

The GRE tunnel device can pull existing outer headers in ipge_xmit. This is a rare path, apparently unique to this device. The below commit ensured that pulling does not move skb->data beyond csum_start.

But it has a false positive if ip_summed is not CHECKSUM_PARTIAL and thus csum_start is irrelevant.

Refine to exclude this. At the same time simplify and strengthen the test.

Simplify, by moving the check next to the offending pull, making it more self documenting and removing an unnecessary branch from other code paths.

Strengthen, by also ensuring that the transport header is correct and therefore the inner headers will be after skb_reset_inner_headers. The transport header is set to csum_start in skb_partial_csum_set.

Quoted 5 lines

FTR:

Thanks