limit bpf_core_types_are_compat recursion
Subsystems (4)
v3 cover limit bpf_core_types_are_compat recursion
From: Matteo Croce <[email protected]>
As formerly discussed on the BPF mailing list:
https://lore.kernel.org/bpf/CAADnVQJDax2j0-7uyqdqFEnpB57om_z+Cqmi1O2QyLpHqkVKwA@mail.gmail.com/
changes from v2:
test the bpf_core_type_exists() return value, and check that the recursion limit is enforced.
Matteo Croce (2):
bpf: limit bpf_core_types_are_compat() recursion selftests/bpf: test maximum recursion depth for
bpf_core_types_are_compat()
include/linux/btf.h | 5 + kernel/bpf/btf.c | 105 +++++++++++++++++- tools/testing/selftests/bpf/Makefile | 2 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 5 + .../selftests/bpf/prog_tests/core_kern.c | 15 ++- .../bpf/prog_tests/core_kern_overflow.c | 13 +++ tools/testing/selftests/bpf/progs/core_kern.c | 14 +++ .../selftests/bpf/progs/core_kern_overflow.c | 21 ++++ 8 files changed, 177 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/core_kern_overflow.c create mode 100644 tools/testing/selftests/bpf/progs/core_kern_overflow.c
Signature · 1 line
2.34.1
PATCH v3 1/2 bpf: limit bpf_core_types_are_compat() recursion
From: Matteo Croce <[email protected]>
In userspace, bpf_core_types_are_compat() is a recursive function which can't be put in the kernel as is.
Limit the recursion depth to 2, to avoid potential stack overflows in kernel.
--- include/linux/btf.h | 5 +++ kernel/bpf/btf.c | 105 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 1 deletion(-)
include/linux/btf.h +5 -0
@@ -327,6 +327,11 @@ static inline const struct btf_var_secinfo *btf_type_var_secinfo(return (const struct btf_var_secinfo *)(t +1 ); } +static inline struct btf_param *btf_params (const struct btf_type *t) +{ +return (struct btf_param *)(t +1 ); +} +#ifdef CONFIG_BPF_SYSCALL struct bpf_prog ;
kernel/bpf/btf.c +104 -1
114 lines not on this page · raw message
Signature · 1 line
2.34.1
PATCH v3 2/2 selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()
In reply to Matteo CroceFrom: Matteo Croce <[email protected]>
bpf_core_types_are_compat() was limited to 2 recursion levels, which are enough to parse a function prototype.
Add a test which checks the existence of a function prototype, so to test the bpf_core_types_are_compat() code path.
The test for the recursion limit being hit is done in a separate object, because the kernel failure makes the whole load to fail.
Sample run log with extra prints:
[ 5689.913751] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.913902] bpf_core_types_are_compat:6896: ret: 1 [ 5689.913994] bpf_core_types_are_compat:6896: ret: 0 [ 5689.914025] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.914141] bpf_core_types_are_compat:6896: ret: 0 [ 5689.914246] bpf_core_types_are_compat:6896: ret: 0 test_core_kern_lskel:PASS:open_and_load 0 nsec test_core_kern_lskel:PASS:attach(core_relo_proto) 0 nsec test_core_kern_lskel:PASS:bpf_core_type_exists 0 nsec test_core_kern_lskel:PASS:!bpf_core_type_exists 0 nsec #41 core_kern_lskel:OK [ 5689.915267] bpf_core_apply_relo_insn:1200 cands->len: 2 [ 5689.915399] bpf_core_types_are_compat:6896: ret: 0 [ 5689.915504] bpf_core_types_are_compat:6896: ret: -22 test_core_kern_overflow_lskel:PASS:open_and_load 0 nsec #42 core_kern_overflow_lskel:OK Summary: 2/0 PASSED, 0 SKIPPED, 0 FAILED Successfully unloaded bpf_testmod.ko.
--- tools/testing/selftests/bpf/Makefile | 2 +- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 5 +++++ .../selftests/bpf/prog_tests/core_kern.c | 15 ++++++++++++- .../bpf/prog_tests/core_kern_overflow.c | 13 ++++++++++++ tools/testing/selftests/bpf/progs/core_kern.c | 14 +++++++++++++ .../selftests/bpf/progs/core_kern_overflow.c | 21 +++++++++++++++++++ 6 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/core_kern_overflow.c create mode 100644 tools/testing/selftests/bpf/progs/core_kern_overflow.c
tools/testing/selftests/bpf/Makefile +1 -1
@@ -330,7 +330,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h \ LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \ test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \ - map_ptr_kern.c core_kern.c + map_ptr_kern.c core_kern.c core_kern_overflow.c # Generate both light skeleton and libbpf skeleton for these LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c SKEL_BLACKLIST += $$(LSKELS)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +5 -0
@@ -13,6 +13,11 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match ); +func_proto_typedef___match funcp =NULL ; +func_proto_typedef___overflow funcp_of =NULL ; +DEFINE_PER_CPU (int, bpf_testmod_ksym_percpu) =123 ;noinline void
tools/testing/selftests/bpf/prog_tests/core_kern.c +14 -1
@@ -7,8 +7,21 @@void test_core_kern_lskel (void ) {struct core_kern_lskel *skel; +int link_fd; skel =core_kern_lskel__open_and_load (); -ASSERT_OK_PTR (skel,"open_and_load" ); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + link_fd =core_kern_lskel__core_relo_proto__attach (skel); +if (!ASSERT_GT (link_fd,0 ,"attach(core_relo_proto)" )) + goto cleanup; + +/* trigger tracepoints */ +usleep (1 ); +ASSERT_TRUE (skel->bss->proto_out[0 ],"bpf_core_type_exists" ); +ASSERT_FALSE (skel->bss->proto_out[1 ],"!bpf_core_type_exists" ); + +cleanup:core_kern_lskel__destroy (skel); }
tools/testing/selftests/bpf/prog_tests/core_kern_overflow.c new file +13 -0
@@ -0,0 +1,13 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include "test_progs.h" +#include "core_kern_overflow.lskel.h" + +void test_core_kern_overflow_lskel (void ) +{ +struct core_kern_overflow_lskel *skel; + + skel =core_kern_overflow_lskel__open_and_load (); +if (!ASSERT_NULL (skel,"open_and_load" )) +core_kern_overflow_lskel__destroy (skel); +}
tools/testing/selftests/bpf/progs/core_kern.c +14 -0
@@ -101,4 +101,18 @@ int balancer_ingress(struct __sk_buff *ctx)return 0 ; } +typedef int (*func_proto_typedef___match )(long ); +typedef void (*func_proto_typedef___doesnt_match )(char *); + +int proto_out[2 ]; + +SEC("raw_tracepoint/sys_enter") +int core_relo_proto (void *ctx) +{ + proto_out[0 ] =bpf_core_type_exists (func_proto_typedef___match); + proto_out[1 ] =bpf_core_type_exists (func_proto_typedef___doesnt_match); + +return 0 ; +} + char LICENSE[] SEC("license") = "GPL";
tools/testing/selftests/bpf/progs/core_kern_overflow.c new file +21 -0
@@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +#include "vmlinux.h" + +#include <bpf/bpf_helpers.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_core_read.h> + +typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match ); + +int proto_out; + +SEC("raw_tracepoint/sys_enter") +int core_relo_proto (void *ctx) +{ + proto_out =bpf_core_type_exists (func_proto_typedef___overflow); + +return 0 ; +} + +char LICENSE[] SEC("license") = "GPL";
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v3 1/2] bpf: limit bpf_core_types_are_compat() recursion
In reply to Matteo Crocekernel/bpf/btf.c int register_btf_kfunc_id_set(enum bpf_prog_type prog_type, · line 6849
Quoted 4 lines · Matteo Croce
kernel/bpf/btf.c +4
-0+ +for (i =0 ; i < local_vlen; i++, local_p++, targ_p++) { +if (level <=1 ) +return -EINVAL ;
<= 1 is not correct, since it makes MAX_TYPES_ARE_COMPAT_DEPTH 2
misleading.
With <= 1 the recursion depth is just 1.
So I've changed to to <=0 to make the actual depth of 2.
Re: [PATCH bpf-next v3 2/2] selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()
In reply to Matteo Crocetools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 17
Quoted 8 lines · Matteo Croce
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0@@ -13,6 +13,11 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match );
There is no need for "___flavor" on the kernel side of type definition. It makes the test confusing to read.
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 19
Quoted 2 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0+func_proto_typedef___match funcp =NULL ; +func_proto_typedef___overflow funcp_of =NULL ;
We have BTF_TYPE_EMIT() macro to avoid unnecessary declaration.
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 21
Quoted 2 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0+typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match );
With <=1 in the previous patch such single depth of func_proto was reaching the recursion limit.
Hence the fix <=0 was necessary.
I've also changed this test to:
+typedef int (*func_proto_typedef)(long);
+typedef int (*func_proto_typedef_nested1)(func_proto_typedef); +typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);
in bpf_testmod.c and in progs/core_kern_overflow.c and bpf_core_type_exists(func_proto_typedef_nested2); to go above the limit.
Also added bpf_core_type_exists(func_proto_typedef_nested1) to progs/core_kern.c to stay at the limit.
Please see the result in bpf-next.
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 10 lines · On Fri, 4 Feb 2022 01:55:17 +0100 you wrote:
From: Matteo Croce <[email protected]>
As formerly discussed on the BPF mailing list:
https://lore.kernel.org/bpf/CAADnVQJDax2j0-7uyqdqFEnpB57om_z+Cqmi1O2QyLpHqkVKwA@mail.gmail.com/changes from v2:
test thebpf_core_type_exists()return value, and check that the recursion limit is enforced.[...]
Here is the summary with links:
- [bpf-next,v3,1/2] bpf: limit bpf_core_types_are_compat() recursion
https://git.kernel.org/bpf/bpf-next/c/e70e13e7d4ab - [bpf-next,v3,2/2] selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat() https://git.kernel.org/bpf/bpf-next/c/976a38e05a49
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html
Re: [PATCH bpf-next v3 2/2] selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()
In reply to Alexei StarovoitovQuoted 36 lines · Alexei Starovoitov
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 17
Quoted 8 lines · Matteo Croce
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0@@ -13,6 +13,11 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match );There is no need for "
___flavor" on the kernel side of type definition. It makes the test confusing to read.tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 19
Quoted 2 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0+func_proto_typedef___match funcp =NULL ; +func_proto_typedef___overflow funcp_of =NULL ;We have
BTF_TYPE_EMIT()macro to avoid unnecessary declaration.tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c line 21
Quoted 2 lines
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +2
-0+typedef int (*func_proto_typedef___match )(long ); +typedef int (*func_proto_typedef___overflow )(func_proto_typedef___match );With <=1 in the previous patch such single depth of
func_protowas reaching the recursion limit.
Hence the fix <=0 was necessary.
I've also changed this test to:(quoted diff) +3
-0+typedef int (*func_proto_typedef)(long); +typedef int (*func_proto_typedef_nested1)(func_proto_typedef); +typedef int (*func_proto_typedef_nested2)(func_proto_typedef_nested1);in
bpf_testmod.cand inprogs/core_kern_overflow.candbpf_core_type_exists(func_proto_typedef_nested2); to go above the limit.
Quoted 4 lines
Also added
bpf_core_type_exists(func_proto_typedef_nested1)toprogs/core_kern.cto stay at the limit.Please see the result in bpf-next.
Awesome.
I've seen both patches in the repo, LGTM.
Signature · 1 line
per aspera ad upstream