All of lore.kernel.org
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Kumar Kartikeya Dwivedi <memxor@gmail.com>, bpf@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	 Daniel Borkmann <daniel@iogearbox.net>,
	Emil Tsalapatis <emil@etsalapatis.com>,
	kkd@meta.com, kernel-team@meta.com
Subject: Re: [PATCH bpf-next v2 11/17] bpf: Report Call Type Safety argument errors
Date: Wed, 24 Jun 2026 23:05:46 -0700	[thread overview]
Message-ID: <40a445e92a18ba6325081ae2be784bf0bdd45052.camel@gmail.com> (raw)
In-Reply-To: <20260619205934.1312876-12-memxor@gmail.com>

On Fri, 2026-06-19 at 22:59 +0200, Kumar Kartikeya Dwivedi wrote:
> 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 or stack-argument history, and targeted
> suggestions.
> 
> Cover helper register-type mismatch, helper and kfunc non-NULL pointer
> requirements, release-helper ownership requirements, scalar and constant kfunc
> arguments, trusted and RCU pointer contracts, kfunc memory arguments,
> memory/length pairs, refcounted kptrs, constant strings, and IRQ flag stack
> arguments.
> 
> Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
> ---

Acked-by: Eduard Zingerman <eddyz87@gmail.com>

[...]

> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index db151e6b8949..fdbf92bffc17 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -320,6 +320,15 @@ static int arg_idx_from_argno(argno_t a)
>  	return arg_from_argno(a) - 1;
>  }
>  
> +static int bpf_diag_stack_arg_slot_from_argno(argno_t a)

Nit: drop the bpf_diag_ prefix (or just inline at the callsite?).

> +{
> +	int arg = arg_from_argno(a);
> +
> +	if (arg <= MAX_BPF_FUNC_REG_ARGS)
> +		return -1;
> +	return arg - MAX_BPF_FUNC_REG_ARGS - 1;
> +}
> +
>  static const char *btf_type_name(const struct btf *btf, u32 id)
>  {
>  	return btf_name_by_offset(btf, btf_type_by_id(btf, id)->name_off);
> @@ -8161,6 +8170,8 @@ static const char *bpf_diag_reg_type_plain(struct bpf_verifier_env *env,
>  			return "a nullable memory pointer";
>  		return "a memory pointer";
>  	case PTR_TO_BTF_ID:
> +		if (type_may_be_null(type))
> +			return "a nullable kernel object pointer";

Nit: move this hunk such that bpf_diag_reg_type_plain() is defined in
     a single patch?

>  		if (type_is_non_owning_ref(type))
>  			return "a borrowed allocated object pointer";
>  		if (type_is_ptr_alloc_obj(type))
> @@ -8173,6 +8184,60 @@ static const char *bpf_diag_reg_type_plain(struct bpf_verifier_env *env,

[...]

> +static void diag_call_arg_fmt(struct bpf_verifier_env *env,
> +			      u32 insn_idx, argno_t argno,
> +			      const char *call_name,
> +			      const char *suggestion,
> +			      const char *fmt, ...) __printf(6, 7);

Nit: Is this prototype necessary?

> +static void diag_call_arg_fmt(struct bpf_verifier_env *env,
> +			      u32 insn_idx, argno_t argno,
> +			      const char *call_name,
> +			      const char *suggestion,
> +			      const char *fmt, ...)
> +{
> +	size_t size;
> +	va_list args;
> +	char *reason;
> +
> +	reason = bpf_diag_scratch_buf(env, 0, &size);

An alternative to juggling the scratch slot numbers
might be some kind of:

  char *bpf_diag_fmt(env, fmt_string, ...)

that would do the vaprintf behind the scene and accumulate
the buffers in env->diag, to be freed after reporting.

> +	if (reason) {
> +		va_start(args, fmt);
> +		vscnprintf(reason, size, fmt, args);
> +		va_end(args);
> +	} else {
> +		reason = "";
> +	}
> +
> +	bpf_diag_report_call_arg(env, insn_idx, argno, call_name, reason,
> +				 suggestion);
> +}
> +
>  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,
> @@ -8226,6 +8291,32 @@ 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]));
> +	{
> +		size_t expected_size;
> +		char *expected_buf;
> +		const char *call_name;
> +		int len = 0;
> +
> +		expected_buf = bpf_diag_scratch_buf(env, 1,
> +						    &expected_size);
> +		if (expected_buf) {
> +			expected_buf[0] = '\0';
> +			for (j = 0; j < i && len < expected_size; j++)
> +				len += scnprintf(expected_buf + len,
> +						 expected_size - len,
> +						 "%s%s", j ? ", " : "",
> +						 reg_type_str(env, compatible->types[j]));

Nit: this is a bit ugly, maybe hide it as a utility function?

> +		} else {
> +			expected_buf = "";
> +		}
> +
> +		call_name = meta->func_id ? func_id_name(meta->func_id) : "callee";

Nit: I'd avoid the defensive meta->func_id ? ... here.

> +		diag_call_arg_fmt(env, env->insn_idx, argno, call_name,
> +				  "Pass a value with one of the accepted pointer or scalar types for this call.",
> +				  "it has type %s, but this argument accepts %s",
> +				  bpf_diag_reg_type_plain(env, reg->type),
> +				  expected_buf);
> +	}
>  	return -EACCES;
>  
>  found:

