net: preserve skb header offsets during headroom changes
Subsystems (3)
PATCH v4 1/1 net: preserve skb header offsets during headroom changes
An MPLS LWT route over a netkit device configured with large headroom can make aligned skb expansion wrap the 16-bit network_header. Neighbour output interprets the resulting negative skb_network_offset() as a large unsigned pull length, causing eth_header() to write outside the skb allocation.
Reject growth that cannot preserve the network, set MAC and transport, recorded inner-network, or partial-checksum offsets. Do not relocate a set MAC or transport header onto its unset value. Check the actual post-pop positions in skb_mpls_pop(). In native MPLS, bound the post-pop offset before decoding labels; reject excessive LWT headroom before reading the IP header.
The issue was found by an LLM.
---
Changes in v4:
- Move the native MPLS headroom check before label decoding and multipath selection.
Changes in v3:
- Check the actual post-pop MAC/network positions and reject the MAC unset value in skb_mpls_pop().
- Check a set outer MAC header and a recorded inner network header during positive relocation.
- Move the MPLS LWT entry check before reading the IP header.
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
v1: https://lore.kernel.org/netdev/[email protected]/
I have a reproducer for this issue and can share via DM.
net/core/skbuff.c | 35 +++++++++++++++++++++++++++++++++++ net/mpls/af_mpls.c | 3 +++ net/mpls/mpls_iptunnel.c | 3 +++ 3 files changed, 41 insertions(+)
net/core/skbuff.c +35 -0
@@ -2130,6 +2130,28 @@ struct sk_buff *skb_clone(struct sk_buff *skb, gfp_t gfp_mask) }EXPORT_SYMBOL (skb_clone); +static bool skb_header_offsets_fit (const struct sk_buff *skb, +unsigned int nhead) +{ +if (!nhead) +return true; +if (nhead >U16_MAX - skb->network_header) +return false; +if (skb_transport_header_was_set (skb) && + nhead >=U16_MAX - skb->transport_header) +return false; +if (skb_mac_header_was_set (skb) && + nhead >=U16_MAX - skb->mac_header) +return false; +if (skb->ip_summed ==CHECKSUM_PARTIAL && + nhead >U16_MAX - skb->csum_start) +return false; +if (skb_inner_network_header_was_set (skb) && + nhead >U16_MAX - skb->inner_network_header) +return false; +return true; +} +void skb_headers_offset_update (struct sk_buff *skb,int off) {/* Only adjust this if it actually is csum_start rather than csum */
@@ -2317,6 +2339,9 @@ int pskb_expand_head(struct sk_buff *skb, int nhead, int ntail,return -EINVAL ; } +if (unlikely (!skb_header_offsets_fit (skb, nhead))) +return -EINVAL ; +if (unlikely (skb_shared (skb))) {DEBUG_NET_WARN_ON_ONCE (1 );return -EINVAL ;
@@ -2535,6 +2560,11 @@ struct sk_buff *skb_copy_expand(const struct sk_buff *skb,return NULL ; oldheadroom =skb_headroom (skb); +if (unlikely (newheadroom > oldheadroom && + !skb_header_offsets_fit (skb, (unsigned int )newheadroom - + oldheadroom))) +return NULL ; + n =__alloc_skb (newheadroom + skb->len + newtailroom, gfp_mask,skb_alloc_rx_flag (skb),NUMA_NO_NODE );
@@ -6804,11 +6834,16 @@ EXPORT_SYMBOL_GPL(skb_mpls_push);int skb_mpls_pop (struct sk_buff *skb,__be16 next_proto,int mac_len,bool ethernet) { +unsigned int headroom =skb_headroom (skb);int err;if (unlikely (!eth_p_mpls (skb->protocol)))return 0 ; +if (unlikely (headroom >=U16_MAX -MPLS_HLEN || + (unsigned int )mac_len >U16_MAX -MPLS_HLEN - headroom)) +return -EINVAL ; + err =skb_ensure_writable (skb, mac_len +MPLS_HLEN );if (unlikely (err))return err;
net/mpls/af_mpls.c +3 -0
@@ -403,6 +403,9 @@ static int mpls_forward(struct sk_buff *skb, struct net_device *dev,if (!pskb_may_pull (skb,sizeof (*hdr))) goto err; +if (unlikely (skb_headroom (skb) >U16_MAX -sizeof (*hdr))) + goto err; +skb_dst_drop (skb);/* Read and decode the label */
net/mpls/mpls_iptunnel.c +3 -0
@@ -59,6 +59,9 @@ static int mpls_xmit(struct sk_buff *skb) !dst->lwtstate || skb_warn_if_lro(skb)) goto drop; +if (unlikely (skb_headroom (skb) >U16_MAX )) + goto drop; +skb_forward_csum (skb); tun_encap_info =mpls_lwtunnel_encap (dst->lwtstate);
base-commit: 2800868f63227c8237d70c41e70d441c919620d6
Signature · 1 line
2.55.0.openai.911.g06873a30d2f3