netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Henry Martin <bsdhenrymartin@gmail.com>
To: David Ahern <dsahern@kernel.org>,
	Ido Schimmel <idosch@nvidia.com>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Yun Zhou <yun.zhou@windriver.com>,
	Maoyi Xie <maoyixie.tju@gmail.com>,
	Xuanqiang Luo <luoxuanqiang@kylinos.cn>,
	Ilya Maximets <i.maximets@ovn.org>,
	Frode Nordahl <fnordahl@ubuntu.com>
Cc: netdev@vger.kernel.org, Henry Martin <bsdhenrymartin@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH net] ip_gre: fix stale header length after erspan version change
Date: Fri, 20 Nov 2026 14:02:54 +0800	[thread overview]
Message-ID: <20261120060254.3766851-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 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 grows the ERSPAN header
without growing needed_headroom.

Recompute the header lengths in erspan_changelink() when the erspan
version attribute is present, mirroring what the IPv6 side does in
ip6erspan_tnl_change(), and propagate the delta 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.")
Signed-off-by: Henry Martin <bsdhenrymartin@gmail.com>
---
 net/ipv4/ip_gre.c | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/net/ipv4/ip_gre.c b/net/ipv4/ip_gre.c
index 5485586fa0e6..8acaefca64da 100644
--- a/net/ipv4/ip_gre.c
+++ b/net/ipv4/ip_gre.c
@@ -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;
@@ -1524,6 +1531,19 @@ 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);
 
+	if (data && data[IFLA_GRE_ERSPAN_VER]) {
+		int len = t->hlen;
+
+		erspan_calc_hlen(dev);
+		len = t->hlen - len;
+		if (len) {
+			dev->needed_headroom += len;
+			if (!tb[IFLA_MTU])
+				WRITE_ONCE(dev->mtu, max_t(int, dev->mtu - len,
+							   68));
+		}
+	}
+
 	return 0;
 }
 
-- 
2.43.7


             reply	other threads:[~2026-11-20  6:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20  6:02 Henry Martin [this message]
2026-11-20  6:07 ` [PATCH net] ip_gre: fix stale header length after erspan version change netdev-bot+sinfo

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=20261120060254.3766851-1-bsdhenrymartin@gmail.com \
    --to=bsdhenrymartin@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fnordahl@ubuntu.com \
    --cc=horms@kernel.org \
    --cc=i.maximets@ovn.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=luoxuanqiang@kylinos.cn \
    --cc=maoyixie.tju@gmail.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=stable@vger.kernel.org \
    --cc=yun.zhou@windriver.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;
as well as URLs for NNTP newsgroup(s).