>From c08079a076420f67742be98d060500965eb22340 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sat, 27 Jun 2015 22:07:01 +0200 Subject: [PATCH] nm: First call elf_getdata, then allocate memory. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This catches bogus data early before we might try to allocate giant amounts of memory. Reported-by: Hanno Böck Signed-off-by: Mark Wielaard --- src/ChangeLog | 4 ++++ src/nm.c | 12 ++++++------ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 7d5e001..50223a4 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,7 @@ +2015-06-27 Mark Wielaard + + * nm.c (show_symbols): First call elf_getdata, then allocate memory. + 2015-06-18 Mark Wielaard * findtextrel.c (process_file): Free segments after use. diff --git a/src/nm.c b/src/nm.c index 7339506..15d9da4 100644 --- a/src/nm.c +++ b/src/nm.c @@ -1200,6 +1200,12 @@ show_symbols (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, Elf_Scn *xndxscn, } } + /* Get the data of the section. */ + Elf_Data *data = elf_getdata (scn, NULL); + Elf_Data *xndxdata = elf_getdata (xndxscn, NULL); + if (data == NULL || (xndxscn != NULL && xndxdata == NULL)) + INTERNAL_ERROR (fullname); + /* Allocate the memory. XXX We can use a dirty trick here. Since GElf_Sym == Elf64_Sym we @@ -1211,12 +1217,6 @@ show_symbols (Ebl *ebl, GElf_Ehdr *ehdr, Elf_Scn *scn, Elf_Scn *xndxscn, else sym_mem = (GElf_SymX *) xmalloc (nentries * sizeof (GElf_SymX)); - /* Get the data of the section. */ - Elf_Data *data = elf_getdata (scn, NULL); - Elf_Data *xndxdata = elf_getdata (xndxscn, NULL); - if (data == NULL || (xndxscn != NULL && xndxdata == NULL)) - INTERNAL_ERROR (fullname); - /* Iterate over all symbols. */ #ifdef USE_DEMANGLE size_t demangle_buffer_len = 0; -- 2.4.3