Skip to contentSkip to conversation

ip_gre: fix stale header length after erspan version change

PATCH Superseded Henry Martin 3 participants 4 messages Nov 25 – Nov 29

Subsystems (4)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 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 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: alternating erspan_hwid 0xff (-EINVAL) and 7 on a version 2 tunnel keeps the MTU at 1446 (v2: 1446, 1434, 1422, ...), and changing the link of a version 1 tunnel yields MTU 1450 (v2:
1458).

This issue was discovered by Tencent CodeBuddy Security.

---
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. Add the v2/v3 A/B numbers.
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 5485586fa0e62..f2103819788a9 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);
 
@@ -1360,9 +1360,16 @@ static int erspan_tunnel_init(struct net_device *dev)
 	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;
@@ -1499,6 +1506,7 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
 	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;
@@ -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;
 
 	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;
 
Signature · 1 line
2.53.0
maintainer
#1 lore raw

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

Quoted 13 lines · Henry Martin
net/ipv4/ip_gre.c +2 -0
@@ -1499,6 +1506,7 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
  	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;
@@ -1509,11 +1517,28 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
  		return -EOPNOTSUPP;
  	}
 
+	hlen = t->hlen;

This is weird. The patch is broken, but it compiles successfully (on some slice of the CI) because the first chunk is applied without fuzz in the wrong context. Still causes CI build issues:

https://netdev-ctrl.bots.linux.dev/logs/vmksft/net-dbg/results/926627/build/stderr

Minor nit: also reverse christmas tree order in the first chunk

/P

#2 lore raw
Quoted 3 lines

This is weird. The patch is broken, but it compiles successfully (on some slice of the CI) because the first chunk is applied without fuzz in the wrong context. Still causes CI build issues:

Hi Paolo,

Sorry for the noise -- I have fixed it locally and will send a v4.

Thanks,
Henry

#3 lore raw
In reply to Henry Martin
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

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

Critical: 0 · High: 2 · Medium: 0 · 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] The fix sets the GRE header length from erspan_ver alone, but the header bytes that get written depend on parms.o_flags.
- [High] erspan_changelink() now recomputes hlen and adjusts dev->needed_headroom and dev->mtu before checking the error from…
- [Low] The new comment in erspan_changelink() says 'ipgre_newlink_encap_setup() reset t->hlen without the ERSPAN part'.

--

ip_gre: fix stale header length after erspan version change

This commit moves the ERSPAN header length calculation into a new helper, erspan_calc_hlen(), and calls it from erspan_changelink(). The goal is to keep tun_hlen, hlen, needed_headroom and mtu in sync when the erspan version changes. The recompute runs right after erspan_netlink_parms(), before its error is checked.

Quoted 14 lines
net/ipv4/ip_gre.c +1 -1
index 5485586fa0e62..f2103819788a9 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 1365

Quoted 7 lines
net/ipv4/ip_gre.c +1 -1
@@ -1360,9 +1360,16 @@ static int erspan_tunnel_init(struct net_device *dev)
 	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);
+}

[Severity: High]
erspan_calc_hlen() picks tun_hlen from erspan_ver alone. Can uninitialized headroom still reach the tunnel peer this way?

erspan_xmit() builds the GRE flags from tunnel->parms.o_flags, and never sets SEQ for v1/v2:

