Skip to contentSkip to conversation

Unify helper and kfunc call_arg_meta

PATCH Superseded Amery Hung 4 participants 17 messages Jul 9 – Jul 15

Subsystems (4)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Amery Hung

author
#0 lore raw

cover Unify helper and kfunc call_arg_meta

Hi all,

I am working toward unifying helper and kfunc handling in the verifier. Historically, kfunc verification was mostly done separately from helper. This causes code/logic duplication and may introduce subtle bugs as the same checks are implemnented differently.

To address this, I plan to unify function call verification, which will operate on a common function call descriptor bpf_func_proto.

As the first step, this patchset merges bpf_{kfunc_}call_arg_meta of kfunc and helepr. The structure is used as a scratch pad for different helper/kfunc verification mechanisms. The previous object tracking refactor patchset has unified some of them. This patch handles the rest and finally merge the two different structures.

The next step will be generating bpf_func_proto for kfunc before the main verification loop and align kfunc and helper argument types.

Then hopefully helper and kfunc can go throguh the same call verification path.

Amery Hung (4):
bpf: Remove dynptr check in check_stack_range_initialized()
bpf: Factor out raw_mode-related fields in bpf_call_arg_meta
bpf: Unify helper and kfunc allocation-size argument handling
bpf: Unify helper and kfunc call argument meta

 include/linux/bpf_verifier.h                  |  55 ++--
 kernel/bpf/cfg.c                              |   2 +-
 kernel/bpf/verifier.c                         | 253 +++++++-----------
 .../selftests/bpf/prog_tests/kfunc_call.c     |   2 +-
 .../testing/selftests/bpf/progs/dynptr_fail.c |   4 +-
 5 files changed, 146 insertions(+), 170 deletions(-)
Signature · 1 line
2.52.0
author
#1 lore raw

PATCH 1/4 bpf: Remove dynptr check in check_stack_range_initialized()

For a MEM_UNINIT ("raw mode") helper argument, check_stack_range_initialized() open-coded a scan that rejected any STACK_DYNPTR slot in the range with "potential write to dynptr". This duplicated, and was stricter than, the handling that runs when the buffer is actually marked initialized. check_helper_call() later replays the write byte by byte via check_mem_access(), which goes through destroy_if_dynptr_stack_slot(), which rejects overwritting a referenced dynptr. Therefore drop the redundant scan and rely on check_mem_access().

---
 kernel/bpf/verifier.c                         | 25 -------------------
 .../testing/selftests/bpf/progs/dynptr_fail.c |  4 +--
 2 files changed, 2 insertions(+), 27 deletions(-)
