Skip to contentSkip to conversation

net: drop the stale dst when steering to another device

PATCH Superseded Jamal Hadi Salim 3 participants 8 messages Nov 12 – Nov 13

Subsystems (6)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover net: drop the stale dst when steering to another device

This patchset is created to address constant whining by both sashikos about this being an issue when fixing a UAF on a teql issue, see:
https://lore.kernel.org/netdev/[email protected]/

Two producers steer a packet to a network device of their choosing and then hand it to the neighbour layer for link-layer resolution:

3/4 netfilter: the neighbour variant of nft fwd, on the netdev egress
hook
4/4 bpf: bpf_redirect_neigh() when the program supplies the nexthop

Both rewrite skb->dev to the target device and leave the skb's dst pointing at the route the packet arrived through. When the target device's neighbour for the nexthop is incomplete the packet is parked on that neighbour's arp_queue, and draining the queue re-looks the neighbour up from the stale dst. The frame is therefore built with the original device's link-layer header and then transmitted on the target device: the packet leaves with another device's addresses, and the sender never learns that its nexthop was unreachable.

This is explicit: the steer-to-another-device path never worked in this respect, so there is nothing here to regress. Both producers shipped with skb->dev swapped to the target device and the route's dst still attached, and the arp_queue drain has re-derived the neighbour from that dst for as long as either producer has existed; the invariant "the skb's dst describes where the skb is going", which the routing path keeps when it steers, was never established for these two entry points. With no commit to anchor with a Fixes: tag and no crash, UAF or lockup involved, the series targets net-next and does not nominate Cc: stable:
a backport would change long-standing redirect behaviour for no crash-class benefit.

More context: this came out of the review of the fix for a parked skb being resolved against the wrong device in sch_teql, posted separately and not part of this series. The neighbour code's queue drain is doing the right thing there, it re-derives the neighbour from the skb, so the repair belongs with the producers rather than with the drain path. Both producers are fixed in the same series so neither is left behind; each patch that changes a producer carries its own reproduction recipe.

The reproduction for both arms is a pair of veth devices, one of them with an unresolved neighbour, plus a capture on the target's peer: the captured frame's source and destination addresses name the device whose neighbour was used to build it.

v2 drops the dst only when it describes a route, and keeps what the route carries (thanks to Pablo for catching this). A DST_METADATA dst is the tunnel template a tunnel helper attached to the packet, not a route, and dropping it would discard the encapsulation parameters, so it is left in place. A route carries encapsulation parameters too when it was installed with an ip, ip6 or xfrm encapsulation; 2/4 adds a helper that moves them onto a metadata dst before the route is released, so a producer that steers into an external tunnel device still finds them in skb_tunnel_info(), and one that steers into a collect_md xfrm interface still finds its if_id and underlying device. Neither case weakens the fix:
a metadata dst resolves neighbours through dst_blackhole_ops, whose lookup returns NULL, so the queue drain keeps the parking (target-device) neighbour exactly as it does with no dst at all.

1/4 is a prerequisite on the xfrm path turned up after Pablo's comment. The interface that consumes that metadata puts the original route back on the skb before the xfrm lookup, which needs its family, and installing a reference counted route over a dst slot that is still occupied trips the slot assertion; the slot is cleared first. The same overwrite is in the tc-bpf helper that attaches the metadata, so both sites are fixed in that patch.

---
v1 -> v2:
- Do not drop a dst that is not a route: a DST_METADATA dst is the tunnel template a tunnel helper attached to the packet, and dropping it discards the encapsulation parameters (Pablo Neira Ayuso).
- Keep a route's encapsulation parameters as well: a route installed with an ip, ip6 or xfrm encapsulation carries them in dst->lwtstate, which a drop would discard before a downstream tunnel or xfrm device could use them. The dst release now goes through a helper that moves them onto a metadata dst first (new 2/4).
- Fix the dst slot the xfrm consumer restores: a collect_md xfrm interface puts the original route back before the xfrm lookup, and installing it over a dst that is still there trips the slot assertion. New 1/4 clears the slot first at both that site and in the tc-bpf helper that attaches the metadata.

