limit bpf_core_types_are_compat recursion
Subsystems (4)
v2 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/
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 +++++++++++++++++- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 3 + tools/testing/selftests/bpf/progs/core_kern.c | 14 +++ 4 files changed, 126 insertions(+), 1 deletion(-)
Signature · 1 line
2.34.1
PATCH v2 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 v2 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.
--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 3 +++ tools/testing/selftests/bpf/progs/core_kern.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+)
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3 -0
@@ -13,6 +13,9 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +typedef int (*func_proto_typedef )(long ); +func_proto_typedef funcp =NULL ; +DEFINE_PER_CPU (int, bpf_testmod_ksym_percpu) =123 ;noinline void
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 out[2 ]; + +SEC("raw_tracepoint/sys_enter") +int core_relo_recur_limit (void *ctx) +{ + out[0 ] =bpf_core_type_exists (func_proto_typedef___match); + out[1 ] =bpf_core_type_exists (func_proto_typedef___doesnt_match); + +return 0 ; +} + char LICENSE[] SEC("license") = "GPL";
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v2 2/2] selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()
Quoted 36 lines · Matteo Croce
From: 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 thebpf_core_types_are_compat()code path.Signed-off-by Matteo Croce <[email protected]>--- .../selftests/bpf/bpf_testmod/bpf_testmod.c | 3 +++ tools/testing/selftests/bpf/progs/core_kern.c | 14 ++++++++++++++ 2 files changed, 17 insertions(+)tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +3
-0@@ -13,6 +13,9 @@#define CREATE_TRACE_POINTS #include "bpf_testmod-events.h" +typedef int (*func_proto_typedef )(long ); +func_proto_typedef funcp =NULL ; +DEFINE_PER_CPU (int, bpf_testmod_ksym_percpu) =123 ;noinline voidtools/testing/selftests/bpf/progs/core_kern.c +0
-0@@ -101,4 +101,18 @@ int balancer_ingress(struct __sk_buff *ctx)return 0 ; }
tools/testing/selftests/bpf/progs/core_kern.c int balancer_ingress(struct __sk_buff *ctx) · line 113
Quoted 10 lines
tools/testing/selftests/bpf/progs/core_kern.c +10
-0+typedef int (*func_proto_typedef___match )(long ); +typedef void (*func_proto_typedef___doesnt_match )(char *); + +int out[2 ]; + +SEC("raw_tracepoint/sys_enter") +int core_relo_recur_limit (void *ctx) +{ + out[0 ] =bpf_core_type_exists (func_proto_typedef___match); + out[1 ] =bpf_core_type_exists (func_proto_typedef___doesnt_match);
How does it test it?
The kernel code could be a nop and there will be no failure in this "test". Please make it real.
Also add tests that exercise the limit of recursion.
One that goes over and fails and another that is right at the limit and passes.