bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf
@ 2019-11-07  2:08 Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 1/5] libbpf: fix memory leak/double free issue Andrii Nakryiko
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Github's mirror of libbpf got LGTM and Coverity statis analysis running
against it and spotted few real bugs and few potential issues. This patch
series fixes found issues.

Andrii Nakryiko (5):
  libbpf: fix memory leak/double free issue
  libbpf: fix potential overflow issue
  libbpf: fix another potential overflow issue in bpf_prog_linfo
  libbpf: make btf__resolve_size logic always check size error condition
  libbpf: improve handling of corrupted ELF during map initialization

 tools/lib/bpf/bpf.c            |  2 +-
 tools/lib/bpf/bpf_prog_linfo.c | 14 +++++++-------
 tools/lib/bpf/btf.c            |  3 +--
 tools/lib/bpf/libbpf.c         |  6 +++---
 4 files changed, 12 insertions(+), 13 deletions(-)

-- 
2.17.1


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

* [PATCH bpf-next 1/5] libbpf: fix memory leak/double free issue
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
@ 2019-11-07  2:08 ` Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 2/5] libbpf: fix potential overflow issue Andrii Nakryiko
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Coverity scan against Github libbpf code found the issue of not freeing memory and
leaving already freed memory still referenced from bpf_program. Fix it by
re-assigning successfully reallocated memory sooner.

Fixes: 2993e0515bb4 ("tools/bpf: add support to read .BTF.ext sections")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index be4af95d5a2c..3ef73a214592 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -3523,6 +3523,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
 			pr_warn("oom in prog realloc\n");
 			return -ENOMEM;
 		}
+		prog->insns = new_insn;
 
 		if (obj->btf_ext) {
 			err = bpf_program_reloc_btf_ext(prog, obj,
@@ -3534,7 +3535,6 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj,
 
 		memcpy(new_insn + prog->insns_cnt, text->insns,
 		       text->insns_cnt * sizeof(*insn));
-		prog->insns = new_insn;
 		prog->main_prog_cnt = prog->insns_cnt;
 		prog->insns_cnt = new_cnt;
 		pr_debug("added %zd insn from %s to prog %s\n",
-- 
2.17.1


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

* [PATCH bpf-next 2/5] libbpf: fix potential overflow issue
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 1/5] libbpf: fix memory leak/double free issue Andrii Nakryiko
@ 2019-11-07  2:08 ` Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 3/5] libbpf: fix another potential overflow issue in bpf_prog_linfo Andrii Nakryiko
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Fix a potential overflow issue found by LGTM analysis, based on Github libbpf
source code.