Jamal Hadi Salim (4):
net: xfrm: interface: restore dst_orig on every xfrm interface
net: dst: keep the LWT metadata of a dropped route
netfilter: nft_fwd_netdev: drop the stale dst when steering to a
device
bpf: drop the stale dst when redirecting to an explicit nexthop

 include/net/dst_metadata.h     |  1 +
 include/net/xfrm.h             |  2 -
 net/core/dst.c                 | 72 ++++++++++++++++++++++++++++++++++
 net/core/filter.c              | 12 ++----
 net/netfilter/nft_fwd_netdev.c |  6 +++
 net/xfrm/xfrm_interface_bpf.c  | 16 +++-----
 net/xfrm/xfrm_interface_core.c | 22 ++++++-----
 7 files changed, 100 insertions(+), 31 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v2 1/4 net: xfrm: interface: restore dst_orig on every xfrm interface

Found while testing the dst release for the two producers that steer a packet to another device, fixed later in this series. One of the consumers of the metadata such a release installs is an xfrm interface, which restores the original route before the xfrm lookup; the swap warned on the metadata dst still installed in the skb, and only the collect_md arm restored the route at all.

The issue:
An xfrm interface takes the if_id and the underlying device either from its configuration or, in collect_md mode, from the xfrm metadata on the skb. When that metadata also carries the route the packet arrived through, xfrmi_xmit2() puts the route back on the skb, because the xfrm lookup takes the address family from it. skb_dst_set() expects the dst slot to be empty for a reference counted dst and warns when it is not, so the route landed over a metadata dst that was still there; a kernel built with CONFIG_DEBUG_NET=y panics on the first packet that takes this path when panic_on_warn is set.

Two more defects sit in the same place. An interface with a configured if_id never restores the route: it takes the metadata dst as it is, whose address family is AF_UNSPEC, so the lookup finds no policy and the packet is dropped as a link failure instead of being transformed. And the restore moved the route out of the metadata dst, which a clone of the skb shares, so the clone that reached the interface first left the original packet with nothing to restore.

The same slot overwrite is in the unstable helper a tc-bpf program uses to attach that metadata: bpf_skb_set_xfrm_info() records the route as dst_orig and installs a per-cpu metadata dst over it. The per-cpu dst is shared by every invocation of the program, so the restore cannot keep anything in it.

The Fix:
Restore dst_orig in both arms of xfrmi_xmit2(), taking a reference with dst_clone() so the metadata dst keeps its own, and empty the counted slot with skb_dst_drop() before the route is installed. The helper allocates a metadata dst per skb instead of a per-cpu one, so the route it records belongs to that skb and is released with it; the per-cpu allocation is no longer used and goes away.

Conditions to recreate the bug:
A tc-bpf program that records the route as dst_orig with bpf_skb_set_xfrm_info() and redirects the packet to an xfrm interface is the shortest way to hit the overwrite. A netdev egress fwd rule that steers a packet routed with an xfrm encapsulation into an interface reaches the same place once the dst release earlier in this series keeps the route:

ip link add xf0 type xfrm if_id 42
ip link set xf0 up
ip neigh replace 10.20.0.1 lladdr 02:aa:00:00:00:ab \
dev va nud permanent
ip route add 10.20.0.1/32 dev va encap xfrm if_id 42 nft add rule netdev t c fwd ip to 10.1.0.2 device xf0

Send one packet to 10.20.0.1. With CONFIG_DEBUG_NET=y and panic_on_warn=1 the kernel panics in xfrmi_xmit() with the metadata dst still installed while the route is put back. The interface with the configured if_id drops the packet as a link failure without consulting the outbound policy, and a clone made after the metadata was attached consumes the route before the original packet arrives.

---
 include/net/xfrm.h             |  2 --
 net/core/filter.c              |  8 --------
 net/xfrm/xfrm_interface_bpf.c  | 16 +++++-----------
 net/xfrm/xfrm_interface_core.c | 22 ++++++++++++----------
 4 files changed, 17 insertions(+), 31 deletions(-)
