summaryrefslogtreecommitdiffstats
path: root/tools/lib
diff options
context:
space:
mode:
authorWang Nan <wangnan0@huawei.com>2015-12-07 21:25:29 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2015-12-11 06:51:09 -0500
commit973170e66726672518eb935eb0dc0e63876d133d (patch)
tree08c9cbfc910c13eb1bce3130c0d775e20913f46c /tools/lib
parentbeefb8d0e556aaf3cb69168c5953e023ace6aa78 (diff)
tools lib bpf: Check return value of strdup when reading map names
Commit 561bbccac72d08babafaa33fd7fa9100ec4c9fb6 ("tools lib bpf: Extract and collect map names from BPF object file") forgets checking return value of strdup(). This patch fixes it. It also checks names pointer before strcmp() for safety. Signed-off-by: Wang Nan <wangnan0@huawei.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Cc: Zefan Li <lizefan@huawei.com> Cc: pi3orama@163.com Fixes: 561bbccac72d ("tools lib bpf: Extract and collect map names from BPF object file") Link: http://lkml.kernel.org/r/1449541544-67621-2-git-send-email-wangnan0@huawei.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib')
-rw-r--r--tools/lib/bpf/libbpf.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index a298614ad091..16485ab05fc1 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -527,14 +527,14 @@ bpf_object__init_maps(struct bpf_object *obj, void *data,
527 return 0; 527 return 0;
528} 528}
529 529
530static void 530static int
531bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) 531bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
532{ 532{
533 int i; 533 int i;
534 Elf_Data *symbols = obj->efile.symbols; 534 Elf_Data *symbols = obj->efile.symbols;
535 535
536 if (!symbols || maps_shndx < 0) 536 if (!symbols || maps_shndx < 0)
537 return; 537 return -EINVAL;
538 538
539 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { 539 for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
540 GElf_Sym sym; 540 GElf_Sym sym;
@@ -556,9 +556,14 @@ bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
556 continue; 556 continue;
557 } 557 }
558 obj->maps[map_idx].name = strdup(map_name); 558 obj->maps[map_idx].name = strdup(map_name);
559 if (!obj->maps[map_idx].name) {
560 pr_warning("failed to alloc map name\n");
561 return -ENOMEM;
562 }
559 pr_debug("map %zu is \"%s\"\n", map_idx, 563 pr_debug("map %zu is \"%s\"\n", map_idx,
560 obj->maps[map_idx].name); 564 obj->maps[map_idx].name);
561 } 565 }
566 return 0;
562} 567}
563 568
564static int bpf_object__elf_collect(struct bpf_object *obj) 569static int bpf_object__elf_collect(struct bpf_object *obj)
@@ -663,7 +668,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
663 } 668 }
664 669
665 if (maps_shndx >= 0) 670 if (maps_shndx >= 0)
666 bpf_object__init_maps_name(obj, maps_shndx); 671 err = bpf_object__init_maps_name(obj, maps_shndx);
667out: 672out:
668 return err; 673 return err;
669} 674}
@@ -1372,7 +1377,7 @@ bpf_object__get_map_by_name(struct bpf_object *obj, const char *name)
1372 struct bpf_map *pos; 1377 struct bpf_map *pos;
1373 1378
1374 bpf_map__for_each(pos, obj) { 1379 bpf_map__for_each(pos, obj) {
1375 if (strcmp(pos->name, name) == 0) 1380 if (pos->name && !strcmp(pos->name, name))
1376 return pos; 1381 return pos;
1377 } 1382 }
1378 return NULL; 1383 return NULL;