[PATCH] xfrm: do not write the ethernet header when the mac header is unset

From: Piyus Kedia

Date: Mon Nov 16 2026 - 12:46:22 EST


xfrm4_remove_tunnel_encap() and xfrm6_remove_tunnel_encap() restore the
mac header after removing the tunnel header and then stamp the protocol
into it:

skb_reset_network_header(skb);
skb_mac_header_rebuild(skb);
if (skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;

skb_mac_header_rebuild() does nothing at all when the mac header was
never set:

static inline void skb_mac_header_rebuild(struct sk_buff *skb)
{
if (skb_mac_header_was_set(skb)) {
...
}
}

and it does not clear skb->mac_len either, so on an skb whose mac header
has been unset while mac_len stayed behind, the guard is true and the
store goes through an unset header. skb_mac_header() is
skb->head + skb->mac_header and the unset value of that u16 is 0xffff,
so eth_hdr(skb)->h_proto is 65535 + offsetof(struct ethhdr, h_proto)
bytes past skb->head: a two-byte out-of-bounds write landing far outside
the skb, in whatever object happens to be there.

skb_unset_mac_header() leaves skb->mac_len alone, and of its callers only
skb_carve_reset_headers() clears mac_len by hand. end_dt_vrf_rcv()
(SRv6 End.DT4/End.DT6) unsets the mac header on the receive path so the
VRF device can rebuild it, and leaves mac_len at the value the original
ethernet frame had, which is how an skb in this state reaches xfrm input:

tun_get_user -> netif_receive_skb -> lwtunnel_input
-> seg6_local_input_core -> End.DT4 -> end_dt_vrf_rcv
-> skb_unset_mac_header (mac_len stays 14)
-> VRF l3_rcv -> ip_local_deliver_finish -> xfrm4_esp_rcv
-> xfrm_input -> xfrm4_remove_tunnel_encap

BUG: KASAN: slab-out-of-bounds in xfrm4_remove_tunnel_encap+0x17d/0x220
net/xfrm/xfrm_input.c:258
Write of size 2 at addr ffff88805f85800b by task syz-executor/10831
...
The buggy address belongs to the object at ffff88805f858000
which belongs to the cache zswap_entry of size 56

The address does not depend on mac_len - it is always
skb->head + 0xffff + offsetof(struct ethhdr, h_proto), since the unset
sentinel is what mac_header holds - and mac_len only decides whether the
write happens. The value is skb->protocol. It is two bytes at a fixed
64KB offset, crossing into an unrelated slab cache.

Only write the ethernet header when there is one. This mirrors what
xfrm4_transport_input() already does, where the header length is taken as
skb_mac_header_was_set(skb) ? skb_mac_header_len(skb) : 0, and it leaves
the behaviour of every well-formed skb unchanged.

Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---

Tested at runtime on mainline itself, before and after. Both kernels were
built from aebb72744764 ("Linux 7.4-rc3") in one scripted run, x86_64 with
CONFIG_KASAN_GENERIC, CONFIG_DEBUG_NET, CONFIG_IPV6_SEG6_LWTUNNEL and
CONFIG_NET_VRF, so each image is provably from its own commit. Six boots
each, injecting after the uptime passed 130s.

The skb_reset_mac_len() warnings at include/linux/skbuff.h:2995 are the
control: they fire when the unset-header skb reaches the receive path, and
this patch does not touch that site.

skb came through reached the write KASAN
unpatched 5 of 6 boots 5 of 5 1
patched 2 of 6 boots 0 of 2 0

Every unpatched boot in which the skb arrived reached the write; no patched
boot did, including the two in which the control shows the same skb
arriving. The assertion inside skb_mac_header() disappears from the patched
runs for the reason the change implies - eth_hdr(skb) is no longer evaluated
when the header is unset, so skb_mac_header() is not called at that site -
while the unrelated warnings remain.

checkpatch reports one thing, the missing Fixes: tag. Neither tree we
reproduced on carries history for the file, so we could not determine the
introducing commit; we will add the tag if you can name it.

net/xfrm/xfrm_input.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
index 5ed87d513..624ff8055 100644
--- a/net/xfrm/xfrm_input.c
+++ b/net/xfrm/xfrm_input.c
@@ -254,7 +254,7 @@ static int xfrm4_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)

skb_reset_network_header(skb);
skb_mac_header_rebuild(skb);
- if (skb->mac_len)
+ if (skb_mac_header_was_set(skb) && skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;

err = 0;
@@ -291,7 +291,7 @@ static int xfrm6_remove_tunnel_encap(struct xfrm_state *x, struct sk_buff *skb)

skb_reset_network_header(skb);
skb_mac_header_rebuild(skb);
- if (skb->mac_len)
+ if (skb_mac_header_was_set(skb) && skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;

err = 0;
--
2.34.1