kernel/bpf/verifier.c +0 -25
index 03e2202cca13..7dd961ede88d 100644
@@ -6733,31 +6733,6 @@ static int check_stack_range_initialized(
 	}
 
 	if (meta && meta->raw_mode) {
-		/* Ensure we won't be overwriting dynptrs when simulating byte
-		 * by byte access in check_helper_call using meta.access_size.
-		 * This would be a problem if we have a helper in the future
-		 * which takes:
-		 *
-		 *	helper(uninit_mem, len, dynptr)
-		 *
-		 * Now, uninint_mem may overlap with dynptr pointer. Hence, it
-		 * may end up writing to dynptr itself when touching memory from
-		 * arg 1. This can be relaxed on a case by case basis for known
-		 * safe cases, but reject due to the possibilitiy of aliasing by
-		 * default.
-		 */
-		for (i = min_off; i < max_off + access_size; i++) {
-			int stack_off = -i - 1;
-
-			spi = bpf_get_spi(i);
-			/* raw_mode may write past allocated_stack */
-			if (state->allocated_stack <= stack_off)
-				continue;
-			if (state->stack[spi].slot_type[stack_off % BPF_REG_SIZE] == STACK_DYNPTR) {
-				verbose(env, "potential write to dynptr at off=%d disallowed\n", i);
-				return -EACCES;
-			}
-		}
 		meta->access_size = access_size;
 		meta->regno = reg_from_argno(argno);
 		return 0;
tools/testing/selftests/bpf/progs/dynptr_fail.c +2 -2
index 344fb2aa0813..94489ac64da8 100644
@@ -1112,7 +1112,7 @@ int dynptr_overwrite_ref(void *ctx)
 
 /* Reject writes to dynptr slot from bpf_dynptr_read */
 SEC("?raw_tp")
-__failure __msg("potential write to dynptr at off=-16")
+__failure __msg("cannot overwrite referenced dynptr")
 int dynptr_read_into_slot(void *ctx)
 {
 	union {
@@ -1558,7 +1558,7 @@ int BPF_PROG(skb_invalid_ctx_fexit, void *skb)
 
 /* Reject writes to dynptr slot for uninit arg */
 SEC("?raw_tp")
-__failure __msg("potential write to dynptr at off=-16")
+__failure __msg("cannot overwrite referenced dynptr")
 int uninit_write_into_slot(void *ctx)
 {
 	struct {
Signature · 1 line
2.52.0
maintainer Acked-by on v1 1/4
#2 lore raw

Re: [PATCH bpf-next v1 1/4] bpf: Remove dynptr check in check_stack_range_initialized()

Quoted 11 lines · Amery Hung

For a MEM_UNINIT ("raw mode") helper argument, check_stack_range_initialized() open-coded a scan that rejected any STACK_DYNPTR slot in the range with "potential write to dynptr". This duplicated, and was stricter than, the handling that runs when the buffer is actually marked initialized. check_helper_call() later replays the write byte by byte via check_mem_access(), which goes through destroy_if_dynptr_stack_slot(), which rejects overwritting a referenced dynptr. Therefore drop the redundant scan and rely on check_mem_access().

---

[...]

author
#3 lore raw

PATCH 2/4 bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

In reply to Amery Hung

To prepare for unifying the helper and kfunc call_arg_meta, group the scattered MEM_UNINIT ("raw") memory argument fields (raw_mode, regno and access_size) into a new struct arg_raw_mem_desc. The intention is to make it clear about when these are set and used instead of fields with overly generic names.

Identify the raw argument once, up front, in check_raw_mode_ok() ( like check_proto_release_reg() does for release_regno), recording its regno. check_stack_range_initialized() now recognizes the raw buffer by matching that regno, so the separate raw_mode flag is no longer needed.

Also drop the bogus raw_mode assignment in the ARG_PTR_TO_MAP_VALUE case:
arg_type_is_raw_mem() only matches ARG_PTR_TO_MEM | MEM_UNINIT, and a map value pointer is handled by check_map_access(), never reaching check_stack_range_initialized(), so tagging it raw mode had no effect.

No functional change intended. This patch does not enable raw_mode memory access for kfunc (i.e., uninit stack will not be allowed to be passed to kfunc for unprivileged programs). Existing kfuncs with arguments tagged with __uninit are either priviledged or dynptr kfuncs, which take another path to make sure the access is checked by check_mem_access().

---
 include/linux/bpf_verifier.h | 10 ++++++++
 kernel/bpf/verifier.c        | 50 +++++++++++++++---------------------
 2 files changed, 30 insertions(+), 30 deletions(-)
include/linux/bpf_verifier.h +10 -0
index 317e99b9acc0..056b99d26cca 100644
@@ -1464,6 +1464,15 @@ struct ref_obj_desc {
 	u8 cnt;
 };
 
+/*
+ * A memory argument a call fills in. The verifier allows the stack to be uninitialized if
+ * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access().
+ */
+struct arg_raw_mem_desc {
+	u8 regno;
+	int size;
+};
+
 struct bpf_kfunc_call_arg_meta {
 	/* In parameters */
 	struct btf *btf;
@@ -1510,6 +1519,7 @@ struct bpf_kfunc_call_arg_meta {
 	struct bpf_map_desc map;
 	struct bpf_dynptr_desc dynptr;
 	struct ref_obj_desc ref_obj;
+	struct arg_raw_mem_desc arg_raw_mem;
 	u64 mem_size;
 };
 
kernel/bpf/verifier.c +20 -30

108 lines not on this page · raw message

Signature · 1 line
2.52.0
#4 lore raw

Re: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The patch drops the detection of `MEM_UNINIT` raw memory arguments for `ARG_PTR_TO_MAP_VALUE`…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch drops the detection of MEM_UNINIT raw memory arguments for ARG_PTR_TO_MAP_VALUE, which incorrectly breaks valid BPF programs that pass uninitialized stack buffers to helpers like bpf_map_pop_elem and bpf_map_peek_elem. --

--- Patch [2]: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta --- commit bbac2673fbeb11bb9179553dc669702134b2c290
Author: Amery Hung <[email protected]>

bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

To prepare for unifying the helper and kfunc call_arg_meta, group the scattered MEM_UNINIT ("raw") memory argument fields (raw_mode, regno and access_size) into a new struct arg_raw_mem_desc.

Quoted 4 lines

Also drop the bogus raw_mode assignment in the ARG_PTR_TO_MAP_VALUE case:
arg_type_is_raw_mem() only matches ARG_PTR_TO_MEM | MEM_UNINIT, and a map value pointer is handled by check_map_access(), never reaching check_stack_range_initialized(), so tagging it raw mode had no effect.

[Severity: High]
Is this assumption accurate? If a BPF program calls a helper like bpf_map_pop_elem() and passes a stack buffer (PTR_TO_STACK) as the map value buffer, wouldn't check_helper_mem_access() call check_stack_range_initialized() to validate the stack access?

kernel/bpf/verifier.c static int check_stack_range_initialized( · line 6694

Quoted 12 lines
kernel/bpf/verifier.c +2 -0
index 7dd961ede88df..e2a3ed67d2eb5 100644
@@ -6690,6 +6688,8 @@ static int check_stack_range_initialized(
 	 * but BTF based global subprog validation isn't accurate enough.
 	 */
 	bool allow_poison = access_size < 0 || clobber;
+	/* The call will initialize the memory; uninitialized stack allowed */
+	bool raw_mode = meta && meta->arg_raw_mem.regno == reg_from_argno(argno);
 
 	access_size = abs(access_size);

[Severity: High]
Because arg_type_is_raw_mem() only checks ARG_PTR_TO_MEM, arg_raw_mem.regno will not be set for ARG_PTR_TO_MAP_VALUE arguments.

Consequently, raw_mode will evaluate to false here. Does this cause the verifier to fail to bypass the uninitialized read checks, rejecting valid unprivileged programs with an 'invalid read from stack' error?

[ ... ]

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8406

Quoted 8 lines
kernel/bpf/verifier.c +0 -1
@@ -8403,7 +8401,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
 			verifier_bug(env, "invalid map_ptr to access map->value");
 			return -EFAULT;
 		}
-		meta->raw_mode = arg_type & MEM_UNINIT;
 		err = check_helper_mem_access(env, reg, argno_from_reg(regno), meta->map.ptr->value_size,
 					      arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ,
 					      false, meta);

[Severity: High]
Without this MEM_UNINIT assignment for ARG_PTR_TO_MAP_VALUE, does the verifier incorrectly reject valid programs passing uninitialized stack memory?

[ ... ]

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 10298

Quoted 10 lines
kernel/bpf/verifier.c +3 -2
@@ -10303,8 +10292,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 	/* Mark slots with STACK_MISC in case of raw mode, stack offset
 	 * is inferred from register state.
 	 */
-	for (i = 0; i < meta.access_size; i++) {
-		err = check_mem_access(env, insn_idx, regs + meta.regno, argno_from_reg(meta.regno), i, BPF_B,
+	for (i = 0; i < meta.arg_raw_mem.size; i++) {
+		err = check_mem_access(env, insn_idx, regs + meta.arg_raw_mem.regno,
+				       argno_from_reg(meta.arg_raw_mem.regno), i, BPF_B,
 				       BPF_WRITE, -1, false, false);

[Severity: High]
Since meta.arg_raw_mem.size remains 0 for ARG_PTR_TO_MAP_VALUE, does this cause check_helper_call() to skip this post-processing loop entirely?

Skipping this could miss crucial state scrubbing when a privileged program uses special slots like STACK_DYNPTR as a map value buffer.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

Re: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

Quoted 20 lines · On Thu, Jul 9, 2026 at 4:17 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The patch drops the detection of MEM_UNINIT raw memory arguments for ARG_PTR_TO_MAP_VALUE, which incorrectly breaks valid BPF programs that pass uninitialized stack buffers to helpers like bpf_map_pop_elem and bpf_map_peek_elem. --

--- Patch [2]: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta --- commit bbac2673fbeb11bb9179553dc669702134b2c290
Author: Amery Hung <[email protected]>

bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

To prepare for unifying the helper and kfunc call_arg_meta, group the scattered MEM_UNINIT ("raw") memory argument fields (raw_mode, regno and access_size) into a new struct arg_raw_mem_desc.

Quoted 4 lines

Also drop the bogus raw_mode assignment in the ARG_PTR_TO_MAP_VALUE case:
arg_type_is_raw_mem() only matches ARG_PTR_TO_MEM | MEM_UNINIT, and a map value pointer is handled by check_map_access(), never reaching check_stack_range_initialized(), so tagging it raw mode had no effect.

Quoted 5 lines

[Severity: High]
Is this assumption accurate? If a BPF program calls a helper like bpf_map_pop_elem() and passes a stack buffer (PTR_TO_STACK) as the map value buffer, wouldn't check_helper_mem_access() call check_stack_range_initialized() to validate the stack access?

I was wrong. This patch will cause a regression. raw_mode should not be dropped for ARG_PTR_TO_MAP_VALUE.

Quoted 60 lines

kernel/bpf/verifier.c static int check_stack_range_initialized( · line 6694

Quoted 13 lines
kernel/bpf/verifier.c +2 -0
index 7dd961ede88df..e2a3ed67d2eb5 100644
@@ -6690,6 +6688,8 @@ static int check_stack_range_initialized(
       * but BTF based global subprog validation isn't accurate enough.
       */
      bool allow_poison = access_size < 0 || clobber;
+     /* The call will initialize the memory; uninitialized stack allowed */
+     bool raw_mode = meta && meta->arg_raw_mem.regno == reg_from_argno(argno);
 
      access_size = abs(access_size);

[Severity: High]
Because arg_type_is_raw_mem() only checks ARG_PTR_TO_MEM, arg_raw_mem.regno will not be set for ARG_PTR_TO_MAP_VALUE arguments.

Consequently, raw_mode will evaluate to false here. Does this cause the verifier to fail to bypass the uninitialized read checks, rejecting valid unprivileged programs with an 'invalid read from stack' error?

[ ... ]

kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8406

Quoted 8 lines
kernel/bpf/verifier.c +0 -1
@@ -8403,7 +8401,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
                      verifier_bug(env, "invalid map_ptr to access map->value");
                      return -EFAULT;
              }
-             meta->raw_mode = arg_type & MEM_UNINIT;
              err = check_helper_mem_access(env, reg, argno_from_reg(regno), meta->map.ptr->value_size,
                                            arg_type & MEM_WRITE ? BPF_WRITE : BPF_READ,
                                            false, meta);

[Severity: High]
Without this MEM_UNINIT assignment for ARG_PTR_TO_MAP_VALUE, does the verifier incorrectly reject valid programs passing uninitialized stack memory?

[ ... ]

kernel/bpf/verifier.c static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn · line 10298

Quoted 10 lines
kernel/bpf/verifier.c +3 -2
@@ -10303,8 +10292,9 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
      /* Mark slots with STACK_MISC in case of raw mode, stack offset
       * is inferred from register state.
       */
-     for (i = 0; i < meta.access_size; i++) {
-             err = check_mem_access(env, insn_idx, regs + meta.regno, argno_from_reg(meta.regno), i, BPF_B,
+     for (i = 0; i < meta.arg_raw_mem.size; i++) {
+             err = check_mem_access(env, insn_idx, regs + meta.arg_raw_mem.regno,
+                                    argno_from_reg(meta.arg_raw_mem.regno), i, BPF_B,
                                     BPF_WRITE, -1, false, false);

[Severity: High]
Since meta.arg_raw_mem.size remains 0 for ARG_PTR_TO_MAP_VALUE, does this cause check_helper_call() to skip this post-processing loop entirely?

Skipping this could miss crucial state scrubbing when a privileged program uses special slots like STACK_DYNPTR as a map value buffer.

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2

maintainer Acked-by on v1 2/4
#6 lore raw

Re: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

In reply to Amery Hung
Quoted 16 lines · Amery Hung

To prepare for unifying the helper and kfunc call_arg_meta, group the scattered MEM_UNINIT ("raw") memory argument fields (raw_mode, regno and access_size) into a new struct arg_raw_mem_desc. The intention is to make it clear about when these are set and used instead of fields with overly generic names.

Identify the raw argument once, up front, in check_raw_mode_ok() ( like check_proto_release_reg() does for release_regno), recording its regno. check_stack_range_initialized() now recognizes the raw buffer by matching that regno, so the separate raw_mode flag is no longer needed.

Also drop the bogus raw_mode assignment in the ARG_PTR_TO_MAP_VALUE case:
arg_type_is_raw_mem() only matches ARG_PTR_TO_MEM | MEM_UNINIT, and a map value pointer is handled by check_map_access(), never reaching check_stack_range_initialized(), so tagging it raw mode had no effect.

Quoted 8 lines

No functional change intended. This patch does not enable raw_mode memory access for kfunc (i.e., uninit stack will not be allowed to be passed to kfunc for unprivileged programs). Existing kfuncs with arguments tagged with __uninit are either priviledged or dynptr kfuncs, which take another path to make sure the access is checked by check_mem_access().

---

Quoted 25 lines

 include/linux/bpf_verifier.h | 10 ++++++++ kernel/bpf/verifier.c        | 50 +++++++++++++++---------------------

 2 files changed, 30 insertions(+), 30 deletions(-)
include/linux/bpf_verifier.h +0 -0
index 317e99b9acc0..056b99d26cca 100644
@@ -1464,6 +1464,15 @@ struct ref_obj_desc {
  	u8 cnt;
  };

include/linux/bpf_verifier.h +9 -0
+/*
+ * A memory argument a call fills in. The verifier allows the stack to be uninitialized if
+ * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access().
+ */
+struct arg_raw_mem_desc {
+	u8 regno;
+	int size;
+};
+

 struct bpf_kfunc_call_arg_meta {
  /* In parameters */
  struct btf *btf;

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1522

Quoted 4 lines
include/linux/bpf_verifier.h +1 -0
@@ -1510,6 +1519,7 @@ struct bpf_kfunc_call_arg_meta {
  	struct bpf_map_desc map;
  	struct bpf_dynptr_desc dynptr;
  	struct ref_obj_desc ref_obj;
+	struct arg_raw_mem_desc arg_raw_mem;

Nit: this field is not used in patch #2 (this one).

Quoted 2 lines
include/linux/bpf_verifier.h +0 -0
  	u64 mem_size;
  };

[...]

author
#7 lore raw

Re: [PATCH bpf-next v1 2/4] bpf: Factor out raw_mode-related fields in bpf_call_arg_meta

Quoted 52 lines · Eduard Zingerman
Quoted 16 lines · Amery Hung

To prepare for unifying the helper and kfunc call_arg_meta, group the scattered MEM_UNINIT ("raw") memory argument fields (raw_mode, regno and access_size) into a new struct arg_raw_mem_desc. The intention is to make it clear about when these are set and used instead of fields with overly generic names.

Identify the raw argument once, up front, in check_raw_mode_ok() ( like check_proto_release_reg() does for release_regno), recording its regno. check_stack_range_initialized() now recognizes the raw buffer by matching that regno, so the separate raw_mode flag is no longer needed.

Also drop the bogus raw_mode assignment in the ARG_PTR_TO_MAP_VALUE case:
arg_type_is_raw_mem() only matches ARG_PTR_TO_MEM | MEM_UNINIT, and a map value pointer is handled by check_map_access(), never reaching check_stack_range_initialized(), so tagging it raw mode had no effect.

Quoted 8 lines

No functional change intended. This patch does not enable raw_mode memory access for kfunc (i.e., uninit stack will not be allowed to be passed to kfunc for unprivileged programs). Existing kfuncs with arguments tagged with __uninit are either priviledged or dynptr kfuncs, which take another path to make sure the access is checked by check_mem_access().

---

Quoted 23 lines
 include/linux/bpf_verifier.h | 10 ++++++++
 kernel/bpf/verifier.c        | 50 +++++++++++++++---------------------
 2 files changed, 30 insertions(+), 30 deletions(-)
include/linux/bpf_verifier.h +9 -0
index 317e99b9acc0..056b99d26cca 100644
@@ -1464,6 +1464,15 @@ struct ref_obj_desc {
      u8 cnt;
 };
 
+/*
+ * A memory argument a call fills in. The verifier allows the stack to be uninitialized if
+ * the range is a known constant. Stack slots are marked as STACK_MISC by check_mem_access().
+ */
+struct arg_raw_mem_desc {
+     u8 regno;
+     int size;
+};
+
 struct bpf_kfunc_call_arg_meta {
      /* In parameters */
Quoted 8 lines

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1522

Quoted 6 lines
include/linux/bpf_verifier.h +1 -0
      struct btf *btf;
@@ -1510,6 +1519,7 @@ struct bpf_kfunc_call_arg_meta {
      struct bpf_map_desc map;
      struct bpf_dynptr_desc dynptr;
      struct ref_obj_desc ref_obj;
+     struct arg_raw_mem_desc arg_raw_mem;

Nit: this field is not used in patch #2 (this one).

Thanks for reviewing the set!

Indeed. Currently, there is no raw mode use case for kfunc. I will drop it here and move it to the helper only section in patch 4 o make it less confusing.

Quoted 5 lines
Quoted 3 lines
include/linux/bpf_verifier.h +0 -0
      u64 mem_size;
 };

[...]

author
#8 lore raw

PATCH 3/4 bpf: Unify helper and kfunc allocation-size argument handling

In reply to Amery Hung

The constant "size of the PTR_TO_MEM returned in R0" argument is handled by both helpers (ARG_CONST_ALLOC_SIZE_OR_ZERO) and kfuncs (__rdonly_buf_size / __rdwr_buf_size), each with its own meta field (meta->mem_size, meta->r0_size) and duplicated validation. Add struct arg_alloc_mem_desc and a shared process_const_alloc_mem_size(), and replace both fields with meta->arg_alloc_mem.

The desc records presence with a 'found' flag instead of using a non-zero size as the sentinel. This also fixes a pre-existing bug on the kfunc return path: "no size argument" was tested as r0_size == 0, so an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size(), giving R0 the size of the pointed-to return type instead of 0. With 'found', an explicit zero size is honored and btf_resolve_size() is used only when no size argument was passed.

The size is stored in a u32, matching regs[R0].mem_size. The U32_MAX check now apply to both helper and kfunc through process_const_alloc_mem_size().

Fold bpf_session_cookie return size assignment into current kfunc return size resolution path.

Note that verifier saves kfunc return size through r0_size instead of mem_size. The later has no active readers so remove it.

---
 include/linux/bpf_verifier.h                  |  9 +-
 kernel/bpf/verifier.c                         | 87 ++++++++++---------
 .../selftests/bpf/prog_tests/kfunc_call.c     |  2 +-
 3 files changed, 55 insertions(+), 43 deletions(-)
include/linux/bpf_verifier.h +7 -2
index 056b99d26cca..3ed788ec6afc 100644
@@ -1473,6 +1473,12 @@ struct arg_raw_mem_desc {
 	int size;
 };
 
+/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
+struct arg_alloc_mem_desc {
+	u32 size;
+	bool found;
+};
+
 struct bpf_kfunc_call_arg_meta {
 	/* In parameters */
 	struct btf *btf;
@@ -1484,7 +1490,6 @@ struct bpf_kfunc_call_arg_meta {
 	u8 release_regno;
 	bool r0_rdonly;
 	u32 ret_btf_id;
-	u64 r0_size;
 	u32 subprogno;
 	struct {
 		u64 value;
@@ -1520,7 +1525,7 @@ struct bpf_kfunc_call_arg_meta {
 	struct bpf_dynptr_desc dynptr;
 	struct ref_obj_desc ref_obj;
 	struct arg_raw_mem_desc arg_raw_mem;
-	u64 mem_size;
+	struct arg_alloc_mem_desc arg_alloc_mem;
 };
 
 int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,
kernel/bpf/verifier.c +47 -40

143 lines not on this page · raw message

tools/testing/selftests/bpf/prog_tests/kfunc_call.c +1 -1
index 67a30bf69509..c9fce95d220e 100644
@@ -66,7 +66,7 @@ static struct kfunc_test_params kfunc_tests[] = {
 	TC_FAIL(kfunc_call_test_get_mem_fail_rdonly, 0, "R0 cannot write into rdonly_mem"),
 	TC_FAIL(kfunc_call_test_get_mem_fail_use_after_free, 0, "invalid mem access 'scalar'"),
 	TC_FAIL(kfunc_call_test_get_mem_fail_oob, 0, "min value is outside of the allowed memory range"),
-	TC_FAIL(kfunc_call_test_get_mem_fail_oversized, 0, "rdonly/rdwr_buf_size exceeds u32 max"),
+	TC_FAIL(kfunc_call_test_get_mem_fail_oversized, 0, "allocation size exceeds u32 max"),
 	TC_FAIL(kfunc_call_test_get_mem_fail_not_const, 0, "is not a const"),
 	TC_FAIL(kfunc_call_test_mem_acquire_fail, 0, "acquire kernel function does not return PTR_TO_BTF_ID"),
 	TC_FAIL(kfunc_call_test_pointer_arg_type_mismatch, 0, "R1 expected pointer to ctx, but got scalar"),
Signature · 1 line
2.52.0
#9 lore raw

Re: [PATCH bpf-next v1 3/4] bpf: Unify helper and kfunc allocation-size argument handling

The changelog describes fixing a pre-existing bug on the kfunc return path, where an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size().
Quoted 21 lines

bpf: Unify helper and kfunc allocation-size argument handling

The constant "size of the PTR_TO_MEM returned in R0" argument is handled by both helpers (ARG_CONST_ALLOC_SIZE_OR_ZERO) and kfuncs (__rdonly_buf_size / __rdwr_buf_size), each with its own meta field (meta->mem_size, meta->r0_size) and duplicated validation. Add struct arg_alloc_mem_desc and a shared process_const_alloc_mem_size(), and replace both fields with meta->arg_alloc_mem.

The desc records presence with a 'found' flag instead of using a non-zero size as the sentinel. This also fixes a pre-existing bug on the kfunc return path: "no size argument" was tested as r0_size == 0, so an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size(), giving R0 the size of the pointed-to return type instead of 0. With 'found', an explicit zero size is honored and btf_resolve_size() is used only when no size argument was passed.

The size is stored in a u32, matching regs[R0].mem_size. The U32_MAX check now apply to both helper and kfunc through process_const_alloc_mem_size().

Quoted 7 lines

Fold bpf_session_cookie return size assignment into current kfunc return size resolution path.

Note that verifier saves kfunc return size through r0_size instead of mem_size. The later has no active readers so remove it.

The changelog describes fixing a pre-existing bug on the kfunc return path, where an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size().

That sentinel-based fallback looks like it dates back to when fixed-size memory pointer returns were added for kfuncs, where the
"if (!meta.r0_size) { ... btf_resolve_size(...) }" block first appeared alongside the rdonly_buf_size/rdwr_buf_size handling that set meta->r0_size.

Should this carry a Fixes: tag so the fix can be backported?

Fixes f4b4eee6169b ("bpf: add support for fixed-size memory pointer returns for kfuncs")

This isn't a bug, but would it be worth tidying the changelog wording? "The later has no active readers" reads as "The latter", and "The U32_MAX check now apply to both" reads as "now applies".

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/29057017398

maintainer Acked-by on v1 3/4
#10 lore raw

Re: [PATCH bpf-next v1 3/4] bpf: Unify helper and kfunc allocation-size argument handling

In reply to Amery Hung
Quoted 7 lines · Amery Hung

The constant "size of the PTR_TO_MEM returned in R0" argument is handled by both helpers (ARG_CONST_ALLOC_SIZE_OR_ZERO) and kfuncs (__rdonly_buf_size / __rdwr_buf_size), each with its own meta field (meta->mem_size, meta->r0_size) and duplicated validation. Add struct arg_alloc_mem_desc and a shared process_const_alloc_mem_size(), and replace both fields with meta->arg_alloc_mem.

Quoted 7 lines

The desc records presence with a 'found' flag instead of using a non-zero size as the sentinel. This also fixes a pre-existing bug on the kfunc return path: "no size argument" was tested as r0_size == 0, so an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size(), giving R0 the size of the pointed-to return type instead of 0. With 'found', an explicit zero size is honored and btf_resolve_size() is used only when no size argument was passed.

Could you please add a selftest that triggers this bug w/o the change?

Quoted 4 lines

The size is stored in a u32, matching regs[R0].mem_size. The U32_MAX check now apply to both helper and kfunc through process_const_alloc_mem_size().

Quoted 8 lines

Fold bpf_session_cookie return size assignment into current kfunc return size resolution path.

Note that verifier saves kfunc return size through r0_size instead of mem_size. The later has no active readers so remove it.

---

[...]

include/linux/bpf_verifier.h struct arg_raw_mem_desc { · line 1477

Quoted 10 lines
include/linux/bpf_verifier.h +2 -0
index 056b99d26cca..3ed788ec6afc 100644
@@ -1473,6 +1473,12 @@ struct arg_raw_mem_desc {
 	int size;
 };
 
+/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
+struct arg_alloc_mem_desc {

Nit: 'returned_mem_desc' seem to be a better name.

Quoted 4 lines
include/linux/bpf_verifier.h +4 -0
+	u32 size;
+	bool found;
+};
+

[...]

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index e2a3ed67d2eb..fcd9715eff01 100644

[...]

kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6978

Quoted 6 lines
kernel/bpf/verifier.c +2 -0
@@ -6974,6 +6974,40 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg
 	return err;
 }
 
+static int process_const_alloc_mem_size(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
+					int regno, argno_t argno,

Nit: argno_t can encode both register and stack arguments,
would it be possible to adjust the code initializing 'argno' variable in check_func_arg() such that it is constructed properly based on 'arg < 5' condition and passed here as a single value?
It appears that such a change would also remove a lot of duplicated 'argno_from_reg(regno)' calls in that function.

Quoted 5 lines
kernel/bpf/verifier.c +5 -0
+					struct arg_alloc_mem_desc *arg_alloc_mem)
+{
+	int err;
+
+	if (arg_alloc_mem->found) {

kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6993

Quoted 10 lines
kernel/bpf/verifier.c +10 -0
+		verifier_bug(env, "only one allocation size argument permitted");
+		return -EFAULT;
+	}
+
+	if (!tnum_is_const(reg->var_off)) {
+		verbose(env, "%s is not a const\n", reg_arg_name(env, argno));
+		return -EINVAL;
+	}
+
+	if (reg->var_off.value > U32_MAX) {

Fun fact: when passing parameters to a function, the verifier forces
the buffer's size to be < BPF_MAX_VAR_SIZ (see check_mem_size_reg()). But that's pre-existing inconsistency.

Quoted 20 lines
kernel/bpf/verifier.c +17 -0
+		verbose(env, "%s allocation size exceeds u32 max\n", reg_arg_name(env, argno));
+		return -EINVAL;
+	}
+
+	if (regno >= 0)
+		err = mark_chain_precision(env, regno);
+	else
+		err = mark_stack_arg_precision(env, arg_idx_from_argno(argno));
+	if (err)
+		return err;
+
+	arg_alloc_mem->size = reg->var_off.value;
+	arg_alloc_mem->found = true;
+
+	return 0;
+}
+
 static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *mem_reg,
 				    struct bpf_reg_state *size_reg, argno_t mem_argno, argno_t size_argno)
 {

[...]

author
#11 lore raw

Re: [PATCH bpf-next v1 3/4] bpf: Unify helper and kfunc allocation-size argument handling

Quoted 17 lines · Eduard Zingerman
Quoted 7 lines · Amery Hung

The constant "size of the PTR_TO_MEM returned in R0" argument is handled by both helpers (ARG_CONST_ALLOC_SIZE_OR_ZERO) and kfuncs (__rdonly_buf_size / __rdwr_buf_size), each with its own meta field (meta->mem_size, meta->r0_size) and duplicated validation. Add struct arg_alloc_mem_desc and a shared process_const_alloc_mem_size(), and replace both fields with meta->arg_alloc_mem.

Quoted 7 lines

The desc records presence with a 'found' flag instead of using a non-zero size as the sentinel. This also fixes a pre-existing bug on the kfunc return path: "no size argument" was tested as r0_size == 0, so an explicit __rdonly_buf_size/__rdwr_buf_size of 0 was treated as absent and fell through to btf_resolve_size(), giving R0 the size of the pointed-to return type instead of 0. With 'found', an explicit zero size is honored and btf_resolve_size() is used only when no size argument was passed.

Quoted 1 line

Could you please add a selftest that triggers this bug w/o the change?

Ah yes. I will add a selftest testing size == 0.

Quoted 26 lines
Quoted 5 lines

The size is stored in a u32, matching regs[R0].mem_size. The U32_MAX check now apply to both helper and kfunc through process_const_alloc_mem_size().

Quoted 8 lines

Fold bpf_session_cookie return size assignment into current kfunc return size resolution path.

Note that verifier saves kfunc return size through r0_size instead of mem_size. The later has no active readers so remove it.

---

[...]

Quoted 8 lines
include/linux/bpf_verifier.h +0 -0
index 056b99d26cca..3ed788ec6afc 100644
@@ -1473,6 +1473,12 @@ struct arg_raw_mem_desc {
      int size;
 };
 
Quoted 4 lines

include/linux/bpf_verifier.h struct arg_raw_mem_desc { · line 1477

Quoted 2 lines
include/linux/bpf_verifier.h +2 -0
+/* Size of PTR_TO_MEM returned, taken from a constant allocation-size argument */
+struct arg_alloc_mem_desc {

Nit: 'returned_mem_desc' seem to be a better name.

Sure. I will change it to ret_mem_desc.

Also worth noting that r0_rdonly is intentionally left out. This is because the information can be derived from BTF before the main verification loop. In the future when we generate bpf_func_proto for kfunc, this can be saved in ret_type (i.e., {rdwr,rdonly}_buf_size imply ret_type = RET_PTR_TO_MEM | {0,MEM_RDONLY}).

Quoted 19 lines
Quoted 5 lines
include/linux/bpf_verifier.h +4 -0
+     u32 size;
+     bool found;
+};
+

[...]

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index e2a3ed67d2eb..fcd9715eff01 100644

[...]

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
@@ -6974,6 +6974,40 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg
      return err;
 }
 
Quoted 10 lines

kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6978

Quoted 2 lines
kernel/bpf/verifier.c +2 -0
+static int process_const_alloc_mem_size(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
+                                     int regno, argno_t argno,

Nit: argno_t can encode both register and stack arguments,
would it be possible to adjust the code initializing 'argno' variable in check_func_arg() such that it is constructed properly based on 'arg < 5' condition and passed here as a single value?
It appears that such a change would also remove a lot of duplicated 'argno_from_reg(regno)' calls in that function.

I will add a patch to first remove the duplicated returned_mem_desc() in check_func_arg().

Quoted 12 lines
Quoted 12 lines
kernel/bpf/verifier.c +11 -0
+                                     struct arg_alloc_mem_desc *arg_alloc_mem)
+{
+     int err;
+
+     if (arg_alloc_mem->found) {
+             verifier_bug(env, "only one allocation size argument permitted");
+             return -EFAULT;
+     }
+
+     if (!tnum_is_const(reg->var_off)) {
+             verbose(env, "%s is not a const\n", reg_arg_name(env, argno));
Quoted 8 lines

kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6993

Quoted 4 lines
kernel/bpf/verifier.c +4 -0
+             return -EINVAL;
+     }
+
+     if (reg->var_off.value > U32_MAX) {

Fun fact: when passing parameters to a function, the verifier forces
the buffer's size to be < BPF_MAX_VAR_SIZ (see check_mem_size_reg()). But that's pre-existing inconsistency.

Noted.

Quoted 23 lines
Quoted 21 lines
kernel/bpf/verifier.c +17 -0
+             verbose(env, "%s allocation size exceeds u32 max\n", reg_arg_name(env, argno));
+             return -EINVAL;
+     }
+
+     if (regno >= 0)
+             err = mark_chain_precision(env, regno);
+     else
+             err = mark_stack_arg_precision(env, arg_idx_from_argno(argno));
+     if (err)
+             return err;
+
+     arg_alloc_mem->size = reg->var_off.value;
+     arg_alloc_mem->found = true;
+
+     return 0;
+}
+
 static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_reg_state *mem_reg,
                                  struct bpf_reg_state *size_reg, argno_t mem_argno, argno_t size_argno)
 {

[...]

author
#12 lore raw

PATCH 4/4 bpf: Unify helper and kfunc call argument meta

In reply to Amery Hung

Helper and kfunc argument checking carried two separate meta structs: the verifier-local struct bpf_call_arg_meta and bpf_kfunc_call_arg_meta. Merge them into a single struct bpf_call_arg_meta. This is groundwork for sharing argument checking between helpers and kfuncs.

While merging, drop the btf_id field from the helper meta since it is never used.

No functional change.

---
 include/linux/bpf_verifier.h | 40 +++++++++------
 kernel/bpf/cfg.c             |  2 +-
 kernel/bpf/verifier.c        | 95 +++++++++++++++---------------------
 3 files changed, 63 insertions(+), 74 deletions(-)
include/linux/bpf_verifier.h +24 -16
index 3ed788ec6afc..5fbc3dfe5b19 100644
@@ -1479,18 +1479,24 @@ struct arg_alloc_mem_desc {
 	bool found;
 };
 
-struct bpf_kfunc_call_arg_meta {
-	/* In parameters */
+struct bpf_call_arg_meta {
+	/* Common */
 	struct btf *btf;
 	u32 func_id;
-	u32 kfunc_flags;
-	const struct btf_type *func_proto;
-	const char *func_name;
-	/* Out parameters */
 	u8 release_regno;
-	bool r0_rdonly;
 	u32 ret_btf_id;
 	u32 subprogno;
+	struct bpf_map_desc map;
+	struct bpf_dynptr_desc dynptr;
+	struct ref_obj_desc ref_obj;
+	struct arg_raw_mem_desc arg_raw_mem;
+	struct arg_alloc_mem_desc arg_alloc_mem;
+
+	/* Only set by kfunc */
+	bool r0_rdonly;
+	u32 kfunc_flags;
+	const struct btf_type *func_proto;
+	const char *func_name;
 	struct {
 		u64 value;
 		bool found;
@@ -1521,29 +1527,31 @@ struct bpf_kfunc_call_arg_meta {
 		u8 spi;
 		u8 frameno;
 	} iter;
-	struct bpf_map_desc map;
-	struct bpf_dynptr_desc dynptr;
-	struct ref_obj_desc ref_obj;
-	struct arg_raw_mem_desc arg_raw_mem;
-	struct arg_alloc_mem_desc arg_alloc_mem;
+
+	/* Only set by helper */
+	bool pkt_access;
+	u64 msize_max_value;
+	s64 const_map_key;
+	struct btf *ret_btf;
+	struct btf_field *kptr_field;
 };
 
 int bpf_get_helper_proto(struct bpf_verifier_env *env, int func_id,
 			 const struct bpf_func_proto **ptr);
 int bpf_fetch_kfunc_arg_meta(struct bpf_verifier_env *env, s32 func_id,
-			     s16 offset, struct bpf_kfunc_call_arg_meta *meta);
+			     s16 offset, struct bpf_call_arg_meta *meta);
 bool bpf_is_async_callback_calling_insn(struct bpf_insn *insn);
 bool bpf_is_sync_callback_calling_insn(struct bpf_insn *insn);
-static inline bool bpf_is_iter_next_kfunc(struct bpf_kfunc_call_arg_meta *meta)
+static inline bool bpf_is_iter_next_kfunc(struct bpf_call_arg_meta *meta)
 {
 	return meta->kfunc_flags & KF_ITER_NEXT;
 }
 
-static inline bool bpf_is_kfunc_sleepable(struct bpf_kfunc_call_arg_meta *meta)
+static inline bool bpf_is_kfunc_sleepable(struct bpf_call_arg_meta *meta)
 {
 	return meta->kfunc_flags & KF_SLEEPABLE;
 }
-bool bpf_is_kfunc_pkt_changing(struct bpf_kfunc_call_arg_meta *meta);
+bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta);
 struct bpf_iarray *bpf_iarray_realloc(struct bpf_iarray *old, size_t n_elem);
 int bpf_copy_insn_array_uniq(struct bpf_map *map, u32 start, u32 end, u32 *off);
 bool bpf_insn_is_cond_jump(u8 code);
kernel/bpf/cfg.c +1 -1
index 26d37066465f..db3416a7c904 100644
@@ -491,7 +491,7 @@ static int visit_insn(int t, struct bpf_verifier_env *env)
 					return ret;
 			}
 		} else if (insn->src_reg == BPF_PSEUDO_KFUNC_CALL) {
-			struct bpf_kfunc_call_arg_meta meta;
+			struct bpf_call_arg_meta meta;
 
 			ret = bpf_fetch_kfunc_arg_meta(env, insn->imm, insn->off, &meta);
 			if (ret == 0 && bpf_is_iter_next_kfunc(&meta)) {
kernel/bpf/verifier.c +38 -57

304 lines not on this page · raw message

Signature · 1 line
2.52.0
#13 lore raw

Re: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Uninitialized stack memory read in check_kfunc_call when handling kfunc calls in signed BPF programs. --

--- Patch [4]: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta --- commit b1105689a4b2bf6e2532e6d414d410ef55c0694b
Author: Amery Hung <[email protected]>

bpf: Unify helper and kfunc call argument meta

Helper and kfunc argument checking carried two separate meta structs: the verifier-local struct bpf_call_arg_meta and bpf_kfunc_call_arg_meta. Merge them into a single struct bpf_call_arg_meta. This is groundwork for sharing argument checking between helpers and kfuncs.

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index fcd9715eff017..5ccb00a8c84ce 100644

[ ... ]

kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 12959

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
@@ -12972,7 +12953,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
 	struct bpf_reg_state *regs = cur_regs(env);
 	const char *func_name, *ptr_type_name;
 	const struct btf_type *t, *ptr_type;
-	struct bpf_kfunc_call_arg_meta meta;
+	struct bpf_call_arg_meta meta;
 	struct bpf_insn_aux_data *insn_aux;
 	int err, insn_idx = *insn_idx_p;
 	const struct btf_param *args;

[Severity: High]
This is a pre-existing issue, but does this lead to reading an uninitialized pointer if bpf_fetch_kfunc_arg_meta() returns early?

In check_kfunc_call() in kernel/bpf/verifier.c, the local variable meta is declared without initialization and passed to bpf_fetch_kfunc_arg_meta(). If the call to fetch_kfunc_meta() inside it returns an error like -EACCES, it returns early before initializing meta:

kernel/bpf/verifier.c:bpf_fetch_kfunc_arg_meta() {
	struct bpf_kfunc_meta kfunc;
	int err;

	err = fetch_kfunc_meta(env, func_id, offset, &kfunc);
	if (err)
		return err;
    ...
}

Back in check_kfunc_call(), the error path evaluates meta.func_name:

	err = bpf_fetch_kfunc_arg_meta(env, insn->imm, insn->off, &meta);
	if (err == -EACCES && meta.func_name)
		verbose(env, "calling kernel function %s is not allowed\n", meta.func_name);

If the stack memory is uninitialized, could meta.func_name contain a garbage pointer that leads to a crash or an info leak when dereferenced by vsnprintf during the verbose() call?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#14 lore raw

Re: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta

Quoted 56 lines · On Thu, Jul 9, 2026 at 4:16 PM <[email protected]> wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Uninitialized stack memory read in check_kfunc_call when handling kfunc calls in signed BPF programs. --

--- Patch [4]: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta --- commit b1105689a4b2bf6e2532e6d414d410ef55c0694b
Author: Amery Hung <[email protected]>

bpf: Unify helper and kfunc call argument meta

Helper and kfunc argument checking carried two separate meta structs: the verifier-local struct bpf_call_arg_meta and bpf_kfunc_call_arg_meta. Merge them into a single struct bpf_call_arg_meta. This is groundwork for sharing argument checking between helpers and kfuncs.

Quoted 4 lines
kernel/bpf/verifier.c +0 -0
index fcd9715eff017..5ccb00a8c84ce 100644

[ ... ]

kernel/bpf/verifier.c static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn, · line 12959

Quoted 9 lines
kernel/bpf/verifier.c +1 -1
@@ -12972,7 +12953,7 @@ static int check_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
      struct bpf_reg_state *regs = cur_regs(env);
      const char *func_name, *ptr_type_name;
      const struct btf_type *t, *ptr_type;
-     struct bpf_kfunc_call_arg_meta meta;
+     struct bpf_call_arg_meta meta;
      struct bpf_insn_aux_data *insn_aux;
      int err, insn_idx = *insn_idx_p;
      const struct btf_param *args;

[Severity: High]
This is a pre-existing issue, but does this lead to reading an uninitialized pointer if bpf_fetch_kfunc_arg_meta() returns early?

In check_kfunc_call() in kernel/bpf/verifier.c, the local variable meta is declared without initialization and passed to bpf_fetch_kfunc_arg_meta(). If the call to fetch_kfunc_meta() inside it returns an error like -EACCES, it returns early before initializing meta:

kernel/bpf/verifier.c:bpf_fetch_kfunc_arg_meta() {
        struct bpf_kfunc_meta kfunc;
        int err;

        err = fetch_kfunc_meta(env, func_id, offset, &kfunc);
        if (err)
                return err;
    ...
}

Back in check_kfunc_call(), the error path evaluates meta.func_name:

Quoted 7 lines
        err = bpf_fetch_kfunc_arg_meta(env, insn->imm, insn->off, &meta);
        if (err == -EACCES && meta.func_name)
                verbose(env, "calling kernel function %s is not allowed\n", meta.func_name);

If the stack memory is uninitialized, could meta.func_name contain a garbage pointer that leads to a crash or an info leak when dereferenced by vsnprintf during the verbose() call?

Confirmed the bug is reachable. I will include the fix in the next respin.

Quoted 3 lines

--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4

maintainer Acked-by on v1 4/4
#15 lore raw

Re: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta

In reply to Amery Hung
Quoted 5 lines · Amery Hung

Helper and kfunc argument checking carried two separate meta structs: the verifier-local struct bpf_call_arg_meta and bpf_kfunc_call_arg_meta. Merge them into a single struct bpf_call_arg_meta. This is groundwork for sharing argument checking between helpers and kfuncs.

Quoted 7 lines

While merging, drop the btf_id field from the helper meta since it is never used.

No functional change.

---

Quoted 43 lines
 include/linux/bpf_verifier.h | 40 +++++++++------
 kernel/bpf/cfg.c             |  2 +-
 kernel/bpf/verifier.c        | 95 +++++++++++++++---------------------
 3 files changed, 63 insertions(+), 74 deletions(-)
include/linux/bpf_verifier.h +13 -7
index 3ed788ec6afc..5fbc3dfe5b19 100644
@@ -1479,18 +1479,24 @@ struct arg_alloc_mem_desc {
 	bool found;
 };
 
-struct bpf_kfunc_call_arg_meta {
-	/* In parameters */
+struct bpf_call_arg_meta {
+	/* Common */
 	struct btf *btf;
 	u32 func_id;
-	u32 kfunc_flags;
-	const struct btf_type *func_proto;
-	const char *func_name;
-	/* Out parameters */
 	u8 release_regno;
-	bool r0_rdonly;
 	u32 ret_btf_id;
 	u32 subprogno;
+	struct bpf_map_desc map;
+	struct bpf_dynptr_desc dynptr;
+	struct ref_obj_desc ref_obj;
+	struct arg_raw_mem_desc arg_raw_mem;
+	struct arg_alloc_mem_desc arg_alloc_mem;
+
+	/* Only set by kfunc */
+	bool r0_rdonly;
+	u32 kfunc_flags;
+	const struct btf_type *func_proto;
+	const char *func_name;
 	struct {
 		u64 value;
 		bool found;
@@ -1521,29 +1527,31 @@ struct bpf_kfunc_call_arg_meta {
 		u8 spi;

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1532

Quoted 10 lines
include/linux/bpf_verifier.h +3 -5
 		u8 frameno;
 	} iter;
-	struct bpf_map_desc map;
-	struct bpf_dynptr_desc dynptr;
-	struct ref_obj_desc ref_obj;
-	struct arg_raw_mem_desc arg_raw_mem;
-	struct arg_alloc_mem_desc arg_alloc_mem;
+
+	/* Only set by helper */
+	bool pkt_access;

It appears this one can be dropped altogether:

    diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
    index 5ccb00a8c84c..b1d5a359c78b 100644
    --- a/kernel/bpf/verifier.c
    +++ b/kernel/bpf/verifier.c
    @@ -4654,7 +4654,7 @@ static int check_map_access(struct bpf_verifier_env *env, struct bpf_reg_state *
     }

     static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
    -                              const struct bpf_call_arg_meta *meta,
    +                              const struct bpf_func_proto *fn,
                                   enum bpf_access_type t)
     {
            enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
    @@ -4678,8 +4678,8 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
            case BPF_PROG_TYPE_LWT_XMIT:
            case BPF_PROG_TYPE_SK_SKB:
            case BPF_PROG_TYPE_SK_MSG:
    -               if (meta)
    -                       return meta->pkt_access;
    +               if (fn)
    +                       return fn->pkt_access;

                    env->seen_direct_write = true;
                    return true;
    @@ -8307,7 +8307,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
            }

            if (type_is_pkt_pointer(type) &&
    -           !may_access_direct_pkt_data(env, meta, BPF_READ)) {
    +           !may_access_direct_pkt_data(env, fn, BPF_READ)) {
                    verbose(env, "helper access to the packet is not allowed\n");
                    return -EACCES;
            }
Quoted 5 lines
include/linux/bpf_verifier.h +4 -0
+	u64 msize_max_value;
+	s64 const_map_key;
+	struct btf *ret_btf;
+	struct btf_field *kptr_field;
 };

[...]

author
#16 lore raw

Re: [PATCH bpf-next v1 4/4] bpf: Unify helper and kfunc call argument meta

Quoted 98 lines · Eduard Zingerman
Quoted 5 lines · Amery Hung

Helper and kfunc argument checking carried two separate meta structs: the verifier-local struct bpf_call_arg_meta and bpf_kfunc_call_arg_meta. Merge them into a single struct bpf_call_arg_meta. This is groundwork for sharing argument checking between helpers and kfuncs.

Quoted 7 lines

While merging, drop the btf_id field from the helper meta since it is never used.

No functional change.

---

Quoted 43 lines
 include/linux/bpf_verifier.h | 40 +++++++++------
 kernel/bpf/cfg.c             |  2 +-
 kernel/bpf/verifier.c        | 95 +++++++++++++++---------------------
 3 files changed, 63 insertions(+), 74 deletions(-)
include/linux/bpf_verifier.h +13 -7
index 3ed788ec6afc..5fbc3dfe5b19 100644
@@ -1479,18 +1479,24 @@ struct arg_alloc_mem_desc {
      bool found;
 };
 
-struct bpf_kfunc_call_arg_meta {
-     /* In parameters */
+struct bpf_call_arg_meta {
+     /* Common */
      struct btf *btf;
      u32 func_id;
-     u32 kfunc_flags;
-     const struct btf_type *func_proto;
-     const char *func_name;
-     /* Out parameters */
      u8 release_regno;
-     bool r0_rdonly;
      u32 ret_btf_id;
      u32 subprogno;
+     struct bpf_map_desc map;
+     struct bpf_dynptr_desc dynptr;
+     struct ref_obj_desc ref_obj;
+     struct arg_raw_mem_desc arg_raw_mem;
+     struct arg_alloc_mem_desc arg_alloc_mem;
+
+     /* Only set by kfunc */
+     bool r0_rdonly;
+     u32 kfunc_flags;
+     const struct btf_type *func_proto;
+     const char *func_name;
      struct {
              u64 value;
              bool found;
@@ -1521,29 +1527,31 @@ struct bpf_kfunc_call_arg_meta {
              u8 spi;

include/linux/bpf_verifier.h struct bpf_kfunc_call_arg_meta { · line 1532

Quoted 10 lines
include/linux/bpf_verifier.h +3 -5
              u8 frameno;
      } iter;
-     struct bpf_map_desc map;
-     struct bpf_dynptr_desc dynptr;
-     struct ref_obj_desc ref_obj;
-     struct arg_raw_mem_desc arg_raw_mem;
-     struct arg_alloc_mem_desc arg_alloc_mem;
+
+     /* Only set by helper */
+     bool pkt_access;

It appears this one can be dropped altogether:

    diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
    index 5ccb00a8c84c..b1d5a359c78b 100644
    --- a/kernel/bpf/verifier.c
    +++ b/kernel/bpf/verifier.c
    @@ -4654,7 +4654,7 @@ static int check_map_access(struct bpf_verifier_env *env, struct bpf_reg_state *
     }

     static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
    -                              const struct bpf_call_arg_meta *meta,
    +                              const struct bpf_func_proto *fn,
                                   enum bpf_access_type t)
     {
            enum bpf_prog_type prog_type = resolve_prog_type(env->prog);
    @@ -4678,8 +4678,8 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env,
            case BPF_PROG_TYPE_LWT_XMIT:
            case BPF_PROG_TYPE_SK_SKB:
            case BPF_PROG_TYPE_SK_MSG:
    -               if (meta)
    -                       return meta->pkt_access;
    +               if (fn)
    +                       return fn->pkt_access;

                    env->seen_direct_write = true;
                    return true;
    @@ -8307,7 +8307,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
Quoted 14 lines
            }

            if (type_is_pkt_pointer(type) &&
    -           !may_access_direct_pkt_data(env, meta, BPF_READ)) {
    +           !may_access_direct_pkt_data(env, fn, BPF_READ)) {
                    verbose(env, "helper access to the packet is not allowed\n");
                    return -EACCES;
            }
Quoted 5 lines
include/linux/bpf_verifier.h +4 -0
+     u64 msize_max_value;
+     s64 const_map_key;
+     struct btf *ret_btf;
+     struct btf_field *kptr_field;
 };

Make sense. I will incorporate this in the respin.

Quoted 2 lines

[...]