Skip to contentSkip to conversation

net: fix stale header length after erspan version change

PATCH New Henry Martin 2 participants 2 messages Nov 26 – Nov 27

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

PATCH v4 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 in ip6erspan_tnl_change(). Sample the old length before ipgre_newlink_encap_setup() resets t->hlen, and run the recompute right after erspan_netlink_parms(), before checking its error:
ip_tunnel_changelink() -> ip_tunnel_bind_dev() consumes t->hlen, and a failed changelink must not leave the reset value for the next call to sample.

Verified on v7.4-rc4 (KASAN, uid 1000): unpatched, 708 post-switch frames carry the sprayed heap marker; with the patch the tag hits drop to 0, and repeating a failed (hwid 0xff) followed by a successful changelink keeps the MTU at 1446, as before the change.

This issue was discovered by Tencent CodeBuddy Security.

---
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, so a failed changelink leaves t->hlen consistent and ip_tunnel_bind_dev() no longer sees the reset value (both 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. 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 | 29 +++++++++++++++++++++++++++--
 1 file changed, 27 insertions(+), 2 deletions(-)
net/ipv4/ip_gre.c +27 -2
index 5485586fa0e6..16f52b9e77b3 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);
@@ -1493,32 +1500,50 @@ 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 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);
+
+	/*
+	 * ipgre_newlink_encap_setup() reset t->hlen without the ERSPAN part.
+	 * Restore it before ip_tunnel_changelink() (and its
+	 * ip_tunnel_bind_dev()) consumes it, and even when the parms above
+	 * failed, so a failed changelink leaves no stale value for the next
+	 * one to sample.
+	 */
+	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));
+	}
 	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);
Signature · 1 line
2.43.7
#1 lore raw
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider.

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

Critical: 0 · High: 3 · Medium: 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] A failed erspan_changelink() now leaves the tunnel with a header length for the new version but o_flags from the old configuration.
- [High] erspan_calc_hlen() sets tun_hlen=8 for every ERSPAN v1/v2 tunnel, which assumes a GRE sequence word is always emitted.
- [High] erspan_changelink() updates the fields the transmit path depends on as separate plain stores, holding only RTNL, while erspan_xmit()…
- [Medium] The commit message says the patch mirrors the IPv6 side, but the IPv4 and IPv6 changelinks now behave differently when a request is…
- [Low] The new dev->needed_headroom += hlen; in erspan_changelink() is a plain, unannotated read-modify-write of dev->needed_headroom.

--

net: fix stale header length after erspan version change

This commit moves the ERSPAN header length calculation out of erspan_tunnel_init() into a new erspan_calc_hlen() helper. The helper is then called from erspan_changelink() right after erspan_netlink_parms(), before its error is checked. needed_headroom and mtu are adjusted by the difference.

Quoted 14 lines
net/ipv4/ip_gre.c +1 -1
index 5485586fa0e62..16f52b9e77b35 100644
@@ -1351,7 +1351,7 @@ static const struct net_device_ops gre_tap_netdev_ops = {
 	.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);
 

net/ipv4/ip_gre.c static int erspan_tunnel_init(struct net_device *dev) · line 1361

Quoted 2 lines
net/ipv4/ip_gre.c +0 -0
@@ -1360,9 +1360,16 @@ static int erspan_tunnel_init(struct net_device *dev)
 	else
 		tunnel->tun_hlen = 8; /* 8-byte GRE hdr. */

[Severity: High]

tun_hlen is now picked from erspan_ver alone on every changelink. That assumes a GRE sequence word is always emitted for v1/v2. Does the non-collect_md transmit path guarantee that?

erspan_xmit() copies tunnel->parms.o_flags and clears KEY for v1/v2. It never sets IP_TUNNEL_SEQ_BIT. gre_build_header() pushes tun_hlen bytes, but it only fills the trailing word when SEQ (or KEY/CSUM) is set.

erspan_validate() only checks the OR of the two flag attributes:

net/ipv4/ip_gre.c:erspan_validate() {
    ...
	if (data[IFLA_GRE_OFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_OFLAGS]);
	if (data[IFLA_GRE_IFLAGS])
		flags |= nla_get_be16(data[IFLA_GRE_IFLAGS]);
	if (!data[IFLA_GRE_COLLECT_METADATA] &&
	    flags != (GRE_SEQ | GRE_KEY))
		return -EINVAL;
    ...
}

