Skip to contentSkip to conversation

net: fix stale header length after erspan version change

PATCH New Henry Martin 2 participants 2 messages Dec 3 – Dec 4

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

PATCH v5 1/1 net: 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 a downgrade to version 0, tun_hlen stays 8 while the version 0 transmit path clears the SEQ and KEY flags, so the upper 4 bytes of every pushed header are uninitialized skb headroom sent to the tunnel peer. An upgrade leaves tun_hlen at 4, so the version 1 transmit path writes the GRE sequence field over the GRE base header, and the ERSPAN header grows without growing needed_headroom.

Triggered 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.

Recompute the header lengths in erspan_changelink() on every change, mirroring the IPv6 side. Sample the old length before ipgre_newlink_encap_setup() resets t->hlen, recompute after erspan_netlink_parms(), and restore the old length, version, index, direction and hwid on every error path, so a rejected change leaves the device unchanged. Apply the
needed_headroom/mtu delta only when the link did not change, since ip_tunnel_bind_dev() recomputes both when it did.

erspan_xmit() always sets the sequence flag for v1/v2: those versions emit an 8-byte GRE header, and a device created with only IFLAGS=SEQ|KEY passes erspan_validate() while o_flags carries no SEQ, in which case the sequence word would never be written.

Verified on v7.4-rc5 (KASAN, uid 1000): unpatched, 704 post-switch frames carry the sprayed heap marker; with the patch the tag hits drop to 0, and a failed changelink no longer changes the MTU.

This issue was discovered by Tencent CodeBuddy Security.

---
v4 -> v5: set the sequence flag for v1/v2 in erspan_xmit(); adjust
needed_headroom/mtu only after a successful changelink and skip the delta when ip_tunnel_bind_dev() already recomputed them; restore t->hlen on every error path; limit IFLA_GRE_ERSPAN_VER to 0..2 in the netlink policy; use READ_ONCE()/WRITE_ONCE() for needed_headroom. From sashiko review.
v3 -> v4: fix the placement of the int hlen; declaration: the hunk
matched the identical declaration block at the top of ipgre_changelink(), leaving an unused variable there and breaking the build of erspan_changelink() with -Werror. Declare it in erspan_changelink() and order the declarations per reverse christmas tree. No functional change.
v2 -> v3: move the recompute block right after erspan_netlink_parms()
and run it before checking its error (reported by Haishuang Yan on v2). Fix the changelog wording: the sequence number overwrites the GRE base header, not the ERSPAN base header.
v1 -> v2: sample hlen before ipgre_newlink_encap_setup() and recompute it
on every changelink (reported by Haishuang Yan).

---
 net/ipv4/ip_gre.c | 55 ++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 50 insertions(+), 5 deletions(-)
net/ipv4/ip_gre.c +50 -5
index 5485586fa0e6..4d7b43b1a0f9 100644
@@ -744,16 +744,19 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb,
 				       tunnel->dir, tunnel->hwid,
 				       truncate, true);
 		proto = htons(ETH_P_ERSPAN2);
 	} else {
 		goto free_skb;
 	}
 
 	__clear_bit(IP_TUNNEL_KEY_BIT, flags);
+	/* v1/v2 always emit an 8-byte GRE header with a sequence word. */
+	if (tunnel->erspan_ver)
+		__set_bit(IP_TUNNEL_SEQ_BIT, flags);
 	__gre_xmit(skb, dev, &tunnel->parms.iph, proto, flags);
 	return NETDEV_TX_OK;
 
 free_skb:
 	kfree_skb(skb);
 	DEV_STATS_INC(dev, tx_dropped);
 	return NETDEV_TX_OK;
 }
@@ -1346,28 +1349,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);
@@ -1493,38 +1503,73 @@ 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;
+	u32 old_index = t->index;
+	u8 old_ver = t->erspan_ver;
+	u8 old_dir = t->dir;
+	u8 old_hwid = t->hwid;
+	bool relink;
+	int hlen;
 	int err;
 
 	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)
