Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1279646 > unrolled thread

[PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names

Started byWang Nan <wangnan0@huawei.com>
First post2015-11-30 08:40 +0100
Last post2015-11-30 09:50 +0100
Articles 2 — 2 participants

Back to article view | Back to linux.kernel

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  [PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names Wang Nan <wangnan0@huawei.com> - 2015-11-30 08:40 +0100
    Re: [PATCH v3 01/13] tools lib bpf: Check return value of strdup  when reading map names Namhyung Kim <namhyung@kernel.org> - 2015-11-30 09:50 +0100

#1279646 — [PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names

FromWang Nan <wangnan0@huawei.com>
Date2015-11-30 08:40 +0100
Subject[PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names
Message-ID<qAre4-Ay-65@gated-at.bofh.it>
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.

Reported-by: Namhyung Kim <namhyung@kernel.org>
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 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/

[toc] | [next] | [standalone]


#1279691 — Re: [PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names

FromNamhyung Kim <namhyung@kernel.org>
Date2015-11-30 09:50 +0100
SubjectRe: [PATCH v3 01/13] tools lib bpf: Check return value of strdup when reading map names
Message-ID<qAsjM-1hf-19@gated-at.bofh.it>
In reply to#1279646
On Mon, Nov 30, 2015 at 07:32:01AM +0000, Wang Nan wrote:
> 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.
> 
> Reported-by: Namhyung Kim <namhyung@kernel.org>
> Signed-off-by: Wang Nan <wangnan0@huawei.com>
> Cc: Arnaldo Carvalho de Melo <acme@redhat.com>

Acked-by: Namhyung Kim <namhyung@kernel.org>

Thanks,
Namhyung


> ---
>  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/

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web