So a v0 tunnel changed to v1 with IFLAGS=SEQ|KEY and no OFLAGS is accepted. ipgre_netlink_parms() memsets parms, so p.o_flags has no SEQ, and erspan_changelink() commits it at the end.

With this patch, that device gets tun_hlen == 8 and o_flags without SEQ:

erspan_xmit()
__gre_xmit()

    gre_build_header(skb, tunnel->tun_hlen /* 8 */, flags /* no SEQ/KEY */, ...)

Would bytes 4..7 of every GRE header then go to the peer as unwritten skb headroom? That is the leak the commit message describes. A v1/v2 device created the same way through newlink has the same gap.

erspan_fb_xmit() and ip6erspan_tunnel_xmit() both force SEQ before an 8-byte gre_build_header():

	__set_bit(IP_TUNNEL_SEQ_BIT, flags);
	gre_build_header(skb, 8, flags, proto, 0,
			 htonl(atomic_fetch_inc(&t->o_seqno)));

Should erspan_xmit() also set IP_TUNNEL_SEQ_BIT for v1/v2? Or should tun_hlen be derived from the flags that are actually emitted?

Quoted 5 lines
net/ipv4/ip_gre.c +1 -1
-	tunnel->parms.iph.protocol = IPPROTO_GRE;
 	tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +
 		       erspan_hdr_len(tunnel->erspan_ver);
+}

[ ... ]

Quoted 10 lines
net/ipv4/ip_gre.c +2 -0
@@ -1509,11 +1517,28 @@ 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;
 

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

Quoted 10 lines
net/ipv4/ip_gre.c +9 -0
 	err = erspan_netlink_parms(dev, data, tb, &p, &fwmark);
+
+	/*
+	 * ipgre_newlink_encap_setup() reset t->hlen without the ERSPAN part.
+	 * Restore it before ip_tunnel_changelink() (and its
+	 * ip_tunnel_bind_dev()) consumes it, and even when the parms above
+	 * failed, so a failed changelink leaves no stale value for the next
+	 * one to sample.
+	 */
+	erspan_calc_hlen(dev);

[Severity: High]

Can a failed changelink now leave the new version's tun_hlen paired with the old o_flags?

erspan_netlink_parms() writes t->erspan_ver into the live tunnel before its own checks run:

net/ipv4/ip_gre.c:erspan_netlink_parms() {
    ...
	if (data[IFLA_GRE_ERSPAN_VER]) {
		t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);

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

	if (t->erspan_ver == 1) {
		if (data[IFLA_GRE_ERSPAN_INDEX]) {
			t->index = nla_get_u32(data[IFLA_GRE_ERSPAN_INDEX]);
			if (t->index & ~INDEX_MASK)
				return -EINVAL;
		}
    ...
}

erspan_calc_hlen() then runs before err is checked. ip_tunnel_changelink() can also fail after it, with -EEXIST, -EINVAL or -ENODEV. Every one of these returns skips the o_flags commit at the end of erspan_changelink():

	ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags);

A v0 device can have o_flags without SEQ, because erspan_validate() returns 0 early for version 0. Take a changelink on such a device with ERSPAN_VER=1, IFLAGS=OFLAGS=SEQ|KEY and ERSPAN_INDEX=0x100000:

erspan_netlink_parms()
t->erspan_ver = 1, returns -EINVAL
erspan_calc_hlen()
tun_hlen = 8, hlen = 20
needed_headroom and mtu adjusted
return -EINVAL
t->parms.o_flags still 0

erspan_xmit() then takes the v1 branch with flags copied from o_flags (0). __gre_xmit() calls gre_build_header() with tun_hlen == 8 and no SEQ/KEY/CSUM. gre_build_header() pushes 8 bytes but only writes greh->flags and greh->protocol.

Would bytes 4..7 of every GRE header then reach the peer as uninitialized skb headroom? Before this patch, the same failure path left tun_hlen at 4.

The commit message says "a failed changelink must not leave the reset value for the next call to sample". It doesn't mention that a failed request now changes tun_hlen, needed_headroom and mtu.

Could the ERSPAN attributes be validated before any tunnel state is written? Alternatively, erspan_ver/index/dir/hwid and the hlen values could be saved and restored on every error return.

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

