selftests/bpf: Test PTR_MAYBE_NULL arguments of struct_ops operators.
authorKui-Feng Lee <thinker.li@gmail.com>
Fri, 9 Feb 2024 02:37:50 +0000 (18:37 -0800)
committerMartin KaFai Lau <martin.lau@kernel.org>
Tue, 13 Feb 2024 23:16:44 +0000 (15:16 -0800)
Test if the verifier verifies nullable pointer arguments correctly for BPF
struct_ops programs.

"test_maybe_null" in struct bpf_testmod_ops is the operator defined for the
test cases here.

A BPF program should check a pointer for NULL beforehand to access the
value pointed by the nullable pointer arguments, or the verifier should
reject the programs. The test here includes two parts; the programs
checking pointers properly and the programs not checking pointers
beforehand. The test checks if the verifier accepts the programs checking
properly and rejects the programs not checking at all.

Signed-off-by: Kui-Feng Lee <thinker.li@gmail.com>
Link: https://lore.kernel.org/r/20240209023750.1153905-5-thinker.li@gmail.com
Signed-off-by: Martin KaFai Lau <martin.lau@kernel.org>
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h
tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c [new file with mode: 0644]
tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c [new file with mode: 0644]

index a06daebc75c9bb5035fe7e585b6773a788a86d58..66787e99ba1b075aa415c0e4a5957e21d7d8e9e8 100644 (file)
@@ -555,7 +555,11 @@ static int bpf_dummy_reg(void *kdata)
 {
        struct bpf_testmod_ops *ops = kdata;
 
-       ops->test_2(4, 3);
+       /* Some test cases (ex. struct_ops_maybe_null) may not have test_2
+        * initialized, so we need to check for NULL.
+        */
+       if (ops->test_2)
+               ops->test_2(4, 3);
 
        return 0;
 }
@@ -573,9 +577,16 @@ static void bpf_testmod_test_2(int a, int b)
 {
 }
 
+static int bpf_testmod_ops__test_maybe_null(int dummy,
+                                           struct task_struct *task__nullable)
+{
+       return 0;
+}
+
 static struct bpf_testmod_ops __bpf_testmod_ops = {
        .test_1 = bpf_testmod_test_1,
        .test_2 = bpf_testmod_test_2,
+       .test_maybe_null = bpf_testmod_ops__test_maybe_null,
 };
 
 struct bpf_struct_ops bpf_bpf_testmod_ops = {
index 537beca42896a2cafdc3ccd19aa70cb6439b6468..c3b0cf788f9f734695606d554ca27613c875b9c1 100644 (file)
@@ -5,6 +5,8 @@
 
 #include <linux/types.h>
 
+struct task_struct;
+
 struct bpf_testmod_test_read_ctx {
        char *buf;
        loff_t off;
@@ -31,6 +33,8 @@ struct bpf_iter_testmod_seq {
 struct bpf_testmod_ops {
        int (*test_1)(void);
        void (*test_2)(int a, int b);
+       /* Used to test nullable arguments. */
+       int (*test_maybe_null)(int dummy, struct task_struct *task);
 };
 
 #endif /* _BPF_TESTMOD_H */
diff --git a/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c b/tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c
new file mode 100644 (file)
index 0000000..01dc261
--- /dev/null
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+
+#include "struct_ops_maybe_null.skel.h"
+#include "struct_ops_maybe_null_fail.skel.h"
+
+/* Test that the verifier accepts a program that access a nullable pointer
+ * with a proper check.
+ */
+static void maybe_null(void)
+{
+       struct struct_ops_maybe_null *skel;
+
+       skel = struct_ops_maybe_null__open_and_load();
+       if (!ASSERT_OK_PTR(skel, "struct_ops_module_open_and_load"))
+               return;
+
+       struct_ops_maybe_null__destroy(skel);
+}
+
+/* Test that the verifier rejects a program that access a nullable pointer
+ * without a check beforehand.
+ */
+static void maybe_null_fail(void)
+{
+       struct struct_ops_maybe_null_fail *skel;
+
+       skel = struct_ops_maybe_null_fail__open_and_load();
+       if (ASSERT_ERR_PTR(skel, "struct_ops_module_fail__open_and_load"))
+               return;
+
+       struct_ops_maybe_null_fail__destroy(skel);
+}
+
+void test_struct_ops_maybe_null(void)
+{
+       /* The verifier verifies the programs at load time, so testing both
+        * programs in the same compile-unit is complicated. We run them in
+        * separate objects to simplify the testing.
+        */
+       if (test__start_subtest("maybe_null"))
+               maybe_null();
+       if (test__start_subtest("maybe_null_fail"))
+               maybe_null_fail();
+}
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c
new file mode 100644 (file)
index 0000000..b450f72
--- /dev/null
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+pid_t tgid = 0;
+
+/* This is a test BPF program that uses struct_ops to access an argument
+ * that may be NULL. This is a test for the verifier to ensure that it can
+ * rip PTR_MAYBE_NULL correctly.
+ */
+SEC("struct_ops/test_maybe_null")
+int BPF_PROG(test_maybe_null, int dummy,
+            struct task_struct *task)
+{
+       if (task)
+               tgid = task->tgid;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+       .test_maybe_null = (void *)test_maybe_null,
+};
+
diff --git a/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c b/tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c
new file mode 100644 (file)
index 0000000..6283099
--- /dev/null
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_tracing.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+pid_t tgid = 0;
+
+SEC("struct_ops/test_maybe_null_struct_ptr")
+int BPF_PROG(test_maybe_null_struct_ptr, int dummy,
+            struct task_struct *task)
+{
+       tgid = task->tgid;
+
+       return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_struct_ptr = {
+       .test_maybe_null = (void *)test_maybe_null_struct_ptr,
+};
+