Skip to contentSkip to conversation

ipv4: ip_gre: Avoid skb_pull() failure in ipgre_xmit()

PATCH Applied Shigeru Yoshida 4 participants 8 messages Dec 2, 2023 – Dec 6, 2023

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

PATCH v2 1/1 ipv4: ip_gre: Avoid skb_pull() failure in ipgre_xmit()

In ipgre_xmit(), skb_pull() may fail even if pskb_inet_may_pull() returns true. For example, applications can use PF_PACKET to create a malformed packet with no IP header. This type of packet causes a problem such as uninit-value access.

This patch ensures that skb_pull() can pull the required size by checking the skb with pskb_network_may_pull() before skb_pull().

Fixes c54419321455 ("GRE: Refactor GRE tunneling code.")

---
v1 -> v2:
- Change the title
- Update the code with Eric's suggestion
https://lore.kernel.org/all/[email protected]/

---
 net/ipv4/ip_gre.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/ipv4/ip_gre.c +7 -4
index 22a26d1d29a0..5169c3c72cff 100644
@@ -635,15 +635,18 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
 	}
 
 	if (dev->header_ops) {
+		int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
 		if (skb_cow_head(skb, 0))
 			goto free_skb;
 
 		tnl_params = (const struct iphdr *)skb->data;
 
-		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-		 * to gre header.
-		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		if (!pskb_network_may_pull(skb, pull_len))
+			goto free_skb;
+
+		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
+		skb_pull(skb, pull_len);
 		skb_reset_mac_header(skb);
 
 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
Signature · 1 line
2.41.0
#1 lore raw

Hi Shigeru,

Quoted 10 lines
net/ipv4/ip_gre.c index renamed from net/ipv4/ip_gre.c +0 -0

22a26d1d29a0..5169c3c72cff 100644

net/ipv4/ip_gre.c +2 -0
@@ -635,15 +635,18 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
 	}
 
 	if (dev->header_ops) {
+		int pull_len = tunnel->hlen + sizeof(struct iphdr);
+

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 645

Quoted 10 lines
net/ipv4/ip_gre.c +1 -4
 		if (skb_cow_head(skb, 0))
 			goto free_skb;
 
 		tnl_params = (const struct iphdr *)skb->data;
 
-		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-		 * to gre header.
-		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		if (!pskb_network_may_pull(skb, pull_len))

[Suman] Since this is transmit path, should we add unlikely() here?

Quoted 10 lines
net/ipv4/ip_gre.c +4 -1
+			goto free_skb;
+
+		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
+		skb_pull(skb, pull_len);
 		skb_reset_mac_header(skb);
 
 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
--

2.41.0

Reviewed-by
#2 lore raw
Quoted 16 lines · Suman Ghosh

Hi Shigeru,

Quoted 13 lines
net/ipv4/ip_gre.c index renamed from net/ipv4/ip_gre.c +0 -0

22a26d1d29a0..5169c3c72cff 100644

net/ipv4/ip_gre.c +2 -0
@@ -635,15 +635,18 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
      }
 
      if (dev->header_ops) {
+              int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
              if (skb_cow_head(skb, 0))
                      goto free_skb;
 
Quoted 8 lines

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 645

Quoted 7 lines
net/ipv4/ip_gre.c +1 -4
              tnl_params = (const struct iphdr *)skb->data;
 
-              /* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-               * to gre header.
-               */
-              skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+              if (!pskb_network_may_pull(skb, pull_len))

[Suman] Since this is transmit path, should we add unlikely() here?

Adding unlikely() is not needed, it is already done generically from the inline helpers.

#3 lore raw
In reply to Suman Ghosh

Hi Suman,

Quoted 15 lines · Suman Ghosh

Hi Shigeru,

Quoted 13 lines
net/ipv4/ip_gre.c index renamed from net/ipv4/ip_gre.c +0 -0

22a26d1d29a0..5169c3c72cff 100644

net/ipv4/ip_gre.c +2 -0
@@ -635,15 +635,18 @@ static netdev_tx_t ipgre_xmit(struct sk_buff *skb,
 	}
 
 	if (dev->header_ops) {
+		int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
 		if (skb_cow_head(skb, 0))
 			goto free_skb;
 
Quoted 8 lines

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 645

Quoted 7 lines
net/ipv4/ip_gre.c +1 -4
 		tnl_params = (const struct iphdr *)skb->data;
 
-		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-		 * to gre header.
-		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		if (!pskb_network_may_pull(skb, pull_len))

[Suman] Since this is transmit path, should we add unlikely() here?

Thanks for your comment.

I traced this function and found that pskb_may_pull_reason() seems to have appropriate likely() and unlikely() as Eric says.

I'm new to Linux networking. Could you kindly explain the background of your suggestion?

I understand that a transmit path must be as fast as possible, so we should use unlikely() for rare cases such like this error path. Am I correct?

Thanks,
Shigeru

Quoted 11 lines
Quoted 10 lines
net/ipv4/ip_gre.c +4 -1
+			goto free_skb;
+
+		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
+		skb_pull(skb, pull_len);
 		skb_reset_mac_header(skb);
 
 		if (skb->ip_summed == CHECKSUM_PARTIAL &&
--

2.41.0

#4 lore raw
Quoted 25 lines
Quoted 8 lines
Quoted 8 lines
	}

	if (dev->header_ops) {
(quoted diff) +2 -0
+		int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
		if (skb_cow_head(skb, 0))
			goto free_skb;
Quoted 8 lines

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 645

Quoted 7 lines
		tnl_params = (const struct iphdr *)skb->data;
net/ipv4/ip_gre.c +1 -4
-		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-		 * to gre header.
-		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		if (!pskb_network_may_pull(skb, pull_len))

[Suman] Since this is transmit path, should we add unlikely() here?

Thanks for your comment.

I traced this function and found that pskb_may_pull_reason() seems to have appropriate likely() and unlikely() as Eric says.

I'm new to Linux networking. Could you kindly explain the background of your suggestion?

Quoted 6 lines

I understand that a transmit path must be as fast as possible, so we should use unlikely() for rare cases such like this error path. Am I correct?

Thanks,
Shigeru

[Suman] Yes. Likely()/unlikely() helps the compiler for branch prediction and we use it mostly on the data path. But I cross checked that this is static inline and the function pskb_may_pull() already have likely()/unlikely() in place. So, you can ignore my comment here.

Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
net/ipv4/ip_gre.c +4 -0
+			goto free_skb;
+
+		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
+		skb_pull(skb, pull_len);
		skb_reset_mac_header(skb);

		if (skb->ip_summed == CHECKSUM_PARTIAL &&

--
2.41.0

#5 lore raw
Quoted 29 lines · Suman Ghosh
Quoted 29 lines
Quoted 8 lines
Quoted 8 lines
	}

	if (dev->header_ops) {
(quoted diff) +2 -0
+		int pull_len = tunnel->hlen + sizeof(struct iphdr);
+
		if (skb_cow_head(skb, 0))
			goto free_skb;
Quoted 8 lines

net/ipv4/ip_gre.c static netdev_tx_t ipgre_xmit(struct sk_buff *skb, · line 645

Quoted 7 lines
		tnl_params = (const struct iphdr *)skb->data;
net/ipv4/ip_gre.c +1 -4
-		/* Pull skb since ip_tunnel_xmit() needs skb->data pointing
-		 * to gre header.
-		 */
-		skb_pull(skb, tunnel->hlen + sizeof(struct iphdr));
+		if (!pskb_network_may_pull(skb, pull_len))

[Suman] Since this is transmit path, should we add unlikely() here?

Thanks for your comment.

I traced this function and found that pskb_may_pull_reason() seems to have appropriate likely() and unlikely() as Eric says.

I'm new to Linux networking. Could you kindly explain the background of your suggestion?

I understand that a transmit path must be as fast as possible, so we should use unlikely() for rare cases such like this error path. Am I correct?

Quoted 5 lines
Quoted 2 lines

Thanks,
Shigeru

[Suman] Yes. Likely()/unlikely() helps the compiler for branch prediction and we use it mostly on the data path. But I cross checked that this is static inline and the function pskb_may_pull() already have likely()/unlikely() in place. So, you can ignore my comment here.

Thank you for your explanation. It is very informative. And thanks for the review as well.

Shigeru

Quoted 13 lines
Quoted 12 lines
Quoted 12 lines
Quoted 11 lines
net/ipv4/ip_gre.c +4 -0
+			goto free_skb;
+
+		/* ip_tunnel_xmit() needs skb->data pointing to gre header. */
+		skb_pull(skb, pull_len);
		skb_reset_mac_header(skb);

		if (skb->ip_summed == CHECKSUM_PARTIAL &&

--
2.41.0

Reviewed-by on v2 1/1
#6 lore raw
In reply to Shigeru Yoshida
Quoted 11 lines

In ipgre_xmit(), skb_pull() may fail even if pskb_inet_may_pull() returns true. For example, applications can use PF_PACKET to create a malformed packet with no IP header. This type of packet causes a problem such as uninit-value access.

This patch ensures that skb_pull() can pull the required size by checking the skb with pskb_network_may_pull() before skb_pull().

Fixes c54419321455 ("GRE: Refactor GRE tunneling code.")

---

Quoted 4 lines

v1 -> v2:
- Change the title
- Update the code with Eric's suggestion

#7 lore raw
In reply to Shigeru Yoshida
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 9 lines · On Sun, 3 Dec 2023 01:14:41 +0900 you wrote:

In ipgre_xmit(), skb_pull() may fail even if pskb_inet_may_pull() returns true. For example, applications can use PF_PACKET to create a malformed packet with no IP header. This type of packet causes a problem such as uninit-value access.

This patch ensures that skb_pull() can pull the required size by checking the skb with pskb_network_may_pull() before skb_pull().

[...]

Here is the summary with links:
- [net,v2] ipv4: ip_gre: Avoid skb_pull() failure in ipgre_xmit()

    https://git.kernel.org/netdev/net/c/80d875cfc9d3

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html