Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] ip_gre: fix stale header length after erspan version change
@ 2026-11-23 12:35 Henry Martin
  2026-11-24  1:46 ` Haishuang Yan
  2026-11-24 13:10 ` netdev-bot+sashiko
  0 siblings, 2 replies; 3+ messages in thread
From: Henry Martin @ 2026-11-23 12:35 UTC (permalink / raw)
  To: dsahern, idosch, davem, edumazet, kuba, pabeni, yanhaishuang
  Cc: horms, netdev, Henry Martin, stable

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.

Cc: stable@vger.kernel.org
Fixes: f989d546a2d5 ("erspan: Add type I version 0 support.")
Link: https://lore.kernel.org/netdev/20261123062752.27920-1-yanhaishuang@cmss.chinamobile.com/
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
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(-)

diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
index 5485586fa0e6..2a1d6ff16dfe 100644
--- a/net/ipv4/ip_gre.c
+++ b/net/ipv4/ip_gre.c
@@ -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) +
-- 
2.43.7

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] ip_gre: fix stale header length after erspan version change
  2026-11-23 12:35 [PATCH net v2] ip_gre: fix stale header length after erspan version change Henry Martin
@ 2026-11-24  1:46 ` Haishuang Yan
  2026-11-24 13:10 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: Haishuang Yan @ 2026-11-24  1:46 UTC (permalink / raw)
  To: Henry Martin
  Cc: Haishuang Yan, dsahern, idosch, davem, edumazet, kuba, pabeni,
	horms, netdev, stable

On Mon, Nov 23, 2026 at 08:35:29PM +0800, Henry Martin wrote:
> +	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.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH net v2] ip_gre: fix stale header length after erspan version change
  2026-11-23 12:35 [PATCH net v2] ip_gre: fix stale header length after erspan version change Henry Martin
  2026-11-24  1:46 ` Haishuang Yan
@ 2026-11-24 13:10 ` netdev-bot+sashiko
  1 sibling, 0 replies; 3+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 13:10 UTC (permalink / raw)
  To: bsdhenrymartin
  Cc: dsahern, idosch, davem, edumazet, kuba, pabeni, yanhaishuang,
	horms, netdev, stable

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

Critical: 0 · High: 2 · 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] The fix is incomplete: every error return in erspan_changelink()
  (net/ipv4/ip_gre.c) skips the new erspan_calc_hlen() call, but two…
- [High] In erspan_changelink() (net/ipv4/ip_gre.c), the new delta
  adjustment is wrong whenever the same request changes IFLA_GRE_LINK or…
- [Medium] erspan_changelink() still updates the ERSPAN version and the
  GRE header length as separate, unsynchronized stores, while the TX…
- [Low] The new code in erspan_changelink() (net/ipv4/ip_gre.c:1540) does
  a plain read-modify-write, `dev->needed_headroom += hlen`, while…

--

ip_gre: fix stale header length after erspan version change

This patch moves the ERSPAN header length calculation out of
erspan_tunnel_init() into a new helper, erspan_calc_hlen(). The helper is
now also called from erspan_changelink(), so switching versions on an
existing tunnel updates tun_hlen and hlen. The difference between the old
and new hlen is then added to needed_headroom and, unless IFLA_MTU was
given, to the MTU.

Link: https://lore.kernel.org/netdev/20261123062752.27920-1-yanhaishuang@cmss.chinamobile.com/

> diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
> index 5485586fa0e62..2a1d6ff16dfe1 100644
> --- a/net/ipv4/ip_gre.c
> +++ b/net/ipv4/ip_gre.c

[ ... ]

> @@ -1509,6 +1517,8 @@ 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;

[Severity: High]
What state are t->hlen and t->erspan_ver left in if one of the later
error returns in erspan_changelink() is taken?

By this point ipgre_newlink_encap_setup()->ip_tunnel_encap_setup() has
already rewritten t->hlen, and the new value leaves out the erspan header:

net/ipv4/ip_tunnel.c:ip_tunnel_encap_setup() {
    ...
	WRITE_ONCE(t->encap_hlen, hlen);
	WRITE_ONCE(t->hlen, hlen + t->tun_hlen);
    ...
}

erspan_netlink_parms() also writes the live version before it validates
the version, index, dir and hwid:

	if (data[IFLA_GRE_ERSPAN_VER]) {
		t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]);

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

Both of the remaining error returns skip the new erspan_calc_hlen() call:

	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_changelink() can return -EEXIST when ip_tunnel_find() matches
another tunnel. It can also return -EINVAL on the fallback device, or
-ENODEV.

Take a v1 tunnel that is given erspan_ver 0 with local/remote/key values
that collide with another erspan tunnel. ip_tunnel_changelink() returns
-EEXIST, and the tunnel is left with erspan_ver == 0 and tun_hlen == 8.
erspan_xmit() then clears SEQ and KEY, and
__gre_xmit()->gre_build_header() pushes 8 bytes but writes only 4.

Doesn't that leave the leak of uninitialized headroom described in the
commit message still reachable?

The reverse case looks similar. A v0 tunnel given erspan_ver 1 with an
out-of-range IFLA_GRE_ERSPAN_INDEX ends up with erspan_ver == 1 and
tun_hlen == 4.

The next successful changelink is affected as well. After a failed call,
t->hlen lacks erspan_hdr_len() while needed_headroom and mtu are
unchanged. The next call samples that value as its baseline, and
erspan_calc_hlen() adds erspan_hdr_len() back. The delta is then +12 (v1)
or +16 (v2) even when nothing changed.

Can repeated failed/successful pairs walk the MTU down to 68 and keep
growing needed_headroom? That is the drift the v1 -> v2 notes describe as
fixed.

The commit message says this mirrors the IPv6 side.
ip6erspan_changelink() only applies the version, via
ip6erspan_set_version(data, &p), after ip6gre_changelink_common() has
succeeded. Would it work to stage version/index/dir/hwid in locals and
commit them only after ip_tunnel_changelink() succeeds? Recomputing hlen
on every error path might also work.

[ ... ]

> @@ -1524,6 +1534,14 @@ static int erspan_changelink(struct net_device *dev, struct nlattr *tb[],
>  	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);

[Severity: Medium]
Can the lockless transmit path see the new erspan_ver together with the
old tun_hlen?

erspan_tunnel_init() sets dev->lltx, so erspan_xmit() runs without RTNL
or any tunnel lock. erspan_netlink_parms() stores t->erspan_ver early.
tun_hlen only changes here, after ip_tunnel_changelink() has taken
tunnels_lock, rehashed the tunnel and run the netdev_state_change()
notifiers.

During a v1 -> v0 change:

CPU0 (changelink)                CPU1 (erspan_xmit)
erspan_netlink_parms()
  t->erspan_ver = 0
                                 reads erspan_ver == 0
                                 clears SEQ and KEY
                                 __gre_xmit()
                                   gre_build_header(skb, tun_hlen == 8, ...)
ip_tunnel_changelink()
erspan_calc_hlen()
  tun_hlen = 4

Would packets sent in that window still get 8 bytes pushed and only 4
written?

Reordering the stores in changelink does not seem to close this, because
the transmit path reads erspan_ver and tun_hlen separately. Would the
transmit path need to derive the GRE length from a single
READ_ONCE(tunnel->erspan_ver) snapshot? These plain write/lockless read
pairs are also unannotated data races.

A similar window already exists for plain GRE in
ipgre_changelink()/ipgre_link_update(), where o_flags is copied before
tun_hlen is updated.

> +	hlen = t->hlen - hlen;
> +	if (hlen) {
> +		dev->needed_headroom += hlen;

[Severity: Low]
This is a plain read-modify-write of dev->needed_headroom with only RTNL
held. The transmit path updates the same field locklessly through
ip_tunnel_xmit()->ip_tunnel_adj_headroom():

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

Could a concurrent transmit overwrite the adjustment made here, or could
this store overwrite a transmit-side increase? It is also an unannotated
data race, unlike the WRITE_ONCE(dev->mtu, ...) just below.

ipgre_link_update() and ip_tunnel_bind_dev() already use plain writes for
this field, so this follows existing practice in the file.

> +		if (!tb[IFLA_MTU])
> +			WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - hlen, 68));
> +	}

[Severity: High]
Is this delta still correct when the same request also changes
IFLA_GRE_LINK or IFLA_GRE_FWMARK?

In that case ip_tunnel_changelink()->ip_tunnel_update() calls
ip_tunnel_bind_dev(). That function sets needed_headroom and mtu from
tunnel->hlen as absolute values, not relative ones:

net/ipv4/ip_tunnel.c:ip_tunnel_update() {
    ...
		mtu = ip_tunnel_bind_dev(dev);
		if (set_mtu)
			WRITE_ONCE(dev->mtu, mtu);
    ...
}

At that point t->hlen still holds the in-between value left by
ip_tunnel_encap_setup(): tun_hlen_old + encap_hlen_new, without
erspan_hdr_len(). The delta here is computed against the hlen sampled
before encap setup, but it is added to values that bind_dev already
recomputed from the in-between hlen.

Take v1 -> v0 plus a link change, with no encap, on an ARPHRD_ETHER
device:

  sampled hlen = 20, in-between hlen = 8
  bind_dev: needed_headroom = 28 + LL, mtu = tdev_mtu - 42
  delta = 4 - 20 = -16
  result:   needed_headroom = 12 + LL, mtu = tdev_mtu - 26
  expected: needed_headroom = 24 + LL, mtu = tdev_mtu - 38

That leaves the MTU 12 bytes too large.

The result can also go negative. Consider a v2 tunnel with GUE encap,
switched to v0 with encap type none and moved to an underlay with zero
headroom. bind_dev sets needed_headroom to 28, then the delta of
-(20 + encap_hlen) is applied. Does that wrap the u16 needed_headroom?
Because this is an open-coded +=, it also skips the 512-byte cap in
ip_tunnel_limit_headroom().

The IPv6 counterpart recomputes headroom and MTU from the final hlen
rather than applying a delta. Would computing hlen before
ip_tunnel_changelink(), or recomputing these values the same way, avoid
mixing the two baselines?

>  	return 0;
>  }

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-24 13:10 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-23 12:35 [PATCH net v2] ip_gre: fix stale header length after erspan version change Henry Martin
2026-11-24  1:46 ` Haishuang Yan
2026-11-24 13:10 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox