Skip to contentSkip to conversation

bpf, selftests/bpf: Support PTR_MAYBE_NULL for struct_ops arguments.

RFC Superseded [email protected] 5 participants 8 messages Jan 22, 2024 – Jan 27, 2024

Subsystems (5)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

RFC v3 1/1 bpf, selftests/bpf: Support PTR_MAYBE_NULL for struct_ops arguments.

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

  {
          return 0;

}

This means that the argument 1 (2nd) of bpf_testmod_ops->test_maybe_null, which is a function pointer that can be null. With this annotation, the verifier will understand how to check programs using this arguments. A BPF program that implement test_maybe_null should check the pointer to make sure it is not null before using it. For example,

if (task__nullable)

      save_tgid = task__nullable->tgid

Without the check, the verifier will reject the program.

Since we already has stub functions for kCFI, we just reuse these stub functions with the naming convention mentioned earlier. These stub functions with the naming convention is only required if there are nullable arguments to annotate. For functions without nullable arguments, stub functions are not necessary for the purpose of this patch.

---

Major changes from v2:

- Remove dead code.

- Add comments to explain the code itself.

Major changes from v1:

- Annotate arguments by suffixing argument names with "__nullable" at stub functions.

v2: https://lore.kernel.org/all/[email protected]/

---
 include/linux/bpf.h                           |   8 +
 kernel/bpf/btf.c                              | 171 +++++++++++++++++-
 kernel/bpf/verifier.c                         |   5 +-
 .../selftests/bpf/bpf_testmod/bpf_testmod.c   |  27 ++-
 .../selftests/bpf/bpf_testmod/bpf_testmod.h   |   4 +
 .../prog_tests/test_struct_ops_maybe_null.c   |  37 ++++
 .../bpf/progs/struct_ops_maybe_null.c         |  27 +++
 .../bpf/progs/struct_ops_maybe_null_fail.c    |  25 +++
 8 files changed, 297 insertions(+), 7 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c
 create mode 100644 tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c
include/linux/bpf.h +8 -0
index 085070ea3021..bdaf6936f091 100644
@@ -1413,6 +1413,7 @@ struct bpf_ctx_arg_aux {
 	u32 offset;
 	enum bpf_reg_type reg_type;
 	u32 btf_id;
+	struct btf *btf;
 };
 
 struct btf_mod_pair {
@@ -1679,6 +1680,11 @@ struct bpf_struct_ops {
 	struct btf_func_model func_models[BPF_STRUCT_OPS_MAX_NR_MEMBERS];
 };
 
+struct bpf_struct_ops_member_arg_info {
+	struct bpf_ctx_arg_aux *arg_info;
+	u32 arg_info_cnt;
+};
+
 struct bpf_struct_ops_desc {
 	struct bpf_struct_ops *st_ops;
 
@@ -1686,6 +1692,8 @@ struct bpf_struct_ops_desc {
 	const struct btf_type *value_type;
 	u32 type_id;
 	u32 value_id;
+
+	struct bpf_struct_ops_member_arg_info *member_arg_info;
 };
 
 enum bpf_struct_ops_state {
kernel/bpf/btf.c +170 -1

195 lines not on this page · raw message

kernel/bpf/verifier.c +4 -1
index 0fc998f3ce86..3920eaafff5d 100644
@@ -20231,8 +20231,8 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 	const struct btf_type *t, *func_proto;
 	const struct bpf_struct_ops_desc *st_ops_desc;
 	const struct bpf_struct_ops *st_ops;
-	const struct btf_member *member;
 	struct bpf_prog *prog = env->prog;
+	const struct btf_member *member;
 	u32 btf_id, member_idx;
 	struct btf *btf;
 	const char *mname;
@@ -20290,6 +20290,9 @@ static int check_struct_ops_btf_id(struct bpf_verifier_env *env)
 		}
 	}
 
+	prog->aux->ctx_arg_info = st_ops_desc->member_arg_info[member_idx].arg_info;
+	prog->aux->ctx_arg_info_size = st_ops_desc->member_arg_info[member_idx].arg_info_cnt;
+
 	prog->aux->attach_func_proto = func_proto;
 	prog->aux->attach_func_name = mname;
 	env->ops = st_ops->verifier_ops;
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +22 -5
index 9a7949730137..caaba7b2391c 100644
@@ -555,7 +555,12 @@ static int bpf_dummy_reg(void *kdata)
 	struct bpf_testmod_ops *ops = kdata;
 	int r;
 
-	r = ops->test_2(4, 3);
+	if (ops->test_maybe_null)
+		r = ops->test_maybe_null(0, NULL);
+	else if (ops->test_non_maybe_null)
+		r = ops->test_non_maybe_null(0, NULL);
+	else
+		r = ops->test_2(4, 3);
 
 	return 0;
 }