include/net/xfrm.h +0 -2
index 445223a8dc6f..1866e5a3b911 100644
@@ -2370,8 +2370,6 @@ static inline bool xfrm6_local_dontfrag(const struct sock *sk)
 #if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
     (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
 
-extern struct metadata_dst __percpu *xfrm_bpf_md_dst;
-
 int register_xfrm_interface_bpf(void);
 
 #else
net/core/filter.c +0 -8
index 9b8d5191c4ba..2d574ae52c4b 100644
@@ -6353,14 +6353,6 @@ static const struct bpf_func_proto bpf_bind_proto = {
 
 #ifdef CONFIG_XFRM
 
-#if (IS_BUILTIN(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF)) || \
-    (IS_MODULE(CONFIG_XFRM_INTERFACE) && IS_ENABLED(CONFIG_DEBUG_INFO_BTF_MODULES))
-
-struct metadata_dst __percpu *xfrm_bpf_md_dst;
-EXPORT_SYMBOL_GPL(xfrm_bpf_md_dst);
-
-#endif
-
 BPF_CALL_5(bpf_skb_get_xfrm_state, struct sk_buff *, skb, u32, index,
 	   struct bpf_xfrm_state *, to, u32, size, u64, flags)
 {
net/xfrm/xfrm_interface_bpf.c +5 -11
index 5ea15037ebd1..8e83c624b774 100644
@@ -68,16 +68,9 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bp
 	if (unlikely(skb_metadata_dst(skb)))
 		return -EINVAL;
 
-	if (!xfrm_bpf_md_dst) {
-		struct metadata_dst __percpu *tmp;
-
-		tmp = metadata_dst_alloc_percpu(0, METADATA_XFRM, GFP_ATOMIC);
-		if (!tmp)
-			return -ENOMEM;
-		if (cmpxchg(&xfrm_bpf_md_dst, NULL, tmp))
-			metadata_dst_free_percpu(tmp);
-	}
-	md_dst = this_cpu_ptr(xfrm_bpf_md_dst);
+	md_dst = metadata_dst_alloc(0, METADATA_XFRM, GFP_ATOMIC);
+	if (unlikely(!md_dst))
+		return -ENOMEM;
 
 	info = &md_dst->u.xfrm_info;
 
@@ -86,7 +79,8 @@ __bpf_kfunc int bpf_skb_set_xfrm_info(struct __sk_buff *skb_ctx, const struct bp
 	skb_dst_force(skb);
 	info->dst_orig = skb_dst(skb);
 
-	dst_hold((struct dst_entry *)md_dst);
+	/* The allocated dst owns one reference, handed to the skb here. */
+	skb_dstref_steal(skb);
 	skb_dst_set(skb, (struct dst_entry *)md_dst);
 	return 0;
 }
net/xfrm/xfrm_interface_core.c +12 -10
index 9835fbe63ccb..896180f8d171 100644
@@ -423,6 +423,7 @@ static int xfrmi_rcv_cb(struct sk_buff *skb, int err)
 static int
 xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
 {
+	struct xfrm_md_info *md_info = skb_xfrm_md_info(skb);
 	struct xfrm_if *xi = netdev_priv(dev);
 	struct dst_entry *dst = skb_dst(skb);
 	unsigned int length = skb->len;
@@ -433,25 +434,26 @@ xfrmi_xmit2(struct sk_buff *skb, struct net_device *dev, struct flowi *fl)
 	int mtu;
 
 	if (xi->p.collect_md) {
-		struct xfrm_md_info *md_info = skb_xfrm_md_info(skb);
-
 		if (unlikely(!md_info))
 			return -EINVAL;
 
 		if_id = md_info->if_id;
 		fl->flowi_oif = md_info->link;
-		if (md_info->dst_orig) {
-			struct dst_entry *tmp_dst = dst;
-
-			dst = md_info->dst_orig;
-			skb_dst_set(skb, dst);
-			md_info->dst_orig = NULL;
-			dst_release(tmp_dst);
-		}
 	} else {
 		if_id = xi->p.if_id;
 	}
 
+	/* The metadata dst carries the route the packet is still to be
+	 * transformed for, and the lookup below takes the address family from
+	 * the dst it is given. Put that route back, taking our own reference:
+	 * the metadata dst keeps its own, as a clone of this skb shares it.
+	 */
+	if (md_info && md_info->dst_orig) {
+		dst = dst_clone(md_info->dst_orig);
+		skb_dst_drop(skb);
+		skb_dst_set(skb, dst);
+	}
+
 	dst_hold(dst);
 	dst = xfrm_lookup_with_ifid(xi->net, dst, fl, NULL, 0, if_id);
 	if (IS_ERR(dst)) {
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net-next v2 1/4] net: xfrm: interface: restore dst_orig on every xfrm interface

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] xfrm: interface: missing Fixes trailer in bug-fix commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] xfrm: interface: missing Fixes trailer in bug-fix commit message --

commit 07b565180a0d355b65e18ab188e00cf30014d9c1
Author: Jamal Hadi Salim <[email protected]>

Quoted 62 lines

net: xfrm: interface: restore dst_orig on every xfrm interface

Found while testing the dst release for the two producers that steer a packet to another device, fixed later in this series. One of the consumers of the metadata such a release installs is an xfrm interface, which restores the original route before the xfrm lookup; the swap warned on the metadata dst still installed in the skb, and only the collect_md arm restored the route at all.

The issue:
An xfrm interface takes the if_id and the underlying device either from its configuration or, in collect_md mode, from the xfrm metadata on the skb. When that metadata also carries the route the packet arrived through, xfrmi_xmit2() puts the route back on the skb, because the xfrm lookup takes the address family from it. skb_dst_set() expects the dst slot to be empty for a reference counted dst and warns when it is not, so the route landed over a metadata dst that was still there; a kernel built with CONFIG_DEBUG_NET=y panics on the first packet that takes this path when panic_on_warn is set.

Two more defects sit in the same place. An interface with a configured if_id never restores the route: it takes the metadata dst as it is, whose address family is AF_UNSPEC, so the lookup finds no policy and the packet is dropped as a link failure instead of being transformed. And the restore moved the route out of the metadata dst, which a clone of the skb shares, so the clone that reached the interface first left the original packet with nothing to restore.

The same slot overwrite is in the unstable helper a tc-bpf program uses to attach that metadata: bpf_skb_set_xfrm_info() records the route as dst_orig and installs a per-cpu metadata dst over it. The per-cpu dst is shared by every invocation of the program, so the restore cannot keep anything in it.

The Fix:
Restore dst_orig in both arms of xfrmi_xmit2(), taking a reference with dst_clone() so the metadata dst keeps its own, and empty the counted slot with skb_dst_drop() before the route is installed. The helper allocates a metadata dst per skb instead of a per-cpu one, so the route it records belongs to that skb and is released with it; the per-cpu allocation is no longer used and goes away.

Conditions to recreate the bug:
A tc-bpf program that records the route as dst_orig with bpf_skb_set_xfrm_info() and redirects the packet to an xfrm interface is the shortest way to hit the overwrite. A netdev egress fwd rule that steers a packet routed with an xfrm encapsulation into an interface reaches the same place once the dst release earlier in this series keeps the route:

ip link add xf0 type xfrm if_id 42
ip link set xf0 up
ip neigh replace 10.20.0.1 lladdr 02:aa:00:00:00:ab \
dev va nud permanent
ip route add 10.20.0.1/32 dev va encap xfrm if_id 42 nft add rule netdev t c fwd ip to 10.1.0.2 device xf0

Send one packet to 10.20.0.1. With CONFIG_DEBUG_NET=y and panic_on_warn=1 the kernel panics in xfrmi_xmit() with the metadata dst still installed while the route is put back. The interface with the configured if_id drops the packet as a link failure without consulting the outbound policy, and a clone made after the metadata was attached consumes the route before the original packet arrives.

Quoted 2 lines

[Severity: Low]
Since this fixes the dst_orig handling and per-cpu metadata dst added in bpf_skb_set_xfrm_info() and xfrmi_xmit2(), should this include:

Fixes 94151f5aa966 ("xfrm: interface: Add unstable helpers for setting/getting XFRM metadata from TC-BPF")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v2 2/4 net: dst: keep the LWT metadata of a dropped route

In reply to Jamal Hadi Salim

Found while working on the two producers that steer a packet to another device's neighbour, fixed later in this series. Both release the route dst on the way, and a route installed with an encapsulation carries the parameters a device downstream needs: an ip or ip6 encapsulation holds an ip_tunnel_info, where skb_tunnel_info() reads it, and an xfrm encapsulation holds an xfrm_md_info, where skb_xfrm_md_info() reads it. The release discards them, so a tunnel or an xfrm interface downstream has nothing left to build the outer header with, or to select its if_id.

The Fix:
Add skb_dst_drop_keep_metadata(), which moves those parameters onto a metadata dst of its own before the route is released. The ip and ip6 payload is copied field by field; the route's dst cache belongs to its lwtstate and is shared by every packet that uses the route, so it is not
duplicated: the copy starts with an empty cache, which costs the tunnel one route lookup. The xfrm payload keeps the route itself, because the xfrm lookup reads its family; the counted reference the skb already holds moves into the metadata dst, which releases it once it has been consumed.

The helper owns the whole operation: a dst that is not a route is not dropped, because a metadata dst is the encapsulation itself. Its callers must not transmit with the stale route, so a failed copy is reported and the skb is left untouched for its owner to drop.

Conditions to recreate the bug:
Install a route with an encapsulation (ip route add ... encap ip id <id> dst <remote>, or encap xfrm if_id <id>), steer a packet to another device's neighbour with a netdev egress fwd rule or bpf_redirect_neigh(), and release the route dst before neigh_xmit() hands the packet on. A tunnel or xfrm device in the path then finds no parameters in skb_tunnel_info() or skb_xfrm_md_info() and drops the packet instead of using them.

---
 include/net/dst_metadata.h |  1 +
 net/core/dst.c             | 72 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+)
include/net/dst_metadata.h +1 -0
index f45d1e3163f0..32d74b4f009f 100644
@@ -136,6 +136,7 @@ struct metadata_dst *metadata_dst_alloc(u8 optslen, enum metadata_type type,
 void metadata_dst_free_percpu(struct metadata_dst __percpu *md_dst);
 struct metadata_dst __percpu *
 metadata_dst_alloc_percpu(u8 optslen, enum metadata_type type, gfp_t flags);
+bool skb_dst_drop_keep_metadata(struct sk_buff *skb);
 
 static inline struct metadata_dst *tun_rx_dst(int md_size)
 {
net/core/dst.c +72 -0

75 lines not on this page · raw message

Signature · 1 line
2.43.0
#4 lore raw

PATCH v2 3/4 netfilter: nft_fwd_netdev: drop the stale dst when steering to a device

In reply to Jamal Hadi Salim

This is a followup of the review of the teql arp-queue fix as reported by Sashiko.

The issue:
The neighbour variant of fwd rewrites skb->dev to the configured output device and calls neigh_xmit(), but leaves the skb's dst untouched. If that device's neighbour for the configured nexthop is incomplete, the packet is parked on the neighbour's arp_queue while the stale dst still describes the device the packet was routed through. Draining the queue re-looks the neighbour up from that dst, so the frame is built with the routed device's link-layer header and then transmitted on the configured device. The packet leaves with another device's addresses.

The Fix:
Release the dst before the device switch. Both lookup inputs the neighbour transmission needs, the device and the nexthop, are supplied by the rule, so the route dst is never consulted. Without it the queue drain keeps the parking neighbour and the frame carries the configured device's addresses.

The release goes through skb_dst_drop_keep_metadata(), so encapsulation parameters a route carries are handed over to a metadata dst instead of being lost, and a metadata dst already on the packet is left in place. If that copy cannot be allocated the packet is dropped, never sent with the stale route.

Conditions to recreate the bug:
Two network namespaces, A (10.10.0.1/24) and B (10.1.0.1/24) in one, their veth peers in the other with only A's peer addressed (10.10.0.2/24) so B's nexthop cannot be resolved by ARP. Resolve A's neighbour for 10.10.0.2 as permanent, then load this ruleset on A:

    table netdev t {
            chain c {
                    type filter hook egress device va priority 0
                    policy accept
                    fwd ip to 10.1.0.2 device vb
            }
    }

Send one packet to 10.10.0.2. It is parked on B's incomplete neighbour for 10.1.0.2; once that neighbour becomes valid the frame leaves B carrying A's source address and A's neighbour as the destination.

---
v1 -> v2:
- Do not drop a dst that is not a route: a DST_METADATA dst is the tunnel template a tunnel helper attached to the packet, and dropping it discards the encapsulation parameters. Considered comment from Pablo Neira Ayuso.
- Keep a route's encapsulation parameters as well: a route installed with an ip, ip6 or xfrm encapsulation carries them in dst->lwtstate, which a drop would discard before a downstream tunnel or xfrm device could use them. The dst release now goes through a helper that moves them onto a metadata dst first (new 2/4).

 net/netfilter/nft_fwd_netdev.c | 6 ++++++
 1 file changed, 6 insertions(+)
net/netfilter/nft_fwd_netdev.c +6 -0
index a48c2f765bba..3f2ffda1998c 100644
@@ -14,6 +14,7 @@
 #include <net/netfilter/nf_tables.h>
 #include <net/netfilter/nf_tables_offload.h>
 #include <net/netfilter/nf_dup_netdev.h>
+#include <net/dst_metadata.h>
 #include <net/neighbour.h>
 #include <net/ip.h>
 
@@ -159,6 +160,11 @@ static void nft_fwd_neigh_eval(const struct nft_expr *expr,
 		goto out;
 	}
 
+	if (!skb_dst_drop_keep_metadata(skb)) {
+		verdict = NF_DROP;
+		goto out;
+	}
+
 	local_bh_disable();
 	if (nf_dev_xmit_recursion()) {
 		local_bh_enable();
Signature · 1 line
2.43.0
maintainer Reviewed-by on v2 3/4
#5 lore raw

Re: [PATCH net-next v2 3/4] netfilter: nft_fwd_netdev: drop the stale dst when steering to a device

Quoted 43 lines · Jamal Hadi Salim

This is a followup of the review of the teql arp-queue fix as reported by Sashiko.

The issue:
The neighbour variant of fwd rewrites skb->dev to the configured output device and calls neigh_xmit(), but leaves the skb's dst untouched. If that device's neighbour for the configured nexthop is incomplete, the packet is parked on the neighbour's arp_queue while the stale dst still describes the device the packet was routed through. Draining the queue re-looks the neighbour up from that dst, so the frame is built with the routed device's link-layer header and then transmitted on the configured device. The packet leaves with another device's addresses.

The Fix:
Release the dst before the device switch. Both lookup inputs the neighbour transmission needs, the device and the nexthop, are supplied by the rule, so the route dst is never consulted. Without it the queue drain keeps the parking neighbour and the frame carries the configured device's addresses.

The release goes through skb_dst_drop_keep_metadata(), so encapsulation parameters a route carries are handed over to a metadata dst instead of being lost, and a metadata dst already on the packet is left in place. If that copy cannot be allocated the packet is dropped, never sent with the stale route.

Conditions to recreate the bug:
Two network namespaces, A (10.10.0.1/24) and B (10.1.0.1/24) in one, their veth peers in the other with only A's peer addressed (10.10.0.2/24) so B's nexthop cannot be resolved by ARP. Resolve A's neighbour for 10.10.0.2 as permanent, then load this ruleset on A:

    table netdev t {
            chain c {
                    type filter hook egress device va priority 0
                    policy accept
                    fwd ip to 10.1.0.2 device vb
            }
    }

Send one packet to 10.10.0.2. It is parked on B's incomplete neighbour for 10.1.0.2; once that neighbour becomes valid the frame leaves B carrying A's source address and A's neighbour as the destination.

Quoted 6 lines
#6 lore raw

PATCH v2 4/4 bpf: drop the stale dst when redirecting to an explicit nexthop

In reply to Jamal Hadi Salim

This is a followup of the review of the teql arp-queue fix as reported by Sashiko.

The issue:
__bpf_redirect_neigh_v4() and __bpf_redirect_neigh_v6() install a dst for the redirect target only when the program lets the helper do the FIB lookup. With BPF_F_NEXTHOP the program supplies the nexthop, the lookup is skipped, and whatever dst the packet arrived with stays attached while bpf_out_neigh_v{4,6}() switch skb->dev to the target device. If that device's neighbour is incomplete the packet is parked on its arp_queue with the original route's dst; draining the queue re-looks the neighbour up from that dst and builds the frame with the original device's link-layer header, then transmits it on the target device.

The Fix:
Release the dst when the nexthop is supplied. That branch never consults the route dst, so nothing that describes the route is lost, and the queue drain keeps the target device's parking neighbour.

The release goes through skb_dst_drop_keep_metadata(), so encapsulation parameters a route carries are handed over to a metadata dst instead of being lost, and a metadata dst already on the packet is left in place. Both wrappers take their existing drop path when that copy cannot be allocated, so a packet is never sent with the stale route.

Conditions to recreate the bug:
Two network namespaces, A (10.10.0.1/24) and B (10.1.0.1/24) in one, their veth peers in the other with only A's peer addressed (10.10.0.2/24) so B's nexthop cannot be resolved by ARP. Resolve A's neighbour for 10.10.0.2 as permanent and attach a cls_bpf egress program to A, with vb the index of B:

    struct bpf_redir_neigh nh = { .nh_family = AF_INET,
                                  .ipv4_nh = htonl(0x0a010002) };

    return bpf_redirect_neigh(vb, &nh, sizeof(nh), 0);

Send one packet to 10.10.0.2. It is parked on B's incomplete neighbour for 10.1.0.2; once that neighbour becomes valid the frame leaves B carrying A's source address and A's neighbour as the destination.

---
v1 -> v2:
- Do not drop a dst that is not a route, and keep the encapsulation parameters a route carries: the release goes through a helper that moves them onto a metadata dst first (new 2/4), and a copy that cannot be allocated drops the packet instead of sending it with the stale route.

 net/core/filter.c | 4 ++++
 1 file changed, 4 insertions(+)
net/core/filter.c +4 -0
index 2d574ae52c4b..4980fb8db299 100644
@@ -2318,6 +2318,8 @@ static int __bpf_redirect_neigh_v6(struct sk_buff *skb, struct net_device *dev,
 		skb_dst_set(skb, dst);
 	} else if (nh->nh_family != AF_INET6) {
 		goto out_drop;
+	} else if (!skb_dst_drop_keep_metadata(skb)) {
+		goto out_drop;
 	}
 
 	err = bpf_out_neigh_v6(net, skb, dev, nh);
@@ -2433,6 +2435,8 @@ static int __bpf_redirect_neigh_v4(struct sk_buff *skb, struct net_device *dev,
 
 		skb_dst_drop(skb);
 		skb_dst_set(skb, &rt->dst);
+	} else if (!skb_dst_drop_keep_metadata(skb)) {
+		goto out_drop;
 	}
 
 	err = bpf_out_neigh_v4(net, skb, dev, nh);
Signature · 1 line
2.43.0
#7 lore raw

Re: [PATCH net-next v2 4/4] bpf: drop the stale dst when redirecting to an explicit nexthop

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] bpf: missing Fixes trailer for commit ba452c9e996d in commit message --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] bpf: missing Fixes trailer for commit ba452c9e996d in commit message
--

commit 69d202e339fb4761ba7ad2174f002c73503bb498
Author: Jamal Hadi Salim <[email protected]>

Quoted 42 lines

bpf: drop the stale dst when redirecting to an explicit nexthop

This is a followup of the review of the teql arp-queue fix as reported by Sashiko.

The issue:
__bpf_redirect_neigh_v4() and __bpf_redirect_neigh_v6() install a dst for the redirect target only when the program lets the helper do the FIB lookup. With BPF_F_NEXTHOP the program supplies the nexthop, the lookup is skipped, and whatever dst the packet arrived with stays attached while bpf_out_neigh_v{4,6}() switch skb->dev to the target device. If that device's neighbour is incomplete the packet is parked on its arp_queue with the original route's dst; draining the queue re-looks the neighbour up from that dst and builds the frame with the original device's link-layer header, then transmits it on the target device.

The Fix:
Release the dst when the nexthop is supplied. That branch never consults the route dst, so nothing that describes the route is lost, and the queue drain keeps the target device's parking neighbour.

The release goes through skb_dst_drop_keep_metadata(), so encapsulation parameters a route carries are handed over to a metadata dst instead of being lost, and a metadata dst already on the packet is left in place. Both wrappers take their existing drop path when that copy cannot be allocated, so a packet is never sent with the stale route.

Conditions to recreate the bug:
Two network namespaces, A (10.10.0.1/24) and B (10.1.0.1/24) in one, their veth peers in the other with only A's peer addressed (10.10.0.2/24) so B's nexthop cannot be resolved by ARP. Resolve A's neighbour for 10.10.0.2 as permanent and attach a cls_bpf egress program to A, with vb the index of B:

    struct bpf_redir_neigh nh = { .nh_family = AF_INET,
                                  .ipv4_nh = htonl(0x0a010002) };

    return bpf_redirect_neigh(vb, &nh, sizeof(nh), 0);

Send one packet to 10.10.0.2. It is parked on B's incomplete neighbour for 10.1.0.2; once that neighbour becomes valid the frame leaves B carrying A's source address and A's neighbour as the destination.

Quoted 6 lines

[Severity: Low]
Should this commit include a Fixes: tag for the commit that added the explicit nexthop branch in __bpf_redirect_neigh_v4() and __bpf_redirect_neigh_v6()?

Fixes ba452c9e996d ("bpf: Fix bpf_redirect_neigh helper api to support supplying nexthop")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4