bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: Andrii Nakryiko <andriin@fb.com>,
	"bpf@vger.kernel.org" <bpf@vger.kernel.org>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	Alexei Starovoitov <ast@fb.com>,
	"daniel@iogearbox.net" <daniel@iogearbox.net>
Cc: "andrii.nakryiko@gmail.com" <andrii.nakryiko@gmail.com>,
	Kernel Team <Kernel-team@fb.com>, Martin Lau <kafai@fb.com>
Subject: Re: [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic
Date: Fri, 12 Jul 2019 05:59:27 +0000	[thread overview]
Message-ID: <ad29872e-a127-f21e-5581-03df5a388a55@fb.com> (raw)
In-Reply-To: <20190711065307.2425636-2-andriin@fb.com>



On 7/10/19 11:53 PM, Andrii Nakryiko wrote:
> BTF verifier has a size resolution bug which in some circumstances leads to
> invalid size resolution for, e.g., TYPEDEF modifier.  This happens if we have
> [1] PTR -> [2] TYPEDEF -> [3] ARRAY, in which case due to being in pointer
> context ARRAY size won't be resolved (because for pointer it doesn't matter, so
> it's a sink in pointer context), but it will be permanently remembered as zero
> for TYPEDEF and TYPEDEF will be marked as RESOLVED. Eventually ARRAY size will
> be resolved correctly, but TYPEDEF resolved_size won't be updated anymore.
> This, subsequently, will lead to erroneous map creation failure, if that
> TYPEDEF is specified as either key or value, as key_size/value_size won't
> correspond to resolved size of TYPEDEF (kernel will believe it's zero).
> 
> Note, that if BTF was ordered as [1] ARRAY <- [2] TYPEDEF <- [3] PTR, this
> won't be a problem, as by the time we get to TYPEDEF, ARRAY's size is already
> calculated and stored.
> 
> This bug manifests itself in rejecting BTF-defined maps that use array
> typedef as a value type:
> 
> typedef int array_t[16];
> 
> struct {
>      __uint(type, BPF_MAP_TYPE_ARRAY);
>      __type(value, array_t); /* i.e., array_t *value; */
> } test_map SEC(".maps");
> 
> The fix consists on not relying on modifier's resolved_size and instead using
> modifier's resolved_id (type ID for "concrete" type to which modifier
> eventually resolves) and doing size determination for that resolved type. This
> allow to preserve existing "early DFS termination" logic for PTR or
> STRUCT_OR_ARRAY contexts, but still do correct size determination for modifier
> types.
> 
> Fixes: eb3f595dab40 ("bpf: btf: Validate type reference")
> Cc: Martin KaFai Lau <kafai@fb.com>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
> ---
>   kernel/bpf/btf.c | 14 ++++++++++----
>   1 file changed, 10 insertions(+), 4 deletions(-)
> 
> diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
> index cad09858a5f2..22fe8b155e51 100644
> --- a/kernel/bpf/btf.c
> +++ b/kernel/bpf/btf.c
> @@ -1073,11 +1073,18 @@ const struct btf_type *btf_type_id_size(const struct btf *btf,
>   				 !btf_type_is_var(size_type)))
>   			return NULL;
>   
> -		size = btf->resolved_sizes[size_type_id];
>   		size_type_id = btf->resolved_ids[size_type_id];
>   		size_type = btf_type_by_id(btf, size_type_id);
>   		if (btf_type_nosize_or_null(size_type))
>   			return NULL;
> +		else if (btf_type_has_size(size_type))
> +			size = size_type->size;
> +		else if (btf_type_is_array(size_type))
> +			size = btf->resolved_sizes[size_type_id];
> +		else if (btf_type_is_ptr(size_type))
> +			size = sizeof(void *);
> +		else
> +			return NULL;

Looks good to me. Not sure whether we need to do any adjustment for
var kind or not. Maybe we can do similar change in btf_var_resolve()
to btf_modifier_resolve()? But I do not think it impacts correctness 
similar to btf_modifier_resolve() below as you changed 
btf_type_id_size() implementation in the above.

>   	}
>   
>   	*type_id = size_type_id;
> @@ -1602,7 +1609,6 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
>   	const struct btf_type *next_type;
>   	u32 next_type_id = t->type;
>   	struct btf *btf = env->btf;
> -	u32 next_type_size = 0;
>   
>   	next_type = btf_type_by_id(btf, next_type_id);
>   	if (!next_type || btf_type_is_resolve_source_only(next_type)) {
> @@ -1620,7 +1626,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
>   	 * save us a few type-following when we use it later (e.g. in
>   	 * pretty print).
>   	 */
> -	if (!btf_type_id_size(btf, &next_type_id, &next_type_size)) {
> +	if (!btf_type_id_size(btf, &next_type_id, NULL)) {
>   		if (env_type_is_resolved(env, next_type_id))
>   			next_type = btf_type_id_resolve(btf, &next_type_id);
>   
> @@ -1633,7 +1639,7 @@ static int btf_modifier_resolve(struct btf_verifier_env *env,
>   		}
>   	}
>   
> -	env_stack_pop_resolved(env, next_type_id, next_type_size);
> +	env_stack_pop_resolved(env, next_type_id, 0);
>   
>   	return 0;
>   }
> 

  reply	other threads:[~2019-07-12  6:00 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-07-11  6:53 [PATCH v2 bpf-next 0/3] fix BTF verification size resolution Andrii Nakryiko
2019-07-11  6:53 ` [PATCH v2 bpf-next 1/3] bpf: fix BTF verifier size resolution logic Andrii Nakryiko
2019-07-12  5:59   ` Yonghong Song [this message]
2019-07-12 15:36     ` Andrii Nakryiko
2019-07-12 15:47       ` Yonghong Song
2019-07-12 17:10         ` Andrii Nakryiko
2019-07-11  6:53 ` [PATCH v2 bpf-next 2/3] selftests/bpf: add trickier size resolution tests Andrii Nakryiko
2019-07-11  6:53 ` [PATCH v2 bpf-next 3/3] selftests/bpf: use typedef'ed arrays as map values Andrii Nakryiko
2019-07-12  6:03 ` [PATCH v2 bpf-next 0/3] fix BTF verification size resolution Yonghong Song
2019-07-12 12:59   ` Daniel Borkmann
2019-07-12 15:42     ` Andrii Nakryiko
2019-07-12 15:51       ` Daniel Borkmann

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=ad29872e-a127-f21e-5581-03df5a388a55@fb.com \
    --to=yhs@fb.com \
    --cc=Kernel-team@fb.com \
    --cc=andrii.nakryiko@gmail.com \
    --cc=andriin@fb.com \
    --cc=ast@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kafai@fb.com \
    --cc=netdev@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).