Fixes: 3d65014146c6 ("bpf: libbpf: Add btf_line_info support to libbpf")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/bpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/lib/bpf/bpf.c b/tools/lib/bpf/bpf.c
index ca0d635b1d5e..b3e3e99a0f28 100644
--- a/tools/lib/bpf/bpf.c
+++ b/tools/lib/bpf/bpf.c
@@ -189,7 +189,7 @@ static void *
 alloc_zero_tailing_info(const void *orecord, __u32 cnt,
 			__u32 actual_rec_size, __u32 expected_rec_size)
 {
-	__u64 info_len = actual_rec_size * cnt;
+	__u64 info_len = (__u64)actual_rec_size * cnt;
 	void *info, *nrecord;
 	int i;
 
-- 
2.17.1


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

* [PATCH bpf-next 3/5] libbpf: fix another potential overflow issue in bpf_prog_linfo
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 1/5] libbpf: fix memory leak/double free issue Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 2/5] libbpf: fix potential overflow issue Andrii Nakryiko
@ 2019-11-07  2:08 ` Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 4/5] libbpf: make btf__resolve_size logic always check size error condition Andrii Nakryiko
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Fix few issues found by Coverity and LGTM.

Fixes: b053b439b72a ("bpf: libbpf: bpftool: Print bpf_line_info during prog dump")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/bpf_prog_linfo.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/tools/lib/bpf/bpf_prog_linfo.c b/tools/lib/bpf/bpf_prog_linfo.c
index 8c67561c93b0..3ed1a27b5f7c 100644
--- a/tools/lib/bpf/bpf_prog_linfo.c
+++ b/tools/lib/bpf/bpf_prog_linfo.c
@@ -101,6 +101,7 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 {
 	struct bpf_prog_linfo *prog_linfo;
 	__u32 nr_linfo, nr_jited_func;
+	__u64 data_sz;
 
 	nr_linfo = info->nr_line_info;
 
@@ -122,11 +123,11 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 	/* Copy xlated line_info */
 	prog_linfo->nr_linfo = nr_linfo;
 	prog_linfo->rec_size = info->line_info_rec_size;
-	prog_linfo->raw_linfo = malloc(nr_linfo * prog_linfo->rec_size);
+	data_sz = (__u64)nr_linfo * prog_linfo->rec_size;
+	prog_linfo->raw_linfo = malloc(data_sz);
 	if (!prog_linfo->raw_linfo)
 		goto err_free;
-	memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info,
-	       nr_linfo * prog_linfo->rec_size);
+	memcpy(prog_linfo->raw_linfo, (void *)(long)info->line_info, data_sz);
 
 	nr_jited_func = info->nr_jited_ksyms;
 	if (!nr_jited_func ||
@@ -142,13 +143,12 @@ struct bpf_prog_linfo *bpf_prog_linfo__new(const struct bpf_prog_info *info)
 	/* Copy jited_line_info */
 	prog_linfo->nr_jited_func = nr_jited_func;
 	prog_linfo->jited_rec_size = info->jited_line_info_rec_size;
-	prog_linfo->raw_jited_linfo = malloc(nr_linfo *
-					     prog_linfo->jited_rec_size);
+	data_sz = (__u64)nr_linfo * prog_linfo->jited_rec_size;
+	prog_linfo->raw_jited_linfo = malloc(data_sz);
 	if (!prog_linfo->raw_jited_linfo)
 		goto err_free;
 	memcpy(prog_linfo->raw_jited_linfo,
-	       (void *)(long)info->jited_line_info,
-	       nr_linfo * prog_linfo->jited_rec_size);
+	       (void *)(long)info->jited_line_info, data_sz);
 
 	/* Number of jited_line_info per jited func */
 	prog_linfo->nr_jited_linfo_per_func = malloc(nr_jited_func *
-- 
2.17.1


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

* [PATCH bpf-next 4/5] libbpf: make btf__resolve_size logic always check size error condition
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
                   ` (2 preceding siblings ...)
  2019-11-07  2:08 ` [PATCH bpf-next 3/5] libbpf: fix another potential overflow issue in bpf_prog_linfo Andrii Nakryiko
@ 2019-11-07  2:08 ` Andrii Nakryiko
  2019-11-07  2:08 ` [PATCH bpf-next 5/5] libbpf: improve handling of corrupted ELF during map initialization Andrii Nakryiko
  2019-11-07 16:35 ` [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Daniel Borkmann
  5 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

Perform size check always in btf__resolve_size. Makes the logic a bit more
robust against corrupted BTF and silences LGTM/Coverity complaining about
always true (size < 0) check.

Fixes: 69eaab04c675 ("btf: extract BTF type size calculation")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/btf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index d72e9a79dce1..86a1847e4a9f 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -269,10 +269,9 @@ __s64 btf__resolve_size(const struct btf *btf, __u32 type_id)
 		t = btf__type_by_id(btf, type_id);
 	}
 
+done:
 	if (size < 0)
 		return -EINVAL;
-
-done:
 	if (nelems && size > UINT32_MAX / nelems)
 		return -E2BIG;
 
-- 
2.17.1


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

* [PATCH bpf-next 5/5] libbpf: improve handling of corrupted ELF during map initialization
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
                   ` (3 preceding siblings ...)
  2019-11-07  2:08 ` [PATCH bpf-next 4/5] libbpf: make btf__resolve_size logic always check size error condition Andrii Nakryiko
