From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 358AC3948A79 for ; Mon, 9 May 2022 20:24:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 358AC3948A79 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (deer0x07.wildebeest.org [172.31.17.137]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 709C93000599; Mon, 9 May 2022 22:24:38 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id C4AA82E81568; Mon, 9 May 2022 22:24:37 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH] strip: Add more NULL check Date: Mon, 9 May 2022 22:24:35 +0200 Message-Id: <20220509202435.16362-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Mon, 09 May 2022 20:24:42 -0000 When gelf_getshdr, gelf_getrela, gelf_getrel or gelf_getsymshndx return NULL it is an internal error which we want to report instead of crashing. Signed-off-by: Mark Wielaard --- src/ChangeLog | 5 +++++ src/strip.c | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index fd87ce2f..b978f9ef 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,8 @@ +2022-05-09 Mark Wielaard + + * strip.c (remove_debug_relocations): Check gelf_getshdr, gelf_getrela, + gelf_getrel and gelf_getsymshndx don't return NULL. + 2022-04-24 Mark Wielaard * elfclassify.c (main): Use classify_flag_no_stdin for no-std in options. diff --git a/src/strip.c b/src/strip.c index 30a1f9da..452b1279 100644 --- a/src/strip.c +++ b/src/strip.c @@ -576,7 +576,8 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr, might want to change the size. */ GElf_Shdr shdr_mem; GElf_Shdr *shdr = gelf_getshdr (scn, &shdr_mem); - if (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA) + if (shdr != NULL + && (shdr->sh_type == SHT_REL || shdr->sh_type == SHT_RELA)) { /* Make sure that this relocation section points to a section to relocate with contents, that isn't @@ -584,7 +585,8 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr, Elf_Scn *tscn = elf_getscn (elf, shdr->sh_info); GElf_Shdr tshdr_mem; GElf_Shdr *tshdr = gelf_getshdr (tscn, &tshdr_mem); - if (tshdr->sh_type == SHT_NOBITS + if (tshdr == NULL + || tshdr->sh_type == SHT_NOBITS || tshdr->sh_size == 0 || (tshdr->sh_flags & SHF_ALLOC) != 0) continue; @@ -653,6 +655,8 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr, if (is_rela) { GElf_Rela *r = gelf_getrela (reldata, relidx, &mem.rela); + if (r == NULL) + INTERNAL_ERROR (fname); offset = r->r_offset; addend = r->r_addend; rtype = GELF_R_TYPE (r->r_info); @@ -662,6 +666,8 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr, else { GElf_Rel *r = gelf_getrel (reldata, relidx, &mem.rel); + if (r == NULL) + INTERNAL_ERROR (fname); offset = r->r_offset; addend = 0; rtype = GELF_R_TYPE (r->r_info); @@ -685,6 +691,8 @@ remove_debug_relocations (Ebl *ebl, Elf *elf, GElf_Ehdr *ehdr, GElf_Sym *sym = gelf_getsymshndx (symdata, xndxdata, symndx, &sym_mem, &xndx); + if (sym == NULL) + INTERNAL_ERROR (fname); Elf32_Word sec = (sym->st_shndx == SHN_XINDEX ? xndx : sym->st_shndx); -- 2.30.2