Skip to contentSkip to conversation

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

PATCH Superseded Amery Hung 2 participants 4 messages Jul 23

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

PATCH 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 d952bf07368d..498f01045be7 100644
@@ -9331,7 +9331,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, BPF_READ | BPF_WRITE, NULL))
 				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 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  | 13 +++++++++++++
 1 file changed, 13 insertions(+)
tools/testing/selftests/bpf/progs/verifier_global_subprogs.c +13 -0
index 75a2e3f48d0f..71acc087e3c1 100644
@@ -185,6 +185,19 @@ int arg_tag_nonnull_ptr_good(void *ctx)
 	return subprog_nonnull_ptr_good(&x, &y);
 }
 
+/* a hardcoded NULL is a SCALAR_VALUE, not a PTR_MAYBE_NULL, so make sure it
+ * still gets rejected for an __arg_nonnull argument
+ */
+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
maintainer Acked-by on v1 1/2
#2 lore raw
In reply to Amery Hung
Quoted 8 lines · Amery Hung

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")

---

Amery, git am complains about merge conflict when applying on top of the current bpf/master (patch can apply it with fuzz). Because of that CI did not trigger for this patch-set. Could you please rebase and resend?

author
#3 lore raw
Quoted 11 lines · Eduard Zingerman
Quoted 8 lines · Amery Hung

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")

---

Quoted 6 lines

Amery, git am complains about merge conflict when applying on top of the current bpf/master (patch can apply it with fuzz). Because of that CI did not trigger for this patch-set. Could you please rebase and resend?

Okay. I will send a v2 based on bpf/master. It was based on bpf-next/master, which probably explains the conflict.