From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id D4091383E81A for ; Sun, 10 May 2020 19:59:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org D4091383E81A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mark@klomp.org Received: from librem (deer0x15.wildebeest.org [172.31.17.151]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 96040300B2FD; Sun, 10 May 2020 21:59:01 +0200 (CEST) Received: by librem (Postfix, from userid 1000) id 6F912C031D; Sun, 10 May 2020 21:58:36 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: David Malcolm , Mark Wielaard Subject: [PATCH 5/7] src: Check ebl_openbackend result before using ebl handle. Date: Sun, 10 May 2020 21:53:38 +0200 Message-Id: <20200510195339.37191-6-mark@klomp.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200510195339.37191-1-mark@klomp.org> References: <20200510195339.37191-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 10 May 2020 19:59:04 -0000 GCC10 -fanalyzer plus -flto sees that ebl_openbackend can fail and return NULL. Most of the time we will get a dummy ebl, but in case of out of memory or corrupt ELF header it might return NULL. Make sure that we report a (fatal) error in that case in all tools. Signed-off-by: Mark Wielaard --- src/ChangeLog | 7 +++++++ src/elflint.c | 9 ++++++++- src/nm.c | 16 +++++++++------- src/objdump.c | 3 +++ 4 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 83d58607..8c72e7d1 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2020-05-09 Mark Wielaard + + * elflint.c (process_elf_file): Error out if ebl_openbackend fails. + * objdump.c (handle_elf): Likewise. + * nm.c (handle_elf): Likewise. Move full name string construction + forward, so it can be used in the error message. + 2020-04-17 Mark Wielaard * readelf.c (print_debug): Check .gnu.debuglto_ prefix. diff --git a/src/elflint.c b/src/elflint.c index 0ef43236..6ad9bc42 100644 --- a/src/elflint.c +++ b/src/elflint.c @@ -4775,7 +4775,14 @@ process_elf_file (Elf *elf, const char *prefix, const char *suffix, ebl = ebl_openbackend (elf); /* If there is no appropriate backend library we cannot test architecture and OS specific features. Any encountered extension - is an error. */ + is an error. Often we'll get a "dummy" ebl, except if something + really bad happen, like a totally corrupted ELF file or out of + memory situation. */ + if (ebl == NULL) + { + ERROR (gettext ("cannot create backend for ELF file\n")); + return; + } /* Go straight by the gABI, check all the parts in turn. */ check_elf_header (ebl, ehdr, size); diff --git a/src/nm.c b/src/nm.c index b7c2aed6..f6ca3b0a 100644 --- a/src/nm.c +++ b/src/nm.c @@ -1510,8 +1510,17 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname, GElf_Ehdr *ehdr; Ebl *ebl; + /* Create the full name of the file. */ + if (prefix != NULL) + cp = mempcpy (cp, prefix, prefix_len); + cp = mempcpy (cp, fname, fname_len); + if (suffix != NULL) + memcpy (cp - 1, suffix, suffix_len + 1); + /* Get the backend for this object file type. */ ebl = ebl_openbackend (elf); + if (ebl == NULL) + INTERNAL_ERROR (fullname); /* We need the ELF header in a few places. */ ehdr = gelf_getehdr (elf, &ehdr_mem); @@ -1530,13 +1539,6 @@ handle_elf (int fd, Elf *elf, const char *prefix, const char *fname, goto out; } - /* Create the full name of the file. */ - if (prefix != NULL) - cp = mempcpy (cp, prefix, prefix_len); - cp = mempcpy (cp, fname, fname_len); - if (suffix != NULL) - memcpy (cp - 1, suffix, suffix_len + 1); - /* Find the symbol table. XXX Can there be more than one? Do we print all? Currently we do. */ diff --git a/src/objdump.c b/src/objdump.c index a619674f..82d7bcf6 100644 --- a/src/objdump.c +++ b/src/objdump.c @@ -755,6 +755,9 @@ handle_elf (Elf *elf, const char *prefix, const char *fname, /* Get the backend for this object file type. */ Ebl *ebl = ebl_openbackend (elf); + if (ebl == NULL) + error (EXIT_FAILURE, 0, + gettext ("cannot create backend for elf file")); printf ("%s: elf%d-%s\n\n", fname, gelf_getclass (elf) == ELFCLASS32 ? 32 : 64, -- 2.20.1