+	if (err < 0) {
+		/* A rejected change must not touch the device. */
+		t->erspan_ver = old_ver;
+		t->index = old_index;
+		t->dir = old_dir;
+		t->hwid = old_hwid;
+		t->hlen = hlen;
 		return err;
+	}
+
+	relink = t->parms.link != p.link || t->fwmark != fwmark;
+
+	/*
+	 * ipgre_newlink_encap_setup() reset t->hlen without the ERSPAN part.
+	 * Recompute it before ip_tunnel_changelink(): ip_tunnel_bind_dev()
+	 * consumes it when the link changes.
+	 */
+	erspan_calc_hlen(dev);
 
 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
-	if (err < 0)
+	if (err < 0) {
+		t->hlen = hlen;
 		return err;
+	}
+
+	hlen = t->hlen - hlen;
+	if (hlen && !relink) {
+		int headroom = READ_ONCE(dev->needed_headroom) + hlen;
+
+		WRITE_ONCE(dev->needed_headroom, max_t(int, headroom, 0));
+		if (!tb[IFLA_MTU])
+			WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - hlen, 68));
+	}
 
 	ip_tunnel_flags_copy(t->parms.i_flags, p.i_flags);
 	ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags);
 
 	return 0;
 }
 
 static size_t ipgre_get_size(const struct net_device *dev)
@@ -1678,17 +1723,17 @@ static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
 	[IFLA_GRE_ENCAP_TYPE]	= { .type = NLA_U16 },
 	[IFLA_GRE_ENCAP_FLAGS]	= { .type = NLA_U16 },
 	[IFLA_GRE_ENCAP_SPORT]	= { .type = NLA_U16 },
 	[IFLA_GRE_ENCAP_DPORT]	= { .type = NLA_U16 },
 	[IFLA_GRE_COLLECT_METADATA]	= { .type = NLA_FLAG },
 	[IFLA_GRE_IGNORE_DF]	= { .type = NLA_U8 },
 	[IFLA_GRE_FWMARK]	= { .type = NLA_U32 },
 	[IFLA_GRE_ERSPAN_INDEX]	= { .type = NLA_U32 },
-	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
+	[IFLA_GRE_ERSPAN_VER]	= NLA_POLICY_MAX(NLA_U8, 2),
 	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
 	[IFLA_GRE_ERSPAN_HWID]	= { .type = NLA_U16 },
 };
 
 static struct rtnl_link_ops ipgre_link_ops __read_mostly = {
 	.kind		= "gre",
 	.maxtype	= IFLA_GRE_MAX,
 	.policy		= ipgre_policy,
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 2 (1) · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] When ip_tunnel_changelink() fails, erspan_changelink() (net/ipv4/ip_gre.c) only restores t->hlen.
- [Medium] The claim that a rejected change leaves the device unchanged is also false for encap and ignore_df state.
- [Medium] erspan_changelink() updates erspan_ver, then tun_hlen/hlen, then needed_headroom/mtu, then o_flags.
- [Low] The patch changes ipgre_policy[IFLA_GRE_ERSPAN_VER] from NLA_U8 to NLA_POLICY_MAX(NLA_U8, 2).

Pre-existing issues:
- [Medium] This problem wasn't introduced by this patch, but for erspan_ver == 0 erspan_calc_hlen() hardcodes tun_hlen = 4, and…

--

net: fix stale header length after erspan version change

This patch recomputes the ERSPAN GRE header lengths in erspan_changelink() when the version changes, and adjusts needed_headroom and mtu to match. It also tries to roll back tunnel state when a change is rejected. In addition, it forces the GRE sequence flag for v1/v2 in erspan_xmit() and limits IFLA_GRE_ERSPAN_VER to 0..2 in ipgre_policy.

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

Quoted 11 lines
net/ipv4/ip_gre.c +3 -0
index 5485586fa0e62..4d7b43b1a0f97 100644
@@ -749,6 +749,9 @@ static netdev_tx_t erspan_xmit(struct sk_buff *skb,
 	}
 
 	__clear_bit(IP_TUNNEL_KEY_BIT, flags);
+	/* v1/v2 always emit an 8-byte GRE header with a sequence word. */
+	if (tunnel->erspan_ver)
+		__set_bit(IP_TUNNEL_SEQ_BIT, flags);

[Severity: Medium]
Can a concurrent transmit still send 4 bytes of uninitialized headroom while the version is being changed?

erspan_xmit() and __gre_xmit() run without a lock (dev->lltx is set). erspan_changelink() runs under RTNL. It stores erspan_ver in erspan_netlink_parms() and only later stores tun_hlen and hlen in erspan_calc_hlen(). For a v1/v2 to v0 switch:

CPU0 (changelink) CPU1 (erspan_xmit)
erspan_netlink_parms()

  t->erspan_ver = 0;
                                  tunnel->erspan_ver == 0
                                    clears SEQ and KEY
                                  __gre_xmit()
                                    gre_build_header(skb,
                                        tunnel->tun_hlen /* still 8 */, ...)

erspan_calc_hlen()
tunnel->tun_hlen = 4;

gre_build_header() pushes 8 bytes but writes only the first 4. That looks like the leak described in the commit message, just limited to a race window.

The new check also reads tunnel->erspan_ver a second time. So one packet can pick its ERSPAN header from one value, its SEQ decision from another, and tun_hlen from a third read in __gre_xmit().

With tun_hlen 4 and SEQ set, wouldn't gre_build_header() write the sequence number over the GRE base header?

Could erspan_xmit() read erspan_ver once with READ_ONCE() and use that copy for the rest of the packet? The stores in erspan_calc_hlen() and in the rollback path are plain stores. ip_tunnel_encap_setup() uses WRITE_ONCE() for the same t->hlen.

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

Quoted 2 lines
net/ipv4/ip_gre.c +0 -0
 	__gre_xmit(skb, dev, &tunnel->parms.iph, proto, flags);
 	return NETDEV_TX_OK;

[Severity: Medium]
This is a pre-existing issue and was not introduced by this patch. The new SEQ forcing covers only v1/v2, and the v0 path still lets GRE_CSUM through.

erspan_validate() returns early for version 0:

	if (data[IFLA_GRE_ERSPAN_VER] &&
	    nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) == 0)
		return 0;

ipgre_tunnel_validate() rejects only GRE_VERSION|GRE_ROUTING. So IFLA_GRE_OFLAGS=GRE_CSUM is accepted on a v0 tunnel. For v0, erspan_xmit() clears only SEQ and KEY, and erspan_calc_hlen() hardcodes tun_hlen = 4.

In gre_build_header() with hdr_len 4 and CSUM set:

	__be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
	...
		if (test_bit(IP_TUNNEL_CSUM_BIT, flags) &&
		    ...
			*ptr = 0;

Here ptr points at greh itself. Doesn't that zero the GRE flags and protocol that were just written?

In the non CHECKSUM_PARTIAL case, csum_offset = sizeof(*greh) also points past the 4-byte header into the inner Ethernet frame.

Should the v0 path in erspan_xmit() also clear IP_TUNNEL_CSUM_BIT, or should erspan_validate() reject CSUM for v0?

[ ... ]

Quoted 11 lines
net/ipv4/ip_gre.c +2 -0
@@ -1509,17 +1525,46 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
 		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);

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

Quoted 10 lines
net/ipv4/ip_gre.c +8 -1
-	if (err < 0)
+	if (err < 0) {
+		/* A rejected change must not touch the device. */
+		t->erspan_ver = old_ver;
+		t->index = old_index;
+		t->dir = old_dir;
+		t->hwid = old_hwid;
+		t->hlen = hlen;
 		return err;
+	}

[Severity: Medium]
Is this comment accurate when the request also carries encap or IFLA_GRE_IGNORE_DF attributes?

ipgre_newlink_encap_setup() calls ip_tunnel_encap_setup(), which has already written to the live tunnel:

	WRITE_ONCE(t->encap.type, ipencap->type);
	...
	WRITE_ONCE(t->encap_hlen, hlen);
	WRITE_ONCE(t->hlen, hlen + t->tun_hlen);

ipgre_netlink_parms() has also set t->ignore_df. Neither change is rolled back here.

erspan_netlink_parms() can still reject the change after that. Examples are an IFLA_GRE_ERSPAN_INDEX outside INDEX_MASK on a v1 tunnel, or a bad DIR/HWID on v2, and erspan_validate() checks none of these. In that case t->hlen goes back to the value sampled before encap setup, but t->encap and t->encap_hlen keep the new values. The ip_tunnel_changelink() failure path below has the same problem.

