bpf: Tighten cgroup storage cookie checks for prog arrays
Subsystems (4)
Daniel Borkmann
maintainerPATCH 1/2 bpf: Tighten cgroup storage cookie checks for prog arrays
The fix in commit abad3d0bad72 ("bpf: Fix oob access in cgroup local storage") is still incomplete. The prog-array compatibility check treats a program with no cgroup storage as compatible with any stored storage cookie. This allows a storage-less program to bridge a tail call chain between an entry program and a storage-using callee even though cgroup local storage at runtime still follows the caller's context, that is, A -> B(no storage) -> C(storage) path.
Requiring exact cookie equality would break the legitimate case of a storage-less leaf program being tail called from a storage-using one. Instead, only accept a zero storage cookie if the program cannot perform tail calls itself. This keeps A -> B(no storage) working while rejecting the A -> B(no storage) -> C(storage) bridge.
--- kernel/bpf/core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/core.c +1 -1
@@ -2481,7 +2481,7 @@ static bool __bpf_prog_map_compatible(struct bpf_map *map, cookie = aux->cgroup_storage[i] ? aux->cgroup_storage[i]->cookie :0 ; ret = map->owner->storage_cookie[i] == cookie || - !cookie; + (!cookie && !aux->tail_call_reachable); }if (ret && map->owner->attach_func_proto != aux->attach_func_proto) {
Signature · 1 line
2.43.0
Daniel Borkmann
maintainerPATCH 2/2 selftests/bpf: Cover tail-call cgroup storage prog-array checks
From: Lin Ma <[email protected]>
Add tail-call selftests for prog-array ownership when cgroup storage is in use. Verify that loading succeeds when callers and callees reuse the owner's cgroup storage map, and that loading fails for a different storage map and for the A(storage) -> B(no storage) -> C(storage) bridge case addressed in the previous commit.
Also verify that a storage-less leaf program which cannot perform tail calls itself is still allowed to join a storage-owned prog array, while a storage-less tail-caller is rejected also at map update time.
# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t tailcalls [...]
#475/25 tailcalls/tailcall_freplace:OK #475/26 tailcalls/tailcall_bpf2bpf_freplace:OK #475/27 tailcalls/tailcall_failure:OK #475/28 tailcalls/reject_tail_call_spin_lock:OK #475/29 tailcalls/reject_tail_call_rcu_lock:OK #475/30 tailcalls/reject_tail_call_preempt_lock:OK #475/31 tailcalls/reject_tail_call_ref:OK #475/32 tailcalls/tailcall_sleepable:OK #475/33 tailcalls/tailcall_cgrp_storage:OK #475/34 tailcalls/tailcall_cgrp_storage_diff_storage:OK #475/35 tailcalls/tailcall_cgrp_storage_no_storage:OK #475/36 tailcalls/tailcall_cgrp_storage_no_storage_leaf:OK #475/37 tailcalls/tailcall_cgrp_storage_no_storage_bridge:OK #475 tailcalls:OK
Summary: 1/37 PASSED, 0 SKIPPED, 0 FAILED
--- .../selftests/bpf/prog_tests/tailcalls.c | 186 ++++++++++++++++++ .../bpf/progs/tailcall_cgrp_storage.c | 44 +++++ .../progs/tailcall_cgrp_storage_no_storage.c | 26 +++ .../bpf/progs/tailcall_cgrp_storage_owner.c | 32 +++ 4 files changed, 288 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c create mode 100644 tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_no_storage.c create mode 100644 tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_owner.c
tools/testing/selftests/bpf/prog_tests/tailcalls.c +186 -0
@@ -8,6 +8,9 @@#include "tailcall_freplace.skel.h" #include "tc_bpf2bpf.skel.h" #include "tailcall_fail.skel.h" +#include "tailcall_cgrp_storage_owner.skel.h" +#include "tailcall_cgrp_storage_no_storage.skel.h" +#include "tailcall_cgrp_storage.skel.h" #include "tailcall_sleepable.skel.h" /* test_tailcall_1 checks basic functionality by patching multiple locations
@@ -1654,6 +1657,179 @@ static void test_tailcall_failure()RUN_TESTS (tailcall_fail); } +static void test_tailcall_cgrp_storage (void ) +{ +struct tailcall_cgrp_storage_owner *owner_skel =NULL ; +struct tailcall_cgrp_storage *skel =NULL ; +int err, key =0 , prog_array_fd, prog_fd, storage_map_fd; + + owner_skel =tailcall_cgrp_storage_owner__open_and_load (); +if (!ASSERT_OK_PTR (owner_skel,"owner_open_and_load" )) +return ; + + prog_array_fd =bpf_map__fd (owner_skel->maps.prog_array); + storage_map_fd =bpf_map__fd (owner_skel->maps.storage_map); + + skel =tailcall_cgrp_storage__open (); +if (!ASSERT_OK_PTR (skel,"tailcall_cgrp_storage__open" )) + goto out; + + err =bpf_map__reuse_fd (skel->maps.prog_array, prog_array_fd); +if (!ASSERT_OK (err,"reuse_prog_array" )) + goto out; + + err =bpf_map__reuse_fd (skel->maps.storage_map, storage_map_fd); +if (!ASSERT_OK (err,"reuse_storage_map" )) + goto out; + + err =bpf_object__load (skel->obj); +if (!ASSERT_OK (err,"tailcall_cgrp_storage__load" )) + goto out; + + prog_fd =bpf_program__fd (skel->progs.callee_prog); + err =bpf_map_update_elem (prog_array_fd, &key, &prog_fd,BPF_ANY ); +ASSERT_OK (err,"update_prog_array" ); +out: +tailcall_cgrp_storage__destroy (skel); +tailcall_cgrp_storage_owner__destroy (owner_skel); +} + +static void test_tailcall_cgrp_storage_diff_storage (void ) +{ +struct tailcall_cgrp_storage_owner *owner_skel =NULL ; +struct tailcall_cgrp_storage *skel =NULL ; +int err, prog_array_fd; + + owner_skel =tailcall_cgrp_storage_owner__open_and_load (); +if (!ASSERT_OK_PTR (owner_skel,"owner_open_and_load" )) +return ; + + prog_array_fd =bpf_map__fd (owner_skel->maps.prog_array); + + skel =tailcall_cgrp_storage__open (); +if (!ASSERT_OK_PTR (skel,"tailcall_cgrp_storage__open" )) + goto out; + + err =bpf_map__reuse_fd (skel->maps.prog_array, prog_array_fd); +if (!ASSERT_OK (err,"reuse_prog_array" )) + goto out; + + err =bpf_object__load (skel->obj); +ASSERT_ERR (err,"tailcall_cgrp_storage__load" ); +out: +tailcall_cgrp_storage__destroy (skel); +tailcall_cgrp_storage_owner__destroy (owner_skel); +} + +static void test_tailcall_cgrp_storage_no_storage (void ) +{ +struct tailcall_cgrp_storage_owner *owner_skel =NULL ; +struct tailcall_cgrp_storage_no_storage *skel =NULL ; +int err, prog_array_fd; + + owner_skel =tailcall_cgrp_storage_owner__open_and_load (); +if (!ASSERT_OK_PTR (owner_skel,"owner_open_and_load" )) +return ; + + prog_array_fd =bpf_map__fd (owner_skel->maps.prog_array); + + skel =tailcall_cgrp_storage_no_storage__open (); +if (!ASSERT_OK_PTR (skel,"tailcall_cgrp_storage_no_storage__open" )) + goto out; + + err =bpf_map__reuse_fd (skel->maps.prog_array, prog_array_fd); +if (!ASSERT_OK (err,"reuse_prog_array" )) + goto out; + + err =bpf_object__load (skel->obj); +ASSERT_ERR (err,"tailcall_cgrp_storage_no_storage__load" ); +out: +tailcall_cgrp_storage_no_storage__destroy (skel); +tailcall_cgrp_storage_owner__destroy (owner_skel); +} + +static void test_tailcall_cgrp_storage_no_storage_leaf (void ) +{ +struct tailcall_cgrp_storage_owner *owner_skel =NULL ; +struct tailcall_cgrp_storage_no_storage *skel =NULL ; +int err, key =0 , prog_array_fd, prog_fd; + + owner_skel =tailcall_cgrp_storage_owner__open_and_load (); +if (!ASSERT_OK_PTR (owner_skel,"owner_open_and_load" )) +return ; + + prog_array_fd =bpf_map__fd (owner_skel->maps.prog_array); + + skel =tailcall_cgrp_storage_no_storage__open_and_load (); +if (!ASSERT_OK_PTR (skel,"tailcall_cgrp_storage_no_storage__open_and_load" )) + goto out; + + prog_fd =bpf_program__fd (skel->progs.leaf_prog); + err =bpf_map_update_elem (prog_array_fd, &key, &prog_fd,BPF_ANY ); +if (!ASSERT_OK (err,"update_prog_array_leaf" )) + goto out; + + prog_fd =bpf_program__fd (skel->progs.caller_prog); + err =bpf_map_update_elem (prog_array_fd, &key, &prog_fd,BPF_ANY ); +ASSERT_ERR (err,"update_prog_array_bridge" ); +out: +tailcall_cgrp_storage_no_storage__destroy (skel); +tailcall_cgrp_storage_owner__destroy (owner_skel); +} + +static void test_tailcall_cgrp_storage_no_storage_bridge (void ) +{ +struct tailcall_cgrp_storage_owner *owner_skel =NULL ; +struct tailcall_cgrp_storage_no_storage *bridge_skel =NULL ; +struct tailcall_cgrp_storage *callee_skel =NULL ; +int err, key =0 , prog_array_fd, prog_fd, storage_map_fd; + + owner_skel =tailcall_cgrp_storage_owner__open_and_load (); +if (!ASSERT_OK_PTR (owner_skel,"owner_open_and_load" )) +return ; + + prog_array_fd =bpf_map__fd (owner_skel->maps.prog_array); + storage_map_fd =bpf_map__fd (owner_skel->maps.storage_map); + + callee_skel =tailcall_cgrp_storage__open (); +if (!ASSERT_OK_PTR (callee_skel,"tailcall_cgrp_storage__open" )) + goto out; + +bpf_program__set_autoload (callee_skel->progs.caller_prog, false); + + err =bpf_map__reuse_fd (callee_skel->maps.prog_array, prog_array_fd); +if (!ASSERT_OK (err,"reuse_prog_array" )) + goto out; + + err =bpf_map__reuse_fd (callee_skel->maps.storage_map, storage_map_fd); +if (!ASSERT_OK (err,"reuse_storage_map" )) + goto out; + + err =bpf_object__load (callee_skel->obj); +if (!ASSERT_OK (err,"tailcall_cgrp_storage__load" )) + goto out; + + prog_fd =bpf_program__fd (callee_skel->progs.callee_prog); + err =bpf_map_update_elem (prog_array_fd, &key, &prog_fd,BPF_ANY ); +if (!ASSERT_OK (err,"update_prog_array" )) + goto out; + + bridge_skel =tailcall_cgrp_storage_no_storage__open (); +if (!ASSERT_OK_PTR (bridge_skel,"tailcall_cgrp_storage_no_storage__open" )) + goto out; + + err =bpf_map__reuse_fd (bridge_skel->maps.prog_array, prog_array_fd); +if (!ASSERT_OK (err,"reuse_prog_array" )) + goto out; + + err =bpf_object__load (bridge_skel->obj); +ASSERT_ERR (err,"tailcall_cgrp_storage_no_storage_bridge__load" ); +out: +tailcall_cgrp_storage_no_storage__destroy (bridge_skel); +tailcall_cgrp_storage__destroy (callee_skel); +tailcall_cgrp_storage_owner__destroy (owner_skel); +} + noinline void uprobe_sleepable_trigger(void) { asmvolatile ("" );
@@ -1781,4 +1957,14 @@ void test_tailcalls(void)test_tailcall_failure ();if (test__start_subtest ("tailcall_sleepable" ))test_tailcall_sleepable (); +if (test__start_subtest ("tailcall_cgrp_storage" )) +test_tailcall_cgrp_storage (); +if (test__start_subtest ("tailcall_cgrp_storage_diff_storage" )) +test_tailcall_cgrp_storage_diff_storage (); +if (test__start_subtest ("tailcall_cgrp_storage_no_storage" )) +test_tailcall_cgrp_storage_no_storage (); +if (test__start_subtest ("tailcall_cgrp_storage_no_storage_leaf" )) +test_tailcall_cgrp_storage_no_storage_leaf (); +if (test__start_subtest ("tailcall_cgrp_storage_no_storage_bridge" )) +test_tailcall_cgrp_storage_no_storage_bridge (); }
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage.c new file +44 -0
44 lines not on this page · raw message
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_no_storage.c new file +26 -0
@@ -0,0 +1,26 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} prog_array SEC(".maps"); + +SEC("cgroup_skb/egress") +int caller_prog (struct __sk_buff *skb) +{ +bpf_tail_call (skb, &prog_array,0 ); +return 1 ; +} + +SEC("cgroup_skb/egress") +int leaf_prog (struct __sk_buff *skb) +{ +return 1 ; +} + +char _license[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/tailcall_cgrp_storage_owner.c new file +32 -0
@@ -0,0 +1,32 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <vmlinux.h> +#include <bpf/bpf_helpers.h> + +struct { + __uint(type, BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE); + __type(key, struct bpf_cgroup_storage_key); + __type(value, __u64); +} storage_map SEC(".maps"); + +struct { + __uint(type, BPF_MAP_TYPE_PROG_ARRAY); + __uint(max_entries, 1); + __uint(key_size, sizeof(__u32)); + __uint(value_size, sizeof(__u32)); +} prog_array SEC(".maps"); + +SEC("cgroup_skb/egress") +int prog_array_owner (struct __sk_buff *skb) +{ +__u64 *storage; + + storage =bpf_get_local_storage (&storage_map,0 ); +if (storage) + *storage =1 ; + +bpf_tail_call (skb, &prog_array,0 ); +return 1 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.0
Re: [PATCH bpf-next 2/2] selftests/bpf: Cover tail-call cgroup storage prog-array checks
Quoted 30 lines · Daniel Borkmann
From: Lin Ma <[email protected]>
Add tail-call selftests for prog-array ownership when cgroup storage is in use. Verify that loading succeeds when callers and callees reuse the owner's cgroup storage map, and that loading fails for a different storage map and for the A(storage) -> B(no storage) -> C(storage) bridge case addressed in the previous commit.
Also verify that a storage-less leaf program which cannot perform tail calls itself is still allowed to join a storage-owned prog array, while a storage-less tail-caller is rejected also at map update time.
# LDLIBS=-static PKG_CONFIG='pkg-config --static' ./vmtest.sh -- ./test_progs -t tailcalls [...] #475/25 tailcalls/tailcall_freplace:OK #475/26 tailcalls/tailcall_bpf2bpf_freplace:OK #475/27 tailcalls/tailcall_failure:OK #475/28 tailcalls/reject_tail_call_spin_lock:OK #475/29 tailcalls/reject_tail_call_rcu_lock:OK #475/30 tailcalls/reject_tail_call_preempt_lock:OK #475/31 tailcalls/reject_tail_call_ref:OK #475/32 tailcalls/tailcall_sleepable:OK #475/33 tailcalls/tailcall_cgrp_storage:OK #475/34 tailcalls/tailcall_cgrp_storage_diff_storage:OK #475/35 tailcalls/tailcall_cgrp_storage_no_storage:OK #475/36 tailcalls/tailcall_cgrp_storage_no_storage_leaf:OK #475/37 tailcalls/tailcall_cgrp_storage_no_storage_bridge:OK #475 tailcalls:OK Summary: 1/37 PASSED, 0 SKIPPED, 0 FAILED
Quoted 4 lines
Signed-off-by Lin Ma <[email protected]>Signed-off-by Rongzhen Cui <[email protected]>Signed-off-by Jingguo Tan <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Quoted 14 lines · Daniel Borkmann
The fix in commit
abad3d0bad72("bpf: Fix oob access in cgroup local storage") is still incomplete. The prog-array compatibility check treats a program with no cgroup storage as compatible with any stored storage cookie. This allows a storage-less program to bridge a tail call chain between an entry program and a storage-using callee even though cgroup local storage at runtime still follows the caller's context, that is, A -> B(no storage) -> C(storage) path.Requiring exact cookie equality would break the legitimate case of a storage-less leaf program being tail called from a storage-using one. Instead, only accept a zero storage cookie if the program cannot perform tail calls itself. This keeps A -> B(no storage) working while rejecting the A -> B(no storage) -> C(storage) bridge.
Quoted 3 lines
Fixes abad3d0bad72 ("bpf: Fix oob access in cgroup local storage")Reported-by Lin Ma <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
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 Wed, 10 Jun 2026 12:55:38 +0200 you wrote:
The fix in commit
abad3d0bad72("bpf: Fix oob access in cgroup local storage") is still incomplete. The prog-array compatibility check treats a program with no cgroup storage as compatible with any stored storage cookie. This allows a storage-less program to bridge a tail call chain between an entry program and a storage-using callee even though cgroup local storage at runtime still follows the caller's context, that is, A -> B(no storage) -> C(storage) path.[...]
Here is the summary with links:
- [bpf-next,1/2] bpf: Tighten cgroup storage cookie checks for prog arrays
https://git.kernel.org/bpf/bpf-next/c/10627ddc0167 - [bpf-next,2/2] selftests/bpf: Cover tail-call cgroup storage prog-array checks https://git.kernel.org/bpf/bpf-next/c/30dee2c176e7
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html