bpf: Fix calling global functions from BPF_PROG_TYPE_EXT programs
Subsystems (5)
PATCH 1/2 bpf: Fix calling global functions from BPF_PROG_TYPE_EXT programs
The verifier allows programs to call global functions as long as their argument types match, using BTF to check the function arguments. One of the allowed argument types to such global functions is PTR_TO_CTX; however the check for this fails on BPF_PROG_TYPE_EXT functions because the verifier uses the wrong type to fetch the vmlinux BTF ID for the program context type. This failure is seen when an XDP program is loaded using libxdp (which loads it as BPF_PROG_TYPE_EXT and attaches it to a global XDP type program).
Fix the issue by passing in the target program type instead of the BPF_PROG_TYPE_EXT type to bpf_prog_get_ctx() when checking function argument compatibility.
The first Fixes tag refers to the latest commit that touched the code in question, while the second one points to the code that first introduced the global function call verification.
--- kernel/bpf/btf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
kernel/bpf/btf.c +5 -1
@@ -6054,6 +6054,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, struct bpf_reg_state *regs, bool ptr_to_mem_ok) { +enum bpf_prog_type prog_type = env->prog->type;struct bpf_verifier_log *log = &env->log;u32 i, nargs, ref_id, ref_obj_id =0 ;bool is_kfunc =btf_is_kernel (btf);
@@ -6095,6 +6096,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, BTF_KFUNC_TYPE_KPTR_ACQUIRE, func_id); } +if (prog_type ==BPF_PROG_TYPE_EXT && env->prog->aux->dst_prog) + prog_type = env->prog->aux->dst_prog->type; +/* check that BTF function arguments match actual types that the * verifier sees. */
@@ -6171,7 +6175,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,return -EINVAL ; }/* rest of the arguments can be anything, like normal kfunc */ - }else if (btf_get_prog_ctx_type (log, btf, t, env->prog->type, i)) { + }else if (btf_get_prog_ctx_type (log, btf, t, prog_type, i)) {/* If function expects ctx type in BTF check that caller * is passing PTR_TO_CTX. */
Signature · 1 line
2.36.1
PATCH 2/2 selftests/bpf: Add selftest for calling global functions from freplace
Add a selftest that calls a global function with a context object parameter from an freplace function to check that the program context type is correctly converted to the freplace target when fetching the context type from the kernel BTF.
--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 13 ++++++++++ .../bpf/progs/freplace_global_func.c | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/freplace_global_func.c
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +13 -0
@@ -395,6 +395,17 @@ static void test_func_map_prog_compatibility(void) "./test_attach_probe.o"); } +static void test_func_replace_global_func (void ) +{ +const char *prog_name[] = { +"freplace/test_pkt_access" , + }; +test_fexit_bpf2bpf_common ("./freplace_global_func.o" , +"./test_pkt_access.o" , +ARRAY_SIZE (prog_name), + prog_name, false,NULL ); +} +/* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf (void ) {
@@ -416,4 +427,6 @@ void serial_test_fexit_bpf2bpf(void)test_func_replace_multi ();if (test__start_subtest ("fmod_ret_freplace" ))test_fmod_ret_freplace (); +if (test__start_subtest ("func_replace_global_func" )) +test_func_replace_global_func (); }
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +24 -0
@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ +#include <linux/stddef.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include <bpf/bpf_tracing.h> + +__attribute__ ((noinline)) +int test_ctx_global_func (struct __sk_buff *skb) +{ +volatile int retval =1 ; +return retval; +} + +__u64 test_pkt_access_global_func =0 ; +SEC("freplace/test_pkt_access") +int new_test_pkt_access (struct __sk_buff *skb) +{ + test_pkt_access_global_func =test_ctx_global_func (skb); +return -1 ; +} + +char _license[] SEC("license") = "GPL";
Signature · 1 line
2.36.1
Kumar Kartikeya Dwivedi
maintainerQuoted 38 lines · Toke Høiland-Jørgensen
The verifier allows programs to call global functions as long as their argument types match, using BTF to check the function arguments. One of the allowed argument types to such global functions is
PTR_TO_CTX; however the check for this fails onBPF_PROG_TYPE_EXTfunctions because the verifier uses the wrong type to fetch the vmlinux BTF ID for the program context type. This failure is seen when an XDP program is loaded using libxdp (which loads it asBPF_PROG_TYPE_EXTand attaches it to a global XDP type program).Fix the issue by passing in the target program type instead of the
BPF_PROG_TYPE_EXTtype tobpf_prog_get_ctx()when checking function argument compatibility.The first Fixes tag refers to the latest commit that touched the code in question, while the second one points to the code that first introduced the global function call verification.
Fixes 3363bd0cfbb8 ("bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support")Fixes 51c39bb1d5d1 ("bpf: Introduce function-by-function verification")Reported-by Simon Sundberg <[email protected]>Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- kernel/bpf/btf.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)kernel/bpf/btf.c +1
-0@@ -6054,6 +6054,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, struct bpf_reg_state *regs, bool ptr_to_mem_ok) { +enum bpf_prog_type prog_type = env->prog->type;struct bpf_verifier_log *log = &env->log;u32 i, nargs, ref_id, ref_obj_id =0 ;bool is_kfunc =btf_is_kernel (btf);
kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 6101
Quoted 6 lines
kernel/bpf/btf.c +3
-0@@ -6095,6 +6096,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, BTF_KFUNC_TYPE_KPTR_ACQUIRE, func_id); } +if (prog_type ==BPF_PROG_TYPE_EXT && env->prog->aux->dst_prog) + prog_type = env->prog->aux->dst_prog->type; +
nit: it might be better to reuse resolve_prog_type here.
Quoted 13 lines
kernel/bpf/btf.c +1
-2/* check that BTF function arguments match actual types that the * verifier sees. */ @@ -6171,7 +6175,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,return -EINVAL ; }/* rest of the arguments can be anything, like normal kfunc */ - }else if (btf_get_prog_ctx_type (log, btf, t, env->prog->type, i)) { + }else if (btf_get_prog_ctx_type (log, btf, t, prog_type, i)) {/* If function expects ctx type in BTF check that caller * is passing PTR_TO_CTX. */ --
Quoted 2 lines
2.36.1
--
Kartikeya
Re: [PATCH bpf 2/2] selftests/bpf: Add selftest for calling global functions from freplace
In reply to Toke Høiland-JørgensenQuoted 48 lines · Toke Høiland-Jørgensen
Add a selftest that calls a global function with a context object parameter from an freplace function to check that the program context type is correctly converted to the freplace target when fetching the context type from the kernel BTF.
Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 13 ++++++++++ .../bpf/progs/freplace_global_func.c | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/freplace_global_func.ctools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +13
-0@@ -395,6 +395,17 @@ static void test_func_map_prog_compatibility(void) "./test_attach_probe.o"); } +static void test_func_replace_global_func (void ) +{ +const char *prog_name[] = { +"freplace/test_pkt_access" , + }; +test_fexit_bpf2bpf_common ("./freplace_global_func.o" , +"./test_pkt_access.o" , +ARRAY_SIZE (prog_name), + prog_name, false,NULL ); +} +/* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf (void ) {@@ -416,4 +427,6 @@ void serial_test_fexit_bpf2bpf(void)test_func_replace_multi ();if (test__start_subtest ("fmod_ret_freplace" ))test_fmod_ret_freplace (); +if (test__start_subtest ("func_replace_global_func" )) +test_func_replace_global_func (); }tools/testing/selftests/bpf/progs/freplace_global_func.c new file +0
-0
tools/testing/selftests/bpf/progs/freplace_global_func.c line 2
Quoted 2 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +2
-0@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */
nit: I think you meant 2022, not 2019? :)
tools/testing/selftests/bpf/progs/freplace_global_func.c line 7
Quoted 5 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +5
-0+#include <linux/stddef.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include <bpf/bpf_tracing.h>
nit: I think the only headers you need here are the linux/bpf.h and bpf/bpf_helpers.h ones.
Quoted 20 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +17
-1+ +__attribute__ ((noinline)) +int test_ctx_global_func (struct __sk_buff *skb) +{ +volatile int retval =1 ; +return retval; +} + +__u64 test_pkt_access_global_func =0 ; +SEC("freplace/test_pkt_access") +int new_test_pkt_access (struct __sk_buff *skb) +{ + test_pkt_access_global_func =test_ctx_global_func (skb); +return -1 ; +} + +char _license[] SEC("license") = "GPL"; --2.36.1
Re: [PATCH bpf 2/2] selftests/bpf: Add selftest for calling global functions from freplace
In reply to Toke Høiland-JørgensenQuoted 18 lines · Toke Høiland-Jørgensen
Add a selftest that calls a global function with a context object parameter from an freplace function to check that the program context type is correctly converted to the freplace target when fetching the context type from the kernel BTF.
Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 13 ++++++++++ .../bpf/progs/freplace_global_func.c | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/freplace_global_func.ctools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +0
-0
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c static void test_func_map_prog_compatibility(void) · line 402
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +5
-0@@ -395,6 +395,17 @@ static void test_func_map_prog_compatibility(void) "./test_attach_probe.o"); } +static void test_func_replace_global_func (void ) +{ +const char *prog_name[] = { +"freplace/test_pkt_access" , + };
empty line between variables and statements
Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +8
-0+test_fexit_bpf2bpf_common ("./freplace_global_func.o" , +"./test_pkt_access.o" , +ARRAY_SIZE (prog_name), + prog_name, false,NULL ); +} +/* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf (void ) {@@ -416,4 +427,6 @@ void serial_test_fexit_bpf2bpf(void)test_func_replace_multi ();if (test__start_subtest ("fmod_ret_freplace" ))test_fmod_ret_freplace (); +if (test__start_subtest ("func_replace_global_func" )) +test_func_replace_global_func (); }tools/testing/selftests/bpf/progs/freplace_global_func.c new file +0
-0
tools/testing/selftests/bpf/progs/freplace_global_func.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +9
-0@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ +#include <linux/stddef.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include <bpf/bpf_tracing.h> + +__attribute__ ((noinline))
__noinline
tools/testing/selftests/bpf/progs/freplace_global_func.c line 13
Quoted 4 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +4
-0+int test_ctx_global_func (struct __sk_buff *skb) +{ +volatile int retval =1 ; +return retval;
just curious, why volatile instead of direct return 1;? Also, empty line between variable and return.
tools/testing/selftests/bpf/progs/freplace_global_func.c line 16
Quoted 3 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +3
-0+} + +__u64 test_pkt_access_global_func =0 ;
nit: it's a variable, please put it at the top before functions, or at the very least add empty line between it and SEC()
Quoted 11 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +8
-1+SEC("freplace/test_pkt_access") +int new_test_pkt_access (struct __sk_buff *skb) +{ + test_pkt_access_global_func =test_ctx_global_func (skb); +return -1 ; +} + +char _license[] SEC("license") = "GPL"; --2.36.1
Re: [PATCH bpf 2/2] selftests/bpf: Add selftest for calling global functions from freplace
Quoted 64 lines · Andrii Nakryiko
Quoted 18 lines · Toke Høiland-Jørgensen
Add a selftest that calls a global function with a context object parameter from an freplace function to check that the program context type is correctly converted to the freplace target when fetching the context type from the kernel BTF.
Signed-off-by Toke Høiland-Jørgensen <[email protected]>--- .../selftests/bpf/prog_tests/fexit_bpf2bpf.c | 13 ++++++++++ .../bpf/progs/freplace_global_func.c | 24 +++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/freplace_global_func.ctools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +0
-0tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c static void test_func_map_prog_compatibility(void) · line 402
Quoted 8 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +5
-0@@ -395,6 +395,17 @@ static void test_func_map_prog_compatibility(void) "./test_attach_probe.o"); } +static void test_func_replace_global_func (void ) +{ +const char *prog_name[] = { +"freplace/test_pkt_access" , + };empty line between variables and statements
Quoted 22 lines
tools/testing/selftests/bpf/prog_tests/fexit_bpf2bpf.c +8
-0+test_fexit_bpf2bpf_common ("./freplace_global_func.o" , +"./test_pkt_access.o" , +ARRAY_SIZE (prog_name), + prog_name, false,NULL ); +} +/* NOTE: affect other tests, must run in serial mode */ void serial_test_fexit_bpf2bpf (void ) {@@ -416,4 +427,6 @@ void serial_test_fexit_bpf2bpf(void)test_func_replace_multi ();if (test__start_subtest ("fmod_ret_freplace" ))test_fmod_ret_freplace (); +if (test__start_subtest ("func_replace_global_func" )) +test_func_replace_global_func (); }tools/testing/selftests/bpf/progs/freplace_global_func.c new file +0
-0tools/testing/selftests/bpf/progs/freplace_global_func.c line 9
Quoted 9 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +9
-0@@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2019 Facebook */ +#include <linux/stddef.h> +#include <linux/bpf.h> +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_endian.h> +#include <bpf/bpf_tracing.h> + +__attribute__ ((noinline))
__noinline
Quoted 7 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c line 13
Quoted 4 lines
tools/testing/selftests/bpf/progs/freplace_global_func.c new file +4
-0+int test_ctx_global_func (struct __sk_buff *skb) +{ +volatile int retval =1 ; +return retval;just curious, why volatile instead of direct
return 1;? Also, empty line between variable and return.
To prevent the compiler from optimising away the whole thing. I was actually under the impression that it shouldn't do that for global functions, but, well, it does and the 'volatile' keeps it from doing so...
Will fix the other nits and resubmit.
-Toke