From patchwork Wed Dec 7 03:52:54 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 30615 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:f944:0:0:0:0:0 with SMTP id q4csp3195969wrr; Tue, 6 Dec 2022 19:53:08 -0800 (PST) X-Google-Smtp-Source: AA0mqf4sL7ex84hg9089EaVkeCwRLG2HNjsgFiwPP+402fCDxrERaGFFysYRr3C4YWTUx+tl4YaJ X-Received: by 2002:a05:6402:388e:b0:468:fb0d:2d8b with SMTP id fd14-20020a056402388e00b00468fb0d2d8bmr48576500edb.124.1670385188427; Tue, 06 Dec 2022 19:53:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670385188; cv=none; d=google.com; s=arc-20160816; b=XKDxC1ZQXkvZpNfGoOfAWQWjPMaY+DOX2ikuZesUCMPFRgy+TYj0IvocDuUEls1HuX 3TJSsOJuqNbAoP7FMKhoRi6sP5EWkrBn0G885uwlfqq2o5tuvjCgtzMasHMeSugh6h+y zWDOdzJmABQbn/ZbZwRKkWO0M8td5uVsj06/1VGzVhWpK3nz6ZDu9UXZgrJyrbqkKl+V 7iVx9PX0rlyijBF7Ei35LYvWrV7Zmj5U6d+xCdZvzM79SNAKjf2M25MsTA6TQY6ogSMu Yeeh1/iyfF9q3V4gn8f/dBASatYvdy2MB6K9zC85VBXvpHCM6rTm8ymWVc/40HHZNVQt a2Mw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=z1Y/76HnHjBlRdELGoB8q99FaEX5oC6oJY96us3yYn4=; b=Nq1Nl1NwwyXfqM8fvaj26xhDiqCDRCUkJP0CouB5ZndRxG/uKA1BKZ5Icd3ZF/oe5J tD/V5Af6H9i+/vHiZWYVo2O605wyI4qgvO+FdRZBO1DE5RNHIxAVslCpun1h8/t2QG3Y QRh7rJklNTu8qegLVjArpRUm0OMa8iDxAKbF0p7D7+n3VLiHtFqCuxGcNsYpaSnCyr4R G4tygSpdvwFVi2Z46X/ZF9DLuIHTJxEWZVbK4IPwQeICp7RED4wwN8g1PwUNfuX1AGN0 iojlPLSzdy2C8RIg3N84mm1tinqr4R1V+aIeN0Y8nhz7t/8sy2wnouWlvHTedjqjyZDX /6bA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ejcEQ8pT; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b12-20020a056402350c00b00469abb21f9esi3892104edd.129.2022.12.06.19.53.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 19:53:08 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=ejcEQ8pT; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4112F388B685 for ; Wed, 7 Dec 2022 03:53:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4112F388B685 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1670385187; bh=z1Y/76HnHjBlRdELGoB8q99FaEX5oC6oJY96us3yYn4=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=ejcEQ8pTLIEjmmCnjLqKFJzOtI0xtkma+f0m5mGpUGiUyIvoStFLor5Md6e7xOSfQ XOTVu8MmwImaKdUvobx/W+ylI2zJNVPeAzrIWXpFcu41v9+FxHf4pnC6zvzh8fhvno W10WajdHSpGMwIcLtfB+flM5g4cXnBK+iU4vFo/U= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id B0197382B3F2 for ; Wed, 7 Dec 2022 03:52:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org B0197382B3F2 Received: by mail-pf1-x435.google.com with SMTP id t18so6084431pfq.13 for ; Tue, 06 Dec 2022 19:52:58 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=z1Y/76HnHjBlRdELGoB8q99FaEX5oC6oJY96us3yYn4=; b=sFCjkw8JqCY7e0Cd2SbnIwpp9Qkr5X4mi+R59GIulUOHCZl9S6gCJce6v3XSuDGmeg zwUp+yCf1dKR5MilQpA1OYpccZPFg2uLHLeyfklMzK0JQylxtWCs6Je9Wtz1GVpRjmAc EAwvV0AaFhlVlTbr1MyP/MdQ2RDQSZPoWapgKcXycCoxXHZIYEB64E0y8pNZZs2BFD3P B6A4A4i329HDgKZ+YQTvaNXwn/PtSnU6Vz6OB26euKQKHegPvp1Sb42jnxwDZLm+D9PI 8IuFIh6dqO8VWelhvYNgRmEOejbs13dXPdm8kl+UYrede0fQ3QablPJMArERJWsarzFl avPA== X-Gm-Message-State: ANoB5pnOKlv2bSUdW4J38X35oEfugwJqj6Gr8DbP+WV8YmaxIGix8bhy WVswIcfVxEZbS2QpoamUVAwKsFHUmeE= X-Received: by 2002:a63:ff5f:0:b0:46f:b6df:3107 with SMTP id s31-20020a63ff5f000000b0046fb6df3107mr63733758pgk.454.1670385177385; Tue, 06 Dec 2022 19:52:57 -0800 (PST) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:2706:5eaf:b1a8:9a0d]) by smtp.gmail.com with ESMTPSA id n6-20020a17090a2fc600b001fb1de10a4dsm139275pjm.33.2022.12.06.19.52.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 19:52:57 -0800 (PST) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id A6CC3114072E; Wed, 7 Dec 2022 14:22:54 +1030 (ACDT) Date: Wed, 7 Dec 2022 14:22:54 +1030 To: binutils@sourceware.org Subject: _bfd_elf_slurp_secondary_reloc_section sanity check Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1751525819617334537?= X-GMAIL-MSGID: =?utf-8?q?1751525819617334537?= * elf.c (_bfd_elf_slurp_secondary_reloc_section): Sanity check section header against file size. Avoid overflow in reloc_count. diff --git a/bfd/elf.c b/bfd/elf.c index ac10715fa93..5548efc44e4 100644 --- a/bfd/elf.c +++ b/bfd/elf.c @@ -13154,6 +13154,7 @@ _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, asection * relsec; bool result = true; bfd_vma (*r_sym) (bfd_vma); + ufile_ptr filesize; #if BFD_DEFAULT_TARGET_SIZE > 32 if (bfd_arch_bits_per_address (abfd) != 32) @@ -13167,6 +13168,7 @@ _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, /* Discover if there are any secondary reloc sections associated with SEC. */ + filesize = bfd_get_file_size (abfd); for (relsec = abfd->sections; relsec != NULL; relsec = relsec->next) { Elf_Internal_Shdr * hdr = & elf_section_data (relsec)->this_hdr; @@ -13180,10 +13182,10 @@ _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, bfd_byte * native_reloc; arelent * internal_relocs; arelent * internal_reloc; - unsigned int i; + size_t i; unsigned int entsize; unsigned int symcount; - unsigned int reloc_count; + bfd_size_type reloc_count; size_t amt; if (ebd->elf_info_to_howto == NULL) @@ -13195,6 +13197,15 @@ _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, #endif entsize = hdr->sh_entsize; + if (filesize != 0 + && ((ufile_ptr) hdr->sh_offset > filesize + || hdr->sh_size > filesize - hdr->sh_offset)) + { + bfd_set_error (bfd_error_file_truncated); + result = false; + continue; + } + native_relocs = bfd_malloc (hdr->sh_size); if (native_relocs == NULL) { @@ -13268,7 +13279,7 @@ _bfd_elf_slurp_secondary_reloc_section (bfd * abfd, { _bfd_error_handler /* xgettext:c-format */ - (_("%pB(%pA): relocation %d has invalid symbol index %ld"), + (_("%pB(%pA): relocation %zu has invalid symbol index %lu"), abfd, sec, i, (long) r_sym (rela.r_info)); bfd_set_error (bfd_error_bad_value); internal_reloc->sym_ptr_ptr =