From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.167.195 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796282679; b=5amyURN/tp/Ajqt8KOdv8VcHkYI3DX1lc3zzj7fACG4j3MLX5JT/y0eDIs48r+vz/oeN p7fDWDctrXm1iOZnz/LvF/rLijf0hsmO+5Q0zyFLCCOkYcaX7hEf2koo+zHFW0wunlNIe y50MWpYIglglvkQHG4Cy7jtQd2vG3pyCRl4jrr1JMOxa1Ssq5w6+fQ1imsFnF+DtMhY32 QKsl3s9+Nh4kmEcdg2MUyMVktOrZU8NSMkeHHwpNgtIrKbyFetImGAar4L8jwStl/FbXL bdzk97ExTMfwQ0A2kMW6LPB5nxe7xuKzJjIdB9enogyjEvqjKswFO4W71rI73brWTbw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796282679; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-ID:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=sft0GIyt0emind+GD+qyj6pXNzHmm2/7Df279St87vE=; b=u9OnPR8OQ1Eu4dghYT3zoHRwhiD2dKZnZD9Ry5gaZVU7mC2YBzpivlKmmX5mjxVzG3Wx eiqHS/ZzcYL1QLmHQbeE82V663pGgEAvIuZaExvCIrkr6yAhX1kUJ4i1MR5riYJcnyTVz pyxASYd885l1OA35WKw6KIZno+Njl6mxK7MXfXCQoMxMKNbNq+/Asi1PpRcUWbTCSUnKR USwlTg4Z+snj2becqwko6Ljizt8+kFrqUC5Fo+EaSZiz5Uy7oSQYXQzr8b59I7qrouG5D xlnbLNOFt1+fiQWKfV4c1+E2bwgNo4kGZ/BW+DLv+pMbFJbIco7hL2BQQTmrtbZYy0Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DLkX854t; arc=none smtp.remote-ip=209.85.167.195 Received: from mail-oi1-f195.google.com (mail-oi1-f195.google.com [209.85.167.195]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 55C72606A322 for ; Thu, 3 Dec 2026 07:24:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 55C72606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=DLkX854t Received: by mail-oi1-f195.google.com with SMTP id 5614622812f47-53dbe9b8d6eso593793b6e.1 for ; Wed, 02 Dec 2026 23:24:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796282677; x=1796887477; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=sft0GIyt0emind+GD+qyj6pXNzHmm2/7Df279St87vE=; b=DLkX854tUQnqogYRbtwT2+mC2o6QRIF5e7Wzui3rUfJAg4U1aMC88qJq6ya04eeeUm g15PLMPrkrS3KHrZYV9IdQVqyJPtGU+s5GG561L3HkQxAVk9CpCw7TpQszEWs7VVD7Zx 0FOnmfW5aqil/h2WiD4/nDL/xvvBWuxlvSpCr52jUEd3vgUHjkoIylJryeZWL04ItYs3 pQSTtdqovVmlQGDxX8yKfTC5wEbqTUAh5DU39EIYu66DOzUImQs7JGArBvkUwqkRVQrN U6sbn3pueNFS8ilgLlBfCEnsB8CiJmIhKPDtAVpmxQBJOIXItc1iZVBXeeakgjG+BK08 3O/g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796282677; x=1796887477; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=sft0GIyt0emind+GD+qyj6pXNzHmm2/7Df279St87vE=; b=NnB94soTlGMqBTReYubmUrizH25jhQXAkPmPfs1ErgjfdJU1PUbp1kC4/2bC1q+aqV Mkf247NRzeox8gNt2Z75cE+M9jxxf6OcFjX5z+zJwYojz3OHH3BkjZy86yMOYiZ7djHC XRMQ9DF+puogsdYq9ufcyMbpoZxlFKAkEPflCChxJQ79EJL1E633PSdMxinlKFG4I7if K3aB1ZzslnlKwC01uYbQfMFUF5/Che9qDgzwY1SWHWqmnTjPf6DFq9/AC0FO+fDTCVIi WsutPJO6rUELiMtICxTJKPd7QEqAPvVv2bQBYR5dGc5YEiOwbIUraPgh9z6zbB0CNG0u FUKA== X-Forwarded-Encrypted: i=1; AL+hErH3jSZCQoYMxEILeydJye/MmfAr39M5xQ9vpxZCr4vmr4ZxOS1KK57FRFT5wTlx50OJTrC0LF0=@vger.kernel.org X-Gm-Message-State: APuU4Z/ciRJSu5l6EvQU6+2qtbYXHdiWZD0v2PQmHwTSeP+TOMoG5q23 p/1msjyJb4n+3dRWRlF64zTpfDSDOCYO0MXFps4nrDWfeG6i2u/U0RdI X-Gm-Gg: AZFdTZehYI7ORqIDhvrqwU2jgqgSJN6AP/yOlhRAU5l57HLhR6yzu1pEacXfPMwS3KA MGmg2xqvzvs47S6haUeoxtoZUpUHQHxrgJ+F/AR+RDyrfCoEL3eSrCE4y2ZHUtDgaI3nt85zJdr EKl+Bt3hXWLsGzM8gZfinAcl/s2b5+hmTZCovOjSXorFBm6/PbWiuA2DQLN2s2sYdaDRtpq8iwT mbUSZFyl6SR/TVoYbTnqmbW2umIkw5+8a9w5tMZ7+K7lM0pwWRz95nuWhp1PgJShJDTvqCtKmrc D8RRT5PDLg0I/XoKjFtFV7vDb0nMA/ZVc3lCpjZ4/vLoJ9D5z3qUybU1eZsSYdJ2kUHBi0QOtNe AgxRm6Es5yAtOlI2C0xuVTiPy9KLWqMfcIyHTXyZLCrLR1MNkp6EXFSZscGO/B3dIMxt2ZAgryG E97iWKWqSC2ssFiWf+pU2NoSkhqts+pnJpiYwyC2/jxTitHHFns1Gzzbyc2D/jPu9MBT5M9KPFu hSfaEgjKJvt2OvhWdzWwQ3xc4iTzLccbYXniUZZ4t1CBHg7PA5ZUkleVb+af4QnBXU5MU9L2U6m zBghRIdjWbB9GXAu1YlSN5NbwzI7ULVbiLP2ykoNdDQUImeWJGh6iRilmdGFVzWzw6R3 X-Received: by 2002:a05:6808:c2aa:b0:4dd:f8bb:e1bd with SMTP id 5614622812f47-57f07b89b5bmr1067236b6e.13.1796282677201; Wed, 02 Dec 2026 23:24:37 -0800 (PST) Received: from localhost.localdomain ([14.116.239.40]) by smtp.gmail.com with ESMTPSA id 5614622812f47-57a347636b9sm33125756b6e.7.2026.12.02.23.24.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Dec 2026 23:24:35 -0800 (PST) From: Henry Martin 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 , 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 Message-ID: <20261203072423.147308-1-bsdhenrymartin@gmail.com> X-Mailer: git-send-email 2.43.7 X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 --- 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