Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
@ 2026-11-17  6:20 Piyus Kedia
  2026-11-17  8:13 ` Eric Dumazet
  2026-11-21  7:10 ` netdev-bot+sashiko
  0 siblings, 2 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-11-17  6:20 UTC (permalink / raw)
  To: Steffen Klassert, Herbert Xu, David S . Miller, Andrea Mayer
  Cc: Eric Dumazet, Jakub Kicinski, Paolo Abeni, Simon Horman, netdev,
	ipsec, linux-kernel, Kapil Vaswani, Piyus Kedia

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@iiitd.ac.in
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
  2026-11-17  6:20 [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset Piyus Kedia
@ 2026-11-17  8:13 ` Eric Dumazet
  2026-11-17 14:39   ` Piyus Kedia
  2026-11-21  7:10 ` netdev-bot+sashiko
  1 sibling, 1 reply; 5+ messages in thread
From: Eric Dumazet @ 2026-11-17  8:13 UTC (permalink / raw)
  To: Piyus Kedia
  Cc: Steffen Klassert, Herbert Xu, David S . Miller, Andrea Mayer,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, ipsec,
	linux-kernel, Kapil Vaswani

Le mar. 17 nov. 2026 à 07:21, Piyus Kedia <piyus@iiitd.ac.in> 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@iiitd.ac.in
> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
> ---
>
> 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@iiitd.ac.in>
Reported-by: Kapil Vaswani <kapil@presceralabs.ai>
Closes: https://lore.kernel.org/netdev/20261116174531.2540348-2-piyus@iiitd.ac.in/
Signed-off-by: Eric Dumazet <edumazet@kernel.org>

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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
  2026-11-17  8:13 ` Eric Dumazet
@ 2026-11-17 14:39   ` Piyus Kedia
  0 siblings, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-11-17 14:39 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Steffen Klassert, Herbert Xu, David S . Miller, Andrea Mayer,
	Jakub Kicinski, Paolo Abeni, Simon Horman, netdev, ipsec,
	linux-kernel, Kapil Vaswani

Hi Eric,

Thanks for the suggestion! We have submitted a new patch:
https://lore.kernel.org/all/20261117143612.95112-1-piyus@iiitd.ac.in/

We also tested it on our platform and the KASAN bugs are gone after this patch.

Thanks and regards,
Piyus Kedia
Kapil Vaswani

On Tue, Nov 17, 2026 at 1:43 PM Eric Dumazet <edumazet@kernel.org> wrote:
>
> Le mar. 17 nov. 2026 à 07:21, Piyus Kedia <piyus@iiitd.ac.in> 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@iiitd.ac.in
> > Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> > Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
> > ---
> >
> > 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@iiitd.ac.in>
> Reported-by: Kapil Vaswani <kapil@presceralabs.ai>
> Closes: https://lore.kernel.org/netdev/20261116174531.2540348-2-piyus@iiitd.ac.in/
> Signed-off-by: Eric Dumazet <edumazet@kernel.org>
>
> 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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
  2026-11-17  6:20 [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset Piyus Kedia
  2026-11-17  8:13 ` Eric Dumazet
@ 2026-11-21  7:10 ` netdev-bot+sashiko
  2026-11-21  9:14   ` Piyus Kedia
  1 sibling, 1 reply; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-21  7:10 UTC (permalink / raw)
  To: piyus
  Cc: steffen.klassert, herbert, davem, andrea.mayer, edumazet, kuba,
	pabeni, horms, netdev, ipsec, linux-kernel, kapil

Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.

Critical: 0 · High: 1 (1) · Medium: 2 · Low: 2

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [High] The fix stops the h_proto store, but the skb still leaves decap
  with an unset mac header.
- [Medium] The changelog correctly finds where the malformed skb comes
  from but does not fix it there.
- [Medium] In __input_process_payload() (net/xfrm/xfrm_iptfs.c) the
  commit message cites the old_mac branch as precedent ('what…
- [Low] The patch fixes an out-of-bounds write but has no Fixes: tag and
  no Cc: stable.
- [Low] The Signed-off-by chain is malformed.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is the same
  out-of-bounds-write-via-mac_header pattern, in the function the patch…

--

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

This patch adds a skb_mac_header_was_set() check before the eth_hdr()
h_proto store at three sites: xfrm4_remove_tunnel_encap(),
xfrm6_remove_tunnel_encap(), and the IPTFS skb reuse path in
__input_process_payload(). The skb state it guards against comes from
end_dt_vrf_rcv(), which calls skb_unset_mac_header() and leaves mac_len
at the Ethernet value.

Link: https://lore.kernel.org/r/20261116174531.2540348-2-piyus@iiitd.ac.in

> Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>

[Severity: Low]
The author is Piyus Kedia, but Kapil Vaswani's Signed-off-by comes first
and there is no Co-developed-by: tag.

Should there be a Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>
line right before his Signed-off-by, as described in
Documentation/process/submitting-patches.rst? If he did not co-write the
patch, should the trailers be reordered?

[Severity: Low]
This fixes an out-of-bounds write, but it has no Fixes: tag and no Cc:
stable. The notes say the introducing commit could not be found. The
history is in tree, though:

  The producer, skb_unset_mac_header() in end_dt_vrf_rcv(), came in with
  664d6f86868b ("seg6: add support for the SRv6 End.DT4 behavior").

  The xfrm_iptfs.c site came in with 3f3339885fb3 ("xfrm: iptfs: add
  reusing received skb for the tunnel egress packet").

  The xfrm_input.c lines blame to b3284df1c86f ("xfrm: remove input2
  indirection from xfrm_mode"), which moved older code.

Should this carry separate Fixes: tags, or be split into two patches? The
IPTFS hunk will not apply to trees that predate IPTFS.

> diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
> index 5ed87d51392a5..624ff8055e470 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;

[Severity: High]
The store is now skipped, but xfrm4_remove_tunnel_encap() still returns 0
with mac_header == 0xffff and mac_len still non-zero. Can the same skb
still be read through the unset header later on, in GRO?

In the tunnel decaps branch, xfrm_input() passes the skb to GRO:

    gro_cells_receive(&gro_cells, skb);

skb->dev is the VRF device. It has NETIF_F_GRO, and ether_setup() gives it
hard_header_len == ETH_HLEN, so GRO is not skipped:

gro_cell_poll()
  napi_gro_receive()
    gro_receive_skb()
      dev_gro_receive()
        gro_list_prepare()

For each held packet p with a matching hash, gro_list_prepare() does:

    if (maclen == ETH_HLEN)
        diffs |= compare_ether_header(skb_mac_header(p),
                                      skb_mac_header(skb));

skb_mac_header() only issues a DEBUG_NET_WARN and returns
skb->head + 0xffff. That makes this a 14-byte read, for both skbs, at the
same fixed address 64KB past head that the commit message describes.

The held packet p is also still unset. At found_ptype, dev_gro_receive()
only calls skb_reset_mac_len(), which leaves mac_header at 0xffff. Two
packets of one inner flow in the same NAPI poll, such as two consecutive
TCP segments, look like enough to trigger this.

The skb_reset_mac_len() warning used as the control in the test notes
looks like this same skb reaching found_ptype in dev_gro_receive(), or
__netif_receive_skb_core(), on the patched kernel.

Would it work better to normalise the skb, either in end_dt_vrf_rcv() or
here by resetting the mac header and setting mac_len = 0 when it was
unset? That would remove both the write and this read.

> @@ -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;

[Severity: Medium]
The commit message identifies where the bad state comes from, but the fix
is applied at three consumers instead. Is there a reason to leave
end_dt_vrf_rcv() and skb_unset_mac_header() unchanged?

net/ipv6/seg6_local.c:end_dt_vrf_rcv() {
    ...
	skb_unset_mac_header(skb);

	skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
    ...
}

skb_unset_mac_header() writes the ~0 sentinel and leaves mac_len alone.
That breaks the rule skb_reset_mac_len() enforces:

	if (!skb_mac_header_was_set(skb)) {
		DEBUG_NET_WARN_ON_ONCE(1);
		skb->mac_len = 0;

vrf_ip_rcv() and vrf_ip6_rcv() only rebuild the header when
!list_empty(&vrf_dev->ptype_all). Without a sniffer attached, the skb goes
through the IP stack and xfrm with mac_header unset and
mac_len == ETH_HLEN.

After this patch, the skb still leaves tunnel decap in that state through
gro_cells_receive(). Any later consumer that trusts mac_len or
skb_mac_header() is still exposed, and the GRO read above is one of them.

There are two ways to fix this at the source. One is to give the skb a
valid mac header with mac_len = 0 after l3mdev_l3_rcv() when VRF did not
rebuild it. The other is to have skb_unset_mac_header() clear mac_len.
Would either one make the original "if (skb->mac_len)" checks correct
again?

> diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
> index 0313a43aa8a97..41af72a9206ad 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 */

[Severity: Medium]
The commit message says:

    This mirrors what xfrm4_transport_input() already does, ... and what
    __input_process_payload() already does for old_mac

Does the old_mac branch really check as much as this one? old_mac depends
only on skb_mac_header_was_set():

	old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;

The alloc path then writes h_proto without looking at mac_len:

			skb->protocol = protocol;
			if (old_mac) {
				/* rebuild the mac header */
				skb_set_mac_header(skb, -first_skb->mac_len);
				memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
				eth_hdr(skb)->h_proto = skb->protocol;
			}

When first_skb->mac_len is 0, the mac header lands at skb->data, which is
the start of the inner IP header that was just copied in. The h_proto
store then writes 0x0800 or 0x86dd at data + 12.

For IPv4 that overwrites the first two bytes of saddr. For IPv6 it
overwrites bytes 4-5 of saddr, after ESP authentication has already been
done.

A set mac header with mac_len == 0 is normal on L3 receive devices such as
tun with IFF_TUN, ipgre without header_ops, and ip6_tunnel. Every inner
packet after the first in an aggregated IPTFS payload takes this alloc
path.

Should both branches check old_mac && first_skb->mac_len, or, better,
require a real Ethernet header?

[Severity: High]
This is a pre-existing issue, not one introduced by this patch. The same
alloc path in __input_process_payload() has a related out-of-bounds write
through mac_header. Is the headroom checked before these lines?

	skb_set_mac_header(skb, -first_skb->mac_len);
	memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);

The new skb is allocated by iptfs_alloc_skb(..., false), called from
iptfs_pskb_extract_seq() or iptfs_pskb_add_frags(). That only reserves:

#define XFRM_IPTFS_MIN_L2HEADROOM (L1_CACHE_BYTES > 64 ? 64 : 64 + 16)

If first_skb->mac_len is larger than that headroom, the u16 mac_header
wraps to roughly 0xfff4-0xffff. memcpy() then writes mac_len bytes about
64KB past skb->head, and the h_proto store after it is out of bounds too.

ipgre with ipgre_header_ops can get there, in ipgre_rcv():

	if (tunnel->dev->header_ops == &ipgre_header_ops)
		skb_pop_mac_header(skb);

That sets mac_len to the outer IP header plus the GRE header, up to 76
bytes. This is more than the 64 bytes reserved on arches with
L1_CACHE_BYTES > 64, and the copied bytes come from outer headers that ESP
does not authenticate.

A tc ingress BPF program using bpf_skb_change_head() can also make mac_len
larger than the headroom on any arch, though that needs privileges.

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117062046.3286096-1-piyus%40iiitd.ac.in

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset
  2026-11-21  7:10 ` netdev-bot+sashiko
@ 2026-11-21  9:14   ` Piyus Kedia
  0 siblings, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-11-21  9:14 UTC (permalink / raw)
  To: netdev-bot+sashiko
  Cc: steffen.klassert, herbert, davem, andrea.mayer, edumazet, kuba,
	pabeni, horms, netdev, ipsec, linux-kernel, kapil

Thank you for the review. This issue has already been resolved through
a different patch (suggested by Eric) that has been merged into the
upstream kernel.

Best regards,
Piyus Kedia
Kapil Vaswani


On Sat, Nov 21, 2026 at 12:40 PM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 5 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 (1) · Medium: 2 · Low: 2
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [High] The fix stops the h_proto store, but the skb still leaves decap
>   with an unset mac header.
> - [Medium] The changelog correctly finds where the malformed skb comes
>   from but does not fix it there.
> - [Medium] In __input_process_payload() (net/xfrm/xfrm_iptfs.c) the
>   commit message cites the old_mac branch as precedent ('what…
> - [Low] The patch fixes an out-of-bounds write but has no Fixes: tag and
>   no Cc: stable.
> - [Low] The Signed-off-by chain is malformed.
>
> Pre-existing issues:
> - [High] This problem wasn't introduced by this patch, but it is the same
>   out-of-bounds-write-via-mac_header pattern, in the function the patch…
>
> --
>
> xfrm: do not write the ethernet header when the mac header is unset
>
> This patch adds a skb_mac_header_was_set() check before the eth_hdr()
> h_proto store at three sites: xfrm4_remove_tunnel_encap(),
> xfrm6_remove_tunnel_encap(), and the IPTFS skb reuse path in
> __input_process_payload(). The skb state it guards against comes from
> end_dt_vrf_rcv(), which calls skb_unset_mac_header() and leaves mac_len
> at the Ethernet value.
>
> Link: https://lore.kernel.org/r/20261116174531.2540348-2-piyus@iiitd.ac.in
>
> > Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
> > Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
>
> [Severity: Low]
> The author is Piyus Kedia, but Kapil Vaswani's Signed-off-by comes first
> and there is no Co-developed-by: tag.
>
> Should there be a Co-developed-by: Kapil Vaswani <kapil@presceralabs.ai>
> line right before his Signed-off-by, as described in
> Documentation/process/submitting-patches.rst? If he did not co-write the
> patch, should the trailers be reordered?
>
> [Severity: Low]
> This fixes an out-of-bounds write, but it has no Fixes: tag and no Cc:
> stable. The notes say the introducing commit could not be found. The
> history is in tree, though:
>
>   The producer, skb_unset_mac_header() in end_dt_vrf_rcv(), came in with
>   664d6f86868b ("seg6: add support for the SRv6 End.DT4 behavior").
>
>   The xfrm_iptfs.c site came in with 3f3339885fb3 ("xfrm: iptfs: add
>   reusing received skb for the tunnel egress packet").
>
>   The xfrm_input.c lines blame to b3284df1c86f ("xfrm: remove input2
>   indirection from xfrm_mode"), which moved older code.
>
> Should this carry separate Fixes: tags, or be split into two patches? The
> IPTFS hunk will not apply to trees that predate IPTFS.
>
> > diff --git a/net/xfrm/xfrm_input.c b/net/xfrm/xfrm_input.c
> > index 5ed87d51392a5..624ff8055e470 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;
>
> [Severity: High]
> The store is now skipped, but xfrm4_remove_tunnel_encap() still returns 0
> with mac_header == 0xffff and mac_len still non-zero. Can the same skb
> still be read through the unset header later on, in GRO?
>
> In the tunnel decaps branch, xfrm_input() passes the skb to GRO:
>
>     gro_cells_receive(&gro_cells, skb);
>
> skb->dev is the VRF device. It has NETIF_F_GRO, and ether_setup() gives it
> hard_header_len == ETH_HLEN, so GRO is not skipped:
>
> gro_cell_poll()
>   napi_gro_receive()
>     gro_receive_skb()
>       dev_gro_receive()
>         gro_list_prepare()
>
> For each held packet p with a matching hash, gro_list_prepare() does:
>
>     if (maclen == ETH_HLEN)
>         diffs |= compare_ether_header(skb_mac_header(p),
>                                       skb_mac_header(skb));
>
> skb_mac_header() only issues a DEBUG_NET_WARN and returns
> skb->head + 0xffff. That makes this a 14-byte read, for both skbs, at the
> same fixed address 64KB past head that the commit message describes.
>
> The held packet p is also still unset. At found_ptype, dev_gro_receive()
> only calls skb_reset_mac_len(), which leaves mac_header at 0xffff. Two
> packets of one inner flow in the same NAPI poll, such as two consecutive
> TCP segments, look like enough to trigger this.
>
> The skb_reset_mac_len() warning used as the control in the test notes
> looks like this same skb reaching found_ptype in dev_gro_receive(), or
> __netif_receive_skb_core(), on the patched kernel.
>
> Would it work better to normalise the skb, either in end_dt_vrf_rcv() or
> here by resetting the mac header and setting mac_len = 0 when it was
> unset? That would remove both the write and this read.
>
> > @@ -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;
>
> [Severity: Medium]
> The commit message identifies where the bad state comes from, but the fix
> is applied at three consumers instead. Is there a reason to leave
> end_dt_vrf_rcv() and skb_unset_mac_header() unchanged?
>
> net/ipv6/seg6_local.c:end_dt_vrf_rcv() {
>     ...
>         skb_unset_mac_header(skb);
>
>         skb = dev->l3mdev_ops->l3mdev_l3_rcv(dev, skb, family);
>     ...
> }
>
> skb_unset_mac_header() writes the ~0 sentinel and leaves mac_len alone.
> That breaks the rule skb_reset_mac_len() enforces:
>
>         if (!skb_mac_header_was_set(skb)) {
>                 DEBUG_NET_WARN_ON_ONCE(1);
>                 skb->mac_len = 0;
>
> vrf_ip_rcv() and vrf_ip6_rcv() only rebuild the header when
> !list_empty(&vrf_dev->ptype_all). Without a sniffer attached, the skb goes
> through the IP stack and xfrm with mac_header unset and
> mac_len == ETH_HLEN.
>
> After this patch, the skb still leaves tunnel decap in that state through
> gro_cells_receive(). Any later consumer that trusts mac_len or
> skb_mac_header() is still exposed, and the GRO read above is one of them.
>
> There are two ways to fix this at the source. One is to give the skb a
> valid mac header with mac_len = 0 after l3mdev_l3_rcv() when VRF did not
> rebuild it. The other is to have skb_unset_mac_header() clear mac_len.
> Would either one make the original "if (skb->mac_len)" checks correct
> again?
>
> > diff --git a/net/xfrm/xfrm_iptfs.c b/net/xfrm/xfrm_iptfs.c
> > index 0313a43aa8a97..41af72a9206ad 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 */
>
> [Severity: Medium]
> The commit message says:
>
>     This mirrors what xfrm4_transport_input() already does, ... and what
>     __input_process_payload() already does for old_mac
>
> Does the old_mac branch really check as much as this one? old_mac depends
> only on skb_mac_header_was_set():
>
>         old_mac = skb_mac_header_was_set(skb) ? skb_mac_header(skb) : NULL;
>
> The alloc path then writes h_proto without looking at mac_len:
>
>                         skb->protocol = protocol;
>                         if (old_mac) {
>                                 /* rebuild the mac header */
>                                 skb_set_mac_header(skb, -first_skb->mac_len);
>                                 memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
>                                 eth_hdr(skb)->h_proto = skb->protocol;
>                         }
>
> When first_skb->mac_len is 0, the mac header lands at skb->data, which is
> the start of the inner IP header that was just copied in. The h_proto
> store then writes 0x0800 or 0x86dd at data + 12.
>
> For IPv4 that overwrites the first two bytes of saddr. For IPv6 it
> overwrites bytes 4-5 of saddr, after ESP authentication has already been
> done.
>
> A set mac header with mac_len == 0 is normal on L3 receive devices such as
> tun with IFF_TUN, ipgre without header_ops, and ip6_tunnel. Every inner
> packet after the first in an aggregated IPTFS payload takes this alloc
> path.
>
> Should both branches check old_mac && first_skb->mac_len, or, better,
> require a real Ethernet header?
>
> [Severity: High]
> This is a pre-existing issue, not one introduced by this patch. The same
> alloc path in __input_process_payload() has a related out-of-bounds write
> through mac_header. Is the headroom checked before these lines?
>
>         skb_set_mac_header(skb, -first_skb->mac_len);
>         memcpy(skb_mac_header(skb), old_mac, first_skb->mac_len);
>
> The new skb is allocated by iptfs_alloc_skb(..., false), called from
> iptfs_pskb_extract_seq() or iptfs_pskb_add_frags(). That only reserves:
>
> #define XFRM_IPTFS_MIN_L2HEADROOM (L1_CACHE_BYTES > 64 ? 64 : 64 + 16)
>
> If first_skb->mac_len is larger than that headroom, the u16 mac_header
> wraps to roughly 0xfff4-0xffff. memcpy() then writes mac_len bytes about
> 64KB past skb->head, and the h_proto store after it is out of bounds too.
>
> ipgre with ipgre_header_ops can get there, in ipgre_rcv():
>
>         if (tunnel->dev->header_ops == &ipgre_header_ops)
>                 skb_pop_mac_header(skb);
>
> That sets mac_len to the outer IP header plus the GRE header, up to 76
> bytes. This is more than the 64 bytes reserved on arches with
> L1_CACHE_BYTES > 64, and the copied bytes come from outer headers that ESP
> does not authenticate.
>
> A tc ingress BPF program using bpf_skb_change_head() can also make mac_len
> larger than the headroom on any arch, though that needs privileges.
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117062046.3286096-1-piyus%40iiitd.ac.in

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-11-21  9:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  6:20 [PATCH net v2] xfrm: do not write the ethernet header when the mac header is unset Piyus Kedia
2026-11-17  8:13 ` Eric Dumazet
2026-11-17 14:39   ` Piyus Kedia
2026-11-21  7:10 ` netdev-bot+sashiko
2026-11-21  9:14   ` Piyus Kedia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox