Skip to contentSkip to conversation

fou: reserve headroom before pushing the encap header

PATCH Applied Theodor Arsenij Larionov Trichkine 3 participants 3 messages Nov 16 – Nov 19

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

PATCH v3 1/1 fou: reserve headroom before pushing the encap header

ip_tunnel_xmit() and ip_md_tunnel_xmit() push the encap header before reserving headroom. A packet redirected into the tunnel (tc mirred, nft fwd/dup) keeps the headroom of its original device, so fou_build_udp() panics:

kernel BUG at net/core/skbuff.c:214!

  Call Trace:
   skb_push+0xcc/0xf0
   fou_build_udp+0x2f/0x380
   gue_build_header+0xfa/0x150
   ip_tunnel_xmit+0x87f/0x21c0

With a mirred mirror, the push overwrites the original packet instead. Reachable with CAP_NET_ADMIN in a user namespace.

Reserve the headroom in fou_build_header() and gue_build_header(); GUE sizes and builds the header from one copy of the encap fields. The skb head may move, so reload inner_iph after ip_tunnel_encap() and read tnl_params, which points into the head for GRE with header_ops, before the call. sit's ipip6_tunnel_xmit() holds no head pointer across it.

Fixes 56328486539d ("net: Changes to ip_tunnel to support foo-over-udp encapsulation")
Fixes ac931d4cdec3 ("ipip,ip_tunnel,sit: Add FOU support for externally controlled ipip devices")

---
v3: read the GUE encap fields one by one with READ_ONCE(), as the review of v2 asked; add Chengfeng's Reported-by.
v2: https://lore.kernel.org/netdev/[email protected]/

 net/ipv4/fou_core.c  | 19 +++++++++++++++----
 net/ipv4/ip_tunnel.c |  6 ++++--
 2 files changed, 19 insertions(+), 6 deletions(-)
net/ipv4/fou_core.c +15 -4
index 5c3c8f55d057..bff4dc488def 100644
@@ -1071,6 +1071,9 @@ static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
 	__be16 sport;
 	int err;
 
+	if (skb_cow_head(skb, fou_encap_hlen(e)))
+		return -ENOMEM;
+
 	err = __fou_build_header(skb, e, protocol, &sport, type);
 	if (err)
 		return err;
@@ -1083,16 +1086,24 @@ static int fou_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
 static int gue_build_header(struct sk_buff *skb, struct ip_tunnel_encap *e,
 			    u8 *protocol, struct flowi4 *fl4)
 {
-	int type = e->flags & TUNNEL_ENCAP_FLAG_CSUM ? SKB_GSO_UDP_TUNNEL_CSUM :
-						       SKB_GSO_UDP_TUNNEL;
+	struct ip_tunnel_encap encap = {
+		.flags = READ_ONCE(e->flags),
+		.sport = READ_ONCE(e->sport),
+		.dport = READ_ONCE(e->dport),
+	};
+	int type = encap.flags & TUNNEL_ENCAP_FLAG_CSUM ?
+		   SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
 	__be16 sport;
 	int err;
 
-	err = __gue_build_header(skb, e, protocol, &sport, type);
+	if (skb_cow_head(skb, gue_encap_hlen(&encap)))
+		return -ENOMEM;
+
+	err = __gue_build_header(skb, &encap, protocol, &sport, type);
 	if (err)
 		return err;
 
-	fou_build_udp(skb, e, fl4, protocol, sport);
+	fou_build_udp(skb, &encap, fl4, protocol, sport);
 
 	return 0;
 }
net/ipv4/ip_tunnel.c +4 -2
index 0875474a578a..1052af6245df 100644
@@ -610,6 +610,7 @@ void ip_md_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
 
 	if (ip_tunnel_encap(skb, &tun_info->encap, &proto, &fl4) < 0)
 		goto tx_error;
+	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
 
 	use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
 	if (use_cache)
@@ -753,6 +754,8 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
 	}
 
 	tos = tnl_params->tos;
+	df = tnl_params->frag_off;
+	ttl = tnl_params->ttl;
 	if (tos & 0x1) {
 		tos &= ~0x1;
 		if (payload_protocol == htons(ETH_P_IP)) {
@@ -771,6 +774,7 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
 
 	if (ip_tunnel_encap(skb, &tunnel->encap, &protocol, &fl4) < 0)
 		goto tx_error;
+	inner_iph = (const struct iphdr *)skb_inner_network_header(skb);
 
 	if (connected && md) {
 		use_cache = ip_tunnel_dst_cache_usable(skb, tun_info);
@@ -803,7 +807,6 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
 		goto tx_error;
 	}
 
-	df = tnl_params->frag_off;
 	if (payload_protocol == htons(ETH_P_IP) && !tunnel->ignore_df)
 		df |= (inner_iph->frag_off & htons(IP_DF));
 
@@ -825,7 +828,6 @@ void ip_tunnel_xmit(struct sk_buff *skb, struct net_device *dev,
 	}
 
 	tos = ip_tunnel_ecn_encap(tos, inner_iph, skb);
-	ttl = tnl_params->ttl;
 	if (ttl == 0) {
 		if (payload_protocol == htons(ETH_P_IP))
 			ttl = inner_iph->ttl;
Signature · 1 line
2.34.1
maintainer Reviewed-by on v3 1/1
#1 lore raw
Quoted 21 lines · Theodor Arsenij Larionov Trichkine

ip_tunnel_xmit() and ip_md_tunnel_xmit() push the encap header before reserving headroom. A packet redirected into the tunnel (tc mirred, nft fwd/dup) keeps the headroom of its original device, so fou_build_udp() panics:

kernel BUG at net/core/skbuff.c:214!

  Call Trace:
   skb_push+0xcc/0xf0
   fou_build_udp+0x2f/0x380
   gue_build_header+0xfa/0x150
   ip_tunnel_xmit+0x87f/0x21c0

With a mirred mirror, the push overwrites the original packet instead. Reachable with CAP_NET_ADMIN in a user namespace.

Reserve the headroom in fou_build_header() and gue_build_header(); GUE sizes and builds the header from one copy of the encap fields. The skb head may move, so reload inner_iph after ip_tunnel_encap() and read tnl_params, which points into the head for GRE with header_ops, before the call. sit's ipip6_tunnel_xmit() holds no head pointer across it.

Quoted 6 lines
Fixes 56328486539d ("net: Changes to ip_tunnel to support foo-over-udp encapsulation")
Fixes ac931d4cdec3 ("ipip,ip_tunnel,sit: Add FOU support for externally controlled ipip devices")
#2 lore raw
In reply to Theodor Arsenij Larionov Trichkine
This patch was applied to netdev/net.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 5 lines · On Mon, 16 Nov 2026 14:38:45 +0300 you wrote:

ip_tunnel_xmit() and ip_md_tunnel_xmit() push the encap header before reserving headroom. A packet redirected into the tunnel (tc mirred, nft fwd/dup) keeps the headroom of its original device, so fou_build_udp() panics:

Quoted 8 lines

kernel BUG at net/core/skbuff.c:214!

  Call Trace:
   skb_push+0xcc/0xf0
   fou_build_udp+0x2f/0x380
   gue_build_header+0xfa/0x150
   ip_tunnel_xmit+0x87f/0x21c0

[...]

Here is the summary with links:
- [net,v3] fou: reserve headroom before pushing the encap header

    https://git.kernel.org/netdev/net/c/bcb42672751a

You are awesome, thank you!

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