@@ -564,19 +569,31 @@ static void bpf_dummy_unreg(void *kdata)
 {
 }
 
-static int bpf_testmod_test_1(void)
+static int bpf_testmod_ops_stub_test_1(void)
 {
 	return 0;
 }
 
-static int bpf_testmod_test_2(int a, int b)
+static int bpf_testmod_ops_stub_test_2(int a, int b)
+{
+	return 0;
+}
+
+static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct task_struct *task__nullable)
+{
+	return 0;
+}
+
+static int bpf_testmod_ops_stub_test_non_maybe_null(int dummy, struct task_struct *task)
 {
 	return 0;
 }
 
 static struct bpf_testmod_ops __bpf_testmod_ops = {
-	.test_1 = bpf_testmod_test_1,
-	.test_2 = bpf_testmod_test_2,
+	.test_1 = bpf_testmod_ops_stub_test_1,
+	.test_2 = bpf_testmod_ops_stub_test_2,
+	.test_maybe_null = bpf_testmod_ops_stub_test_maybe_null,
+	.test_non_maybe_null = bpf_testmod_ops_stub_test_non_maybe_null,
 };
 
 struct bpf_struct_ops bpf_bpf_testmod_ops = {
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.h +4 -0
index ca5435751c79..846b472e4810 100644
@@ -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);
 	int (*test_2)(int a, int b);
+	int (*test_maybe_null)(int dummy, struct task_struct *task);
+	int (*test_non_maybe_null)(int dummy, struct task_struct *task);
 };
 
 #endif /* _BPF_TESTMOD_H */
tools/testing/selftests/bpf/prog_tests/test_struct_ops_maybe_null.c new file +37 -0
new file mode 100644
index 000000000000..19065c4d1868
@@ -0,0 +1,37 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <test_progs.h>
+#include <time.h>
+
+#include "struct_ops_maybe_null.skel.h"
+#include "struct_ops_maybe_null_fail.skel.h"
+
+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);
+}
+
+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)
+{
+	if (test__start_subtest("maybe_null"))
+		maybe_null();
+	if (test__start_subtest("maybe_null_fail"))
+		maybe_null_fail();
+}
tools/testing/selftests/bpf/progs/struct_ops_maybe_null.c new file +27 -0
new file mode 100644
index 000000000000..adbbb17865fb
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+u64 tgid = 0;
+
+SEC("struct_ops/test_maybe_null")
+int BPF_PROG(test_maybe_null, int dummy, struct task_struct *task)
+{
+	if (task == NULL)
+		tgid = 0;
+	else
+		tgid = task->tgid;
+
+	return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+	.test_maybe_null = (void *)test_maybe_null,
+};
+
tools/testing/selftests/bpf/progs/struct_ops_maybe_null_fail.c new file +25 -0
new file mode 100644
index 000000000000..2f7a9b6ef864
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2023 Meta Platforms, Inc. and affiliates. */
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "../bpf_testmod/bpf_testmod.h"
+
+char _license[] SEC("license") = "GPL";
+
+int tgid = 0;
+
+SEC("struct_ops/test_maybe_null")
+int BPF_PROG(test_maybe_null, int dummy, struct task_struct *task)
+{
+	tgid = task->tgid;
+
+	return 0;
+}
+
+SEC(".struct_ops.link")
+struct bpf_testmod_ops testmod_1 = {
+	.test_maybe_null = (void *)test_maybe_null,
+};
+
+
Signature · 1 line
2.34.1
#1 lore raw
Quoted 37 lines · On 1/22/24 1:22 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

Quoted 8 lines

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Neat idea to reuse the cfi stub. Some high level comments.

bpf_struct_ops_desc_init is also collecting the details of each func_proto member. Check if this "__nullable" collection can be done in the same loop.

Simplify the implementation of the member_arg_info allocations. There is no need to compact everything in one continuous memory.

#2 lore raw
Quoted 47 lines · Martin KaFai Lau
Quoted 37 lines · On 1/22/24 1:22 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

