Augment selected helper and kfunc argument-contract failures with Call Type Safety reports. Keep the existing terse verifier messages and add reason, source context, causal register history, and targeted suggestions. Cover helper register-type mismatch, helper non-NULL pointer requirements, kfunc trusted-pointer nullability, release kfunc referenced-pointer requirements, kfunc memory arguments, memory/length pairs, and kernel object pointer contracts. Signed-off-by: Kumar Kartikeya Dwivedi --- kernel/bpf/diagnostics.c | 37 +++++ kernel/bpf/diagnostics.h | 4 + kernel/bpf/verifier.c | 295 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 329 insertions(+), 7 deletions(-) diff --git a/kernel/bpf/diagnostics.c b/kernel/bpf/diagnostics.c index 369b7813b5dc..169c1a2425c8 100644 --- a/kernel/bpf/diagnostics.c +++ b/kernel/bpf/diagnostics.c @@ -714,6 +714,43 @@ static const char *bpf_diag_arg_ordinal(int argno) } } +void bpf_diag_report_call_type(struct bpf_verifier_env *env, u32 insn_idx, + int regno, const char *call_name, + const char *arg_name, const char *reason, + const char *suggestion) +{ + struct bpf_diag_history_opts opts = { + .scope = BPF_DIAG_HISTORY_SCOPE_REG, + .frameno = bpf_diag_current_frameno(env), + .regno = regno, + }; + const char *ordinal = bpf_diag_arg_ordinal(regno); + char arg_desc[48]; + + if (ordinal && arg_name) + scnprintf(arg_desc, sizeof(arg_desc), "%s argument (%s)", + ordinal, arg_name); + else if (arg_name) + scnprintf(arg_desc, sizeof(arg_desc), "argument %s", arg_name); + else + scnprintf(arg_desc, sizeof(arg_desc), "argument"); + + bpf_diag_report_header(env, BPF_DIAG_CATEGORY_CALL_TYPE_SAFETY, + "invalid call argument"); + bpf_diag_report_reason(env, + "The %s to %s does not satisfy the verifier contract: %s.", + arg_desc, call_name, reason); + + bpf_diag_report_section(env, "At"); + bpf_diag_report_source(env, insn_idx, '!', + "invalid %s for %s", arg_desc, call_name); + + if (regno >= 0) + bpf_diag_print_history(env, &opts); + + bpf_diag_report_suggestion(env, "%s", suggestion); +} + void bpf_diag_report_invalid_deref(struct bpf_verifier_env *env, u32 insn_idx, int regno, const char *reg_name, const char *type_name, diff --git a/kernel/bpf/diagnostics.h b/kernel/bpf/diagnostics.h index 0bc5f3492c47..d8c433759dee 100644 --- a/kernel/bpf/diagnostics.h +++ b/kernel/bpf/diagnostics.h @@ -181,6 +181,10 @@ void bpf_diag_report_resource_state(struct bpf_verifier_env *env, const char *suggestion); void bpf_diag_report_ref_leak(struct bpf_verifier_env *env, u32 ref_id, u32 alloc_insn, u32 fail_insn); +void bpf_diag_report_call_type(struct bpf_verifier_env *env, u32 insn_idx, + int regno, const char *call_name, + const char *arg_name, const char *reason, + const char *suggestion); void bpf_diag_clear_history(struct bpf_verifier_state *state); void bpf_diag_copy_history(struct bpf_verifier_state *dst, diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index 1c26614bec2b..d2a776d2059c 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -8039,6 +8039,39 @@ static const struct bpf_reg_types *compatible_reg_types[__BPF_ARG_TYPE_MAX] = { [ARG_PTR_TO_DYNPTR] = &dynptr_types, }; +static const char *bpf_diag_reg_type_plain(struct bpf_verifier_env *env, + enum bpf_reg_type type) +{ + switch (base_type(type)) { + case NOT_INIT: + return "an uninitialized value"; + case SCALAR_VALUE: + return "an integer scalar"; + case PTR_TO_CTX: + return "a context pointer"; + case PTR_TO_STACK: + return "a stack pointer"; + case PTR_TO_MAP_VALUE: + if (type_may_be_null(type)) + return "a nullable map value pointer"; + return "a map value pointer"; + case PTR_TO_MEM: + if (type_may_be_null(type)) + return "a nullable memory pointer"; + return "a memory pointer"; + case PTR_TO_BTF_ID: + if (type_is_non_owning_ref(type)) + return "a borrowed allocated object pointer"; + if (type_is_ptr_alloc_obj(type)) + return "an owned allocated object pointer"; + if (type_flag(type) & PTR_UNTRUSTED) + return "an untrusted kernel object pointer"; + return "a kernel object pointer"; + default: + return reg_type_str(env, type); + } +} + static int check_reg_type(struct bpf_verifier_env *env, struct bpf_reg_state *reg, argno_t argno, enum bpf_arg_type arg_type, const u32 *arg_btf_id, @@ -8092,6 +8125,27 @@ static int check_reg_type(struct bpf_verifier_env *env, struct bpf_reg_state *re for (j = 0; j + 1 < i; j++) verbose(env, "%s, ", reg_type_str(env, compatible->types[j])); verbose(env, "%s\n", reg_type_str(env, compatible->types[j])); + { + char expected_buf[160], reason[256]; + int len = 0; + + expected_buf[0] = '\0'; + for (j = 0; j < i; j++) + len += scnprintf(expected_buf + len, + sizeof(expected_buf) - len, + "%s%s", j ? ", " : "", + reg_type_str(env, compatible->types[j])); + + scnprintf(reason, sizeof(reason), + "it has type %s, but this helper argument accepts %s", + bpf_diag_reg_type_plain(env, reg->type), + expected_buf); + bpf_diag_report_call_type(env, env->insn_idx, + reg_from_argno(argno), + func_id_name(meta->func_id), + reg_arg_name(env, argno), reason, + "Pass a value with one of the accepted pointer or scalar types for this helper."); + } return -EACCES; found: @@ -8128,6 +8182,12 @@ static int check_reg_type(struct bpf_verifier_env *env, struct bpf_reg_state *re (!type_may_be_null(arg_type) || arg_type_is_release(arg_type))) { verbose(env, "Possibly NULL pointer passed to helper %s\n", reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, env->insn_idx, + reg_from_argno(argno), + func_id_name(meta->func_id), + reg_arg_name(env, argno), + "the pointer may be NULL, but this helper requires a non-NULL pointer", + "Add a NULL check and call the helper only on the non-NULL path."); return -EACCES; } @@ -8476,6 +8536,12 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg, !reg_is_referenced(env, reg) && !bpf_register_is_null(reg)) { verbose(env, "release helper %s expects referenced PTR_TO_BTF_ID passed to %s\n", func_id_name(meta->func_id), reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_id_name(meta->func_id), + reg_arg_name(env, argno), + "release helpers require a value that owns a live resource returned by a matching acquire helper", + "Pass the resource-owning pointer returned by the matching acquire helper, and avoid calling the release helper after ownership has " + "already been transferred or released."); return -EINVAL; } @@ -11560,9 +11626,22 @@ get_kfunc_ptr_arg_type(struct bpf_verifier_env *env, struct bpf_func_state *call */ if (!btf_type_is_scalar(ref_t) && !__btf_type_is_scalar_struct(env, meta->btf, ref_t, 0) && (arg_mem_size ? !btf_type_is_void(ref_t) : 1)) { + char arg_name[32], expected_type[64], reason[256]; + + strscpy(arg_name, reg_arg_name(env, argno), sizeof(arg_name)); + bpf_diag_format_btf_type(expected_type, sizeof(expected_type), + ref_t, ref_tname); verbose(env, "%s pointer type %s %s must point to %sscalar, or struct with scalar\n", - reg_arg_name(env, argno), + arg_name, btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : ""); + scnprintf(reason, sizeof(reason), + "the kfunc expects a pointer to %s, but this argument is %s and cannot be used as that kernel object pointer", + expected_type, + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, env->insn_idx, + reg_from_argno(argno), + meta->func_name, arg_name, reason, + "Pass a verifier-tracked pointer to the expected kernel object type, not a pointer to stack storage or another memory buffer."); return -EINVAL; } return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM; @@ -12209,7 +12288,18 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ if (btf_type_is_scalar(t)) { if (reg->type != SCALAR_VALUE) { + char reason[256]; + verbose(env, "%s is not a scalar\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc expects an integer scalar, but %s is %s", + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass an integer scalar value for this argument, not a pointer or resource object."); return -EINVAL; } @@ -12219,8 +12309,18 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EFAULT; } if (!tnum_is_const(reg->var_off)) { + char reason[256]; + verbose(env, "%s must be a known constant\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc requires this scalar argument to be a verifier-known constant, but %s is variable on this path", + reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a compile-time constant or a value the verifier can prove is constant at this call."); return -EINVAL; } if (regno >= 0) @@ -12245,8 +12345,18 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ } if (!tnum_is_const(reg->var_off)) { + char reason[256]; + verbose(env, "%s is not a const\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc uses this argument as a return-buffer size, but %s is variable on this path", + reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a verifier-known constant size for this kfunc buffer argument."); return -EINVAL; } @@ -12267,26 +12377,53 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EINVAL; } + ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); + ref_tname = btf_name_by_offset(btf, ref_t->name_off); + if ((bpf_register_is_null(reg) || type_may_be_null(reg->type)) && !is_kfunc_arg_nullable(meta->btf, &args[i])) { + char expected_type[64], reason[256]; + + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), ref_t, + ref_tname); + scnprintf(reason, sizeof(reason), + "the pointer may be NULL, but this kfunc requires a non-NULL pointer to %s", + expected_type); verbose(env, "Possibly NULL pointer passed to trusted %s\n", reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Add a NULL check and call the kfunc only on the non-NULL path."); return -EACCES; } if (regno == meta->release_regno && !is_kfunc_arg_dynptr(meta->btf, &args[i]) && !reg_is_referenced(env, reg) && !bpf_register_is_null(reg)) { + char expected_type[64], reason[256]; + + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), ref_t, + ref_tname); + scnprintf(reason, sizeof(reason), + "release kfuncs require a resource-owning pointer to %s returned by a matching acquire kfunc", + expected_type); verbose(env, "release kfunc %s expects referenced PTR_TO_BTF_ID passed to %s\n", func_name, reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass the resource-owning pointer returned by the matching acquire kfunc, and avoid calling the release kfunc after ownership has " + "already been transferred or released."); return -EINVAL; } if (reg_is_referenced(env, reg)) update_ref_obj(&meta->ref_obj, reg); - ref_t = btf_type_skip_modifiers(btf, t->type, &ref_id); - ref_tname = btf_name_by_offset(btf, ref_t->name_off); - kf_arg_type = get_kfunc_ptr_arg_type(env, caller, regs, meta, t, ref_t, ref_tname, args, i, nargs, argno, reg); if (kf_arg_type < 0) @@ -12336,13 +12473,44 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ case KF_ARG_PTR_TO_BTF_ID: if (!is_trusted_reg(env, reg)) { if (!is_kfunc_rcu(meta)) { + char expected_type[64], reason[256]; + + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), + ref_t, ref_tname); verbose(env, "%s must be referenced or trusted\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc requires a trusted or resource-owning pointer to %s, but %s is %s", + expected_type, + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a pointer acquired from a verifier-tracked source, or call this kfunc only inside the required protection if it accepts RCU " + "pointers."); return -EINVAL; } if (!is_rcu_reg(reg)) { + char expected_type[64], reason[256]; + + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), + ref_t, ref_tname); verbose(env, "%s must be a rcu pointer\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc requires an RCU-protected pointer to %s, but %s is %s", + expected_type, + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Use this kfunc with a pointer that is valid in an RCU read lock region."); return -EINVAL; } } @@ -12383,8 +12551,19 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ switch (kf_arg_type) { case KF_ARG_PTR_TO_CTX: if (reg->type != PTR_TO_CTX) { + char reason[256]; + verbose(env, "%s expected pointer to ctx, but got %s\n", reg_arg_name(env, argno), reg_type_str(env, reg->type)); + scnprintf(reason, sizeof(reason), + "the kfunc expects a context pointer, but %s is %s", + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass the original program context pointer or preserve it before modifying registers."); return -EINVAL; } @@ -12409,12 +12588,33 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EINVAL; } } else { + char reason[256]; + verbose(env, "%s expected pointer to allocated object\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc expects an allocated object pointer, but %s is %s", + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a pointer returned by the matching BPF object allocation path."); return -EINVAL; } if (!reg_is_referenced(env, reg)) { + char reason[256]; + verbose(env, "allocated object must be referenced\n"); + scnprintf(reason, sizeof(reason), + "the allocated object pointer in %s must still carry verifier-tracked ownership, but this pointer no longer owns a live resource", + reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass the owned object pointer before it is released or transferred."); return -EINVAL; } if (meta->btf == btf_vmlinux) { @@ -12580,8 +12780,25 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EINVAL; } ret = check_mem_reg(env, reg, argno, type_size); - if (ret < 0) + if (ret < 0) { + char arg_name[32], expected_type[64], reason[256]; + + strscpy(arg_name, reg_arg_name(env, argno), + sizeof(arg_name)); + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), + ref_t, ref_tname); + scnprintf(reason, sizeof(reason), + "the kfunc expects %u bytes of memory for %s, but it is %s and not verifier-known memory", + type_size, expected_type, + bpf_diag_reg_type_plain(env, + reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, arg_name, + reason, + "Pass stack, map, context, or other verifier-known memory of the expected type and size, not an integer cast to a pointer."); return ret; + } break; case KF_ARG_PTR_TO_MEM_SIZE: { @@ -12595,9 +12812,27 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ ret = check_kfunc_mem_size_reg(env, buff_reg, size_reg, argno, next_argno); if (ret < 0) { - verbose(env, "%s and ", reg_arg_name(env, argno)); + char mem_arg[32], size_arg[32]; + char reason[256]; + + strscpy(mem_arg, reg_arg_name(env, argno), + sizeof(mem_arg)); + strscpy(size_arg, reg_arg_name(env, next_argno), + sizeof(size_arg)); + verbose(env, "%s and ", mem_arg); verbose(env, "%s memory, len pair leads to invalid memory access\n", - reg_arg_name(env, next_argno)); + size_arg); + scnprintf(reason, sizeof(reason), + "it is the memory pointer in a memory/length pair with %s, but it is %s and does not point to verifier-readable memory for the " + "requested length", + size_arg, + bpf_diag_reg_type_plain(env, + buff_reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + mem_arg, + reason, + "Pass a stack, map, context, or other verifier-known memory pointer, and keep the paired length within that object."); return ret; } } @@ -12608,8 +12843,19 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ return -EFAULT; } if (!tnum_is_const(size_reg->var_off)) { + char reason[256]; + verbose(env, "%s must be a known constant\n", reg_arg_name(env, next_argno)); + scnprintf(reason, sizeof(reason), + "the kfunc requires the paired memory size argument %s to be a verifier-known constant, but it is variable on this path", + reg_arg_name(env, next_argno)); + bpf_diag_report_call_type(env, insn_idx, + reg_from_argno(next_argno), + func_name, + reg_arg_name(env, next_argno), + reason, + "Pass a compile-time constant or verifier-known constant for this memory size argument."); return -EINVAL; } meta->arg_constant.found = true; @@ -12629,8 +12875,21 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ break; case KF_ARG_PTR_TO_REFCOUNTED_KPTR: if (!type_is_ptr_alloc_obj(reg->type)) { + char expected_type[64], reason[256]; + + bpf_diag_format_btf_type(expected_type, + sizeof(expected_type), + ref_t, ref_tname); + scnprintf(reason, sizeof(reason), + "the kfunc expects a pointer to BPF-managed refcounted object type %s, but this argument is not such an object pointer", + expected_type); verbose(env, "%s is neither owning or non-owning ref\n", reg_arg_name(env, argno)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a pointer returned by the matching BPF object allocation or lookup operation for this kfunc."); return -EINVAL; } if (!type_is_non_owning_ref(reg->type)) @@ -12653,8 +12912,19 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ break; case KF_ARG_PTR_TO_CONST_STR: if (reg->type != PTR_TO_MAP_VALUE) { + char reason[256]; + verbose(env, "%s doesn't point to a const string\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc expects a pointer to a constant string stored in verifier-known memory, but %s is %s", + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass a constant string pointer that the verifier recognizes, such as a string stored in a read-only map value."); return -EINVAL; } ret = check_arg_const_str(env, reg, argno); @@ -12693,8 +12963,19 @@ static int check_kfunc_args(struct bpf_verifier_env *env, struct bpf_kfunc_call_ break; case KF_ARG_PTR_TO_IRQ_FLAG: if (reg->type != PTR_TO_STACK) { + char reason[256]; + verbose(env, "%s doesn't point to an irq flag on stack\n", reg_arg_name(env, argno)); + scnprintf(reason, sizeof(reason), + "the kfunc expects a stack pointer to an IRQ flag slot, but %s is %s", + reg_arg_name(env, argno), + bpf_diag_reg_type_plain(env, reg->type)); + bpf_diag_report_call_type(env, insn_idx, regno, + func_name, + reg_arg_name(env, argno), + reason, + "Pass the same stack slot used by bpf_local_irq_save() or bpf_res_spin_lock_irqsave()."); return -EINVAL; } ret = process_irq_flag(env, reg, argno, meta); -- 2.53.0