Fix bugs and issues found by static analysis in libbpf
Subsystems (4)
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
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.
--- tools/lib/bpf/libbpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +1 -1
@@ -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
PATCH 2/5 libbpf: fix potential overflow issue
In reply to Andrii NakryikoFix a potential overflow issue found by LGTM analysis, based on Github libbpf source code.
--- tools/lib/bpf/bpf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf.c +1 -1
@@ -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
PATCH 3/5 libbpf: fix another potential overflow issue in bpf_prog_linfo
In reply to Andrii NakryikoFix few issues found by Coverity and LGTM.
--- tools/lib/bpf/bpf_prog_linfo.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
tools/lib/bpf/bpf_prog_linfo.c +7 -7
@@ -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
PATCH 4/5 libbpf: make btf__resolve_size logic always check size error condition
In reply to Andrii NakryikoPerform 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.
--- tools/lib/bpf/btf.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
tools/lib/bpf/btf.c +1 -2
@@ -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
PATCH 5/5 libbpf: improve handling of corrupted ELF during map initialization
In reply to Andrii NakryikoIf 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.
--- tools/lib/bpf/libbpf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -2
@@ -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
Daniel Borkmann
maintainerQuoted 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 inbpf_prog_linfo
libbpf: makebtf__resolve_sizelogic 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!