From: Mark Wielaard Date: Sat, 27 Jun 2015 20:07:01 +0000 (+0200) Subject: nm: First call elf_getdata, then allocate memory. X-Git-Tag: elfutils-0.164~56 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c08079a076420f67742be98d060500965eb22340;p=thirdparty%2Felfutils.git nm: First call elf_getdata, then allocate memory. 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 --- diff --git a/src/ChangeLog b/src/ChangeLog index 7d5e001b0..50223a4fc 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 733950697..15d9da4ab 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;