Skip to contentSkip to conversation

Two small fixes for global subprog tagging

PATCH Applied Andrii Nakryiko 3 participants 6 messages Feb 2, 2024 – Feb 3, 2024

Subsystems (4)
lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

cover Two small fixes for global subprog tagging

Fix a bug with passing trusted PTR_TO_BTF_ID_OR_NULL register into global subprog that expects __arg_trusted __arg_nullable arguments, which was discovered when adopting production BPF application.

Also fix annoying warnings that are irrelevant for static subprogs, which are just an artifact of using btf_prepare_func_args() for both static and global subprogs.

Andrii Nakryiko (3):
bpf: handle trusted PTR_TO_BTF_ID_OR_NULL in argument check logic selftests/bpf: add more cases for __arg_trusted __arg_nullable args
bpf: don't emit warnings intended for global subprogs for static
subprogs

 kernel/bpf/btf.c                              |  6 ++++
 kernel/bpf/verifier.c                         |  1 +
 .../bpf/progs/verifier_global_ptr_args.c      | 32 +++++++++++++++++--
 3 files changed, 36 insertions(+), 3 deletions(-)
Signature · 1 line
2.34.1
author maintainer
#1 lore raw

PATCH 1/3 bpf: handle trusted PTR_TO_BTF_ID_OR_NULL in argument check logic

Add PTR_TRUSTED | PTR_MAYBE_NULL modifiers for PTR_TO_BTF_ID to check_reg_type() to support passing trusted nullable PTR_TO_BTF_ID registers into global functions accepting __arg_trusted __arg_nullable arguments. This hasn't been caught earlier because tests were either passing known non-NULL PTR_TO_BTF_ID registers or known NULL (SCALAR) registers.

When utilizing this functionality in complicated real-world BPF application that passes around PTR_TO_BTF_ID_OR_NULL, it became apparent that verifier rejects valid case because check_reg_type() doesn't handle this case explicitly. Existing check_reg_type() logic is already anticipating this combination, so we just need to explicitly list this combo in the switch statement.

Fixes e2b3c4ff5d18 ("bpf: add __arg_trusted global func arg tag")
---
 kernel/bpf/verifier.c | 1 +
 1 file changed, 1 insertion(+)
