Skip to contentSkip to conversation

net: iptunnel: fix stale transport header after GRE/TEB decap

PATCH In review Jiayuan Chen 3 participants 5 messages Apr 19 – Apr 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: iptunnel: fix stale transport header after GRE/TEB decap

syzbot reported a BUG.

I found that after GRE decapsulation in gretap/ip6gretap paths, the transport_header becomes stale with a negative offset. The sequence is:

1. Before decap, transport_header points to the outer L4 (GRE) header.
2. __iptunnel_pull_header() calls skb_pull_rcsum() to advance skb->data past the GRE header, but does not update transport_header.
3. For TEB (gretap/ip6gretap), eth_type_trans() in ip_tunnel_rcv() / __ip6_tnl_rcv() further pulls ETH_HLEN (14 bytes) from skb->data.

After these two pulls, skb->data has moved forward while transport_header still points to the old (now behind skb->data) position, resulting in a negative skb_transport_offset(): typically -4 after GRE pull alone, or -18 after GRE + inner Ethernet pull.

In the normal case where the inner frame is a recognizable protocol (e.g., IPv4/TCP), the transport_header is subsequently overwritten by ip_rcv_core() (or inet_gro_receive() on the GRO path) via skb_set_transport_header(), and the stale value never reaches downstream consumers. However, if the inner frame cannot be parsed (e.g., eth_type_trans() classifies it as ETH_P_802_2 due to a zero/invalid inner Ethernet header), neither rescue runs, and the stale offset persists into __netif_receive_skb_core().

When this stale offset is combined with contradictory GSO metadata (e.g., SKB_GSO_TCPV4 injected via virtio_net_hdr from a tun device), qdisc_pkt_len_segs_init() trusts the negative offset: the unsigned wraparound makes pskb_may_pull() effectively a no-op, and __tcp_hdrlen() then reads from an invalid memory location, causing a use-after-free.

The UAF only triggers on the GSO path, where qdisc_pkt_len_segs_init() dereferences the transport header to compute per-segment length. Fix this by introducing iptunnel_rebuild_transport_header(), which is a no-op for non-GSO packets and otherwise re-probes the transport header via the flow dissector. If re-probing fails, the contradictory GSO metadata is cleared via skb_gso_reset() so downstream consumers cannot trust stale offsets. Restricting the rebuild to GSO packets keeps the flow-dissector cost off the common rx fast path.

reproducer: https://gist.github.com/mrpre/5ba943fd86367af748b70de99263da4b

Fixes c54419321455 ("GRE: Refactor GRE tunneling code.")
Fixes 0d3c703a9d17 ("ipv6: Cleanup IPv6 tunnel receive path")
Reported-by [email protected]

---

As a follow-up for production reliability, I am wondering whether we can extend the existing safety net in __netif_receive_skb_core() to also handle set-but-negative transport_header:

        if (!skb_transport_header_was_set(skb) ||
            skb_transport_offset(skb) < 0)
                skb_reset_transport_header(skb);
---
 include/net/ip_tunnels.h | 12 ++++++++++++
 net/ipv4/ip_tunnel.c     |  2 ++
 net/ipv6/ip6_tunnel.c    |  2 ++
 3 files changed, 16 insertions(+)
include/net/ip_tunnels.h +12 -0
index d708b66e55cd..9b4e662833a1 100644
@@ -662,6 +662,18 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
 	return 0;
 }
 