Quoted 3 lines
net/ipv4/ip_gre.c +3 -0
+	hlen = t->hlen - hlen;
+	if (hlen) {
+		dev->needed_headroom += hlen;

[Severity: Low]

This is a plain read-modify-write of dev->needed_headroom, while the mtu store next to it uses WRITE_ONCE(). Can it race with the datapath?

ip_tunnel_xmit() updates the same field locklessly:

include/net/ip_tunnels.h:ip_tunnel_adj_headroom() {
	headroom = ip_tunnel_limit_headroom(headroom);

	if (headroom > READ_ONCE(dev->needed_headroom))
		WRITE_ONCE(dev->needed_headroom, headroom);
}

Say changelink loads H0, xmit raises the value to H1, and changelink then stores H0 + hlen. The datapath raise is lost. With a negative hlen, the field can also drop below a value the datapath just set.

In practice this means extra skb reallocations and a KCSAN report. ipgre_link_update() has the same unannotated pattern.

Would this work better here:

	WRITE_ONCE(dev->needed_headroom, READ_ONCE(dev->needed_headroom) + hlen)

ideally clamped with ip_tunnel_limit_headroom() as ip_tunnel_bind_dev() does?

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

Quoted 5 lines
net/ipv4/ip_gre.c +3 -0
+		if (!tb[IFLA_MTU])
+			WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - hlen, 68));
+	}
 	if (err < 0)
 		return err;

[Severity: Medium]

The commit message says this mirrors ip6erspan_tnl_change(). Does the IPv4 failure path actually behave like the IPv6 one?

ip6erspan_changelink() parses into a local struct __ip6_tnl_parm. It only applies the version and header length on success:

net/ipv6/ip6_gre.c:ip6erspan_changelink() {
    ...
	t = ip6gre_changelink_common(dev, tb, data, &p, extack);
	if (IS_ERR(t)) {
		err = PTR_ERR(t);
		goto unlock;
	}

	ip6erspan_set_version(data, &p);
	...
	ip6erspan_tnl_change(t, &p, !tb[IFLA_MTU]);
    ...
}

Here, needed_headroom and mtu are adjusted before err is checked, based on whatever erspan_netlink_parms() already wrote into the live tunnel. IFLA_GRE_ERSPAN_VER also has no range policy in ipgre_policy:

	[IFLA_GRE_ERSPAN_VER]	= { .type = NLA_U8 },

For example, take a changelink on a v1 device with ERSPAN_VER=3 and SEQ|KEY flags. It sets t->erspan_ver to 3 and returns -EINVAL.

erspan_calc_hlen() then calls erspan_hdr_len(3), which returns the v2 size. needed_headroom grows and mtu shrinks by 4 before -EINVAL is returned. From then on, erspan_xmit() drops every packet because erspan_ver is 3.

A v1->v2 change with an invalid hwid likewise switches the version, header length, headroom and mtu while reporting failure.

Doesn't this contradict the new comment's claim that "a failed changelink leaves no stale value"?

Would it match the IPv6 behavior better to parse the ERSPAN attributes into locals, and to commit them (plus the hlen/mtu/headroom update) only on success? NLA_POLICY_MAX(NLA_U8, 2) for IFLA_GRE_ERSPAN_VER might also help.

[Severity: High]

Is anything ordering these updates against the transmit path?

erspan_tunnel_init() sets dev->lltx, so erspan_xmit() runs without a lock, while erspan_changelink() holds only RTNL. The fields are updated as separate plain stores, in this order:

erspan_netlink_parms()
t->erspan_ver = ...
erspan_calc_hlen()
t->tun_hlen = ..., t->hlen = ...
ip_tunnel_changelink()
ip_tunnel_update()
netdev_state_change()
ip_tunnel_flags_copy(t->parms.o_flags, p.o_flags)

erspan_xmit() reads tunnel->erspan_ver to pick the header and clear SEQ, then reads o_flags. __gre_xmit() reads tunnel->tun_hlen separately for gre_build_header().

On a v1->v0 change, xmit can see erspan_ver == 0 (SEQ and KEY cleared) together with the old tun_hlen == 8. Four bytes of uninitialized headroom then go out.

On a v0->v1 change where the old o_flags lack SEQ, xmit sees erspan_ver == 1 and tun_hlen == 8 with the old o_flags for the whole ip_tunnel_changelink() window. Every packet sent in that window has the same 4-byte leak.

The opposite interleaving (new erspan_ver, old tun_hlen == 4, o_flags with SEQ) writes the sequence number over the GRE base header.

The caller controls both the version toggling and the traffic. Doesn't that turn the permanent leak into a repeatable race rather than remove it?

Could erspan_xmit() compute the GRE header length locally from a single READ_ONCE() of erspan_ver, and force SEQ for v1/v2? Or could version changes be refused while the device is up?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261126031224.430850-1-bsdhenrymartin%40gmail.com