Skip to contentSkip to conversation

net: xdp: fix generic XDP on skbs without an Ethernet header

PATCH New Jiayuan Chen 3 participants 3 messages Nov 23 – Nov 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 net: xdp: fix generic XDP on skbs without an Ethernet header

Kenneth Lee reported a bug [1]:

skb_vlan_push got skb with skb->data not at mac header (offset 14)

  WARNING: net/core/skbuff.c:6474 at skb_vlan_push+0x5ec/0x8a0
   ____bpf_skb_vlan_push net/core/filter.c:3239 [inline]
   bpf_skb_vlan_push+0x216/0x8b0 net/core/filter.c:3229
   bpf_prog_run_data_pointers+0x17c/0x240 include/linux/filter.h:917
   tcf_bpf_act+0x31c/0x940 net/sched/act_bpf.c:50
   tcf_action_exec+0x2c1/0x720 net/sched/act_api.c:1147
   tcf_classify+0x6e4/0x1080 net/sched/cls_api.c:1860
   tc_run+0x31c/0x5d0 net/core/dev.c:4411
   sch_handle_ingress net/core/dev.c:4486 [inline]
   __netif_receive_skb_core+0x141b/0x2ec0 net/core/dev.c:6054

bpf_prog_run_generic_xdp() reads xdp->data as an Ethernet header. On an L3 device like sit0 there is none, so the "did the program change the header" check compares garbage, and the fixup:

	__skb_push(skb, ETH_HLEN);
	skb->pkt_type = PACKET_HOST;
	skb->protocol = eth_type_trans(skb, skb->dev);

leaves mac_len == 0 with skb_mac_header(skb) == skb->data - ETH_HLEN. tc ingress then can't get skb->data back onto the mac header.

XDP is Ethernet only, so refuse to attach a generic XDP program to a device that is not ARPHRD_ETHER.

The one exception is lo. Its type is ARPHRD_LOOPBACK, but it uses eth_header_ops and eth_type_trans() like any Ethernet device, so its skbs do carry an Ethernet header. Generic XDP on lo works today and the BPF selftests rely on it (xdp_attach, xdp_devmap_attach, check_mtu, ...), so keep allowing it.

Rejecting the attach is not enough on its own, dev->type can change after the program is attached. An empty bond or team takes the type of its first port, and TUNSETLINK lets a tun device change its type. dev->hard_header_len is no help either, it is what the driver wants reserved on TX and many Ethernet drivers make it larger than ETH_HLEN. So also skip the program for skbs whose mac header is shorter than ETH_HLEN.

[1]: https://lore.kernel.org/bpf/[email protected]/

Fixes 297249569932 ("net: fix generic XDP to handle if eth header was mangled")

---
v1 -> v2:
- Generic XDP does not support L3 devices, as discussed on v1. Reject the attach and skip skbs without an Ethernet header, instead of running the program on them.
- Target net instead of net-next.

v1: https://lore.kernel.org/netdev/[email protected]/

---
 net/core/dev.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
net/core/dev.c +10 -0
index 1a63f6b939f7..7598ac4c217b 100644
@@ -5844,6 +5844,10 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
 	if (skb_is_redirected(skb))
 		return XDP_PASS;
 
+	/* XDP is Ethernet only, skip skbs without an Ethernet header. */
+	if (skb->data - skb_mac_header(skb) < ETH_HLEN)
+		return XDP_PASS;
+
 	/* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM
 	 * bytes. This is the guarantee that also native XDP provides,
 	 * thus we need to do it here as well.
@@ -10869,6 +10873,12 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 			NL_SET_ERR_MSG(extack, "XDP program already attached");
 			return -EBUSY;
 		}
+		/* lo is ARPHRD_LOOPBACK but uses Ethernet framing. */
+		if (mode == XDP_MODE_SKB && dev->type != ARPHRD_ETHER &&
+		    dev->type != ARPHRD_LOOPBACK) {
+			NL_SET_ERR_MSG(extack, "Generic XDP only supports Ethernet devices");
+			return -EOPNOTSUPP;
+		}
 	}
 
 	/* don't call drivers if the effective program didn't change */
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] net: xdp: incomplete Ethernet check in netif_receive_generic_xdp() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] net: xdp: incomplete Ethernet check in netif_receive_generic_xdp()
--

commit 2ed38b0924ab170f5d151d7bc82023a0581fce18
Author: Jiayuan Chen <[email protected]>

net: xdp: fix generic XDP on skbs without an Ethernet header

This commit rejects generic XDP attachment on devices whose type is not ARPHRD_ETHER or ARPHRD_LOOPBACK, and skips generic XDP processing in netif_receive_generic_xdp() when skb->data - skb_mac_header(skb) < ETH_HLEN.

Quoted 5 lines
net/core/dev.c +0 -0
index 6140fd9d11cd0..df335f361706d 100644

