mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Anton Danilov <littlesmilingcloud@gmail.com>
To: netdev@vger.kernel.org
Cc: Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	"David S. Miller" <davem@davemloft.net>,
	Simon Horman <horms@kernel.org>, David Ahern <dsahern@kernel.org>,
	Willem de Bruijn <willemb@google.com>,
	Ido Schimmel <idosch@nvidia.com>, Florian Westphal <fw@strlen.de>,
	William Tu <u9012063@gmail.com>,
	Meenakshi Vohra <mvohra@vmware.com>,
	Matthias May <matthias.may@westermo.com>,
	Mazin Al Haddad <mazin@getstate.dev>,
	Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH net-next v7 1/5] erspan: bound the VLAN tag read in erspan_build_header{,_v2}()
Date: Tue,  1 Dec 2026 03:59:37 +0300	[thread overview]
Message-ID: <20261201005941.1536429-2-littlesmilingcloud@gmail.com> (raw)
In-Reply-To: <20261201005941.1536429-1-littlesmilingcloud@gmail.com>

erspan_build_header() and erspan_build_header_v2() decide whether the
mirrored frame is VLAN tagged by reading the EtherType at offset 12
and, for ETH_P_8021Q, the TCI at offset 14, without checking that the
frame is that long. Shorter frames do reach them: the transmit paths
check the length with pskb_inet_may_pull_reason() on IPv4 and
pskb_inet_may_pull() on IPv6. These only make sure that the IPv4 or
IPv6 header announced by skb->protocol is there. A frame
that announces anything else gets through, whatever its length.

syzbot reported the EtherType read for a short frame redirected into
an erspan device by bpf. It does not take bpf: a 10 byte frame
redirected into erspan or ip6erspan by tc mirred from a tun device
gives "KMSAN: uninit-value in erspan_build_header".

The TCI read has the same problem two bytes further on. A 14 byte
frame that ends right after the 0x8100 EtherType gets two bytes from
past its end copied into the VLAN field of the ERSPAN header, and KMSAN
reports them when the packet is received on the other side.

Read both fields with skb_header_pointer(), so that a frame too short
to carry a tag is treated as untagged.

The TCI read was found by an AI code review agent, in a review of
another series that touches these functions. Both reads were confirmed
in a VM on an unmodified kernel built with KMSAN, with the two frames
above. With this patch and the next one, which bounds the remaining
read of this kind, the same frames give no report on erspan.

Fixes: 84e54fe0a5ea ("gre: introduce native tunnel support for ERSPAN")
Fixes: f551c91de262 ("net: erspan: introduce erspan v2 for ip_gre")
Reported-by: syzbot+9e27778c0edc62cb97d8@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=9e27778c0edc62cb97d8
Signed-off-by: Anton Danilov <littlesmilingcloud@gmail.com>
---
 include/net/erspan.h | 15 ++++++---------
 1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/include/net/erspan.h b/include/net/erspan.h
index c6209e7b6c96..6bda757b6da2 100644
--- a/include/net/erspan.h
+++ b/include/net/erspan.h
@@ -174,13 +174,12 @@ static inline void erspan_build_header(struct sk_buff *skb,
 				u32 id, u32 index,
 				bool truncate, bool is_ipv4)
 {
-	struct ethhdr *eth = (struct ethhdr *)skb->data;
 	enum erspan_encap_type enc_type;
 	struct erspan_base_hdr *ershdr;
 	struct qtag_prefix {
 		__be16 eth_type;
 		__be16 tci;
-	} *qp;
+	} *qp, _qp;
 	u16 vlan_tci = 0;
 	u8 tos;
 	__be32 *idx;
@@ -194,8 +193,8 @@ static inline void erspan_build_header(struct sk_buff *skb,
 	/* If mirrored packet has vlan tag, extract tci and
 	 * preserve vlan header in the mirrored frame.
 	 */
-	if (eth->h_proto == htons(ETH_P_8021Q)) {
-		qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
+	qp = skb_header_pointer(skb, 2 * ETH_ALEN, sizeof(_qp), &_qp);
+	if (qp && qp->eth_type == htons(ETH_P_8021Q)) {
 		vlan_tci = ntohs(qp->tci);
 		enc_type = ERSPAN_ENCAP_INFRAME;
 	}
@@ -268,13 +267,12 @@ static inline void erspan_build_header_v2(struct sk_buff *skb,
 					  u32 id, u8 direction, u16 hwid,
 					  bool truncate, bool is_ipv4)
 {
-	struct ethhdr *eth = (struct ethhdr *)skb->data;
 	struct erspan_base_hdr *ershdr;
 	struct erspan_md2 *md2;
 	struct qtag_prefix {
 		__be16 eth_type;
 		__be16 tci;
-	} *qp;
+	} *qp, _qp;
 	u16 vlan_tci = 0;
 	u8 gra = 0; /* 100 usec */
 	u8 bso = 0; /* Bad/Short/Oversized */
@@ -288,10 +286,9 @@ static inline void erspan_build_header_v2(struct sk_buff *skb,
 	/* Unlike v1, v2 does not have En field,
 	 * so only extract vlan tci field.
 	 */
-	if (eth->h_proto == htons(ETH_P_8021Q)) {
-		qp = (struct qtag_prefix *)(skb->data + 2 * ETH_ALEN);
+	qp = skb_header_pointer(skb, 2 * ETH_ALEN, sizeof(_qp), &_qp);
+	if (qp && qp->eth_type == htons(ETH_P_8021Q))
 		vlan_tci = ntohs(qp->tci);
-	}
 
 	bso = erspan_detect_bso(skb);
 	skb_push(skb, sizeof(*ershdr) + ERSPAN_V2_MDSIZE);
-- 
2.47.3


  reply	other threads:[~2026-12-01  0:59 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01  0:59 [PATCH net-next v7 0/5] ip6_gre, erspan: fix inner header handling for VLAN-tagged and short frames Anton Danilov
2026-12-01  0:59 ` Anton Danilov [this message]
2026-12-01  0:59 ` [PATCH net-next v7 2/5] erspan: take the COS from the mirrored frame, not from the tunnel family Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 3/5] ip6_gre: classify the inner packet once on transmit Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 4/5] ip6_gre: go by the inner protocol behind VLAN tags in ip6erspan Anton Danilov
2026-12-01  0:59 ` [PATCH net-next v7 5/5] selftests: net: add a test for ip6gretap and ip6erspan with VLAN tags Anton Danilov

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=20261201005941.1536429-2-littlesmilingcloud@gmail.com \
    --to=littlesmilingcloud@gmail.com \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=matthias.may@westermo.com \
    --cc=mazin@getstate.dev \
    --cc=mvohra@vmware.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=u9012063@gmail.com \
    --cc=willemb@google.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

all inboxes | Powered by JetHome®