Add a linked_list negative loader case for a program-BTF type whose last
member is a zero-length flexible array. The program writes through the
first flexible-array element so an incorrect allocation-time acceptance
would leave the verifier to approve an access outside the allocated object.

Use the explicit _impl kfunc forms in this negative test so the case is
scoped to the allocation type check and not to implicit kfunc wrapper
resolution.

Signed-off-by: Yiyang Chen <[email protected]>
---
 .../selftests/bpf/prog_tests/linked_list.c    |  2 ++
 .../selftests/bpf/progs/linked_list_fail.c    | 23 +++++++++++++++++++
 2 files changed, 25 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/linked_list.c 
b/tools/testing/selftests/bpf/prog_tests/linked_list.c
index 8defea025..49c250132 100644
--- a/tools/testing/selftests/bpf/prog_tests/linked_list.c
+++ b/tools/testing/selftests/bpf/prog_tests/linked_list.c
@@ -68,6 +68,8 @@ static struct {
        { "obj_type_id_oor", "local type ID argument must be in range [0, 
U32_MAX]" },
        { "obj_new_no_composite", "bpf_obj_new/bpf_percpu_obj_new type ID 
argument must be of a struct" },
        { "obj_new_no_struct", "bpf_obj_new/bpf_percpu_obj_new type ID argument 
must be of a struct" },
+       { "obj_new_flex_array",
+         "bpf_obj_new type must not contain a flexible array" },
        { "obj_drop_non_zero_off", "R1 must have zero offset when passed to 
release func" },
        { "new_null_ret", "R0 invalid mem access 'ptr_or_null_'" },
        { "obj_new_acq", "Unreleased reference id=" },
diff --git a/tools/testing/selftests/bpf/progs/linked_list_fail.c 
b/tools/testing/selftests/bpf/progs/linked_list_fail.c
index ddd26d1a0..031e77a28 100644
--- a/tools/testing/selftests/bpf/progs/linked_list_fail.c
+++ b/tools/testing/selftests/bpf/progs/linked_list_fail.c
@@ -167,6 +167,16 @@ CHECK_OP(push_back);
 #undef CHECK_OP
 #undef INIT
 
+struct obj_new_flex_elem {
+       int lo;
+       int hi;
+};
+
+struct obj_new_flex {
+       int hdr;
+       struct obj_new_flex_elem cells[];
+};
+
 SEC("?kprobe/xyz")
 int map_compat_kprobe(void *ctx)
 {
@@ -230,6 +240,19 @@ int obj_new_no_struct(void *ctx)
        return 0;
 }
 
+SEC("?tc")
+int obj_new_flex_array(void *ctx)
+{
+       struct obj_new_flex *p;
+
+       p = bpf_obj_new_impl(bpf_core_type_id_local(struct obj_new_flex), NULL);
+       if (!p)
+               return 0;
+       p->cells[0].hi = 42;
+       bpf_obj_drop_impl(p, NULL);
+       return 0;
+}
+
 SEC("?tc")
 int obj_drop_non_zero_off(void *ctx)
 {
-- 
2.34.1


Reply via email to