bpf: WARN_ONCE on verifier bugs
PATCH v2 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 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 | 6 ++ kernel/bpf/btf.c | 4 +- kernel/bpf/verifier.c | 119 +++++++++++++++++------------------ 4 files changed, 70 insertions(+), 65 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 +6 -0
@@ -838,6 +838,12 @@ __printf(3, 4) void verbose_linfo(struct bpf_verifier_env *env,u32 insn_off, const char *prefix_fmt, ...); +#define verifier_bug(env, fmt, args...) \ + do { \ + BPF_WARN_ONCE(1, "verifier bug: " fmt, ##args); \ + bpf_log(&env->log, "verifier bug: " fmt, ##args); \ + } while (0) +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\n" );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()\n" , tname);return -EFAULT ; }if (prog_type ==BPF_PROG_TYPE_EXT )
kernel/bpf/verifier.c +56 -63
297 lines not on this page · raw message
Signature · 1 line
2.43.0
Quoted 20 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 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.
[...]
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4278
Quoted 7 lines
kernel/bpf/verifier.c +2
-1@@ -4277,14 +4274,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" ); +if (unlikely (idx +1 != subseq_idx)) +verifier_bug (env,"extra insn from subprog" );
maybe let's add verifier_buf_if(cond, env, fmt, args...) that would do if (unlikely(...)) inside? it can also return the result of cond if we want to do something like
if (verifier_bug_if(<condition>, env, "we are doomed"))
return -EFAULT;
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4286
Quoted 10 lines
kernel/bpf/verifier.c +2
-2/* 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\n" , +bt_reg_mask (bt));return -EFAULT ;
but please don't go overboard with verifier_buf_if() for cases like this, I think this should use plain verifier_bug() as you did, even if it *can* be expressed with verifier_buf_if() check
Quoted 23 lines
kernel/bpf/verifier.c +4
-4}/* global subprog always sets R0 */ @@ -4298,16 +4296,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\n" , +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\n" , +bt_stack_mask (bt));return -EFAULT ; }/* propagate r1-r5 to the caller */
[...]
Quoted 21 lines · Andrii Nakryiko
Quoted 20 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 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 12 lines
kernel/bpf/verifier.c static int backtrack_insn(struct bpf_verifier_env *env, int idx, int subseq_idx, · line 4286
Quoted 10 lines
/* 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) {kernel/bpf/verifier.c +2
-2-verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"scratch reg set: regs %x\n" , +bt_reg_mask (bt));return -EFAULT ;
Quoted 3 lines
but please don't go overboard with
verifier_buf_if()for cases like this, I think this should use plainverifier_bug()as you did, even if it *can* be expressed withverifier_buf_if()check
Where would you set the bar on these cases? Is it mostly a matter of readability?
I'm asking because, with Alexei's suggestion to stringify the condition in verifier_bug_if() (cf. v3), we would gain from using verifier_bug_if more often.
[...]
Quoted 38 lines · Paul Chaignon
Quoted 21 lines · Andrii Nakryiko
Quoted 20 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 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 12 lines
Quoted 10 lines
/* 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) {kernel/bpf/verifier.c +2
-2-verbose (env,"BUG regs %x\n" ,bt_reg_mask (bt)); -WARN_ONCE (1 ,"verifier backtracking bug" ); +verifier_bug (env,"scratch reg set: regs %x\n" , +bt_reg_mask (bt));return -EFAULT ;
Quoted 6 lines
Quoted 3 lines
but please don't go overboard with
verifier_buf_if()for cases like this, I think this should use plainverifier_bug()as you did, even if it *can* be expressed withverifier_buf_if()checkWhere would you set the bar on these cases? Is it mostly a matter of readability?
yes, it's all subjective, there is no algorithm and no clear cut boundary on what's readable or not, so it's no big deal
Quoted 7 lines
I'm asking because, with Alexei's suggestion to stringify the condition in
verifier_bug_if()(cf. v3), we would gain from usingverifier_bug_ifmore often.[...]