selftests/bpf: add anonymous user struct as global subprog arg test
authorAndrii Nakryiko <andrii@kernel.org>
Mon, 12 Feb 2024 23:32:21 +0000 (15:32 -0800)
committerAlexei Starovoitov <ast@kernel.org>
Wed, 14 Feb 2024 02:46:47 +0000 (18:46 -0800)
Add tests validating that kernel handles pointer to anonymous struct
argument as PTR_TO_MEM case, not as PTR_TO_CTX case.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/r/20240212233221.2575350-5-andrii@kernel.org
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c

index 67dddd9418911cb1ce3db26132fab340e93aab4d..baff5ffe940516e5d482bda26a0029eb5a582751 100644 (file)
@@ -115,6 +115,35 @@ int arg_tag_nullable_ptr_fail(void *ctx)
        return subprog_nullable_ptr_bad(&x);
 }
 
+typedef struct {
+       int x;
+} user_struct_t;
+
+__noinline __weak int subprog_user_anon_mem(user_struct_t *t)
+{
+       return t ? t->x : 0;
+}
+
+SEC("?tracepoint")
+__failure __log_level(2)
+__msg("invalid bpf_context access")
+__msg("Caller passes invalid args into func#1 ('subprog_user_anon_mem')")
+int anon_user_mem_invalid(void *ctx)
+{
+       /* can't pass PTR_TO_CTX as user memory */
+       return subprog_user_anon_mem(ctx);
+}
+
+SEC("?tracepoint")
+__success __log_level(2)
+__msg("Func#1 ('subprog_user_anon_mem') is safe for any args that match its prototype")
+int anon_user_mem_valid(void *ctx)
+{
+       user_struct_t t = { .x = 42 };
+
+       return subprog_user_anon_mem(&t);
+}
+
 __noinline __weak int subprog_nonnull_ptr_good(int *p1 __arg_nonnull, int *p2 __arg_nonnull)
 {
        return (*p1) * (*p2); /* good, no need for NULL checks */