kernel/bpf/verifier.c +1 -0
index cd4d780e5400..c6f566d9dc3a 100644
@@ -8234,6 +8234,7 @@ static int check_reg_type(struct bpf_verifier_env *env, u32 regno,
 	switch ((int)reg->type) {
 	case PTR_TO_BTF_ID:
 	case PTR_TO_BTF_ID | PTR_TRUSTED:
+	case PTR_TO_BTF_ID | PTR_TRUSTED | PTR_MAYBE_NULL:
 	case PTR_TO_BTF_ID | MEM_RCU:
 	case PTR_TO_BTF_ID | PTR_MAYBE_NULL:
 	case PTR_TO_BTF_ID | PTR_MAYBE_NULL | MEM_RCU:
Signature · 1 line
2.34.1
author maintainer
#2 lore raw

PATCH 2/3 selftests/bpf: add more cases for __arg_trusted __arg_nullable args

In reply to Andrii Nakryiko

Add extra layer of global functions to ensure that passing around (trusted) PTR_TO_BTF_ID_OR_NULL registers works as expected. We also extend trusted_task_arg_nullable subtest to check three possible valid
argumements: known NULL, known non-NULL, and maybe NULL cases.

---
 .../bpf/progs/verifier_global_ptr_args.c      | 32 +++++++++++++++++--
 1 file changed, 29 insertions(+), 3 deletions(-)
tools/testing/selftests/bpf/progs/verifier_global_ptr_args.c +29 -3
index 484d6262363f..4ab0ef18d7eb 100644
@@ -19,15 +19,41 @@ __weak int subprog_trusted_task_nullable(struct task_struct *task __arg_trusted
 	return task->pid + task->tgid;
 }
 
-SEC("?kprobe")
+__weak int subprog_trusted_task_nullable_extra_layer(struct task_struct *task __arg_trusted __arg_nullable)
+{
+	return subprog_trusted_task_nullable(task) + subprog_trusted_task_nullable(NULL);
+}
+
+SEC("?tp_btf/task_newtask")
 __success __log_level(2)
 __msg("Validating subprog_trusted_task_nullable() func#1...")
 __msg(": R1=trusted_ptr_or_null_task_struct(")
 int trusted_task_arg_nullable(void *ctx)
 {
-	struct task_struct *t = bpf_get_current_task_btf();
+	struct task_struct *t1 = bpf_get_current_task_btf();
+	struct task_struct *t2 = bpf_task_acquire(t1);
+	int res = 0;
 
-	return subprog_trusted_task_nullable(t) + subprog_trusted_task_nullable(NULL);
+	/* known NULL */
+	res += subprog_trusted_task_nullable(NULL);
+
+	/* known non-NULL */
+	res += subprog_trusted_task_nullable(t1);
+	res += subprog_trusted_task_nullable_extra_layer(t1);
+
+	/* unknown if NULL or not */
+	res += subprog_trusted_task_nullable(t2);
+	res += subprog_trusted_task_nullable_extra_layer(t2);
+
+	if (t2) {
+		/* known non-NULL after explicit NULL check, just in case */
+		res += subprog_trusted_task_nullable(t2);
+		res += subprog_trusted_task_nullable_extra_layer(t2);
+
+		bpf_task_release(t2);
+	}
+
+	return res;
 }
 
 __weak int subprog_trusted_task_nonnull(struct task_struct *task __arg_trusted)
Signature · 1 line
2.34.1
author maintainer
#3 lore raw

PATCH 3/3 bpf: don't emit warnings intended for global subprogs for static subprogs

In reply to Andrii Nakryiko

When btf_prepare_func_args() was generalized to handle both static and global subprogs, a few warnings/errors that are meant only for global subprog cases started to be emitted for static subprogs, where they are sort of expected and irrelavant.

Stop polutting verifier logs with irrelevant scary-looking messages.

Fixes e26080d0da87 ("bpf: prepare btf_prepare_func_args() for handling static subprogs")
---
 kernel/bpf/btf.c | 6 ++++++
 1 file changed, 6 insertions(+)
kernel/bpf/btf.c +6 -0
index ef380e546952..f7725cb6e564 100644
@@ -7122,6 +7122,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
 	args = (const struct btf_param *)(t + 1);
 	nargs = btf_type_vlen(t);
 	if (nargs > MAX_BPF_FUNC_REG_ARGS) {
+		if (!is_global)
+			return -EINVAL;
 		bpf_log(log, "Global function %s() with %d > %d args. Buggy compiler.\n",
 			tname, nargs, MAX_BPF_FUNC_REG_ARGS);
 		return -EINVAL;
@@ -7131,6 +7133,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
 	while (btf_type_is_modifier(t))
 		t = btf_type_by_id(btf, t->type);
 	if (!btf_type_is_int(t) && !btf_is_any_enum(t)) {
+		if (!is_global)
+			return -EINVAL;
 		bpf_log(log,
 			"Global function %s() doesn't return scalar. Only those are supported.\n",
 			tname);
@@ -7251,6 +7255,8 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)
 			sub->args[i].arg_type = ARG_ANYTHING;
 			continue;
 		}
+		if (!is_global)
+			return -EINVAL;
 		bpf_log(log, "Arg#%d type %s in %s() is not supported yet.\n",
 			i, btf_type_str(t), tname);
 		return -EINVAL;
Signature · 1 line
2.34.1
maintainer Acked-by on the v1 series
#4 lore raw
In reply to Andrii Nakryiko
Quoted 7 lines · Andrii Nakryiko

Fix a bug with passing trusted PTR_TO_BTF_ID_OR_NULL register into global subprog that expects __arg_trusted __arg_nullable arguments, which was discovered when adopting production BPF application.

Also fix annoying warnings that are irrelevant for static subprogs, which are just an artifact of using btf_prepare_func_args() for both static and global subprogs.

Full patch-set looks good to me.

#5 lore raw
In reply to Andrii Nakryiko
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 9 lines · On Fri, 2 Feb 2024 11:05:26 -0800 you wrote:

Fix a bug with passing trusted PTR_TO_BTF_ID_OR_NULL register into global subprog that expects __arg_trusted __arg_nullable arguments, which was discovered when adopting production BPF application.

Also fix annoying warnings that are irrelevant for static subprogs, which are just an artifact of using btf_prepare_func_args() for both static and global subprogs.

[...]

Here is the summary with links:
- [bpf-next,1/3] bpf: handle trusted PTR_TO_BTF_ID_OR_NULL in argument check logic

    https://git.kernel.org/bpf/bpf-next/c/8f13c34087d3
  - [bpf-next,2/3] selftests/bpf: add more cases for __arg_trusted __arg_nullable args
    https://git.kernel.org/bpf/bpf-next/c/e2e70535dd76
  - [bpf-next,3/3] bpf: don't emit warnings intended for global subprogs for static subprogs
    https://git.kernel.org/bpf/bpf-next/c/1eb986746a67

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html