From patchwork Fri Jun 2 00:00:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 102245 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp690299vqr; Thu, 1 Jun 2023 17:01:17 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6xwHpxtICC4xzHX4PPPgnMtY5vbjmRRp/m1r70JRWG7XA0QiA5xDCwNHwn1KQlayWMnKza X-Received: by 2002:a2e:3516:0:b0:2ad:9acb:4849 with SMTP id z22-20020a2e3516000000b002ad9acb4849mr601660ljz.47.1685664077323; Thu, 01 Jun 2023 17:01:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685664077; cv=none; d=google.com; s=arc-20160816; b=YjiqkjjyFMKyHXgqXrWKfQ+JVZVU3LxP+UXvK5OIehkHCktmjmqJ5iEWMQAQn6Kxwk /oX0NTrnMyfLcDLDIyScYs23RBwzYWUB1xcGM1FnNTMgn00Pr0B9c9XqeTuchmV/Pvhp IjppXB06OBPdDYB8tvvAoNnzAU5OD4hpphfa7ZqeyS4dpqZ/f0yX/Id+AAHTCc6/xXvO XMCNfZa7ao0WA01pk81lAHRPW4WNxVJ+KA24Sz43n7G9WF/j9DuOpCfPgABcqnduUfzN WA0loHnfpxovkV9oXvF0GqyBw0XXroxXwf55RoK5twm09Q6/rRKRV1UEFuXY7yqLJ2QK mlxg== 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=zMP6Ai5L9IwyVZX0Ch9EPJWnWvF8lQgxBKFlCA+QcZ8=; b=ks7Zgjz4/jkcfp9EmVQCorp9q1L0nHtoLGjvj1B55Eazs7LOxkUZ5vX52bBLuGd/tZ TFSMpMkBJemZHu3+HgXqpbjP90s+ijSkKUML9kl9RlUjwDhHO+0MV/8P10IcDI5Qcaew bZNoS29t/SVWzlweiAxNp4WR30SdNEQUa+29OQ49UUwzMWykWq8t17KYIWtFZM+7jHtn YGT3eHaHp49jps7HKBlLk2lCpjR13C8lfnSreHk+9Va4+YiHbejKlclfQViCac5cA0fl BDSHr9+hlyum89IS0/uc3KxpkyITu8GGnHY7Wob58DaZqxHUpHxixKdUKWU9Uz1e3g5R AEZg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=kpUmIZcL; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id w20-20020aa7cb54000000b00514224ba47asi15075edt.509.2023.06.01.17.01.17 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 17:01:17 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=kpUmIZcL; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 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 2587A3857733 for ; Fri, 2 Jun 2023 00:01:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2587A3857733 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685664076; bh=zMP6Ai5L9IwyVZX0Ch9EPJWnWvF8lQgxBKFlCA+QcZ8=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=kpUmIZcLe5/w/HxDUOJyPaOFyZTYsRelWtj+sEkiqG2CjtGPjfjVim8NSsTluS9Px nU7013dQaRDphwQNp2C4HrQhxtoXEF8/2wRRlvVGTZxH0KhG9Fsi8Bq+gTREZiunGA ISYqeBGHeCLLRZUehNTLwMbx0m7tGWvtlZ9IEa0E= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-qv1-xf2b.google.com (mail-qv1-xf2b.google.com [IPv6:2607:f8b0:4864:20::f2b]) by sourceware.org (Postfix) with ESMTPS id 966E1385773F for ; Fri, 2 Jun 2023 00:01:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 966E1385773F Received: by mail-qv1-xf2b.google.com with SMTP id 6a1803df08f44-6260a9ef126so13323766d6.2 for ; Thu, 01 Jun 2023 17:01:01 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685664060; x=1688256060; 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=zMP6Ai5L9IwyVZX0Ch9EPJWnWvF8lQgxBKFlCA+QcZ8=; b=XbLZDKOeXPjKHPsb5dknS8TTAuV8MFJyWLYOfspEGieFLLXRtHrMsVQrPVuOUvpMZL uWvYxF5855pra/jf0f0zVWO/MGOzW8aMuCs50dCTzEhJ8RHShR6+XD/sQvR34um8brVL T2fAaF0Dmwi3vWFo2DtlwNaDsutev3jGN9UkPBJCl7WeIPbClIZOmyOEcXEZ5Ms5rCXl L+efCoL0Ol0Dsw74unok8yo/BVqaCiyePX7FNBRH3T4P6WwLrS7ATLCD+QmTVxNG5+YY meWhP0j1gIyAaQwcDKiI/tng3FzdREQkZKBJ7eATLgDGE6RLWr2T+12uSgU03XN2xHNP NOdw== X-Gm-Message-State: AC+VfDwVg9of61EYKJOyh006vEK2ItWU1LnwJOcM8Gm9QlhsDoZBTKQc DnOtt9REajbgvl5IRyqKfbbJCtPTVcU= X-Received: by 2002:ad4:5aab:0:b0:5fd:7701:88c5 with SMTP id u11-20020ad45aab000000b005fd770188c5mr14090148qvg.6.1685664060529; Thu, 01 Jun 2023 17:01:00 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id h6-20020a170902748600b001b04b1bd774sm4069663pll.208.2023.06.01.17.00.59 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Jun 2023 17:00:59 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 312D91140B11; Fri, 2 Jun 2023 09:30:57 +0930 (ACST) Date: Fri, 2 Jun 2023 09:30:57 +0930 To: binutils@sourceware.org Subject: loongarch readelf support Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3034.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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?1767546895241636403?= X-GMAIL-MSGID: =?utf-8?q?1767546895241636403?= This fixes two buffer overflows found by fuzzers. * readelf.c (target_specific_reloc_handling): Sanity check loongarch reloc symbol index. Don't apply reloc after errors. Reduce translation work of "invalid symbol index" error message. diff --git a/binutils/readelf.c b/binutils/readelf.c index 7292dd0941a..042d2301517 100644 --- a/binutils/readelf.c +++ b/binutils/readelf.c @@ -14028,24 +14028,32 @@ target_specific_reloc_handling (Filedata *filedata, "ULEB128 value\n"), (long) reloc->r_offset); - if (107 == reloc_type) - value += (reloc->r_addend + symtab[sym_index].st_value); - else if (108 == reloc_type) - value -= (reloc->r_addend + symtab[sym_index].st_value); - - /* Write uleb128 value to p. */ - bfd_byte c; - bfd_byte *p = start + reloc->r_offset; - do + else if (sym_index >= num_syms) + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), + (reloc_type == 107 + ? "R_LARCH_ADD_ULEB128" + : "R_LARCH_SUB_ULEB128"), + sym_index); + else { - c = value & 0x7f; - if (reloc_size > 1) - c |= 0x80; - *(p++) = c; - value >>= 7; - reloc_size--; + if (reloc_type == 107) + value += reloc->r_addend + symtab[sym_index].st_value; + else + value -= reloc->r_addend + symtab[sym_index].st_value; + + /* Write uleb128 value to p. */ + bfd_byte *p = start + reloc->r_offset; + do + { + bfd_byte c = value & 0x7f; + value >>= 7; + if (--reloc_size != 0) + c |= 0x80; + *p++ = c; + } + while (reloc_size); } - while (reloc_size); return true; } @@ -14075,8 +14083,8 @@ target_specific_reloc_handling (Filedata *filedata, case 23: /* R_MSP430X_GNU_SUB_ULEB128 */ /* PR 21139. */ if (sym_index >= num_syms) - error (_("MSP430 SYM_DIFF reloc contains invalid symbol index" - " %" PRIu64 "\n"), sym_index); + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), "MSP430 SYM_DIFF", sym_index); else saved_sym = symtab + sym_index; return true; @@ -14126,9 +14134,8 @@ target_specific_reloc_handling (Filedata *filedata, " contains invalid ULEB128 value\n"), reloc->r_offset); else if (sym_index >= num_syms) - error (_("MSP430 reloc contains invalid symbol index " - "%" PRIu64 "\n"), - sym_index); + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), "MSP430", sym_index); else { value = reloc->r_addend + (symtab[sym_index].st_value @@ -14173,9 +14180,8 @@ target_specific_reloc_handling (Filedata *filedata, return true; case 33: /* R_MN10300_SYM_DIFF */ if (sym_index >= num_syms) - error (_("MN10300_SYM_DIFF reloc contains invalid symbol index " - "%" PRIu64 "\n"), - sym_index); + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), "MN10300_SYM_DIFF", sym_index); else saved_sym = symtab + sym_index; return true; @@ -14188,9 +14194,8 @@ target_specific_reloc_handling (Filedata *filedata, uint64_t value; if (sym_index >= num_syms) - error (_("MN10300 reloc contains invalid symbol index " - "%" PRIu64 "\n"), - sym_index); + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), "MN10300", sym_index); else { value = reloc->r_addend + (symtab[sym_index].st_value @@ -14233,8 +14238,8 @@ target_specific_reloc_handling (Filedata *filedata, case 0x80: /* R_RL78_SYM. */ saved_sym1 = saved_sym2; if (sym_index >= num_syms) - error (_("RL78_SYM reloc contains invalid symbol index " - "%" PRIu64 "\n"), sym_index); + error (_("%s reloc contains invalid symbol index " + "%" PRIu64 "\n"), "RL78_SYM", sym_index); else { saved_sym2 = symtab[sym_index].st_value;