BPF List
 help / color / mirror / Atom feed
From: Eduard Zingerman <eddyz87@gmail.com>
To: Daniel Xu <dxu@dxuuu.xyz>,
	daniel@iogearbox.net, andrii@kernel.org,  ast@kernel.org
Cc: john.fastabend@gmail.com, martin.lau@linux.dev, song@kernel.org,
	 yonghong.song@linux.dev, kpsingh@kernel.org, sdf@fomichev.me,
	haoluo@google.com, 	jolsa@kernel.org, bpf@vger.kernel.org,
	linux-kernel@vger.kernel.org, 	mhartmay@linux.ibm.com,
	iii@linux.ibm.com
Subject: Re: [PATCH bpf-next 1/3] bpf: verifier: Do not extract constant map keys for irrelevant maps
Date: Mon, 03 Feb 2025 10:45:35 -0800	[thread overview]
Message-ID: <084abedc8ec36ffe77f97531c0bcebc291547415.camel@gmail.com> (raw)
In-Reply-To: <ebbf8edf871a6543425b75bb659400221bd28275.1738439839.git.dxu@dxuuu.xyz>

On Sat, 2025-02-01 at 12:58 -0700, Daniel Xu wrote:
> Previously, we were trying to extract constant map keys for all
> bpf_map_lookup_elem(), regardless of map type. This is an issue if the
> map has a u64 key and the value is very high, as it can be interpreted
> as a negative signed value. This in turn is treated as an error value by
> check_func_arg() which causes a valid program to be incorrectly
> rejected.
> 
> Fix by only extracting constant map keys for relevant maps. See next
> commit for an example via selftest.
> 
> Reported-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> Reported-by: Ilya Leoshkevich <iii@linux.ibm.com>
> Tested-by: Marc Hartmayer <mhartmay@linux.ibm.com>
> Signed-off-by: Daniel Xu <dxu@dxuuu.xyz>

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

Nit:
  would be good if commit message said something along the lines:
  ... the fix works because nullness elision is only allowed for
      {PERCPU_}ARRAY maps, and keys for these are within u32 range ...

> ---
>  kernel/bpf/verifier.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)
> 
> diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c
> index 9971c03adfd5..e9176a5ce215 100644
> --- a/kernel/bpf/verifier.c
> +++ b/kernel/bpf/verifier.c
> @@ -9206,6 +9206,8 @@ static s64 get_constant_map_key(struct bpf_verifier_env *env,
>  	return reg->var_off.value;
>  }
>  
> +static bool can_elide_value_nullness(enum bpf_map_type type);
> +
>  static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>  			  struct bpf_call_arg_meta *meta,
>  			  const struct bpf_func_proto *fn,
> @@ -9354,9 +9356,11 @@ static int check_func_arg(struct bpf_verifier_env *env, u32 arg,
>  		err = check_helper_mem_access(env, regno, key_size, BPF_READ, false, NULL);
>  		if (err)
>  			return err;
> -		meta->const_map_key = get_constant_map_key(env, reg, key_size);
> -		if (meta->const_map_key < 0 && meta->const_map_key != -EOPNOTSUPP)
> -			return meta->const_map_key;
> +		if (can_elide_value_nullness(meta->map_ptr->map_type)) {
> +			meta->const_map_key = get_constant_map_key(env, reg, key_size);
> +			if (meta->const_map_key < 0 && meta->const_map_key != -EOPNOTSUPP)
> +				return meta->const_map_key;
> +		}
>  		break;
>  	case ARG_PTR_TO_MAP_VALUE:
>  		if (type_may_be_null(arg_type) && register_is_null(reg))



  reply	other threads:[~2025-02-03 18:45 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-02-01 19:58 [PATCH bpf-next 0/3] bpf: Some fixes for nullness elision Daniel Xu
2025-02-01 19:58 ` [PATCH bpf-next 1/3] bpf: verifier: Do not extract constant map keys for irrelevant maps Daniel Xu
2025-02-03 18:45   ` Eduard Zingerman [this message]
2025-02-04  9:09     ` Daniel Xu
2025-02-01 19:58 ` [PATCH bpf-next 2/3] bpf: selftests: Test constant key extraction on " Daniel Xu
2025-02-03 18:48   ` Eduard Zingerman
2025-02-01 19:58 ` [PATCH bpf-next 3/3] bpf: verifier: Disambiguate get_constant_map_key() errors Daniel Xu
2025-02-03 18:49   ` Eduard Zingerman

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=084abedc8ec36ffe77f97531c0bcebc291547415.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=dxu@dxuuu.xyz \
    --cc=haoluo@google.com \
    --cc=iii@linux.ibm.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=mhartmay@linux.ibm.com \
    --cc=sdf@fomichev.me \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox