ip_gre: fix stale header length after erspan version change
Subsystems (4)
PATCH 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 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.
--- net/ipv4/ip_gre.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-)
net/ipv4/ip_gre.c +22 -2
@@ -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 ; }
Signature · 1 line
2.43.7
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1535
Quoted 2 lines · Henry Martin
net/ipv4/ip_gre.c +2
-0+if (data && data[IFLA_GRE_ERSPAN_VER ]) { +int len = t->hlen;
The bug is real (on 0 -> 1 the GRE sequence number even overwrites the GRE base header), and the patch fixes the packets. But t->hlen has already been reset to tun_hlen + encap_hlen by ipgre_newlink_encap_setup(), so the delta is wrong. iproute2 always sends IFLA_GRE_ERSPAN_VER, so each "ip link set erspan1 type erspan ttl N" on a ver 1 device lowers the MTU by 8 (1450, 1442, 1434, ...) and raises needed_headroom by 8.
Saving t->hlen before ipgre_newlink_encap_setup() and always recomputing it fixes that in my tests:
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));
}Thanks -- v2 is posted with hlen sampled before ipgre_newlink_encap_setup(), so the delta is now correct:
https://lore.kernel.org/netdev/[email protected]/
Henry
Quoted 24 lines · Haishuang Yan <[email protected]> 于2026年11月23日周一 14:28写道:
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1535
Quoted 2 lines · Henry Martin
net/ipv4/ip_gre.c +2
-0+if (data && data[IFLA_GRE_ERSPAN_VER ]) { +int len = t->hlen;The bug is real (on 0 -> 1 the GRE sequence number even overwrites the GRE base header), and the patch fixes the packets. But
t->hlenhas already been reset totun_hlen+encap_hlenbyipgre_newlink_encap_setup(), so the delta is wrong. iproute2 always sendsIFLA_GRE_ERSPAN_VER, so each "ip link set erspan1 type erspan ttl N" on a ver 1 device lowers the MTU by 8 (1450, 1442, 1434, ...) and raisesneeded_headroomby 8.Saving
t->hlenbeforeipgre_newlink_encap_setup()and always recomputing it fixes that in my tests: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)); }
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider.
Critical: 0 · High: 3 · Medium: 2 · Low: 2
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] erspan_changelink() (net/ipv4/ip_gre.c:1535) takes its 'old' header length from int len = t->hlen;.
- [High] erspan_netlink_parms() (net/ipv4/ip_gre.c:1271) writes t->erspan_ver = nla_get_u8(data[IFLA_GRE_ERSPAN_VER]) straight into…
- [High] erspan_calc_hlen() (net/ipv4/ip_gre.c:1361) sets tun_hlen=8 for every v1/v2 tunnel without looking at t->parms.o_flags.
- [Medium] The recompute in erspan_changelink() only runs when data[IFLA_GRE_ERSPAN_VER] is present (net/ipv4/ip_gre.c:1534).
- [Medium] erspan_changelink() updates fields the lockless transmit path depends on in two separate steps.
- [Low] The new dev->needed_headroom += len; in erspan_changelink() (net/ipv4/ip_gre.c:1540) does not go through…
- [Low] The new dev->needed_headroom += len; in erspan_changelink() (net/ipv4/ip_gre.c:1540) is a plain read-modify-write under RTNL.
--
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 then called from erspan_changelink() when IFLA_GRE_ERSPAN_VER is present. The change in t->hlen is applied as a delta to dev->needed_headroom and, unless IFLA_MTU is given, to dev->mtu.
Quoted 14 lines
net/ipv4/ip_gre.c +1
-1@@ -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 1361
Quoted 2 lines
net/ipv4/ip_gre.c +0
-0@@ -1360,9 +1360,16 @@ static int erspan_tunnel_init(struct net_device *dev)else tunnel->tun_hlen =8 ;/* 8-byte GRE hdr. */
[Severity: High]
Can a v1/v2 tunnel end up with tun_hlen = 8 while gre_build_header() only writes 4 bytes?
erspan_calc_hlen() picks tun_hlen from the version alone. The number of bytes actually written depends on the flags erspan_xmit() passes, which are o_flags with KEY always cleared:
net/ipv4/ip_gre.c:erspan_xmit() {
...
__clear_bit(IP_TUNNEL_KEY_BIT, flags);
__gre_xmit(skb, dev, &tunnel->parms.iph, proto, flags);
...
}erspan_validate() only checks the OR of IFLA_GRE_OFLAGS and IFLA_GRE_IFLAGS, so o_flags can lack SEQ. Two ways to get there:
- OFLAGS=KEY with IFLAGS=SEQ|KEY (iproute2 "iseq okey")
- OFLAGS absent, where ipgre_netlink_parms() memsets parms and o_flags ends up 0
erspan_changelink() then copies p.o_flags into t->parms.o_flags.
With SEQ missing, erspan_xmit() passes no flags. gre_build_header(skb, 8, ...) then pushes 8 bytes but writes only greh->flags and greh->protocol. Bytes 4-7 are uninitialized headroom and are sent to the peer.
Before this patch, a v0 -> v1/v2 changelink with such flags kept tun_hlen = 4. Now erspan_calc_hlen() sets tun_hlen = 8, which leads to the same leak the commit message describes. The same state can also be set up at newlink time.
Would it be better to compute tun_hlen from the emitted flags, as gre_calc_hlen() does, or to force SEQ for v1/v2?
Quoted 4 lines
net/ipv4/ip_gre.c +1
-1- tunnel->parms.iph.protocol =IPPROTO_GRE ; tunnel->hlen = tunnel->tun_hlen + tunnel->encap_hlen +erspan_hdr_len (tunnel->erspan_ver); +}
[ ... ]
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1533
Quoted 4 lines
net/ipv4/ip_gre.c +0
-0@@ -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);
[Severity: High]
What happens to erspan_ver if ip_tunnel_changelink() fails?
erspan_netlink_parms() writes the new version into the live tunnel before ip_tunnel_changelink() runs:
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]);
...
}On error, erspan_changelink() returns before it reaches the new erspan_calc_hlen() block:
err = ip_tunnel_changelink(dev, tb, &p, fwmark); if (err < 0) return err;
ip_tunnel_changelink() can fail in three ways:
- -EEXIST when the new parameters match another tunnel in ip_tunnel_find()
- -EINVAL on the erspan0 fallback device
- -ENODEV while the device is unregistering
Take a v1 tunnel that receives ERSPAN_VER=0 with remote/key matching another tunnel. It gets -EEXIST but keeps erspan_ver = 0 and tun_hlen = 8. erspan_xmit() then clears SEQ and KEY, and __gre_xmit() pushes 8 bytes but writes only 4. That sends 4 bytes of uninitialized headroom to the peer in every packet.
In the other direction, a failed v0 -> v1/v2 changelink leaves tun_hlen = 4 while the larger ERSPAN header is built.
Isn't this the same leak the commit message says it fixes?
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1534
Quoted 1 line
net/ipv4/ip_gre.c +1
-0+if (data && data[IFLA_GRE_ERSPAN_VER ]) {
[Severity: Medium]
Should this recompute run on every changelink, not only when IFLA_GRE_ERSPAN_VER is present?
A changelink with only IFLA_GRE_ENCAP_* attributes (plus the flags erspan_validate() requires) still runs ip_tunnel_encap_setup(). That drops the ERSPAN header length from t->hlen:
net/ipv4/ip_tunnel.c:ip_tunnel_encap_setup() {
...
WRITE_ONCE(t->encap_hlen, hlen);
WRITE_ONCE(t->hlen, hlen + t->tun_hlen);
...
}The truncation predates this patch, but the new delta logic builds on it. A later changelink with only ERSPAN_VER=1 and no encap attributes reads the truncated baseline (8 + enc) and recomputes 8 + enc + 12. It then adds 12 to needed_headroom and subtracts 12 from the MTU, even though nothing changed.
The IPv6 code cited in the commit message, ip6erspan_tnl_change()->ip6erspan_tnl_link_config()->ip6erspan_calc_hlen(), recomputes hlen and assigns needed_headroom absolutely on every changelink:
tunnel->dev->needed_headroom = LL_MAX_HEADER + t_hlen;
Would following that model remove the dependency on the previously stored t->hlen?
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1535
Quoted 1 line
net/ipv4/ip_gre.c +1
-0+int len = t->hlen;
[Severity: High]
Is t->hlen still the old ERSPAN header length at this point?
ipgre_newlink_encap_setup() runs earlier in erspan_changelink(). If any IFLA_GRE_ENCAP_TYPE/FLAGS/SPORT/DPORT attribute is present, ip_tunnel_encap_setup() has already overwritten t->hlen without the erspan_hdr_len() term:
WRITE_ONCE(t->hlen, hlen + t->tun_hlen);
erspan_calc_hlen() then adds erspan_hdr_len() back, so the delta becomes (tun_new - tun_old) + erspan_hdr_len(new_ver). In practice:
- an unchanged v1 tunnel gains 12 in needed_headroom and loses 12 from the MTU on every changelink (16 for v2), until the MTU reaches 68
- a v1 -> v0 change applies -4 instead of -16
iproute2's "ip link set DEV type erspan ..." sends all ENCAP_* attributes and ERSPAN_VER on every changelink, so a routine change such as ttl triggers this.
needed_headroom is an unsigned short and the add is not clamped. After repeated calls it goes past the 512 cap and eventually wraps. erspan_xmit() then passes that value toskb_cow_head(skb, dev->needed_headroom).
Before this patch, erspan_changelink() never changed mtu or needed_headroom. ipgre_link_update() avoids this problem by taking its delta from tun_hlen, which encap setup does not overwrite.
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1537
Quoted 2 lines
net/ipv4/ip_gre.c +2
-0+ +erspan_calc_hlen (dev);
[Severity: Medium]
Can erspan_xmit() see the new erspan_ver together with the old tun_hlen?
erspan_netlink_parms() sets t->erspan_ver well before this call. In between, ip_tunnel_changelink() takes tunnels_lock, re-hashes the tunnel, may call ip_tunnel_bind_dev(), and runs netdev_state_change().
erspan devices are lltx. The transmit path reads tunnel->erspan_ver and then, separately, tunnel->tun_hlen, without RTNL or tunnels_lock:
net/ipv4/ip_gre.c:erspan_xmit() {
...
if (tunnel->erspan_ver == 0) {
proto = htons(ETH_P_ERSPAN);
__clear_bit(IP_TUNNEL_SEQ_BIT, flags);
...
}net/ipv4/ip_gre.c:__gre_xmit() {
...
gre_build_header(skb, tunnel->tun_hlen,
...
}During a v1 -> v0 downgrade, packets sent in that window have SEQ/KEY cleared while tun_hlen is still 8, so 4 bytes of uninitialized headroom leak. During v0 -> v1/v2, packets carry the new ERSPAN header with the old 4-byte GRE header.
The window is much narrower than before. Could the xmit path compute the GRE length from the same erspan_ver snapshot it uses to build the header, the way erspan_fb_xmit() computes tunnel_hlen locally?
net/ipv4/ip_gre.c static int erspan_changelink(struct net_device *dev, struct nlattr *tb[], · line 1540
Quoted 3 lines
net/ipv4/ip_gre.c +3
-0+ len = t->hlen - len; +if (len) { + dev->needed_headroom += len;
[Severity: Low]
Should this go through ip_tunnel_limit_headroom()?
The other writers on these paths clamp the value to 512, for example ip_tunnel_bind_dev():
dev->needed_headroom = ip_tunnel_limit_headroom(t_hlen + hlen);
ip6gre_tnl_link_config_route() on the IPv6 ERSPAN side does the same.
If needed_headroom is already at the 512 cap, a v0 -> v2 change pushes it to 532. Combined with the wrong delta above, nothing limits how far this unsigned short field can grow.
ipgre_link_update() already has the same unclamped pattern, but this patch adds a new instance on the ERSPAN path.
[Severity: Low]
Can this read-modify-write race with the transmit path?
ip_tunnel_xmit() updates the same field without a lock, from BH, via ip_tunnel_adj_headroom():
if (headroom > READ_ONCE(dev->needed_headroom)) WRITE_ONCE(dev->needed_headroom, headroom);
Suppose xmit reads N, changelink stores N + len, and xmit then stores Y with N < Y < N + len. The increase from changelink is lost. The plain access here is also an unannotated data race that KCSAN would report.
Quoted 8 lines
net/ipv4/ip_gre.c +6
-0+if (!tb[IFLA_MTU ]) +WRITE_ONCE (dev->mtu,max_t (int, dev->mtu - len, +68 )); + } + } +return 0 ; }
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120060254.3766851-1-bsdhenrymartin%40gmail.com