Quoted 8 lines

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function.  Here is the example in bpf_testmod.c.

Quoted 3 lines

Neat idea to reuse the cfi stub. Some high level comments.

bpf_struct_ops_desc_init is also collecting the details of each func_proto member. Check if this "__nullable" collection can be done in the same loop.

It is a good idea.

Quoted 3 lines

Simplify the implementation of the member_arg_info allocations. There is no need to compact everything in one continuous memory.

Sure

#3 lore raw
In reply to [email protected]
Quoted 44 lines · On Mon, Jan 22, 2024 at 1:22 PM <[email protected]> wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Quoted 2 lines

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

let's keep this consistent with __arg_nullable/__arg_maybe_null? ([0]) I'd very much prefer __arg_nullable and __nullable vs __arg_maybe_null/__maybe_null, but Alexei didn't like the naming when I posted v1.

But in any case, I think it helps to keep similar concepts named similarly, right?

[0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Quoted 23 lines
  {
          return 0;
  }

This means that the argument 1 (2nd) of bpf_testmod_ops->test_maybe_null, which is a function pointer that can be null. With this annotation, the verifier will understand how to check programs using this arguments. A BPF program that implement test_maybe_null should check the pointer to make sure it is not null before using it. For example,

if (task__nullable)

      save_tgid = task__nullable->tgid

Without the check, the verifier will reject the program.

Since we already has stub functions for kCFI, we just reuse these stub functions with the naming convention mentioned earlier. These stub functions with the naming convention is only required if there are nullable arguments to annotate. For functions without nullable arguments, stub functions are not necessary for the purpose of this patch.

---

[...]

#4 lore raw
Quoted 46 lines · Andrii Nakryiko
Quoted 44 lines · On Mon, Jan 22, 2024 at 1:22 PM <[email protected]> wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Quoted 7 lines
Quoted 2 lines

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

let's keep this consistent with __arg_nullable/__arg_maybe_null? ([0]) I'd very much prefer __arg_nullable and __nullable vs __arg_maybe_null/__maybe_null, but Alexei didn't like the naming when I posted v1.

fwiw I'm aware that _Nullable is a standard and it's supported by clang,etc. If folks insist on such suffix, I can live with that.
But I absolutely don't want that to be a reason to rename PTR_MAYBE_NULL in the verifier.
My preference is for consistency in the verifier and suffixes. Hence __maybe_null.
But I'm ok if we do __nullable and keep PTR_MAYBE_NULL.

#5 lore raw
Quoted 57 lines · Alexei Starovoitov
Quoted 46 lines · Andrii Nakryiko
Quoted 44 lines · On Mon, Jan 22, 2024 at 1:22 PM <[email protected]> wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Quoted 7 lines
Quoted 2 lines

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

let's keep this consistent with __arg_nullable/__arg_maybe_null? ([0]) I'd very much prefer __arg_nullable and __nullable vs __arg_maybe_null/__maybe_null, but Alexei didn't like the naming when I posted v1.

Quoted 7 lines

fwiw I'm aware that _Nullable is a standard and it's supported by clang,etc. If folks insist on such suffix, I can live with that.
But I absolutely don't want that to be a reason to rename PTR_MAYBE_NULL in the verifier.
My preference is for consistency in the verifier and suffixes. Hence __maybe_null.
But I'm ok if we do __nullable and keep PTR_MAYBE_NULL.

+1 for that. That is the internal name, and we also have an internal xxx_OR_NULL convention. We don't need to change any of that, IMO.

#6 lore raw
In reply to Andrii Nakryiko
Quoted 53 lines · Andrii Nakryiko
Quoted 44 lines · On Mon, Jan 22, 2024 at 1:22 PM <[email protected]> wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Quoted 2 lines

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

let's keep this consistent with __arg_nullable/__arg_maybe_null? ([0]) I'd very much prefer __arg_nullable and __nullable vs __arg_maybe_null/__maybe_null, but Alexei didn't like the naming when I posted v1.

Quoted 4 lines

But in any case, I think it helps to keep similar concepts named similarly, right?

   [0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Let me paraphrase it. "__arg_maybe_null" is prefered for the case here.

Quoted 26 lines
Quoted 24 lines
   {
           return 0;
   }

This means that the argument 1 (2nd) of bpf_testmod_ops->test_maybe_null, which is a function pointer that can be null. With this annotation, the verifier will understand how to check programs using this arguments. A BPF program that implement test_maybe_null should check the pointer to make sure it is not null before using it. For example,

   if (task__nullable)
       save_tgid = task__nullable->tgid

Without the check, the verifier will reject the program.

Since we already has stub functions for kCFI, we just reuse these stub functions with the naming convention mentioned earlier. These stub functions with the naming convention is only required if there are nullable arguments to annotate. For functions without nullable arguments, stub functions are not necessary for the purpose of this patch.

---

[...]

#7 lore raw
Quoted 57 lines · Kui-Feng Lee
Quoted 53 lines · Andrii Nakryiko
Quoted 44 lines · On Mon, Jan 22, 2024 at 1:22 PM <[email protected]> wrote:

From: Kui-Feng Lee <[email protected]>

Allow passing a null pointer to the operators provided by a struct_ops object. This is an RFC to collect feedbacks/opinions.

The previous discussions against v1 came to the conclusion that the developer should did it in ".is_valid_access". However, recently, kCFI for struct_ops has been landed. We found it is possible to provide a generic way to annotate arguments by adding a suffix after argument names of stub functions. So, this RFC is resent to present the new idea.

The function pointers that are passed to struct_ops operators (the function pointers) are always considered reliable until now. They cannot be null. However, in certain scenarios, it should be possible to pass null pointers to these operators. For instance, sched_ext may pass a null pointer in the struct task type to an operator that is provided by its struct_ops objects.

The proposed solution here is to add PTR_MAYBE_NULL annotations to arguments and create instances of struct bpf_ctx_arg_aux (arg_info) for these arguments. These arg_infos will be installed at prog->aux->ctx_arg_info and will be checked by the BPF verifier when loading the programs. When a struct_ops program accesses arguments in the ctx, the verifier will call btf_ctx_access() (through bpf_verifier_ops->is_valid_access) to verify the access. btf_ctx_access() will check arg_info and use the information of the matched arg_info to properly set reg_type.

For nullable arguments, this patch sets an arg_info to label them with PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL. This enforces the verifier to check programs and ensure that they properly check the pointer. The programs should check if the pointer is null before reading/writing the pointed memory.

The implementer of a struct_ops should annotate the arguments that can be null. The implementer should define a stub function (empty) as a placeholder for each defined operator. The name of a stub function should be in the pattern "<st_op_type>_stub_<operator name>". For example, for test_maybe_null of struct bpf_testmod_ops, it's stub function name should be "bpf_testmod_ops_stub_test_maybe_null". You mark an argument nullable by suffixing the argument name with "__nullable" at the stub function. Here is the example in bpf_testmod.c.

Quoted 2 lines

static int bpf_testmod_ops_stub_test_maybe_null(int dummy, struct
task_struct *task__nullable)

let's keep this consistent with __arg_nullable/__arg_maybe_null? ([0]) I'd very much prefer __arg_nullable and __nullable vs __arg_maybe_null/__maybe_null, but Alexei didn't like the naming when I posted v1.

Quoted 6 lines
Quoted 4 lines

But in any case, I think it helps to keep similar concepts named similarly, right?

   [0] https://patchwork.kernel.org/project/netdevbpf/patch/[email protected]/

Let me paraphrase it. "__arg_maybe_null" is prefered for the case here.

See [0], seems like you can stick to __nullable, and I'll update my patch set with __arg_nullable. User-facing naming will be consistent. Verifier internally will keep using PTR_MAYBE_NULL flag, of course.

[0] https://lore.kernel.org/bpf/CAADnVQKx3RK8pK4xpNEPQKYGUemO0VjdRePdr34fJwHZs6Urag@mail.gmail.com/

Quoted 27 lines
Quoted 27 lines
Quoted 25 lines

   {
           return 0;
   }

This means that the argument 1 (2nd) of bpf_testmod_ops->test_maybe_null, which is a function pointer that can be null. With this annotation, the verifier will understand how to check programs using this arguments. A BPF program that implement test_maybe_null should check the pointer to make sure it is not null before using it. For example,

   if (task__nullable)
       save_tgid = task__nullable->tgid

Without the check, the verifier will reject the program.

Since we already has stub functions for kCFI, we just reuse these stub functions with the naming convention mentioned earlier. These stub functions with the naming convention is only required if there are nullable arguments to annotate. For functions without nullable arguments, stub functions are not necessary for the purpose of this patch.

---

[...]