[PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
From: Piyus Kedia
Date: Tue Nov 17 2026 - 01:21:13 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.
A THIRD SITE HAS THE SAME THREE LINES, in __input_process_payload()
(net/xfrm/xfrm_iptfs.c), reached when an IPTFS payload is decapsulated:
skb->protocol = protocol;
skb_mac_header_rebuild(skb);
if (skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;
and it has its own KASAN report, a use-after-free rather than an
out-of-bounds, for the reason given below.
That function already knows the test is needed. Its other mac-header path
is guarded by old_mac, which it computes at the top as
old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;
and only rebuilds the header when that is non-NULL. The site above omits
the same check.
WHY THE THREE REPORTS CARRY DIFFERENT TITLES, which is worth stating so
they are not read as three defects. The address written is a fixed
skb->head + 0xffff + offsetof(struct ethhdr, h_proto), so what it lands in
is whatever the allocator placed 16 pages past the skb: a live object gives
slab-out-of-bounds, a freed one or a page back on the buddy free list gives
use-after-free, and a different LIVE object gives nothing at all, since
generic KASAN only tests whether the target address is poisoned. Across
six boots of one reproducer the write executed in five and KASAN reported
it in one. On a kernel without KASAN it is always silent.
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 what
__input_process_payload() already does for old_mac, and it leaves the
behaviour of every well-formed skb unchanged.
Link: https://lore.kernel.org/r/20261116174531.2540348-2-piyus@xxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
v2:
- guard the third site, net/xfrm/xfrm_iptfs.c in __input_process_payload().
It has the same three lines and its own KASAN report, a use-after-free,
and v1 left it alone - which would have closed two write sites of the
three while leaving a confirmed one live.
- say why the three reports carry different titles (slab-out-of-bounds,
use-after-free, use-after-free): the address is fixed relative to
skb->head, so the verdict is decided by whatever the allocator put 16
pages later, and a live neighbour gives no report at all.
- cite __input_process_payload()'s own old_mac computation as the in-tree
precedent alongside xfrm4_transport_input()'s.
No change to the two hunks v1 carried.
Tested at runtime on mainline before and after: both kernels 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. 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 one-in-five KASAN rate is the point made in the changelog about the
verdict depending on the neighbour.
checkpatch reports one thing, the missing Fixes: tag. Neither tree we
reproduced on carries history for these files, so we could not determine the
introducing commit; we will add it if you can name it.
net/xfrm/xfrm_input.c | 4 ++--
net/xfrm/xfrm_iptfs.c | 2 +-
2 files changed, 3 insertions(+), 3 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;
diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
index 0313a43aa..41af72a92 100644
--- a/net/xfrm/xfrm_iptfs.c
+++ b/net/xfrm/xfrm_iptfs.c
@@ -1099,7 +1099,7 @@ static bool __input_process_payload(struct xfrm_state *x, u32 data,
skb->protocol = protocol;
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;
/* all pointers could be changed now reset walk */
--
2.34.1