bpf: WARN_ONCE on verifier bugs
PATCH v4 1/1 bpf: WARN_ONCE on verifier bugs
Throughout the verifier's logic, there are multiple checks for inconsistent states that should never happen and would indicate a verifier bug. These bugs are typically logged in the verifier logs and sometimes preceded by a WARN_ONCE.
This patch reworks these checks to consistently emit a verifier log AND a warning when CONFIG_DEBUG_KERNEL is enabled. The consistent use of WARN_ONCE should help fuzzers (ex. syzkaller) expose any situation where they are actually able to reach one of those buggy verifier states.
---
Changes in v4:
- Evaluate condition once and stringify it, as suggested by Alexei.
- Use verifier_bug_if instead of verifier_bug where it can help disambiguate the callsite or shorten the message.
- Add newline character in verifier_bug_if directly.
Changes in v3:
- Introduce and use verifier_bug_if, as suggested by Andrii.
Changes in v2:
- Introduce a new BPF_WARN_ONCE macro, with WARN_ONCE conditioned on CONFIG_DEBUG_KERNEL, as per reviews.
- Use the new helper function for verifier bugs missed in v1, particularly around backtracking.
include/linux/bpf.h | 6 ++ include/linux/bpf_verifier.h | 11 +++ kernel/bpf/btf.c | 4 +- kernel/bpf/verifier.c | 140 +++++++++++++++-------------------- 4 files changed, 77 insertions(+), 84 deletions(-)
include/linux/bpf.h +6 -0
@@ -346,6 +346,12 @@ static inline const char *btf_field_type_name(enum btf_field_type type) } } +#if IS_ENABLED (CONFIG_DEBUG_KERNEL ) +#define BPF_WARN_ONCE(cond, format...) WARN_ONCE(cond, format) +#else +#define BPF_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond) +#endif +static inline u32 btf_field_type_size (enum btf_field_type type) {switch (type) {
include/linux/bpf_verifier.h +11 -0
@@ -839,6 +839,17 @@ __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env,u32 insn_off, const char *prefix_fmt, ...); +#define verifier_bug_if(cond, env, fmt, args...) \ + ({ \ + bool __cond = unlikely(cond); \ + if (__cond) { \ + BPF_WARN_ONCE(1, "verifier bug: " fmt "(" #cond ")\n", ##args); \ + bpf_log(&env->log, "verifier bug: " fmt "(" #cond ")\n", ##args); \ + } \ + (__cond); \ + }) +#define verifier_bug(env, fmt, args...) verifier_bug_if(1, env, fmt, ##args) +static inline struct bpf_func_state *cur_func (struct bpf_verifier_env *env) {struct bpf_verifier_state *cur = env->cur_state;
kernel/bpf/btf.c +2 -2
@@ -7659,7 +7659,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)return 0 ;if (!prog->aux->func_info) { -bpf_log (log,"Verifier bug\n" ); +verifier_bug (env,"func_info undefined" );return -EFAULT ; }
@@ -7683,7 +7683,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tname =btf_name_by_offset (btf, fn_t->name_off);if (prog->aux->func_info_aux[subprog].unreliable) { -bpf_log (log,"Verifier bug in function %s()\n" , tname); +verifier_bug (env,"unreliable BTF for function %s()" , tname);return -EFAULT ; }if (prog_type ==BPF_PROG_TYPE_EXT )
kernel/bpf/verifier.c +58 -82
338 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 27 lines · Paul Chaignon
Throughout the verifier's logic, there are multiple checks for inconsistent states that should never happen and would indicate a verifier bug. These bugs are typically logged in the verifier logs and sometimes preceded by a
WARN_ONCE.This patch reworks these checks to consistently emit a verifier log AND a warning when
CONFIG_DEBUG_KERNELis enabled. The consistent use ofWARN_ONCEshould help fuzzers (ex. syzkaller) expose any situation where they are actually able to reach one of those buggy verifier states.Signed-off-by Paul Chaignon <[email protected]>---
Changes in v4:
- Evaluate condition once and stringify it, as suggested by Alexei.
- Useverifier_bug_ifinstead ofverifier_bugwhere it can help disambiguate the callsite or shorten the message.
- Add newline character inverifier_bug_ifdirectly.
Changes in v3:
- Introduce and useverifier_bug_if, as suggested by Andrii.
Changes in v2:
- Introduce a newBPF_WARN_ONCEmacro, withWARN_ONCEconditioned onCONFIG_DEBUG_KERNEL, as per reviews.
- Use the new helper function for verifier bugs missed in v1, particularly around backtracking.
Quoted 6 lines
include/linux/bpf.h | 6 ++ include/linux/bpf_verifier.h | 11 +++ kernel/bpf/btf.c | 4 +- kernel/bpf/verifier.c | 140 +++++++++++++++-------------------- 4 files changed, 77 insertions(+), 84 deletions(-)
LGTM overall, left a few comments below, please take a look
Quoted 22 lines
include/linux/bpf.h +6
-0@@ -346,6 +346,12 @@ static inline const char *btf_field_type_name(enum btf_field_type type) } } +#if IS_ENABLED (CONFIG_DEBUG_KERNEL ) +#define BPF_WARN_ONCE(cond, format...) WARN_ONCE(cond, format) +#else +#define BPF_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond) +#endif +static inline u32 btf_field_type_size (enum btf_field_type type) {switch (type) {include/linux/bpf_verifier.h +0
-0
include/linux/bpf_verifier.h __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env, · line 845
Quoted 7 lines
include/linux/bpf_verifier.h +4
-0@@ -839,6 +839,17 @@ __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env,u32 insn_off, const char *prefix_fmt, ...); +#define verifier_bug_if(cond, env, fmt, args...) \ + ({ \ + bool __cond = unlikely(cond); \ + if (__cond) { \
this might be equivalent in terms of code generation, but I'd expect unlikely() to be inside the if()
bool __cond = (cond);
if (unlikely(__cond)) { ... }
Quoted 37 lines
include/linux/bpf_verifier.h +7
-0+ BPF_WARN_ONCE(1, "verifier bug: " fmt "(" #cond ")\n", ##args); \ + bpf_log(&env->log, "verifier bug: " fmt "(" #cond ")\n", ##args); \ + } \ + (__cond); \ + }) +#define verifier_bug(env, fmt, args...) verifier_bug_if(1, env, fmt, ##args) +static inline struct bpf_func_state *cur_func (struct bpf_verifier_env *env) {struct bpf_verifier_state *cur = env->cur_state;kernel/bpf/btf.c +2
-2@@ -7659,7 +7659,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog)return 0 ;if (!prog->aux->func_info) { -bpf_log (log,"Verifier bug\n" ); +verifier_bug (env,"func_info undefined" );return -EFAULT ; }@@ -7683,7 +7683,7 @@ int btf_prepare_func_args(struct bpf_verifier_env *env, int subprog) tname =btf_name_by_offset (btf, fn_t->name_off);if (prog->aux->func_info_aux[subprog].unreliable) { -bpf_log (log,"Verifier bug in function %s()\n" , tname); +verifier_bug (env,"unreliable BTF for function %s()" , tname);return -EFAULT ; }if (prog_type ==BPF_PROG_TYPE_EXT )kernel/bpf/verifier.c +0
-0
kernel/bpf/verifier.c static struct bpf_verifier_state *get_loop_entry(struct bpf_verifier_env *env, · line 1928
Quoted 8 lines
kernel/bpf/verifier.c +2
-3@@ -1924,11 +1924,9 @@ static struct bpf_verifier_state *get_loop_entry(struct bpf_verifier_env *env,u32 steps =0 ;while (topmost && topmost->loop_entry) { -if (steps++ > st->dfs_depth) { -WARN_ONCE (true,"verifier bug: infinite loop in get_loop_entry\n" ); -verbose (env,"verifier bug: infinite loop in get_loop_entry()\n" ); +if (verifier_bug_if (steps++ > st->dfs_depth, env, +"infinite loop" ))
nit: I'd keep it single-line (it probably fits 100 characters limit)
Quoted 5 lines
kernel/bpf/verifier.c +0
-1return ERR_PTR (-EFAULT ); - } topmost = topmost->loop_entry; }return topmost;
[...]
Quoted 15 lines
kernel/bpf/verifier.c +1
-3@@ -3999,8 +3995,7 @@ static inline int bt_subprog_enter(struct backtrack_state *bt)static inline int bt_subprog_exit (struct backtrack_state *bt) {if (bt->frame ==0 ) { -verbose (bt->env,"BUG subprog exit from frame 0\n" ); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (bt->env,"subprog exit from frame 0" );return -EFAULT ; } bt->frame--;@@ -4278,14 +4273,15 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* should be literally next instruction in * caller program */ -WARN_ONCE (idx +1 != subseq_idx,"verifier backtracking bug" );
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4283
Quoted 10 lines
kernel/bpf/verifier.c +3
-2+verifier_bug_if (idx +1 != subseq_idx, env, +"extra insn from subprog" );/* r1-r5 are invalidated after subprog call, * so for global func call it shouldn't be set * anymore */ if (bt_reg_mask (bt) &BPF_REGMASK_ARGS ) { -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); + verifier_bug(env, "scratch reg set: regs %x",
"global subprog unexpected regs %x"
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4298
Quoted 11 lines
kernel/bpf/verifier.c +2
-2+ bt_reg_mask(bt));return -EFAULT ; }/* global subprog always sets R0 */ @@ -4299,16 +4295,16 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* the current frame should be zero by now */ if (bt_reg_mask (bt) & ~BPF_REGMASK_ARGS ) { -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); + verifier_bug(env, "unexpected precise regs %x",
"static subprog unexpected regs %x"
(note, user is not expected to really make sense out of this, it's just for reporting to us and our debugging, so let's make messages distinct, but they don't necessarily need to be precise, IMO)
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4307
Quoted 11 lines
kernel/bpf/verifier.c +3
-2+ bt_reg_mask(bt));return -EFAULT ; }/* we are now tracking register spills correctly, * so any instance of leftover slots is a bug */ if (bt_stack_mask (bt) !=0 ) { -verbose (env,"BUG stack slots %llx\n" ,bt_stack_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug (subprog leftover stack slots)" ); +verifier_bug (env,"subprog leftover stack slots %llx" , +bt_stack_mask (bt));
good enough as is, but we can add "static " for consistency with the above
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4331
Quoted 11 lines
kernel/bpf/verifier.c +2
-2return -EFAULT ; }/* propagate r1-r5 to the caller */ @@ -4331,13 +4327,13 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* not actually arguments passed directly to callback subprogs */ if (bt_reg_mask (bt) & ~BPF_REGMASK_ARGS ) { -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"unexpected precise regs %x" , +bt_reg_mask (bt));
"callback unexpected regs %x"
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4355
Quoted 12 lines
kernel/bpf/verifier.c +2
-2return -EFAULT ; }if (bt_stack_mask (bt) !=0 ) { -verbose (env,"BUG stack slots %llx\n" ,bt_stack_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug (callback leftover stack slots)" ); +verifier_bug (env,"callback leftover stack slots %llx" , +bt_stack_mask (bt));return -EFAULT ; }/* clear r1-r5 in callback subprog's mask */ @@ -4359,8 +4355,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,/* if backtracing was looking for registers R1-R5
oops, "backtracking" :)
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4358
Quoted 5 lines
kernel/bpf/verifier.c +1
-2* they should have been found already. */ -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"regs not found %x" ,bt_reg_mask (bt));
"call unexpected regs %x"
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4376
Quoted 10 lines
kernel/bpf/verifier.c +1
-2return -EFAULT ; } }else if (opcode ==BPF_EXIT ) {@@ -4378,8 +4373,7 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,for (i =BPF_REG_1 ; i <=BPF_REG_5 ; i++)bt_clear_reg (bt, i);if (bt_reg_mask (bt) &BPF_REGMASK_ARGS ) { -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"regs not found %x" ,bt_reg_mask (bt));
for this one and a few above, I'd probably leave the "backtracking" word in the message, so something like
"backtracking exit unexpected regs %x"
Quoted 16 lines
kernel/bpf/verifier.c +2
-3return -EFAULT ; }@@ -4720,9 +4714,8 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno)return 0 ; } -verbose (env,"BUG backtracking func entry subprog %d reg_mask %x stack_mask %llx\n" , - st->frame[0 ]->subprogno,bt_reg_mask (bt),bt_stack_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"backtracking func entry subprog %d reg_mask %x stack_mask %llx" , + st->frame[0 ]->subprogno,bt_reg_mask (bt),bt_stack_mask (bt));return -EFAULT ; }
kernel/bpf/verifier.c static int __mark_chain_precision(struct bpf_verifier_env *env, int regno) · line 4747
Quoted 5 lines
kernel/bpf/verifier.c +1
-1@@ -4751,17 +4744,14 @@ static int __mark_chain_precision(struct bpf_verifier_env *env, int regno) i =get_prev_insn_idx (env, st, i, hist_start, &hist_end);if (i == -ENOENT )break ; -if (i >= env->prog->len) { +if (verifier_bug_if (i >= env->prog->len, env,"backtracking idx %d" , i))
this is the case where I'd leaver verifier_bug_if() inside the if() {} itself, at least due to that long comment that actually describes the bug context, but as I said, it's minor
Quoted 13 lines
kernel/bpf/verifier.c +0
-3/* This can happen if backtracking reached insn 0 * and there are still reg_mask or stack_mask * to backtrack. * It means the backtracking missed the spot where * particular register was initialized with a constant. */ -verbose (env,"BUG backtracking idx %d\n" , i); -WARN_ONCE (1 ,"verifier backtracking bug" );return -EFAULT ; - } } st = st->parent;if (!st)
[...]
kernel/bpf/verifier.c static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls · line 10272
Quoted 8 lines
kernel/bpf/verifier.c +2
-2@@ -10286,8 +10268,8 @@ static int setup_func_entry(struct bpf_verifier_env *env, int subprog, int calls }if (state->frame[state->curframe +1 ]) { -verbose (env,"verifier bug. Frame %d already allocated\n" , - state->curframe +1 ); +verifier_bug (env,"Frame %d already allocated" , + state->curframe +1 );
nit: single line
kernel/bpf/verifier.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog, · line 10387
Quoted 11 lines
kernel/bpf/verifier.c +2
-2return -EFAULT ; }@@ -10401,8 +10383,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, int subprog,if (err)return err; }else { -bpf_log (log,"verifier bug: unrecognized arg#%d type %d\n" , - i, arg->arg_type); +verifier_bug (env,"unrecognized arg#%d type %d" , + i, arg->arg_type);
single line, if possible
Quoted 3 lines
kernel/bpf/verifier.c +0
-0return -EFAULT ; } }
[...]
Quoted 35 lines · Andrii Nakryiko
Quoted 27 lines · Paul Chaignon
Throughout the verifier's logic, there are multiple checks for inconsistent states that should never happen and would indicate a verifier bug. These bugs are typically logged in the verifier logs and sometimes preceded by a
WARN_ONCE.This patch reworks these checks to consistently emit a verifier log AND a warning when
CONFIG_DEBUG_KERNELis enabled. The consistent use ofWARN_ONCEshould help fuzzers (ex. syzkaller) expose any situation where they are actually able to reach one of those buggy verifier states.Signed-off-by Paul Chaignon <[email protected]>---
Changes in v4:
- Evaluate condition once and stringify it, as suggested by Alexei.
- Useverifier_bug_ifinstead ofverifier_bugwhere it can help disambiguate the callsite or shorten the message.
- Add newline character inverifier_bug_ifdirectly.
Changes in v3:
- Introduce and useverifier_bug_if, as suggested by Andrii.
Changes in v2:
- Introduce a newBPF_WARN_ONCEmacro, withWARN_ONCEconditioned onCONFIG_DEBUG_KERNEL, as per reviews.
- Use the new helper function for verifier bugs missed in v1, particularly around backtracking.Quoted 6 lines
include/linux/bpf.h | 6 ++ include/linux/bpf_verifier.h | 11 +++ kernel/bpf/btf.c | 4 +- kernel/bpf/verifier.c | 140 +++++++++++++++-------------------- 4 files changed, 77 insertions(+), 84 deletions(-)
Quoted 3 lines
LGTM overall, left a few comments below, please take a look
Acked-by Andrii Nakryiko <[email protected]>
Thanks a lot for the detailed review!
Quoted 34 lines
Quoted 23 lines
include/linux/bpf.h +6
-0@@ -346,6 +346,12 @@ static inline const char *btf_field_type_name(enum btf_field_type type) } } +#if IS_ENABLED (CONFIG_DEBUG_KERNEL ) +#define BPF_WARN_ONCE(cond, format...) WARN_ONCE(cond, format) +#else +#define BPF_WARN_ONCE(cond, format...) BUILD_BUG_ON_INVALID(cond) +#endif +static inline u32 btf_field_type_size (enum btf_field_type type) {switch (type) {include/linux/bpf_verifier.h +0
-0include/linux/bpf_verifier.h __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env, · line 845
Quoted 7 lines
include/linux/bpf_verifier.h +4
-0@@ -839,6 +839,17 @@ __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env,u32 insn_off, const char *prefix_fmt, ...); +#define verifier_bug_if(cond, env, fmt, args...) \ + ({ \ + bool __cond = unlikely(cond); \ + if (__cond) { \this might be equivalent in terms of code generation, but I'd expect
unlikely()to be inside theif()
Quoted 2 lines
bool
__cond= (cond);
if (unlikely(__cond)) { ... }
I was worried the compiler may not take the unlikely into account when doing if (verifier_bug_if(...)). I checked with a small example involving a similar macro and the generated code is indeed the exact same. I'll stick to the usual style, as suggested.
[...]
Quoted 14 lines
include/linux/bpf_verifier.h static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4298
Quoted 11 lines
include/linux/bpf_verifier.h +2
-2+ bt_reg_mask(bt));return -EFAULT ; }/* global subprog always sets R0 */ @@ -4299,16 +4295,16 @@ static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx,* the current frame should be zero by now */ if (bt_reg_mask (bt) & ~BPF_REGMASK_ARGS ) { -verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); + verifier_bug(env, "unexpected precise regs %x","static subprog unexpected regs %x"
Quoted 3 lines
(note, user is not expected to really make sense out of this, it's just for reporting to us and our debugging, so let's make messages distinct, but they don't necessarily need to be precise, IMO)
That makes sense. Considering this, I went back over the four identical "No program starts at insn" error messages and tweaked them a bit based on context. All error messages should now be unique.
[...]