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. 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/ Link: https://lore.kernel.org/netdev/20261124014646.7954-1-yanhaishuang@cmss.chinamobile.com/ Signed-off-by: Henry Martin --- 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(-) diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c index 5485586fa0e6..16f52b9e77b3 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); @@ -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); -- 2.43.7