net/ipv4/ip_gre.c:erspan_xmit() {
    ...
	ip_tunnel_flags_copy(flags, tunnel->parms.o_flags);

	/* Push ERSPAN header */
	if (tunnel->erspan_ver == 0) {
		proto = htons(ETH_P_ERSPAN);
		__clear_bit(IP_TUNNEL_SEQ_BIT, flags);
	} else if (tunnel->erspan_ver == 1) {
    ...
	__clear_bit(IP_TUNNEL_KEY_BIT, flags);
	__gre_xmit(skb, dev, &tunnel->parms.iph, proto, flags);
    ...
}

gre_build_header() pushes tun_hlen bytes but writes only the optional words whose flags are set.

erspan_validate() does not tie o_flags to the version. For v1/v2 it checks only the OR of OFLAGS and IFLAGS:

	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 IFLAGS=SEQ|KEY with OFLAGS absent or 0 is accepted. For v0 it returns early and checks no flags at all.

That leaves a few cases:

A newlink with ERSPAN_VER=1 or 2, IFLAGS=SEQ|KEY and OFLAGS=0 gets tun_hlen=8 with no SEQ in the TX flags. Bytes 4..7 of every GRE header are then unwritten headroom. This predates the patch, but it is the same leak the commit message describes.

New with this patch: take a v0 tunnel whose o_flags lack SEQ and change it to ERSPAN_VER=1 with IFLAGS=SEQ|KEY and no OFLAGS. The changelink succeeds and o_flags still lacks SEQ, but erspan_calc_hlen() now sets tun_hlen=8. Before the patch tun_hlen stayed at 4, and the 4-byte header was fully written.

A v0 tunnel with CSUM in OFLAGS gets tun_hlen=4. gre_build_header() then puts the checksum word on top of the GRE base header instead of after it. This one is also pre-existing.

The commit message says this mirrors ip6erspan_tnl_change(). However, ip6erspan_tunnel_xmit() on the IPv6 side, and erspan_fb_xmit() on the IPv4 side, force SEQ with a fixed 8-byte GRE header:

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

Should tun_hlen come from the effective TX flags instead? Alternatively, erspan_xmit() could force SEQ for v1/v2 (or o_flags could be normalized per version) as part of this fix.

[ ... ]

Quoted 9 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 1534

Quoted 10 lines
net/ipv4/ip_gre.c +8 -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.
+	 */

[Severity: Low]
This isn't a bug, but is the comment accurate? ipgre_newlink_encap_setup() changes t->hlen only when an IFLA_GRE_ENCAP_* attribute is present:

	if (ipgre_netlink_encap_parms(data, &ipencap)) {
		struct ip_tunnel *t = netdev_priv(dev);
		int err = ip_tunnel_encap_setup(t, &ipencap);

Without encap attributes t->hlen is left alone. The comment could say the call "may reset" t->hlen.

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

Quoted 9 lines
net/ipv4/ip_gre.c +7 -0
+	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;

[Severity: High]
This commits the new tun_hlen, needed_headroom and mtu before err is checked. Is that safe when the request is rejected?

erspan_netlink_parms() writes t->erspan_ver, t->index, t->dir and t->hwid into the live tunnel before validating them, and nothing rolls them back:

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;
	}
    ...
}

t->parms.o_flags, on the other hand, is copied only at the end of erspan_changelink(), after ip_tunnel_changelink() succeeds.

ERSPAN_VER values 3..255 pass the NLA_U8 policy and erspan_validate(). So t->erspan_ver is left out of range, and erspan_hdr_len() treats it as v2. The rejected request still changes mtu and needed_headroom. After that, erspan_xmit() drops every packet through its final else branch.

Now take a v0 tunnel whose o_flags lack SEQ and send a changelink with ERSPAN_VER=1, IFLAGS=OFLAGS=SEQ|KEY and ERSPAN_INDEX=0x100000. It fails the INDEX_MASK check with -EINVAL. A valid v1/v2 request does the same when ip_tunnel_changelink() returns -EEXIST.

In both cases t->erspan_ver becomes 1 and erspan_calc_hlen() sets tun_hlen=8, but o_flags keeps the v0 value without SEQ. erspan_xmit() then calls gre_build_header() with an 8-byte length and no optional flags, so bytes 4..7 of every GRE header are uninitialized skb headroom sent to the peer. Before this patch tun_hlen stayed at 4 on this path.

For comparison, ipgre_changelink() applies its header-length update only after ip_tunnel_changelink() succeeds:

	ipgre_link_update(dev, !tb[IFLA_MTU]);

Could erspan_netlink_parms() parse and validate the erspan fields into locals before writing anything to t? A failed changelink would then leave the tunnel unchanged.

Quoted 7 lines
net/ipv4/ip_gre.c +0 -0
 	err = ip_tunnel_changelink(dev, tb, &p, fwmark);
net/ipv4/ip_gre.c +0 -0
 	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);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261125074617.629026-1-bsdhenrymartin%40gmail.com