Commit 973170e6 authored by Wang Nan's avatar Wang Nan Committed by Arnaldo Carvalho de Melo

tools lib bpf: Check return value of strdup when reading map names

Commit 561bbcca ("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: default avatarWang Nan <wangnan0@huawei.com>
Acked-by: default avatarNamhyung Kim <namhyung@kernel.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Fixes: 561bbcca ("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.comSigned-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent beefb8d0
...@@ -527,14 +527,14 @@ bpf_object__init_maps(struct bpf_object *obj, void *data, ...@@ -527,14 +527,14 @@ bpf_object__init_maps(struct bpf_object *obj, void *data,
return 0; return 0;
} }
static void static int
bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
{ {
int i; int i;
Elf_Data *symbols = obj->efile.symbols; Elf_Data *symbols = obj->efile.symbols;
if (!symbols || maps_shndx < 0) if (!symbols || maps_shndx < 0)
return; return -EINVAL;
for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) {
GElf_Sym sym; GElf_Sym sym;
...@@ -556,9 +556,14 @@ bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) ...@@ -556,9 +556,14 @@ bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx)
continue; continue;
} }
obj->maps[map_idx].name = strdup(map_name); obj->maps[map_idx].name = strdup(map_name);
if (!obj->maps[map_idx].name) {
pr_warning("failed to alloc map name\n");
return -ENOMEM;
}
pr_debug("map %zu is \"%s\"\n", map_idx, pr_debug("map %zu is \"%s\"\n", map_idx,
obj->maps[map_idx].name); obj->maps[map_idx].name);
} }
return 0;
} }
static int bpf_object__elf_collect(struct bpf_object *obj) static int bpf_object__elf_collect(struct bpf_object *obj)
...@@ -663,7 +668,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj) ...@@ -663,7 +668,7 @@ static int bpf_object__elf_collect(struct bpf_object *obj)
} }
if (maps_shndx >= 0) if (maps_shndx >= 0)
bpf_object__init_maps_name(obj, maps_shndx); err = bpf_object__init_maps_name(obj, maps_shndx);
out: out:
return err; return err;
} }
...@@ -1372,7 +1377,7 @@ bpf_object__get_map_by_name(struct bpf_object *obj, const char *name) ...@@ -1372,7 +1377,7 @@ bpf_object__get_map_by_name(struct bpf_object *obj, const char *name)
struct bpf_map *pos; struct bpf_map *pos;
bpf_map__for_each(pos, obj) { bpf_map__for_each(pos, obj) {
if (strcmp(pos->name, name) == 0) if (pos->name && !strcmp(pos->name, name))
return pos; return pos;
} }
return NULL; return NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment