Skip to content

Commit 491dd8e

Browse files
anakryikoborkmann
authored andcommitted
bpf: Emit global subprog name in verifier logs
We have the name, instead of emitting just func#N to identify global subprog, augment verifier log messages with actual function name to make it more user-friendly. Signed-off-by: Andrii Nakryiko <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Acked-by: Daniel Borkmann <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent b8d78cb commit 491dd8e

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

kernel/bpf/verifier.c

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ struct bpf_kfunc_call_arg_meta {
339339

340340
struct btf *btf_vmlinux;
341341

342+
static const char *btf_type_name(const struct btf *btf, u32 id)
343+
{
344+
return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
345+
}
346+
342347
static DEFINE_MUTEX(bpf_verifier_lock);
343348
static DEFINE_MUTEX(bpf_percpu_ma_lock);
344349

@@ -418,6 +423,17 @@ static bool subprog_is_global(const struct bpf_verifier_env *env, int subprog)
418423
return aux && aux[subprog].linkage == BTF_FUNC_GLOBAL;
419424
}
420425

426+
static const char *subprog_name(const struct bpf_verifier_env *env, int subprog)
427+
{
428+
struct bpf_func_info *info;
429+
430+
if (!env->prog->aux->func_info)
431+
return "";
432+
433+
info = &env->prog->aux->func_info[subprog];
434+
return btf_type_name(env->prog->aux->btf, info->type_id);
435+
}
436+
421437
static bool reg_may_point_to_spin_lock(const struct bpf_reg_state *reg)
422438
{
423439
return btf_record_has_field(reg_btf_record(reg), BPF_SPIN_LOCK);
@@ -587,11 +603,6 @@ static int iter_get_spi(struct bpf_verifier_env *env, struct bpf_reg_state *reg,
587603
return stack_slot_obj_get_spi(env, reg, "iter", nr_slots);
588604
}
589605

590-
static const char *btf_type_name(const struct btf *btf, u32 id)
591-
{
592-
return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
593-
}
594-
595606
static enum bpf_dynptr_type arg_to_dynptr_type(enum bpf_arg_type arg_type)
596607
{
597608
switch (arg_type & DYNPTR_TYPE_FLAG_MASK) {
@@ -9269,13 +9280,16 @@ static int check_func_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
92699280
if (err == -EFAULT)
92709281
return err;
92719282
if (subprog_is_global(env, subprog)) {
9283+
const char *sub_name = subprog_name(env, subprog);
9284+
92729285
if (err) {
9273-
verbose(env, "Caller passes invalid args into func#%d\n", subprog);
9286+
verbose(env, "Caller passes invalid args into func#%d ('%s')\n",
9287+
subprog, sub_name);
92749288
return err;
92759289
}
92769290

9277-
if (env->log.level & BPF_LOG_LEVEL)
9278-
verbose(env, "Func#%d is global and valid. Skipping.\n", subprog);
9291+
verbose(env, "Func#%d ('%s') is global and assumed valid.\n",
9292+
subprog, sub_name);
92799293
clear_caller_saved_regs(env, caller->regs);
92809294

92819295
/* All global functions return a 64-bit SCALAR_VALUE */
@@ -19893,9 +19907,8 @@ static int do_check_subprogs(struct bpf_verifier_env *env)
1989319907
if (ret) {
1989419908
return ret;
1989519909
} else if (env->log.level & BPF_LOG_LEVEL) {
19896-
verbose(env,
19897-
"Func#%d is safe for any args that match its prototype\n",
19898-
i);
19910+
verbose(env, "Func#%d ('%s') is safe for any args that match its prototype\n",
19911+
i, subprog_name(env, i));
1989919912
}
1990019913
}
1990119914
return 0;

0 commit comments

Comments
 (0)