@ 2019-11-07  2:08 ` Andrii Nakryiko
  2019-11-07 16:35 ` [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Daniel Borkmann
  5 siblings, 0 replies; 7+ messages in thread
From: Andrii Nakryiko @ 2019-11-07  2:08 UTC (permalink / raw)
  To: bpf, netdev, ast, daniel; +Cc: andrii.nakryiko, kernel-team, Andrii Nakryiko

If we get ELF file with "maps" section, but no symbols pointing to it, we'll
end up with division by zero. Add check against this situation and exit early
with error. Found by Coverity scan against Github libbpf sources.

Fixes: bf82927125dd ("libbpf: refactor map initialization")
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index 3ef73a214592..fde6cb3e5d41 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -956,13 +956,13 @@ static int bpf_object__init_user_maps(struct bpf_object *obj, bool strict)
 	pr_debug("maps in %s: %d maps in %zd bytes\n",
 		 obj->path, nr_maps, data->d_size);
 
-	map_def_sz = data->d_size / nr_maps;
-	if (!data->d_size || (data->d_size % nr_maps) != 0) {
+	if (!data->d_size || nr_maps == 0 || (data->d_size % nr_maps) != 0) {
 		pr_warn("unable to determine map definition size "
 			"section %s, %d maps in %zd bytes\n",
 			obj->path, nr_maps, data->d_size);
 		return -EINVAL;
 	}
+	map_def_sz = data->d_size / nr_maps;
 
 	/* Fill obj->maps using data in "maps" section.  */
 	for (i = 0; i < nr_syms; i++) {
-- 
2.17.1


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

* Re: [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf
  2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
                   ` (4 preceding siblings ...)
  2019-11-07  2:08 ` [PATCH bpf-next 5/5] libbpf: improve handling of corrupted ELF during map initialization Andrii Nakryiko
@ 2019-11-07 16:35 ` Daniel Borkmann
  5 siblings, 0 replies; 7+ messages in thread
From: Daniel Borkmann @ 2019-11-07 16:35 UTC (permalink / raw)
  To: Andrii Nakryiko, bpf, netdev, ast; +Cc: andrii.nakryiko, kernel-team

On 11/7/19 3:08 AM, Andrii Nakryiko wrote:
> Github's mirror of libbpf got LGTM and Coverity statis analysis running
> against it and spotted few real bugs and few potential issues. This patch
> series fixes found issues.
> 
> Andrii Nakryiko (5):
>    libbpf: fix memory leak/double free issue
>    libbpf: fix potential overflow issue
>    libbpf: fix another potential overflow issue in bpf_prog_linfo
>    libbpf: make btf__resolve_size logic always check size error condition
>    libbpf: improve handling of corrupted ELF during map initialization
> 
>   tools/lib/bpf/bpf.c            |  2 +-
>   tools/lib/bpf/bpf_prog_linfo.c | 14 +++++++-------
>   tools/lib/bpf/btf.c            |  3 +--
>   tools/lib/bpf/libbpf.c         |  6 +++---
>   4 files changed, 12 insertions(+), 13 deletions(-)
> 

All look good, applied, thanks!

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

end of thread, other threads:[~2019-11-07 16:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-07  2:08 [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf Andrii Nakryiko
2019-11-07  2:08 ` [PATCH bpf-next 1/5] libbpf: fix memory leak/double free issue Andrii Nakryiko
2019-11-07  2:08 ` [PATCH bpf-next 2/5] libbpf: fix potential overflow issue Andrii Nakryiko
2019-11-07  2:08 ` [PATCH bpf-next 3/5] libbpf: fix another potential overflow issue in bpf_prog_linfo Andrii Nakryiko
2019-11-07  2:08 ` [PATCH bpf-next 4/5] libbpf: make btf__resolve_size logic always check size error condition Andrii Nakryiko
2019-11-07  2:08 ` [PATCH bpf-next 5/5] libbpf: improve handling of corrupted ELF during map initialization Andrii Nakryiko
2019-11-07 16:35 ` [PATCH bpf-next 0/5] Fix bugs and issues found by static analysis in libbpf 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).