net/core/dev.c static u32 netif_receive_generic_xdp(struct sk_buff **pskb, · line 5832

Quoted 10 lines
net/core/dev.c +4 -0
@@ -5823,6 +5823,10 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
 	if (skb_is_redirected(skb))
 		return XDP_PASS;
 
+	/* XDP is Ethernet only, skip skbs without an Ethernet header. */
+	if (skb->data - skb_mac_header(skb) < ETH_HLEN)
+		return XDP_PASS;
+
 	/* XDP packets must have sufficient headroom of XDP_PACKET_HEADROOM
 	 * bytes. This is the guarantee that also native XDP provides,
 	 * thus we need to do it here as well.

[Severity: Medium]
Does checking for < ETH_HLEN still let skbs with mac_len > ETH_HLEN or a non-Ethernet skb->dev->type reach bpf_prog_run_generic_xdp()?

On a VLAN device with VLAN_FLAG_REORDER_HDR cleared, vlan_do_receive() pulls ETH_HLEN + VLAN_HLEN (18 bytes) and resets skb->mac_len to 18. Similarly, if an empty bond or team device has generic XDP attached and then enslaves an ARPHRD_INFINIBAND or ARPHRD_IPGRE port, skb->dev->type changes and received skbs arrive with skb->data - skb_mac_header(skb) >= 24.

When mac_len > ETH_HLEN, the header-mangle fixup in bpf_prog_run_generic_xdp() pushes ETH_HLEN (14) and calls eth_type_trans(), leaving skb->mac_header at skb->data - ETH_HLEN while skb->mac_len is still 18 or 24, which triggers the same skb_vlan_push() warning on TC ingress. In addition, xdp_prepare_buff() includes mac_len bytes ahead of skb->data while bpf_xdp_adjust_head() only enforces a minimum frame size of ETH_HLEN, allowing off > skb_headlen(skb) in __skb_pull(skb, off) to underflow skb->len or hit BUG() when skb->data_len > 0.

Should netif_receive_generic_xdp() check skb->data - skb_mac_header(skb) != ETH_HLEN as well as skb->dev->type != ARPHRD_ETHER && skb->dev->type != ARPHRD_LOOPBACK?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Jiayuan Chen
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

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

Critical: 0 · High: 1 · Medium: 0 · Low: 0

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 new runtime guard in netif_receive_generic_xdp() (net/core/dev.c:5827, `if (skb->data - skb_mac_header(skb) < ETH_HLEN)…

--

net: xdp: fix generic XDP on skbs without an Ethernet header

This patch stops generic XDP programs from being attached to devices that are not ARPHRD_ETHER or ARPHRD_LOOPBACK. dev->type can change after attach, so it also skips generic XDP for skbs whose mac header is shorter than ETH_HLEN.

net/core/dev.c static u32 netif_receive_generic_xdp(struct sk_buff **pskb, · line 5828

Quoted 11 lines
net/core/dev.c +3 -0
index 6140fd9d11cd0..df335f361706d 100644
@@ -5823,6 +5823,10 @@ static u32 netif_receive_generic_xdp(struct sk_buff **pskb,
 	if (skb_is_redirected(skb))
 		return XDP_PASS;
 
+	/* XDP is Ethernet only, skip skbs without an Ethernet header. */
+	if (skb->data - skb_mac_header(skb) < ETH_HLEN)
+		return XDP_PASS;

[Severity: High]

According to the commit message, this check covers the case where dev->type changes after attach:

"An empty bond or team takes the type of its first port ... So also skip the program for skbs whose mac header is shorter than ETH_HLEN."

What happens with non-Ethernet ports whose L2 or pseudo header is ETH_HLEN bytes or longer? In these two cases data - mac_header is at least 24.

IPoIB, in skb_add_pseudo_hdr():

	skb_reset_mac_header(skb);
	skb_pull(skb, IPOIB_HARD_LEN);

ipgre with ipgre_header_ops, in __ipgre_rcv():

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

That second case leaves mac_header on the outer IP header, so mac_len is the outer IP header plus the GRE header.

The sequence looks like this:

1. Attach an XDP_FLAGS_SKB_MODE program to an empty bond or team while it is still ARPHRD_ETHER. This passes the new dev_xdp_attach() check.

2. Enslave an IPoIB device to the bond (bond_enslave()->bond_setup_by_slave()), or add an ipgre device to the team (team_dev_type_check_change()->team_setup_by_port()). Nothing vetoes NETDEV_PRE_TYPE_CHANGE because of a generic XDP program. bond_enslave() only checks the native-mode bond->xdp_prog.

3. On RX, __netif_receive_skb_core() calls skb_reset_mac_len() on the lower device's skb. The rx_handler returns RX_HANDLER_ANOTHER, and at another_round do_xdp_generic() runs the upper device's generic program.

4. The new check is false, so the program treats the IPoIB pseudo header, or the outer IP/GRE bytes, as an Ethernet header.

If the program then changes bytes 0-5 or 12-13, bpf_prog_run_generic_xdp() does:

	__skb_push(skb, ETH_HLEN);
	skb->pkt_type = PACKET_HOST;
	skb->protocol = eth_type_trans(skb, skb->dev);

This leaves mac_header at data - 14 while skb->mac_len stays at 24 or more. skb->protocol is also taken from non-Ethernet bytes.

Would tc ingress then hit the same "skb_vlan_push got skb with skb->data not at mac header" warning in skb_vlan_push() that this patch is meant to fix?

There is also a second gap. bpf_xdp_link_update() calls dev_xdp_install() directly and does not go through dev_xdp_attach(). After a type change like the one above, could a new generic program be swapped in through an existing link without hitting the new ARPHRD check?

Would any of these close the remaining cases?

- require mac_len == ETH_HLEN at run time
- check skb->dev->type for ARPHRD_ETHER or ARPHRD_LOOPBACK at run time
- veto NETDEV_PRE_TYPE_CHANGE while a generic XDP program is attached

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123121540.275576-1-jiayuan.chen%40linux.dev