Wouldn't ip_tunnel_xmit() then add the new FOU/GUE header, while t->hlen (used by tnl_update_pmtu() and ip_tunnel_change_mtu()) does not count it?

Quoted 6 lines
net/ipv4/ip_gre.c +6 -0
+
+	relink = t->parms.link != p.link || t->fwmark != fwmark;
+
+	/*
+	 * ipgre_newlink_encap_setup() reset t->hlen without the ERSPAN part.
+	 * Recompute it before ip_tunnel_changelink(): ip_tunnel_bind_dev()

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

Quoted 10 lines
net/ipv4/ip_gre.c +6 -1
+	 * consumes it when the link changes.
+	 */
+	erspan_calc_hlen(dev);
 
 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
-	if (err < 0)
+	if (err < 0) {
+		t->hlen = hlen;
 		return err;
+	}

[Severity: High]
Does this error path leave the tunnel half switched to the new version?

When ip_tunnel_changelink() runs, erspan_netlink_parms() has already written the new t->erspan_ver, t->index, t->dir and t->hwid into the live tunnel. erspan_calc_hlen() has also set t->tun_hlen for the new version. Only t->hlen is restored here.

ip_tunnel_changelink() can fail without touching the device:

	if (dev == itn->fb_tunnel_dev)
		return -EINVAL;
	...
	if (t) {
		if (t->dev != dev) {
			err = -EEXIST;
			goto out;

This happens for a changelink on erspan0, or when the new saddr/daddr/key/link collide with another tunnel. The flags and unregistering checks can also fail.

Take a rejected v0 to v1 change. The device then sends an 8-byte GRE header plus a 12-byte ERSPAN v1 header. Meanwhile t->hlen (4), dev->mtu and dev->needed_headroom still describe v0. tnl_update_pmtu() and ip_tunnel_change_mtu() read t->hlen.

Wouldn't the PMTU then be under-counted by 16 bytes?

The commit message says:

restore the old length, version, index,
direction and hwid on every error path, so a rejected change leaves the device unchanged.

That restore only happens on the erspan_netlink_parms() error path.

Should this path also restore erspan_ver/index/dir/hwid and recompute tun_hlen? Alternatively, could all the ERSPAN parameters be validated before anything is written to the live tunnel?

[ ... ]

net/ipv4/ip_gre.c static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = { · line 1731

Quoted 6 lines
net/ipv4/ip_gre.c +1 -1
@@ -1683,7 +1728,7 @@ static const struct nla_policy ipgre_policy[IFLA_GRE_MAX + 1] = {
 	[IFLA_GRE_IGNORE_DF]	= { .type = NLA_U8 },
 	[IFLA_GRE_FWMARK]	= { .type = NLA_U32 },
 	[IFLA_GRE_ERSPAN_INDEX]	= { .type = NLA_U32 },
-	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },
+	[IFLA_GRE_ERSPAN_VER]	= NLA_POLICY_MAX(NLA_U8, 2),

[Severity: Low]
Is this policy change meant to apply to gre and gretap as well?

ipgre_policy is shared by ipgre_link_ops, ipgre_tap_ops and erspan_link_ops. Neither ipgre_netlink_parms() nor ipgre_tunnel_validate() reads IFLA_GRE_ERSPAN_VER, so gre and gretap used to ignore the attribute. Now their newlink/changelink requests fail with -ERANGE when the value is above 2.

For erspan devices, the errno changes from -EINVAL to -ERANGE. This check in erspan_netlink_parms() can also no longer be reached:

		if (t->erspan_ver > 2)
			return -EINVAL;

ip6gre_policy in net/ipv6/ip6_gre.c still uses plain NLA_U8 for the same attribute. The erspan-ver entry in Documentation/netlink/specs/rt-link.yaml also has no max check.

Since this is tagged for stable, could the commit message body mention this uAPI change? Right now it only appears in the v4 -> v5 notes after the --- line.

Quoted 1 line
net/ipv4/ip_gre.c +0 -0
 	[IFLA_GRE_ERSPAN_DIR]	= { .type = NLA_U8 },
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261203072423.147308-1-bsdhenrymartin%40gmail.com