Skip to contentSkip to conversation

Fix bugs and issues found by static analysis in libbpf

PATCH In review Andrii Nakryiko 2 participants 7 messages Nov 7, 2019

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

cover Fix bugs and issues found by static analysis in libbpf

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(-)
Signature · 1 line
2.17.1
#1 lore raw

PATCH 1/5 libbpf: fix memory leak/double free issue

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")
---
 tools/lib/bpf/libbpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
index be4af95d5a2c..3ef73a214592 100644
@@ -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",
Signature · 1 line
2.17.1
#2 lore raw

PATCH 2/5 libbpf: fix potential overflow issue

In reply to 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")
---
 tools/lib/bpf/bpf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.c +1 -1
index ca0d635b1d5e..b3e3e99a0f28 100644
@@ -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;
 
Signature · 1 line
2.17.1
#3 lore raw

PATCH 3/5 libbpf: fix another potential overflow issue in bpf_prog_linfo

In reply to Andrii Nakryiko

Fix few issues found by Coverity and LGTM.

Fixes b053b439b72a ("bpf: libbpf: bpftool: Print bpf_line_info during prog dump")
---
 tools/lib/bpf/bpf_prog_linfo.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
tools/lib/bpf/bpf_prog_linfo.c +7 -7
index 8c67561c93b0..3ed1a27b5f7c 100644
@@ -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 *
Signature · 1 line
2.17.1
#4 lore raw

PATCH 4/5 libbpf: make btf__resolve_size logic always check size error condition

In reply to 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")
---
 tools/lib/bpf/btf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
tools/lib/bpf/btf.c +1 -2
index d72e9a79dce1..86a1847e4a9f 100644
@@ -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;
 
Signature · 1 line
2.17.1
#5 lore raw

PATCH 5/5 libbpf: improve handling of corrupted ELF during map initialization

In reply to 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")
---
 tools/lib/bpf/libbpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -2
index 3ef73a214592..fde6cb3e5d41 100644
@@ -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++) {
Signature · 1 line
2.17.1
maintainer
#6 lore raw
In reply to Andrii Nakryiko
Quoted 11 lines · 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

Quoted 6 lines
  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!