bpf: Fix generic devmap egress skb sharing
Subsystems (6)
v5 cover bpf: Fix generic devmap egress skb sharing
Generic XDP devmap multi redirect can leave cloned skbs sharing packet data. When a devmap egress program mutates packet data, another destination sharing the same data may observe that mutation.
Fix this by making cloned skbs private before running the generic devmap egress program. The private copy is made in dev_map_generic_redirect() so dev_map_bpf_prog_run_skb() can keep returning the XDP action directly.
Add selftest coverage for the last-destination case, where the final destination runs on the original skb while earlier destinations use cloned skbs. The test records the source MAC observed by an earlier destination and checks that it is neither the sentinel value left in the result map nor the MAC written by the final destination.
---
v5:
- Move the skb_copy() check back to dev_map_generic_redirect() to keep dev_map_bpf_prog_run_skb() returning only the XDP action.
- Preserve mac_len after skb_copy().
- Use __be64 temporary values when updating mac_map from userspace.
- Initialize rx_mac with a sentinel in the last-destination test instead of relying on -ENOENT for ARRAY map lookups.
- Adjust the last-destination test topology so the checked earlier destination is not the ingress/source veth.
- Split the last-destination check into two assertions: one for store_mac_1 updating rx_mac and one for detecting last-destination rewrite leakage.
v4: https://lore.kernel.org/bpf/[email protected]/T/#mf830f03d362f33e0941d1b0e425169698fce76e5
- Preserve mac_len after skb_copy().
- Separate errno return from XDP action output in
dev_map_bpf_prog_run_skb().
- Zero-initialize net_config in the new selftest.
v3: https://lore.kernel.org/bpf/[email protected]/
- Split the kernel fix and selftest into separate patches.
- Move the private-copy logic into dev_map_bpf_prog_run_skb().
- Use deterministic DEVMAP_HASH keys in the last-destination selftest.
- Fix the Fixes tag.
v2: https://lore.kernel.org/bpf/[email protected]/
- Move the private-copy step into dev_map_generic_redirect() so the last-destination path is covered as well.
- Use skb_copy() instead of skb_unshare() to keep caller ownership unchanged on allocation failure.
- Add a generic XDP last-destination selftest case.
v1: https://lore.kernel.org/bpf/CABFUUZFimdrZdq=NWi+N-0sJZWvMwY=f4iF6-3TVMS8=m07Zmw@mail.gmail.com/
Sun Jian (2):
bpf: Run generic devmap egress prog on private skb selftests/bpf: Cover generic devmap egress last-dst rewrite
kernel/bpf/devmap.c | 12 ++ .../selftests/bpf/prog_tests/test_xdp_veth.c | 166 +++++++++++++++++- 2 files changed, 175 insertions(+), 3 deletions(-)
base-commit: e7ae89a0c97ce2b68b0983cd01eda67cf373517d
Signature · 1 line
2.43.0
PATCH v5 1/2 bpf: Run generic devmap egress prog on private skb
Generic XDP devmap multi redirect uses skb_clone() for intermediate destinations and sends the last destination with the original skb. This can leave multiple destinations sharing the same packet data.
This becomes visible after generic devmap egress-program support was
added: a devmap egress program may mutate packet data, and another destination sharing the same data can observe that mutation.
Native XDP broadcast redirect does not have this issue because xdpf_clone() copies the frame data for each destination. Generic XDP should provide the same per-destination isolation before running a devmap egress program.
Fix this by making cloned skbs private before running the generic devmap egress program. Use skb_copy() instead of skb_unshare() so allocation failure does not consume the skb and the existing caller error paths keep their ownership semantics.
--- kernel/bpf/devmap.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
kernel/bpf/devmap.c +12 -0
@@ -706,6 +706,18 @@ int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,if (unlikely (err))return err; +if (dst->xdp_prog &&skb_cloned (skb)) { +struct sk_buff *nskb; + + nskb =skb_copy (skb,GFP_ATOMIC ); +if (!nskb) +return -ENOMEM ; + + nskb->mac_len = skb->mac_len; +consume_skb (skb); + skb = nskb; + } +/* Redirect has already succeeded semantically at this point, so we just * return 0 even if packet is dropped. Helper below takes care of * freeing skb.
Signature · 1 line
2.43.0
Jakub Kicinski
maintainerRe: [PATCH bpf v5 1/2] bpf: Run generic devmap egress prog on private skb
Quoted 1 line · On Fri, 12 Jun 2026 19:40:31 +0800 Sun Jian wrote:
Suggested-by Jakub Kicinski <[email protected]>
I did not suggest this
Re: [PATCH bpf v5 1/2] bpf: Run generic devmap egress prog on private skb
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Fri, 12 Jun 2026 19:40:31 +0800 Sun Jian wrote:
Suggested-by Jakub Kicinski <[email protected]>I did not suggest this
ohh. I didn't follow discussion closely.
Do you want me to revert the whole set or just remove that line?
Jakub Kicinski
maintainerRe: [PATCH bpf v5 1/2] bpf: Run generic devmap egress prog on private skb
Quoted 9 lines · On Sat, 13 Jun 2026 10:53:35 -0700 Alexei Starovoitov wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Fri, 12 Jun 2026 19:40:31 +0800 Sun Jian wrote:
Suggested-by Jakub Kicinski <[email protected]>I did not suggest this
ohh. I didn't follow discussion closely.
Do you want me to revert the whole set or just remove that line?
It's alright, I was just being grumpy.
Maybe we can amend Chris's prompts to catch these? Noobs tend to add Suggested-by for people who simply commented on patches.
Re: [PATCH bpf v5 1/2] bpf: Run generic devmap egress prog on private skb
Quoted 9 lines · Jakub Kicinski
Quoted 7 lines · On Sat, 13 Jun 2026 10:53:35 -0700 Alexei Starovoitov wrote:
Quoted 5 lines · Jakub Kicinski
Quoted 1 line · On Fri, 12 Jun 2026 19:40:31 +0800 Sun Jian wrote:
Suggested-by Jakub Kicinski <[email protected]>I did not suggest this
Quoted 8 lines
Quoted 2 lines
ohh. I didn't follow discussion closely.
Do you want me to revert the whole set or just remove that line?It's alright, I was just being grumpy.
Maybe we can amend Chris's prompts to catch these? Noobs tend to add Suggested-by for people who simply commented on patches.
Hi Jakub, Alexei,
Sorry about that. I had meant to drop that Suggested-by tag before sending v5, but missed it.
No action needed from my side.
Thanks,
Sun Jian
PATCH v5 2/2 selftests/bpf: Cover generic devmap egress last-dst rewrite
In reply to Sun JianStrengthen xdp_veth_egress to check that each destination observes the MAC selected for its own egress ifindex, instead of only checking that the observed MAC differs from a single magic value.
Add a generic XDP last-destination test where an earlier destination does not have a devmap egress program while the final destination does. This covers the case where the final destination runs on the original skb and could otherwise rewrite packet data still shared with an earlier cloned skb.
Use deterministic DEVMAP_HASH keys for the egress map so the intended last destination is stable. Initialize the result map with a sentinel value and check that store_mac_1 overwrites it before checking that the earlier destination did not observe the MAC written by the final destination.
--- .../selftests/bpf/prog_tests/test_xdp_veth.c | 166 +++++++++++++++++- 1 file changed, 163 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +163 -3
@@ -456,7 +456,11 @@ static void xdp_veth_egress(u32 flags) .remote_flags = flags, } }; -const char magic_mac[6 ] = {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0xFF }; +const unsigned char egress_macs[VETH_PAIRS_COUNT ][ETH_ALEN ] = { + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x01 }, + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x02 }, + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x03 }, + };struct xdp_redirect_multi_kern *xdp_redirect_multi_kern;struct bpf_object *bpf_objs[VETH_EGRESS_SKEL_NB ];struct xdp_redirect_map *xdp_redirect_map;
@@ -512,7 +516,13 @@ static void xdp_veth_egress(u32 flags) &net_config, prog_cfg, i)) goto destroy_xdp_redirect_map; - err =bpf_map_update_elem (mac_map, &ifindex, magic_mac,0 ); + { +__be64 mac =0 ; + +memcpy (&mac, egress_macs[i],ETH_ALEN ); + err =bpf_map_update_elem (mac_map, &ifindex, &mac,0 ); + } +if (!ASSERT_OK (err,"bpf_map_update_elem" )) goto destroy_xdp_redirect_map;
@@ -531,15 +541,162 @@ static void xdp_veth_egress(u32 flags)for (i =0 ; i <2 ; i++) {u32 key = i; +__be64 expected =0 ;u64 res; err =bpf_map_lookup_elem (res_map, &key, &res);if (!ASSERT_OK (err,"get MAC res" )) goto destroy_xdp_redirect_map; -ASSERT_STRNEQ ((const char *)&res, magic_mac,ETH_ALEN ,"compare mac" ); +/* store_mac_1/2 run on the second/third remote veths. */ +memcpy (&expected, egress_macs[i +1 ],ETH_ALEN ); +ASSERT_EQ (res, expected,"compare mac" ); + } + +destroy_xdp_redirect_map: +close_netns (nstoken); +xdp_redirect_map__destroy (xdp_redirect_map); +destroy_xdp_redirect_multi_kern: +xdp_redirect_multi_kern__destroy (xdp_redirect_multi_kern); +destroy_xdp_dummy: +xdp_dummy__destroy (xdp_dummy); + +cleanup_network (&net_config); +} + +static void xdp_veth_egress_last_dst (u32 flags) +{ +struct prog_configuration prog_cfg[VETH_PAIRS_COUNT ] = { + { + .local_name ="xdp_redirect_map_all_prog" , + .remote_name ="xdp_dummy_prog" , + .local_flags = flags, + .remote_flags = flags, + }, + { + .local_name ="xdp_redirect_map_all_prog" , + .remote_name ="store_mac_1" , + .local_flags = flags, + .remote_flags = flags, + }, + { + .local_name ="xdp_redirect_map_all_prog" , + .remote_name ="xdp_dummy_prog" , + .local_flags = flags, + .remote_flags = flags, + } + }; +const unsigned char egress_macs[VETH_PAIRS_COUNT ][ETH_ALEN ] = { + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x01 }, + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x02 }, + {0xAA ,0xBB ,0xCC ,0xDD ,0xEE ,0x03 }, + }; +struct xdp_redirect_multi_kern *xdp_redirect_multi_kern; +struct bpf_object *bpf_objs[VETH_EGRESS_SKEL_NB ]; +struct xdp_redirect_map *xdp_redirect_map; +struct net_configuration net_config = {}; +int mac_map, egress_map, res_map; +struct nstoken *nstoken =NULL ; +struct xdp_dummy *xdp_dummy; +__be64 sentinel_mac =0 ; +__be64 last_mac =0 ; +__be64 res; +u32 key; +int err; +int i; + + xdp_dummy =xdp_dummy__open_and_load (); +if (!ASSERT_OK_PTR (xdp_dummy,"xdp_dummy__open_and_load" )) +return ; + + xdp_redirect_multi_kern =xdp_redirect_multi_kern__open_and_load (); +if (!ASSERT_OK_PTR (xdp_redirect_multi_kern,"xdp_redirect_multi_kern__open_and_load" )) + goto destroy_xdp_dummy; + + xdp_redirect_map =xdp_redirect_map__open_and_load (); +if (!ASSERT_OK_PTR (xdp_redirect_map,"xdp_redirect_map__open_and_load" )) + goto destroy_xdp_redirect_multi_kern; + +if (!ASSERT_OK (create_network (&net_config),"create network" )) + goto destroy_xdp_redirect_map; + + mac_map =bpf_map__fd (xdp_redirect_multi_kern->maps.mac_map); +if (!ASSERT_OK_FD (mac_map,"open mac_map" )) + goto destroy_xdp_redirect_map; + + egress_map =bpf_map__fd (xdp_redirect_multi_kern->maps.map_egress); +if (!ASSERT_OK_FD (egress_map,"open map_egress" )) + goto destroy_xdp_redirect_map; + + bpf_objs[0 ] = xdp_dummy->obj; + bpf_objs[1 ] = xdp_redirect_multi_kern->obj; + bpf_objs[2 ] = xdp_redirect_map->obj; + + nstoken =open_netns (net_config.ns0_name); +if (!ASSERT_OK_PTR (nstoken,"open NS0" )) + goto destroy_xdp_redirect_map; + +for (i =0 ; i <VETH_PAIRS_COUNT ; i++) { +struct bpf_devmap_val devmap_val = {}; +int ifindex =if_nametoindex (net_config.veth_cfg[i].local_veth); +u32 key = i; + +SYS (destroy_xdp_redirect_map, +"ip -n %s neigh add %s lladdr 00:00:00:00:00:01 dev %s" , + net_config.veth_cfg[i].namespace,IP_NEIGH , + net_config.veth_cfg[i].remote_veth); + +if (attach_programs_to_veth_pair (bpf_objs,VETH_EGRESS_SKEL_NB , + &net_config, prog_cfg, i)) + goto destroy_xdp_redirect_map; + + { +__be64 mac =0 ; + +memcpy (&mac, egress_macs[i],ETH_ALEN ); + err =bpf_map_update_elem (mac_map, &ifindex, &mac,0 ); + } + +if (!ASSERT_OK (err,"bpf_map_update_elem" )) + goto destroy_xdp_redirect_map; + + devmap_val.ifindex = ifindex; + devmap_val.bpf_prog.fd =-1 ; + +if (i ==VETH_PAIRS_COUNT -1 ) + devmap_val.bpf_prog.fd = +bpf_program__fd (xdp_redirect_multi_kern->progs.xdp_devmap_prog); + + err =bpf_map_update_elem (egress_map, &key, &devmap_val,0 ); +if (!ASSERT_OK (err,"bpf_map_update_elem" )) + goto destroy_xdp_redirect_map; } + res_map =bpf_map__fd (xdp_redirect_map->maps.rx_mac); +if (!ASSERT_OK_FD (res_map,"open rx_map" )) + goto destroy_xdp_redirect_map; + +memcpy (&sentinel_mac, egress_macs[VETH_PAIRS_COUNT -1 ],ETH_ALEN ); +memcpy (&last_mac, egress_macs[VETH_PAIRS_COUNT -1 ],ETH_ALEN ); + + key =0 ; + err =bpf_map_update_elem (res_map, &key, &sentinel_mac,0 ); +if (!ASSERT_OK (err,"init rx mac" )) + goto destroy_xdp_redirect_map; + +SYS_NOFAIL ("ip netns exec %s ping %s -i 0.1 -c 4 -W1 > /dev/null " , + net_config.veth_cfg[0 ].namespace,IP_NEIGH ); + + err =bpf_map_lookup_elem (res_map, &key, &res); +if (!ASSERT_OK (err,"get MAC res" )) + goto destroy_xdp_redirect_map; + +if (!ASSERT_NEQ (res, sentinel_mac,"rx_mac overwritten by store_mac_1" )) + goto destroy_xdp_redirect_map; + +if (!ASSERT_NEQ (res, last_mac,"earlier dst not rewritten by last dst" )) + goto destroy_xdp_redirect_map; + destroy_xdp_redirect_map:close_netns (nstoken);xdp_redirect_map__destroy (xdp_redirect_map);
@@ -596,4 +753,7 @@ void test_xdp_veth_egress(void)if (test__start_subtest ("SKB_MODE/egress" ))xdp_veth_egress (XDP_FLAGS_SKB_MODE ); + +if (test__start_subtest ("SKB_MODE/egress_last_dst" )) +xdp_veth_egress_last_dst (XDP_FLAGS_SKB_MODE ); }
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH bpf v5 2/2] selftests/bpf: Cover generic devmap egress last-dst rewrite
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Medium] The sentinel value sentinel_mac is identically initialized to the same value as last_mac, masking the bug the test is intended to detect and resulting in unreachably broken assertions.
- [Low] Redundant manual file descriptor validation on skeleton-generated maps violates the BPF subsystem Skeleton vs Manual Lookup guidelines.
Pre-existing issues:
- [High] Uninitialized stack variable net_config is passed to cleanup_network on error paths in xdp_veth_egress, leading to undefined behavior and potential command injection. --
commit a20e1dfd3338912dfdbb631c91d105ada7591c25
Author: Sun Jian <[email protected]>
selftests/bpf: Cover generic devmap egress last-dst rewrite
This commit strengthens xdp_veth_egress to check that each destination observes the MAC selected for its own egress ifindex, and adds a generic XDP last-destination test.
Quoted 5 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +0
-0@@ -531,15 +541,162 @@ static void xdp_veth_egress(u32 flags)
[ ... ]
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c static void xdp_veth_egress(u32 flags) · line 564
Quoted 9 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +9
-0+destroy_xdp_redirect_map: +close_netns (nstoken); +xdp_redirect_map__destroy (xdp_redirect_map); +destroy_xdp_redirect_multi_kern: +xdp_redirect_multi_kern__destroy (xdp_redirect_multi_kern); +destroy_xdp_dummy: +xdp_dummy__destroy (xdp_dummy); + +cleanup_network (&net_config);
[Severity: High]
This isn't a bug introduced by this patch, but is it safe to pass the uninitialized net_config struct to cleanup_network() on error paths?
In xdp_veth_egress(), we declare net_config without initialization:
struct net_configuration net_config;
If skeleton loading fails (e.g., xdp_redirect_multi_kern__open_and_load()), the code jumps to destroy_xdp_dummy and calls cleanup_network(&net_config). Inside cleanup_network(), uninitialized stack data like net_config.ns0_name is passed to system() commands via SYS_NOFAIL(), which might trigger arbitrary shell commands since selftests run as root.
Quoted 4 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +4
-0+} + +static void xdp_veth_egress_last_dst (u32 flags) +{
[ ... ]
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c static void xdp_veth_egress(u32 flags) · line 629
Quoted 10 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +10
-0+if (!ASSERT_OK (create_network (&net_config),"create network" )) + goto destroy_xdp_redirect_map; + + mac_map =bpf_map__fd (xdp_redirect_multi_kern->maps.mac_map); +if (!ASSERT_OK_FD (mac_map,"open mac_map" )) + goto destroy_xdp_redirect_map; + + egress_map =bpf_map__fd (xdp_redirect_multi_kern->maps.map_egress); +if (!ASSERT_OK_FD (egress_map,"open map_egress" )) + goto destroy_xdp_redirect_map;
[Severity: Low]
Are these FD validation checks needed?
After a successful skeleton __open_and_load(), the skeleton guarantees that all maps and programs are loaded with valid FDs. We don't need additional ASSERT_OK_FD() validation when accessing fields like xdp_redirect_multi_kern->maps.mac_map.
[ ... ]
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c static void xdp_veth_egress(u32 flags) · line 680
Quoted 6 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +6
-0+ res_map =bpf_map__fd (xdp_redirect_map->maps.rx_mac); +if (!ASSERT_OK_FD (res_map,"open rx_map" )) + goto destroy_xdp_redirect_map; + +memcpy (&sentinel_mac, egress_macs[VETH_PAIRS_COUNT -1 ],ETH_ALEN ); +memcpy (&last_mac, egress_macs[VETH_PAIRS_COUNT -1 ],ETH_ALEN );
[Severity: Medium]
Does initializing sentinel_mac and last_mac to the same value bypass the intended checks?
Since both are initialized to egress_macs[VETH_PAIRS_COUNT - 1], if the res equals last_mac, the test will fail at the first assertion:
if (!ASSERT_NEQ(res, sentinel_mac, "rx_mac overwritten by store_mac_1"))
with a misleading error message. The second assertion checking against last_mac then becomes unreachable:
if (!ASSERT_NEQ(res, last_mac, "earlier dst not rewritten by last dst"))
Quoted 3 lines
tools/testing/selftests/bpf/prog_tests/test_xdp_veth.c +3
-0+ + key =0 ; + err =bpf_map_update_elem (res_map, &key, &sentinel_mac,0 );
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Fri, 12 Jun 2026 19:40:30 +0800 you wrote:
Generic XDP devmap multi redirect can leave cloned skbs sharing packet data. When a devmap egress program mutates packet data, another destination sharing the same data may observe that mutation.
Fix this by making cloned skbs private before running the generic devmap egress program. The private copy is made in
dev_map_generic_redirect()sodev_map_bpf_prog_run_skb()can keep returning the XDP action directly.[...]
Here is the summary with links:
- [bpf,v5,1/2] bpf: Run generic devmap egress prog on private skb
https://git.kernel.org/bpf/bpf-next/c/6001896f0098 - [bpf,v5,2/2] selftests/bpf: Cover generic devmap egress last-dst rewrite https://git.kernel.org/bpf/bpf-next/c/f0eff94d07cd
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html