Skip to contentSkip to conversation

bpf: Emit verbose message when prog-specific btf_struct_access rejects a write

PATCH Applied Alexei Starovoitov 4 participants 4 messages Jun 15 – Jun 22

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Emit verbose message when prog-specific btf_struct_access rejects a write

From: Alexei Starovoitov <[email protected]>

When BPF_WRITE goes through a PTR_TO_BTF_ID register, check_ptr_to_btf_access() delegates to env->ops->btf_struct_access(). Most implementations (bpf_scx_btf_struct_access, tc_cls_act_btf_struct_access, etc.) return -EACCES for disallowed fields without logging anything, so the verifier rejects the program with an empty message. For example a scx program doing

1: R1=trusted_ptr_task_struct()
...
4: (7b) *(u64 *)(r1 +0) = r2
verification time 83 usec
the program is rejected

leaves the user guessing which field is off-limits. Emit verbose message.

---
 kernel/bpf/verifier.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/verifier.c +4 -0
index 2abc79dbf281..030505c8f3e2 100644
@@ -5786,6 +5786,10 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
 			return -EFAULT;
 		}
 		ret = env->ops->btf_struct_access(&env->log, reg, off, size);
+		if (ret < 0)
+			verbose(env,
+				"%s cannot write into ptr_%s at off=%d size=%d\n",
+				reg_arg_name(env, argno), tname, off, size);
 	} else {
 		/* Writes are permitted with default btf_struct_access for
 		 * program allocated objects (which always have id > 0),
Signature · 1 line
2.53.0-Meta
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

When BPF_WRITE goes through a PTR_TO_BTF_ID register, check_ptr_to_btf_access() delegates to env->ops->btf_struct_access(). Most implementations (bpf_scx_btf_struct_access, tc_cls_act_btf_struct_access, etc.) return -EACCES for disallowed fields without logging anything, so the verifier rejects the program with an empty message. For example a scx program doing

1: R1=trusted_ptr_task_struct()
...
4: (7b) *(u64 *)(r1 +0) = r2
verification time 83 usec
the program is rejected

Quoted 4 lines

leaves the user guessing which field is off-limits. Emit verbose message.

Quoted 19 lines
---
 kernel/bpf/verifier.c | 4 ++++
 1 file changed, 4 insertions(+)
kernel/bpf/verifier.c +4 -0
index 2abc79dbf281..030505c8f3e2 100644
@@ -5786,6 +5786,10 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env,
 			return -EFAULT;
 		}
 		ret = env->ops->btf_struct_access(&env->log, reg, off, size);
+		if (ret < 0)
+			verbose(env,
+				"%s cannot write into ptr_%s at off=%d size=%d\n",
+				reg_arg_name(env, argno), tname, off, size);
 	} else {
 		/* Writes are permitted with default btf_struct_access for
 		 * program allocated objects (which always have id > 0),
reviewer Acked-by on v1 1/1
#2 lore raw
In reply to Alexei Starovoitov
Quoted 14 lines · Alexei Starovoitov

From: Alexei Starovoitov <[email protected]>

When BPF_WRITE goes through a PTR_TO_BTF_ID register, check_ptr_to_btf_access() delegates to env->ops->btf_struct_access(). Most implementations (bpf_scx_btf_struct_access, tc_cls_act_btf_struct_access, etc.) return -EACCES for disallowed fields without logging anything, so the verifier rejects the program with an empty message. For example a scx program doing

   1: R1=trusted_ptr_task_struct()
   ...
   4: (7b) *(u64 *)(r1 +0) = r2
   verification time 83 usec
   the program is rejected
Quoted 4 lines

leaves the user guessing which field is off-limits. Emit verbose message.

Indeed, e.g. bpf_scx_btf_struct_access() does not emit any verifier log for failure. So it makes sense to have a generic verifier log after env->ops->btf_struct_access().

#3 lore raw
In reply to Alexei Starovoitov
This patch was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

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

Quoted 9 lines · On Mon, 15 Jun 2026 16:21:46 -0700 you wrote:

From: Alexei Starovoitov <[email protected]>

When BPF_WRITE goes through a PTR_TO_BTF_ID register, check_ptr_to_btf_access() delegates to env->ops->btf_struct_access(). Most implementations (bpf_scx_btf_struct_access, tc_cls_act_btf_struct_access, etc.) return -EACCES for disallowed fields without logging anything, so the verifier rejects the program with an empty message. For example a scx program doing

[...]

Here is the summary with links:
- [bpf-next] bpf: Emit verbose message when prog-specific btf_struct_access rejects a write

    https://git.kernel.org/bpf/bpf/c/a933bade82b9

You are awesome, thank you!

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