bpf: Prepare to support stack arguments
Subsystems (5)
Yonghong Song
reviewercover bpf: Prepare to support stack arguments
The patch set prepares to support stack arguments for bpf functions and kfunc's. The major changes include:
- Avoid redundant calculation of bpf_reg_state. For stack arguments, there exists no corresponding register number.
- Refactor check_kfunc_mem_size_reg() to have bpf_reg_state's for both mem_reg and size_reg.
- Allow verifier logs to print stack arguments if there is no corresponding register.
Please see individual patches for details.
Yonghong Song (9):
bpf: Remove unused parameter from check_map_kptr_access()
bpf: Fix tail_call_reachable leak
bpf: Remove WARN_ON_ONCE in check_kfunc_mem_size_reg()
bpf: Refactor to avoid redundant calculation of bpf_reg_state
bpf: Refactor to handle memory and size together
bpf: Rename existing argno to arg
bpf: Prepare verifier logs for upcoming kfunc stack arguments
bpf: Introduce bpf register BPF_REG_PARAMS
bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments
include/linux/bpf.h | 5 + include/linux/bpf_verifier.h | 1 + include/linux/filter.h | 5 +- kernel/bpf/core.c | 4 +- kernel/bpf/verifier.c | 813 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/ctx_rewrite.c | 14 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_bpf_fastcall.c | 24 +- .../selftests/bpf/progs/verifier_may_goto_1.c | 12 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_sdiv.c | 64 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 37 files changed, 604 insertions(+), 536 deletions(-)
Signature · 1 line
2.52.0
Yonghong Song
reviewerPATCH 1/9 bpf: Remove unused parameter from check_map_kptr_access()
The parameter 'regno' in check_map_kptr_access() is unused. Remove it.
--- kernel/bpf/verifier.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +2 -2
@@ -4710,7 +4710,7 @@ static int mark_uptr_ld_reg(struct bpf_verifier_env *env, u32 regno,return 0 ; } -static int check_map_kptr_access (struct bpf_verifier_env *env,u32 regno, +static int check_map_kptr_access (struct bpf_verifier_env *env,int value_regno,int insn_idx,struct btf_field *kptr_field) {
@@ -6357,7 +6357,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn kptr_field =btf_record_find (reg->map_ptr->record, off + reg->var_off.value,BPF_KPTR |BPF_UPTR );if (kptr_field) { - err =check_map_kptr_access (env, regno, value_regno, insn_idx, kptr_field); + err =check_map_kptr_access (env, value_regno, insn_idx, kptr_field); }else if (t ==BPF_READ && value_regno >=0 ) {struct bpf_map *map = reg->map_ptr;
Signature · 1 line
2.52.0
Yonghong Song
reviewerPATCH 2/9 bpf: Fix tail_call_reachable leak
In reply to Yonghong SongIn check_max_stack_depth_subprog(), the local variable tail_call_reachable is set when entering a callee that has a tail call, but never reset when popping back to the parent. This causes the flag to leak across sibling subprogs in the DFS traversal.
This results in unnecessary JIT overhead: the JIT emits tail call counter preservation code for subprogs that can never be reached via a tail call path.
Fix this by resetting tail_call_reachable to the parent's actual per-subprog flag when popping a frame. If the parent was already marked tail_call_reachable by a previous sibling's traversal, the local variable stays true. Otherwise it resets to false, so subsequent siblings start with a clean state.
--- kernel/bpf/verifier.c | 3 +++ 1 file changed, 3 insertions(+)
kernel/bpf/verifier.c +3 -0
@@ -5490,6 +5490,9 @@ static int check_max_stack_depth_subprog(struct bpf_verifier_env *env, int idx, frame = dinfo[idx].frame; i = dinfo[idx].ret_insn; +/* reset tail_call_reachable to the parent's actual state */ + tail_call_reachable = subprog[idx].tail_call_reachable; + goto continue_func; }
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next 2/9] bpf: Fix tail_call_reachable leak
commit 697f215251be114235dcf2ec469f425797a26a51 Author: Yonghong Song <[email protected]>
commit 697f215251be114235dcf2ec469f425797a26a51
Author: Yonghong Song <[email protected]>
bpf: Fix tail_call_reachable leak
This commit fixes a leak of the local tail_call_reachable flag across sibling subprogs during the DFS traversal in check_max_stack_depth_subprog(), which caused the JIT to emit unnecessary tail call counter preservation code for subprogs that could not be reached via a tail call path.
Should this commit carry a Fixes: tag? The buggy pattern (set tail_call_reachable to true on push, never reset on pop, then mark all currently-on-stack frames) appears to have been introduced by commit ebf7d1f508a7 ("bpf, x64: rework pro/epilogue and tailcall handling in JIT") which originally added tail_call_reachable tracking to the stack-depth DFS.
Something like:
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24736686700
Yonghong Song
reviewerRe: [PATCH bpf-next 2/9] bpf: Fix tail_call_reachable leak
Quoted 18 lines · On 4/21/26 11:06 AM, [email protected] wrote:
commit
697f215251be114235dcf2ec469f425797a26a51
Author: Yonghong Song <[email protected]>bpf: Fix
tail_call_reachableleakThis commit fixes a leak of the local
tail_call_reachableflag across sibling subprogs during the DFS traversal incheck_max_stack_depth_subprog(), which caused the JIT to emit unnecessary tail call counter preservation code for subprogs that could not be reached via a tail call path.Should this commit carry a Fixes: tag? The buggy pattern (set
tail_call_reachableto true on push, never reset on pop, then mark all currently-on-stack frames) appears to have been introduced by commitebf7d1f508a7("bpf, x64: rework pro/epilogue and tailcall handling in JIT") which originally addedtail_call_reachabletracking to the stack-depth DFS.
Quoted 3 lines
Something like:
Fixes ebf7d1f508a7 ("bpf, x64: rework pro/epilogue and tailcall handling in JIT")
The above fix tag is in 2020 so it has been 5+ years. BTW, this is not really a bug although in certain cases it may cause more stack space than needed.
I would skip Fixes tag for this patch.
Quoted 7 lines
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.mdCI run summary: https://github.com/kernel-patches/bpf/actions/runs/24736686700
Yonghong Song
reviewerPATCH 3/9 bpf: Remove WARN_ON_ONCE in check_kfunc_mem_size_reg()
In reply to Yonghong SongThe warning is too late if it does happen. Remove it.
--- kernel/bpf/verifier.c | 2 -- 1 file changed, 2 deletions(-)
kernel/bpf/verifier.c +0 -2
@@ -7134,8 +7134,6 @@ static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_regstruct bpf_call_arg_meta meta;int err; -WARN_ON_ONCE (regno <BPF_REG_2 || regno >BPF_REG_5 ); -memset (&meta,0 ,sizeof (meta));if (may_be_null) {
Signature · 1 line
2.52.0
Yonghong Song
reviewerPATCH 4/9 bpf: Refactor to avoid redundant calculation of bpf_reg_state
In reply to Yonghong SongIn many cases, once a bpf_reg_state is defined, it can pass to callee's. Otherwise, callee will need to get bpf_reg_state again based on regno. More importantly, this is needed for later stack arguments for kfuncs since the register state for stack arguments does not have a corresponding regno. So it makes sense to pass reg state for callee's.
The following is the only change to avoid compilation warning:
static int sanitize_check_bounds(struct bpf_verifier_env *env,
const struct bpf_insn *insn,
- const struct bpf_reg_state *dst_reg)
+ struct bpf_reg_state *dst_reg)
--- kernel/bpf/verifier.c | 213 ++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 120 deletions(-)
kernel/bpf/verifier.c +93 -120
@@ -3929,13 +3929,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,static int check_stack_write_var_off (struct bpf_verifier_env *env,/* func where register points to */ struct bpf_func_state *state, -int ptr_regno,int off,int size, +struct bpf_reg_state *ptr_reg,int off,int size,int value_regno,int insn_idx) {struct bpf_func_state *cur;/* state of the current function */ int min_off, max_off;int i, err; -struct bpf_reg_state *ptr_reg =NULL , *value_reg =NULL ; +struct bpf_reg_state *value_reg =NULL ;struct bpf_insn *insn = &env->prog->insnsi[insn_idx];bool writing_zero = false;/* set if the fact that we're writing a zero is used to let any
@@ -3944,7 +3944,6 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,bool zero_used = false; cur = env->cur_state->frame[env->cur_state->curframe]; - ptr_reg = &cur->regs[ptr_regno]; min_off = ptr_reg->smin_value + off; max_off = ptr_reg->smax_value + off + size;if (value_regno >=0 )
@@ -4241,7 +4240,7 @@ enum bpf_access_src { ACCESS_HELPER = 2, /* the access is performed by a helper */ }; -static int check_stack_range_initialized(struct bpf_verifier_env *env, +static int check_stack_range_initialized(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int regno, int off, int access_size, bool zero_size_allowed, enum bpf_access_type type,
@@ -4265,18 +4264,16 @@ static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)* offset; for a fixed offset check_stack_read_fixed_off should be used * instead. */ -static int check_stack_read_var_off (struct bpf_verifier_env *env, +static int check_stack_read_var_off (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -/* The state of the source register. */ -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *ptr_state =bpf_func (env, reg);int err;int min_off, max_off;/* Note that we pass a NULL meta, so raw access will not be permitted. */ - err =check_stack_range_initialized (env, ptr_regno, off, size, + err =check_stack_range_initialized (env, reg, ptr_regno, off, size, false,BPF_READ ,NULL );if (err)return err;
@@ -4298,10 +4295,9 @@ static int check_stack_read_var_off(struct bpf_verifier_env *env,* can be -1, meaning that the read value is not going to a register. */ static int check_stack_read (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;/* Some accesses are only permitted with a static offset. */
@@ -4337,7 +4333,7 @@ static int check_stack_read(struct bpf_verifier_env *env,* than fixed offset ones. Note that dst_regno >= 0 on this * branch. */ - err =check_stack_read_var_off (env, ptr_regno, off, size, + err =check_stack_read_var_off (env, reg, ptr_regno, off, size, dst_regno); }return err;
@@ -4354,10 +4350,9 @@ static int check_stack_read(struct bpf_verifier_env *env,* The caller must ensure that the offset falls within the maximum stack size. */ static int check_stack_write (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int off,int size,int value_regno,int insn_idx) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;
@@ -4370,16 +4365,15 @@ static int check_stack_write(struct bpf_verifier_env *env,* than fixed offset ones. */ err =check_stack_write_var_off (env, state, - ptr_regno, off, size, + reg, off, size, value_regno, insn_idx); }return err; } -static int check_map_access_type (struct bpf_verifier_env *env,u32 regno, +static int check_map_access_type (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,int off,int size,enum bpf_access_type type) { -struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_map *map = reg->map_ptr;u32 cap =bpf_map_flags_to_cap (map);
@@ -4399,17 +4393,15 @@ static int check_map_access_type(struct bpf_verifier_env *env, u32 regno, }/* check read/write into memory region (e.g., map value, ringbuf sample, etc) */ -static int __check_mem_access (struct bpf_verifier_env *env,int regno, +static int __check_mem_access (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int off,int size,u32 mem_size,bool zero_size_allowed) {bool size_ok = size >0 || (size ==0 && zero_size_allowed); -struct bpf_reg_state *reg;if (off >=0 && size_ok && (u64 )off + size <= mem_size)return 0 ; - reg = &cur_regs (env)[regno];switch (reg->type) {case PTR_TO_MAP_KEY : verbose(env, "invalid access to map key, key_size=%d off=%d size=%d\n",
@@ -4439,13 +4431,10 @@ static int __check_mem_access(struct bpf_verifier_env *env, int regno, }/* check read/write into a memory region with possible variable offset */ -static int check_mem_region_access (struct bpf_verifier_env *env,u32 regno, +static int check_mem_region_access (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,int off,int size,u32 mem_size,bool zero_size_allowed) { -struct bpf_verifier_state *vstate = env->cur_state; -struct bpf_func_state *state = vstate->frame[vstate->curframe]; -struct bpf_reg_state *reg = &state->regs[regno];int err;/* We may have adjusted the register pointing to memory region, so we
@@ -4466,7 +4455,7 @@ static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, regno);return -EACCES ; } - err =__check_mem_access (env, regno, reg->smin_value + off, size, + err =__check_mem_access (env, reg, regno, reg->smin_value + off, size, mem_size, zero_size_allowed);if (err) { verbose(env, "R%d min value is outside of the allowed memory range\n",
@@ -4483,7 +4472,7 @@ static int check_mem_region_access(struct bpf_verifier_env *env, u32 regno, regno);return -EACCES ; } - err =__check_mem_access (env, regno, reg->umax_value + off, size, + err =__check_mem_access (env, reg, regno, reg->umax_value + off, size, mem_size, zero_size_allowed);if (err) { verbose(env, "R%d max value is outside of the allowed memory range\n",
@@ -4787,19 +4776,16 @@ static u32 map_mem_size(const struct bpf_map *map) }/* check read/write into a map element with possible variable offset */ -static int check_map_access (struct bpf_verifier_env *env,u32 regno, +static int check_map_access (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,int off,int size,bool zero_size_allowed,enum bpf_access_src src) { -struct bpf_verifier_state *vstate = env->cur_state; -struct bpf_func_state *state = vstate->frame[vstate->curframe]; -struct bpf_reg_state *reg = &state->regs[regno];struct bpf_map *map = reg->map_ptr;u32 mem_size =map_mem_size (map);struct btf_record *rec;int err, i; - err =check_mem_region_access (env, regno, off, size, mem_size, zero_size_allowed); + err =check_mem_region_access (env, reg, regno, off, size, mem_size, zero_size_allowed);if (err)return err;
@@ -4895,10 +4881,9 @@ static bool may_access_direct_pkt_data(struct bpf_verifier_env *env, } } -static int check_packet_access (struct bpf_verifier_env *env,u32 regno,int off, +static int check_packet_access (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,int off,int size,bool zero_size_allowed) { -struct bpf_reg_state *reg =reg_state (env, regno);int err;if (reg->range <0 ) {
@@ -4906,7 +4891,7 @@ static int check_packet_access(struct bpf_verifier_env *env, u32 regno, int off,return -EINVAL ; } - err =check_mem_region_access (env, regno, off, size, reg->range, zero_size_allowed); + err =check_mem_region_access (env, reg, regno, off, size, reg->range, zero_size_allowed);if (err)return err;
@@ -4961,7 +4946,7 @@ static int __check_ctx_access(struct bpf_verifier_env *env, int insn_idx, int ofreturn -EACCES ; } -static int check_ctx_access (struct bpf_verifier_env *env,int insn_idx,u32 regno, +static int check_ctx_access (struct bpf_verifier_env *env,int insn_idx,struct bpf_reg_state *reg,u32 regno,int off,int access_size,enum bpf_access_type t,struct bpf_insn_access_aux *info) {
@@ -4971,12 +4956,10 @@ static int check_ctx_access(struct bpf_verifier_env *env, int insn_idx, u32 regn*/ bool var_off_ok =is_var_ctx_off_allowed (env->prog);bool fixed_off_ok = !env->ops->convert_ctx_access; -struct bpf_reg_state *regs =cur_regs (env); -struct bpf_reg_state *reg = regs + regno;int err;if (var_off_ok) - err =check_mem_region_access (env, regno, off, access_size,U16_MAX , false); + err =check_mem_region_access (env, reg, regno, off, access_size,U16_MAX , false);else err =__check_ptr_off_reg (env, reg, regno, fixed_off_ok);if (err)
@@ -5002,10 +4985,9 @@ static int check_flow_keys_access(struct bpf_verifier_env *env, int off, }static int check_sock_access (struct bpf_verifier_env *env,int insn_idx, -u32 regno,int off,int size, +struct bpf_reg_state *reg,u32 regno,int off,int size,enum bpf_access_type t) { -struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_insn_access_aux info = {};bool valid;
@@ -5971,12 +5953,11 @@ static bool type_is_trusted_or_null(struct bpf_verifier_env *env, }static int check_ptr_to_btf_access (struct bpf_verifier_env *env, -struct bpf_reg_state *regs, +struct bpf_reg_state *regs,struct bpf_reg_state *reg,int regno,int off,int size,enum bpf_access_type atype,int value_regno) { -struct bpf_reg_state *reg = regs + regno;const struct btf_type *t =btf_type_by_id (reg->btf, reg->btf_id);const char *tname =btf_name_by_offset (reg->btf, t->name_off);const char *field_name =NULL ;
@@ -6128,12 +6109,11 @@ static int check_ptr_to_btf_access(struct bpf_verifier_env *env, }static int check_ptr_to_map_access (struct bpf_verifier_env *env, -struct bpf_reg_state *regs, +struct bpf_reg_state *regs,struct bpf_reg_state *reg,int regno,int off,int size,enum bpf_access_type atype,int value_regno) { -struct bpf_reg_state *reg = regs + regno;struct bpf_map *map = reg->map_ptr;struct bpf_reg_state map_reg;enum bpf_type_flag flag =0 ;
@@ -6222,11 +6202,10 @@ static int check_stack_slot_within_bounds(struct bpf_verifier_env *env,* 'off' includes `regno->offset`, but not its dynamic part (if any). */ static int check_stack_access_within_bounds ( -struct bpf_verifier_env *env, +struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int off,int access_size,enum bpf_access_type type) { -struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_func_state *state =bpf_func (env, reg);s64 min_off, max_off;int err;
@@ -6314,12 +6293,11 @@ static void add_scalar_to_reg(struct bpf_reg_state *dst_reg, s64 val)* if t==write && value_regno==-1, some unknown value is stored into memory * if t==read && value_regno==-1, don't care what we read from memory */ -static int check_mem_access (struct bpf_verifier_env *env,int insn_idx,u32 regno, +static int check_mem_access (struct bpf_verifier_env *env,int insn_idx,struct bpf_reg_state *reg,u32 regno,int off,int bpf_size,enum bpf_access_type t,int value_regno,bool strict_alignment_once,bool is_ldsx) {struct bpf_reg_state *regs =cur_regs (env); -struct bpf_reg_state *reg = regs + regno;int size, err =0 ; size =bpf_size_to_bytes (bpf_size);
@@ -6336,7 +6314,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn -EACCES ; } - err =check_mem_region_access (env, regno, off, size, + err =check_mem_region_access (env, reg, regno, off, size, reg->map_ptr->key_size, false);if (err)return err;
@@ -6350,10 +6328,10 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnverbose (env,"R%d leaks addr into map\n" , value_regno);return -EACCES ; } - err =check_map_access_type (env, regno, off, size, t); + err =check_map_access_type (env, reg, regno, off, size, t);if (err)return err; - err =check_map_access (env, regno, off, size, false,ACCESS_DIRECT ); + err =check_map_access (env, reg, regno, off, size, false,ACCESS_DIRECT );if (err)return err;if (tnum_is_const (reg->var_off))
@@ -6422,7 +6400,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn* instructions, hence no need to check bounds in that case. */ if (!rdonly_untrusted) - err =check_mem_region_access (env, regno, off, size, + err =check_mem_region_access (env, reg, regno, off, size, reg->mem_size, false);if (!err && value_regno >=0 && (t ==BPF_READ || rdonly_mem))mark_reg_unknown (env, regs, value_regno);
@@ -6440,7 +6418,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnreturn -EACCES ; } - err =check_ctx_access (env, insn_idx, regno, off, size, t, &info); + err =check_ctx_access (env, insn_idx, reg, regno, off, size, t, &info);if (!err && t ==BPF_READ && value_regno >=0 ) {/* ctx access returns either a scalar, or a * PTR_TO_PACKET[_META,_END]. In the latter
@@ -6477,15 +6455,15 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn }else if (reg->type ==PTR_TO_STACK ) {/* Basic bounds checks. */ - err =check_stack_access_within_bounds (env, regno, off, size, t); + err =check_stack_access_within_bounds (env, reg, regno, off, size, t);if (err)return err;if (t ==BPF_READ ) - err =check_stack_read (env, regno, off, size, + err =check_stack_read (env, reg, regno, off, size, value_regno);else - err =check_stack_write (env, regno, off, size, + err =check_stack_write (env, reg, off, size, value_regno, insn_idx); }else if (reg_is_pkt_pointer (reg)) {if (t ==BPF_WRITE && !may_access_direct_pkt_data (env,NULL , t)) {
@@ -6498,7 +6476,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn value_regno);return -EACCES ; } - err =check_packet_access (env, regno, off, size, false); + err =check_packet_access (env, reg, regno, off, size, false);if (!err && t ==BPF_READ && value_regno >=0 )mark_reg_unknown (env, regs, value_regno); }else if (reg->type ==PTR_TO_FLOW_KEYS ) {
@@ -6518,7 +6496,7 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regn regno, reg_type_str(env, reg->type));return -EACCES ; } - err =check_sock_access (env, insn_idx, regno, off, size, t); + err =check_sock_access (env, insn_idx, reg, regno, off, size, t);if (!err && value_regno >=0 )mark_reg_unknown (env, regs, value_regno); }else if (reg->type ==PTR_TO_TP_BUFFER ) {
@@ -6527,10 +6505,10 @@ static int check_mem_access(struct bpf_verifier_env *env, int insn_idx, u32 regnmark_reg_unknown (env, regs, value_regno); }else if (base_type (reg->type) ==PTR_TO_BTF_ID && !type_may_be_null (reg->type)) { - err =check_ptr_to_btf_access (env, regs, regno, off, size, t, + err =check_ptr_to_btf_access (env, regs, reg, regno, off, size, t, value_regno); }else if (reg->type ==CONST_PTR_TO_MAP ) { - err =check_ptr_to_map_access (env, regs, regno, off, size, t, + err =check_ptr_to_map_access (env, regs, reg, regno, off, size, t, value_regno); }else if (base_type (reg->type) ==PTR_TO_BUF && !type_may_be_null (reg->type)) {
@@ -6599,7 +6577,7 @@ static int check_load_mem(struct bpf_verifier_env *env, struct bpf_insn *insn,/* Check if (src_reg + off) is readable. The state of dst_reg will be * updated by this call. */ - err =check_mem_access (env, env->insn_idx, insn->src_reg, insn->off, + err =check_mem_access (env, env->insn_idx, regs + insn->src_reg, insn->src_reg, insn->off,BPF_SIZE (insn->code),BPF_READ , insn->dst_reg, strict_alignment_once, is_ldsx); err = err ?: save_aux_ptr_type(env, src_reg_type,
@@ -6629,7 +6607,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn, dst_reg_type = regs[insn->dst_reg].type;/* Check if (dst_reg + off) is writeable. */ - err =check_mem_access (env, env->insn_idx, insn->dst_reg, insn->off, + err =check_mem_access (env, env->insn_idx, regs + insn->dst_reg, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_WRITE , insn->src_reg, strict_alignment_once, false); err = err ?:save_aux_ptr_type (env, dst_reg_type, false);
@@ -6640,6 +6618,7 @@ static int check_store_reg(struct bpf_verifier_env *env, struct bpf_insn *insn,static int check_atomic_rmw (struct bpf_verifier_env *env,struct bpf_insn *insn) { +struct bpf_reg_state *dst_reg;int load_reg;int err;
@@ -6701,13 +6680,15 @@ static int check_atomic_rmw(struct bpf_verifier_env *env, load_reg =-1 ; } + dst_reg =cur_regs (env) + insn->dst_reg; +/* Check whether we can read the memory, with second call for fetch * case to simulate the register fill. */ - err =check_mem_access (env, env->insn_idx, insn->dst_reg, insn->off, + err =check_mem_access (env, env->insn_idx, dst_reg, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_READ ,-1 , true, false);if (!err && load_reg >=0 ) - err =check_mem_access (env, env->insn_idx, insn->dst_reg, + err =check_mem_access (env, env->insn_idx, dst_reg, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_READ , load_reg, true, false);if (err)
@@ -6719,7 +6700,7 @@ static int check_atomic_rmw(struct bpf_verifier_env *env,return err; }/* Check whether we can write into the same memory. */ - err =check_mem_access (env, env->insn_idx, insn->dst_reg, insn->off, + err =check_mem_access (env, env->insn_idx, dst_reg, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_WRITE ,-1 , true, false);if (err)return err;
@@ -6808,11 +6789,10 @@ static int check_atomic(struct bpf_verifier_env *env, struct bpf_insn *insn)* read offsets are marked as read. */ static int check_stack_range_initialized ( -struct bpf_verifier_env *env,int regno,int off, +struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int off,int access_size,bool zero_size_allowed,enum bpf_access_type type,struct bpf_call_arg_meta *meta) { -struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_func_state *state =bpf_func (env, reg);int err, min_off, max_off, i, j, slot, spi;/* Some accesses can write anything into the stack, others are
@@ -6834,7 +6814,7 @@ static int check_stack_range_initialized(return -EACCES ; } - err =check_stack_access_within_bounds (env, regno, off, access_size, type); + err =check_stack_access_within_bounds (env, reg, regno, off, access_size, type);if (err)return err;
@@ -6965,7 +6945,7 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,switch (base_type (reg->type)) {case PTR_TO_PACKET :case PTR_TO_PACKET_META : -return check_packet_access (env, regno,0 , access_size, +return check_packet_access (env, reg, regno,0 , access_size, zero_size_allowed);case PTR_TO_MAP_KEY :if (access_type ==BPF_WRITE ) {
@@ -6973,12 +6953,12 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, reg_type_str(env, reg->type));return -EACCES ; } -return check_mem_region_access (env, regno,0 , access_size, +return check_mem_region_access (env, reg, regno,0 , access_size, reg->map_ptr->key_size, false);case PTR_TO_MAP_VALUE : -if (check_map_access_type (env, regno,0 , access_size, access_type)) +if (check_map_access_type (env, reg, regno,0 , access_size, access_type))return -EACCES ; -return check_map_access (env, regno,0 , access_size, +return check_map_access (env, reg, regno,0 , access_size, zero_size_allowed,ACCESS_HELPER );case PTR_TO_MEM :if (type_is_rdonly_mem (reg->type)) {
@@ -6988,7 +6968,7 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,return -EACCES ; } } -return check_mem_region_access (env, regno,0 , +return check_mem_region_access (env, reg, regno,0 , access_size, reg->mem_size, zero_size_allowed);case PTR_TO_BUF :
@@ -7008,16 +6988,16 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno, max_access);case PTR_TO_STACK :return check_stack_range_initialized ( - env, + env, reg, regno,0 , access_size, zero_size_allowed, access_type, meta);case PTR_TO_BTF_ID : -return check_ptr_to_btf_access (env, regs, regno,0 , +return check_ptr_to_btf_access (env, regs, reg, regno,0 , access_size,BPF_READ ,-1 );case PTR_TO_CTX :/* Only permit reading or writing syscall context using helper calls. */ if (is_var_ctx_off_allowed (env->prog)) { -int err =check_mem_region_access (env, regno,0 , access_size,U16_MAX , +int err =check_mem_region_access (env, reg, regno,0 , access_size,U16_MAX , zero_size_allowed);if (err)return err;
@@ -7178,11 +7158,10 @@ enum {* env->cur_state->active_locks remembers which map value element or allocated * object got locked and clears it after bpf_spin_unlock. */ -static int process_spin_lock (struct bpf_verifier_env *env,int regno,int flags) +static int process_spin_lock (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int flags) {bool is_lock = flags &PROCESS_SPIN_LOCK , is_res_lock = flags &PROCESS_RES_LOCK ;const char *lock_str = is_res_lock ?"bpf_res_spin" :"bpf_spin" ; -struct bpf_reg_state *reg =reg_state (env, regno);struct bpf_verifier_state *cur = env->cur_state;bool is_const =tnum_is_const (reg->var_off);bool is_irq = flags &PROCESS_LOCK_IRQ ;
@@ -7295,11 +7274,10 @@ static int process_spin_lock(struct bpf_verifier_env *env, int regno, int flags) }/* Check if @regno is a pointer to a specific field in a map value */ -static int check_map_field_pointer (struct bpf_verifier_env *env,u32 regno, +static int check_map_field_pointer (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,enum btf_field_type field_type,struct bpf_map_desc *map_desc) { -struct bpf_reg_state *reg =reg_state (env, regno);bool is_const =tnum_is_const (reg->var_off);struct bpf_map *map = reg->map_ptr;u64 val = reg->var_off.value;
@@ -7349,26 +7327,26 @@ static int check_map_field_pointer(struct bpf_verifier_env *env, u32 regno,return 0 ; } -static int process_timer_func (struct bpf_verifier_env *env,int regno, +static int process_timer_func (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,struct bpf_map_desc *map) {if (IS_ENABLED (CONFIG_PREEMPT_RT )) {verbose (env,"bpf_timer cannot be used for PREEMPT_RT.\n" );return -EOPNOTSUPP ; } -return check_map_field_pointer (env, regno,BPF_TIMER , map); +return check_map_field_pointer (env, reg, regno,BPF_TIMER , map); } -static int process_timer_helper (struct bpf_verifier_env *env,int regno, +static int process_timer_helper (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,struct bpf_call_arg_meta *meta) { -return process_timer_func (env, regno, &meta->map); +return process_timer_func (env, reg, regno, &meta->map); } -static int process_timer_kfunc (struct bpf_verifier_env *env,int regno, +static int process_timer_kfunc (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,struct bpf_kfunc_call_arg_meta *meta) { -return process_timer_func (env, regno, &meta->map); +return process_timer_func (env, reg, regno, &meta->map); } static int process_kptr_func(struct bpf_verifier_env *env, int regno,
@@ -7433,10 +7411,9 @@ static int process_kptr_func(struct bpf_verifier_env *env, int regno,* use case. The second level is tracked using the upper bit of bpf_dynptr->size * and checked dynamically during runtime. */ -static int process_dynptr_func (struct bpf_verifier_env *env,int regno,int insn_idx, +static int process_dynptr_func (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int insn_idx,enum bpf_arg_type arg_type,int clone_ref_obj_id) { -struct bpf_reg_state *reg =reg_state (env, regno);int err;if (reg->type !=PTR_TO_STACK && reg->type !=CONST_PTR_TO_DYNPTR ) {
@@ -7470,7 +7447,7 @@ static int process_dynptr_func(struct bpf_verifier_env *env, int regno, int insn/* we write BPF_DW bits (8 bytes) at a time */ for (i =0 ; i <BPF_DYNPTR_SIZE ; i +=8 ) { - err =check_mem_access (env, insn_idx, regno, + err =check_mem_access (env, insn_idx, reg, regno, i,BPF_DW ,BPF_WRITE ,-1 , false, false);if (err)return err;
@@ -7540,10 +7517,9 @@ static bool is_kfunc_arg_iter(struct bpf_kfunc_call_arg_meta *meta, int arg_idx,return btf_param_match_suffix (meta->btf, arg,"__iter" ); } -static int process_iter_arg (struct bpf_verifier_env *env,int regno,int insn_idx, +static int process_iter_arg (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int insn_idx,struct bpf_kfunc_call_arg_meta *meta) { -struct bpf_reg_state *reg =reg_state (env, regno);const struct btf_type *t;int spi, err, i, nr_slots, btf_id;
@@ -7575,7 +7551,7 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id }for (i =0 ; i < nr_slots *8 ; i +=BPF_REG_SIZE ) { - err =check_mem_access (env, insn_idx, regno, + err =check_mem_access (env, insn_idx, reg, regno, i,BPF_DW ,BPF_WRITE ,-1 , false, false);if (err)return err;
@@ -8014,12 +7990,11 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_DYNPTR] = &dynptr_types, }; -static int check_reg_type (struct bpf_verifier_env *env,u32 regno, +static int check_reg_type (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,enum bpf_arg_type arg_type,const u32 *arg_btf_id,struct bpf_call_arg_meta *meta) { -struct bpf_reg_state *reg =reg_state (env, regno);enum bpf_reg_type expected, type = reg->type;const struct bpf_reg_types *compatible;int i, j, err;
@@ -8362,7 +8337,7 @@ static int check_reg_const_str(struct bpf_verifier_env *env,return -EACCES ; } - err =check_map_access (env, regno,0 , + err =check_map_access (env, reg, regno,0 , map->value_size - reg->var_off.value, false,ACCESS_HELPER );if (err)
@@ -8498,7 +8473,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, base_type(arg_type) == ARG_PTR_TO_SPIN_LOCK) arg_btf_id = fn->arg_btf_id[arg]; - err =check_reg_type (env, regno, arg_type, arg_btf_id, meta); + err =check_reg_type (env, reg, regno, arg_type, arg_btf_id, meta);if (err)return err;
@@ -8636,11 +8611,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return -EACCES ; }if (meta->func_id == BPF_FUNC_spin_lock) { - err =process_spin_lock (env, regno,PROCESS_SPIN_LOCK ); + err =process_spin_lock (env, reg, regno,PROCESS_SPIN_LOCK );if (err)return err; }else if (meta->func_id == BPF_FUNC_spin_unlock) { - err =process_spin_lock (env, regno,0 ); + err =process_spin_lock (env, reg, regno,0 );if (err)return err; }else {
@@ -8649,7 +8624,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, }break ;case ARG_PTR_TO_TIMER : - err =process_timer_helper (env, regno, meta); + err =process_timer_helper (env, reg, regno, meta);if (err)return err;break ;
@@ -8684,7 +8659,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, true, meta);break ;case ARG_PTR_TO_DYNPTR : - err =process_dynptr_func (env, regno, insn_idx, arg_type,0 ); + err =process_dynptr_func (env, reg, regno, insn_idx, arg_type,0 );if (err)return err;break ;
@@ -9343,7 +9318,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,if (ret)return ret; - ret =process_dynptr_func (env, regno,-1 , arg->arg_type,0 ); + ret =process_dynptr_func (env, reg, regno,-1 , arg->arg_type,0 );if (ret)return ret; }else if (base_type (arg->arg_type) ==ARG_PTR_TO_BTF_ID ) {
@@ -9354,7 +9329,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,continue ;memset (&meta,0 ,sizeof (meta));/* leave func_id as zero */ - err =check_reg_type (env, regno, arg->arg_type, &arg->btf_id, &meta); + err =check_reg_type (env, reg, regno, arg->arg_type, &arg->btf_id, &meta); err = err ?:check_func_arg_reg_off (env, reg, regno, arg->arg_type);if (err)return err;
@@ -10312,18 +10287,18 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insnif (err)return err; + regs =cur_regs (env); +/* Mark slots with STACK_MISC in case of raw mode, stack offset * is inferred from register state. */ for (i =0 ; i < meta.access_size; i++) { - err =check_mem_access (env, insn_idx, meta.regno, i,BPF_B , + err =check_mem_access (env, insn_idx, regs + meta.regno, meta.regno, i,BPF_B ,BPF_WRITE ,-1 , false, false);if (err)return err; } - regs =cur_regs (env); -if (meta.release_regno) { err = -EINVAL ;if (arg_type_is_dynptr (fn->arg_type[meta.release_regno -BPF_REG_1 ])) {
@@ -11327,11 +11302,10 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,struct bpf_kfunc_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 argno, int nargs) + int argno, int nargs, struct bpf_reg_state *reg) { u32 regno = argno + 1;struct bpf_reg_state *regs =cur_regs (env); -struct bpf_reg_state *reg = ®s[regno];bool arg_mem_size = false; if (meta->func_id == special_kfunc_list[KF_bpf_cast_to_kern_ctx] ||
@@ -11498,10 +11472,9 @@ static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env,return 0 ; } -static int process_irq_flag (struct bpf_verifier_env *env,int regno, +static int process_irq_flag (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,struct bpf_kfunc_call_arg_meta *meta) { -struct bpf_reg_state *reg =reg_state (env, regno);int err, kfunc_class =IRQ_NATIVE_KFUNC ;bool irq_save;
@@ -11526,7 +11499,7 @@ static int process_irq_flag(struct bpf_verifier_env *env, int regno,return -EINVAL ; } - err =check_mem_access (env, env->insn_idx, regno,0 ,BPF_DW ,BPF_WRITE ,-1 , false, false); + err =check_mem_access (env, env->insn_idx, reg, regno,0 ,BPF_DW ,BPF_WRITE ,-1 , false, false);if (err)return err;
@@ -12114,7 +12087,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ 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); + kf_arg_type =get_kfunc_ptr_arg_type (env, meta, t, ref_t, ref_tname, args, i, nargs, reg);if (kf_arg_type <0 )return kf_arg_type;
@@ -12276,7 +12249,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ } } - ret =process_dynptr_func (env, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id); + ret =process_dynptr_func (env, reg, regno, insn_idx, dynptr_arg_type, clone_ref_obj_id);if (ret <0 )return ret;
@@ -12301,7 +12274,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_return -EINVAL ; } } - ret =process_iter_arg (env, regno, insn_idx, meta); + ret =process_iter_arg (env, reg, regno, insn_idx, meta);if (ret <0 )return ret;break ;
@@ -12478,7 +12451,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_verbose (env,"arg#%d doesn't point to a map value\n" , i);return -EINVAL ; } - ret =check_map_field_pointer (env, regno,BPF_WORKQUEUE , &meta->map); + ret =check_map_field_pointer (env, reg, regno,BPF_WORKQUEUE , &meta->map);if (ret <0 )return ret;break ;
@@ -12487,7 +12460,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_verbose (env,"arg#%d doesn't point to a map value\n" , i);return -EINVAL ; } - ret =process_timer_kfunc (env, regno, meta); + ret =process_timer_kfunc (env, reg, regno, meta);if (ret <0 )return ret;break ;
@@ -12496,7 +12469,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_verbose (env,"arg#%d doesn't point to a map value\n" , i);return -EINVAL ; } - ret =check_map_field_pointer (env, regno,BPF_TASK_WORK , &meta->map); + ret =check_map_field_pointer (env, reg, regno,BPF_TASK_WORK , &meta->map);if (ret <0 )return ret;break ;
@@ -12505,7 +12478,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_verbose (env,"arg#%d doesn't point to an irq flag on stack\n" , i);return -EINVAL ; } - ret =process_irq_flag (env, regno, meta); + ret =process_irq_flag (env, reg, regno, meta);if (ret <0 )return ret;break ;
@@ -12526,7 +12499,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_if (meta->func_id == special_kfunc_list[KF_bpf_res_spin_lock_irqsave] || meta->func_id == special_kfunc_list[KF_bpf_res_spin_unlock_irqrestore]) flags |=PROCESS_LOCK_IRQ ; - ret =process_spin_lock (env, regno, flags); + ret =process_spin_lock (env, reg, regno, flags);if (ret <0 )return ret;break ;
@@ -13660,7 +13633,7 @@ static int check_stack_access_for_ptr_arithmetic(static int sanitize_check_bounds (struct bpf_verifier_env *env,const struct bpf_insn *insn, -const struct bpf_reg_state *dst_reg) +struct bpf_reg_state *dst_reg) {u32 dst = insn->dst_reg;
@@ -13677,7 +13650,7 @@ static int sanitize_check_bounds(struct bpf_verifier_env *env,return -EACCES ;break ;case PTR_TO_MAP_VALUE : -if (check_map_access (env, dst,0 ,1 , false,ACCESS_HELPER )) { +if (check_map_access (env, dst_reg, dst,0 ,1 , false,ACCESS_HELPER )) {verbose (env,"R%d pointer arithmetic of map value goes out of range, " "prohibited for !root\n" , dst);return -EACCES ;
@@ -17563,7 +17536,7 @@ static int do_check_insn(struct bpf_verifier_env *env, bool *do_print_state) dst_reg_type =cur_regs (env)[insn->dst_reg].type; - err =check_mem_access (env, env->insn_idx, insn->dst_reg, + err =check_mem_access (env, env->insn_idx,cur_regs (env) + insn->dst_reg, insn->dst_reg, insn->off,BPF_SIZE (insn->code),BPF_WRITE ,-1 , false, false);if (err)
Signature · 1 line
2.52.0
Re: [PATCH bpf-next 4/9] bpf: Refactor to avoid redundant calculation of bpf_reg_state
Quoted 25 lines · Yonghong Song
In many cases, once a
bpf_reg_stateis defined, it can pass to callee's. Otherwise, callee will need to getbpf_reg_stateagain based on regno. More importantly, this is needed for later stack arguments for kfuncs since the register state for stack arguments does not have a corresponding regno. So it makes sense to pass reg state for callee's.The following is the only change to avoid compilation warning:
static int sanitize_check_bounds(structbpf_verifier_env*env,
const structbpf_insn*insn,
- const structbpf_reg_state*dst_reg)
+ structbpf_reg_state*dst_reg)Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/verifier.c | 213 ++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 120 deletions(-)kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static int check_stack_write_fixed_off(struct bpf_verifier_env *env, · line 3934
Quoted 7 lines
kernel/bpf/verifier.c +1
-1@@ -3929,13 +3929,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,static int check_stack_write_var_off (struct bpf_verifier_env *env,/* func where register points to */ struct bpf_func_state *state, -int ptr_regno,int off,int size, +struct bpf_reg_state *ptr_reg,int off,int size,int value_regno,int insn_idx) {
The comment needs to be updated.
Quoted 26 lines
kernel/bpf/verifier.c +2
-3struct bpf_func_state *cur;/* state of the current function */ int min_off, max_off;int i, err; -struct bpf_reg_state *ptr_reg =NULL , *value_reg =NULL ; +struct bpf_reg_state *value_reg =NULL ;struct bpf_insn *insn = &env->prog->insnsi[insn_idx];bool writing_zero = false;/* set if the fact that we're writing a zero is used to let any @@ -3944,7 +3944,6 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,bool zero_used = false; cur = env->cur_state->frame[env->cur_state->curframe]; - ptr_reg = &cur->regs[ptr_regno]; min_off = ptr_reg->smin_value + off; max_off = ptr_reg->smax_value + off + size;if (value_regno >=0 )@@ -4241,7 +4240,7 @@ enum bpf_access_src { ACCESS_HELPER = 2, /* the access is performed by a helper */ }; -static int check_stack_range_initialized(struct bpf_verifier_env *env, +static int check_stack_range_initialized(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int regno, int off, int access_size, bool zero_size_allowed, enum bpf_access_type type,
kernel/bpf/verifier.c static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) · line 4266
Quoted 3 lines
kernel/bpf/verifier.c +0
-0@@ -4265,18 +4264,16 @@ static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)* offset; for a fixed offset check_stack_read_fixed_off should be used * instead. */
Same here
Quoted 39 lines
kernel/bpf/verifier.c +4
-7-static int check_stack_read_var_off (struct bpf_verifier_env *env, +static int check_stack_read_var_off (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -/* The state of the source register. */ -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *ptr_state =bpf_func (env, reg);int err;int min_off, max_off;/* Note that we pass a NULL meta, so raw access will not be permitted. */ - err =check_stack_range_initialized (env, ptr_regno, off, size, + err =check_stack_range_initialized (env, reg, ptr_regno, off, size, false,BPF_READ ,NULL );if (err)return err;@@ -4298,10 +4295,9 @@ static int check_stack_read_var_off(struct bpf_verifier_env *env,* can be -1, meaning that the read value is not going to a register. */ static int check_stack_read (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;/* Some accesses are only permitted with a static offset. */ @@ -4337,7 +4333,7 @@ static int check_stack_read(struct bpf_verifier_env *env,* than fixed offset ones. Note that dst_regno >= 0 on this * branch. */ - err =check_stack_read_var_off (env, ptr_regno, off, size, + err =check_stack_read_var_off (env, reg, ptr_regno, off, size, dst_regno); }return err;
kernel/bpf/verifier.c static int check_stack_read(struct bpf_verifier_env *env, · line 4351
Quoted 2 lines
kernel/bpf/verifier.c +0
-0@@ -4354,10 +4350,9 @@ static int check_stack_read(struct bpf_verifier_env *env,* The caller must ensure that the offset falls within the maximum stack size. */
Ditto
Quoted 10 lines
kernel/bpf/verifier.c +1
-2static int check_stack_write (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int off,int size,int value_regno,int insn_idx) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;
kernel/bpf/verifier.c static int check_stack_write(struct bpf_verifier_env *env, · line 4373
Quoted 10 lines
kernel/bpf/verifier.c +1
-1@@ -4370,16 +4365,15 @@ static int check_stack_write(struct bpf_verifier_env *env,* than fixed offset ones. */ err =check_stack_write_var_off (env, state, - ptr_regno, off, size, + reg, off, size, value_regno, insn_idx); }return err; }
Otherwise looks good to me.
Yonghong Song
reviewerRe: [PATCH bpf-next 4/9] bpf: Refactor to avoid redundant calculation of bpf_reg_state
Quoted 99 lines · Amery Hung
Quoted 24 lines · Yonghong Song
In many cases, once a
bpf_reg_stateis defined, it can pass to callee's. Otherwise, callee will need to getbpf_reg_stateagain based on regno. More importantly, this is needed for later stack arguments for kfuncs since the register state for stack arguments does not have a corresponding regno. So it makes sense to pass reg state for callee's.The following is the only change to avoid compilation warning:
static int sanitize_check_bounds(structbpf_verifier_env*env,
const structbpf_insn*insn,
- const structbpf_reg_state*dst_reg)
+ structbpf_reg_state*dst_reg)Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/verifier.c | 213 ++++++++++++++++++------------------------ 1 file changed, 93 insertions(+), 120 deletions(-)kernel/bpf/verifier.c +0
-0kernel/bpf/verifier.c static int check_stack_write_fixed_off(struct bpf_verifier_env *env, · line 3934
Quoted 7 lines
kernel/bpf/verifier.c +1
-1@@ -3929,13 +3929,13 @@ static int check_stack_write_fixed_off(struct bpf_verifier_env *env,static int check_stack_write_var_off (struct bpf_verifier_env *env,/* func where register points to */ struct bpf_func_state *state, -int ptr_regno,int off,int size, +struct bpf_reg_state *ptr_reg,int off,int size,int value_regno,int insn_idx) {The comment needs to be updated.
Quoted 26 lines
kernel/bpf/verifier.c +2
-3struct bpf_func_state *cur;/* state of the current function */ int min_off, max_off;int i, err; -struct bpf_reg_state *ptr_reg =NULL , *value_reg =NULL ; +struct bpf_reg_state *value_reg =NULL ;struct bpf_insn *insn = &env->prog->insnsi[insn_idx];bool writing_zero = false;/* set if the fact that we're writing a zero is used to let any @@ -3944,7 +3944,6 @@ static int check_stack_write_var_off(struct bpf_verifier_env *env,bool zero_used = false; cur = env->cur_state->frame[env->cur_state->curframe]; - ptr_reg = &cur->regs[ptr_regno]; min_off = ptr_reg->smin_value + off; max_off = ptr_reg->smax_value + off + size;if (value_regno >=0 )@@ -4241,7 +4240,7 @@ enum bpf_access_src { ACCESS_HELPER = 2, /* the access is performed by a helper */ }; -static int check_stack_range_initialized(struct bpf_verifier_env *env, +static int check_stack_range_initialized(struct bpf_verifier_env *env, struct bpf_reg_state *reg, int regno, int off, int access_size, bool zero_size_allowed, enum bpf_access_type type,kernel/bpf/verifier.c static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno) · line 4266
Quoted 3 lines
kernel/bpf/verifier.c +0
-0@@ -4265,18 +4264,16 @@ static struct bpf_reg_state *reg_state(struct bpf_verifier_env *env, int regno)* offset; for a fixed offset check_stack_read_fixed_off should be used * instead. */ Same here
Quoted 34 lines
kernel/bpf/verifier.c +3
-7-static int check_stack_read_var_off (struct bpf_verifier_env *env, +static int check_stack_read_var_off (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -/* The state of the source register. */ -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *ptr_state =bpf_func (env, reg);int err;int min_off, max_off;/* Note that we pass a NULL meta, so raw access will not be permitted. */ - err =check_stack_range_initialized (env, ptr_regno, off, size, + err =check_stack_range_initialized (env, reg, ptr_regno, off, size, false,BPF_READ ,NULL );if (err)return err;@@ -4298,10 +4295,9 @@ static int check_stack_read_var_off(struct bpf_verifier_env *env,* can be -1, meaning that the read value is not going to a register. */ static int check_stack_read (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int ptr_regno,int off,int size,int dst_regno) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;/* Some accesses are only permitted with a static offset. */ @@ -4337,7 +4333,7 @@ static int check_stack_read(struct bpf_verifier_env *env,* than fixed offset ones. Note that dst_regno >= 0 on this * branch. */ - err = check_stack_read_var_off(env, ptr_regno, off, size,
Quoted 8 lines
kernel/bpf/verifier.c static int check_stack_read(struct bpf_verifier_env *env, · line 4351
Quoted 7 lines
kernel/bpf/verifier.c +1
-0+ err =check_stack_read_var_off (env, reg, ptr_regno, off, size, dst_regno); }return err;@@ -4354,10 +4350,9 @@ static int check_stack_read(struct bpf_verifier_env *env,* The caller must ensure that the offset falls within the maximum stack size. */ Ditto
Ack. Will fix all the above in comments.
Quoted 24 lines
Quoted 11 lines
kernel/bpf/verifier.c +1
-2static int check_stack_write (struct bpf_verifier_env *env, -int ptr_regno,int off,int size, +struct bpf_reg_state *reg,int off,int size,int value_regno,int insn_idx) { -struct bpf_reg_state *reg =reg_state (env, ptr_regno);struct bpf_func_state *state =bpf_func (env, reg);int err;kernel/bpf/verifier.c static int check_stack_write(struct bpf_verifier_env *env, · line 4373
Quoted 10 lines
kernel/bpf/verifier.c +1
-1@@ -4370,16 +4365,15 @@ static int check_stack_write(struct bpf_verifier_env *env,* than fixed offset ones. */ err =check_stack_write_var_off (env, state, - ptr_regno, off, size, + reg, off, size, value_regno, insn_idx); }return err; }Otherwise looks good to me.
Reviewed-by Amery Hung <[email protected]>
Yonghong Song
reviewerPATCH 5/9 bpf: Refactor to handle memory and size together
In reply to Yonghong SongSimilar to the previous patch, try to pass bpf_reg_state from caller to callee. Both mem_reg and size_reg are passed to helper functions. This is important for stack arguments as they may be beyond registers 1-5.
--- kernel/bpf/verifier.c | 57 +++++++++++++++++++++---------------------- 1 file changed, 28 insertions(+), 29 deletions(-)
kernel/bpf/verifier.c +28 -29
@@ -6934,12 +6934,12 @@ static int check_stack_range_initialized(return 0 ; } -static int check_helper_mem_access (struct bpf_verifier_env *env,int regno, +static int check_helper_mem_access (struct bpf_verifier_env *env,struct bpf_reg_state *reg,int regno,int access_size,enum bpf_access_type access_type,bool zero_size_allowed,struct bpf_call_arg_meta *meta) { -struct bpf_reg_state *regs =cur_regs (env), *reg = ®s[regno]; +struct bpf_reg_state *regs =cur_regs (env);u32 *max_access;switch (base_type (reg->type)) {
@@ -7022,12 +7022,12 @@ static int check_helper_mem_access(struct bpf_verifier_env *env, int regno,/* verify arguments to helpers or kfuncs consisting of a pointer and an access * size. * -* @regno is the register containing the access size. regno-1 is the register -* containing the pointer. +* @mem_reg contains the pointer, @size_reg contains the access size. */ static int check_mem_size_reg (struct bpf_verifier_env *env, -struct bpf_reg_state *reg,u32 regno, -enum bpf_access_type access_type, +struct bpf_reg_state *mem_reg, +struct bpf_reg_state *size_reg,u32 mem_regno, +u32 size_regno,enum bpf_access_type access_type,bool zero_size_allowed,struct bpf_call_arg_meta *meta) {
@@ -7041,37 +7041,37 @@ static int check_mem_size_reg(struct bpf_verifier_env *env,* out. Only upper bounds can be learned because retval is an * int type and negative retvals are allowed. */ - meta->msize_max_value = reg->umax_value; + meta->msize_max_value = size_reg->umax_value;/* The register is SCALAR_VALUE; the access check happens using * its boundaries. For unprivileged variable accesses, disable * raw mode so that the program is required to initialize all * the memory that the helper could just partially fill up. */ -if (!tnum_is_const (reg->var_off)) +if (!tnum_is_const (size_reg->var_off)) meta =NULL ; -if (reg->smin_value <0 ) { +if (size_reg->smin_value <0 ) {verbose (env,"R%d min value is negative, either use unsigned or 'var &= const'\n" , - regno); + size_regno);return -EACCES ; } -if (reg->umin_value ==0 && !zero_size_allowed) { +if (size_reg->umin_value ==0 && !zero_size_allowed) {verbose (env,"R%d invalid zero-sized read: u64=[%lld,%lld]\n" , - regno, reg->umin_value, reg->umax_value); + size_regno, size_reg->umin_value, size_reg->umax_value);return -EACCES ; } -if (reg->umax_value >=BPF_MAX_VAR_SIZ ) { +if (size_reg->umax_value >=BPF_MAX_VAR_SIZ ) {verbose (env,"R%d unbounded memory access, use 'var &= const' or 'if (var < const)'\n" , - regno); + size_regno);return -EACCES ; } - err =check_helper_mem_access (env, regno -1 , reg->umax_value, + err =check_helper_mem_access (env, mem_reg, mem_regno, size_reg->umax_value, access_type, zero_size_allowed, meta);if (!err) - err =mark_chain_precision (env, regno); + err =mark_chain_precision (env, size_regno);return err; }
@@ -7096,8 +7096,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, regno, size,BPF_READ , true,NULL ); - err = err ?:check_helper_mem_access (env, regno, size,BPF_WRITE , true,NULL ); + err =check_helper_mem_access (env, reg, regno, size,BPF_READ , true,NULL ); + err = err ?:check_helper_mem_access (env, reg, regno, size,BPF_WRITE , true,NULL );if (may_be_null) *reg = saved_reg;
@@ -7105,10 +7105,9 @@ static int check_mem_reg(struct bpf_verifier_env *env, struct bpf_reg_state *regreturn err; } -static int check_kfunc_mem_size_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, -u32 regno) +static int check_kfunc_mem_size_reg (struct bpf_verifier_env *env,struct bpf_reg_state *mem_reg, +struct bpf_reg_state *size_reg,u32 mem_regno,u32 size_regno) { -struct bpf_reg_state *mem_reg = &cur_regs (env)[regno -1 ];bool may_be_null =type_may_be_null (mem_reg->type);struct bpf_reg_state saved_reg;struct bpf_call_arg_meta meta;
@@ -7121,8 +7120,8 @@ static int check_kfunc_mem_size_reg(struct bpf_verifier_env *env, struct bpf_regmark_ptr_not_null_reg (mem_reg); } - err =check_mem_size_reg (env, reg, regno,BPF_READ , true, &meta); - err = err ?:check_mem_size_reg (env, reg, regno,BPF_WRITE , true, &meta); + err =check_mem_size_reg (env, mem_reg, size_reg, mem_regno, size_regno,BPF_READ , true, &meta); + err = err ?:check_mem_size_reg (env, mem_reg, size_reg, mem_regno, size_regno,BPF_WRITE , true, &meta);if (may_be_null) *mem_reg = saved_reg;
@@ -8566,7 +8565,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return -EFAULT ; } key_size = meta->map.ptr->key_size; - err =check_helper_mem_access (env, regno, key_size,BPF_READ , false,NULL ); + err =check_helper_mem_access (env, reg, regno, key_size,BPF_READ , false,NULL );if (err)return err;if (can_elide_value_nullness (meta->map.ptr->map_type)) {
@@ -8593,7 +8592,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,return -EFAULT ; } meta->raw_mode = arg_type &MEM_UNINIT ; - err =check_helper_mem_access (env, regno, meta->map.ptr->value_size, + err =check_helper_mem_access (env, reg, regno, meta->map.ptr->value_size, arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , false, meta);break ;
@@ -8637,7 +8636,7 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,*/ meta->raw_mode = arg_type &MEM_UNINIT ;if (arg_type &MEM_FIXED_SIZE ) { - err =check_helper_mem_access (env, regno, fn->arg_size[arg], + err =check_helper_mem_access (env, reg, regno, fn->arg_size[arg], arg_type &MEM_WRITE ?BPF_WRITE :BPF_READ , false, meta);if (err)
@@ -8647,13 +8646,13 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, }break ;case ARG_CONST_SIZE : - err =check_mem_size_reg (env, reg, regno, + err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg, regno -1 , regno, 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, regno, + err =check_mem_size_reg (env,reg_state (env, regno -1 ), reg, regno -1 , regno, fn->arg_type[arg -1 ] &MEM_WRITE ?BPF_WRITE :BPF_READ , true, meta);
@@ -12384,7 +12383,7 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_const struct btf_param *size_arg = &args[i +1 ];if (!bpf_register_is_null (buff_reg) || !is_kfunc_arg_nullable (meta->btf, buff_arg)) { - ret =check_kfunc_mem_size_reg (env, size_reg, regno +1 ); + ret =check_kfunc_mem_size_reg (env, buff_reg, size_reg, regno, regno +1 );if (ret <0 ) {verbose (env,"arg#%d arg#%d memory, len pair leads to invalid memory access\n" , i, i +1 );return ret;
Signature · 1 line
2.52.0
Yonghong Song
reviewerPATCH 6/9 bpf: Rename existing argno to arg
In reply to Yonghong SongTo support stack arguments, in later patches, argno will represent both registers and stack arguments. To avoid confusion, rename existing argno to arg.
--- kernel/bpf/verifier.c | 54 +++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 27 deletions(-)
kernel/bpf/verifier.c +27 -27
@@ -11301,9 +11301,9 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,struct bpf_kfunc_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 argno, int nargs, struct bpf_reg_state *reg) + int arg, int nargs, struct bpf_reg_state *reg) { - u32 regno = argno + 1; + u32 regno = arg + 1;struct bpf_reg_state *regs =cur_regs (env);bool arg_mem_size = false;
@@ -11312,9 +11312,9 @@ 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 (argno +1 < nargs && - (is_kfunc_arg_mem_size (meta->btf, &args[argno +1 ], ®s[regno +1 ]) || -is_kfunc_arg_const_mem_size (meta->btf, &args[argno +1 ], ®s[regno +1 ]))) +if (arg +1 < nargs && + (is_kfunc_arg_mem_size (meta->btf, &args[arg +1 ], ®s[regno +1 ]) || +is_kfunc_arg_const_mem_size (meta->btf, &args[arg +1 ], ®s[regno +1 ]))) arg_mem_size = true;/* In this function, we verify the kfunc's BTF as per the argument type,
@@ -11322,68 +11322,68 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,* 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), argno)) +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[argno]) &&bpf_register_is_null (reg) && +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[argno])) +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[argno])) +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[argno])) +if (is_kfunc_arg_dynptr (meta->btf, &args[arg]))return KF_ARG_PTR_TO_DYNPTR ; -if (is_kfunc_arg_iter (meta, argno, &args[argno])) +if (is_kfunc_arg_iter (meta, arg, &args[arg]))return KF_ARG_PTR_TO_ITER ; -if (is_kfunc_arg_list_head (meta->btf, &args[argno])) +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[argno])) +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[argno])) +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[argno])) +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[argno])) +if (is_kfunc_arg_const_str (meta->btf, &args[arg]))return KF_ARG_PTR_TO_CONST_STR ; -if (is_kfunc_arg_map (meta->btf, &args[argno])) +if (is_kfunc_arg_map (meta->btf, &args[arg]))return KF_ARG_PTR_TO_MAP ; -if (is_kfunc_arg_wq (meta->btf, &args[argno])) +if (is_kfunc_arg_wq (meta->btf, &args[arg]))return KF_ARG_PTR_TO_WORKQUEUE ; -if (is_kfunc_arg_timer (meta->btf, &args[argno])) +if (is_kfunc_arg_timer (meta->btf, &args[arg]))return KF_ARG_PTR_TO_TIMER ; -if (is_kfunc_arg_task_work (meta->btf, &args[argno])) +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[argno])) +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[argno])) +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 args#%d pointer type %s %s is not supported\n" , - meta->func_name, argno,btf_type_str (ref_t), ref_tname); + meta->func_name, arg,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[argno])) +if (is_kfunc_arg_callback (env, meta->btf, &args[arg]))return KF_ARG_PTR_TO_CALLBACK ;/* This is the catch all argument type of register types supported by
@@ -11394,7 +11394,7 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env,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,"arg#%d pointer type %s %s must point to %sscalar, or struct with scalar\n" , - argno,btf_type_str (ref_t), ref_tname, arg_mem_size ?"void, " :"" ); + arg,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 ;
@@ -11405,7 +11405,7 @@ static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env,const struct btf_type *ref_t, const char *ref_tname, u32 ref_id, struct bpf_kfunc_call_arg_meta *meta, - int argno) + int arg) { const struct btf_type *reg_ref_t;bool strict_type_match = false;
@@ -11464,7 +11464,7 @@ static int process_kf_arg_ptr_to_btf_id(struct bpf_verifier_env *env, taking_projection =btf_is_projection_of (ref_tname, reg_ref_tname);if (!taking_projection && !struct_same) {verbose (env,"kernel function %s args#%d expected pointer to %s %s but R%d has a pointer to %s %s\n" , - meta->func_name, argno,btf_type_str (ref_t), ref_tname, argno +1 , + meta->func_name, arg,btf_type_str (ref_t), ref_tname, arg +1 ,btf_type_str (reg_ref_t), reg_ref_tname);return -EINVAL ; }
Signature · 1 line
2.52.0
Yonghong Song
reviewerPATCH 7/9 bpf: Prepare verifier logs for upcoming kfunc stack arguments
In reply to Yonghong SongThis change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in R1 to R5, but it does not work once kfunc arguments can also be passed on the stack.
Introduce an internal argno representation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base. reg_arg_name() converts this representation into either R%d or *(R11-off) when emitting verifier logs. If a particular argno is corresponding to a stack argument, print *(R11-off). Otherwise, print R%d. Here R11 presents the base of stack arguments.
This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)
include/linux/bpf_verifier.h +1 -0
@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;
kernel/bpf/verifier.c +364 -276
1,739 lines not on this page · raw message
tools/testing/selftests/bpf/prog_tests/bpf_nf.c +11 -11
@@ -11,18 +11,18 @@ struct {const char *prog_name;const char *err_msg; } test_bpf_nf_fail_tests[] = { - { "alloc_release", "kernel function bpf_ct_release args#0 expected pointer to STRUCT nf_conn but" }, - { "insert_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, - { "lookup_insert", "kernel function bpf_ct_insert_entry args#0 expected pointer to STRUCT nf_conn___init but" }, - { "set_timeout_after_insert", "kernel function bpf_ct_set_timeout args#0 expected pointer to STRUCT nf_conn___init but" }, - { "set_status_after_insert", "kernel function bpf_ct_set_status args#0 expected pointer to STRUCT nf_conn___init but" }, - { "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" }, - { "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" }, + { "alloc_release", "kernel function bpf_ct_release R1 expected pointer to STRUCT nf_conn but" }, + { "insert_insert", "kernel function bpf_ct_insert_entry R1 expected pointer to STRUCT nf_conn___init but" }, + { "lookup_insert", "kernel function bpf_ct_insert_entry R1 expected pointer to STRUCT nf_conn___init but" }, + { "set_timeout_after_insert", "kernel function bpf_ct_set_timeout R1 expected pointer to STRUCT nf_conn___init but" }, + { "set_status_after_insert", "kernel function bpf_ct_set_status R1 expected pointer to STRUCT nf_conn___init but" }, + { "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout R1 expected pointer to STRUCT nf_conn but" }, + { "change_status_after_alloc", "kernel function bpf_ct_change_status R1 expected pointer to STRUCT nf_conn but" }, { "write_not_allowlisted_field", "no write support to nf_conn at off" }, - { "lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted arg1" }, - { "lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted arg3" }, - { "xdp_lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted arg1" }, - { "xdp_lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted arg3" }, + { "lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted R2" }, + { "lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted R4" }, + { "xdp_lookup_null_bpf_tuple", "Possibly NULL pointer passed to trusted R2" }, + { "xdp_lookup_null_bpf_opts", "Possibly NULL pointer passed to trusted R4" }, }; enum {
tools/testing/selftests/bpf/prog_tests/cb_refs.c +1 -1
@@ -12,7 +12,7 @@ struct {const char *err_msg; } cb_refs_tests[] = { { "underflow_prog", "must point to scalar, or struct with scalar" }, - { "leak_prog", "Possibly NULL pointer passed to helper arg2" }, + { "leak_prog", "Possibly NULL pointer passed to helper R2" }, { "nested_cb", "Unreleased reference id=4 alloc_insn=2" }, /* alloc_insn=2{4,5} */ { "non_cb_transfer_ref", "Unreleased reference id=4 alloc_insn=1" }, /* alloc_insn=1{1,2} */ };
tools/testing/selftests/bpf/prog_tests/kfunc_call.c +1 -1
@@ -68,7 +68,7 @@ static struct kfunc_test_params kfunc_tests[] = {TC_FAIL (kfunc_call_test_get_mem_fail_oob,0 ,"min value is outside of the allowed memory range" ),TC_FAIL (kfunc_call_test_get_mem_fail_not_const,0 ,"is not a const" ),TC_FAIL (kfunc_call_test_mem_acquire_fail,0 ,"acquire kernel function does not return PTR_TO_BTF_ID" ), -TC_FAIL (kfunc_call_test_pointer_arg_type_mismatch,0 ,"arg#0 expected pointer to ctx, but got scalar" ), +TC_FAIL (kfunc_call_test_pointer_arg_type_mismatch,0 ,"R1 expected pointer to ctx, but got scalar" ),/* success cases */ TC_TEST(kfunc_call_test1, 12),
tools/testing/selftests/bpf/prog_tests/linked_list.c +2 -2
@@ -81,8 +81,8 @@ static struct { { "direct_write_node", "direct access to bpf_list_node is disallowed" }, { "use_after_unlock_push_front", "invalid mem access 'scalar'" }, { "use_after_unlock_push_back", "invalid mem access 'scalar'" }, - { "double_push_front", "arg#1 expected pointer to allocated object" }, - { "double_push_back", "arg#1 expected pointer to allocated object" }, + { "double_push_front", "R2 expected pointer to allocated object" }, + { "double_push_back", "R2 expected pointer to allocated object" }, { "no_node_value_type", "bpf_list_node not found at offset=0" }, { "incorrect_value_type","operation on bpf_list_head expects arg#1 bpf_list_node at offset=48 in struct foo, "
tools/testing/selftests/bpf/progs/cgrp_kfunc_failure.c +7 -7
@@ -29,7 +29,7 @@ static struct __cgrps_kfunc_map_value *insert_lookup_cgrp(struct cgroup *cgrp) } SEC("tp_btf/cgroup_mkdir") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(cgrp_kfunc_acquire_untrusted, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -48,7 +48,7 @@ int BPF_PROG(cgrp_kfunc_acquire_untrusted, struct cgroup *cgrp, const char *path } SEC("tp_btf/cgroup_mkdir") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(cgrp_kfunc_acquire_no_null_check, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -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("arg#0 pointer type STRUCT cgroup must point") +__failure __msg("R1 pointer type STRUCT cgroup must point") int BPF_PROG(cgrp_kfunc_acquire_fp, struct cgroup *cgrp, const char *path) {struct cgroup *acquired, *stack_cgrp = (struct cgroup *)&path;
@@ -106,7 +106,7 @@ int BPF_PROG(cgrp_kfunc_acquire_trusted_walked, struct cgroup *cgrp, const char } SEC("tp_btf/cgroup_mkdir") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(cgrp_kfunc_acquire_null, struct cgroup *cgrp, const char *path) {struct cgroup *acquired;
@@ -175,7 +175,7 @@ int BPF_PROG(cgrp_kfunc_rcu_get_release, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(cgrp_kfunc_release_untrusted, struct cgroup *cgrp, const char *path) {struct __cgrps_kfunc_map_value *v;
@@ -191,7 +191,7 @@ int BPF_PROG(cgrp_kfunc_release_untrusted, struct cgroup *cgrp, const char *path } SEC("tp_btf/cgroup_mkdir") -__failure __msg("arg#0 pointer type STRUCT cgroup must point") +__failure __msg("R1 pointer type STRUCT cgroup must point") int BPF_PROG(cgrp_kfunc_release_fp, struct cgroup *cgrp, const char *path) {struct cgroup *acquired = (struct cgroup *)&path;
@@ -203,7 +203,7 @@ int BPF_PROG(cgrp_kfunc_release_fp, struct cgroup *cgrp, const char *path) } SEC("tp_btf/cgroup_mkdir") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(cgrp_kfunc_release_null, struct cgroup *cgrp, const char *path) {struct __cgrps_kfunc_map_value local, *v;
tools/testing/selftests/bpf/progs/cpumask_failure.c +5 -5
@@ -45,7 +45,7 @@ int BPF_PROG(test_alloc_no_release, struct task_struct *task, u64 clone_flags) } SEC("tp_btf/task_newtask") -__failure __msg("NULL pointer passed to trusted arg0") +__failure __msg("NULL pointer passed to trusted R1") int BPF_PROG(test_alloc_double_release, struct task_struct *task, u64 clone_flags) {struct bpf_cpumask *cpumask;
@@ -73,7 +73,7 @@ int BPF_PROG(test_acquire_wrong_cpumask, struct task_struct *task, u64 clone_fla } SEC("tp_btf/task_newtask") -__failure __msg("bpf_cpumask_set_cpu args#1 expected pointer to STRUCT bpf_cpumask") +__failure __msg("bpf_cpumask_set_cpu R2 expected pointer to STRUCT bpf_cpumask") int BPF_PROG(test_mutate_cpumask, struct task_struct *task, u64 clone_flags) {/* Can't set the CPU of a non-struct bpf_cpumask. */
@@ -107,7 +107,7 @@ int BPF_PROG(test_insert_remove_no_release, struct task_struct *task, u64 clone_ } SEC("tp_btf/task_newtask") -__failure __msg("NULL pointer passed to trusted arg0") +__failure __msg("NULL pointer passed to trusted R1") int BPF_PROG(test_cpumask_null, struct task_struct *task, u64 clone_flags) {/* NULL passed to kfunc. */
@@ -151,7 +151,7 @@ int BPF_PROG(test_global_mask_out_of_rcu, struct task_struct *task, u64 clone_fl } SEC("tp_btf/task_newtask") -__failure __msg("NULL pointer passed to trusted arg1") +__failure __msg("NULL pointer passed to trusted R2") int BPF_PROG(test_global_mask_no_null_check, struct task_struct *task, u64 clone_flags) {struct bpf_cpumask *local, *prev;
@@ -179,7 +179,7 @@ int BPF_PROG(test_global_mask_no_null_check, struct task_struct *task, u64 clone } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to helper arg2") +__failure __msg("Possibly NULL pointer passed to helper R2") int BPF_PROG(test_global_mask_rcu_no_null_check, struct task_struct *task, u64 clone_flags) {struct bpf_cpumask *prev, *curr;
tools/testing/selftests/bpf/progs/dynptr_fail.c +11 -11
@@ -149,7 +149,7 @@ int ringbuf_release_uninit_dynptr(void *ctx)/* A dynptr can't be used after it has been invalidated */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #2") +__failure __msg("Expected an initialized dynptr as R3")int use_after_invalid (void *ctx) {struct bpf_dynptr ptr;
@@ -448,7 +448,7 @@ int invalid_helper2(void *ctx)/* A bpf_dynptr is invalidated if it's been written into */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int invalid_write1 (void *ctx) {struct bpf_dynptr ptr;
@@ -1642,7 +1642,7 @@ int invalid_slice_rdwr_rdonly(struct __sk_buff *skb)/* bpf_dynptr_adjust can only be called on initialized dynptrs */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int dynptr_adjust_invalid (void *ctx) {struct bpf_dynptr ptr = {};
@@ -1655,7 +1655,7 @@ int dynptr_adjust_invalid(void *ctx)/* bpf_dynptr_is_null can only be called on initialized dynptrs */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int dynptr_is_null_invalid (void *ctx) {struct bpf_dynptr ptr = {};
@@ -1668,7 +1668,7 @@ int dynptr_is_null_invalid(void *ctx)/* bpf_dynptr_is_rdonly can only be called on initialized dynptrs */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int dynptr_is_rdonly_invalid (void *ctx) {struct bpf_dynptr ptr = {};
@@ -1681,7 +1681,7 @@ int dynptr_is_rdonly_invalid(void *ctx)/* bpf_dynptr_size can only be called on initialized dynptrs */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int dynptr_size_invalid (void *ctx) {struct bpf_dynptr ptr = {};
@@ -1694,7 +1694,7 @@ int dynptr_size_invalid(void *ctx)/* Only initialized dynptrs can be cloned */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #0") +__failure __msg("Expected an initialized dynptr as R1")int clone_invalid1 (void *ctx) {struct bpf_dynptr ptr1 = {};
@@ -1728,7 +1728,7 @@ int clone_invalid2(struct xdp_md *xdp)/* Invalidating a dynptr should invalidate its clones */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #2") +__failure __msg("Expected an initialized dynptr as R3")int clone_invalidate1 (void *ctx) {struct bpf_dynptr clone;
@@ -1749,7 +1749,7 @@ int clone_invalidate1(void *ctx)/* Invalidating a dynptr should invalidate its parent */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #2") +__failure __msg("Expected an initialized dynptr as R3")int clone_invalidate2 (void *ctx) {struct bpf_dynptr ptr;
@@ -1770,7 +1770,7 @@ int clone_invalidate2(void *ctx)/* Invalidating a dynptr should invalidate its siblings */ SEC("?raw_tp") -__failure __msg("Expected an initialized dynptr as arg #2") +__failure __msg("Expected an initialized dynptr as R3")int clone_invalidate3 (void *ctx) {struct bpf_dynptr ptr;
@@ -1981,7 +1981,7 @@ __noinline long global_call_bpf_dynptr(const struct bpf_dynptr *dynptr) } SEC("?raw_tp") -__failure __msg("arg#0 expected pointer to stack or const struct bpf_dynptr") +__failure __msg("R1 expected pointer to stack or const struct bpf_dynptr") int test_dynptr_reg_type(void *ctx) {struct task_struct *current =NULL ;
tools/testing/selftests/bpf/progs/file_reader_fail.c +2 -2
@@ -30,7 +30,7 @@ int on_nanosleep_unreleased_ref(void *ctx) SEC("xdp") __failure -__msg("Expected a dynptr of type file as arg #0") +__msg("Expected a dynptr of type file as R1")int xdp_wrong_dynptr_type (struct xdp_md *xdp) {struct bpf_dynptr dynptr;
@@ -42,7 +42,7 @@ int xdp_wrong_dynptr_type(struct xdp_md *xdp) SEC("xdp") __failure -__msg("Expected an initialized dynptr as arg #0") +__msg("Expected an initialized dynptr as R1")int xdp_no_dynptr_type (struct xdp_md *xdp) {struct bpf_dynptr dynptr;
tools/testing/selftests/bpf/progs/irq.c +2 -2
@@ -15,7 +15,7 @@ struct bpf_res_spin_lock lockA __hidden SEC(".data.A"); struct bpf_res_spin_lock lockB __hidden SEC(".data.B"); SEC("?tc") -__failure __msg("arg#0 doesn't point to an irq flag on stack") +__failure __msg("R1 doesn't point to an irq flag on stack") int irq_save_bad_arg(struct __sk_buff *ctx) {bpf_local_irq_save (&global_flags);
@@ -23,7 +23,7 @@ int irq_save_bad_arg(struct __sk_buff *ctx) } SEC("?tc") -__failure __msg("arg#0 doesn't point to an irq flag on stack") +__failure __msg("R1 doesn't point to an irq flag on stack") int irq_restore_bad_arg(struct __sk_buff *ctx) {bpf_local_irq_restore (&global_flags);
tools/testing/selftests/bpf/progs/iters.c +3 -3
@@ -1605,7 +1605,7 @@ int iter_subprog_check_stacksafe(const void *ctx)struct bpf_iter_num global_it; SEC("raw_tp") -__failure __msg("arg#0 expected pointer to an iterator on stack") +__failure __msg("R1 expected pointer to an iterator on stack") int iter_new_bad_arg(const void *ctx) {bpf_iter_num_new (&global_it,0 ,1 );
@@ -1613,7 +1613,7 @@ int iter_new_bad_arg(const void *ctx) } SEC("raw_tp") -__failure __msg("arg#0 expected pointer to an iterator on stack") +__failure __msg("R1 expected pointer to an iterator on stack") int iter_next_bad_arg(const void *ctx) {bpf_iter_num_next (&global_it);
@@ -1621,7 +1621,7 @@ int iter_next_bad_arg(const void *ctx) } SEC("raw_tp") -__failure __msg("arg#0 expected pointer to an iterator on stack") +__failure __msg("R1 expected pointer to an iterator on stack") int iter_destroy_bad_arg(const void *ctx) {bpf_iter_num_destroy (&global_it);
tools/testing/selftests/bpf/progs/iters_state_safety.c +7 -7
@@ -73,7 +73,7 @@ int create_and_forget_to_destroy_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int destroy_without_creating_fail(void *ctx) {/* init with zeros to stop verifier complaining about uninit stack */
@@ -91,7 +91,7 @@ int destroy_without_creating_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int compromise_iter_w_direct_write_fail(void *ctx) {struct bpf_iter_num iter;
@@ -143,7 +143,7 @@ int compromise_iter_w_direct_write_and_skip_destroy_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int compromise_iter_w_helper_write_fail(void *ctx) {struct bpf_iter_num iter;
@@ -230,7 +230,7 @@ int valid_stack_reuse(void *ctx) } SEC("?raw_tp") -__failure __msg("expected uninitialized iter_num as arg #0") +__failure __msg("expected uninitialized iter_num as R1") int double_create_fail(void *ctx) {struct bpf_iter_num iter;
@@ -258,7 +258,7 @@ int double_create_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int double_destroy_fail(void *ctx) {struct bpf_iter_num iter;
@@ -284,7 +284,7 @@ int double_destroy_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int next_without_new_fail(void *ctx) {struct bpf_iter_num iter;
@@ -305,7 +305,7 @@ int next_without_new_fail(void *ctx) } SEC("?raw_tp") -__failure __msg("expected an initialized iter_num as arg #0") +__failure __msg("expected an initialized iter_num as R1") int next_after_destroy_fail(void *ctx) {struct bpf_iter_num iter;
tools/testing/selftests/bpf/progs/iters_testmod.c +2 -2
@@ -29,7 +29,7 @@ int iter_next_trusted(const void *ctx) } SEC("raw_tp/sys_enter") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int iter_next_trusted_or_null(const void *ctx) {struct task_struct *cur_task =bpf_get_current_task_btf ();
@@ -67,7 +67,7 @@ int iter_next_rcu(const void *ctx) } SEC("raw_tp/sys_enter") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int iter_next_rcu_or_null(const void *ctx) {struct task_struct *cur_task =bpf_get_current_task_btf ();
tools/testing/selftests/bpf/progs/iters_testmod_seq.c +2 -2
@@ -79,7 +79,7 @@ int testmod_seq_truncated(const void *ctx) SEC("?raw_tp") __failure -__msg("expected an initialized iter_testmod_seq as arg #1") +__msg("expected an initialized iter_testmod_seq as R2")int testmod_seq_getter_before_bad (const void *ctx) {struct bpf_iter_testmod_seq it;
@@ -89,7 +89,7 @@ int testmod_seq_getter_before_bad(const void *ctx) SEC("?raw_tp") __failure -__msg("expected an initialized iter_testmod_seq as arg #1") +__msg("expected an initialized iter_testmod_seq as R2")int testmod_seq_getter_after_bad (const void *ctx) {struct bpf_iter_testmod_seq it;
tools/testing/selftests/bpf/progs/map_kptr_fail.c +1 -1
@@ -364,7 +364,7 @@ int kptr_xchg_ref_state(struct __sk_buff *ctx) } SEC("?tc") -__failure __msg("Possibly NULL pointer passed to helper arg2") +__failure __msg("Possibly NULL pointer passed to helper R2") int kptr_xchg_possibly_null(struct __sk_buff *ctx) {struct prog_test_ref_kfunc *p;
tools/testing/selftests/bpf/progs/percpu_alloc_fail.c +2 -2
@@ -110,7 +110,7 @@ int BPF_PROG(test_array_map_3) } SEC("?fentry.s/bpf_fentry_test1") -__failure __msg("arg#0 expected for bpf_percpu_obj_drop()") +__failure __msg("R1 expected for bpf_percpu_obj_drop()") int BPF_PROG(test_array_map_4) { struct val_t __percpu_kptr *p;
@@ -124,7 +124,7 @@ int BPF_PROG(test_array_map_4) } SEC("?fentry.s/bpf_fentry_test1") -__failure __msg("arg#0 expected for bpf_obj_drop()") +__failure __msg("R1 expected for bpf_obj_drop()") int BPF_PROG(test_array_map_5) {struct val_t *p;
tools/testing/selftests/bpf/progs/rbtree_fail.c +3 -3
@@ -134,7 +134,7 @@ long rbtree_api_remove_no_drop(void *ctx) } SEC("?tc") -__failure __msg("arg#1 expected pointer to allocated object") +__failure __msg("R2 expected pointer to allocated object") long rbtree_api_add_to_multiple_trees(void *ctx) {struct node_data *n;
@@ -153,7 +153,7 @@ long rbtree_api_add_to_multiple_trees(void *ctx) } SEC("?tc") -__failure __msg("Possibly NULL pointer passed to trusted arg1") +__failure __msg("Possibly NULL pointer passed to trusted R2") long rbtree_api_use_unchecked_remove_retval(void *ctx) {struct bpf_rb_node *res;
@@ -281,7 +281,7 @@ long add_with_cb(bool (cb)(struct bpf_rb_node *a, const struct bpf_rb_node *b)) } SEC("?tc") -__failure __msg("arg#1 expected pointer to allocated object") +__failure __msg("R2 expected pointer to allocated object") long rbtree_api_add_bad_cb_bad_fn_call_add(void *ctx) {return add_with_cb (less__bad_fn_call_add);
tools/testing/selftests/bpf/progs/refcounted_kptr_fail.c +1 -1
@@ -54,7 +54,7 @@ long rbtree_refcounted_node_ref_escapes(void *ctx) } SEC("?tc") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") long refcount_acquire_maybe_null(void *ctx) {struct node_acquire *n, *m;
tools/testing/selftests/bpf/progs/stream_fail.c +1 -1
@@ -23,7 +23,7 @@ int stream_vprintk_scalar_arg(void *ctx) } SEC("syscall") -__failure __msg("arg#1 doesn't point to a const string") +__failure __msg("R2 doesn't point to a const string") int stream_vprintk_string_arg(void *ctx) {bpf_stream_vprintk (BPF_STDOUT , ctx,NULL ,0 );
tools/testing/selftests/bpf/progs/task_kfunc_failure.c +9 -9
@@ -28,7 +28,7 @@ static struct __tasks_kfunc_map_value *insert_lookup_task(struct task_struct *ta } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_acquire_untrusted, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired;
@@ -49,7 +49,7 @@ int BPF_PROG(task_kfunc_acquire_untrusted, struct task_struct *task, u64 clone_f } SEC("tp_btf/task_newtask") -__failure __msg("arg#0 pointer type STRUCT task_struct must point") +__failure __msg("R1 pointer type STRUCT task_struct must point") 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;
@@ -100,7 +100,7 @@ int BPF_PROG(task_kfunc_acquire_unsafe_kretprobe_rcu, struct task_struct *task, } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_acquire_null, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired;
@@ -149,7 +149,7 @@ int BPF_PROG(task_kfunc_xchg_unreleased, struct task_struct *task, u64 clone_fla } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_acquire_release_no_null_check, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired;
@@ -162,7 +162,7 @@ int BPF_PROG(task_kfunc_acquire_release_no_null_check, struct task_struct *task, } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_release_untrusted, struct task_struct *task, u64 clone_flags) {struct __tasks_kfunc_map_value *v;
@@ -178,7 +178,7 @@ int BPF_PROG(task_kfunc_release_untrusted, struct task_struct *task, u64 clone_f } SEC("tp_btf/task_newtask") -__failure __msg("arg#0 pointer type STRUCT task_struct must point") +__failure __msg("R1 pointer type STRUCT task_struct must point") int BPF_PROG(task_kfunc_release_fp, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired = (struct task_struct *)&clone_flags;
@@ -190,7 +190,7 @@ int BPF_PROG(task_kfunc_release_fp, struct task_struct *task, u64 clone_flags) } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_release_null, struct task_struct *task, u64 clone_flags) {struct __tasks_kfunc_map_value local, *v;
@@ -234,7 +234,7 @@ int BPF_PROG(task_kfunc_release_unacquired, struct task_struct *task, u64 clone_ } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired;
@@ -248,7 +248,7 @@ int BPF_PROG(task_kfunc_from_pid_no_null_check, struct task_struct *task, u64 cl } SEC("tp_btf/task_newtask") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(task_kfunc_from_vpid_no_null_check, struct task_struct *task, u64 clone_flags) {struct task_struct *acquired;
tools/testing/selftests/bpf/progs/task_work_fail.c +3 -3
@@ -58,7 +58,7 @@ int mismatch_map(struct pt_regs *args) } SEC("perf_event") -__failure __msg("arg#1 doesn't point to a map value") +__failure __msg("R2 doesn't point to a map value") int no_map_task_work(struct pt_regs *args) {struct task_struct *task;
@@ -70,7 +70,7 @@ int no_map_task_work(struct pt_regs *args) } SEC("perf_event") -__failure __msg("Possibly NULL pointer passed to trusted arg1") +__failure __msg("Possibly NULL pointer passed to trusted R2") int task_work_null(struct pt_regs *args) {struct task_struct *task;
@@ -81,7 +81,7 @@ int task_work_null(struct pt_regs *args) } SEC("perf_event") -__failure __msg("Possibly NULL pointer passed to trusted arg2") +__failure __msg("Possibly NULL pointer passed to trusted R3") int map_null(struct pt_regs *args) {struct elem *work;
tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c +4 -4
@@ -152,7 +152,7 @@ int change_status_after_alloc(struct __sk_buff *ctx) } SEC("?tc") -__failure __msg("Possibly NULL pointer passed to trusted arg1") +__failure __msg("Possibly NULL pointer passed to trusted R2") int lookup_null_bpf_tuple(struct __sk_buff *ctx) {struct bpf_ct_opts___local opts = {};
@@ -165,7 +165,7 @@ int lookup_null_bpf_tuple(struct __sk_buff *ctx) } SEC("?tc") -__failure __msg("Possibly NULL pointer passed to trusted arg3") +__failure __msg("Possibly NULL pointer passed to trusted R4") int lookup_null_bpf_opts(struct __sk_buff *ctx) {struct bpf_sock_tuple tup = {};
@@ -178,7 +178,7 @@ int lookup_null_bpf_opts(struct __sk_buff *ctx) } SEC("?xdp") -__failure __msg("Possibly NULL pointer passed to trusted arg1") +__failure __msg("Possibly NULL pointer passed to trusted R2") int xdp_lookup_null_bpf_tuple(struct xdp_md *ctx) {struct bpf_ct_opts___local opts = {};
@@ -191,7 +191,7 @@ int xdp_lookup_null_bpf_tuple(struct xdp_md *ctx) } SEC("?xdp") -__failure __msg("Possibly NULL pointer passed to trusted arg3") +__failure __msg("Possibly NULL pointer passed to trusted R4") int xdp_lookup_null_bpf_opts(struct xdp_md *ctx) {struct bpf_sock_tuple tup = {};
tools/testing/selftests/bpf/progs/test_kfunc_dynptr_param.c +1 -1
@@ -40,7 +40,7 @@ int BPF_PROG(not_valid_dynptr, int cmd, union bpf_attr *attr, unsigned int size, } SEC("?lsm.s/bpf") -__failure __msg("arg#0 expected pointer to stack or const struct bpf_dynptr") +__failure __msg("R1 expected pointer to stack or const struct bpf_dynptr") int BPF_PROG(not_ptr_to_stack, int cmd, union bpf_attr *attr, unsigned int size, bool kernel) {static struct bpf_dynptr val;
tools/testing/selftests/bpf/progs/test_kfunc_param_nullable.c +1 -1
@@ -29,7 +29,7 @@ int kfunc_dynptr_nullable_test2(struct __sk_buff *skb) } SEC("tc") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int kfunc_dynptr_nullable_test3(struct __sk_buff *skb) {struct bpf_dynptr data;
tools/testing/selftests/bpf/progs/verifier_bits_iter.c +2 -2
@@ -32,7 +32,7 @@ int BPF_PROG(no_destroy, struct bpf_iter_meta *meta, struct cgroup *cgrp) SEC("iter/cgroup") __description("uninitialized iter in ->next()") -__failure __msg("expected an initialized iter_bits as arg #0") +__failure __msg("expected an initialized iter_bits as R1") int BPF_PROG(next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) {struct bpf_iter_bits it = {};
@@ -43,7 +43,7 @@ int BPF_PROG(next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) SEC("iter/cgroup") __description("uninitialized iter in ->destroy()") -__failure __msg("expected an initialized iter_bits as arg #0") +__failure __msg("expected an initialized iter_bits as R1") int BPF_PROG(destroy_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp) {struct bpf_iter_bits it = {};
tools/testing/selftests/bpf/progs/verifier_ref_tracking.c +3 -3
@@ -263,7 +263,7 @@ l0_%=: r0 = 0; \ SEC("lsm.s/bpf") __description("reference tracking: release user key reference without check") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") __naked void user_key_reference_without_check(void) { asm volatile (" \
@@ -282,7 +282,7 @@ __naked void user_key_reference_without_check(void) SEC("lsm.s/bpf") __description("reference tracking: release system key reference without check") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") __naked void system_key_reference_without_check(void) { asm volatile (" \
@@ -300,7 +300,7 @@ __naked void system_key_reference_without_check(void) SEC("lsm.s/bpf") __description("reference tracking: release with NULL key pointer") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") __naked void release_with_null_key_pointer(void) { asm volatile (" \
tools/testing/selftests/bpf/progs/verifier_vfs_reject.c +4 -4
@@ -13,7 +13,7 @@static char buf[PATH_MAX ]; SEC("lsm.s/file_open") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(get_task_exe_file_kfunc_null) {struct file *acquired;
@@ -28,7 +28,7 @@ int BPF_PROG(get_task_exe_file_kfunc_null) } SEC("lsm.s/inode_getxattr") -__failure __msg("arg#0 pointer type STRUCT task_struct must point to scalar, or struct with scalar") +__failure __msg("R1 pointer type STRUCT task_struct must point to scalar, or struct with scalar") int BPF_PROG(get_task_exe_file_kfunc_fp) {u64 x;
@@ -89,7 +89,7 @@ int BPF_PROG(put_file_kfunc_unacquired, struct file *file) } SEC("lsm.s/file_open") -__failure __msg("Possibly NULL pointer passed to trusted arg0") +__failure __msg("Possibly NULL pointer passed to trusted R1") int BPF_PROG(path_d_path_kfunc_null) {/* Can't pass NULL value to bpf_path_d_path() kfunc. */
@@ -128,7 +128,7 @@ int BPF_PROG(path_d_path_kfunc_untrusted_from_current) } SEC("lsm.s/file_open") -__failure __msg("kernel function bpf_path_d_path args#0 expected pointer to STRUCT path but R1 has a pointer to STRUCT file") +__failure __msg("kernel function bpf_path_d_path R1 expected pointer to STRUCT path but R1 has a pointer to STRUCT file") int BPF_PROG(path_d_path_kfunc_type_mismatch, struct file *file) {bpf_path_d_path ((struct path *)&file->f_task_work, buf,sizeof (buf));
tools/testing/selftests/bpf/progs/wq_failures.c +1 -1
@@ -98,7 +98,7 @@ __failure* is a correct bpf_wq pointer. */ __msg(": (85) call bpf_wq_set_callback#") /* anchor message */ -__msg("arg#0 doesn't point to a map value") +__msg("R1 doesn't point to a map value")long test_wrong_wq_pointer (void *ctx) {int key =0 ;
tools/testing/selftests/bpf/verifier/calls.c +7 -7
@@ -31,7 +31,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "arg#0 pointer type STRUCT prog_test_fail1 must point to scalar", + .errstr = "R1 pointer type STRUCT prog_test_fail1 must point to scalar", .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\narg#0 pointer type STRUCT prog_test_fail2", + .errstr = "max struct nesting depth exceeded\nR1 pointer type 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 = "arg#0 pointer type STRUCT prog_test_fail3 must point to scalar", + .errstr = "R1 pointer type STRUCT prog_test_fail3 must point to scalar", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_fail3", 2 }, },
@@ -76,7 +76,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "arg#0 expected pointer to ctx, but got fp", + .errstr = "R1 expected pointer to ctx, but got fp", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_pass_ctx", 2 }, },
@@ -91,7 +91,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "arg#0 pointer type UNKNOWN must point to scalar", + .errstr = "R1 pointer type UNKNOWN must point to scalar", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_mem_len_fail1", 2 }, },
@@ -109,7 +109,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "Possibly NULL pointer passed to trusted arg0", + .errstr = "Possibly NULL pointer passed to trusted R1", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_test_acquire", 3 }, { "bpf_kfunc_call_test_release", 5 },
@@ -152,7 +152,7 @@ }, .prog_type = BPF_PROG_TYPE_SCHED_CLS, .result = REJECT, - .errstr = "kernel function bpf_kfunc_call_memb1_release args#0 expected pointer", + .errstr = "kernel function bpf_kfunc_call_memb1_release R1 expected pointer", .fixup_kfunc_btf_id = { { "bpf_kfunc_call_memb_acquire", 1 }, { "bpf_kfunc_call_memb1_release", 5 },
Signature · 1 line
2.52.0
Re: [PATCH bpf-next 7/9] bpf: Prepare verifier logs for upcoming kfunc stack arguments
Quoted 81 lines · Yonghong Song
This change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in
R1toR5, but it does not work once kfunc arguments can also be passed on the stack.Introduce an internal
argnorepresentation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base.reg_arg_name()converts this representation into eitherR%dor*(R11-off)when emitting verifier logs. If a particularargnois corresponding to a stack argument, print*(R11-off). Otherwise, printR%d. Here R11 presents the base of stack arguments.This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)include/linux/bpf_verifier.h +1
-0@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;kernel/bpf/verifier.c +4
-0@@ -1742,6 +1742,44 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,return &elem->st; } +#define STACK_ARGNO_BASE 100 + +static bool is_stack_argno (int argno) +{
kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1758
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return argno >STACK_ARGNO_BASE ; +} + +/* arg starts at 1 */ +static u32 make_argno (u32 arg) +{ +if (arg <=MAX_BPF_FUNC_REG_ARGS ) +return arg; +return STACK_ARGNO_BASE + arg; +}
You can remove this and simplify everything further by
static bool is_stack_argno(int argno)
{
return argno > MAX_BPF_FUNC_REG_ARGS;
}kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1765
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+ +static u32 arg_from_argno (int argno) +{ +if (is_stack_argno (argno)) +return argno -STACK_ARGNO_BASE ; +return argno; +}
remove as well.
and a comment like:
/*
* switch (argno) {
* case 1: R1
* case 5: R5
* case 6: *(u64 *)(R11 +- 8)
* case 7: *(u64 *)(R11 +- 16)
*/
kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1778
Quoted 12 lines
kernel/bpf/verifier.c +12
-0+static const char *reg_arg_name (struct bpf_verifier_env *env,int argno) +{ +char *buf = env->tmp_arg_name; +int len =sizeof (env->tmp_arg_name); +u32 arg; + +if (!is_stack_argno (argno)) { +snprintf (buf, len,"R%d" , argno); +return buf; + } + + arg =arg_from_argno (argno);
gone
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+snprintf (buf, len,"*(R11-%u)" , (arg -MAX_BPF_FUNC_REG_ARGS ) *BPF_REG_SIZE ); +return buf; +}
Yonghong Song
reviewerRe: [PATCH bpf-next 7/9] bpf: Prepare verifier logs for upcoming kfunc stack arguments
Quoted 110 lines · Alexei Starovoitov
Quoted 81 lines · Yonghong Song
This change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in
R1toR5, but it does not work once kfunc arguments can also be passed on the stack.Introduce an internal
argnorepresentation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base.reg_arg_name()converts this representation into eitherR%dor*(R11-off)when emitting verifier logs. If a particularargnois corresponding to a stack argument, print*(R11-off). Otherwise, printR%d. Here R11 presents the base of stack arguments.This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)include/linux/bpf_verifier.h +1
-0@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;kernel/bpf/verifier.c +4
-0@@ -1742,6 +1742,44 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,return &elem->st; } +#define STACK_ARGNO_BASE 100 + +static bool is_stack_argno (int argno) +{kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1758
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return argno >STACK_ARGNO_BASE ; +} + +/* arg starts at 1 */ +static u32 make_argno (u32 arg) +{ +if (arg <=MAX_BPF_FUNC_REG_ARGS ) +return arg; +return STACK_ARGNO_BASE + arg; +}You can remove this and simplify everything further by
static bool
is_stack_argno(int argno){ return argno > MAX_BPF_FUNC_REG_ARGS; }kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1765
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+ +static u32 arg_from_argno (int argno) +{ +if (is_stack_argno (argno)) +return argno -STACK_ARGNO_BASE ; +return argno; +}remove as well.
and a comment like:
Quoted 7 lines
/*
* switch (argno) {
* case 1: R1
* case 5: R5
* case 6: *(u64 *)(R11 +- 8)
* case 7: *(u64 *)(R11 +- 16)
*/
This doesn't work. Let us see the following example:
check_kfunc_args
process_dynptr_func (argno)
check_mem_access (argno, 4th argument)
check_packet_access (argno)
check_mem_region_access (argno)
__check_mem_access (argno)
<== verbose log with argno
do_check
do_check_insn (env)
check_load_mem (insn)
check_mem_access (insn->src_reg, 4th argument)
check_packet_access (...)
check_mem_region_access (...)
__check_mem_access (insn->src_reg or argno)In the above case, function __check_mem_access() intends to issue an verbose log with the 'argno' argument. The possible values are
- R0 to R11 with do_check() call stack
- R1 to R5 or stack arguments
In such cases, print will be
R0 to R11 if argno <= 100 (STACK_ARGNO_BASE)
*(R11-%u) if argno > 100
Does this make sense?
Quoted 17 lines
kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1778
Quoted 12 lines
kernel/bpf/verifier.c +12
-0+static const char *reg_arg_name (struct bpf_verifier_env *env,int argno) +{ +char *buf = env->tmp_arg_name; +int len =sizeof (env->tmp_arg_name); +u32 arg; + +if (!is_stack_argno (argno)) { +snprintf (buf, len,"R%d" , argno); +return buf; + } + + arg =arg_from_argno (argno);gone
Quoted 3 lines
kernel/bpf/verifier.c +3
-0+snprintf (buf, len,"*(R11-%u)" , (arg -MAX_BPF_FUNC_REG_ARGS ) *BPF_REG_SIZE ); +return buf; +}
Re: [PATCH bpf-next 7/9] bpf: Prepare verifier logs for upcoming kfunc stack arguments
Quoted 131 lines · Yonghong Song
Quoted 110 lines · Alexei Starovoitov
Quoted 81 lines · Yonghong Song
This change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in
R1toR5, but it does not work once kfunc arguments can also be passed on the stack.Introduce an internal
argnorepresentation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base.reg_arg_name()converts this representation into eitherR%dor*(R11-off)when emitting verifier logs. If a particularargnois corresponding to a stack argument, print*(R11-off). Otherwise, printR%d. Here R11 presents the base of stack arguments.This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)include/linux/bpf_verifier.h +1
-0@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;kernel/bpf/verifier.c +4
-0@@ -1742,6 +1742,44 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,return &elem->st; } +#define STACK_ARGNO_BASE 100 + +static bool is_stack_argno (int argno) +{kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1758
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return argno >STACK_ARGNO_BASE ; +} + +/* arg starts at 1 */ +static u32 make_argno (u32 arg) +{ +if (arg <=MAX_BPF_FUNC_REG_ARGS ) +return arg; +return STACK_ARGNO_BASE + arg; +}You can remove this and simplify everything further by
static bool
is_stack_argno(int argno){ return argno > MAX_BPF_FUNC_REG_ARGS; }kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1765
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+ +static u32 arg_from_argno (int argno) +{ +if (is_stack_argno (argno)) +return argno -STACK_ARGNO_BASE ; +return argno; +}remove as well.
and a comment like:
Quoted 7 lines
/*
* switch (argno) {
* case 1: R1
* case 5: R5
* case 6: *(u64 *)(R11 +- 8)
* case 7: *(u64 *)(R11 +- 16)
*/This doesn't work. Let us see the following example:
check_kfunc_args
process_dynptr_func(argno)
check_mem_access(argno, 4th argument)
check_packet_access(argno)
check_mem_region_access(argno)
__check_mem_access(argno)
<== verbose log with argno
Quoted 7 lines
do_check
do_check_insn(env)check_load_mem (insn) check_mem_access (insn->src_reg, 4th argument) check_packet_access (...) check_mem_region_access (...) __check_mem_access (insn->src_reg or argno)
Ohh. Silent conversion. That's quite error prone.
let's do
typedef struct argno {
int argno;
} argno_t;and make sure this callchain passes arg_t unmodified:
process_dynptr_func (argno)
check_mem_access (argno, 4th argument)
check_packet_access (argno) ...
while here:
check_load_mem (insn)
check_mem_access (argno_from_reg(insn->src_reg), 4th argument)static argno_t argno_from_reg(u32 regno)
{
return (argno_t){ .argno = regno };
}static argno_t argno_from_arg(u32 arg)
{
return (argno_t){ .argno = -arg };
}static const char *reg_arg_name(struct bpf_verifier_env *env, argno_t argno)
When positive vs negative is an internal implemenation of argno_t it's fine. It's better than shift by 100, but when negative was used as a signal everywhere it leaked details to caller.
Yonghong Song
reviewerRe: [PATCH bpf-next 7/9] bpf: Prepare verifier logs for upcoming kfunc stack arguments
Quoted 165 lines · Alexei Starovoitov
Quoted 129 lines · Yonghong Song
Quoted 110 lines · Alexei Starovoitov
Quoted 81 lines · Yonghong Song
This change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in
R1toR5, but it does not work once kfunc arguments can also be passed on the stack.Introduce an internal
argnorepresentation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base.reg_arg_name()converts this representation into eitherR%dor*(R11-off)when emitting verifier logs. If a particularargnois corresponding to a stack argument, print*(R11-off). Otherwise, printR%d. Here R11 presents the base of stack arguments.This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)include/linux/bpf_verifier.h +1
-0@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;kernel/bpf/verifier.c +4
-0@@ -1742,6 +1742,44 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,return &elem->st; } +#define STACK_ARGNO_BASE 100 + +static bool is_stack_argno (int argno) +{kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1758
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return argno >STACK_ARGNO_BASE ; +} + +/* arg starts at 1 */ +static u32 make_argno (u32 arg) +{ +if (arg <=MAX_BPF_FUNC_REG_ARGS ) +return arg; +return STACK_ARGNO_BASE + arg; +}You can remove this and simplify everything further by
static bool
is_stack_argno(int argno){ return argno > MAX_BPF_FUNC_REG_ARGS; }kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1765
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+ +static u32 arg_from_argno (int argno) +{ +if (is_stack_argno (argno)) +return argno -STACK_ARGNO_BASE ; +return argno; +}remove as well.
and a comment like:
Quoted 7 lines
/*
* switch (argno) {
* case 1: R1
* case 5: R5
* case 6: *(u64 *)(R11 +- 8)
* case 7: *(u64 *)(R11 +- 16)
*/This doesn't work. Let us see the following example:
check_kfunc_args
process_dynptr_func(argno)
check_mem_access(argno, 4th argument)
check_packet_access(argno)
check_mem_region_access(argno)
__check_mem_access(argno)
<== verbose log with argnoQuoted 7 lines
do_checkdo_check_insn (env) check_load_mem (insn) check_mem_access (insn->src_reg, 4th argument) check_packet_access (...) check_mem_region_access (...) __check_mem_access (insn->src_reg or argno)Ohh. Silent conversion. That's quite error prone.
let's do
typedef struct argno { int argno; } argno_t;and make sure this callchain passes
arg_tunmodified:
process_dynptr_func(argno)
check_mem_access(argno, 4th argument)
check_packet_access(argno) ...while here:
check_load_mem (insn) check_mem_access (argno_from_reg(insn->src_reg), 4th argument)static
argno_targno_from_reg(u32 regno){ return (argno_t){ .argno = regno }; }static
argno_targno_from_arg(u32 arg){ return (argno_t){ .argno = -arg }; }
Quoted 5 lines
static const char *reg_arg_name(struct
bpf_verifier_env*env,argno_targno)When positive vs negative is an internal implemenation of
argno_tit's fine. It's better than shift by 100, but when negative was used as a signal everywhere it leaked details to caller.
This approach is kind of similar to what I proposed earlier with an int variable, non-negative for reg and negative for arg (value -1/-2/...).
https://lore.kernel.org/bpf/[email protected]/
But it is not explicit except the name 'reg_or_arg'. Here, argno_t type makes it more explicit and should better.
For printing, I guess we still want to print 'R#' whenever possible including positive registers and negative argno (1-5), and print '*(R11-off)' for negative argno (6->...)?
Re: [PATCH bpf-next 7/9] bpf: Prepare verifier logs for upcoming kfunc stack arguments
Quoted 170 lines · Yonghong Song
Quoted 167 lines · Alexei Starovoitov
Quoted 129 lines · Yonghong Song
Quoted 110 lines · Alexei Starovoitov
Quoted 81 lines · Yonghong Song
This change prepares verifier log reporting for upcoming kfunc stack argument support.
Today verifier log code mostly assumes that an argument can be described directly by a register number. That works for arguments passed in
R1toR5, but it does not work once kfunc arguments can also be passed on the stack.Introduce an internal
argnorepresentation such that register-passed arguments keep using their real register numbers, while stack-passed arguments use an encoded value above a dedicated base.reg_arg_name()converts this representation into eitherR%dor*(R11-off)when emitting verifier logs. If a particularargnois corresponding to a stack argument, print*(R11-off). Otherwise, printR%d. Here R11 presents the base of stack arguments.This keeps existing logs readable for register arguments and allows the same log sites to handle future stack arguments without open-coding special cases.
Update selftests accordingly.
Acked-by Puranjay Mohan <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- include/linux/bpf_verifier.h | 1 + kernel/bpf/verifier.c | 640 ++++++++++-------- .../testing/selftests/bpf/prog_tests/bpf_nf.c | 22 +- .../selftests/bpf/prog_tests/cb_refs.c | 2 +- .../selftests/bpf/prog_tests/kfunc_call.c | 2 +- .../selftests/bpf/prog_tests/linked_list.c | 4 +- .../selftests/bpf/progs/cgrp_kfunc_failure.c | 14 +- .../selftests/bpf/progs/cpumask_failure.c | 10 +- .../testing/selftests/bpf/progs/dynptr_fail.c | 22 +- .../selftests/bpf/progs/file_reader_fail.c | 4 +- tools/testing/selftests/bpf/progs/irq.c | 4 +- tools/testing/selftests/bpf/progs/iters.c | 6 +- .../selftests/bpf/progs/iters_state_safety.c | 14 +- .../selftests/bpf/progs/iters_testmod.c | 4 +- .../selftests/bpf/progs/iters_testmod_seq.c | 4 +- .../selftests/bpf/progs/map_kptr_fail.c | 2 +- .../selftests/bpf/progs/percpu_alloc_fail.c | 4 +- .../testing/selftests/bpf/progs/rbtree_fail.c | 6 +- .../bpf/progs/refcounted_kptr_fail.c | 2 +- .../testing/selftests/bpf/progs/stream_fail.c | 2 +- .../selftests/bpf/progs/task_kfunc_failure.c | 18 +- .../selftests/bpf/progs/task_work_fail.c | 6 +- .../selftests/bpf/progs/test_bpf_nf_fail.c | 8 +- .../bpf/progs/test_kfunc_dynptr_param.c | 2 +- .../bpf/progs/test_kfunc_param_nullable.c | 2 +- .../selftests/bpf/progs/verifier_bits_iter.c | 4 +- .../bpf/progs/verifier_ref_tracking.c | 6 +- .../selftests/bpf/progs/verifier_vfs_reject.c | 8 +- .../testing/selftests/bpf/progs/wq_failures.c | 2 +- tools/testing/selftests/bpf/verifier/calls.c | 14 +- 30 files changed, 464 insertions(+), 375 deletions(-)include/linux/bpf_verifier.h +1
-0@@ -913,6 +913,7 @@ struct bpf_verifier_env {* e.g., in reg_type_str() to generate reg_type string */ char tmp_str_buf[TMP_STR_BUF_LEN ]; +char tmp_arg_name[32 ];struct bpf_insn insn_buf[INSN_BUF_SIZE ];struct bpf_insn epilogue_buf[INSN_BUF_SIZE ];struct bpf_scc_callchain callchain_buf;kernel/bpf/verifier.c +4
-0@@ -1742,6 +1742,44 @@ static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env,return &elem->st; } +#define STACK_ARGNO_BASE 100 + +static bool is_stack_argno (int argno) +{kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1758
Quoted 10 lines
kernel/bpf/verifier.c +10
-0+return argno >STACK_ARGNO_BASE ; +} + +/* arg starts at 1 */ +static u32 make_argno (u32 arg) +{ +if (arg <=MAX_BPF_FUNC_REG_ARGS ) +return arg; +return STACK_ARGNO_BASE + arg; +}You can remove this and simplify everything further by
static bool
is_stack_argno(int argno){ return argno > MAX_BPF_FUNC_REG_ARGS; }kernel/bpf/verifier.c static struct bpf_verifier_state *push_stack(struct bpf_verifier_env *env, · line 1765
Quoted 7 lines
kernel/bpf/verifier.c +7
-0+ +static u32 arg_from_argno (int argno) +{ +if (is_stack_argno (argno)) +return argno -STACK_ARGNO_BASE ; +return argno; +}remove as well.
and a comment like:
Quoted 7 lines
/*
* switch (argno) {
* case 1: R1
* case 5: R5
* case 6: *(u64 *)(R11 +- 8)
* case 7: *(u64 *)(R11 +- 16)
*/This doesn't work. Let us see the following example:
check_kfunc_args
process_dynptr_func(argno)
check_mem_access(argno, 4th argument)
check_packet_access(argno)
check_mem_region_access(argno)
__check_mem_access(argno)
<== verbose log with argnoQuoted 7 lines
do_checkdo_check_insn (env) check_load_mem (insn) check_mem_access (insn->src_reg, 4th argument) check_packet_access (...) check_mem_region_access (...) __check_mem_access (insn->src_reg or argno)Ohh. Silent conversion. That's quite error prone.
let's do
typedef struct argno { int argno; } argno_t;and make sure this callchain passes
arg_tunmodified:
process_dynptr_func(argno)
check_mem_access(argno, 4th argument)
check_packet_access(argno) ...while here:
check_load_mem (insn) check_mem_access (argno_from_reg(insn->src_reg), 4th argument)static
argno_targno_from_reg(u32 regno){ return (argno_t){ .argno = regno }; }static
argno_targno_from_arg(u32 arg){ return (argno_t){ .argno = -arg }; }static const char *reg_arg_name(struct
bpf_verifier_env*env,argno_targno)
Quoted 7 lines
Quoted 3 lines
When positive vs negative is an internal implemenation of
argno_tit's fine. It's better than shift by 100, but when negative was used as a signal everywhere it leaked details to caller.This approach is kind of similar to what I proposed earlier with an int variable, non-negative for reg and negative for arg (value -1/-2/...).
https://lore.kernel.org/bpf/[email protected]/
Of course. It's your proposal. I just relayed it back. My objection for inband signaling was that it leaks into the caller and you "fixed" with make_argno() plus "shift-by-100" while I was objecting to "leaks to caller" part.
In band signaling almost always sucks, but when it's there to save extra word of being passed around by value, it's worth doing. But only if it's hidden behind api.
Quoted 2 lines
But it is not explicit except the name '
reg_or_arg'. Here,argno_ttype makes it more explicit and should better.
"not explicit" is the key.
When it's abstracted it can change into "shift-by-100" or extra bit without affecting callers or callees.
Quoted 3 lines
For printing, I guess we still want to print 'R#' whenever possible including positive registers and negative argno (1-5), and print '*(R11-off)' for negative argno (6->...)?
Yes, because that's what verifier log just before the error message has. 'arg#' is disconnected from verifier output that immediately preceding that error message. It's bad for humans and especially bad for agents that cannot connect the dots. r1=..; arg#0 is invalid because ...;
How agent suppose to infer that arg#0 is the same as r1 ? It can if it thinks "effort xhigh", but it shouldn't need to do that.
Yonghong Song
reviewerPATCH 8/9 bpf: Introduce bpf register BPF_REG_PARAMS
In reply to Yonghong SongIntroduce BPF_REG_PARAMS as a dedicated BPF register for stack argument accesses. It occupies the BPF register number 11 (R11), which is used as the base pointer for the stack argument area, keeping it separate from the R10-based (BPF_REG_FP) program stack.
The kernel-internal hidden register BPF_REG_AX previously occupied slot 11 (MAX_BPF_REG). With BPF_REG_PARAMS taking that slot, BPF_REG_AX moves to slot 12 and MAX_BPF_EXT_REG increases accordingly.
--- include/linux/filter.h | 5 +- kernel/bpf/core.c | 4 +- .../selftests/bpf/prog_tests/ctx_rewrite.c | 14 ++-- .../bpf/progs/verifier_bpf_fastcall.c | 24 +++---- .../selftests/bpf/progs/verifier_may_goto_1.c | 12 ++-- .../selftests/bpf/progs/verifier_sdiv.c | 64 +++++++++---------- 6 files changed, 62 insertions(+), 61 deletions(-)
include/linux/filter.h +3 -2
@@ -58,8 +58,9 @@ struct ctl_table_header;#define BPF_REG_H BPF_REG_9/* hlen, callee-saved */ /* Kernel hidden auxiliary/helper register. */ -#define BPF_REG_AX MAX_BPF_REG -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) +#define BPF_REG_PARAMS MAX_BPF_REG +#define BPF_REG_AX (MAX_BPF_REG + 1) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2)#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG/* unused opcode to mark special call to bpf_tail_call() helper */
kernel/bpf/core.c +2 -2
@@ -1299,8 +1299,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,u32 imm_rnd =get_random_u32 ();s16 off; -BUILD_BUG_ON (BPF_REG_AX +1 !=MAX_BPF_JIT_REG ); -BUILD_BUG_ON (MAX_BPF_REG +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_PARAMS +2 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_AX +1 !=MAX_BPF_JIT_REG );/* Constraints on AX register: *
tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c +7 -7
@@ -69,19 +69,19 @@ static struct test_case test_cases[] = { #if defined(__x86_64__) || defined(__aarch64__) { N(SCHED_CLS, struct __sk_buff, tstamp), - .read = "r11 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" - "if w11 & 0x4 goto pc+1;" + .read = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" + "if w12 & 0x4 goto pc+1;" "goto pc+4;" - "if w11 & 0x3 goto pc+1;" + "if w12 & 0x3 goto pc+1;" "goto pc+2;" "$dst = 0;" "goto pc+1;" "$dst = *(u64 *)($ctx + sk_buff::tstamp);", - .write = "r11 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" - "if w11 & 0x4 goto pc+1;" + .write = "r12 = *(u8 *)($ctx + sk_buff::__mono_tc_offset);" + "if w12 & 0x4 goto pc+1;" "goto pc+2;" - "w11 &= -4;" - "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r11;" + "w12 &= -4;" + "*(u8 *)($ctx + sk_buff::__mono_tc_offset) = r12;" "*(u64 *)($ctx + sk_buff::tstamp) = $src;", }, #endif
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +12 -12
@@ -630,13 +630,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)")__xlated ("..." ) /* may_goto expansion starts */ -__xlated("6: r11 = *(u64 *)(r10 -24)") -__xlated("7: if r11 == 0x0 goto pc+6") -__xlated("8: r11 -= 1") -__xlated("9: if r11 != 0x0 goto pc+2") -__xlated("10: r11 = -24") +__xlated("6: r12 = *(u64 *)(r10 -24)") +__xlated("7: if r12 == 0x0 goto pc+6") +__xlated("8: r12 -= 1") +__xlated("9: if r12 != 0x0 goto pc+2") +__xlated("10: r12 = -24") __xlated("11: call unknown") -__xlated ("12: *(u64 *)(r10 -24) = r11" ) +__xlated ("12: *(u64 *)(r10 -24) = r12" ) /* may_goto expansion ends */ __xlated("13: *(u64 *)(r10 -8) = r1")__xlated ("14: exit" )
@@ -668,13 +668,13 @@ __xlated("1: *(u64 *)(r10 -16) =") __xlated("2: r1 = 1")__xlated ("3: call bpf_get_smp_processor_id" ) /* may_goto expansion starts */ -__xlated("4: r11 = *(u64 *)(r10 -24)") -__xlated("5: if r11 == 0x0 goto pc+6") -__xlated("6: r11 -= 1") -__xlated("7: if r11 != 0x0 goto pc+2") -__xlated("8: r11 = -24") +__xlated("4: r12 = *(u64 *)(r10 -24)") +__xlated("5: if r12 == 0x0 goto pc+6") +__xlated("6: r12 -= 1") +__xlated("7: if r12 != 0x0 goto pc+2") +__xlated("8: r12 = -24") __xlated("9: call unknown") -__xlated ("10: *(u64 *)(r10 -24) = r11" ) +__xlated ("10: *(u64 *)(r10 -24) = r12" ) /* may_goto expansion ends */ __xlated("11: *(u64 *)(r10 -8) = r1")__xlated ("12: exit" )
tools/testing/selftests/bpf/progs/verifier_may_goto_1.c +6 -6
@@ -81,13 +81,13 @@ __arch_s390x __arch_arm64 __xlated("0: *(u64 *)(r10 -16) = 65535") __xlated("1: *(u64 *)(r10 -8) = 0") -__xlated("2: r11 = *(u64 *)(r10 -16)") -__xlated("3: if r11 == 0x0 goto pc+6") -__xlated("4: r11 -= 1") -__xlated("5: if r11 != 0x0 goto pc+2") -__xlated("6: r11 = -16") +__xlated("2: r12 = *(u64 *)(r10 -16)") +__xlated("3: if r12 == 0x0 goto pc+6") +__xlated("4: r12 -= 1") +__xlated("5: if r12 != 0x0 goto pc+2") +__xlated("6: r12 = -16") __xlated("7: call unknown") -__xlated("8: *(u64 *)(r10 -16) = r11") +__xlated("8: *(u64 *)(r10 -16) = r12") __xlated("9: r0 = 1") __xlated("10: r0 = 2")__xlated ("11: exit" )
tools/testing/selftests/bpf/progs/verifier_sdiv.c +32 -32
112 lines not on this page · raw message
Signature · 1 line
2.52.0
Re: [PATCH bpf-next 8/9] bpf: Introduce bpf register BPF_REG_PARAMS
Quoted 7 lines · Yonghong Song
/* Kernel hidden auxiliary/helper register. */
include/linux/filter.h +3
-2-#define BPF_REG_AX MAX_BPF_REG -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) +#define BPF_REG_PARAMS MAX_BPF_REG +#define BPF_REG_AX (MAX_BPF_REG + 1) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2)
...
Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +0
-0@@ -630,13 +630,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)")__xlated ("..." )/* may_goto expansion starts */
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c __xlated("...") · line 637
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +5
-5-__xlated("6: r11 = *(u64 *)(r10 -24)") -__xlated("7: if r11 == 0x0 goto pc+6") -__xlated("8: r11 -= 1") -__xlated("9: if r11 != 0x0 goto pc+2") -__xlated ("10: r11 = -24" ) +__xlated("6: r12 = *(u64 *)(r10 -24)") +__xlated("7: if r12 == 0x0 goto pc+6") +__xlated("8: r12 -= 1") +__xlated("9: if r12 != 0x0 goto pc+2") +__xlated ("10: r12 = -24" )
maybe shift it to r15 right away, so we don't need to touch this code if/when true r12 is introduced?
Yonghong Song
reviewerRe: [PATCH bpf-next 8/9] bpf: Introduce bpf register BPF_REG_PARAMS
Quoted 20 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song
/* Kernel hidden auxiliary/helper register. */
(quoted diff) +3
-2-#define BPF_REG_AX MAX_BPF_REG -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) +#define BPF_REG_PARAMS MAX_BPF_REG +#define BPF_REG_AX (MAX_BPF_REG + 1) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2)...
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +0
-4@@ -630,13 +630,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)")__xlated ("..." )/* may_goto expansion starts */ -__xlated("6: r11 = *(u64 *)(r10 -24)") -__xlated("7: if r11 == 0x0 goto pc+6") -__xlated("8: r11 -= 1") -__xlated ("9: if r11 != 0x0 goto pc+2" )
Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c __xlated("...") · line 637
Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +5
-1-__xlated ("10: r11 = -24" ) +__xlated("6: r12 = *(u64 *)(r10 -24)") +__xlated("7: if r12 == 0x0 goto pc+6") +__xlated("8: r12 -= 1") +__xlated("9: if r12 != 0x0 goto pc+2") +__xlated ("10: r12 = -24" )maybe shift it to r15 right away, so we don't need to touch this code if/when true r12 is introduced?
We can do this. Do you think the following is okay:
include/linux/filter.h +2 -2
@@ -59,9 +59,9 @@ struct ctl_table_header;/* Kernel hidden auxiliary/helper register. */ #define BPF_REG_PARAMS MAX_BPF_REG -#define BPF_REG_AX (MAX_BPF_REG + 1) -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG +#define BPF_REG_AX (MAX_BPF_REG + 4)/* unused opcode to mark special call to bpf_tail_call() helper */ #define BPF_TAIL_CALL 0xf0
kernel/bpf/core.c +2 -2
@@ -1299,8 +1299,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,u32 imm_rnd =get_random_u32 ();s16 off; -BUILD_BUG_ON (BPF_REG_PARAMS +2 !=MAX_BPF_JIT_REG ); -BUILD_BUG_ON (BPF_REG_AX +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_PARAMS +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_AX !=MAX_BPF_JIT_REG +3 );/* Constraints on AX register: *
Re: [PATCH bpf-next 8/9] bpf: Introduce bpf register BPF_REG_PARAMS
Quoted 55 lines · Yonghong Song
Quoted 20 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song
/* Kernel hidden auxiliary/helper register. */
(quoted diff) +3
-2-#define BPF_REG_AX MAX_BPF_REG -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) +#define BPF_REG_PARAMS MAX_BPF_REG +#define BPF_REG_AX (MAX_BPF_REG + 1) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2)...
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +0
-4@@ -630,13 +630,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)")__xlated ("..." )/* may_goto expansion starts */ -__xlated("6: r11 = *(u64 *)(r10 -24)") -__xlated("7: if r11 == 0x0 goto pc+6") -__xlated("8: r11 -= 1") -__xlated ("9: if r11 != 0x0 goto pc+2" )Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c __xlated("...") · line 637
Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +5
-1-__xlated ("10: r11 = -24" ) +__xlated("6: r12 = *(u64 *)(r10 -24)") +__xlated("7: if r12 == 0x0 goto pc+6") +__xlated("8: r12 -= 1") +__xlated("9: if r12 != 0x0 goto pc+2") +__xlated ("10: r12 = -24" )maybe shift it to r15 right away, so we don't need to touch this code if/when true r12 is introduced?
We can do this. Do you think the following is okay:
include/linux/filter.h +2
-2@@ -59,9 +59,9 @@ struct ctl_table_header;/* Kernel hidden auxiliary/helper register. */ #define BPF_REG_PARAMS MAX_BPF_REG -#define BPF_REG_AX (MAX_BPF_REG + 1) -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG +#define BPF_REG_AX (MAX_BPF_REG + 4)/* unused opcode to mark special call to bpf_tail_call() helper */ #define BPF_TAIL_CALL 0xf0kernel/bpf/core.c +0
-0
kernel/bpf/core.c static int bpf_jit_blind_insn(const struct bpf_insn *from, · line 1303
Quoted 7 lines
kernel/bpf/core.c +2
-2@@ -1299,8 +1299,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,u32 imm_rnd =get_random_u32 ();s16 off; -BUILD_BUG_ON (BPF_REG_PARAMS +2 !=MAX_BPF_JIT_REG ); -BUILD_BUG_ON (BPF_REG_AX +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_PARAMS +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_AX !=MAX_BPF_JIT_REG +3 );
Ohh. We have:
static unsigned int PROG_NAME(stack_size)(const void *ctx, const struct bpf_insn *insn) \
{ \
u64 stack[stack_size / sizeof(u64)]; \
u64 regs[MAX_BPF_EXT_REG] = {}; \Please double check that BPF_REG_AX is not used by the interpreter.
but it's starting to feel that my suggestion was premature. Probably better to keep this patchset as-is.
Yonghong Song
reviewerRe: [PATCH bpf-next 8/9] bpf: Introduce bpf register BPF_REG_PARAMS
Quoted 68 lines · Alexei Starovoitov
Quoted 53 lines · Yonghong Song
Quoted 20 lines · Alexei Starovoitov
Quoted 7 lines · Yonghong Song
/* Kernel hidden auxiliary/helper register. */(quoted diff) +3
-2-#define BPF_REG_AX MAX_BPF_REG -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1) +#define BPF_REG_PARAMS MAX_BPF_REG +#define BPF_REG_AX (MAX_BPF_REG + 1) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2)...
Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +0
-4@@ -630,13 +630,13 @@ __xlated("...") __xlated("4: r0 = &(void __percpu *)(r0)")__xlated ("..." )/* may_goto expansion starts */ -__xlated("6: r11 = *(u64 *)(r10 -24)") -__xlated("7: if r11 == 0x0 goto pc+6") -__xlated("8: r11 -= 1") -__xlated ("9: if r11 != 0x0 goto pc+2" )Quoted 8 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c __xlated("...") · line 637
Quoted 6 lines
tools/testing/selftests/bpf/progs/verifier_bpf_fastcall.c +5
-1-__xlated ("10: r11 = -24" ) +__xlated("6: r12 = *(u64 *)(r10 -24)") +__xlated("7: if r12 == 0x0 goto pc+6") +__xlated("8: r12 -= 1") +__xlated("9: if r12 != 0x0 goto pc+2") +__xlated ("10: r12 = -24" )maybe shift it to r15 right away, so we don't need to touch this code if/when true r12 is introduced?
We can do this. Do you think the following is okay:
include/linux/filter.h +2
-2@@ -59,9 +59,9 @@ struct ctl_table_header;/* Kernel hidden auxiliary/helper register. */ #define BPF_REG_PARAMS MAX_BPF_REG -#define BPF_REG_AX (MAX_BPF_REG + 1) -#define MAX_BPF_EXT_REG (MAX_BPF_REG + 2) +#define MAX_BPF_EXT_REG (MAX_BPF_REG + 1)#define MAX_BPF_JIT_REG MAX_BPF_EXT_REG +#define BPF_REG_AX (MAX_BPF_REG + 4)/* unused opcode to mark special call to bpf_tail_call() helper */ #define BPF_TAIL_CALL 0xf0kernel/bpf/core.c +0
-0kernel/bpf/core.c static int bpf_jit_blind_insn(const struct bpf_insn *from, · line 1303
Quoted 7 lines
kernel/bpf/core.c +2
-2@@ -1299,8 +1299,8 @@ static int bpf_jit_blind_insn(const struct bpf_insn *from,u32 imm_rnd =get_random_u32 ();s16 off; -BUILD_BUG_ON (BPF_REG_PARAMS +2 !=MAX_BPF_JIT_REG ); -BUILD_BUG_ON (BPF_REG_AX +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_PARAMS +1 !=MAX_BPF_JIT_REG ); +BUILD_BUG_ON (BPF_REG_AX !=MAX_BPF_JIT_REG +3 );Ohh. We have:
static unsigned int PROG_NAME(stack_size)(const void *ctx, const structbpf_insn*insn) \{ \ u64 stack[stack_size / sizeof(u64)]; \ u64 regs[MAX_BPF_EXT_REG] = {}; \
Quoted 4 lines
Please double check that
BPF_REG_AXis not used by the interpreter.but it's starting to feel that my suggestion was premature. Probably better to keep this patchset as-is.
Okay, let me keep the current one.
Yonghong Song
reviewerPATCH 9/9 bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments
In reply to Yonghong SongCurrently, MAX_BPF_FUNC_ARGS is used for tracepoint related progs where the number of parameters cannot exceed MAX_BPF_FUNC_ARGS.
Here, MAX_BPF_FUNC_ARGS is reused to set a limit of the number of arguments for bpf functions and kfunc's. The current value for MAX_BPF_FUNC_ARGS is 12 which should be sufficient for majority of bpf functions and kfunc's.
--- include/linux/bpf.h | 5 +++++ 1 file changed, 5 insertions(+)
include/linux/bpf.h +5 -0
@@ -1151,6 +1151,11 @@ struct bpf_prog_offload {/* 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
Signature · 1 line
2.52.0
[email protected]
AI reviewRe: [PATCH bpf-next 9/9] bpf: Reuse MAX_BPF_FUNC_ARGS for maximum number of arguments
This isn't a bug, but the multi-line comment opening doesn't follow BPF subsystem style. Per bpf.md, multi-line comments must have the opening /* on its own line. The rule applies even if…
include/linux/bpf.h struct bpf_prog_offload { · line 1153
Quoted 8 lines
include/linux/bpf.h +1
-0@@ -1151,6 +1151,11 @@ struct bpf_prog_offload { +/* The longest tracepoint has 12 args. * See include/trace/bpf_probe.h
This isn't a bug, but the multi-line comment opening doesn't follow BPF subsystem style. Per bpf.md, multi-line comments must have the opening /* on its own line. The rule applies even if surrounding code uses the old style.
This was raised by [email protected] at
https://lore.kernel.org/bpf/3bc734dfbeffc1c45e1c640be67af1824500aa1b8e04aee814e1942c1064cc96@mail.kernel.org/
include/linux/bpf.h struct bpf_prog_offload { · line 1156
Quoted 3 lines
include/linux/bpf.h +3
-0+* +* 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
Should this say 'kfuncs' instead of 'kfunc's'? The context is plural (referring to multiple functions), not possessive. The same issue appears twice in the commit message.
This was raised by [email protected] at
https://lore.kernel.org/bpf/3bc734dfbeffc1c45e1c640be67af1824500aa1b8e04aee814e1942c1064cc96@mail.kernel.org/
include/linux/bpf.h struct bpf_prog_offload { · line 1160
Quoted 4 lines
include/linux/bpf.h +2
-0+* 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
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/24736686700
Quoted 11 lines · Yonghong Song
The patch set prepares to support stack arguments for bpf functions and kfunc's. The major changes include:
- Avoid redundant calculation ofbpf_reg_state. For stack arguments, there exists no corresponding register number.
- Refactorcheck_kfunc_mem_size_reg()to have bpf_reg_state's for bothmem_regandsize_reg.
- Allow verifier logs to print stack arguments if there is no corresponding register.Please see individual patches for details.
For the set:
Quoted 1 line
[...]