[...]

> @@ -11808,9 +11908,20 @@ 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)) {
> +		const char *expected_type;
> +
> +		expected_type = bpf_diag_format_btf_type_scratch(env,
> +								 1,
> +								 meta->btf,
> +								 ref_id);

Nit: isn't this exactly the diag_btf_type_name()?
     (and in a few places below).

>  		verbose(env, "%s pointer type %s %s must point to %sscalar, or struct with scalar\n",
>  			reg_arg_name(env, argno),
>  			btf_type_str(ref_t), ref_tname, arg_mem_size ? "void, " : "");
> +		diag_call_arg_fmt(env, env->insn_idx, argno, meta->func_name,
> +				  "Pass a verifier-tracked pointer to the expected kernel object type, not a pointer to stack storage or another memory buffer.",
> +				  "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));
>  		return -EINVAL;
>  	}
>  	return arg_mem_size ? KF_ARG_PTR_TO_MEM_SIZE : KF_ARG_PTR_TO_MEM;

[...]

  parent reply	other threads:[~2026-06-25  6:05 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-06-19 20:59 [PATCH bpf-next v2 00/17] Redesign Verification Errors Kumar Kartikeya Dwivedi
2026-06-19 20:59 ` [PATCH bpf-next v2 01/17] bpf: Add verifier diagnostics report helpers Kumar Kartikeya Dwivedi
2026-06-19 21:09   ` sashiko-bot
2026-06-22 23:47   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 02/17] bpf: Add source and instruction diagnostic context Kumar Kartikeya Dwivedi
2026-06-19 21:46   ` bot+bpf-ci
2026-06-23 20:34   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 03/17] bpf: Add verifier diagnostic event log Kumar Kartikeya Dwivedi
2026-06-19 21:46   ` bot+bpf-ci
2026-06-23 21:20   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 04/17] bpf: Prune verifier diagnostics on backtracking Kumar Kartikeya Dwivedi
2026-06-19 21:46   ` bot+bpf-ci
2026-06-23 21:57   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 05/17] bpf: Track verifier register diagnostic events Kumar Kartikeya Dwivedi
2026-06-19 21:18   ` sashiko-bot
2026-06-19 23:35   ` Alexei Starovoitov
2026-06-24 19:17   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 06/17] bpf: Track verifier reference " Kumar Kartikeya Dwivedi
2026-06-24 19:49   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 07/17] bpf: Track verifier context " Kumar Kartikeya Dwivedi
2026-06-19 21:13   ` sashiko-bot
2026-06-19 21:19     ` Kumar Kartikeya Dwivedi
2026-06-19 21:46   ` bot+bpf-ci
2026-06-24 21:21   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 08/17] bpf: Report Register Type Safety errors Kumar Kartikeya Dwivedi
2026-06-24 23:55   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 09/17] bpf: Report Memory Safety bounds errors Kumar Kartikeya Dwivedi
2026-06-19 21:46   ` bot+bpf-ci
2026-06-19 23:40   ` Alexei Starovoitov
2026-06-25  1:30   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 10/17] bpf: Report Resource Lifetime reference leaks Kumar Kartikeya Dwivedi
2026-06-19 21:12   ` sashiko-bot
2026-06-19 23:42   ` Alexei Starovoitov
2026-06-25  1:58   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 11/17] bpf: Report Call Type Safety argument errors Kumar Kartikeya Dwivedi
2026-06-19 21:47   ` bot+bpf-ci
2026-06-25  6:05   ` Eduard Zingerman [this message]
2026-06-19 20:59 ` [PATCH bpf-next v2 12/17] bpf: Report Execution Context Safety errors Kumar Kartikeya Dwivedi
2026-06-19 21:19   ` sashiko-bot
2026-06-19 23:44   ` Alexei Starovoitov
2026-06-19 20:59 ` [PATCH bpf-next v2 13/17] bpf: Report Program Structure CFG errors Kumar Kartikeya Dwivedi
2026-06-25  0:47   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 14/17] bpf: Report Policy helper and kfunc errors Kumar Kartikeya Dwivedi
2026-06-25  0:36   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 15/17] bpf: Report Verifier Limit errors Kumar Kartikeya Dwivedi
2026-06-19 20:59 ` [PATCH bpf-next v2 16/17] bpf: Report Verifier Internal errors Kumar Kartikeya Dwivedi
2026-06-25  0:26   ` Eduard Zingerman
2026-06-19 20:59 ` [PATCH bpf-next v2 17/17] bpf: Gate verifier diagnostics on log level Kumar Kartikeya Dwivedi

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=40a445e92a18ba6325081ae2be784bf0bdd45052.camel@gmail.com \
    --to=eddyz87@gmail.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=emil@etsalapatis.com \
    --cc=kernel-team@meta.com \
    --cc=kkd@meta.com \
    --cc=memxor@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.