Re: [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
From: Eric Dumazet
Date: Tue Nov 17 2026 - 03:13:54 EST
Le mar. 17 nov. 2026 à 07:21, Piyus Kedia <piyus@xxxxxxxxxxx> a écrit :
>
> 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
I think you are fixing the wrong layer.
I would have used something more generic.
net: clear skb->mac_len in skb_unset_mac_header()
skb_unset_mac_header() marks the mac header as unset but leaves
skb->mac_len alone, so an skb can end up with no mac header and a
non-zero mac_len. Code that uses mac_len to decide whether a mac
header is present is then misled.
end_dt_vrf_rcv() (SRv6 End.DT4/End.DT6) unsets the mac header of the
decapsulated packet so that the VRF device can rebuild it. The VRF
device only does so when a packet sniffer is attached, so without one
the skb continues up the stack with mac_len == 14. In
xfrm4_remove_tunnel_encap() and xfrm6_remove_tunnel_encap(),
skb_mac_header_rebuild() is a no-op for such an skb, and the following
if (skb->mac_len)
eth_hdr(skb)->h_proto = skb->protocol;
writes two bytes at skb->head + 0xffff + offsetof(struct ethhdr, h_proto):
BUG: KASAN: slab-out-of-bounds in xfrm4_remove_tunnel_encap+0x17d/0x220
Write of size 2 at addr ffff88805f85800b by task syz-executor/10831
xfrm4_remove_tunnel_encap
xfrm_input
xfrm4_esp_rcv
ip_protocol_deliver_rcu
ip_local_deliver_finish
seg6_local_input_core
lwtunnel_input
__netif_receive_skb
netif_receive_skb
tun_rx_batched
tun_get_user
Clear mac_len in skb_unset_mac_header() so that all callers leave the
skb in a consistent state, and remove the now redundant assignment in
skb_carve_reset_headers(). The other callers are on egress paths
(__bpf_redirect_neigh(), bpf_lwt_push_ip_encap()) or only reach the
helper when the mac header was already unset (bpf_update_srh_state()),
and do not read mac_len afterwards.
The issue was reported on the list by Piyus Kedia and Kapil Vaswani,
who found it by fuzzing with AI assistance and reproduced the KASAN
splat on v7.4-rc3. Their reproducer was not published, so this patch
is build tested only.
Fixes: 664d6f86868b ("seg6: add support for the SRv6 End.DT4 behavior")
Reported-by: Piyus Kedia <piyus@xxxxxxxxxxx>
Reported-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Closes: https://lore.kernel.org/netdev/20261116174531.2540348-2-piyus@xxxxxxxxxxx/
Signed-off-by: Eric Dumazet <edumazet@xxxxxxxxxx>
diff --git a/include/linux/skbuff.h b/include/linux/skbuff.h
index 6787fcf6ac3e43fe87fa5f9007b6d99f7aea0b44..9cd8eff0309cc3a646dc49e83a0cea79512cf8f5
100644
--- a/include/linux/skbuff.h
+++ b/include/linux/skbuff.h
@@ -3186,6 +3186,7 @@ static inline u32 skb_mac_header_len(const
struct sk_buff *skb)
static inline void skb_unset_mac_header(struct sk_buff *skb)
{
skb->mac_header = (typeof(skb->mac_header))~0U;
+ skb->mac_len = 0;
}
static inline void skb_reset_mac_header(struct sk_buff *skb)
diff --git a/net/core/skbuff.c b/net/core/skbuff.c
index 1ac1e327ac4c6258726efaba346297ccbec7eef4..8fe0938d6131031a33b535fdf60d9eec50041ea4
100644
--- a/net/core/skbuff.c
+++ b/net/core/skbuff.c
@@ -6984,7 +6984,6 @@ static void skb_carve_reset_headers(struct sk_buff *skb)
skb_unset_mac_header(skb);
skb_unset_transport_header(skb);
skb_reset_network_header(skb);
- skb->mac_len = 0;
/* Inner offsets have no "unset" marker, zero them so that
* skb_inner_network_header_was_set() becomes false and no