Skip to contentSkip to conversation

ip_gre: fix stale header length after erspan version change

PATCH In review Henry Martin 2 participants 2 messages Nov 23 – Nov 24

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

PATCH v2 1/1 ip_gre: fix stale header length after erspan version change

erspan_changelink() can switch an existing tunnel between erspan versions, but the GRE header length is only computed once in erspan_tunnel_init(). After downgrading a tunnel from version 1 or 2 to version 0, tun_hlen stays 8 while the version 0 transmit path clears the SEQ and KEY flags, so gre_build_header() writes only the 4-byte base header. The upper 4 bytes of the pushed header are uninitialized skb headroom and are sent to the tunnel peer with every packet. Switching in the other direction leaves tun_hlen at 4, so the version 1 transmit path writes the GRE sequence field over the ERSPAN base header, and the ERSPAN header grows without growing needed_headroom.

Triggered on v7.4-rc3 with a version 1 tunnel switched to version 0:
the four bytes above the GRE base header carried the heap marker sprayed into skb headroom instead of the GRE sequence number, in 1056/1550 and 744/1552 frames over two independent boots, while the version 1 baseline carried the expected 1..65 sequence.

Recompute the header lengths in erspan_changelink() on every change, mirroring what the IPv6 side does in ip6erspan_tnl_change(). The old length has to be sampled before ipgre_newlink_encap_setup(), which resets t->hlen to tun_hlen + encap_hlen, and the delta propagated to needed_headroom and the MTU.

This issue was discovered by Tencent CodeBuddy Security.

Fixes f989d546a2d5 ("erspan: Add type I version 0 support.")

---
v1 -> v2: sample hlen before ipgre_newlink_encap_setup() and recompute it
on every changelink. v1 sampled it after the encap setup had already reset t->hlen, so the delta was always the new ERSPAN header length and every "ip link set" on a version 1 device shrank the MTU by 8 and grew needed_headroom by 8 (reported by Haishuang Yan). Add the runtime trigger observations.

 net/ipv4/ip_gre.c | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)
net/ipv4/ip_gre.c +20 -2
index 5485586fa0e6..2a1d6ff16dfe 100644
@@ -1346,28 +1346,35 @@ static const struct net_device_ops gre_tap_netdev_ops = {
 	.ndo_set_mac_address 	= eth_mac_addr,
 	.ndo_validate_addr	= eth_validate_addr,
 	.ndo_change_mtu		= ip_tunnel_change_mtu,
 	.ndo_get_stats64	= dev_get_tstats64,
 	.ndo_get_iflink		= ip_tunnel_get_iflink,
 	.ndo_fill_metadata_dst	= gre_fill_metadata_dst,
 };
 
-static int erspan_tunnel_init(struct net_device *dev)
+static void erspan_calc_hlen(struct net_device *dev)
 {
 	struct ip_tunnel *tunnel = netdev_priv(dev);
 
 	if (tunnel->erspan_ver == 0)
 		tunnel->tun_hlen = 4; /* 4-byte GRE hdr. */
 	else
 		tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */
 
-	tunnel->parms.iph.protocol = IPPROTO_GRE;
 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
 		       erspan_hdr_len(tunnel->erspan_ver);
+}
+
+static int erspan_tunnel_init(struct net_device *dev)
+{
+	struct ip_tunnel *tunnel = netdev_priv(dev);
+
+	erspan_calc_hlen(dev);
+	tunnel->parms.iph.protocol = IPPROTO_GRE;
 
 	dev->features		|= GRE_FEATURES;
 	dev->hw_features	|= GRE_FEATURES;
 	dev->priv_flags		|= IFF_LIVE_ADDR_CHANGE;
 	dev->lltx = true;
 	netif_keep_dst(dev);
 
 	return ip_tunnel_init(dev);
@@ -1494,41 +1501,52 @@ static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[],
 static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
 			     struct nlattr *data[],
 			     struct netlink_ext_ack *extack)
 {
 	struct ip_tunnel *t = netdev_priv(dev);
 	struct ip_tunnel_parm_kern p;
 	__u32 fwmark = t->fwmark;
 	int err;
+	int hlen;
 
 	if (!rtnl_dev_link_net_capable(dev, t->net))
 		return -EPERM;
 
 	if (data && data[IFLA_GRE_COLLECT_METADATA] && !t->collect_md) {
 		NL_SET_ERR_MSG(extack,
 			       "Enabling collect_md on an existing device is not supported");
 		return -EOPNOTSUPP;
 	}
 
+	hlen = t->hlen;
+
 	err = ipgre_newlink_encap_setup(dev, data);
 	if (err)
 		return err;
 
 	err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
 	if (err < 0)
 		return err;
 
 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
 	if (err < 0)
 		return err;
 
 	ip_tunnel_flags_copy(t->parms.i_flags, p.i_flags);
 	ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags);
 
+	erspan_calc_hlen(dev);
+	hlen = t->hlen - hlen;
+	if (hlen) {
+		dev->needed_headroom += hlen;
+		if (!tb[IFLA_MTU])
+			WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - hlen, 68));
+	}
+
 	return 0;
 }
 
 static size_t ipgre_get_size(const struct net_device *dev)
 {
 	return
 		/* IFLA_GRE_LINK */
 		nla_total_size(4) +
Signature · 1 line
2.43.7
#1 lore raw

net/ipv4/ip_gre.c static int ipgre_changelink(struct net_device *dev, struct nlattr *tb[], · line 1538

Quoted 2 lines · Henry Martin
net/ipv4/ip_gre.c +2 -0
+	erspan_calc_hlen(dev);
+	hlen = t->hlen - hlen;

Thanks. Version changes and repeated ttl changes are fine with v2, but my suggestion missed two cases, sorry:

- If changelink fails after the encap setup, t->hlen stays reset, so the next good one lowers the MTU again. On ver 2, alternating erspan_hwid 0xff (-EINVAL) and 7 gives 1446, 1434, 1422, ... It stays at 1446 without the patch.

- When the link changes, ip_tunnel_changelink() calls ip_tunnel_bind_dev() with the reset t->hlen: "dev va" on ver 1 gives MTU 1458 instead of 1450 (same without the patch).

Moving the block right after erspan_netlink_parms(), before checking err, fixes both in my tests.

The changelog also says the sequence number is written over the ERSPAN base header; it is the GRE base header.