bpf: WARN_ONCE on verifier bugs
PATCH v5 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 v5:
- Improve error messages according to Andrii's suggestions.
- Style adjustments related to line lengths and unlikely.
- Replaced the 4 "No program starts at insn" to be unique.
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 | 142 +++++++++++++++-------------------- 4 files changed, 79 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 = (cond); \ + if (unlikely(__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 +60 -82
336 lines not on this page · raw message
Signature · 1 line
2.43.0