+static inline void iptunnel_rebuild_transport_header(struct sk_buff *skb)
+{
+	if (!skb_is_gso(skb))
+		return;
+
+	skb->transport_header = (typeof(skb->transport_header))~0U;
+	skb_probe_transport_header(skb);
+
+	if (!skb_transport_header_was_set(skb))
+		skb_gso_reset(skb);
+}
+
 static inline void iptunnel_xmit_stats(struct net_device *dev, int pkt_len)
 {
 	if (pkt_len > 0) {
net/ipv4/ip_tunnel.c +2 -0
index 50d0f5fe4e4c..c46be68cfafa 100644
@@ -445,6 +445,8 @@ int ip_tunnel_rcv(struct ip_tunnel *tunnel, struct sk_buff *skb,
 	if (tun_dst)
 		skb_dst_set(skb, (struct dst_entry *)tun_dst);
 
+	iptunnel_rebuild_transport_header(skb);
+
 	gro_cells_receive(&tunnel->gro_cells, skb);
 	return 0;
 
net/ipv6/ip6_tunnel.c +2 -0
index 46bc06506470..f95348cf3c77 100644
@@ -879,6 +879,8 @@ static int __ip6_tnl_rcv(struct ip6_tnl *tunnel, struct sk_buff *skb,
 	if (tun_dst)
 		skb_dst_set(skb, (struct dst_entry *)tun_dst);
 
+	iptunnel_rebuild_transport_header(skb);
+
 	gro_cells_receive(&tunnel->gro_cells, skb);
 	return 0;
 
Signature · 1 line
2.43.0
#1 lore raw
Quoted 72 lines · Jiayuan Chen

syzbot reported a BUG.

I found that after GRE decapsulation in gretap/ip6gretap paths, the transport_header becomes stale with a negative offset. The sequence is:

1. Before decap, transport_header points to the outer L4 (GRE) header.
2. __iptunnel_pull_header() calls skb_pull_rcsum() to advance skb->data past the GRE header, but does not update transport_header.
3. For TEB (gretap/ip6gretap), eth_type_trans() in ip_tunnel_rcv() / __ip6_tnl_rcv() further pulls ETH_HLEN (14 bytes) from skb->data.

After these two pulls, skb->data has moved forward while transport_header still points to the old (now behind skb->data) position, resulting in a negative skb_transport_offset(): typically -4 after GRE pull alone, or -18 after GRE + inner Ethernet pull.

In the normal case where the inner frame is a recognizable protocol (e.g., IPv4/TCP), the transport_header is subsequently overwritten by ip_rcv_core() (or inet_gro_receive() on the GRO path) via skb_set_transport_header(), and the stale value never reaches downstream consumers. However, if the inner frame cannot be parsed (e.g., eth_type_trans() classifies it as ETH_P_802_2 due to a zero/invalid inner Ethernet header), neither rescue runs, and the stale offset persists into __netif_receive_skb_core().

When this stale offset is combined with contradictory GSO metadata (e.g., SKB_GSO_TCPV4 injected via virtio_net_hdr from a tun device), qdisc_pkt_len_segs_init() trusts the negative offset: the unsigned wraparound makes pskb_may_pull() effectively a no-op, and __tcp_hdrlen() then reads from an invalid memory location, causing a use-after-free.

The UAF only triggers on the GSO path, where qdisc_pkt_len_segs_init() dereferences the transport header to compute per-segment length. Fix this by introducing iptunnel_rebuild_transport_header(), which is a no-op for non-GSO packets and otherwise re-probes the transport header via the flow dissector. If re-probing fails, the contradictory GSO metadata is cleared via skb_gso_reset() so downstream consumers cannot trust stale offsets. Restricting the rebuild to GSO packets keeps the flow-dissector cost off the common rx fast path.

reproducer: https://gist.github.com/mrpre/5ba943fd86367af748b70de99263da4b

Fixes c54419321455 ("GRE: Refactor GRE tunneling code.")
Fixes 0d3c703a9d17 ("ipv6: Cleanup IPv6 tunnel receive path")
Reported-by [email protected]

---

As a follow-up for production reliability, I am wondering whether we can extend the existing safety net in __netif_receive_skb_core() to also handle set-but-negative transport_header:

        if (!skb_transport_header_was_set(skb) ||
            skb_transport_offset(skb) < 0)
                skb_reset_transport_header(skb);
---
 include/net/ip_tunnels.h | 12 ++++++++++++
 net/ipv4/ip_tunnel.c     |  2 ++
 net/ipv6/ip6_tunnel.c    |  2 ++
 3 files changed, 16 insertions(+)
include/net/ip_tunnels.h +0 -0
index d708b66e55cd..9b4e662833a1 100644
@@ -662,6 +662,18 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
        return 0;
 }
 

include/net/ip_tunnels.h static inline int iptunnel_pull_offloads(struct sk_buff *skb) · line 674

Quoted 10 lines
include/net/ip_tunnels.h +10 -0
+static inline void iptunnel_rebuild_transport_header(struct sk_buff *skb)
+{
+       if (!skb_is_gso(skb))
+               return;
+
+       skb->transport_header = (typeof(skb->transport_header))~0U;
+       skb_probe_transport_header(skb);
+
+       if (!skb_transport_header_was_set(skb))
+               skb_gso_reset(skb);

I do not think this makes sense.
What is a valid case for this packet being processed further? The buggy packet must be dropped, instead of being mangled like this.

#2 lore raw

[...]

Quoted 4 lines
Quoted 4 lines
  +662,18 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
         return 0;
  }
Quoted 13 lines

include/net/ip_tunnels.h static inline int iptunnel_pull_offloads(struct sk_buff *skb) · line 674

Quoted 10 lines
include/net/ip_tunnels.h +10 -0
+static inline void iptunnel_rebuild_transport_header(struct sk_buff *skb)
+{
+       if (!skb_is_gso(skb))
+               return;
+
+       skb->transport_header = (typeof(skb->transport_header))~0U;
+       skb_probe_transport_header(skb);
+
+       if (!skb_transport_header_was_set(skb))
+               skb_gso_reset(skb);

I do not think this makes sense.
What is a valid case for this packet being processed further? The buggy packet must be dropped, instead of being mangled like this.

Hi Eric,

The reproducer builds a gre frame whose inner Ethernet header is all-zero. Tracing the skb through RX:

1. At GRE decap exit, skb_transport_offset(skb) < 0 is the rule, not the exception.

It is negative for every packet leaving the tunnel, including perfectly well-formed inner IPv4 traffic because the tunnel leaves skb->transport_header at the outer L4 offset while
pskb_pull() has already advanced skb->data past it. skb_transport_header_was_set() stays true, so downstream code that trusts that flag now trusts a stale, negative offset.

2. GRO repairs it — but only for protocols it knows.

In dev_gro_receive(), skb->protocol is dispatched through the offload table. For ETH_P_IP, inet_gro_receive() calls skb_set_transport_header(skb, skb_gro_offset(skb)), and the offset becomes valid again. But for malformed skb, dev_gro_receive just bypass it.

3. Both kinds then reach __netif_receive_skb_core().

So the skb that qdisc/tc/BPF segmenters later see has an invariant violation — _was_set == true but offset < 0 — that the core layer has no intention of catching for us.

My reading of this is that the tunnel decap path is producing an skb that doesn't honor the contract __netif_receive_skb_core() expects from its producers, and that it doesn't really make sense to ask GRE to parse or validate the inner L4 in order to fix this.

I'm thinking at the end of GRE decap, before handing the skb to gro_cells_receive(), call skb_reset_transport_header(skb).

Thanks,
Jiayuan

maintainer
#3 lore raw
Quoted 23 lines · Jiayuan Chen

[...]

Quoted 4 lines
Quoted 4 lines
  +662,18 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
         return 0;
  }
Quoted 13 lines

include/net/ip_tunnels.h static inline int iptunnel_pull_offloads(struct sk_buff *skb) · line 674

Quoted 10 lines
include/net/ip_tunnels.h +10 -0
+static inline void iptunnel_rebuild_transport_header(struct sk_buff *skb)
+{
+       if (!skb_is_gso(skb))
+               return;
+
+       skb->transport_header = (typeof(skb->transport_header))~0U;
+       skb_probe_transport_header(skb);
+
+       if (!skb_transport_header_was_set(skb))
+               skb_gso_reset(skb);

I do not think this makes sense.
What is a valid case for this packet being processed further? The buggy packet must be dropped, instead of being mangled like this.

Hi Eric,

The reproducer builds a gre frame whose inner Ethernet header is all-zero. Tracing the skb through RX:

Quoted 7 lines

1. At GRE decap exit, skb_transport_offset(skb) < 0 is the rule, not the exception.

It is negative for every packet leaving the tunnel, including perfectly well-formed inner IPv4 traffic
because the tunnel leaves skb->transport_header at the outer L4 offset while pskb_pull() has already advanced skb->data past it.

Is it? the transport header is an offset on top of skb->head, pskb_pull changes head only if the header is not in the linear part (and the transport offset is already invalid).

Quoted 10 lines

skb_transport_header_was_set() stays true, so downstream code that trusts that flag now trusts a stale, negative offset.

2. GRO repairs it — but only for protocols it knows.

In dev_gro_receive(), skb->protocol is dispatched through the offload table. For ETH_P_IP,
inet_gro_receive() calls skb_set_transport_header(skb, skb_gro_offset(skb)), and the offset
becomes valid again. But for malformed skb, dev_gro_receive just bypass it.

So only malformed packets cause trouble, right?

Quoted 14 lines

3. Both kinds then reach __netif_receive_skb_core().

So the skb that qdisc/tc/BPF segmenters later see has an invariant violation — _was_set == true but offset < 0 — that the core layer has no intention of catching for us.

My reading of this is that the tunnel decap path is producing an skb that doesn't
honor the contract __netif_receive_skb_core() expects from its producers, and that
it doesn't really make sense to ask GRE to parse or validate the inner L4 in order
to fix this.

Quoted 3 lines

I'm thinking at the end of GRE decap, before handing the skb to gro_cells_receive(),
call skb_reset_transport_header(skb).

My take is that you need to address the issue earlier than the current patch, dropping the malformed packets.

/P

#4 lore raw
Quoted 27 lines · Paolo Abeni
Quoted 26 lines · Jiayuan Chen

[...]

Quoted 4 lines
Quoted 4 lines
   +662,18 @@ static inline int iptunnel_pull_offloads(struct sk_buff *skb)
          return 0;
   }
Quoted 13 lines

include/net/ip_tunnels.h static inline int iptunnel_pull_offloads(struct sk_buff *skb) · line 674

Quoted 10 lines
include/net/ip_tunnels.h +10 -0
+static inline void iptunnel_rebuild_transport_header(struct sk_buff *skb)
+{
+       if (!skb_is_gso(skb))
+               return;
+
+       skb->transport_header = (typeof(skb->transport_header))~0U;
+       skb_probe_transport_header(skb);
+
+       if (!skb_transport_header_was_set(skb))
+               skb_gso_reset(skb);

I do not think this makes sense.
What is a valid case for this packet being processed further? The buggy packet must be dropped, instead of being mangled like this.

Hi Eric,

The reproducer builds a gre frame whose inner Ethernet header is all-zero. Tracing the skb through RX:

1. At GRE decap exit, skb_transport_offset(skb) < 0 is the rule, not the exception.

Quoted 7 lines
Quoted 4 lines

It is negative for every packet leaving the tunnel, including perfectly well-formed inner IPv4 traffic
because the tunnel leaves skb->transport_header at the outer L4 offset while pskb_pull() has already advanced skb->data past it.

Is it? the transport header is an offset on top of skb->head, pskb_pull changes head only if the header is not in the linear part (and the transport offset is already invalid).

Sorry, my wording was imprecise. The point is not that transport_header itself holds a negative value — it does not — but that after GRE processing, skb->data has advanced past the outer L4 while skb->transport_header is never touched, so skb_transport_offset(skb) ends up negative.

Quoted 12 lines
Quoted 10 lines

skb_transport_header_was_set() stays true, so downstream code that trusts that flag now trusts a stale, negative offset.

2. GRO repairs it — but only for protocols it knows.

In dev_gro_receive(), skb->protocol is dispatched through the offload table. For ETH_P_IP,
inet_gro_receive() calls skb_set_transport_header(skb, skb_gro_offset(skb)), and the offset
becomes valid again. But for malformed skb, dev_gro_receive just bypass it.

So only malformed packets cause trouble, right?

The negative offset is produced for every packet leaving GRE, not just malformed ones. What differs is what happens downstream:

- For well-formed inner IPv4, inet_gro_receive() calls skb_set_transport_header(skb, skb_gro_offset(skb)) and restores a valid offset before any consumer observes it.
- For malformed inner frames (e.g. skb->protocol == ETH_P_802_2 or other ), dev_gro_receive() finds no ptype and just passes the skb through. The stale negative offset survives into __netif_receive_skb_core().

So the UAF needs both conditions: GRE producing the stale offset *and*

no downstream rescue.

Quoted 14 lines
Quoted 14 lines

3. Both kinds then reach __netif_receive_skb_core().

So the skb that qdisc/tc/BPF segmenters later see has an invariant violation — _was_set == true but offset < 0 — that the core layer has no intention of catching for us.

My reading of this is that the tunnel decap path is producing an skb that doesn't
honor the contract __netif_receive_skb_core() expects from its producers, and that
it doesn't really make sense to ask GRE to parse or validate the inner L4 in order
to fix this.

Quoted 7 lines
Quoted 3 lines

I'm thinking at the end of GRE decap, before handing the skb to gro_cells_receive(),
call skb_reset_transport_header(skb).

My take is that you need to address the issue earlier than the current patch, dropping the malformed packets.

/P

Dropping at tunnel decap is a reasonable option, e.g.:

  if (unlikely(skb->protocol == htons(ETH_P_802_2) || skb->protocol == htons(ETH_P_802_3) || ....)) {
      kfree_skb_reason(skb, SKB_DROP_REASON_...); return 0;
  }

Two concerns about this approach, though:

1.It asks GRE to decide whether an inner L2 frame is "sensible", which I don't think should be GRE's responsibility — GRE is a generic L2/L3 tunnel and historically stays agnostic about the inner payload.

2. More importantly, filtering on ETH_P_802_2 / ETH_P_802_3 only covers the case where inner h_proto < ETH_P_802_3_MIN. The same stale-offset condition can also be reached with any inner ethertype that has no GRO receive callback resetting transport_header.

In my earlier reply to Eric I suggested calling skb_reset_transport_header(skb) at the tunnel decap exit instead. A few reasons I think this is a cleaner fix:

1.It is inner-protocol agnostic — it normalizes the skb regardless of what the inner ethertype happens to be, so ARP/PPPoE/... are fixed by the same one-liner.

2.ip_tunnel_rcv() already updates mac_header (via eth_type_trans) and network_header (ip_tunnel.c:414). transport_header is the only one of the three left pointing at the outer offset; resetting it here is completing what the function is already doing for the other two.

3.Malformed frames that carry it downstream simply fail ptype_base dispatch and are dropped there, the same way any unknown-ethertype frame is dropped today.