public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Martin KaFai Lau <martin.lau@linux.dev>
To: Andrii Nakryiko <andrii@kernel.org>
Cc: kernel-team@meta.com, bpf@vger.kernel.org, ast@kernel.org,
	daniel@iogearbox.net, martin.lau@kernel.org
Subject: Re: [PATCH bpf-next] libbpf: add basic BTF sanity validation
Date: Wed, 23 Aug 2023 22:04:17 -0700	[thread overview]
Message-ID: <f4933856-ce85-0611-409d-bd62240d15af@linux.dev> (raw)
In-Reply-To: <20230823234426.2506685-1-andrii@kernel.org>

On 8/23/23 4:44 PM, Andrii Nakryiko wrote:
> +/* Validate basic sanity of BTF. It's intentionally less thorough than
> + * kernel's validation and validates only properties of BTF that libbpf relies
> + * on to be correct (e.g., valid type IDs, valid string offsets, etc)
> + */
> +int btf_sanity_check(const struct btf *btf)
> +{
> +	const struct btf_type *t;
> +	__u32 i, n = btf__type_cnt(btf);
> +	int err;
> +
> +	for (i = 1; i < n; i++) {
> +		t = btf_type_by_id(btf, i);
> +		err = btf_validate_type(btf, t, i);
> +		if (err)
> +			return err;
> +	}
> +	return 0;
> +}
> +
>   static void *btf_get_raw_data(const struct btf *btf, __u32 *size, bool swap_endian);
>   
>   int btf_load_into_kernel(struct btf *btf, char *log_buf, size_t log_sz, __u32 log_level)
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 4c3967d94b6d..71a3c768d9af 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -2833,6 +2833,13 @@ static int bpf_object__init_btf(struct bpf_object *obj,
>   			pr_warn("Error loading ELF section %s: %d.\n", BTF_ELF_SEC, err);
>   			goto out;
>   		}
> +		err = btf_sanity_check(obj->btf);

Should btf_sanity_check() be called in btf_parse_type_sec() instead such that 
btf__parse_elf() can also have sanity check?

  reply	other threads:[~2023-08-24  5:04 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-23 23:44 [PATCH bpf-next] libbpf: add basic BTF sanity validation Andrii Nakryiko
2023-08-24  5:04 ` Martin KaFai Lau [this message]
2023-08-24 16:20   ` Andrii Nakryiko
2023-08-24 12:10 ` Eduard Zingerman
2023-08-24 16:21   ` Andrii Nakryiko

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=f4933856-ce85-0611-409d-bd62240d15af@linux.dev \
    --to=martin.lau@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@meta.com \
    --cc=martin.lau@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