From: Henry Martin <bsdhenrymartin@gmail.com>
To: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net,
edumazet@kernel.org, kuba@kernel.org, pabeni@redhat.com,
yanhaishuang@cmss.chinamobile.com
Cc: horms@kernel.org, netdev@vger.kernel.org,
Henry Martin <bsdhenrymartin@gmail.com>,
stable@vger.kernel.org
Subject: [PATCH net v5] net: fix stale header length after erspan version change
Date: Thu, 3 Dec 2026 15:24:22 +0800 [thread overview]
Message-ID: <20261203072423.147308-1-bsdhenrymartin@gmail.com> (raw)
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.
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 <bsdhenrymartin@gmail.com>
---
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(-)
diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
index 5485586fa0e6..4d7b43b1a0f9 100644
--- a/net/ipv4/ip_gre.c
+++ b/net/ipv4/ip_gre.c
@@ -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,
--
2.43.7
reply other threads:[~2026-12-03 7:24 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261203072423.147308-1-bsdhenrymartin@gmail.com \
--to=bsdhenrymartin@gmail.com \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=kuba@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=stable@vger.kernel.org \
--cc=yanhaishuang@cmss.chinamobile.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox