bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts
@ 2020-03-12 18:50 Andrii Nakryiko
  2020-03-12 19:58 ` [Potential Spoof] " Martin KaFai Lau
  2020-03-12 23:16 ` Daniel Borkmann
  0 siblings, 2 replies; 3+ messages in thread
From: Andrii Nakryiko @ 2020-03-12 18:50 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko, Quentin Monnet

Needs for application BTF being present differs between user-space libbpf needs and kernel
needs. Currently, BTF is mandatory only in kernel only when BPF application is
using STRUCT_OPS. While libbpf itself relies more heavily on presense of BTF:
  - for BTF-defined maps;
  - for Kconfig externs;
  - for STRUCT_OPS as well.

Thus, checks for presence and validness of bpf_object's BPF needs to be
performed separately, which is patch does.

Fixes: 5327644614a1 ("libbpf: Relax check whether BTF is mandatory")
Reported-by: Michal Rostecki <mrostecki@opensuse.org>
Cc: Quentin Monnet <quentin@isovalent.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 223be01dc466..1a787a2faf58 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -2284,9 +2284,16 @@ static void bpf_object__sanitize_btf_ext(struct bpf_object *obj)
 	}
 }
 
-static bool bpf_object__is_btf_mandatory(const struct bpf_object *obj)
+static bool libbpf_needs_btf(const struct bpf_object *obj)
 {
-	return obj->efile.st_ops_shndx >= 0 || obj->nr_extern > 0;
+	return obj->efile.btf_maps_shndx >= 0 ||
+	       obj->efile.st_ops_shndx >= 0 ||
+	       obj->nr_extern > 0;
+}
+
+static bool kernel_needs_btf(const struct bpf_object *obj)
+{
+	return obj->efile.st_ops_shndx >= 0;
 }
 
 static int bpf_object__init_btf(struct bpf_object *obj,
@@ -2322,7 +2329,7 @@ static int bpf_object__init_btf(struct bpf_object *obj,
 		}
 	}
 out:
-	if (err && bpf_object__is_btf_mandatory(obj)) {
+	if (err && libbpf_needs_btf(obj)) {
 		pr_warn("BTF is required, but is missing or corrupted.\n");
 		return err;
 	}
@@ -2346,7 +2353,7 @@ static int bpf_object__finalize_btf(struct bpf_object *obj)
 	btf_ext__free(obj->btf_ext);
 	obj->btf_ext = NULL;
 
-	if (bpf_object__is_btf_mandatory(obj)) {
+	if (libbpf_needs_btf(obj)) {
 		pr_warn("BTF is required, but is missing or corrupted.\n");
 		return -ENOENT;
 	}
@@ -2410,7 +2417,7 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
 			obj->btf_ext = NULL;
 		}
 
-		if (bpf_object__is_btf_mandatory(obj))
+		if (kernel_needs_btf(obj))
 			return err;
 	}
 	return 0;
-- 
2.17.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [Potential Spoof] [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts
  2020-03-12 18:50 [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts Andrii Nakryiko
@ 2020-03-12 19:58 ` Martin KaFai Lau
  2020-03-12 23:16 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Martin KaFai Lau @ 2020-03-12 19:58 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, netdev, ast, daniel, andrii.nakryiko, kernel-team, Quentin Monnet

On Thu, Mar 12, 2020 at 11:50:33AM -0700, Andrii Nakryiko wrote:
> Needs for application BTF being present differs between user-space libbpf needs and kernel
Nit. This line looks too long for commit message.

> needs. Currently, BTF is mandatory only in kernel only when BPF application is
> using STRUCT_OPS. While libbpf itself relies more heavily on presense of BTF:
>   - for BTF-defined maps;
>   - for Kconfig externs;
>   - for STRUCT_OPS as well.
> 
> Thus, checks for presence and validness of bpf_object's BPF needs to be
> performed separately, which is patch does.
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts
  2020-03-12 18:50 [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts Andrii Nakryiko
  2020-03-12 19:58 ` [Potential Spoof] " Martin KaFai Lau
@ 2020-03-12 23:16 ` Daniel Borkmann
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Borkmann @ 2020-03-12 23:16 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast
  Cc: andrii.nakryiko, kernel-team, Quentin Monnet

On 3/12/20 7:50 PM, Andrii Nakryiko wrote:
> Needs for application BTF being present differs between user-space libbpf needs and kernel
> needs. Currently, BTF is mandatory only in kernel only when BPF application is
> using STRUCT_OPS. While libbpf itself relies more heavily on presense of BTF:
>    - for BTF-defined maps;
>    - for Kconfig externs;
>    - for STRUCT_OPS as well.
> 
> Thus, checks for presence and validness of bpf_object's BPF needs to be
> performed separately, which is patch does.
> 
> Fixes: 5327644614a1 ("libbpf: Relax check whether BTF is mandatory")
> Reported-by: Michal Rostecki <mrostecki@opensuse.org>
> Cc: Quentin Monnet <quentin@isovalent.com>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Applied, thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-03-12 23:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-12 18:50 [PATCH bpf-next] libbpf: split BTF presence checks into libbpf- and kernel-specific parts Andrii Nakryiko
2020-03-12 19:58 ` [Potential Spoof] " Martin KaFai Lau
2020-03-12 23:16 ` Daniel Borkmann

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).