Skip to content

Commit d9762e8

Browse files
iamkafaiAlexei Starovoitov
authored andcommitted
bpf: verbose log bpf_line_info in verifier
This patch adds bpf_line_info during the verifier's verbose. It can give error context for debug purpose. ~~~~~~~~~~ Here is the verbose log for backedge: while (a) { a += bpf_get_smp_processor_id(); bpf_trace_printk(fmt, sizeof(fmt), a); } ~> bpftool prog load ./test_loop.o /sys/fs/bpf/test_loop type tracepoint 13: while (a) { 3: a += bpf_get_smp_processor_id(); back-edge from insn 13 to 3 ~~~~~~~~~~ Here is the verbose log for invalid pkt access: Modification to test_xdp_noinline.c: data = (void *)(long)xdp->data; data_end = (void *)(long)xdp->data_end; /* if (data + 4 > data_end) return XDP_DROP; */ *(u32 *)data = dst->dst; ~> bpftool prog load ./test_xdp_noinline.o /sys/fs/bpf/test_xdp_noinline type xdp ; data = (void *)(long)xdp->data; 224: (79) r2 = *(u64 *)(r10 -112) 225: (61) r2 = *(u32 *)(r2 +0) ; *(u32 *)data = dst->dst; 226: (63) *(u32 *)(r2 +0) = r1 invalid access to packet, off=0 size=4, R2(id=0,off=0,r=0) R2 offset is outside of the packet Signed-off-by: Martin KaFai Lau <kafai@fb.com> Acked-by: Yonghong Song <yhs@fb.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 23127b3 commit d9762e8

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

include/linux/bpf_verifier.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ struct bpf_verifier_env {
224224
bool allow_ptr_leaks;
225225
bool seen_direct_write;
226226
struct bpf_insn_aux_data *insn_aux_data; /* array of per-insn state */
227+
const struct bpf_line_info *prev_linfo;
227228
struct bpf_verifier_log log;
228229
struct bpf_subprog_info subprog_info[BPF_MAX_SUBPROGS + 1];
229230
u32 subprog_cnt;

kernel/bpf/verifier.c

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <linux/bsearch.h>
2727
#include <linux/sort.h>
2828
#include <linux/perf_event.h>
29+
#include <linux/ctype.h>
2930

3031
#include "disasm.h"
3132

@@ -216,6 +217,27 @@ struct bpf_call_arg_meta {
216217

217218
static DEFINE_MUTEX(bpf_verifier_lock);
218219

220+
static const struct bpf_line_info *
221+
find_linfo(const struct bpf_verifier_env *env, u32 insn_off)
222+
{
223+
const struct bpf_line_info *linfo;
224+
const struct bpf_prog *prog;
225+
u32 i, nr_linfo;
226+
227+
prog = env->prog;
228+
nr_linfo = prog->aux->nr_linfo;
229+
230+
if (!nr_linfo || insn_off >= prog->len)
231+
return NULL;
232+
233+
linfo = prog->aux->linfo;
234+
for (i = 1; i < nr_linfo; i++)
235+
if (insn_off < linfo[i].insn_off)
236+
break;
237+
238+
return &linfo[i - 1];
239+
}
240+
219241
void bpf_verifier_vlog(struct bpf_verifier_log *log, const char *fmt,
220242
va_list args)
221243
{
@@ -266,6 +288,42 @@ __printf(2, 3) static void verbose(void *private_data, const char *fmt, ...)
266288
va_end(args);
267289
}
268290

291+
static const char *ltrim(const char *s)
292+
{
293+
while (isspace(*s))
294+
s++;
295+
296+
return s;
297+
}
298+
299+
__printf(3, 4) static void verbose_linfo(struct bpf_verifier_env *env,
300+
u32 insn_off,
301+
const char *prefix_fmt, ...)
302+
{
303+
const struct bpf_line_info *linfo;
304+
305+
if (!bpf_verifier_log_needed(&env->log))
306+
return;
307+
308+
linfo = find_linfo(env, insn_off);
309+
if (!linfo || linfo == env->prev_linfo)
310+
return;
311+
312+
if (prefix_fmt) {
313+
va_list args;
314+
315+
va_start(args, prefix_fmt);
316+
bpf_verifier_vlog(&env->log, prefix_fmt, args);
317+
va_end(args);
318+
}
319+
320+
verbose(env, "%s\n",
321+
ltrim(btf_name_by_offset(env->prog->aux->btf,
322+
linfo->line_off)));
323+
324+
env->prev_linfo = linfo;
325+
}
326+
269327
static bool type_is_pkt_pointer(enum bpf_reg_type type)
270328
{
271329
return type == PTR_TO_PACKET ||
@@ -4561,6 +4619,7 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
45614619
return 0;
45624620

45634621
if (w < 0 || w >= env->prog->len) {
4622+
verbose_linfo(env, t, "%d: ", t);
45644623
verbose(env, "jump out of range from insn %d to %d\n", t, w);
45654624
return -EINVAL;
45664625
}
@@ -4578,6 +4637,8 @@ static int push_insn(int t, int w, int e, struct bpf_verifier_env *env)
45784637
insn_stack[cur_stack++] = w;
45794638
return 1;
45804639
} else if ((insn_state[w] & 0xF0) == DISCOVERED) {
4640+
verbose_linfo(env, t, "%d: ", t);
4641+
verbose_linfo(env, w, "%d: ", w);
45814642
verbose(env, "back-edge from insn %d to %d\n", t, w);
45824643
return -EINVAL;
45834644
} else if (insn_state[w] == EXPLORED) {
@@ -4600,10 +4661,6 @@ static int check_cfg(struct bpf_verifier_env *env)
46004661
int ret = 0;
46014662
int i, t;
46024663

4603-
ret = check_subprogs(env);
4604-
if (ret < 0)
4605-
return ret;
4606-
46074664
insn_state = kcalloc(insn_cnt, sizeof(int), GFP_KERNEL);
46084665
if (!insn_state)
46094666
return -ENOMEM;
@@ -5448,6 +5505,8 @@ static int do_check(struct bpf_verifier_env *env)
54485505
int insn_processed = 0;
54495506
bool do_print_state = false;
54505507

5508+
env->prev_linfo = NULL;
5509+
54515510
state = kzalloc(sizeof(struct bpf_verifier_state), GFP_KERNEL);
54525511
if (!state)
54535512
return -ENOMEM;
@@ -5521,6 +5580,7 @@ static int do_check(struct bpf_verifier_env *env)
55215580
.private_data = env,
55225581
};
55235582

5583+
verbose_linfo(env, insn_idx, "; ");
55245584
verbose(env, "%d: ", insn_idx);
55255585
print_bpf_insn(&cbs, insn, env->allow_ptr_leaks);
55265586
}
@@ -6755,14 +6815,18 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr,
67556815

67566816
env->allow_ptr_leaks = capable(CAP_SYS_ADMIN);
67576817

6758-
ret = check_cfg(env);
6818+
ret = check_subprogs(env);
67596819
if (ret < 0)
67606820
goto skip_full_check;
67616821

67626822
ret = check_btf_info(env, attr, uattr);
67636823
if (ret < 0)
67646824
goto skip_full_check;
67656825

6826+
ret = check_cfg(env);
6827+
if (ret < 0)
6828+
goto skip_full_check;
6829+
67666830
ret = do_check(env);
67676831
if (env->cur_state) {
67686832
free_verifier_state(env->cur_state, true);

0 commit comments

Comments
 (0)