Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932487AbbLHC0h (ORCPT ); Mon, 7 Dec 2015 21:26:37 -0500 Received: from szxga01-in.huawei.com ([58.251.152.64]:42587 "EHLO szxga01-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755513AbbLHC0V (ORCPT ); Mon, 7 Dec 2015 21:26:21 -0500 From: Wang Nan To: , CC: , , , , Wang Nan , "Arnaldo Carvalho de Melo" Subject: [PATCH v4 01/16] tools lib bpf: Check return value of strdup when reading map names Date: Tue, 8 Dec 2015 02:25:29 +0000 Message-ID: <1449541544-67621-2-git-send-email-wangnan0@huawei.com> X-Mailer: git-send-email 1.8.3.4 In-Reply-To: <1449541544-67621-1-git-send-email-wangnan0@huawei.com> References: <1449541544-67621-1-git-send-email-wangnan0@huawei.com> MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.107.193.248] X-CFilter-Loop: Reflected X-Mirapoint-Virus-RAPID-Raw: score=unknown(0), refid=str=0001.0A020206.56663FBF.009C,ss=1,re=0.000,recu=0.000,reip=0.000,cl=1,cld=1,fgs=0, ip=0.0.0.0, so=2013-06-18 04:22:30, dmn=2013-03-21 17:37:32 X-Mirapoint-Loop-Id: 614e11188c0068016a225f9716dc5b4c Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 2187 Lines: 74 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 safty. Signed-off-by: Wang Nan Acked-by: Namhyung Kim Cc: Arnaldo Carvalho de Melo --- tools/lib/bpf/libbpf.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index a298614..16485ab 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, return 0; } -static void +static int bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) { int i; Elf_Data *symbols = obj->efile.symbols; if (!symbols || maps_shndx < 0) - return; + return -EINVAL; for (i = 0; i < symbols->d_size / sizeof(GElf_Sym); i++) { GElf_Sym sym; @@ -556,9 +556,14 @@ bpf_object__init_maps_name(struct bpf_object *obj, int maps_shndx) continue; } 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, obj->maps[map_idx].name); } + return 0; } 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) - bpf_object__init_maps_name(obj, maps_shndx); + err = bpf_object__init_maps_name(obj, maps_shndx); out: return err; } @@ -1372,7 +1377,7 @@ bpf_object__get_map_by_name(struct bpf_object *obj, const char *name) struct bpf_map *pos; bpf_map__for_each(pos, obj) { - if (strcmp(pos->name, name) == 0) + if (pos->name && !strcmp(pos->name, name)) return pos; } return NULL; -- 1.8.3.4 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/