Skip to contentSkip to conversation

bpf: Reject passing scalar NULL to nonnull arg of a global subprog

PATCH Applied Amery Hung 2 participants 3 messages Jul 23

Subsystems (4)
lore
Share & tools
lore

Amery Hung

author
#0 lore raw

PATCH v2 1/2 bpf: Reject passing scalar NULL to nonnull arg of a global subprog

A global subprogram argument tagged __arg_nonnull is set up as a non-nullable PTR_TO_MEM. However the verifier does not check against a scalar NULL, leading to real NULL pointer dereference. Reject it as well.

Fixes 94e1c70a3452 ("bpf: support 'arg:xxx' btf_decl_tag-based hints for global subprog args")
---
 kernel/bpf/verifier.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/bpf/verifier.c +2 -1
index 99444eae917e..7aa47342dc65 100644
@@ -9189,7 +9189,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,
 				return ret;
 			if (check_mem_reg(env, reg, argno, arg->mem_size))
 				return -EINVAL;
-			if (!(arg->arg_type & PTR_MAYBE_NULL) && (reg->type & PTR_MAYBE_NULL)) {
+			if (!(arg->arg_type & PTR_MAYBE_NULL) &&
+			    (type_may_be_null(reg->type) || bpf_register_is_null(reg))) {
 				bpf_log(log, "%s is expected to be non-NULL\n",
 					reg_arg_name(env, argno));
 				return -EINVAL;
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH v2 2/2 selftests/bpf: Test passing scalar NULL to nonnull global subprog

Make sure the verifier reject passing a hardcoded NULL to an __arg_nonnull argument.

---
 .../selftests/bpf/progs/verifier_global_subprogs.c     | 10 ++++++++++
 1 file changed, 10 insertions(+)
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +10 -0
index 75a2e3f48d0f..67dc352addfd 100644
@@ -185,6 +185,16 @@ int arg_tag_nonnull_ptr_good(void *ctx)
 	return subprog_nonnull_ptr_good(&x, &y);
 }
 
+SEC("?raw_tp")
+__failure __log_level(2)
+__msg("R1 is expected to be non-NULL")
+int arg_tag_nonnull_ptr_null_bad(void *ctx)
+{
+	int y = 74;
+
+	return subprog_nonnull_ptr_good(NULL, &y);
+}
+
 /* this global subprog can be now called from many types of entry progs, each
  * with different context type
  */
Signature · 1 line
2.52.0
#2 lore raw
In reply to Amery Hung
This series was applied to bpf/bpf.git (master) by Eduard Zingerman <[email protected]>:

Hello:

This series was applied to bpf/bpf.git (master)
by Eduard Zingerman <[email protected]>:

Quoted 10 lines · On Thu, 23 Jul 2026 15:18:14 -0700 you wrote:

A global subprogram argument tagged __arg_nonnull is set up as a non-nullable PTR_TO_MEM. However the verifier does not check against a scalar NULL, leading to real NULL pointer dereference. Reject it as well.

Fixes 94e1c70a3452 ("bpf: support 'arg:xxx' btf_decl_tag-based hints for global subprog args")

[...]

Here is the summary with links:
- [bpf,v2,1/2] bpf: Reject passing scalar NULL to nonnull arg of a global subprog

    https://git.kernel.org/bpf/bpf/c/289e680c89ae
  - [bpf,v2,2/2] selftests/bpf: Test passing scalar NULL to nonnull global subprog
    https://git.kernel.org/bpf/bpf/c/55c7bd2ddee5

You are awesome, thank you!

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