Generate bpf_func_proto for kfunc
Subsystems (11)
v2 cover Generate bpf_func_proto for kfunc
Hi,
This is the second of three patch sets to unify kfunc and helper argument verification. It:
1) further aligns the kfunc and helper argument checks,
2) makes kfunc argument type classification depend solely on BTF, and
3) generates a bpf_func_proto for each kfunc.
With classification now a pure function of the kfunc's BTF, it is computed once at add-call time and cached in the generated bpf_func_proto, rather than re-derived on every verification of the call. Along the way it also fixes a few issues.
The next patch set will align the argument register compatibility checks and route helper and kfunc argument verification through a single shared function.
[1/3] https://lore.kernel.org/bpf/[email protected]/
Changelog
v1 -> v2:
- patch 2: use reg_arg_name() for the map-mismatch message; derive the object register correctly on both helper and kfunc paths
- patch 3: reject non-CONST_PTR_TO_MAP regs (base_type check) to fix map-value type confusion
- patch 15: also reject referenced regs with unsafe modifiers (e.g. MEM_PERCPU)
- patch 17: reject non-SCALAR_VALUE for KF_ARG_MEM_SIZE
Amery Hung (18):
bpf: Drop process_timer_func wrappers
bpf: Unify const map ptr argument checking for helpers and kfuncs
bpf: Split kfunc map argument into __const_map and __map
bpf: Pass kfunc meta to mem and mem_size check
bpf: Check helper and kfunc mem+size arguments identically selftests/bpf: Add tests for helper and kfunc mem+size arguments
bpf: Check fixed-size mem args of helpers and kfuncs the same way
bpf: Express ARG_CONST_SIZE_OR_ZERO as ARG_CONST_SIZE |
SCALAR_MAYBE_ZERO
bpf: Rename ARG_CONST_SIZE{,_OR_ZERO} to ARG_MEM_SIZE{,_OR_ZERO}
bpf: Fold __szk const size handling into the scalar arg path
bpf: Classify kfunc mem_size args from BTF without register state
bpf: Handle NULL kfunc pointer args without a KF_ARG_PTR_TO_NULL type
bpf: Distinguish fixed- and variable-size kfunc mem args with
MEM_FIXED_SIZE
bpf: Check helper mem+size in ARG_PTR_TO_MEM case
bpf: Classify kfunc pointer arguments from BTF, resolve type against
the register
bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL
bpf: Classify scalar kfunc arguments from BTF
bpf: Generate kfunc argument prototype at add-call time
Documentation/bpf/kfuncs.rst | 30 +- include/linux/bpf.h | 42 +- include/linux/bpf_verifier.h | 31 +- kernel/bpf/backtrack.c | 2 +- kernel/bpf/bpf_lsm.c | 4 +- kernel/bpf/btf.c | 2 +- kernel/bpf/cgroup.c | 8 +- kernel/bpf/core.c | 4 +- kernel/bpf/helpers.c | 42 +- kernel/bpf/ringbuf.c | 2 +- kernel/bpf/stackmap.c | 10 +- kernel/bpf/syscall.c | 6 +- kernel/bpf/verifier.c | 786 ++++++++++-------- kernel/trace/bpf_trace.c | 62 +- net/core/filter.c | 116 +-- .../selftests/bpf/prog_tests/verifier.c | 2 + .../selftests/bpf/progs/cgrp_kfunc_failure.c | 2 +- .../bpf/progs/mem_rdonly_untrusted.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 2 +- .../selftests/bpf/progs/verifier_bounds.c | 2 +- .../progs/verifier_helper_access_var_len.c | 6 +- .../bpf/progs/verifier_helper_value_access.c | 2 +- .../bpf/progs/verifier_mem_size_reg.c | 60 ++ .../selftests/bpf/progs/verifier_vfs_reject.c | 6 +- tools/testing/selftests/bpf/verifier/calls.c | 6 +- 25 files changed, 711 insertions(+), 526 deletions(-) create mode 100644 tools/testing/selftests/bpf/progs/verifier_mem_size_reg.c
Signature · 1 line
2.52.0
PATCH v2 1/18 bpf: Drop process_timer_func wrappers
Drop process_timer_{helper,kfunc}() since bpf_call_arg_meta is now shared by helper and kfunc. Call process_timer_func() directly.
--- kernel/bpf/verifier.c | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-)
kernel/bpf/verifier.c +2 -14
@@ -7224,18 +7224,6 @@ static int process_timer_func(struct bpf_verifier_env *env, struct bpf_reg_statereturn check_map_field_pointer (env, reg, argno,BPF_TIMER , map); } -static int process_timer_helper (struct bpf_verifier_env *env,struct bpf_reg_state *reg,argno_t argno, -struct bpf_call_arg_meta *meta) -{ -return process_timer_func (env, reg, argno, &meta->map); -} - -static int process_timer_kfunc (struct bpf_verifier_env *env,struct bpf_reg_state *reg,argno_t argno, -struct bpf_call_arg_meta *meta) -{ -return process_timer_func (env, reg, argno, &meta->map); -} -static int process_kptr_func (struct bpf_verifier_env *env,int regno,struct bpf_call_arg_meta *meta) {
@@ -8466,7 +8454,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, }break ;case ARG_PTR_TO_TIMER : - err =process_timer_helper (env, reg, argno, meta); + err =process_timer_func (env, reg, argno, &meta->map);if (err)return err;break ;
@@ -12514,7 +12502,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me reg_arg_name(env, argno));return -EINVAL ; } - ret =process_timer_kfunc (env, reg, argno, meta); + ret =process_timer_func (env, reg, argno, &meta->map);if (ret <0 )return ret;break ;
Signature · 1 line
2.52.0
PATCH v2 2/18 bpf: Unify const map ptr argument checking for helpers and kfuncs
In reply to Amery HungBoth the helper ARG_CONST_MAP_PTR and the kfunc KF_ARG_PTR_TO_MAP recorded the map pointer in meta->map and, when a map was already bound by a preceding timer/workqueue/task_work argument, rejected a mismatching map.
Factor the logic into a single process_map_ptr_arg() used by both paths. The bound-object name (timer, workqueue, or bpf_task_work) is derived from the bound map's btf_record, and the register numbers in the message are computed from the map argument position instead of being hard-coded.
--- kernel/bpf/verifier.c | 97 ++++++++++++++++++++----------------------- 1 file changed, 44 insertions(+), 53 deletions(-)
kernel/bpf/verifier.c +44 -53
@@ -8274,6 +8274,44 @@ static int get_constant_map_key(struct bpf_verifier_env *env,static bool can_elide_value_nullness (const struct bpf_map *map); +static int process_map_ptr_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +argno_t argno,struct bpf_call_arg_meta *meta) +{ +/* Use map_uid (which is unique id of inner map) to reject: +* inner_map1 = bpf_map_lookup_elem(outer_map, key1) +* inner_map2 = bpf_map_lookup_elem(outer_map, key2) +* if (inner_map1 && inner_map2) { +* timer = bpf_map_lookup_elem(inner_map1); +* if (timer) +* // mismatch would have been allowed +* bpf_timer_init(timer, inner_map2); +* } +* +* Comparing map_ptr is enough to distinguish normal and outer maps. +*/ +if (meta->map.ptr && + (meta->map.ptr != reg->map_ptr || meta->map.uid != reg->map_uid)) { +argno_t obj_argno =argno_from_reg (reg_from_argno (argno) -1 ); +struct btf_record *rec = meta->map.ptr->record; +const char *obj_name ="workqueue" ; + +if (rec->timer_off >=0 ) + obj_name ="timer" ; +else if (rec->task_work_off >=0 ) + obj_name ="bpf_task_work" ; + +verbose (env,"%s pointer in %s map_uid=%d " , + obj_name,reg_arg_name (env, obj_argno), meta->map.uid); +verbose (env,"doesn't match map pointer in %s map_uid=%d\n" , +reg_arg_name (env, argno), reg->map_uid); +return -EINVAL ; + } + + meta->map.ptr = reg->map_ptr; + meta->map.uid = reg->map_uid; +return 0 ; +} + static int check_func_arg(struct bpf_verifier_env *env, u32 arg, struct bpf_call_arg_meta *meta, const struct bpf_func_proto *fn,
@@ -8349,29 +8387,9 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,switch (base_type (arg_type)) {case ARG_CONST_MAP_PTR :/* bpf_map_xxx(map_ptr) call: remember that map_ptr */ -if (meta->map.ptr) { -/* Use map_uid (which is unique id of inner map) to reject: -* inner_map1 = bpf_map_lookup_elem(outer_map, key1) -* inner_map2 = bpf_map_lookup_elem(outer_map, key2) -* if (inner_map1 && inner_map2) { -* timer = bpf_map_lookup_elem(inner_map1); -* if (timer) -* // mismatch would have been allowed -* bpf_timer_init(timer, inner_map2); -* } -* -* Comparing map_ptr is enough to distinguish normal and outer maps. -*/ -if (meta->map.ptr != reg->map_ptr || - meta->map.uid != reg->map_uid) { -verbose (env, -"timer pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n" , - meta->map.uid, reg->map_uid); -return -EINVAL ; - } - } - meta->map.ptr = reg->map_ptr; - meta->map.uid = reg->map_uid; + err =process_map_ptr_arg (env, reg, argno, meta); +if (err) +return err;break ;case ARG_PTR_TO_MAP_KEY :/* bpf_map_xxx(..., map_ptr, ..., key) call:
@@ -12122,36 +12140,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me reg_arg_name(env, argno));return -EINVAL ; } -if (meta->map.ptr && (reg->map_ptr->record->wq_off >=0 || - reg->map_ptr->record->task_work_off >=0 )) { -/* Use map_uid (which is unique id of inner map) to reject: -* inner_map1 = bpf_map_lookup_elem(outer_map, key1) -* inner_map2 = bpf_map_lookup_elem(outer_map, key2) -* if (inner_map1 && inner_map2) { -* wq = bpf_map_lookup_elem(inner_map1); -* if (wq) -* // mismatch would have been allowed -* bpf_wq_init(wq, inner_map2); -* } -* -* Comparing map_ptr is enough to distinguish normal and outer maps. -*/ -if (meta->map.ptr != reg->map_ptr || - meta->map.uid != reg->map_uid) { -if (reg->map_ptr->record->task_work_off >=0 ) { -verbose (env, -"bpf_task_work pointer in R2 map_uid=%d doesn't match map pointer in R3 map_uid=%d\n" , - meta->map.uid, reg->map_uid); -return -EINVAL ; - } -verbose (env, -"workqueue pointer in R1 map_uid=%d doesn't match map pointer in R2 map_uid=%d\n" , - meta->map.uid, reg->map_uid); -return -EINVAL ; - } - } - meta->map.ptr = reg->map_ptr; - meta->map.uid = reg->map_uid; + ret =process_map_ptr_arg (env, reg, argno, meta); +if (ret <0 ) +return ret; fallthrough;case KF_ARG_PTR_TO_ALLOC_BTF_ID :case KF_ARG_PTR_TO_BTF_ID :
Signature · 1 line
2.52.0
PATCH v2 3/18 bpf: Split kfunc map argument into __const_map and __map
In reply to Amery HungKfuncs used a single '__map' suffix (KF_ARG_PTR_TO_MAP) for two different
things: a verifier-known map matched by map_uid against a bound timer/wq/task_work object (bpf_wq_init, bpf_task_work_schedule*), and an opaque 'struct bpf_map *' used only at runtime (bpf_arena_*), which may be a map fd or a PTR_TO_BTF_ID struct bpf_map (e.g. a bpf_map iterator's ctx->map).
That combined path only accepted the btf map form due to type confusion. The 'if (!reg->map_ptr)' check reads reg->map_ptr, which aliases reg->btf in the bpf_reg_state union. A PTR_TO_BTF_ID register always has a non-NULL reg->btf, so the guard silently passed and validation fell through to process_kf_arg_ptr_to_btf_id(). It also recorded PTR_TO_BTF_ID info in meta->map, which would be meaningless.
Split the annotation to avoid such type confusion and to align with helper:
- '__const_map' -> KF_ARG_CONST_MAP_PTR: verifier-known map, handled by process_map_ptr_arg() like helper ARG_CONST_MAP_PTR.
- '__map' -> KF_ARG_PTR_TO_BTF_ID: opaque struct bpf_map, validated by process_kf_arg_ptr_to_btf_id(). A map fd still matches via reg2btf_ids[CONST_PTR_TO_MAP], so bpf_arena_alloc_pages(&map) keeps working.
--- Documentation/bpf/kfuncs.rst | 30 +++++++++++++++++++++++- kernel/bpf/helpers.c | 16 ++++++------- kernel/bpf/verifier.c | 45 +++++++++++++++++++++--------------- 3 files changed, 64 insertions(+), 27 deletions(-)
Documentation/bpf/kfuncs.rst +29 -1
@@ -250,6 +250,34 @@ Or:: ... } +2.3.7 __const_map and __map Annotations +--------------------------------------- + +These annotations are used for ``struct bpf_map *`` arguments and distinguish a +verifier-known map from an opaque one. + +``__const_map`` indicates a map must be known at the verification time, i.e. a +concrete map fd the BPF program references directly. + +An example is given below:: + + __bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, + unsigned int flags) + { + ... + } + +``__map`` indicates an opaque ``struct bpf_map *`` that may be resolved +at run time. The argument may take either a map fd or a ``PTR_TO_BTF_ID`` +``struct bpf_map`` pointer. + +An example is given below:: + + __bpf_kfunc void *bpf_arena_alloc_pages(void *p__map, ...) + { + ... + } + .. _BPF_kfunc_nodef: 2.4 Using an existing kernel function
@@ -411,7 +439,7 @@ Example declaration: .. code-block:: c __bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, - void *map__map, bpf_task_work_callback_t callback, + void *map__const_map, bpf_task_work_callback_t callback, struct bpf_prog_aux *aux) { ... } Example usage in BPF program:
kernel/bpf/helpers.c +8 -8
@@ -3404,10 +3404,10 @@ __bpf_kfunc void bpf_throw(u64 cookie)WARN (1 ,"A call to BPF exception callback should never return\n" ); } -__bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__map, unsigned int flags) +__bpf_kfunc int bpf_wq_init(struct bpf_wq *wq, void *p__const_map, unsigned int flags) {struct bpf_async_kern *async = (struct bpf_async_kern *)wq; -struct bpf_map *map = p__map; +struct bpf_map *map = p__const_map;BUILD_BUG_ON (sizeof (struct bpf_async_kern ) >sizeof (struct bpf_wq ));BUILD_BUG_ON (__alignof__(struct bpf_async_kern ) != __alignof__(struct bpf_wq ));
@@ -4642,17 +4642,17 @@ static int bpf_task_work_schedule(struct task_struct *task, struct bpf_task_work* mode * @task: Task struct for which callback should be scheduled * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping -* @map__map: bpf_map that embeds struct bpf_task_work in the values +* @map__const_map: bpf_map that embeds struct bpf_task_work in the values * @callback: pointer to BPF subprogram to call * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier * * Return: 0 if task work has been scheduled successfully, negative error code otherwise */ __bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, -void *map__map,bpf_task_work_callback_t callback, +void *map__const_map,bpf_task_work_callback_t callback,struct bpf_prog_aux *aux) { -return bpf_task_work_schedule (task, tw, map__map, callback, aux,TWA_SIGNAL ); +return bpf_task_work_schedule (task, tw, map__const_map, callback, aux,TWA_SIGNAL ); }/**
@@ -4660,17 +4660,17 @@ __bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct b* mode * @task: Task struct for which callback should be scheduled * @tw: Pointer to struct bpf_task_work in BPF map value for internal bookkeeping -* @map__map: bpf_map that embeds struct bpf_task_work in the values +* @map__const_map: bpf_map that embeds struct bpf_task_work in the values * @callback: pointer to BPF subprogram to call * @aux: pointer to bpf_prog_aux of the caller BPF program, implicitly set by the verifier * * Return: 0 if task work has been scheduled successfully, negative error code otherwise */ __bpf_kfunc int bpf_task_work_schedule_resume(struct task_struct *task, struct bpf_task_work *tw, -void *map__map,bpf_task_work_callback_t callback, +void *map__const_map,bpf_task_work_callback_t callback,struct bpf_prog_aux *aux) { -return bpf_task_work_schedule (task, tw, map__map, callback, aux,TWA_RESUME ); +return bpf_task_work_schedule (task, tw, map__const_map, callback, aux,TWA_RESUME ); } static int make_file_dynptr(struct file *file, u32 flags, bool may_sleep,
kernel/bpf/verifier.c +27 -18
85 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v2 4/18 bpf: Pass kfunc meta to mem and mem_size check
In reply to Amery Hungkfunc now shares the same bpf_call_arg_meta with helpers. Pass kfunc's own meta to check_mem_reg() and check_kfunc_mem_size() instead of NULL or a temporary meta on the stack.
--- kernel/bpf/verifier.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
kernel/bpf/verifier.c +10 -12
@@ -6924,7 +6924,7 @@ static int check_mem_size_reg(struct bpf_verifier_env *env, }static int check_mem_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, -argno_t argno,u32 mem_size) +argno_t argno,u32 mem_size,struct bpf_call_arg_meta *meta) {bool may_be_null =type_may_be_null (reg->type);struct bpf_reg_state saved_reg;
@@ -6950,8 +6950,8 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regint size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; - err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true,NULL ); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true,NULL ); + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); + err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);if (may_be_null) *reg = saved_reg;
@@ -6994,22 +6994,20 @@ static int process_const_alloc_mem_size(struct bpf_verifier_env *env, struct bpf }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) +struct bpf_reg_state *size_reg,argno_t mem_argno, +argno_t size_argno,struct bpf_call_arg_meta *meta) {bool may_be_null =type_may_be_null (mem_reg->type);struct bpf_reg_state saved_reg; -struct bpf_call_arg_meta meta;int err; -memset (&meta,0 ,sizeof (meta)); -if (may_be_null) { saved_reg = *mem_reg;mark_ptr_not_null_reg (mem_reg); } - err =check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_READ , true, &meta); - err = err ?:check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_WRITE , true, &meta); + err =check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_READ , true, meta); + err = err ?:check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_WRITE , true, meta);if (may_be_null) *mem_reg = saved_reg;
@@ -9258,7 +9256,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, ret =check_func_arg_reg_off (env, reg, argno,ARG_DONTCARE );if (ret <0 )return ret; -if (check_mem_reg (env, reg, argno, arg->mem_size)) +if (check_mem_reg (env, reg, argno, arg->mem_size,NULL ))return -EINVAL ;if (!(arg->arg_type &PTR_MAYBE_NULL ) && (reg->type &PTR_MAYBE_NULL )) { bpf_log(log, "%s is expected to be non-NULL\n",
@@ -12404,7 +12402,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_tname, PTR_ERR(resolve_ret));return -EINVAL ; } - ret =check_mem_reg (env, reg, argno, type_size); + ret =check_mem_reg (env, reg, argno, type_size, meta);if (ret <0 )return ret;break ;
@@ -12418,7 +12416,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meif (!bpf_register_is_null (buff_reg) || !is_kfunc_arg_nullable (meta->btf, buff_arg)) { ret =check_kfunc_mem_size_reg (env, buff_reg, size_reg, - argno, next_argno); + argno, next_argno, meta);if (ret <0 ) {verbose (env,"%s and " ,reg_arg_name (env, argno)); verbose(env, "%s memory, len pair leads to invalid memory access\n",
Signature · 1 line
2.52.0
PATCH v2 5/18 bpf: Check helper and kfunc mem+size arguments identically
In reply to Amery HungHelper ARG_CONST_SIZE and kfunc KF_ARG_PTR_TO_MEM_SIZE memory arguments already share check_mem_size_reg(), but only the kfunc wrapper check_kfunc_mem_size_reg() ran mark_ptr_not_null_reg() on a possibly-NULL pointer before the access check. That call does more than clearingPTR_MAYBE_NULL: it converts special map-value-or-null registers to their real non-NULL type, e.g. a map-in-map value to CONST_PTR_TO_MAP. Without it the register keeps base type PTR_TO_MAP_VALUE and check_map_access() treats it as raw data.
Because the helper path skipped that step, an un-narrowed map-of-maps value could be passed to a nullable helper mem argument (e.g. bpf_csum_diff()) and read as bytes, leaking the inner map descriptor (a kernel pointer). The kfunc path already rejected the same program.
Handle the possibly-NULL pointer in the shared mem-argument checking so helper and kfunc arguments are verified the same way, and drop the now redundant check_kfunc_mem_size_reg() wrapper. Both paths now reject an un-narrowed map-in-map value used as memory.
--- kernel/bpf/verifier.c | 49 +++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 27 deletions(-)
kernel/bpf/verifier.c +22 -27
@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,bool zero_size_allowed, struct bpf_call_arg_meta *meta) { - int err; + bool may_be_null = type_may_be_null(mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }/* This is used to refine r0 return value bounds for helpers * that enforce this value as an upper bound on return values.
@@ -6910,8 +6917,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env, reg_arg_name(env, size_argno));return -EACCES ; } - err =check_helper_mem_access (env, mem_reg, mem_argno,reg_umax (size_reg), - access_type, zero_size_allowed, meta); + +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, mem_reg, mem_argno,reg_umax (size_reg), +BPF_READ , zero_size_allowed, meta); +if (!err && access_type &BPF_WRITE ) + err =check_helper_mem_access (env, mem_reg, mem_argno,reg_umax (size_reg), +BPF_WRITE , zero_size_allowed, meta); +if (!err) {int regno =reg_from_argno (size_argno);
@@ -6920,6 +6933,10 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,else err =mark_stack_arg_precision (env,arg_idx_from_argno (size_argno)); } + +if (may_be_null) + *mem_reg = saved_reg; +return err; }
@@ -6993,28 +7010,6 @@ static int process_const_alloc_mem_size(struct bpf_verifier_env *env, struct bpfreturn 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,struct bpf_call_arg_meta *meta) -{ -bool may_be_null =type_may_be_null (mem_reg->type); -struct bpf_reg_state saved_reg; -int err; - -if (may_be_null) { - saved_reg = *mem_reg; -mark_ptr_not_null_reg (mem_reg); - } - - err =check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_READ , true, meta); - err = err ?:check_mem_size_reg (env, mem_reg, size_reg, mem_argno, size_argno,BPF_WRITE , true, meta); - -if (may_be_null) - *mem_reg = saved_reg; - -return err; -} -enum {PROCESS_SPIN_LOCK = (1 <<0 ),PROCESS_RES_LOCK = (1 <<1 ),
@@ -12415,8 +12410,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meargno_t next_argno =argno_from_arg (i +2 );if (!bpf_register_is_null (buff_reg) || !is_kfunc_arg_nullable (meta->btf, buff_arg)) { - ret =check_kfunc_mem_size_reg (env, buff_reg, size_reg, - argno, next_argno, meta); + ret =check_mem_size_reg (env, buff_reg, size_reg, argno, next_argno, +BPF_READ |BPF_WRITE , true, meta);if (ret <0 ) {verbose (env,"%s and " ,reg_arg_name (env, argno)); verbose(env, "%s memory, len pair leads to invalid memory access\n",
Signature · 1 line
2.52.0
PATCH v2 6/18 selftests/bpf: Add tests for helper and kfunc mem+size arguments
In reply to Amery HungMake sure the return of map-of-maps lookup cannot be passed to nullable memory buffer argument for helper and kfunc.
- mapofmaps_value_as_kfunc_mem_buf: an un-narrowed (possibly-NULL) map-of-maps value must not be usable as bpf_dynptr_slice()'s __nullable buffer. mark_ptr_not_null_reg() converts such a value to CONST_PTR_TO_MAP; without it check_map_access() would let the program read the inner map descriptor as raw bytes.
- mapofmaps_value_as_helper_mem_buf: the same map-of-maps value passed to a nullable helper mem argument (bpf_csum_diff()) must be rejected too, guarding that helper and kfunc arguments are checked the same way.
--- .../selftests/bpf/prog_tests/verifier.c | 2 + .../bpf/progs/verifier_mem_size_reg.c | 60 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 tools/testing/selftests/bpf/progs/verifier_mem_size_reg.c
tools/testing/selftests/bpf/prog_tests/verifier.c +2 -0
@@ -67,6 +67,7 @@#include "verifier_masking.skel.h" #include "verifier_may_goto_1.skel.h" #include "verifier_may_goto_2.skel.h" +#include "verifier_mem_size_reg.skel.h" #include "verifier_meta_access.skel.h" #include "verifier_movsx.skel.h" #include "verifier_mtu.skel.h"
@@ -221,6 +222,7 @@ void test_verifier_map_ret_val(void) { RUN(verifier_map_ret_val); }void test_verifier_masking (void ) {RUN (verifier_masking); }void test_verifier_may_goto_1 (void ) {RUN (verifier_may_goto_1); }void test_verifier_may_goto_2 (void ) {RUN (verifier_may_goto_2); } +void test_verifier_mem_size_reg (void ) {RUN (verifier_mem_size_reg); }void test_verifier_meta_access (void ) {RUN (verifier_meta_access); }void test_verifier_movsx (void ) {RUN (verifier_movsx); }void test_verifier_mul (void ) {RUN (verifier_mul); }
tools/testing/selftests/bpf/progs/verifier_mem_size_reg.c new file +60 -0
60 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v2 7/18 bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to Amery HungFixed-size memory arguments went through two paths: helpers called check_helper_mem_access() directly, while kfuncs and global subprogs used check_mem_reg(). Route the helper MEM_FIXED_SIZE case through check_mem_reg() too so all three share the same check.
This also fixes a bug in the helper path. When passing a NULL to PTR_MAYBE_NULL | ARG_PTR_TO_FIXED_SIZE_MEM argument, the program would be falsely rejected by check_helper_mem_access(). This is not triggerable since there is no such kind of helper. Also, note that check_reg_type() still make sure NULL cannot be passed to an argument not marked with PTR_MAYBE_NULL.
--- kernel/bpf/verifier.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
kernel/bpf/verifier.c +14 -10
@@ -6941,11 +6941,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env, }static int check_mem_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, -argno_t argno,u32 mem_size,struct bpf_call_arg_meta *meta) +argno_t argno,u32 mem_size,enum bpf_access_type access_type, +struct bpf_call_arg_meta *meta) { +/* meta->btf is set only for kfuncs; a NULL meta means a global subprog. */ +bool helper = meta && !meta->btf;bool may_be_null =type_may_be_null (reg->type);struct bpf_reg_state saved_reg; -int err; +int size, err =0 ;if (bpf_register_is_null (reg))return 0 ;
@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regmark_ptr_not_null_reg (reg); } -int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size; - err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);if (may_be_null) *reg = saved_reg;
@@ -8477,9 +8482,8 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,* next is_mem_size argument below. */ if (arg_type &MEM_FIXED_SIZE ) { - err =check_helper_mem_access (env, reg, argno, fn->arg_size[arg], - arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , - false, meta); + err =check_mem_reg (env, reg,argno_from_reg (regno), fn->arg_size[arg], + arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , meta);if (err)return err;if (arg_type &MEM_ALIGNED )
@@ -9251,7 +9255,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, ret =check_func_arg_reg_off (env, reg, argno,ARG_DONTCARE );if (ret <0 )return ret; -if (check_mem_reg (env, reg, argno, arg->mem_size,NULL )) +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 )) { bpf_log(log, "%s is expected to be non-NULL\n",
@@ -12397,7 +12401,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_tname, PTR_ERR(resolve_ret));return -EINVAL ; } - ret =check_mem_reg (env, reg, argno, type_size, meta); + ret =check_mem_reg (env, reg, argno, type_size,BPF_READ |BPF_WRITE , meta);if (ret <0 )return ret;break ;
Signature · 1 line
2.52.0
PATCH v2 8/18 bpf: Express ARG_CONST_SIZE_OR_ZERO as ARG_CONST_SIZE | SCALAR_MAYBE_ZERO
In reply to Amery HungAdd SCALAR_MAYBE_ZERO as an alias of PTR_MAYBE_NULL for scalar size arguments, and define ARG_CONST_SIZE_OR_ZERO as ARG_CONST_SIZE | SCALAR_MAYBE_ZERO instead of a distinct base type. This lets check_func_arg() handle both with a single ARG_CONST_SIZE case, and the redundant compatible_reg_types[] entry is dropped.
--- include/linux/bpf.h | 4 +++- kernel/bpf/verifier.c | 12 ++---------- 2 files changed, 5 insertions(+), 11 deletions(-)
include/linux/bpf.h +3 -1
@@ -748,6 +748,7 @@ extern const struct bpf_map_ops bpf_map_offload_ops;enum bpf_type_flag {/* PTR may be NULL. */ PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), +SCALAR_MAYBE_ZERO =PTR_MAYBE_NULL ,/* MEM is read-only. When applied on bpf_arg, it indicates the arg is * compatible with both mutable and immutable memory.
@@ -889,7 +890,6 @@ enum bpf_arg_type {ARG_PTR_TO_ARENA ,ARG_CONST_SIZE ,/* number of bytes accessed from memory */ -ARG_CONST_SIZE_OR_ZERO ,/* number of bytes accessed from memory or 0 */ ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING, /* any (initialized) argument is ok */
@@ -922,6 +922,8 @@ enum bpf_arg_type {ARG_PTR_TO_UNINIT_MEM =MEM_UNINIT |MEM_WRITE |ARG_PTR_TO_MEM ,/* Pointer to valid memory of size known at compile time. */ ARG_PTR_TO_FIXED_SIZE_MEM =MEM_FIXED_SIZE |ARG_PTR_TO_MEM , +/* Number of bytes accessed from memory, or 0. */ + ARG_CONST_SIZE_OR_ZERO = SCALAR_MAYBE_ZERO | ARG_CONST_SIZE,/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag.
kernel/bpf/verifier.c +2 -10
@@ -7724,8 +7724,7 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,static bool arg_type_is_mem_size (enum bpf_arg_type type) { -return type ==ARG_CONST_SIZE || - type ==ARG_CONST_SIZE_OR_ZERO ; +return base_type (type) ==ARG_CONST_SIZE ; }static bool arg_type_is_raw_mem (enum bpf_arg_type type)
@@ -7872,7 +7871,6 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_MAP_KEY] = &mem_types, [ARG_PTR_TO_MAP_VALUE] = &mem_types, [ARG_CONST_SIZE] = &scalar_types, - [ARG_CONST_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_ALLOC_SIZE_OR_ZERO] = &scalar_types, [ARG_CONST_MAP_PTR] = &const_map_ptr_types, [ARG_PTR_TO_CTX] = &context_types,
@@ -8494,13 +8492,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg,argno_from_reg (regno -1 ), argno, fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ , - false, meta); -break ; -case ARG_CONST_SIZE_OR_ZERO : - err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg, -argno_from_reg (regno -1 ), argno, - fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ , - true, meta); + arg_type &SCALAR_MAYBE_ZERO , meta);break ;case ARG_PTR_TO_DYNPTR : err = process_dynptr_func(env, reg, argno, insn_idx, arg_type, &meta->ref_obj,
Signature · 1 line
2.52.0
PATCH v2 9/18 bpf: Rename ARG_CONST_SIZE{,_OR_ZERO} to ARG_MEM_SIZE{,_OR_ZERO}
In reply to Amery HungARG_CONST_SIZE does not require a constant: check_mem_size_reg() accepts any bounded scalar and verifies the memory access against its maximum (reg_umax). Rename ARG_CONST_SIZE and ARG_CONST_SIZE_OR_ZERO to ARG_MEM_SIZE and ARG_MEM_SIZE_OR_ZERO to reflect that. ARG_CONST_ALLOC_ SIZE_OR_ZERO, which does require a constant, is left unchanged.
Pure rename, no functional change.
--- include/linux/bpf.h | 4 +- include/linux/bpf_verifier.h | 2 +- kernel/bpf/backtrack.c | 2 +- kernel/bpf/bpf_lsm.c | 4 +- kernel/bpf/btf.c | 2 +- kernel/bpf/cgroup.c | 8 +- kernel/bpf/helpers.c | 26 ++-- kernel/bpf/ringbuf.c | 2 +- kernel/bpf/stackmap.c | 10 +- kernel/bpf/syscall.c | 4 +- kernel/bpf/verifier.c | 6 +- kernel/trace/bpf_trace.c | 62 +++++----- net/core/filter.c | 116 +++++++++--------- .../bpf/progs/mem_rdonly_untrusted.c | 2 +- .../selftests/bpf/progs/verifier_bounds.c | 2 +- .../progs/verifier_helper_access_var_len.c | 6 +- .../bpf/progs/verifier_helper_value_access.c | 2 +- 17 files changed, 130 insertions(+), 130 deletions(-)
include/linux/bpf.h +2 -2
@@ -889,7 +889,7 @@ enum bpf_arg_type {ARG_PTR_TO_MEM ,/* pointer to valid memory (stack, packet, map value) */ ARG_PTR_TO_ARENA , -ARG_CONST_SIZE ,/* number of bytes accessed from memory */ +ARG_MEM_SIZE ,/* number of bytes accessed from memory */ ARG_PTR_TO_CTX ,/* pointer to context */ ARG_ANYTHING, /* any (initialized) argument is ok */
@@ -923,7 +923,7 @@ enum bpf_arg_type {/* Pointer to valid memory of size known at compile time. */ ARG_PTR_TO_FIXED_SIZE_MEM =MEM_FIXED_SIZE |ARG_PTR_TO_MEM ,/* Number of bytes accessed from memory, or 0. */ - ARG_CONST_SIZE_OR_ZERO = SCALAR_MAYBE_ZERO | ARG_CONST_SIZE, + ARG_MEM_SIZE_OR_ZERO = SCALAR_MAYBE_ZERO | ARG_MEM_SIZE,/* This must be the last entry. Its purpose is to ensure the enum is * wide enough to hold the higher bits reserved for bpf_type_flag.
include/linux/bpf_verifier.h +1 -1
@@ -15,7 +15,7 @@* ensures that umax_value + (int)off + (int)size cannot overflow a u64. */ #define BPF_MAX_VAR_OFF (1 << 29) -/* Maximum variable size permitted for ARG_CONST_SIZE[_OR_ZERO]. This ensures +/* Maximum variable size permitted for ARG_MEM_SIZE[_OR_ZERO]. This ensures * that converting umax_value to int cannot overflow. */ #define BPF_MAX_VAR_SIZ (1 << 29)
kernel/bpf/backtrack.c +1 -1
@@ -636,7 +636,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* r5 += 1 * ... * call bpf_perf_event_output#25 -* where .arg5_type = ARG_CONST_SIZE_OR_ZERO +* where .arg5_type = ARG_MEM_SIZE_OR_ZERO * * and this case: * r6 = 1
kernel/bpf/bpf_lsm.c +2 -2
@@ -186,7 +186,7 @@ static const struct bpf_func_proto bpf_ima_inode_hash_proto = { .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &bpf_ima_inode_hash_btf_ids[0], .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, .allowed = bpf_ima_inode_hash_allowed, };
@@ -205,7 +205,7 @@ static const struct bpf_func_proto bpf_ima_file_hash_proto = { .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &bpf_ima_file_hash_btf_ids[0], .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, .allowed = bpf_ima_inode_hash_allowed, };
kernel/bpf/btf.c +1 -1
@@ -8709,7 +8709,7 @@ const struct bpf_func_proto bpf_btf_find_by_name_kind_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg2_type = ARG_CONST_SIZE, + .arg2_type = ARG_MEM_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_ANYTHING, };
kernel/bpf/cgroup.c +4 -4
@@ -2305,7 +2305,7 @@ static const struct bpf_func_proto bpf_sysctl_get_name_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_MEM | MEM_WRITE, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, .arg4_type = ARG_ANYTHING, };
@@ -2347,7 +2347,7 @@ static const struct bpf_func_proto bpf_sysctl_get_current_value_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, }; BPF_CALL_3(bpf_sysctl_get_new_value, struct bpf_sysctl_kern *, ctx, char *, buf,
@@ -2367,7 +2367,7 @@ static const struct bpf_func_proto bpf_sysctl_get_new_value_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, }; BPF_CALL_3(bpf_sysctl_set_new_value, struct bpf_sysctl_kern *, ctx,
@@ -2393,7 +2393,7 @@ static const struct bpf_func_proto bpf_sysctl_set_new_value_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, }; static const struct bpf_func_proto *
kernel/bpf/helpers.c +13 -13
@@ -278,7 +278,7 @@ const struct bpf_func_proto bpf_get_current_comm_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_UNINIT_MEM, - .arg2_type = ARG_CONST_SIZE, + .arg2_type = ARG_MEM_SIZE, };#if defined(CONFIG_QUEUED_SPINLOCKS ) || defined(CONFIG_BPF_ARCH_SPINLOCK )
@@ -539,7 +539,7 @@ const struct bpf_func_proto bpf_strtol_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg2_type = ARG_CONST_SIZE, + .arg2_type = ARG_MEM_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, .arg4_size = sizeof(s64),
@@ -567,7 +567,7 @@ const struct bpf_func_proto bpf_strtoul_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg2_type = ARG_CONST_SIZE, + .arg2_type = ARG_MEM_SIZE, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, .arg4_size = sizeof(u64),
@@ -583,7 +583,7 @@ static const struct bpf_func_proto bpf_strncmp_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg2_type = ARG_CONST_SIZE, + .arg2_type = ARG_MEM_SIZE, .arg3_type = ARG_PTR_TO_CONST_STR, };
@@ -627,7 +627,7 @@ const struct bpf_func_proto bpf_get_ns_current_pid_tgid_proto = { .arg1_type = ARG_ANYTHING, .arg2_type = ARG_ANYTHING, .arg3_type = ARG_PTR_TO_UNINIT_MEM, - .arg4_type = ARG_CONST_SIZE, + .arg4_type = ARG_MEM_SIZE, };static const struct bpf_func_proto bpf_get_raw_smp_processor_id_proto = {
@@ -653,7 +653,7 @@ const struct bpf_func_proto bpf_event_output_data_proto = { .arg2_type = ARG_CONST_MAP_PTR, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg5_type = ARG_CONST_SIZE_OR_ZERO, + .arg5_type = ARG_MEM_SIZE_OR_ZERO, }; BPF_CALL_3(bpf_copy_from_user, void *, dst, u32, size,
@@ -675,7 +675,7 @@ const struct bpf_func_proto bpf_copy_from_user_proto = { .might_sleep = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_UNINIT_MEM, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, };
@@ -706,7 +706,7 @@ const struct bpf_func_proto bpf_copy_from_user_task_proto = { .might_sleep = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_UNINIT_MEM, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_BTF_ID, .arg4_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK],
@@ -1093,10 +1093,10 @@ const struct bpf_func_proto bpf_snprintf_proto = { .gpl_only = true, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM_OR_NULL | MEM_WRITE, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_PTR_TO_CONST_STR, .arg4_type = ARG_PTR_TO_MEM | PTR_MAYBE_NULL | MEM_RDONLY, - .arg5_type = ARG_CONST_SIZE_OR_ZERO, + .arg5_type = ARG_MEM_SIZE_OR_ZERO, };static void *map_key_from_value (struct bpf_map *map,void *value,u32 *arr_idx)
@@ -1888,7 +1888,7 @@ static const struct bpf_func_proto bpf_dynptr_from_mem_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_UNINIT_MEM, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_DYNPTR | DYNPTR_TYPE_LOCAL | MEM_UNINIT | MEM_WRITE, };
@@ -1943,7 +1943,7 @@ static const struct bpf_func_proto bpf_dynptr_read_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_UNINIT_MEM, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_PTR_TO_DYNPTR, .arg4_type = ARG_ANYTHING, .arg5_type = ARG_ANYTHING,
@@ -2004,7 +2004,7 @@ static const struct bpf_func_proto bpf_dynptr_write_proto = { .arg1_type = ARG_PTR_TO_DYNPTR, .arg2_type = ARG_ANYTHING, .arg3_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg4_type = ARG_CONST_SIZE_OR_ZERO, + .arg4_type = ARG_MEM_SIZE_OR_ZERO, .arg5_type = ARG_ANYTHING, };
kernel/bpf/ringbuf.c +1 -1
@@ -634,7 +634,7 @@ const struct bpf_func_proto bpf_ringbuf_output_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_CONST_MAP_PTR, .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
kernel/bpf/stackmap.c +5 -5
@@ -781,7 +781,7 @@ const struct bpf_func_proto bpf_get_stack_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -797,7 +797,7 @@ const struct bpf_func_proto bpf_get_stack_sleepable_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -831,7 +831,7 @@ const struct bpf_func_proto bpf_get_task_stack_proto = { .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -848,7 +848,7 @@ const struct bpf_func_proto bpf_get_task_stack_sleepable_proto = { .arg1_type = ARG_PTR_TO_BTF_ID, .arg1_btf_id = &btf_tracing_ids[BTF_TRACING_TYPE_TASK], .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
@@ -911,7 +911,7 @@ const struct bpf_func_proto bpf_get_stack_proto_pe = { .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_CTX, .arg2_type = ARG_PTR_TO_UNINIT_MEM, - .arg3_type = ARG_CONST_SIZE_OR_ZERO, + .arg3_type = ARG_MEM_SIZE_OR_ZERO, .arg4_type = ARG_ANYTHING, };
kernel/bpf/syscall.c +2 -2
@@ -6544,7 +6544,7 @@ static const struct bpf_func_proto bpf_sys_bpf_proto = { .ret_type = RET_INTEGER, .arg1_type = ARG_ANYTHING, .arg2_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg3_type = ARG_CONST_SIZE, + .arg3_type = ARG_MEM_SIZE, }; const struct bpf_func_proto * __weak
@@ -6591,7 +6591,7 @@ static const struct bpf_func_proto bpf_kallsyms_lookup_name_proto = { .gpl_only = false, .ret_type = RET_INTEGER, .arg1_type = ARG_PTR_TO_MEM | MEM_RDONLY, - .arg2_type = ARG_CONST_SIZE_OR_ZERO, + .arg2_type = ARG_MEM_SIZE_OR_ZERO, .arg3_type = ARG_ANYTHING, .arg4_type = ARG_PTR_TO_FIXED_SIZE_MEM | MEM_UNINIT | MEM_WRITE | MEM_ALIGNED, .arg4_size = sizeof(u64),
kernel/bpf/verifier.c +3 -3
@@ -7724,7 +7724,7 @@ static int process_iter_next_call(struct bpf_verifier_env *env, int insn_idx,static bool arg_type_is_mem_size (enum bpf_arg_type type) { -return base_type (type) ==ARG_CONST_SIZE ; +return base_type (type) ==ARG_MEM_SIZE ; }static bool arg_type_is_raw_mem (enum bpf_arg_type type)
@@ -7870,7 +7870,7 @@ static const struct bpf_reg_types dynptr_types = {static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_MAP_KEY ] = &mem_types, [ARG_PTR_TO_MAP_VALUE ] = &mem_types, - [ARG_CONST_SIZE ] = &scalar_types, + [ARG_MEM_SIZE ] = &scalar_types, [ARG_CONST_ALLOC_SIZE_OR_ZERO ] = &scalar_types, [ARG_CONST_MAP_PTR ] = &const_map_ptr_types, [ARG_PTR_TO_CTX ] = &context_types,
@@ -8488,7 +8488,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, err =check_ptr_alignment (env, reg,0 , fn->arg_size[arg], true); }break ; -case ARG_CONST_SIZE : +case ARG_MEM_SIZE : err = check_mem_size_reg(env, reg_state(env, regno - 1), reg, argno_from_reg(regno - 1), argno, fn->arg_type[arg - 1] & MEM_WRITE ? BPF_WRITE : BPF_READ,
kernel/trace/bpf_trace.c +31 -31
203 lines not on this page · raw message
net/core/filter.c +58 -58
449 lines not on this page · raw message
tools/testing/selftests/bpf/progs/mem_rdonly_untrusted.c +1 -1
@@ -137,7 +137,7 @@ int helper_param_not_ok(void *ctx) p =bpf_rdonly_cast (0 ,0 );/* -* Any helper with ARG_CONST_SIZE_OR_ZERO constraint will do, +* Any helper with ARG_MEM_SIZE_OR_ZERO constraint will do, * the most permissive constraint */ bpf_copy_from_user (p,0 , (void *)42 );
tools/testing/selftests/bpf/progs/verifier_bounds.c +1 -1
@@ -1195,7 +1195,7 @@ l0_%=: r1 = r6; \ r3 +=-8 ; \ r5 =0 ; \/* The 4th argument of bpf_skb_store_bytes is defined as \ -* ARG_CONST_SIZE, so 0 is not allowed. The 'r4 != 0' \ +* ARG_MEM_SIZE, so 0 is not allowed. The 'r4 != 0' \ * is providing us this exclusion of zero from initial \ * [0, 7] range. \ */ \
tools/testing/selftests/bpf/progs/verifier_helper_access_var_len.c +3 -3
@@ -85,7 +85,7 @@ __naked void stack_bitwise_and_zero_included(void) r2 +=-64 ; \ r4 =0 ; \/* Call bpf_ringbuf_output(), it is one of a few helper functions with\ -* ARG_CONST_SIZE_OR_ZERO parameter allowed in unpriv mode.\ +* ARG_MEM_SIZE_OR_ZERO parameter allowed in unpriv mode.\ * For unpriv this should signal an error, because memory at &fp[-64] is\ * not initialized. \ */ \
@@ -278,7 +278,7 @@ __naked void stack_jmp_no_min_check(void) r2 +=-64 ; \ r4 =0 ; \/* Call bpf_ringbuf_output(), it is one of a few helper functions with\ -* ARG_CONST_SIZE_OR_ZERO parameter allowed in unpriv mode.\ +* ARG_MEM_SIZE_OR_ZERO parameter allowed in unpriv mode.\ * For unpriv this should signal an error, because memory at &fp[-64] is\ * not initialized. \ */ \
@@ -778,7 +778,7 @@ __naked void variable_memory_8_bytes_leak(void) r3 +=1 ; \ r4 =0 ; \/* Call bpf_ringbuf_output(), it is one of a few helper functions with\ -* ARG_CONST_SIZE_OR_ZERO parameter allowed in unpriv mode.\ +* ARG_MEM_SIZE_OR_ZERO parameter allowed in unpriv mode.\ * For unpriv this should signal an error, because memory region [1, 64]\ * at &fp[-64] is not fully initialized. \ */ \
tools/testing/selftests/bpf/progs/verifier_helper_value_access.c +1 -1
@@ -91,7 +91,7 @@ l0_%=: exit; \/* Call a function taking a pointer and a size which doesn't allow the size to * be zero (i.e. bpf_trace_printk() declares the second argument to be -* ARG_CONST_SIZE, not ARG_CONST_SIZE_OR_ZERO). We attempt to pass zero for the +* ARG_MEM_SIZE, not ARG_MEM_SIZE_OR_ZERO). We attempt to pass zero for the * size and expect to fail. */ SEC ("tracepoint" )
Signature · 1 line
2.52.0
PATCH v2 10/18 bpf: Fold __szk const size handling into the scalar arg path
In reply to Amery HungTo align helper and kfunc pointer to memory argument handling, move kfunc constant memorry size argument handling to the kfunc scalar section. In addition, factor out constant scalar argument handling.
The constant size argument (__szk) of a kfunc memory/size pair was recorded into meta->arg_constant by a dedicated block in the KF_ARG_PTR_TO_MEM_SIZE case, duplicating the "only one constant argument" and "must be a known constant" checks already in the generic scalar argument handling. That block also did an explicit i++ to skip the size argument.
This also fixes a precision gap: the old dedicated block did not mark the size register precise, relying on check_mem_size_reg() for that. But check_mem_size_reg() is skipped when the buffer is a nullable arg passed as NULL (e.g. bpf_dynptr_slice(_rdwr) with a NULL buffer), so in that case the __szk value was recorded and used for regs[R0].mem_size without marking it precise. Routing the size through the scalar path marks it precise in all cases.
--- include/linux/bpf_verifier.h | 11 +++--- kernel/bpf/verifier.c | 66 +++++++++++++++++------------------- 2 files changed, 39 insertions(+), 38 deletions(-)
include/linux/bpf_verifier.h +7 -4
@@ -1479,6 +1479,12 @@ struct ret_mem_desc {bool found; }; +/* A constant scalar argument; Populated by process_const_arg() */ +struct arg_constant_desc { +u64 value; +bool found; +}; +struct bpf_call_arg_meta {/* Common */ struct btf *btf;
@@ -1496,10 +1502,7 @@ struct bpf_call_arg_meta {u32 kfunc_flags;const struct btf_type *func_proto;const char *func_name; -struct { -u64 value; -bool found; - } arg_constant; +struct arg_constant_desc arg_constant;/* arg_{btf,btf_id,owning_ref} are used by kfunc-specific handling, * generally to pass info about user-defined local kptr types to later
kernel/bpf/verifier.c +32 -34
92 lines not on this page · raw message
Signature · 1 line
2.52.0
PATCH v2 11/18 bpf: Classify kfunc mem_size args from BTF without register state
In reply to Amery Hungcheck_kfunc_args() already makes sure a scalar value is passed to a scalar kfunc argument. Drop the check in is_kfunc_arg_mem_size() and is_kfunc_arg_const_mem_size() to further decouple get_kfunc_ptr_arg_type() from register state (a prerequisite for generating a helper-like prototype from kfunc's BTF).
--- kernel/bpf/verifier.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-)
kernel/bpf/verifier.c +9 -11
@@ -10795,26 +10795,24 @@ static bool is_kfunc_rcu_protected(struct bpf_call_arg_meta *meta) }static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +const struct btf_param *arg) {const struct btf_type *t; t =btf_type_skip_modifiers (btf, arg->type,NULL ); -if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +if (!btf_type_is_scalar (t))return false;return btf_param_match_suffix (btf, arg,"__sz" ); }static bool is_kfunc_arg_const_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +const struct btf_param *arg) {const struct btf_type *t; t =btf_type_skip_modifiers (btf, arg->type,NULL ); -if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +if (!btf_type_is_scalar (t))return false;return btf_param_match_suffix (btf, arg,"__szk" );
@@ -11348,7 +11346,7 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta) } static enum kfunc_ptr_arg_type -get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_func_state *caller, +get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_reg_state *regs, struct bpf_call_arg_meta *meta, const struct btf_type *t, const struct btf_type *ref_t, const char *ref_tname, const struct btf_param *args,
@@ -11362,8 +11360,8 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_func_state *callreturn KF_ARG_PTR_TO_CTX ;if (arg +1 < nargs && - (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ],get_func_arg_reg (caller, regs, arg +1 )) || -is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ],get_func_arg_reg (caller, regs, arg +1 )))) + (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ]) || +is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ]))) arg_mem_size = true;/* In this function, we verify the kfunc's BTF as per the argument type,
@@ -12094,7 +12092,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me }if (is_kfunc_arg_constant (meta->btf, &args[i]) || -is_kfunc_arg_const_mem_size (meta->btf, &args[i], reg)) { +is_kfunc_arg_const_mem_size (meta->btf, &args[i])) { ret =process_const_arg (env, reg, argno, meta);if (ret <0 )return ret;
@@ -12139,7 +12137,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); ref_tname =btf_name_by_offset (btf, ref_t->name_off); - kf_arg_type =get_kfunc_ptr_arg_type (env, caller, regs, meta, t, ref_t, ref_tname, + kf_arg_type =get_kfunc_ptr_arg_type (env, regs, meta, t, ref_t, ref_tname, args, i, nargs, argno, reg);if (kf_arg_type <0 )return kf_arg_type;
Signature · 1 line
2.52.0
PATCH v2 12/18 bpf: Handle NULL kfunc pointer args without a KF_ARG_PTR_TO_NULL type
In reply to Amery Hungget_kfunc_ptr_arg_type() returned KF_ARG_PTR_TO_NULL when a nullable pointer argument was passed a NULL register. This folded a register-state decision (bpf_register_is_null()) into what is otherwise BTF-based argument classification, and it short-circuited before the BTF_ID/MEM resolution.
Drop KF_ARG_PTR_TO_NULL and handle the NULL case in check_kfunc_args()
instead: a nullable argument that is actually NULL is skipped. Note that it is okay to skip even when it is a mem+size pair because the size argument check has been moved to the scalar section. The skip is done before get_kfunc_ptr_arg_type() so that a NULL passed to a nullable non-scalar-struct argument is not newly rejected by the BTF_ID/MEM resolution.
No functional change.
--- kernel/bpf/verifier.c | 37 +++++++++++++++---------------------- 1 file changed, 15 insertions(+), 22 deletions(-)
kernel/bpf/verifier.c +15 -22
@@ -11082,7 +11082,6 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_CALLBACK ,KF_ARG_PTR_TO_RB_ROOT ,KF_ARG_PTR_TO_RB_NODE , -KF_ARG_PTR_TO_NULL ,KF_ARG_PTR_TO_CONST_STR ,KF_ARG_CONST_MAP_PTR , KF_ARG_PTR_TO_TIMER,
@@ -11359,11 +11358,6 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, meta->func_id == special_kfunc_list[KF_bpf_session_cookie])return KF_ARG_PTR_TO_CTX ; -if (arg +1 < nargs && - (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ]) || -is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ]))) - arg_mem_size = true; -/* In this function, we verify the kfunc's BTF as per the argument type, * leaving the rest of the verification with respect to the register * type to our caller. When a set of conditions hold in the BTF type of
@@ -11372,10 +11366,6 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (btf_is_prog_ctx_type (&env->log, meta->btf, t,resolve_prog_type (env->prog), arg))return KF_ARG_PTR_TO_CTX ; -if (is_kfunc_arg_nullable (meta->btf, &args[arg]) &&bpf_register_is_null (reg) && - !arg_mem_size) -return KF_ARG_PTR_TO_NULL ; -if (is_kfunc_arg_alloc_obj (meta->btf, &args[arg]))return KF_ARG_PTR_TO_ALLOC_BTF_ID ;
@@ -11437,6 +11427,11 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_callback (env, meta->btf, &args[arg]))return KF_ARG_PTR_TO_CALLBACK ; +if (arg +1 < nargs && + (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ]) || +is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ]))) + arg_mem_size = true; +/* This is the catch all argument type of register types supported by * check_helper_mem_access. However, we only allow when argument type is * pointer to scalar, or struct composed (recursively) of scalars. When
@@ -12137,6 +12132,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); ref_tname =btf_name_by_offset (btf, ref_t->name_off); +if (is_kfunc_arg_nullable (meta->btf, &args[i]) &&bpf_register_is_null (reg)) +continue ; + kf_arg_type =get_kfunc_ptr_arg_type (env, regs, meta, t, ref_t, ref_tname, args, i, nargs, argno, reg);if (kf_arg_type <0 )
@@ -12149,8 +12147,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me }switch (kf_arg_type) { -case KF_ARG_PTR_TO_NULL : -continue ;case KF_ARG_PTR_TO_ALLOC_BTF_ID :case KF_ARG_PTR_TO_BTF_ID :if (!is_trusted_reg (env, reg)) {
@@ -12414,19 +12410,16 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mecase KF_ARG_PTR_TO_MEM_SIZE : {struct bpf_reg_state *buff_reg = reg; -const struct btf_param *buff_arg = &args[i];struct bpf_reg_state *size_reg =get_func_arg_reg (caller, regs, i +1 );argno_t next_argno =argno_from_arg (i +2 ); -if (!bpf_register_is_null (buff_reg) || !is_kfunc_arg_nullable (meta->btf, buff_arg)) { - ret =check_mem_size_reg (env, buff_reg, size_reg, argno, next_argno, -BPF_READ |BPF_WRITE , true, meta); -if (ret <0 ) { -verbose (env,"%s and " ,reg_arg_name (env, argno)); -verbose (env,"%s memory, len pair leads to invalid memory access\n" , -reg_arg_name (env, next_argno)); -return ret; - } + ret =check_mem_size_reg (env, buff_reg, size_reg, argno, next_argno, +BPF_READ |BPF_WRITE , true, meta); +if (ret <0 ) { +verbose (env,"%s and " ,reg_arg_name (env, argno)); +verbose (env,"%s memory, len pair leads to invalid memory access\n" , +reg_arg_name (env, next_argno)); +return ret; }break ; }
Signature · 1 line
2.52.0
PATCH v2 13/18 bpf: Distinguish fixed- and variable-size kfunc mem args with MEM_FIXED_SIZE
In reply to Amery HungA kfunc memory-pointer argument comes in two flavors: a fixed-size buffer whose access size is derived from the pointed-to BTF type, and a variable-size buffer paired with a following __sz/__szk size argument. Both were represented by separate kfunc_ptr_arg_type values (KF_ARG_PTR_TO_MEM vs KF_ARG_PTR_TO_MEM_SIZE) with the pointer classified as the latter when a size argument followed.
Mirror how helpers describe the same distinction: classify both as KF_ARG_PTR_TO_MEM and OR in MEM_FIXED_SIZE for the fixed-size case, just as helpers use ARG_PTR_TO_MEM | MEM_FIXED_SIZE. The switches now key on base_type(kf_arg_type) so the flag rides along, and the KF_ARG_PTR_TO_MEM handler either resolves the size from BTF (MEM_FIXED_SIZE) or falls through to the mem/size-pair check, which validates the buffer against the following size register and skips it. No functional change.
--- kernel/bpf/verifier.c | 59 ++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 31 deletions(-)
kernel/bpf/verifier.c +28 -31
@@ -11078,7 +11078,6 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_LIST_NODE ,KF_ARG_PTR_TO_BTF_ID ,/* Also covers reg2btf_ids conversions */ KF_ARG_PTR_TO_MEM , -KF_ARG_PTR_TO_MEM_SIZE ,/* Size derived from next argument, skip it */ KF_ARG_PTR_TO_CALLBACK ,KF_ARG_PTR_TO_RB_ROOT , KF_ARG_PTR_TO_RB_NODE,
@@ -11344,7 +11343,7 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta)return meta->func_id == special_kfunc_list[KF_bpf_xdp_pull_data]; } -static enum kfunc_ptr_arg_type +static int get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_reg_state *regs, struct bpf_call_arg_meta *meta, const struct btf_type *t, const struct btf_type *ref_t,
@@ -11444,7 +11443,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : "");return -EINVAL ; } -return arg_mem_size ?KF_ARG_PTR_TO_MEM_SIZE :KF_ARG_PTR_TO_MEM ; +return arg_mem_size ?KF_ARG_PTR_TO_MEM :KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; } static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env,
@@ -12146,7 +12145,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_tname =btf_name_by_offset (btf, ref_t->name_off); } -switch (kf_arg_type) { +switch (base_type (kf_arg_type)) {case KF_ARG_PTR_TO_ALLOC_BTF_ID :case KF_ARG_PTR_TO_BTF_ID :if (!is_trusted_reg (env, reg)) {
@@ -12169,7 +12168,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mecase KF_ARG_PTR_TO_RB_ROOT :case KF_ARG_PTR_TO_RB_NODE :case KF_ARG_PTR_TO_MEM : -case KF_ARG_PTR_TO_MEM_SIZE :case KF_ARG_PTR_TO_CALLBACK :case KF_ARG_PTR_TO_CONST_STR :case KF_ARG_PTR_TO_WORKQUEUE :
@@ -12200,7 +12198,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meif (ret <0 )return ret; -switch (kf_arg_type) { +switch (base_type (kf_arg_type)) {case KF_ARG_PTR_TO_CTX :if (reg->type !=PTR_TO_CTX ) { verbose(env, "%s expected pointer to ctx, but got %s\n",
@@ -12396,33 +12394,32 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mereturn ret;break ;case KF_ARG_PTR_TO_MEM : - resolve_ret =btf_resolve_size (btf, ref_t, &type_size); -if (IS_ERR (resolve_ret)) { -verbose (env,"%s reference type('%s %s') size cannot be determined: %ld\n" , -reg_arg_name (env, argno),btf_type_str (ref_t), - ref_tname,PTR_ERR (resolve_ret)); -return -EINVAL ; - } - ret =check_mem_reg (env, reg, argno, type_size,BPF_READ |BPF_WRITE , meta); -if (ret <0 ) -return ret; -break ; -case KF_ARG_PTR_TO_MEM_SIZE : - { -struct bpf_reg_state *buff_reg = reg; -struct bpf_reg_state *size_reg =get_func_arg_reg (caller, regs, i +1 ); -argno_t next_argno =argno_from_arg (i +2 ); - - ret =check_mem_size_reg (env, buff_reg, size_reg, argno, next_argno, -BPF_READ |BPF_WRITE , true, meta); -if (ret <0 ) { -verbose (env,"%s and " ,reg_arg_name (env, argno)); -verbose (env,"%s memory, len pair leads to invalid memory access\n" , -reg_arg_name (env, next_argno)); -return ret; +if (kf_arg_type &MEM_FIXED_SIZE ) { + resolve_ret =btf_resolve_size (btf, ref_t, &type_size); +if (IS_ERR (resolve_ret)) { +verbose (env,"%s reference type('%s %s') size cannot be determined: %ld\n" , +reg_arg_name (env, argno),btf_type_str (ref_t), + ref_tname,PTR_ERR (resolve_ret)); +return -EINVAL ; + } + ret =check_mem_reg (env, reg, argno, type_size,BPF_READ |BPF_WRITE , meta); +if (ret <0 ) +return ret; + }else { +struct bpf_reg_state *buff_reg = reg; +struct bpf_reg_state *size_reg =get_func_arg_reg (caller, regs, i +1 ); +argno_t next_argno =argno_from_arg (i +2 ); + + ret =check_mem_size_reg (env, buff_reg, size_reg, argno, next_argno, +BPF_READ |BPF_WRITE , true, meta); +if (ret <0 ) { +verbose (env,"%s and " ,reg_arg_name (env, argno)); +verbose (env,"%s memory, len pair leads to invalid memory access\n" , +reg_arg_name (env, next_argno)); +return ret; + } }break ; - }case KF_ARG_PTR_TO_CALLBACK :if (reg->type !=PTR_TO_FUNC ) {verbose (env,"%s expected pointer to func\n" ,reg_arg_name (env, argno));
Signature · 1 line
2.52.0
PATCH v2 14/18 bpf: Check helper mem+size in ARG_PTR_TO_MEM case
In reply to Amery Hungcheck_func_arg() verified a variable-size memory argument across two
cases: ARG_PTR_TO_MEM was a no-op and the buffer+size pair was checked only when the following ARG_MEM_SIZE argument was reached. Handle the pair in the ARG_PTR_TO_MEM case instead, looking ahead to the size register at regno + 1, mirroring the kfunc handling.
check_func_proto() already rejects a non-MEM_FIXED_SIZE ARG_PTR_TO_MEM that is not paired with a following mem_size argument, so the size register is guaranteed to exist and ARG_MEM_SIZE needs no case of its own.
--- kernel/bpf/verifier.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
kernel/bpf/verifier.c +7 -9
@@ -8505,9 +8505,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, meta->subprogno = reg->subprogno;break ;case ARG_PTR_TO_MEM : -/* The access to this pointer is only checked when we hit the -* next is_mem_size argument below. -*/ if (arg_type &MEM_FIXED_SIZE ) { err =check_mem_reg (env, reg,argno_from_reg (regno), fn->arg_size[arg], arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , meta);
@@ -8515,14 +8512,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return err;if (arg_type &MEM_ALIGNED ) err =check_ptr_alignment (env, reg,0 , fn->arg_size[arg], true); + }else { +struct bpf_reg_state *size_reg =reg_state (env, regno +1 ); + + err =check_mem_size_reg (env, reg, size_reg, argno, +argno_from_reg (regno +1 ), + arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , + fn->arg_type[arg +1 ] &SCALAR_MAYBE_ZERO , meta); }break ; -case ARG_MEM_SIZE : - err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg, -argno_from_reg (regno -1 ), argno, - fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ , - arg_type &SCALAR_MAYBE_ZERO , meta); -break ;case ARG_PTR_TO_DYNPTR : err =process_dynptr_func (env, reg, argno, insn_idx, arg_type, &meta->ref_obj, &meta->dynptr);
Signature · 1 line
2.52.0
PATCH v2 15/18 bpf: Classify kfunc pointer arguments from BTF, resolve type against the register
In reply to Amery Hungget_kfunc_ptr_arg_type() decided part of a kfunc pointer argument's type from the caller's register: a PTR_TO_BTF_ID (or reg2btf_ids) register made the argument KF_ARG_PTR_TO_BTF_ID, otherwise it fell through to a memory buffer. Folding register state into argument classification prevents describing a kfunc's arguments from its BTF alone, which is a prerequisite for generating a helper-like prototype and eventually sharing the argument checking (check_func_arg()) between helpers and kfuncs.
Classify pointer arguments from BTF only, and resolve them against the register in check_kfunc_args():
- A pointer to a struct that is not paired with a __sz/__szk size argument is classified KF_ARG_PTR_TO_BTF_ID and then checked against the register. A register carrying a BTF ID (PTR_TO_BTF_ID or a reg2btf_ids type) must be referenced or trusted and is matched against the expected type. The only relaxation is when the struct is composed of scalars, the register may be verified as a fixed-size memory buffer sized from the BTF type; anything else is rejected.
- A pointer paired with a size argument is always a memory buffer and is never classified as BTF_ID, so the __sz/__szk case no longer detours through BTF_ID.
The referenced-or-trusted check thus moves into the KF_ARG_PTR_TO_BTF_ID resolution, alongside the type match.
get_kfunc_ptr_arg_type() no longer needs the register, so drop its regs and reg parameters; it is now a pure function of the kfunc's BTF.
When a register cannot satisfy a BTF_ID argument, report the register type passed and, when the expected struct has a reg2btf_ids mapping, the register type that would be accepted, instead of a confusing "socket". Update the affected selftest messages accordingly.
--- kernel/bpf/verifier.c | 107 +++++++++++------- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 2 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 6 +- tools/testing/selftests/bpf/verifier/calls.c | 6 +- 5 files changed, 71 insertions(+), 52 deletions(-)
kernel/bpf/verifier.c +63 -44
@@ -4924,6 +4924,19 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { [CONST_PTR_TO_MAP] = btf_bpf_map_id, }; +static enum bpf_reg_type lookup_reg2btf_ids (const struct btf_type *ref_t) +{ +enum bpf_reg_type type; + +for (type =0 ; type < __BPF_REG_TYPE_MAX; type++) { +if (reg2btf_ids[type] && +btf_type_by_id (btf_vmlinux, *reg2btf_ids[type]) == ref_t) +return type; + } + +return NOT_INIT ; +} +static bool is_trusted_reg (struct bpf_verifier_env *env,const struct bpf_reg_state *reg) {/* A referenced register is always trusted. */
@@ -11342,11 +11355,10 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta) }static int -get_kfunc_ptr_arg_type (struct bpf_verifier_env *env, -struct bpf_reg_state *regs,struct bpf_call_arg_meta *meta, +get_kfunc_ptr_arg_type (struct bpf_verifier_env *env,struct bpf_call_arg_meta *meta,const struct btf_type *t,const struct btf_type *ref_t,const char *ref_tname,const struct btf_param *args, -int arg,int nargs,argno_t argno,struct bpf_reg_state *reg) +int arg,int nargs,argno_t argno) {bool arg_mem_size = false;
@@ -11411,16 +11423,6 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,if (is_kfunc_arg_res_spin_lock (meta->btf, &args[arg]))return KF_ARG_PTR_TO_RES_SPIN_LOCK ; -if ((base_type (reg->type) ==PTR_TO_BTF_ID || reg2btf_ids[base_type (reg->type)])) { -if (!btf_type_is_struct (ref_t)) { -verbose (env,"kernel function %s %s pointer type %s %s is not supported\n" , - meta->func_name,reg_arg_name (env, argno), -btf_type_str (ref_t), ref_tname); -return -EINVAL ; - } -return KF_ARG_PTR_TO_BTF_ID ; - } -if (is_kfunc_arg_callback (env, meta->btf, &args[arg]))return KF_ARG_PTR_TO_CALLBACK ;
@@ -11429,10 +11431,14 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, is_kfunc_arg_const_mem_size(meta->btf, &args[arg + 1]))) arg_mem_size = true; -/* This is the catch all argument type of register types supported by -* check_helper_mem_access. However, we only allow when argument type is -* pointer to scalar, or struct composed (recursively) of scalars. When -* arg_mem_size is true, the pointer can be void *. +/* A pointer to a struct without a size argument is classified as KF_ARG_PTR_TO_BTF_ID */ +if (btf_type_is_struct (ref_t) && !arg_mem_size) +return KF_ARG_PTR_TO_BTF_ID ; + +/* +* Otherwise this is a memory buffer supported by check_helper_mem_access(): a pointer +* to a scalar, or to void when paired with a size argument. The access size is derived +* from the pointed-to BTF type unless a size argument follows. */ if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) {
@@ -12132,8 +12138,8 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meif (is_kfunc_arg_nullable (meta->btf, &args[i]) &&bpf_register_is_null (reg))continue ; - kf_arg_type =get_kfunc_ptr_arg_type (env, regs, meta, t, ref_t, ref_tname, - args, i, nargs, argno, reg); + kf_arg_type =get_kfunc_ptr_arg_type (env, meta, t, ref_t, ref_tname, + args, i, nargs, argno);if (kf_arg_type <0 )return kf_arg_type;
@@ -12146,19 +12152,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meswitch (base_type (kf_arg_type)) {case KF_ARG_PTR_TO_ALLOC_BTF_ID :case KF_ARG_PTR_TO_BTF_ID : -if (!is_trusted_reg (env, reg)) { -if (!is_kfunc_rcu (meta)) { -verbose (env,"%s must be referenced or trusted\n" , -reg_arg_name (env, argno)); -return -EINVAL ; - } -if (!is_rcu_reg (reg)) { -verbose (env,"%s must be a rcu pointer\n" , -reg_arg_name (env, argno)); -return -EINVAL ; - } - } - fallthrough;case KF_ARG_CONST_MAP_PTR :case KF_ARG_PTR_TO_ITER :case KF_ARG_PTR_TO_LIST_HEAD :
@@ -12377,20 +12370,46 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mebreak ;case KF_ARG_PTR_TO_BTF_ID :/* Only base_type is checked, further checks are done here */ -if ((base_type (reg->type) !=PTR_TO_BTF_ID || - (bpf_type_has_unsafe_modifiers (reg->type) && !is_rcu_reg (reg))) && - !reg2btf_ids[base_type (reg->type)]) { -verbose (env,"%s is %s " ,reg_arg_name (env, argno), -reg_type_str (env, reg->type)); -verbose (env,"expected %s or socket\n" , -reg_type_str (env,base_type (reg->type) | - (type_flag (reg->type) &BPF_REG_TRUSTED_MODIFIERS ))); +if (base_type (reg->type) ==PTR_TO_BTF_ID || + reg2btf_ids[base_type (reg->type)]) { +if (!is_trusted_reg (env, reg) || +bpf_type_has_unsafe_modifiers (reg->type)) { +if (!is_kfunc_rcu (meta)) { +verbose (env,"%s must be referenced or trusted\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } +if (!is_rcu_reg (reg)) { +verbose (env,"%s must be a rcu pointer\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } + } + + ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno); +if (ret <0 ) +return ret; +break ; + } + +if (!__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 )) { +enum bpf_reg_type reg2btf_type =lookup_reg2btf_ids (ref_t); + +verbose (env,"%s is %s expected %s %s" , +reg_arg_name (env, argno),reg_type_str (env, reg->type), +btf_type_str (ref_t), ref_tname); +if (reg2btf_type !=NOT_INIT ) +verbose (env," or %s" ,reg_type_str (env, reg2btf_type)); +verbose (env,"\n" );return -EINVAL ; } - ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno); -if (ret <0 ) -return ret; -break ; + +/* +* If the register does not contain btf id but the argument type is a pointer to +* scalar-only struct, allow verifying it as a fixed size memory. +*/ + kf_arg_type =KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + fallthrough;case KF_ARG_PTR_TO_MEM :if (kf_arg_type &MEM_FIXED_SIZE ) { resolve_ret =btf_resolve_size (btf, ref_t, &type_size);
tools/testing/selftests/bpf/progs/cgrp_kfunc_failure.c +1 -1
@@ -64,7 +64,7 @@ int BPF_PROG(cgrp_kfunc_acquire_no_null_check, struct cgroup *cgrp, const char * } SEC("tp_btf/cgroup_mkdir") -__failure __msg("R1 pointer type STRUCT cgroup must point") +__failure __msg("R1 is fp expected STRUCT cgroup") int BPF_PROG(cgrp_kfunc_acquire_fp, struct cgroup *cgrp, const char *path) {struct cgroup *acquired, *stack_cgrp = (struct cgroup *)&path;
tools/testing/selftests/bpf/progs/task_kfunc_failure.c +1 -1
@@ -50,7 +50,7 @@ int BPF_PROG(task_kfunc_acquire_untrusted, struct task_struct *task, u64 clone_f } SEC("tp_btf/task_newtask") -__failure __msg("R1 pointer type STRUCT task_struct must point") +__failure __msg("R1 is fp expected STRUCT task_struct") int BPF_PROG(task_kfunc_acquire_fp, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired, *stack_task = (struct task_struct *)&clone_flags;
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +3 -3
@@ -28,7 +28,7 @@ int BPF_PROG(get_task_exe_file_kfunc_null) } SEC("lsm.s/inode_getxattr") -__failure __msg("R1 pointer type STRUCT task_struct must point to scalar, or struct with scalar") +__failure __msg("R1 is fp expected STRUCT task_struct") int BPF_PROG(get_task_exe_file_kfunc_fp) {u64 x;
@@ -98,7 +98,7 @@ int BPF_PROG(path_d_path_kfunc_null) } SEC("lsm.s/task_alloc") -__failure __msg("R1 must be referenced or trusted") +__failure __msg("dereference of modified untrusted_ptr_") int BPF_PROG(path_d_path_kfunc_untrusted_from_argument, struct task_struct *task) {struct path *root;
@@ -112,7 +112,7 @@ int BPF_PROG(path_d_path_kfunc_untrusted_from_argument, struct task_struct *task } SEC("lsm.s/file_open") -__failure __msg("R1 must be referenced or trusted") +__failure __msg("dereference of modified untrusted_ptr_") int BPF_PROG(path_d_path_kfunc_untrusted_from_current) {struct path *pwd;
tools/testing/selftests/bpf/verifier/calls.c +3 -3
@@ -31,7 +31,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "R1 pointer type STRUCT prog_test_fail1 must point to scalar", + .errstr = "R1 is fp expected STRUCT prog_test_fail1", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_fail1", 2 }, },
@@ -46,7 +46,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "max struct nesting depth exceeded\nR1 pointer type STRUCT prog_test_fail2", + .errstr = "max struct nesting depth exceeded\nR1 is fp expected STRUCT prog_test_fail2", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_fail2", 2 }, },
@@ -61,7 +61,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "R1 pointer type STRUCT prog_test_fail3 must point to scalar", + .errstr = "R1 is fp expected STRUCT prog_test_fail3", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_fail3", 2 }, },
Signature · 1 line
2.52.0
PATCH v2 16/18 bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL
In reply to Amery HungNow that get_kfunc_ptr_arg_type() classifies a kfunc pointer argument from its BTF alone, express a nullable argument by OR-ing PTR_MAYBE_NULL into the classified type, and resolve a NULL register after classification instead of before it.
Previously check_kfunc_args() short-circuited a nullable argument passed a NULL register with a continue placed before get_kfunc_ptr_arg_type(), so the NULL never reached classification. That kept a register-state decision (bpf_register_is_null()) ahead of the BTF-based classification.
This mirrors how helper arguments carry PTR_MAYBE_NULL in their bpf_arg_type and is a step toward describing kfuncs with a bpf_func_proto:
the nullability now travels with the per-argument classification, so it is captured when the prototype is generated at add-call time.
--- kernel/bpf/verifier.c | 156 ++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 82 deletions(-)
kernel/bpf/verifier.c +74 -82
@@ -11361,93 +11361,85 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *m int arg, int nargs, argno_t argno) { bool arg_mem_size = false; - -if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || - meta->func_id == special_kfunc_list[KF_bpf_session_is_return] || - meta->func_id == special_kfunc_list[KF_bpf_session_cookie]) -return KF_ARG_PTR_TO_CTX ; +int arg_type;/* In this function, we verify the kfunc's BTF as per the argument type, * leaving the rest of the verification with respect to the register * type to our caller. When a set of conditions hold in the BTF type of * arguments, we resolve it to a known kfunc_ptr_arg_type. */ -if (btf_is_prog_ctx_type (&env->log, meta->btf, t,resolve_prog_type (env->prog), arg)) -return KF_ARG_PTR_TO_CTX ; - -if (is_kfunc_arg_alloc_obj (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_ALLOC_BTF_ID ; - -if (is_kfunc_arg_refcounted_kptr (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_REFCOUNTED_KPTR ; - -if (is_kfunc_arg_dynptr (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_DYNPTR ; - -if (is_kfunc_arg_iter (meta, arg, &args[arg])) -return KF_ARG_PTR_TO_ITER ; - -if (is_kfunc_arg_list_head (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_LIST_HEAD ; - -if (is_kfunc_arg_list_node (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_LIST_NODE ; - -if (is_kfunc_arg_rbtree_root (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_RB_ROOT ; - -if (is_kfunc_arg_rbtree_node (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_RB_NODE ; - -if (is_kfunc_arg_const_str (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_CONST_STR ; - -if (is_kfunc_arg_const_map (meta->btf, &args[arg])) -return KF_ARG_CONST_MAP_PTR ; - -if (is_kfunc_arg_map (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_BTF_ID ; - -if (is_kfunc_arg_wq (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_WORKQUEUE ; - -if (is_kfunc_arg_timer (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_TIMER ; - -if (is_kfunc_arg_task_work (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_TASK_WORK ; - -if (is_kfunc_arg_irq_flag (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_IRQ_FLAG ; - -if (is_kfunc_arg_res_spin_lock (meta->btf, &args[arg])) -return KF_ARG_PTR_TO_RES_SPIN_LOCK ; - -if (is_kfunc_arg_callback (env, meta->btf, &args[arg])) -return KF_ARG_PTR_TO_CALLBACK ; - -if (arg +1 < nargs && - (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ]) || -is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ]))) - arg_mem_size = true; +if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] || + meta->func_id == special_kfunc_list[KF_bpf_session_is_return] || + meta->func_id == special_kfunc_list[KF_bpf_session_cookie]) + arg_type =KF_ARG_PTR_TO_CTX ; +else if (btf_is_prog_ctx_type (&env->log, meta->btf, t,resolve_prog_type (env->prog), arg)) + arg_type =KF_ARG_PTR_TO_CTX ; +else if (is_kfunc_arg_alloc_obj (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_ALLOC_BTF_ID ; +else if (is_kfunc_arg_refcounted_kptr (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_REFCOUNTED_KPTR ; +else if (is_kfunc_arg_dynptr (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_DYNPTR ; +else if (is_kfunc_arg_iter (meta, arg, &args[arg])) + arg_type =KF_ARG_PTR_TO_ITER ; +else if (is_kfunc_arg_list_head (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_LIST_HEAD ; +else if (is_kfunc_arg_list_node (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_LIST_NODE ; +else if (is_kfunc_arg_rbtree_root (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_RB_ROOT ; +else if (is_kfunc_arg_rbtree_node (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_RB_NODE ; +else if (is_kfunc_arg_const_str (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_CONST_STR ; +else if (is_kfunc_arg_const_map (meta->btf, &args[arg])) + arg_type =KF_ARG_CONST_MAP_PTR ; +else if (is_kfunc_arg_map (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_BTF_ID ; +else if (is_kfunc_arg_wq (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_WORKQUEUE ; +else if (is_kfunc_arg_timer (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_TIMER ; +else if (is_kfunc_arg_task_work (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_TASK_WORK ; +else if (is_kfunc_arg_irq_flag (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_IRQ_FLAG ; +else if (is_kfunc_arg_res_spin_lock (meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_RES_SPIN_LOCK ; +else if (is_kfunc_arg_callback (env, meta->btf, &args[arg])) + arg_type =KF_ARG_PTR_TO_CALLBACK ; +else { +if (arg +1 < nargs && + (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ]) || +is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ]))) + arg_mem_size = true; + +if (btf_type_is_struct (ref_t) && !arg_mem_size) { +/* A pointer to a struct without a size argument is classified +* as KF_ARG_PTR_TO_BTF_ID. +*/ + arg_type =KF_ARG_PTR_TO_BTF_ID ; + }else if (!btf_type_is_scalar (ref_t) && + !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && + (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) { +verbose (env,"%s pointer type %s %s must point to %sscalar, or struct with scalar\n" , +reg_arg_name (env, argno), +btf_type_str (ref_t), ref_tname, arg_mem_size ?"void, " :"" ); +return -EINVAL ; + }else { +/* Otherwise this is a memory buffer supported by +* check_helper_mem_access(): a pointer to a scalar, or to void +* when paired with a size argument. The access size is derived +* from the pointed-to BTF type unless a size argument follows. +*/ + arg_type = arg_mem_size ?KF_ARG_PTR_TO_MEM :KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + } + } -/* A pointer to a struct without a size argument is classified as KF_ARG_PTR_TO_BTF_ID */ -if (btf_type_is_struct (ref_t) && !arg_mem_size) -return KF_ARG_PTR_TO_BTF_ID ; +if (is_kfunc_arg_nullable (meta->btf, &args[arg])) + arg_type |=PTR_MAYBE_NULL ; -/* -* Otherwise this is a memory buffer supported by check_helper_mem_access(): a pointer -* to a scalar, or to void when paired with a size argument. The access size is derived -* from the pointed-to BTF type unless a size argument follows. -*/ -if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && - (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) { -verbose (env,"%s pointer type %s %s must point to %sscalar, or struct with scalar\n" , -reg_arg_name (env, argno), -btf_type_str (ref_t), ref_tname, arg_mem_size ?"void, " :"" ); -return -EINVAL ; - } -return arg_mem_size ?KF_ARG_PTR_TO_MEM :KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; +return arg_type; } static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env,
@@ -12135,14 +12127,14 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); ref_tname =btf_name_by_offset (btf, ref_t->name_off); -if (is_kfunc_arg_nullable (meta->btf, &args[i]) &&bpf_register_is_null (reg)) -continue ; - kf_arg_type =get_kfunc_ptr_arg_type (env, meta, t, ref_t, ref_tname, args, i, nargs, argno);if (kf_arg_type <0 )return kf_arg_type; +if (bpf_register_is_null (reg) &&type_may_be_null (kf_arg_type)) +continue ; +if (is_kfunc_arg_map (btf, &args[i])) { ref_id = *reg2btf_ids[CONST_PTR_TO_MAP ]; ref_t =btf_type_by_id (btf_vmlinux, ref_id);
Signature · 1 line
2.52.0
PATCH v2 17/18 bpf: Classify scalar kfunc arguments from BTF
In reply to Amery HungAdd kfunc scalar argument types, classify them in get_kfunc_arg_type() along side with pointer arguments and move scalar type verification into the main switch in check_kfunc_args(). This keeps BTF-based classification separate from register validation for every argument, paving the way for generating the kfunc argument prototype at add-call time. No functional change intended.
--- kernel/bpf/verifier.c | 118 +++++++++++++++++++++++++++--------------- 1 file changed, 76 insertions(+), 42 deletions(-)
kernel/bpf/verifier.c +76 -42
@@ -11099,6 +11099,11 @@ enum kfunc_ptr_arg_type {KF_ARG_PTR_TO_IRQ_FLAG ,KF_ARG_PTR_TO_RES_SPIN_LOCK ,KF_ARG_PTR_TO_TASK_WORK , +KF_ARG_CONST ,/* const scalar */ +KF_ARG_CONST_MEM_SIZE ,/* const scalar sizing a preceding memory arg */ +KF_ARG_CONST_ALLOC_SIZE_OR_ZERO ,/* const scalar sizing a returned buffer */ +KF_ARG_MEM_SIZE ,/* scalar sizing a preceding memory arg */ + KF_ARG_ANYTHING, /* unconstrained scalar */ }; enum special_kfunc_type {
@@ -11355,14 +11360,40 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta) }static int -get_kfunc_ptr_arg_type (struct bpf_verifier_env *env,struct bpf_call_arg_meta *meta, -const struct btf_type *t,const struct btf_type *ref_t, -const char *ref_tname,const struct btf_param *args, -int arg,int nargs,argno_t argno) +get_kfunc_arg_type (struct bpf_verifier_env *env,struct bpf_call_arg_meta *meta, +const struct btf_param *args,int arg,int nargs) { +const struct btf_type *t, *ref_t =NULL ; +argno_t argno =argno_from_arg (arg +1 ); +const char *ref_tname =NULL ;bool arg_mem_size = false;int arg_type; + t =btf_type_skip_modifiers (meta->btf, args[arg].type,NULL ); + +/* Scalar arguments are classified from their BTF suffix/name alone. */ +if (btf_type_is_scalar (t)) { +if (is_kfunc_arg_constant (meta->btf, &args[arg])) +return KF_ARG_CONST ; +if (is_kfunc_arg_const_mem_size (meta->btf, &args[arg])) +return KF_ARG_CONST_MEM_SIZE ; +if (is_kfunc_arg_mem_size (meta->btf, &args[arg])) +return KF_ARG_MEM_SIZE ; +if (is_kfunc_arg_scalar_with_name (meta->btf, &args[arg],"rdonly_buf_size" ) || +is_kfunc_arg_scalar_with_name (meta->btf, &args[arg],"rdwr_buf_size" )) +return KF_ARG_CONST_ALLOC_SIZE_OR_ZERO ; +return KF_ARG_ANYTHING ; + } + +if (!btf_type_is_ptr (t)) { +verbose (env,"Unrecognized %s type %s\n" , +reg_arg_name (env, argno),btf_type_str (t)); +return -EINVAL ; + } + + ref_t =btf_type_skip_modifiers (meta->btf, t->type,NULL ); + ref_tname =btf_name_by_offset (meta->btf, ref_t->name_off); +/* In this function, we verify the kfunc's BTF as per the argument type, * leaving the rest of the verification with respect to the register * type to our caller. When a set of conditions hold in the BTF type of
@@ -12051,7 +12082,6 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meint regno =reg_from_argno (argno);bool btf_id_fixed_off_ok = true;u32 ref_id, type_size; -bool is_ret_buf_sz = false;int kf_arg_type;if (is_kfunc_arg_prog_aux (btf, &args[i])) {
@@ -12075,39 +12105,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me t =btf_type_skip_modifiers (btf, args[i].type,NULL ); -if (btf_type_is_scalar (t)) { -if (reg->type !=SCALAR_VALUE ) { -verbose (env,"%s is not a scalar\n" ,reg_arg_name (env, argno)); -return -EINVAL ; - } - -if (is_kfunc_arg_constant (meta->btf, &args[i]) || -is_kfunc_arg_const_mem_size (meta->btf, &args[i])) { - ret =process_const_arg (env, reg, argno, meta); -if (ret <0 ) -return ret; - }else if (is_kfunc_arg_scalar_with_name (btf, &args[i],"rdonly_buf_size" )) { - meta->r0_rdonly = true; - is_ret_buf_sz = true; - }else if (is_kfunc_arg_scalar_with_name (btf, &args[i],"rdwr_buf_size" )) { - is_ret_buf_sz = true; - } - -if (is_ret_buf_sz) { - ret =process_const_alloc_mem_size (env, reg, argno, &meta->ret_mem); -if (ret <0 ) -return ret; - } -continue ; - } - -if (!btf_type_is_ptr (t)) { -verbose (env,"Unrecognized %s type %s\n" , -reg_arg_name (env, argno),btf_type_str (t)); -return -EINVAL ; - } - -if ((bpf_register_is_null (reg) ||type_may_be_null (reg->type)) && +if (btf_type_is_ptr (t) && (bpf_register_is_null (reg) ||type_may_be_null (reg->type)) && !is_kfunc_arg_nullable (meta->btf, &args[i])) {verbose (env,"Possibly NULL pointer passed to trusted %s\n" ,reg_arg_name (env, argno));
@@ -12124,11 +12122,12 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_meif (reg_is_referenced (env, reg))update_ref_obj (&meta->ref_obj, reg); - ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); - ref_tname =btf_name_by_offset (btf, ref_t->name_off); +if (btf_type_is_ptr (t)) { + ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); + ref_tname =btf_name_by_offset (btf, ref_t->name_off); + } - kf_arg_type =get_kfunc_ptr_arg_type (env, meta, t, ref_t, ref_tname, - args, i, nargs, argno); + kf_arg_type =get_kfunc_arg_type (env, meta, args, i, nargs);if (kf_arg_type <0 )return kf_arg_type;
@@ -12142,6 +12141,11 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me }switch (base_type (kf_arg_type)) { +case KF_ARG_CONST : +case KF_ARG_CONST_MEM_SIZE : +case KF_ARG_MEM_SIZE : +case KF_ARG_ANYTHING : +case KF_ARG_CONST_ALLOC_SIZE_OR_ZERO :case KF_ARG_PTR_TO_ALLOC_BTF_ID :case KF_ARG_PTR_TO_BTF_ID :case KF_ARG_CONST_MAP_PTR :
@@ -12182,6 +12186,36 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mereturn ret;switch (base_type (kf_arg_type)) { +case KF_ARG_CONST : +case KF_ARG_CONST_MEM_SIZE : +if (reg->type !=SCALAR_VALUE ) { +verbose (env,"%s is not a scalar\n" ,reg_arg_name (env, argno)); +return -EINVAL ; + } + + ret =process_const_arg (env, reg, argno, meta); +if (ret <0 ) +return ret; +break ; +case KF_ARG_MEM_SIZE : +case KF_ARG_ANYTHING : +if (reg->type !=SCALAR_VALUE ) { +verbose (env,"%s is not a scalar\n" ,reg_arg_name (env, argno)); +return -EINVAL ; + } +break ; +case KF_ARG_CONST_ALLOC_SIZE_OR_ZERO : +if (reg->type !=SCALAR_VALUE ) { +verbose (env,"%s is not a scalar\n" ,reg_arg_name (env, argno)); +return -EINVAL ; + } + +if (is_kfunc_arg_scalar_with_name (btf, &args[i],"rdonly_buf_size" )) + meta->r0_rdonly = true; + ret =process_const_alloc_mem_size (env, reg, argno, &meta->ret_mem); +if (ret <0 ) +return ret; +break ;case KF_ARG_PTR_TO_CTX :if (reg->type !=PTR_TO_CTX ) { verbose(env, "%s expected pointer to ctx, but got %s\n",
Signature · 1 line
2.52.0
PATCH v2 18/18 bpf: Generate kfunc argument prototype at add-call time
In reply to Amery HungKfunc argument checking re-derives each argument's kfunc_ptr_arg_type from BTF on every verification of a call in check_kfunc_args(). Now that get_kfunc_arg_type() is a function of the kfunc's BTF alone, it no longer inspects register state. The classification can be computed once when the call is added and cached. This is a step toward describing kfuncs with a bpf_func_proto and sharing the helper argument-checking path.
Generate the classification at bpf_add_kfunc_call() time:
- Extend struct bpf_func_proto to be able to describe a kfunc: widen arg_type[] and the arg_btf_id[]/arg_size[] union from 5 to MAX_BPF_FUNC_ARGS, since a kfunc may take up to 12 arguments (5 in registers, 7 on the stack).
- Add a bpf_func_proto pointer to struct bpf_kfunc_desc, populated by gen_kfunc_arg_proto() which runs get_kfunc_arg_type() for each argument and stores the result in proto->arg_type[]. It is freed with the descriptor table in bpf_free_kfunc_desc_tab().
- check_kfunc_args() reads the cached classification from meta->fn
The KF_ARG_PTR_TO_CTX classification depends on the resolved program type, and for BPF_PROG_TYPE_EXT the target type (saved_dst_prog_type) is only recorded later, in check_attach_btf_id(). Make resolve_prog_type() fall back to prog->aux->dst_prog->type, which is set at load time and holds the same value, so the resolved type is available at add-call time without reordering verification passes. This keeps e.g. an freplace of an XDP program calling bpf_xdp_metadata_rx_hash() classifying its struct xdp_md * argument as context.
The classification result is unchanged; it is only computed earlier and cached.
--- include/linux/bpf.h | 36 +++++------ include/linux/bpf_verifier.h | 18 +++++- kernel/bpf/core.c | 4 +- kernel/bpf/syscall.c | 2 +- kernel/bpf/verifier.c | 112 ++++++++++++++++++++++++++++------- 5 files changed, 128 insertions(+), 44 deletions(-)
include/linux/bpf.h +18 -18
@@ -962,6 +962,21 @@ enum bpf_return_type { };static_assert (__BPF_RET_TYPE_MAX <=BPF_BASE_TYPE_LIMIT ); +/* The longest tracepoint has 12 args. +* See include/trace/bpf_probe.h +* +* Also reuse this macro for maximum number of arguments a BPF function +* or a kfunc can have. Args 1-5 are passed in registers, args 6-12 via +* stack arg slots. The JIT may map some stack arg slots to registers based +* on the native calling convention (e.g., arg 6 to R9 on x86-64). +*/ +#define MAX_BPF_FUNC_ARGS 12 + +/* The maximum number of arguments passed through registers +* a single function may have. +*/ +#define MAX_BPF_FUNC_REG_ARGS 5 +/* eBPF function prototype used by verifier to allow BPF_CALLs from eBPF programs * to in-kernel helper functions and for adjusting imm32 field in BPF_CALL * instructions after verifying
@@ -986,7 +1001,7 @@ struct bpf_func_proto {enum bpf_arg_type arg4_type;enum bpf_arg_type arg5_type; }; -enum bpf_arg_type arg_type[5 ]; +enum bpf_arg_type arg_type[MAX_BPF_FUNC_ARGS ]; }; union { struct {
@@ -996,7 +1011,7 @@ struct bpf_func_proto {u32 *arg4_btf_id;u32 *arg5_btf_id; }; -u32 *arg_btf_id[5 ]; +u32 *arg_btf_id[MAX_BPF_FUNC_ARGS ];struct {size_t arg1_size;size_t arg2_size;
@@ -1004,7 +1019,7 @@ struct bpf_func_proto {size_t arg4_size;size_t arg5_size; }; -size_t arg_size[5 ]; +size_t arg_size[MAX_BPF_FUNC_ARGS ]; };int *ret_btf_id;/* return value btf_id */ bool (*allowed)(const struct bpf_prog *prog);
@@ -1194,21 +1209,6 @@ struct bpf_prog_offload {u32 jited_len; }; -/* The longest tracepoint has 12 args. -* See include/trace/bpf_probe.h -* -* Also reuse this macro for maximum number of arguments a BPF function -* or a kfunc can have. Args 1-5 are passed in registers, args 6-12 via -* stack arg slots. The JIT may map some stack arg slots to registers based -* on the native calling convention (e.g., arg 6 to R9 on x86-64). -*/ -#define MAX_BPF_FUNC_ARGS 12 - -/* The maximum number of arguments passed through registers -* a single function may have. -*/ -#define MAX_BPF_FUNC_REG_ARGS 5 -/* The argument is a structure or a union. */ #define BTF_FMODEL_STRUCT_ARG BIT(0)
include/linux/bpf_verifier.h +16 -2
@@ -1188,6 +1188,7 @@ int bpf_check_attach_target(struct bpf_verifier_log *log,u32 btf_id, struct bpf_attach_target_info *tgt_info);void bpf_free_kfunc_btf_tab (struct bpf_kfunc_btf_tab *tab); +void bpf_free_kfunc_desc_tab (struct bpf_kfunc_desc_tab *tab);int mark_chain_precision (struct bpf_verifier_env *env,int regno);
@@ -1305,8 +1306,19 @@ static inline u32 type_flag(u32 type)/* only use after check_attach_btf_id() */ static inline enum bpf_prog_type resolve_prog_type (const struct bpf_prog *prog) { -return (prog->type ==BPF_PROG_TYPE_EXT && prog->aux->saved_dst_prog_type) ? - prog->aux->saved_dst_prog_type : prog->type; +if (prog->type ==BPF_PROG_TYPE_EXT ) { +/* +* saved_dst_prog_type is only set once check_attach_btf_id() +* runs. Before that -- e.g. when generating kfunc prototypes at +* add-call time -- fall back to the attach target's type, which +* is available from load time and holds the same value. +*/ +if (prog->aux->saved_dst_prog_type) +return prog->aux->saved_dst_prog_type; +if (prog->aux->dst_prog) +return prog->aux->dst_prog->type; + } +return prog->type; }static inline bool bpf_prog_check_recur (const struct bpf_prog *prog)
@@ -1489,6 +1501,7 @@ struct bpf_call_arg_meta {/* Common */ struct btf *btf;u32 func_id; +const struct bpf_func_proto *fn;u8 release_regno;u32 ret_btf_id;u32 subprogno;
@@ -1617,6 +1630,7 @@ enum bpf_reg_arg_type {struct bpf_kfunc_desc {struct btf_func_model func_model; +struct bpf_func_proto *proto;u32 func_id;s32 imm;u16 offset;
kernel/bpf/core.c +3 -1
@@ -198,7 +198,9 @@ void bpf_prog_jit_attempt_done(struct bpf_prog *prog) prog->aux->jited_linfo =NULL ; } -kfree (prog->aux->kfunc_tab); +#ifdef CONFIG_BPF_SYSCALL +bpf_free_kfunc_desc_tab (prog->aux->kfunc_tab); +#endif prog->aux->kfunc_tab =NULL ; }
kernel/bpf/syscall.c +1 -1
@@ -2444,7 +2444,7 @@ static void __bpf_prog_put_noref(struct bpf_prog *prog, bool deferred)module_put (prog->aux->mod);kvfree (prog->aux->jited_linfo);kvfree (prog->aux->linfo); -kfree (prog->aux->kfunc_tab); +bpf_free_kfunc_desc_tab (prog->aux->kfunc_tab);kfree (prog->aux->ctx_arg_info);if (prog->aux->attach_btf)btf_put (prog->aux->attach_btf);
kernel/bpf/verifier.c +90 -22
219 lines not on this page · raw message
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next v2 06/18] selftests/bpf: Add tests for helper and kfunc mem+size arguments
In reply to Amery HungThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Incorrect argument name formatting in verifier log for map pointers passed on the stack in…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Incorrect argument name formatting in verifier log for map pointers passed on the stack in process_map_ptr_arg(). --
--- Patch [6]: [PATCH bpf-next v2 06/18] selftests/bpf: Add tests for helper and kfunc mem+size arguments --- commit 26d0efc0fe7fbd940204152f62931d959ef53578
Author: Amery Hung <[email protected]>
selftests/bpf: Add tests for helper and kfunc mem+size arguments
This commit adds tests to ensure that the return of a map-of-maps lookup cannot be passed to a nullable memory buffer argument for helpers and kfuncs. It verifies that both mapofmaps_value_as_kfunc_mem_buf and mapofmaps_value_as_helper_mem_buf are properly rejected by the verifier. This guarantees that helper and kfunc arguments are checked in the same way.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 7 lines
kernel/bpf/verifier.c +3
-0@@ -8286,6 +8267,44 @@ static int get_constant_map_key(struct bpf_verifier_env *env,static bool can_elide_value_nullness (const struct bpf_map *map); +static int process_map_ptr_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +argno_t argno,struct bpf_call_arg_meta *meta) +{
[ ... ]
kernel/bpf/verifier.c
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+if (meta->map.ptr && + (meta->map.ptr != reg->map_ptr || meta->map.uid != reg->map_uid)) { +argno_t obj_argno =argno_from_reg (reg_from_argno (argno) -1 );
[Severity: Low]
If the map argument is passed on the stack, does this incorrectly calculate the preceding argument's argno? It appears that this can result in an invalid internal representation for obj_argno.
kernel/bpf/verifier.c
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+struct btf_record *rec = meta->map.ptr->record; +const char *obj_name ="workqueue" ; + +if (rec->timer_off >=0 ) + obj_name ="timer" ; +else if (rec->task_work_off >=0 ) + obj_name ="bpf_task_work" ; + +verbose (env,"%s pointer in %s map_uid=%d " , + obj_name,reg_arg_name (env, obj_argno), meta->map.uid);
[Severity: Low]
Does this use the incorrectly computed obj_argno, causing reg_arg_name() to print "R2" in the verifier log instead of the actual register or stack location?
Quoted 9 lines
kernel/bpf/verifier.c +9
-0+verbose (env,"doesn't match map pointer in %s map_uid=%d\n" , +reg_arg_name (env, argno), reg->map_uid); +return -EINVAL ; + } + + meta->map.ptr = reg->map_ptr; + meta->map.uid = reg->map_uid; +return 0 ; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
[email protected]
AI reviewRe: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs
In reply to Amery HungThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] Multi-line comment does not follow the required BPF subsystem comment style.
Pre-existing issues:
- [Critical] Pre-existing verifier panic when an array map is passed to kfuncs like bpf_wq_init.
- [High] Pre-existing logic bug allows mismatching maps to bypass safety checks in kfuncs. --
--- Patch [2]: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs --- commit 08bfe87a36be7126ec3db2d3ab4783e72b62e453
Author: Amery Hung <[email protected]>
bpf: Unify const map ptr argument checking for helpers and kfuncs
Both the helper ARG_CONST_MAP_PTR and the kfunc KF_ARG_PTR_TO_MAP recorded the map pointer in meta->map and, when a map was already bound by a preceding timer/workqueue/task_work argument, rejected a mismatching map. This logic is factored into a single process_map_ptr_arg() used by both paths.
kernel/bpf/verifier.c static int get_constant_map_key(struct bpf_verifier_env *env, · line 8280
Quoted 12 lines
kernel/bpf/verifier.c +4
-0@@ -8274,6 +8274,44 @@ static int get_constant_map_key(struct bpf_verifier_env *env,static bool can_elide_value_nullness (const struct bpf_map *map); +static int process_map_ptr_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +argno_t argno,struct bpf_call_arg_meta *meta) +{ +/* Use map_uid (which is unique id of inner map) to reject:
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF subsystem formatting?
According to the subsystem guidelines, multi-line comments should have the opening /* on its own line:
/*
* Use map_uid (which is unique id of inner map) to reject:
* ...
*/
[ ... ]
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12126
Quoted 6 lines
kernel/bpf/verifier.c +0
-2@@ -12122,36 +12140,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me reg_arg_name(env, argno));return -EINVAL ; } -if (meta->map.ptr && (reg->map_ptr->record->wq_off >=0 || - reg->map_ptr->record->task_work_off >=0 )) {
[Severity: Critical]
This is a pre-existing issue that was not introduced by this patch (in fact, the patch appears to fix it!), but could the old code crash the verifier here if a BPF program called bpf_wq_init(wq, map, 0) and passed an array map without BTF for the map argument?
Since an array map without BTF has a NULL record, evaluating reg->map_ptr->record->wq_off would result in an unconditional NULL pointer dereference, leading to a panic. This could be triggered by any user with privileges to load BPF programs.
[Severity: High]
This is also a pre-existing issue that was not introduced by this patch, but did this original condition allow mismatching maps to bypass safety checks?
If a BPF program called bpf_wq_init(wq, map, 0) with a valid workqueue but passed a mismatched map (like a map containing only a bpf_timer), the mismatched map would lack both wq_off and task_work_off. This would make the condition evaluate to false, bypassing the mismatch rejection and potentially leading to type confusion or memory corruption during runtime callbacks.
Quoted 2 lines
kernel/bpf/verifier.c +0
-2-/* Use map_uid (which is unique id of inner map) to reject: -* inner_map1 = bpf_map_lookup_elem(outer_map, key1)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
[email protected]
AI reviewRe: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to Amery HungThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [Critical] An integer overflow in check_mem_reg when calculating the stack access size allows global subprograms to bypass memory bounds checks, leading to out-of-bounds kernel memory accesses.
- [Critical] Global subprograms unconditionally accept literal NULL scalars for non-nullable pointer arguments, potentially leading to a kernel crash due to NULL pointer dereferences. --
--- Patch [7]: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way --- commit 7f7a12884bb4c90a70c0a9e694af984eca478eee
Author: Amery Hung <[email protected]>
bpf: Check fixed-size mem args of helpers and kfuncs the same way
Fixed-size memory arguments went through two paths: helpers called check_helper_mem_access() directly, while kfuncs and global subprogs used check_mem_reg(). Route the helper MEM_FIXED_SIZE case through check_mem_reg() too so all three share the same check.
This also fixes a bug in the helper path. When passing a NULL to PTR_MAYBE_NULL | ARG_PTR_TO_FIXED_SIZE_MEM argument, the program would be falsely rejected by check_helper_mem_access().
kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6971
Quoted 10 lines
kernel/bpf/verifier.c +1
-1@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regmark_ptr_not_null_reg (reg); } -int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size;
[Severity: Critical]
This is a pre-existing issue, but does the -(int)mem_size cast introduce an integer overflow risk for global subprograms?
If a user defines a BTF struct with a size close to U32_MAX (e.g. 0xFFFFFFFC), -(int)mem_size evaluates to a small positive integer like 4.
The verifier would only validate this small 4-byte region on the caller's stack, while the global subprogram is verified separately with a PTR_TO_MEM of the original massive size.
Could this allow the subprogram to read and write out-of-bounds kernel memory beyond the provided stack object?
Quoted 7 lines
kernel/bpf/verifier.c +4
-2- err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);
[ ... ]
kernel/bpf/verifier.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, · line 9260
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -9251,7 +9255,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, ret =check_func_arg_reg_off (env, reg, argno,ARG_DONTCARE );if (ret <0 )return ret; -if (check_mem_reg (env, reg, argno, arg->mem_size,NULL )) +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 )) {
[Severity: Critical]
This isn't a bug introduced by this patch, but is it possible for global subprograms to bypass nullability requirements here?
When check_mem_reg() is called just above this line, it returns 0 early for literal NULL scalars:
kernel/bpf/verifier.c:check_mem_reg() {
if (bpf_register_is_null(reg))
return 0;
...
}Because of this early return, the nullability check immediately following check_mem_reg() in btf_check_func_arg_match() fails to catch the NULL. The check relies on the PTR_MAYBE_NULL flag, which a literal NULL scalar (SCALAR_VALUE) does not have.
Does this allow global subprograms to accept unvalidated NULL pointers even for arguments not marked as nullable, leading to potential NULL pointer dereferences?
Quoted 2 lines
kernel/bpf/verifier.c +0
-0bpf_log(log, "%s is expected to be non-NULL\n",
reg_arg_name(env, argno));
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
[email protected]
AI reviewRe: [PATCH bpf-next v2 16/18] bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL
In reply to Amery HungThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Multi-line comments do not follow the BPF subsystem style. --
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Multi-line comments do not follow the BPF subsystem style.
--
--- Patch [16]: [PATCH bpf-next v2 16/18] bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL --- commit 8714272b2414d4acacf57ee10e018ba054ac3e95
Author: Amery Hung <[email protected]>
bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL
This commit updates get_kfunc_ptr_arg_type() to classify nullable kfunc pointer arguments by OR-ing PTR_MAYBE_NULL into the returned type based on BTF alone. It defers the check of whether the register is actually NULL until after the classification is complete, matching how helper arguments are handled.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
Quoted 1 line
kernel/bpf/verifier.c +0
-0@@ -11361,93 +11361,85 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *m
[ ... ]
kernel/bpf/verifier.c get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *m · line 11420
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+if (btf_type_is_struct (ref_t) && !arg_mem_size) { +/* A pointer to a struct without a size argument is classified +* as KF_ARG_PTR_TO_BTF_ID. +*/
[Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF subsystem style? Multi-line comments in this subsystem typically require the opening /* on its own line before the text begins.
Quoted 4 lines
kernel/bpf/verifier.c +4
-0+ arg_type =KF_ARG_PTR_TO_BTF_ID ; + }else if (!btf_type_is_scalar (ref_t) && + !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && + (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) {
kernel/bpf/verifier.c get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_call_arg_meta *m · line 11434
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+verbose (env,"%s pointer type %s %s must point to %sscalar, or struct with scalar\n" , +reg_arg_name (env, argno), +btf_type_str (ref_t), ref_tname, arg_mem_size ?"void, " :"" ); +return -EINVAL ; + }else { +/* Otherwise this is a memory buffer supported by +* check_helper_mem_access(): a pointer to a scalar, or to void +* when paired with a size argument. The access size is derived +* from the pointed-to BTF type unless a size argument follows. +*/
[Severity: Low]
This isn't a bug, but this comment has the same style issue where the text starts on the same line as the opening /*.
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+ arg_type = arg_mem_size ?KF_ARG_PTR_TO_MEM :KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + } + }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
[email protected]
AI reviewRe: [PATCH bpf-next v2 11/18] bpf: Classify kfunc mem_size args from BTF without register state
In reply to Amery HungThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Removing the `SCALAR_VALUE` check from `is_kfunc_arg_mem_size()` and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing the SCALAR_VALUE check from is_kfunc_arg_mem_size() and is_kfunc_arg_const_mem_size() allows unvalidated pointer registers to be passed as memory size arguments during kfunc lookahead validation, triggering a verifier_bug kernel warning splat and local DoS. --
--- Patch [11]: [PATCH bpf-next v2 11/18] bpf: Classify kfunc mem_size args from BTF without register state --- commit 55013229ec44003dda12328783652d410aa4f75e
Author: Amery Hung <[email protected]>
bpf: Classify kfunc mem_size args from BTF without register state
This commit drops the scalar register type check in is_kfunc_arg_mem_size() and is_kfunc_arg_const_mem_size() to decouple get_kfunc_ptr_arg_type() from register state.
Quoted 11 lines
kernel/bpf/verifier.c +1
-2@@ -10795,26 +10795,24 @@ static bool is_kfunc_rcu_protected(struct bpf_call_arg_meta *meta) }static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +const struct btf_param *arg)
kernel/bpf/verifier.c static bool is_kfunc_rcu_protected(struct bpf_call_arg_meta *meta) · line 10807
Quoted 10 lines
kernel/bpf/verifier.c +1
-1{const struct btf_type *t; t =btf_type_skip_modifiers (btf, arg->type,NULL ); -if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +if (!btf_type_is_scalar (t))return false;return btf_param_match_suffix (btf, arg,"__sz" ); }
[Severity: High]
Could this removal allow a pointer register to be incorrectly classified as a memory size parameter during kfunc lookahead validation?
If a BPF program passes a pointer register (such as PTR_TO_STACK with a fixed offset of 0) instead of a scalar to a size argument suffixed with __sz, get_kfunc_ptr_arg_type() will now look ahead and classify it solely based on the BTF signature.
Because of this classification, check_kfunc_args() eagerly processes the unverified register as a size parameter before the generic scalar validation loop catches it:
check_kfunc_args()
...
check_mem_size_reg(env, buff_reg, size_reg, argno, next_argno, ...)
Inside check_mem_size_reg(), the pointer with a fixed offset of 0 yields a size of 0. Since zero-size is allowed, it passes the bounds check and calls mark_chain_precision() on the pointer register.
Because mark_chain_precision() expects a SCALAR_VALUE, it triggers a verifier_bug splat for backtracking misuse when given a pointer. On systems configured with panic_on_warn, this creates a local denial of service regression.
Should the register type check be retained here, or is there another way to prevent pointer registers from bypassing validation during the lookahead?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
[email protected]
AI reviewRe: [PATCH bpf-next v2 14/18] bpf: Check helper mem+size in ARG_PTR_TO_MEM case
In reply to Amery HungThank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Calling check_mem_size_reg() on the size register before validating its type allows an…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Calling check_mem_size_reg() on the size register before validating its type allows an unprivileged user to trigger a kernel warning (WARN_ONCE) and potential panic, leading to a local Denial of Service. --
--- Patch [14]: [PATCH bpf-next v2 14/18] bpf: Check helper mem+size in ARG_PTR_TO_MEM case --- commit 49ede5ce4d16f1df74560df7eef17176d13cb3e1
Author: Amery Hung <[email protected]>
bpf: Check helper mem+size in ARG_PTR_TO_MEM case
This commit modifies the BPF verifier to check variable-size memory arguments and their associated size registers simultaneously during the ARG_PTR_TO_MEM case, rather than waiting for the ARG_MEM_SIZE argument. It looks ahead to regno + 1 to process the size register.
Quoted 4 lines
kernel/bpf/verifier.c +0
-0
[ ... ]
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8521
Quoted 11 lines
kernel/bpf/verifier.c +7
-0@@ -8515,14 +8512,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return err;if (arg_type &MEM_ALIGNED ) err =check_ptr_alignment (env, reg,0 , fn->arg_size[arg], true); + }else { +struct bpf_reg_state *size_reg =reg_state (env, regno +1 ); + + err =check_mem_size_reg (env, reg, size_reg, argno, +argno_from_reg (regno +1 ), + arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , + fn->arg_type[arg +1 ] &SCALAR_MAYBE_ZERO , meta);
[Severity: High]
Could this call to check_mem_size_reg() process a non-scalar register?
By looking ahead and processing size_reg at regno + 1 here, it seems this is happening before the size register's type is validated in the next loop iteration of check_func_arg().
If a BPF program passes a pointer (like PTR_TO_MAP_VALUE with var_off=0) instead of a scalar for the size argument, it appears it would bypass the scalar bounds checks. When check_mem_size_reg() subsequently calls mark_chain_precision() on that non-scalar register, could this trigger a verifier_bug() and BPF_WARN_ONCE()?
On systems with panic_on_warn enabled, this might lead to a local denial of service.
Quoted 2 lines
kernel/bpf/verifier.c +0
-0}break ;
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Re: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to [email protected][...]
Quoted 13 lines
kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6971
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regmark_ptr_not_null_reg (reg); } -int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size;[Severity: Critical]
This is a pre-existing issue, but does the -(int)mem_size cast introduce an integer overflow risk for global subprograms?
Quoted 2 lines
If a user defines a BTF struct with a size close to
U32_MAX(e.g.0xFFFFFFFC), -(int)mem_size evaluates to a small positive integer like 4.
This won't happen. The early "mem_size > S32_MAX" check in check_mem_reg() will prevent this.
Quoted 40 lines
The verifier would only validate this small 4-byte region on the caller's stack, while the global subprogram is verified separately with a
PTR_TO_MEMof the original massive size.Could this allow the subprogram to read and write out-of-bounds kernel memory beyond the provided stack object?
Quoted 7 lines
kernel/bpf/verifier.c +4
-2- err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);[ ... ]
kernel/bpf/verifier.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, · line 9260
Quoted 8 lines
kernel/bpf/verifier.c +1
-1@@ -9251,7 +9255,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, ret =check_func_arg_reg_off (env, reg, argno,ARG_DONTCARE );if (ret <0 )return ret; -if (check_mem_reg (env, reg, argno, arg->mem_size,NULL )) +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 )) {[Severity: Critical]
This isn't a bug introduced by this patch, but is it possible for global subprograms to bypass nullability requirements here?When
check_mem_reg()is called just above this line, it returns 0 early for literal NULL scalars:kernel/bpf/verifier.c:check_mem_reg() { if (bpf_register_is_null(reg)) return 0; ... }
Quoted 4 lines
Because of this early return, the nullability check immediately following
check_mem_reg()inbtf_check_func_arg_match()fails to catch the NULL. The check relies on thePTR_MAYBE_NULLflag, which a literal NULL scalar (SCALAR_VALUE) does not have.
This has been fixed in bpf.
Re: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs
In reply to [email protected]Quoted 67 lines · On Fri, Jul 24, 2026 at 12:28 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] Multi-line comment does not follow the required BPF subsystem comment style.Pre-existing issues:
- [Critical] Pre-existing verifier panic when an array map is passed to kfuncs likebpf_wq_init.
- [High] Pre-existing logic bug allows mismatching maps to bypass safety checks in kfuncs. ----- Patch [2]: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs --- commit
08bfe87a36be7126ec3db2d3ab4783e72b62e453
Author: Amery Hung <[email protected]>bpf: Unify const map ptr argument checking for helpers and kfuncs
Both the helper
ARG_CONST_MAP_PTRand the kfuncKF_ARG_PTR_TO_MAPrecorded the map pointer inmeta->mapand, when a map was already bound by a preceding timer/workqueue/task_work argument, rejected a mismatching map. This logic is factored into a singleprocess_map_ptr_arg()used by both paths.kernel/bpf/verifier.c static int get_constant_map_key(struct bpf_verifier_env *env, · line 8280
Quoted 12 lines
kernel/bpf/verifier.c +4
-0@@ -8274,6 +8274,44 @@ static int get_constant_map_key(struct bpf_verifier_env *env,static bool can_elide_value_nullness (const struct bpf_map *map); +static int process_map_ptr_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +argno_t argno,struct bpf_call_arg_meta *meta) +{ +/* Use map_uid (which is unique id of inner map) to reject: [Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF subsystem formatting?According to the subsystem guidelines, multi-line comments should have the opening /* on its own line:
/*
* Usemap_uid(which is unique id of inner map) to reject:
* ...
*/[ ... ]
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12126
Quoted 6 lines
kernel/bpf/verifier.c +0
-2@@ -12122,36 +12140,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me reg_arg_name(env, argno));return -EINVAL ; } -if (meta->map.ptr && (reg->map_ptr->record->wq_off >=0 || - reg->map_ptr->record->task_work_off >=0 )) {[Severity: Critical]
This is a pre-existing issue that was not introduced by this patch (in fact, the patch appears to fix it!), but could the old code crash the verifier here if a BPF program calledbpf_wq_init(wq, map, 0)and passed an array map without BTF for the map argument?Since an array map without BTF has a NULL record, evaluating
reg->map_ptr->record->wq_offwould result in an unconditional NULL pointer dereference, leading to a panic. This could be triggered by any user with privileges to load BPF programs.
[...]
Quoted 5 lines
[Severity: High]
This is also a pre-existing issue that was not introduced by this patch, but did this original condition allow mismatching maps to bypass safety checks?
Quoted 8 lines
If a BPF program called
bpf_wq_init(wq, map, 0)with a valid workqueue but passed a mismatched map (like a map containing only abpf_timer), the mismatched map would lack bothwq_offandtask_work_off. This would make the condition evaluate to false, bypassing the mismatch rejection and potentially leading to type confusion or memory corruption during runtime callbacks.Quoted 2 lines
kernel/bpf/verifier.c +0
-2-/* Use map_uid (which is unique id of inner map) to reject: -* inner_map1 = bpf_map_lookup_elem(outer_map, key1)
Yes and that is why this patch use meta->map.ptr->record that is already verified. I can also separate it into a standalone fix targeting bpf if needed.
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf-next v2 14/18] bpf: Check helper mem+size in ARG_PTR_TO_MEM case
In reply to [email protected]Quoted 38 lines · On Fri, Jul 24, 2026 at 12:47 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Callingcheck_mem_size_reg()on the size register before validating its type allows an unprivileged user to trigger a kernel warning (WARN_ONCE) and potential panic, leading to a local Denial of Service. ----- Patch [14]: [PATCH bpf-next v2 14/18] bpf: Check helper mem+size in
ARG_PTR_TO_MEMcase --- commit49ede5ce4d16f1df74560df7eef17176d13cb3e1
Author: Amery Hung <[email protected]>bpf: Check helper mem+size in
ARG_PTR_TO_MEMcaseThis commit modifies the BPF verifier to check variable-size memory arguments and their associated size registers simultaneously during the
ARG_PTR_TO_MEMcase, rather than waiting for theARG_MEM_SIZEargument. It looks ahead to regno + 1 to process the size register.Quoted 4 lines
kernel/bpf/verifier.c +0
-0[ ... ]
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8521
Quoted 11 lines
kernel/bpf/verifier.c +7
-0@@ -8515,14 +8512,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return err;if (arg_type &MEM_ALIGNED ) err =check_ptr_alignment (env, reg,0 , fn->arg_size[arg], true); + }else { +struct bpf_reg_state *size_reg =reg_state (env, regno +1 ); + + err =check_mem_size_reg (env, reg, size_reg, argno, +argno_from_reg (regno +1 ), + arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , + fn->arg_type[arg +1 ] &SCALAR_MAYBE_ZERO , meta);[Severity: High]
Could this call tocheck_mem_size_reg()process a non-scalar register?
Quoted 4 lines
By looking ahead and processing
size_regat regno + 1 here, it seems this is happening before the size register's type is validated in the next loop iteration ofcheck_func_arg().
This is a real issue.
I will drop patch 15, not moving helper check_mem_size_reg(). Then for kfunc, defer check_mem_size_reg() in KF_ARG_MEM_SIZE and KF_ARG_CONST_MEM_SIZE similar to helper.
Quoted 16 lines
If a BPF program passes a pointer (like
PTR_TO_MAP_VALUEwith var_off=0) instead of a scalar for the size argument, it appears it would bypass the scalar bounds checks. Whencheck_mem_size_reg()subsequently callsmark_chain_precision()on that non-scalar register, could this trigger averifier_bug()andBPF_WARN_ONCE()?On systems with
panic_on_warnenabled, this might lead to a local denial of service.kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8523
Quoted 2 lines
kernel/bpf/verifier.c +0
-0}break ;[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs
In reply to Amery HungQuoted 82 lines · Amery Hung
Quoted 67 lines · On Fri, Jul 24, 2026 at 12:28 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
New issues:
- [Low] Multi-line comment does not follow the required BPF subsystem comment style.Pre-existing issues:
- [Critical] Pre-existing verifier panic when an array map is passed to kfuncs likebpf_wq_init.
- [High] Pre-existing logic bug allows mismatching maps to bypass safety checks in kfuncs. ----- Patch [2]: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs --- commit
08bfe87a36be7126ec3db2d3ab4783e72b62e453
Author: Amery Hung <[email protected]>bpf: Unify const map ptr argument checking for helpers and kfuncs
Both the helper
ARG_CONST_MAP_PTRand the kfuncKF_ARG_PTR_TO_MAPrecorded the map pointer inmeta->mapand, when a map was already bound by a preceding timer/workqueue/task_work argument, rejected a mismatching map. This logic is factored into a singleprocess_map_ptr_arg()used by both paths.kernel/bpf/verifier.c static int get_constant_map_key(struct bpf_verifier_env *env, · line 8280
Quoted 12 lines
kernel/bpf/verifier.c +4
-0@@ -8274,6 +8274,44 @@ static int get_constant_map_key(struct bpf_verifier_env *env,static bool can_elide_value_nullness (const struct bpf_map *map); +static int process_map_ptr_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +argno_t argno,struct bpf_call_arg_meta *meta) +{ +/* Use map_uid (which is unique id of inner map) to reject: [Severity: Low]
This isn't a bug, but does this multi-line comment follow the preferred BPF subsystem formatting?According to the subsystem guidelines, multi-line comments should have the opening /* on its own line:
/*
* Usemap_uid(which is unique id of inner map) to reject:
* ...
*/[ ... ]
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12126
Quoted 6 lines
kernel/bpf/verifier.c +0
-2@@ -12122,36 +12140,9 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me reg_arg_name(env, argno));return -EINVAL ; } -if (meta->map.ptr && (reg->map_ptr->record->wq_off >=0 || - reg->map_ptr->record->task_work_off >=0 )) {[Severity: Critical]
This is a pre-existing issue that was not introduced by this patch (in fact, the patch appears to fix it!), but could the old code crash the verifier here if a BPF program calledbpf_wq_init(wq, map, 0)and passed an array map without BTF for the map argument?Since an array map without BTF has a NULL record, evaluating
reg->map_ptr->record->wq_offwould result in an unconditional NULL pointer dereference, leading to a panic. This could be triggered by any user with privileges to load BPF programs.[...]
Quoted 11 lines
[Severity: High]
This is also a pre-existing issue that was not introduced by this patch, but did this original condition allow mismatching maps to bypass safety checks?If a BPF program called
bpf_wq_init(wq, map, 0)with a valid workqueue but passed a mismatched map (like a map containing only abpf_timer), the mismatched map would lack bothwq_offandtask_work_off. This would make the condition evaluate to false, bypassing the mismatch rejection and potentially leading to type confusion or memory corruption during runtime callbacks.
Quoted 7 lines
Quoted 2 lines
kernel/bpf/verifier.c
Quoted 2 lines
kernel/bpf/verifier.c +0
-2-/* Use map_uid (which is unique id of inner map) to reject: -* inner_map1 = bpf_map_lookup_elem(outer_map, key1) Yes and that is why this patch use
meta->map.ptr->recordthat is already verified. I can also separate it into a standalone fix targeting bpf if needed.
I think keeping it in your set is fine. Since this is not reported I doubt anyone hit this in practice, though I agree it's worth fixing.
Quoted 3 lines
Quoted 3 lines
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH bpf-next v2 11/18] bpf: Classify kfunc mem_size args from BTF without register state
In reply to [email protected]Quoted 58 lines · On Fri, Jul 24, 2026 at 12:38 PM <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Removing theSCALAR_VALUEcheck fromis_kfunc_arg_mem_size()andis_kfunc_arg_const_mem_size()allows unvalidated pointer registers to be passed as memory size arguments during kfunc lookahead validation, triggering averifier_bugkernel warning splat and local DoS. ----- Patch [11]: [PATCH bpf-next v2 11/18] bpf: Classify kfunc
mem_sizeargs from BTF without register state --- commit55013229ec44003dda12328783652d410aa4f75e
Author: Amery Hung <[email protected]>bpf: Classify kfunc
mem_sizeargs from BTF without register stateThis commit drops the scalar register type check in
is_kfunc_arg_mem_size()andis_kfunc_arg_const_mem_size()to decoupleget_kfunc_ptr_arg_type()from register state.Quoted 11 lines
kernel/bpf/verifier.c +1
-2@@ -10795,26 +10795,24 @@ static bool is_kfunc_rcu_protected(struct bpf_call_arg_meta *meta) }static bool is_kfunc_arg_mem_size (const struct btf *btf, -const struct btf_param *arg, -const struct bpf_reg_state *reg) +const struct btf_param *arg)kernel/bpf/verifier.c static bool is_kfunc_rcu_protected(struct bpf_call_arg_meta *meta) · line 10807
Quoted 10 lines
kernel/bpf/verifier.c +1
-1{const struct btf_type *t; t =btf_type_skip_modifiers (btf, arg->type,NULL ); -if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +if (!btf_type_is_scalar (t))return false;return btf_param_match_suffix (btf, arg,"__sz" ); }[Severity: High]
Could this removal allow a pointer register to be incorrectly classified as a memory size parameter during kfunc lookahead validation?If a BPF program passes a pointer register (such as
PTR_TO_STACKwith a fixed offset of 0) instead of a scalar to a size argument suffixed with__sz,get_kfunc_ptr_arg_type()will now look ahead and classify it solely based on the BTF signature.Because of this classification,
check_kfunc_args()eagerly processes the unverified register as a size parameter before the generic scalar validation loop catches it:
check_kfunc_args()
...
check_mem_size_reg(env, buff_reg, size_reg, argno, next_argno, ...)Inside
check_mem_size_reg(), the pointer with a fixed offset of 0 yields a size of 0. Since zero-size is allowed, it passes the bounds check and callsmark_chain_precision()on the pointer register.
Quoted 7 lines
Because
mark_chain_precision()expects aSCALAR_VALUE, it triggers averifier_bugsplat for backtracking misuse when given a pointer. On systems configured withpanic_on_warn, this creates a local denial of service regression.Should the register type check be retained here, or is there another way to prevent pointer registers from bypassing validation during the lookahead?
I will change check_mem_size_reg() to be lookback by calling it in the KF_ARG_MEM_SIZE case in patch 13 ("bpf: Distinguish fixed- and variable-size kfunc mem args with MEM_FIXED_SIZE").
It is not bisect safe, but it will be free of this issue after patch 13 in the next respin.
Quoted 5 lines
[ ... ]
--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Re: [PATCH bpf-next v2 01/18] bpf: Drop process_timer_func wrappers
In reply to Amery HungQuoted 5 lines · Amery Hung
Drop process_timer_{helper,kfunc}() since
bpf_call_arg_metais now shared by helper and kfunc. Callprocess_timer_func()directly.Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 02/18] bpf: Unify const map ptr argument checking for helpers and kfuncs
In reply to Amery HungQuoted 5 lines · Amery Hung
Both the helper
ARG_CONST_MAP_PTRand the kfuncKF_ARG_PTR_TO_MAPrecorded the map pointer inmeta->mapand, when a map was already bound by a preceding timer/workqueue/task_work argument, rejected a mismatching map.
Quoted 8 lines
Factor the logic into a single
process_map_ptr_arg()used by both paths. The bound-object name (timer, workqueue, orbpf_task_work) is derived from the bound map'sbtf_record, and the register numbers in the message are computed from the map argument position instead of being hard-coded.Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 03/18] bpf: Split kfunc map argument into __const_map and __map
In reply to a message not in the archiveQuoted 6 lines · Amery Hung
Kfuncs used a single '
__map' suffix (KF_ARG_PTR_TO_MAP) for two different
things: a verifier-known map matched bymap_uidagainst a bound timer/wq/task_work object (bpf_wq_init, bpf_task_work_schedule*), and an opaque 'structbpf_map*' used only at runtime (bpf_arena_*), which may be a map fd or aPTR_TO_BTF_IDstructbpf_map(e.g. abpf_mapiterator'sctx->map).
Quoted 6 lines
That combined path only accepted the btf map form due to type confusion. The 'if (!reg->map_ptr)' check reads
reg->map_ptr, which aliasesreg->btfin thebpf_reg_stateunion. APTR_TO_BTF_IDregister always has a non-NULLreg->btf, so the guard silently passed and validation fell through toprocess_kf_arg_ptr_to_btf_id(). It also recordedPTR_TO_BTF_IDinfo inmeta->map, which would be meaningless.
What a bug, I wonder if we have more places like that. In LLVM code base they use the following pattern:
class foo {
int tag;
union {
struct bar_info { ... } bar;
struct buz_info { ... } buz;
};
};
struct bar_info *get_bar(class foo *f) {
if (f->tag != BAR)
fatal_error(); // the error is compiled away in release builds
return &f->bar;
}And similar pattern with checks in getter for just mutually exclusive fields. I wonder if we should adopt the same approach.
Quoted 6 lines
Split the annotation to avoid such type confusion and to align with helper:
- '
__const_map' ->KF_ARG_CONST_MAP_PTR: verifier-known map, handled byprocess_map_ptr_arg()like helperARG_CONST_MAP_PTR.
Quoted 7 lines
- '
__map' ->KF_ARG_PTR_TO_BTF_ID: opaque structbpf_map, validated byprocess_kf_arg_ptr_to_btf_id(). A map fd still matches via reg2btf_ids[CONST_PTR_TO_MAP], sobpf_arena_alloc_pages(&map)keeps working.Signed-off-by Amery Hung <[email protected]>---
Re: [PATCH bpf-next v2 04/18] bpf: Pass kfunc meta to mem and mem_size check
In reply to Amery HungQuoted 6 lines · Amery Hung
kfunc now shares the same
bpf_call_arg_metawith helpers. Pass kfunc's own meta tocheck_mem_reg()andcheck_kfunc_mem_size()instead of NULL or a temporary meta on the stack.Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Amery HungQuoted 14 lines · Amery Hung
Helper
ARG_CONST_SIZEand kfuncKF_ARG_PTR_TO_MEM_SIZEmemory arguments already sharecheck_mem_size_reg(), but only the kfunc wrappercheck_kfunc_mem_size_reg()ranmark_ptr_not_null_reg()on a possibly-NULL pointer before the access check. That call does more than clearingPTR_MAYBE_NULL: it converts special map-value-or-null registers to their real non-NULL type, e.g. a map-in-map value toCONST_PTR_TO_MAP. Without it the register keeps base typePTR_TO_MAP_VALUEandcheck_map_access()treats it as raw data.Because the helper path skipped that step, an un-narrowed map-of-maps value could be passed to a nullable helper mem argument (e.g.
bpf_csum_diff()) and read as bytes, leaking the inner map descriptor (a kernel pointer). The kfunc path already rejected the same program.
Quoted 7 lines
Handle the possibly-NULL pointer in the shared mem-argument checking so helper and kfunc arguments are verified the same way, and drop the now redundant
check_kfunc_mem_size_reg()wrapper. Both paths now reject an un-narrowed map-in-map value used as memory.Signed-off-by Amery Hung <[email protected]>---
...
kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env,
Quoted 21 lines
kernel/bpf/verifier.c +8
-1@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env, bool zero_size_allowed, struct bpf_call_arg_meta *meta) { -int err; +bool may_be_null =type_may_be_null (mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }
/* This is used to refine r0 return value bounds for helpers
* that enforce this value as an upper bound on return values.kernel/bpf/verifier.c +0
-0@@ -6910,8 +6917,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,
Nit: change the prototype of the check_mem_size_reg() to have 'access_type'
declared as u32? As it is no longer an enum, but a mask.
...
I think there is a followup for this patch, consider the selftest added by the next patch:
int mapofmaps_value_as_helper_mem_buf(struct __sk_buff *skb)
{
__u32 key = 0;
void *inner;
inner = bpf_map_lookup_elem(&outer_map, &key);
/* intentionally NOT NULL-checked: reg stays PTR_TO_MAP_VALUE_OR_NULL */
/* @from is a nullable read-only mem+size arg; outer value_size is 4 */
return bpf_csum_diff(inner, 4, NULL, 0, 0) + skb->len;}
It reports the following error with this patch-set applied:
; inner = bpf_map_lookup_elem(&outer_map, &key); @ verifier_mem_size_reg.c:55
5: (18) r1 = 0xffff88810813c000 ; R1=map_ptr(map=outer_map,ks=4,vs=4)
7: (85) call bpf_map_lookup_elem#1 ; R0=map_value_or_null(id=1,map=outer_map,ks=4,vs=4)
; return bpf_csum_diff(inner, 4, NULL, 0, 0) + skb->len; @ verifier_mem_size_reg.c:59
8: (bf) r1 = r0 ; R0=map_value_or_null(id=1,map=outer_map,ks=4,vs=4) R1=map_value_or_null(id=1,map=outer_map,ks=4,vs=4)
9: (b4) w2 = 4 ; R2=4
10: (b7) r3 = 0 ; R3=0
11: (b4) w4 = 0 ; R4=0
12: (b4) w5 = 0 ; R5=0
13: (85) call bpf_csum_diff#28
R1 type=map_ptr expected=fp
^^^^^^^^^^^That's not the right thing to report here.
...
Re: [PATCH bpf-next v2 06/18] selftests/bpf: Add tests for helper and kfunc mem+size arguments
In reply to Amery HungQuoted 9 lines · Amery Hung
Make sure the return of map-of-maps lookup cannot be passed to nullable memory buffer argument for helper and kfunc.
-
mapofmaps_value_as_kfunc_mem_buf: an un-narrowed (possibly-NULL) map-of-maps value must not be usable as bpf_dynptr_slice()'s__nullablebuffer.mark_ptr_not_null_reg()converts such a value toCONST_PTR_TO_MAP; without itcheck_map_access()would let the program read the inner map descriptor as raw bytes.
Quoted 6 lines
-
mapofmaps_value_as_helper_mem_buf: the same map-of-maps value passed to a nullable helper mem argument (bpf_csum_diff()) must be rejected too, guarding that helper and kfunc arguments are checked the same way.Signed-off-by Amery Hung <[email protected]>---
(Modulo error message issue raised in the previous email).
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Amery HungOn Fri, 2026-07-24 at 12:07 -0700, Amery Hung wrote:
...
Quoted 7 lines
kernel/bpf/verifier.c +0
-0@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env, bool zero_size_allowed, struct bpf_call_arg_meta *meta)
kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env, · line 6883
Quoted 10 lines
kernel/bpf/verifier.c +8
-1{ -int err; +bool may_be_null =type_may_be_null (mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }
Also, this pre-existing hack with saved_reg and mark_ptr_not_null_reg() is quite ugly. Consider mark_ptr_not_null_reg():
static void mark_ptr_not_null_reg(struct bpf_reg_state *reg)
{
if (base_type(reg->type) == PTR_TO_MAP_VALUE) {
const struct bpf_map *map = reg->map_ptr;
if (map->inner_map_meta) {
reg->type = CONST_PTR_TO_MAP;
reg->map_ptr = map->inner_map_meta;
/* transfer reg's id which is unique for every map_lookup_elem
* as UID of the inner map.
*/
if (btf_record_has_field(map->inner_map_meta->record,
BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) {
reg->map_uid = reg->id;
}
} else if (map->map_type == BPF_MAP_TYPE_XSKMAP) {
reg->type = PTR_TO_XDP_SOCK;
} else if (map->map_type == BPF_MAP_TYPE_SOCKMAP ||
map->map_type == BPF_MAP_TYPE_SOCKHASH) {
reg->type = PTR_TO_SOCKET;
} else {
reg->type = PTR_TO_MAP_VALUE;
}
return;
}
reg->type &= ~PTR_MAYBE_NULL;}
Note that the check_helper_mem_access() already calls base_type(mem_reg), so the '&= ~PTR_MAYBE_NULL' part is irrelevant, what matters are the following mutations:
- PTR_TO_MAP_VALUE -> CONST_PTR_TO_MAP (plus it's uid, type, ptr)
- BPF_MAP_TYPE_XSKMAP -> PTR_TO_XDP_SOCK
- BPF_MAP_TYPE_SOCKMAP -> PTR_TO_SOCKET
- BPF_MAP_TYPE_SOCKHASH -> PTR_TO_SOCKET
A better design compared to current temporary fixup for a helper/kfunc call would be to deal with types CONST_PTR_TO_MAP | PTR_MAYBE_NULL, PTR_TO_XDP_SOCK | PTR_MAYBE_NULL etc, instantiated at map lookup. Wdyt?
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to Amery HungOn Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6971
Quoted 6 lines
kernel/bpf/verifier.c +0
-0@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg mark_ptr_not_null_reg(reg); }kernel/bpf/verifier.c +1
-1-int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
When is this negation necessary?
Is there a way to get rid of it?
Quoted 10 lines
kernel/bpf/verifier.c +4
-2- err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);
if (may_be_null)
*reg =saved_reg;
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 08/18] bpf: Express ARG_CONST_SIZE_OR_ZERO as ARG_CONST_SIZE | SCALAR_MAYBE_ZERO
In reply to Amery HungQuoted 17 lines · Amery Hung
Add
SCALAR_MAYBE_ZEROas an alias ofPTR_MAYBE_NULLfor scalar size arguments, and defineARG_CONST_SIZE_OR_ZEROasARG_CONST_SIZE|SCALAR_MAYBE_ZEROinstead of a distinct base type. This letscheck_func_arg()handle both with a singleARG_CONST_SIZEcase, and the redundant compatible_reg_types[] entry is dropped.Signed-off-by Amery Hung <[email protected]>---
include/linux/bpf.h | 4 +++-
kernel/bpf/verifier.c | 12 ++----------2 files changed, 5 insertions(+), 11 deletions(-)include/linux/bpf.h +0
-0
include/linux/bpf.h extern const struct bpf_map_ops bpf_map_offload_ops; · line 751
Quoted 4 lines
include/linux/bpf.h +1
-0@@ -748,6 +748,7 @@ extern const struct bpf_map_ops bpf_map_offload_ops; enum bpf_type_flag { /* PTR may be NULL. */ PTR_MAYBE_NULL = BIT(0 + BPF_BASE_TYPE_BITS), + SCALAR_MAYBE_ZERO = PTR_MAYBE_NULL,
I'd suggest to avoid adding aliases, this might be error-prone moving forward. Is this change crucial for the entire kfunc/helper merge? Does not seem to win much on it's own.
...
Re: [PATCH bpf-next v2 10/18] bpf: Fold __szk const size handling into the scalar arg path
In reply to Amery HungQuoted 11 lines · Amery Hung
To align helper and kfunc pointer to memory argument handling, move kfunc constant memorry size argument handling to the kfunc scalar section. In addition, factor out constant scalar argument handling.
The constant size argument (
__szk) of a kfunc memory/size pair was recorded intometa->arg_constantby a dedicated block in theKF_ARG_PTR_TO_MEM_SIZEcase, duplicating the "only one constant argument" and "must be a known constant" checks already in the generic scalar argument handling. That block also did an explicit i++ to skip the size argument.
Quoted 7 lines
This also fixes a precision gap: the old dedicated block did not mark the size register precise, relying on
check_mem_size_reg()for that. Butcheck_mem_size_reg()is skipped when the buffer is a nullable arg passed as NULL (e.g.bpf_dynptr_slice(_rdwr)with a NULL buffer), so in that case the__szkvalue was recorded and used for regs[R0].mem_size without marking it precise. Routing the size through the scalar path marks it precise in all cases.
Could you please add a test case for this?
Quoted 3 lines
Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 11/18] bpf: Classify kfunc mem_size args from BTF without register state
In reply to Amery HungQuoted 8 lines · Amery Hung
check_kfunc_args()already makes sure a scalar value is passed to a scalar kfunc argument. Drop the check inis_kfunc_arg_mem_size()andis_kfunc_arg_const_mem_size()to further decoupleget_kfunc_ptr_arg_type()from register state (a prerequisite for generating a helper-like prototype from kfunc's BTF).Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 12/18] bpf: Handle NULL kfunc pointer args without a KF_ARG_PTR_TO_NULL type
In reply to Amery HungQuoted 14 lines · Amery Hung
get_kfunc_ptr_arg_type()returnedKF_ARG_PTR_TO_NULLwhen a nullable pointer argument was passed a NULL register. This folded a register-state decision (bpf_register_is_null()) into what is otherwise BTF-based argument classification, and it short-circuited before the BTF_ID/MEM resolution.Drop
KF_ARG_PTR_TO_NULLand handle the NULL case incheck_kfunc_args()
instead: a nullable argument that is actually NULL is skipped. Note that it is okay to skip even when it is a mem+size pair because the size argument check has been moved to the scalar section. The skip is done beforeget_kfunc_ptr_arg_type()so that a NULL passed to a nullable non-scalar-struct argument is not newly rejected by the BTF_ID/MEM resolution.
Quoted 4 lines
No functional change.
Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 13/18] bpf: Distinguish fixed- and variable-size kfunc mem args with MEM_FIXED_SIZE
In reply to Amery HungQuoted 15 lines · Amery Hung
A kfunc memory-pointer argument comes in two flavors: a fixed-size buffer whose access size is derived from the pointed-to BTF type, and a variable-size buffer paired with a following __sz/__szk size argument. Both were represented by separate
kfunc_ptr_arg_typevalues (KF_ARG_PTR_TO_MEMvsKF_ARG_PTR_TO_MEM_SIZE) with the pointer classified as the latter when a size argument followed.Mirror how helpers describe the same distinction: classify both as
KF_ARG_PTR_TO_MEMand OR inMEM_FIXED_SIZEfor the fixed-size case, just as helpers useARG_PTR_TO_MEM|MEM_FIXED_SIZE. The switches now key onbase_type(kf_arg_type)so the flag rides along, and theKF_ARG_PTR_TO_MEMhandler either resolves the size from BTF (MEM_FIXED_SIZE) or falls through to the mem/size-pair check, which validates the buffer against the following size register and skips it. No functional change.
Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 14/18] bpf: Check helper mem+size in ARG_PTR_TO_MEM case
In reply to Amery HungOn Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 21 lines
kernel/bpf/verifier.c | 16 +++++++---------
1 file changed, 7 insertions(+), 9 deletions(-)kernel/bpf/verifier.c +0
-0@@ -8505,9 +8505,6 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, meta->subprogno = reg->subprogno; break;case
ARG_PTR_TO_MEM:kernel/bpf/verifier.c +0
-3-/* The access to this pointer is only checked when we hit the -* next is_mem_size argument below. -*/ if (
arg_type&MEM_FIXED_SIZE) {
err = check_mem_reg(env, reg,argno_from_reg(regno), fn->arg_size[arg],arg_type&MEM_WRITE?BPF_WRITE:BPF_READ, meta);kernel/bpf/verifier.c +0
-0@@ -8515,14 +8512,15 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, return err; if (arg_type & MEM_ALIGNED) err = check_ptr_alignment(env, reg, 0, fn->arg_size[arg], true);
kernel/bpf/verifier.c static int check_func_arg(struct bpf_verifier_env *env, u32 arg, · line 8520
Quoted 10 lines
kernel/bpf/verifier.c +7
-1+ }else { +struct bpf_reg_state *size_reg =reg_state (env, regno +1 ); + + err =check_mem_size_reg (env, reg, size_reg, argno, +argno_from_reg (regno +1 ), + arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , + fn->arg_type[arg +1 ] &SCALAR_MAYBE_ZERO , meta); } break; -case ARG_MEM_SIZE :
Nit:
The ARG_MEM_SIZE would be ignored by this switch after the change, right? This is a correct thing to do, but I'd be a little bit more defensive. Let's modify this switch to:
- explicitly skip ARG_MEM_SIZE
- have default clause signalling a verifier error in case of unknown arg type.
Quoted 8 lines
kernel/bpf/verifier.c +0
-5- err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg, -argno_from_reg (regno -1 ), argno, - fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ , - arg_type &SCALAR_MAYBE_ZERO , meta); -break ;case
ARG_PTR_TO_DYNPTR:
err =process_dynptr_func(env, reg, argno, insn_idx, arg_type, &meta->ref_obj, &meta->dynptr);
Re: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Eduard ZingermanQuoted 21 lines · Eduard Zingerman
Quoted 19 lines · Amery Hung
Helper
ARG_CONST_SIZEand kfuncKF_ARG_PTR_TO_MEM_SIZEmemory arguments already sharecheck_mem_size_reg(), but only the kfunc wrappercheck_kfunc_mem_size_reg()ranmark_ptr_not_null_reg()on a possibly-NULL pointer before the access check. That call does more than clearingPTR_MAYBE_NULL: it converts special map-value-or-null registers to their real non-NULL type, e.g. a map-in-map value toCONST_PTR_TO_MAP. Without it the register keeps base typePTR_TO_MAP_VALUEandcheck_map_access()treats it as raw data.Because the helper path skipped that step, an un-narrowed map-of-maps value could be passed to a nullable helper mem argument (e.g.
bpf_csum_diff()) and read as bytes, leaking the inner map descriptor (a kernel pointer). The kfunc path already rejected the same program.Handle the possibly-NULL pointer in the shared mem-argument checking so helper and kfunc arguments are verified the same way, and drop the now redundant
check_kfunc_mem_size_reg()wrapper. Both paths now reject an un-narrowed map-in-map value used as memory.
Quoted 5 lines
Quoted 2 lines
Signed-off-by Amery Hung <[email protected]>---
Reviewed-by Eduard Zingerman <[email protected]>
Thank you reviewing the patchset!
Quoted 20 lines
...
Quoted 18 lines
kernel/bpf/verifier.c +8
-1@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,bool zero_size_allowed, struct bpf_call_arg_meta *meta) { - int err; + bool may_be_null = type_may_be_null(mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }
Quoted 6 lines
kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env,
Quoted 3 lines
kernel/bpf/verifier.c +0
-0/* This is used to refine r0 return value bounds for helpers * that enforce this value as an upper bound on return values. @@ -6910,8 +6917,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,Nit: change the prototype of the
check_mem_size_reg()to have 'access_type'
declared as u32? As it is no longer an enum, but a mask.
Make sense. Will change.
Quoted 32 lines
...
I think there is a followup for this patch, consider the selftest added by the next patch:
int mapofmaps_value_as_helper_mem_buf(struct __sk_buff *skb) { __u32 key = 0; void *inner;inner = bpf_map_lookup_elem(&outer_map, &key); /* intentionally NOT NULL-checked: reg stays PTR_TO_MAP_VALUE_OR_NULL */ /* @from is a nullable read-only mem+size arg; outer value_size is 4 */ return bpf_csum_diff(inner, 4, NULL, 0, 0) + skb->len;}
It reports the following error with this patch-set applied:
; inner =
bpf_map_lookup_elem(&outer_map, &key); @ verifier_mem_size_reg.c:55
5: (18) r1 =0xffff88810813c000; R1=map_ptr(map=outer_map,ks=4,vs=4)
7: (85) call bpf_map_lookup_elem#1 ; R0=map_value_or_null(id=1,map=outer_map,ks=4,vs=4)
; returnbpf_csum_diff(inner, 4, NULL, 0, 0)+skb->len; @ verifier_mem_size_reg.c:598: (bf) r1 = r0 ; R0=map_value_or_null(id=1,map=outer_map,ks=4,vs=4) R1=map_value_or_null(id=1,map=outer_map,ks=4,vs=4) 9: (b4) w2 = 4 ; R2=4 10: (b7) r3 = 0 ; R3=0 11: (b4) w4 = 0 ; R4=0 12: (b4) w5 = 0 ; R5=0 13: (85) call bpf_csum_diff#28 R1 type=map_ptr expected=fp ^^^^^^^^^^^
Quoted 1 line
That's not the right thing to report here.
I will include this in a follow-up patchset.
Quoted 2 lines
...
Re: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Eduard ZingermanQuoted 63 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:07 -0700, Amery Hung wrote:
...
Quoted 7 lines
kernel/bpf/verifier.c +0
-0@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,bool zero_size_allowed, struct bpf_call_arg_meta *meta)kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env, · line 6883
Quoted 10 lines
kernel/bpf/verifier.c +8
-1{ -int err; +bool may_be_null =type_may_be_null (mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }Also, this pre-existing hack with
saved_regandmark_ptr_not_null_reg()is quite ugly. Considermark_ptr_not_null_reg():static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) { if (base_type(reg->type) == PTR_TO_MAP_VALUE) { const struct bpf_map *map = reg->map_ptr;if (map->inner_map_meta) { reg->type = CONST_PTR_TO_MAP; reg->map_ptr = map->inner_map_meta; /* transfer reg's id which is unique for every map_lookup_elem * as UID of the inner map. */ if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) { reg->map_uid = reg->id; } } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { reg->type = PTR_TO_XDP_SOCK; } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || map->map_type == BPF_MAP_TYPE_SOCKHASH) { reg->type = PTR_TO_SOCKET; } else { reg->type = PTR_TO_MAP_VALUE; } return; } reg->type &= ~PTR_MAYBE_NULL;}
Note that the
check_helper_mem_access()already callsbase_type(mem_reg), so the '&= ~PTR_MAYBE_NULL' part is irrelevant, what matters are the following mutations:
-PTR_TO_MAP_VALUE->CONST_PTR_TO_MAP(plus it's uid, type, ptr)
-BPF_MAP_TYPE_XSKMAP->PTR_TO_XDP_SOCK
-BPF_MAP_TYPE_SOCKMAP->PTR_TO_SOCKET
-BPF_MAP_TYPE_SOCKHASH->PTR_TO_SOCKET
Quoted 4 lines
A better design compared to current temporary fixup for a helper/kfunc call would be to deal with types
CONST_PTR_TO_MAP|PTR_MAYBE_NULL,PTR_TO_XDP_SOCK|PTR_MAYBE_NULLetc, instantiated at map lookup. Wdyt?
I agree that this is a better approach. This can be another follow-up considering that this is an independent refactor and the size of the current set.
Quoted 2 lines
...
Re: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to Eduard ZingermanQuoted 5 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 8 lines
kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6971
Quoted 6 lines
kernel/bpf/verifier.c +1
-1@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regmark_ptr_not_null_reg (reg); } -int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^When is this negation necessary?
This was introduced recently in 2cb27158adb3 ("bpf: poison dead stack slots")
The negative size signals check_stack_range_initialized() to accept STACK_POISON slots (allow_poison). That is only safe for global subprogs since a finer-grain stack slot liveness analysis proves the callee body doesn't read those slots.
Quoted 1 line
Is there a way to get rid of it?
I think this is needed. However, after reading the commit again, I think this should only apply to global subprogs. Current code allows kfunc by mistake. The line should be fixed:
size = (!meta && base_type(reg->type) == PTR_TO_STACK) ? -(int)mem_size : mem_size;
Quoted 13 lines
Quoted 11 lines
kernel/bpf/verifier.c +4
-2- err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);if (may_be_null) *reg = saved_reg;...
Re: [PATCH bpf-next v2 08/18] bpf: Express ARG_CONST_SIZE_OR_ZERO as ARG_CONST_SIZE | SCALAR_MAYBE_ZERO
In reply to Eduard ZingermanQuoted 14 lines · Eduard Zingerman
Quoted 12 lines · Amery Hung
Add
SCALAR_MAYBE_ZEROas an alias ofPTR_MAYBE_NULLfor scalar size arguments, and defineARG_CONST_SIZE_OR_ZEROasARG_CONST_SIZE|SCALAR_MAYBE_ZEROinstead of a distinct base type. This letscheck_func_arg()handle both with a singleARG_CONST_SIZEcase, and the redundant compatible_reg_types[] entry is dropped.Signed-off-by Amery Hung <[email protected]>--- include/linux/bpf.h | 4 +++- kernel/bpf/verifier.c | 12 ++---------- 2 files changed, 5 insertions(+), 11 deletions(-)
Quoted 13 lines
include/linux/bpf.h extern const struct bpf_map_ops bpf_map_offload_ops; · line 751
Quoted 9 lines
include/linux/bpf.h +1
-0@@ -748,6 +748,7 @@ extern const struct bpf_map_ops bpf_map_offload_ops;enum bpf_type_flag {/* PTR may be NULL. */ PTR_MAYBE_NULL =BIT (0 +BPF_BASE_TYPE_BITS ), +SCALAR_MAYBE_ZERO =PTR_MAYBE_NULL ,I'd suggest to avoid adding aliases, this might be error-prone moving forward. Is this change crucial for the entire kfunc/helper merge? Does not seem to win much on it's own.
Ack. I will drop it.
Quoted 2 lines
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 15/18] bpf: Classify kfunc pointer arguments from BTF, resolve type against the register
In reply to Amery HungOn Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
kernel/bpf/verifier.c static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { · line 4927
Quoted 9 lines
kernel/bpf/verifier.c +1
-0@@ -4924,6 +4924,19 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { [CONST_PTR_TO_MAP] = btf_bpf_map_id, }; +static enum bpf_reg_type lookup_reg2btf_ids (const struct btf_type *ref_t)
Nit: the function would be a bit simpler, if ref_id is passed as a
parameter instead of ref_t.
Quoted 11 lines
kernel/bpf/verifier.c +11
-0+{ +enum bpf_reg_type type; + +for (type =0 ; type < __BPF_REG_TYPE_MAX; type++) { +if (reg2btf_ids[type] && +btf_type_by_id (btf_vmlinux, *reg2btf_ids[type]) == ref_t) +return type; + } + +return NOT_INIT ; +}
...
Quoted 6 lines
kernel/bpf/verifier.c +0
-2@@ -11429,10 +11431,14 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, is_kfunc_arg_const_mem_size(meta->btf, &args[arg + 1]))) arg_mem_size = true; -/* This is the catch all argument type of register types supported by -* check_helper_mem_access. However, we only allow when argument type is
kernel/bpf/verifier.c get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, · line 11441
Quoted 10 lines
kernel/bpf/verifier.c +8
-2-* pointer to scalar, or struct composed (recursively) of scalars. When -* arg_mem_size is true, the pointer can be void *. +/* A pointer to a struct without a size argument is classified as KF_ARG_PTR_TO_BTF_ID */ +if (btf_type_is_struct (ref_t) && !arg_mem_size) +return KF_ARG_PTR_TO_BTF_ID ; + +/* +* Otherwise this is a memory buffer supported by check_helper_mem_access(): a pointer +* to a scalar, or to void when paired with a size argument. The access size is derived +* from the pointed-to BTF type unless a size argument follows.
Nit: below conditions would be easier to read if the whole
thing was structured as:
if (arg_mem_size) {
if (!btf_type_is_void(ref_t)) {
...
return -EINVAL;
}
return KF_ARG_PTR_TO_MEM;
} else {
if (btf_type_is_struct(ref_t))
return KF_ARG_PTR_TO_BTF_ID;
if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(ref_t)) {
...
return -EINVAL;
}
return KF_ARG_PTR_TO_MEM || MEM_FIXED_SIZE;
}Quoted 3 lines
kernel/bpf/verifier.c +0
-0*/ if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) {
...
Quoted 46 lines
kernel/bpf/verifier.c +31
-9@@ -12377,20 +12370,46 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mebreak ;case KF_ARG_PTR_TO_BTF_ID :/* Only base_type is checked, further checks are done here */ -if ((base_type (reg->type) !=PTR_TO_BTF_ID || - (bpf_type_has_unsafe_modifiers (reg->type) && !is_rcu_reg (reg))) && - !reg2btf_ids[base_type (reg->type)]) { -verbose (env,"%s is %s " ,reg_arg_name (env, argno), -reg_type_str (env, reg->type)); -verbose (env,"expected %s or socket\n" , -reg_type_str (env,base_type (reg->type) | - (type_flag (reg->type) &BPF_REG_TRUSTED_MODIFIERS ))); +if (base_type (reg->type) ==PTR_TO_BTF_ID || + reg2btf_ids[base_type (reg->type)]) { +if (!is_trusted_reg (env, reg) || +bpf_type_has_unsafe_modifiers (reg->type)) { +if (!is_kfunc_rcu (meta)) { +verbose (env,"%s must be referenced or trusted\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } +if (!is_rcu_reg (reg)) { +verbose (env,"%s must be a rcu pointer\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } + } + + ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno); +if (ret <0 ) +return ret; +break ; + } + +if (!__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 )) { +enum bpf_reg_type reg2btf_type =lookup_reg2btf_ids (ref_t); + +verbose (env,"%s is %s expected %s %s" , +reg_arg_name (env, argno),reg_type_str (env, reg->type), +btf_type_str (ref_t), ref_tname); +if (reg2btf_type !=NOT_INIT ) +verbose (env," or %s" ,reg_type_str (env, reg2btf_type)); +verbose (env,"\n" );return -EINVAL ; } - ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno);
kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12412
Quoted 10 lines
kernel/bpf/verifier.c +7
-3-if (ret <0 ) -return ret; -break ; + +/* +* If the register does not contain btf id but the argument type is a pointer to +* scalar-only struct, allow verifying it as a fixed size memory. +*/ + kf_arg_type =KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + fallthrough;
I think this now accepts one previously rejected case:
- register being PTR_TO_BTF_ID, pointer to scalar
- parameter being pointer to scalar w/o size register. Previously it was rejected by the following code in the get_kfunc_ptr_arg_type():
if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) {
if (!btf_type_is_struct(ref_t)) {
verbose(env, "kernel function %s %s pointer type %s %s is not supported\n",Is this intended?
...
Re: [PATCH bpf-next v2 16/18] bpf: Tag nullable kfunc pointer args with PTR_MAYBE_NULL
In reply to Amery HungQuoted 10 lines · Amery Hung
Now that
get_kfunc_ptr_arg_type()classifies a kfunc pointer argument from its BTF alone, express a nullable argument by OR-ingPTR_MAYBE_NULLinto the classified type, and resolve a NULL register after classification instead of before it.Previously
check_kfunc_args()short-circuited a nullable argument passed a NULL register with a continue placed beforeget_kfunc_ptr_arg_type(), so the NULL never reached classification. That kept a register-state decision (bpf_register_is_null()) ahead of the BTF-based classification.
Quoted 7 lines
This mirrors how helper arguments carry
PTR_MAYBE_NULLin theirbpf_arg_typeand is a step toward describing kfuncs with abpf_func_proto:
the nullability now travels with the per-argument classification, so it is captured when the prototype is generated at add-call time.Signed-off-by Amery Hung <[email protected]>---
...
Re: [PATCH bpf-next v2 17/18] bpf: Classify scalar kfunc arguments from BTF
In reply to Amery HungQuoted 9 lines · Amery Hung
Add kfunc scalar argument types, classify them in
get_kfunc_arg_type()along side with pointer arguments and move scalar type verification into the main switch incheck_kfunc_args(). This keeps BTF-based classification separate from register validation for every argument, paving the way for generating the kfunc argument prototype at add-call time. No functional change intended.Signed-off-by Amery Hung <[email protected]>---
...
kernel/bpf/verifier.c bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta) · line 11367
Quoted 13 lines
kernel/bpf/verifier.c +4
-4@@ -11355,14 +11360,40 @@ bool bpf_is_kfunc_pkt_changing(struct bpf_call_arg_meta *meta) }static int -get_kfunc_ptr_arg_type (struct bpf_verifier_env *env,struct bpf_call_arg_meta *meta, -const struct btf_type *t,const struct btf_type *ref_t, -const char *ref_tname,const struct btf_param *args, -int arg,int nargs,argno_t argno) +get_kfunc_arg_type (struct bpf_verifier_env *env,struct bpf_call_arg_meta *meta, +const struct btf_param *args,int arg,int nargs) { +const struct btf_type *t, *ref_t =NULL ; +argno_t argno =argno_from_arg (arg +1 );
^^^^^^^^^
Nit: this is a bit confusing
Quoted 4 lines
kernel/bpf/verifier.c +1
-0+const char *ref_tname =NULL ;bool arg_mem_size = false;int arg_type;
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 18/18] bpf: Generate kfunc argument prototype at add-call time
In reply to Amery HungOn Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 4 lines
include/linux/bpf_verifier.h +0
-0
...
Quoted 7 lines
include/linux/bpf_verifier.h +1
-2@@ -1305,8 +1306,19 @@ static inline u32 type_flag(u32 type)/* only use after check_attach_btf_id() */ static inline enum bpf_prog_type resolve_prog_type (const struct bpf_prog *prog) { -return (prog->type ==BPF_PROG_TYPE_EXT && prog->aux->saved_dst_prog_type) ? - prog->aux->saved_dst_prog_type : prog->type; +if (prog->type ==BPF_PROG_TYPE_EXT ) {
include/linux/bpf_verifier.h static inline u32 type_flag(u32 type) · line 1319
Quoted 10 lines
include/linux/bpf_verifier.h +10
-0+/* +* saved_dst_prog_type is only set once check_attach_btf_id() +* runs. Before that -- e.g. when generating kfunc prototypes at +* add-call time -- fall back to the attach target's type, which +* is available from load time and holds the same value. +*/ +if (prog->aux->saved_dst_prog_type) +return prog->aux->saved_dst_prog_type; +if (prog->aux->dst_prog) +return prog->aux->dst_prog->type;
Let's avoid this fallback and set saved_dst_prog_type in syscallc:bpf_prog_load() in the same place where prog->aux->dst_prog = dst_prog is set up.
...
Quoted 5 lines
include/linux/bpf_verifier.h +0
-0static inline bool bpf_prog_check_recur (const struct bpf_prog *prog)kernel/bpf/verifier.c +0
-0
...
kernel/bpf/verifier.c int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · line 2828
Quoted 7 lines
kernel/bpf/verifier.c +3
-0@@ -2806,11 +2823,29 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)if (err)return err; + proto =kzalloc_obj (*proto,GFP_KERNEL_ACCOUNT ); +if (!proto) +return -ENOMEM ;
Nit: since proto is allocated for each kfunc object I'd embed the
structure directly into the struct bpf_kfunc_desc and make the struct bpf_kfunc_desc_tab dynamically allocated by bpf_add_kfunc_call(). As a side-effect, there would be no need in bpf_free_kfunc_desc_tab().
Quoted 23 lines
kernel/bpf/verifier.c +15
-0+ +memset (&meta,0 ,sizeof (meta)); + meta.btf = kfunc.btf; + meta.func_id = kfunc.id; + meta.func_proto = kfunc.proto; + meta.func_name = kfunc.name; + meta.kfunc_flags = kfunc.flags ? *kfunc.flags :0 ; + + err =gen_kfunc_arg_proto (env, &meta, proto); +if (err) { +kfree (proto); +return err; + } + desc = &tab->descs[tab->nr_descs++]; desc->func_id = func_id; desc->offset = offset; desc->addr = addr; desc->func_model = func_model; + desc->proto = proto;sort (tab->descs, tab->nr_descs,sizeof (tab->descs[0 ]), kfunc_desc_cmp_by_id_off,NULL );return 0 ;
...
kernel/bpf/verifier.c static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a · line 8896
Quoted 6 lines
kernel/bpf/verifier.c +1
-1@@ -8858,7 +8893,7 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a {int i; -for (i =0 ; i <ARRAY_SIZE (fn->arg_type); i++) { +for (i =0 ; i <MAX_BPF_FUNC_REG_ARGS ; i++) {
Nit: here and in other places I'd keep the ARRAY_SIZE for uniformity
(and if 5->12 move is bothering, one can break at ARG_DONTCARE).
Quoted 3 lines
kernel/bpf/verifier.c +0
-0if (!arg_type_is_raw_mem (fn->arg_type[i]))continue ;if (meta->arg_raw_mem.regno)
...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 07/18] bpf: Check fixed-size mem args of helpers and kfuncs the same way
In reply to Amery HungQuoted 15 lines · Amery Hung
Quoted 5 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 8 lines
kernel/bpf/verifier.c static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *reg · line 6971
Quoted 6 lines
kernel/bpf/verifier.c +1
-1@@ -6965,10 +6968,12 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regmark_ptr_not_null_reg (reg); } -int size =base_type (reg->type) ==PTR_TO_STACK ? -(int )mem_size : mem_size; + size = (!helper &&base_type (reg->type) ==PTR_TO_STACK ) ? -(int )mem_size : mem_size;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^When is this negation necessary?
Quoted 6 lines
This was introduced recently in
2cb27158adb3("bpf: poison dead stack slots")The negative size signals
check_stack_range_initialized()to acceptSTACK_POISONslots (allow_poison). That is only safe for global subprogs since a finer-grain stack slot liveness analysis proves the callee body doesn't read those slots.
I see, thank you, while changing this line, could you please drop a one-line comment here?
Quoted 9 lines
Quoted 2 lines
Is there a way to get rid of it?
I think this is needed. However, after reading the commit again, I think this should only apply to global subprogs. Current code allows kfunc by mistake. The line should be fixed:
size = (!meta &&
base_type(reg->type)==PTR_TO_STACK) ? -(int)mem_size :mem_size;
Yes, makes sense.
Quoted 13 lines
Quoted 13 lines
Quoted 11 lines
kernel/bpf/verifier.c +4
-2- err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); - err = err ?:check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta); +if (access_type &BPF_READ ) + err =check_helper_mem_access (env, reg, argno, size,BPF_READ , true, meta); +if (!err && (access_type &BPF_WRITE )) + err =check_helper_mem_access (env, reg, argno, size,BPF_WRITE , true, meta);if (may_be_null) *reg = saved_reg;...
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Amery HungQuoted 64 lines · Amery Hung
Quoted 63 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:07 -0700, Amery Hung wrote:
...
Quoted 7 lines
kernel/bpf/verifier.c +0
-0@@ -6873,7 +6873,14 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,bool zero_size_allowed, struct bpf_call_arg_meta *meta)kernel/bpf/verifier.c static int check_mem_size_reg(struct bpf_verifier_env *env, · line 6883
Quoted 10 lines
kernel/bpf/verifier.c +8
-1{ -int err; +bool may_be_null =type_may_be_null (mem_reg->type); +struct bpf_reg_state saved_reg; +int err =0 ; + +if (may_be_null) { + saved_reg = *mem_reg; +mark_ptr_not_null_reg (mem_reg); + }Also, this pre-existing hack with
saved_regandmark_ptr_not_null_reg()is quite ugly. Considermark_ptr_not_null_reg():static void mark_ptr_not_null_reg(struct bpf_reg_state *reg) { if (base_type(reg->type) == PTR_TO_MAP_VALUE) { const struct bpf_map *map = reg->map_ptr;if (map->inner_map_meta) { reg->type = CONST_PTR_TO_MAP; reg->map_ptr = map->inner_map_meta; /* transfer reg's id which is unique for every map_lookup_elem * as UID of the inner map. */ if (btf_record_has_field(map->inner_map_meta->record, BPF_TIMER | BPF_WORKQUEUE | BPF_TASK_WORK)) { reg->map_uid = reg->id; } } else if (map->map_type == BPF_MAP_TYPE_XSKMAP) { reg->type = PTR_TO_XDP_SOCK; } else if (map->map_type == BPF_MAP_TYPE_SOCKMAP || map->map_type == BPF_MAP_TYPE_SOCKHASH) { reg->type = PTR_TO_SOCKET; } else { reg->type = PTR_TO_MAP_VALUE; } return; } reg->type &= ~PTR_MAYBE_NULL;}
Note that the
check_helper_mem_access()already callsbase_type(mem_reg), so the '&= ~PTR_MAYBE_NULL' part is irrelevant, what matters are the following mutations:
-PTR_TO_MAP_VALUE->CONST_PTR_TO_MAP(plus it's uid, type, ptr)
-BPF_MAP_TYPE_XSKMAP->PTR_TO_XDP_SOCK
-BPF_MAP_TYPE_SOCKMAP->PTR_TO_SOCKET
-BPF_MAP_TYPE_SOCKHASH->PTR_TO_SOCKET
Quoted 8 lines
Quoted 4 lines
A better design compared to current temporary fixup for a helper/kfunc call would be to deal with types
CONST_PTR_TO_MAP|PTR_MAYBE_NULL,PTR_TO_XDP_SOCK|PTR_MAYBE_NULLetc, instantiated at map lookup. Wdyt?I agree that this is a better approach. This can be another follow-up considering that this is an independent refactor and the size of the current set.
Hi Amery,
The change appears small enough to include in the current patch-set. Here is the patch:
https://github.com/kernel-patches/bpf/commit/e59423209d66e71baa7c66e5cda3b2018fcf273a
Wdyt?
Re: [PATCH bpf-next v2 15/18] bpf: Classify kfunc pointer arguments from BTF, resolve type against the register
In reply to Eduard ZingermanQuoted 13 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 8 lines
kernel/bpf/verifier.c +0
-0@@ -4924,6 +4924,19 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { [CONST_PTR_TO_MAP] = btf_bpf_map_id, };
Quoted 4 lines
kernel/bpf/verifier.c static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { · line 4927
Quoted 1 line
kernel/bpf/verifier.c +1
-0+static enum bpf_reg_type lookup_reg2btf_ids (const struct btf_type *ref_t)Nit: the function would be a bit simpler, if
ref_idis passed as a
parameter instead ofref_t.
Ack. will change.
Quoted 19 lines
Quoted 12 lines
kernel/bpf/verifier.c +11
-0+{ +enum bpf_reg_type type; + +for (type =0 ; type < __BPF_REG_TYPE_MAX; type++) { +if (reg2btf_ids[type] && +btf_type_by_id (btf_vmlinux, *reg2btf_ids[type]) == ref_t) +return type; + } + +return NOT_INIT ; +}...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -11429,10 +11431,14 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, is_kfunc_arg_const_mem_size(meta->btf, &args[arg + 1]))) arg_mem_size = true;
Quoted 15 lines
kernel/bpf/verifier.c get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, · line 11441
Quoted 12 lines
kernel/bpf/verifier.c +8
-4-/* This is the catch all argument type of register types supported by -* check_helper_mem_access. However, we only allow when argument type is -* pointer to scalar, or struct composed (recursively) of scalars. When -* arg_mem_size is true, the pointer can be void *. +/* A pointer to a struct without a size argument is classified as KF_ARG_PTR_TO_BTF_ID */ +if (btf_type_is_struct (ref_t) && !arg_mem_size) +return KF_ARG_PTR_TO_BTF_ID ; + +/* +* Otherwise this is a memory buffer supported by check_helper_mem_access(): a pointer +* to a scalar, or to void when paired with a size argument. The access size is derived +* from the pointed-to BTF type unless a size argument follows. Nit: below conditions would be easier to read if the whole
thing was structured as:
I agree that your version reads better. Thanks for the suggestion!
Quoted 86 lines
if (arg_mem_size) { if (!btf_type_is_void(ref_t)) { ... return -EINVAL; } return KF_ARG_PTR_TO_MEM; } else { if (btf_type_is_struct(ref_t)) return KF_ARG_PTR_TO_BTF_ID; if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(ref_t)) { ... return -EINVAL; } return KF_ARG_PTR_TO_MEM || MEM_FIXED_SIZE; }Quoted 3 lines
kernel/bpf/verifier.c +0
-0*/ if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 ) && (arg_mem_size ? !btf_type_is_void (ref_t) :1 )) {...
Quoted 46 lines
kernel/bpf/verifier.c +31
-9@@ -12377,20 +12370,46 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_mebreak ;case KF_ARG_PTR_TO_BTF_ID :/* Only base_type is checked, further checks are done here */ -if ((base_type (reg->type) !=PTR_TO_BTF_ID || - (bpf_type_has_unsafe_modifiers (reg->type) && !is_rcu_reg (reg))) && - !reg2btf_ids[base_type (reg->type)]) { -verbose (env,"%s is %s " ,reg_arg_name (env, argno), -reg_type_str (env, reg->type)); -verbose (env,"expected %s or socket\n" , -reg_type_str (env,base_type (reg->type) | - (type_flag (reg->type) &BPF_REG_TRUSTED_MODIFIERS ))); +if (base_type (reg->type) ==PTR_TO_BTF_ID || + reg2btf_ids[base_type (reg->type)]) { +if (!is_trusted_reg (env, reg) || +bpf_type_has_unsafe_modifiers (reg->type)) { +if (!is_kfunc_rcu (meta)) { +verbose (env,"%s must be referenced or trusted\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } +if (!is_rcu_reg (reg)) { +verbose (env,"%s must be a rcu pointer\n" , +reg_arg_name (env, argno)); +return -EINVAL ; + } + } + + ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno); +if (ret <0 ) +return ret; +break ; + } + +if (!__btf_type_is_scalar_struct (env, meta->btf, ref_t,0 )) { +enum bpf_reg_type reg2btf_type =lookup_reg2btf_ids (ref_t); + +verbose (env,"%s is %s expected %s %s" , +reg_arg_name (env, argno),reg_type_str (env, reg->type), +btf_type_str (ref_t), ref_tname); +if (reg2btf_type !=NOT_INIT ) +verbose (env," or %s" ,reg_type_str (env, reg2btf_type)); +verbose (env,"\n" );return -EINVAL ; } - ret =process_kf_arg_ptr_to_btf_id (env, reg, ref_t, ref_tname, ref_id, meta, i, argno);kernel/bpf/verifier.c static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_call_arg_me · line 12412
Quoted 10 lines
kernel/bpf/verifier.c +7
-3-if (ret <0 ) -return ret; -break ; + +/* +* If the register does not contain btf id but the argument type is a pointer to +* scalar-only struct, allow verifying it as a fixed size memory. +*/ + kf_arg_type =KF_ARG_PTR_TO_MEM |MEM_FIXED_SIZE ; + fallthrough;I think this now accepts one previously rejected case:
- register beingPTR_TO_BTF_ID, pointer to scalar
- parameter being pointer to scalar w/o size register. Previously it was rejected by the following code in theget_kfunc_ptr_arg_type():
Quoted 5 lines
if ((base_type(reg->type) == PTR_TO_BTF_ID || reg2btf_ids[base_type(reg->type)])) { if (!btf_type_is_struct(ref_t)) { verbose(env, "kernel function %s %s pointer type %s %s is not supported\n",Is this intended?
Good catch. No, it was not intended, but it seems to be an acceptable relaxation. The argument will be classified as KF_ARG_PTR_TO_MEM | MEM_FIXED_SIZE. The PTR_TO_BTF_ID register will go through check_mem_reg() -> check_helper_mem_access() -> check_ptr_to_btf_access(), and by default only read is allowed.
Quoted 2 lines
...
Re: [PATCH bpf-next v2 18/18] bpf: Generate kfunc argument prototype at add-call time
In reply to Eduard ZingermanQuoted 25 lines · Eduard Zingerman
On Fri, 2026-07-24 at 12:08 -0700, Amery Hung wrote:
...
Quoted 4 lines
include/linux/bpf_verifier.h +0
-0...
Quoted 13 lines
include/linux/bpf_verifier.h +7
-2@@ -1305,8 +1306,19 @@ static inline u32 type_flag(u32 type)/* only use after check_attach_btf_id() */ static inline enum bpf_prog_type resolve_prog_type (const struct bpf_prog *prog) { -return (prog->type ==BPF_PROG_TYPE_EXT && prog->aux->saved_dst_prog_type) ? - prog->aux->saved_dst_prog_type : prog->type; +if (prog->type ==BPF_PROG_TYPE_EXT ) { +/* +* saved_dst_prog_type is only set once check_attach_btf_id() +* runs. Before that -- e.g. when generating kfunc prototypes at +* add-call time -- fall back to the attach target's type, which +* is available from load time and holds the same value. +*/
Quoted 8 lines
include/linux/bpf_verifier.h static inline u32 type_flag(u32 type) · line 1319
Quoted 4 lines
include/linux/bpf_verifier.h +4
-0+if (prog->aux->saved_dst_prog_type) +return prog->aux->saved_dst_prog_type; +if (prog->aux->dst_prog) +return prog->aux->dst_prog->type;Let's avoid this fallback and set
saved_dst_prog_typein syscallc:bpf_prog_load() in the same place whereprog->aux->dst_prog = dst_progis set up.
Makes sense. This looks better now.
Quoted 15 lines
...
Quoted 5 lines
static inline bool
bpf_prog_check_recur(const struct bpf_prog *prog)kernel/bpf/verifier.c +0
-0...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -2806,11 +2823,29 @@ int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset)if (err)return err;
Quoted 8 lines
kernel/bpf/verifier.c int bpf_add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, u16 offset) · line 2828
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+ proto =kzalloc_obj (*proto,GFP_KERNEL_ACCOUNT ); +if (!proto) +return -ENOMEM ;Nit: since
protois allocated for each kfunc object I'd embed the
structure directly into the structbpf_kfunc_descand make the structbpf_kfunc_desc_tabdynamically allocated bybpf_add_kfunc_call(). As a side-effect, there would be no need inbpf_free_kfunc_desc_tab().
Ack. Will change.
Quoted 31 lines
Quoted 24 lines
kernel/bpf/verifier.c +15
-0+ +memset (&meta,0 ,sizeof (meta)); + meta.btf = kfunc.btf; + meta.func_id = kfunc.id; + meta.func_proto = kfunc.proto; + meta.func_name = kfunc.name; + meta.kfunc_flags = kfunc.flags ? *kfunc.flags :0 ; + + err =gen_kfunc_arg_proto (env, &meta, proto); +if (err) { +kfree (proto); +return err; + } + desc = &tab->descs[tab->nr_descs++]; desc->func_id = func_id; desc->offset = offset; desc->addr = addr; desc->func_model = func_model; + desc->proto = proto;sort (tab->descs, tab->nr_descs,sizeof (tab->descs[0 ]), kfunc_desc_cmp_by_id_off,NULL );return 0 ;...
Quoted 4 lines
kernel/bpf/verifier.c +0
-0@@ -8858,7 +8893,7 @@ static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a {int i;
Quoted 5 lines
kernel/bpf/verifier.c static bool check_raw_mode_ok(const struct bpf_func_proto *fn, struct bpf_call_a · line 8896
Quoted 2 lines
kernel/bpf/verifier.c +1
-1-for (i =0 ; i <ARRAY_SIZE (fn->arg_type); i++) { +for (i =0 ; i <MAX_BPF_FUNC_REG_ARGS ; i++) {Nit: here and in other places I'd keep the
ARRAY_SIZEfor uniformity(and if 5->12 move is bothering, one can break at ARG_DONTCARE).
I will revert and add ARG_DONTCARE break. Going through all 12 args adds an observable slowdown in the verification time.
Quoted 6 lines
Quoted 4 lines
kernel/bpf/verifier.c +0
-0if (!arg_type_is_raw_mem (fn->arg_type[i]))continue ;if (meta->arg_raw_mem.regno)...
Re: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
In reply to Eduard Zingerman[...]
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Note that the
check_helper_mem_access()already callsbase_type(mem_reg), so the '&= ~PTR_MAYBE_NULL' part is irrelevant, what matters are the following mutations:
-PTR_TO_MAP_VALUE->CONST_PTR_TO_MAP(plus it's uid, type, ptr)
-BPF_MAP_TYPE_XSKMAP->PTR_TO_XDP_SOCK
-BPF_MAP_TYPE_SOCKMAP->PTR_TO_SOCKET
-BPF_MAP_TYPE_SOCKHASH->PTR_TO_SOCKETQuoted 8 lines
Quoted 4 lines
A better design compared to current temporary fixup for a helper/kfunc call would be to deal with types
CONST_PTR_TO_MAP|PTR_MAYBE_NULL,PTR_TO_XDP_SOCK|PTR_MAYBE_NULLetc, instantiated at map lookup. Wdyt?I agree that this is a better approach. This can be another follow-up considering that this is an independent refactor and the size of the current set.
Quoted 8 lines
Hi Amery,
The change appears small enough to include in the current patch-set. Here is the patch:
https://github.com/kernel-patches/bpf/commit/e59423209d66e71baa7c66e5cda3b2018fcf273a
Wdyt?
It makes total sense to separate refinement from mark_ptr_not_null_reg(). It is better than the stopgap I have. Do you mind if I carry this as a part of this set?
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 05/18] bpf: Check helper and kfunc mem+size arguments identically
Quoted 25 lines · Amery Hung
[...]
Quoted 23 lines
Quoted 9 lines
Quoted 9 lines
Note that the
check_helper_mem_access()already callsbase_type(mem_reg), so the '&= ~PTR_MAYBE_NULL' part is irrelevant, what matters are the following mutations:
-PTR_TO_MAP_VALUE->CONST_PTR_TO_MAP(plus it's uid, type, ptr)
-BPF_MAP_TYPE_XSKMAP->PTR_TO_XDP_SOCK
-BPF_MAP_TYPE_SOCKMAP->PTR_TO_SOCKET
-BPF_MAP_TYPE_SOCKHASH->PTR_TO_SOCKETQuoted 8 lines
Quoted 4 lines
A better design compared to current temporary fixup for a helper/kfunc call would be to deal with types
CONST_PTR_TO_MAP|PTR_MAYBE_NULL,PTR_TO_XDP_SOCK|PTR_MAYBE_NULLetc, instantiated at map lookup. Wdyt?I agree that this is a better approach. This can be another follow-up considering that this is an independent refactor and the size of the current set.
Hi Amery,
The change appears small enough to include in the current patch-set. Here is the patch:
Quoted 7 lines
Quoted 3 lines
https://github.com/kernel-patches/bpf/commit/e59423209d66e71baa7c66e5cda3b2018fcf273a
Wdyt?
It makes total sense to separate refinement from
mark_ptr_not_null_reg(). It is better than the stopgap I have. Do you mind if I carry this as a part of this set?
Sure, thank you for looking into it.
Eduard Zingerman
maintainerRe: [PATCH bpf-next v2 15/18] bpf: Classify kfunc pointer arguments from BTF, resolve type against the register
In reply to Amery HungOn Tue, 2026-07-28 at 23:22 -0700, Amery Hung wrote:
...
Quoted 10 lines
Quoted 10 lines
I think this now accepts one previously rejected case:
- register beingPTR_TO_BTF_ID, pointer to scalar
- parameter being pointer to scalar w/o size register. Previously it was rejected by the following code in theget_kfunc_ptr_arg_type():if ((
base_type(reg->type)==PTR_TO_BTF_ID|| reg2btf_ids[base_type(reg->type)])) { if (!btf_type_is_struct(ref_t)) {
verbose(env, "kernel function %s %s pointer type %s %s is not supported\n",
Quoted 7 lines
Quoted 1 line
Is this intended?
Good catch. No, it was not intended, but it seems to be an acceptable relaxation. The argument will be classified as
KF_ARG_PTR_TO_MEM|MEM_FIXED_SIZE. ThePTR_TO_BTF_IDregister will go throughcheck_mem_reg()->check_helper_mem_access()->check_ptr_to_btf_access(), and by default only read is allowed.
Yeah, probably harmless.