Skip to contentSkip to conversation

limit bpf_core_types_are_compat recursion

PATCH Superseded Matteo Croce 2 participants 4 messages Feb 2, 2022 – Feb 3, 2022

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index f6c43dd513fa..36bc09b8e890 100644
@@ -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
#2 lore raw

PATCH v2 2/2 selftests/bpf: test maximum recursion depth for bpf_core_types_are_compat()

In reply to 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 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
index 595d32ab285a..a457071a7751 100644
@@ -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
index 13499cc15c7d..bfea86b42563 100644
@@ -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
#3 lore raw

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 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
index 595d32ab285a..a457071a7751 100644
@@ -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 +0 -0
index 13499cc15c7d..bfea86b42563 100644
@@ -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.