bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf] libbpf: fix SIGSEGV when BTF loading fails, but .BTF.ext exists
@ 2019-07-19 19:32 Andrii Nakryiko
  2019-07-19 19:38 ` Alexei Starovoitov
  0 siblings, 1 reply; 2+ messages in thread
From: Andrii Nakryiko @ 2019-07-19 19:32 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel, rdna
  Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

In case when BTF loading fails despite sanitization, but BPF object has
.BTF.ext loaded as well, we free and null obj->btf, but not
obj->btf_ext. This leads to an attempt to relocate .BTF.ext later on
during bpf_object__load(), which assumes obj->btf is present. This leads
to SIGSEGV on null pointer access. Fix bug by freeing and nulling
obj->btf_ext as well.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 794dd5064ae8..87168f21ef43 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -1500,6 +1500,12 @@ static int bpf_object__sanitize_and_load_btf(struct bpf_object *obj)
 			   BTF_ELF_SEC, err);
 		btf__free(obj->btf);
 		obj->btf = NULL;
+		/* btf_ext can't exist without btf, so free it as well */
+		if (obj->btf_ext) {
+			btf_ext__free(obj->btf_ext);
+			obj->btf_ext = NULL;
+		}
+
 		if (bpf_object__is_btf_mandatory(obj))
 			return err;
 	}
-- 
2.17.1


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

* Re: [PATCH bpf] libbpf: fix SIGSEGV when BTF loading fails, but .BTF.ext exists
  2019-07-19 19:32 [PATCH bpf] libbpf: fix SIGSEGV when BTF loading fails, but .BTF.ext exists Andrii Nakryiko
@ 2019-07-19 19:38 ` Alexei Starovoitov
  0 siblings, 0 replies; 2+ messages in thread
From: Alexei Starovoitov @ 2019-07-19 19:38 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, daniel, Andrey Ignatov
  Cc: andrii.nakryiko, Kernel Team

On 7/19/19 12:32 PM, Andrii Nakryiko wrote:
> In case when BTF loading fails despite sanitization, but BPF object has
> .BTF.ext loaded as well, we free and null obj->btf, but not
> obj->btf_ext. This leads to an attempt to relocate .BTF.ext later on
> during bpf_object__load(), which assumes obj->btf is present. This leads
> to SIGSEGV on null pointer access. Fix bug by freeing and nulling
> obj->btf_ext as well.
> 
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>

Applied. Thanks

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

end of thread, other threads:[~2019-07-19 19:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-07-19 19:32 [PATCH bpf] libbpf: fix SIGSEGV when BTF loading fails, but .BTF.ext exists Andrii Nakryiko
2019-07-19 19:38 ` Alexei Starovoitov

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