From patchwork Mon Oct 16 06:02:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tsukasa OI X-Patchwork-Id: 153201 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp3261203vqb; Sun, 15 Oct 2023 23:03:10 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHvZAZ2D8DiwEzAcSs3GUU9cQQdSRT5P4DLWmYgYPMPKqtYSJpEla2L/XQ3jl4NsaB+9puH X-Received: by 2002:a05:622a:1208:b0:419:a2c6:8217 with SMTP id y8-20020a05622a120800b00419a2c68217mr39327556qtx.5.1697436190473; Sun, 15 Oct 2023 23:03:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697436190; cv=pass; d=google.com; s=arc-20160816; b=BEBARn3MvbOw5mI2cRW7Rr+WBzQTMGe4oH/Niau1qtSq3jXff+bf/OjvFv572oebG9 /5RhG3j/fZmCI2M1PmvM1zN9GoEf2jKcZaKSvrzigQDQKnqySGfFchDmlDmp5nzL/ylq iHTp5F6ZoxhrI+74HlZUVO4fOsEML5OclE4GD1COFGhDPiG+i0gSv6gEZau0lekLPoA8 f1k0etrzF+x6nkP5uqFFXyYRxWlpqBSgL3U7JqG4zEaJo4ljkUQApsg/BLIt/gs8NbuQ lXL8NUxzukV7fLG9LwTUdkSg/M9RxQtiSfo7PfCV7snwUGq1zm5DNRVs6H7qeWatANWX CJTw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=9z1V41i/Jx9TVyoar9DVw4+oZLv9Hs+Rs+W82JU0zmY=; fh=Q+ZPQdyJDzJx4BfovHNTR1lP4msTSfN6TDra8V1lyf8=; b=oFFl1+WteX/YMvJvxLoAtVnDa9/KskPD3kBSJCAhbZI9NNrwu8N7dECkpH1nJGhstg HhTmh4eDZCQ8GFeJoFqCYhfgjs7oABs/DkwxE6Yp6YP+6XtWi4Z/XWmG/IpGJSFF3hxQ LdYI9+450UTH1Hi92KTZzuno/OTQtjwWOqZeGdAfE57LjDG7gAvR9t83TO/n3n5yWcTR +tbVEX9W6L8LyN3nSinmyPzEoxkJITkQGX1mRyaMbJqJWN56s7xNwz114w+IIQqdjMEX ZxfvLVQNBZy9pFXiAt/u8sNdNxSgVKiAa5X4WI/TJKjnIUfDb2yFazHdcxkb+zsBrjL9 +NkQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@irq.a4lg.com header.s=2017s01 header.b=DU83PyyI; arc=pass (i=1); 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=irq.a4lg.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id g22-20020ac87d16000000b0041230bc1d65si4923859qtb.371.2023.10.15.23.03.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 15 Oct 2023 23:03:10 -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=@irq.a4lg.com header.s=2017s01 header.b=DU83PyyI; arc=pass (i=1); 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=irq.a4lg.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 336E03857B8E for ; Mon, 16 Oct 2023 06:03:10 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-sender-0.a4lg.com (mail-sender.a4lg.com [153.120.152.154]) by sourceware.org (Postfix) with ESMTPS id 088DC385783F for ; Mon, 16 Oct 2023 06:03:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 088DC385783F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=irq.a4lg.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=irq.a4lg.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 088DC385783F Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=153.120.152.154 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697436185; cv=none; b=Ezq/GQQz/qvQKat1XxbAvW0N7Y3Mg6xRQvrkdlt24KSshFEoXwrwZ/UHSy+YKlBQkPjyifqa/1F8U0asiDjbBIhpC2HEBjwGg0/Z+s+sCIQqtNg8fYA6EmV8NAjV5j1Esd6FXaXSa0B3NUz04jb2RI5Ap4Men9l5LJUvThLP8vk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697436185; c=relaxed/simple; bh=dmpWFuyAx+gsTT7L3u3cj69dQ6Lg3CdeyHA9Hmtkz/E=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:Mime-Version; b=BbyCklgAbMIive9cp59AHrzmZZSKj3IREwfEZQaa+0NIOscup6pay/0Lp863lxOatvJQ+dA4WT+iiOL1jUGzRaup/KpWnBAziFZUrIuxhyb63dkZKKfVpLbkG+mSTetGv+t/hMO2OW29f2N4arEMRsxaZR630cd0s7P4PLD7s4c= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from [127.0.0.1] (localhost [127.0.0.1]) by mail-sender-0.a4lg.com (Postfix) with ESMTPSA id DAA40300089; Mon, 16 Oct 2023 06:03:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=irq.a4lg.com; s=2017s01; t=1697436182; bh=9z1V41i/Jx9TVyoar9DVw4+oZLv9Hs+Rs+W82JU0zmY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: Mime-Version:Content-Transfer-Encoding; b=DU83PyyIDwVOmV9ux0KukzR7Xx3HUbAzvOJCPdsKisTC2CJ560ykXT5D1xyBW+m38 czkhp+CY7j5xYxPKZ0HcdBfiLlo5Ix5BPMhWnRgiZ9DtVS7ux5JGNAq9ZQXx1IDG1p +Eq0m+MIH8fqwwCphAOWZ2jt3KiLVlfQqA/5Ygww= From: Tsukasa OI To: Tsukasa OI , Palmer Dabbelt , Andrew Waterman , Jim Wilson , Nelson Chu , Kito Cheng Cc: binutils@sourceware.org Subject: [PATCH 1/2] RISC-V: Reject invalid relocation types Date: Mon, 16 Oct 2023 06:02:36 +0000 Message-ID: <208922596bc01f3be066b8e5bd388690b9d5c643.1697436144.git.research_trasio@irq.a4lg.com> In-Reply-To: References: Mime-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, GIT_PATCH_0, KAM_MANYTO, 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.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1779890850714476998 X-GMAIL-MSGID: 1779890850714476998 From: Tsukasa OI In RISC-V BFD, there are several internal-only relocation types. Such relocation fed from the outside can be a cause of unexpected behaviors and should be rejected before being parsed further. This commit adds checks to make sure that we only handle known relocation types. For maintainability, internal-only relocation types are listed separately. Changes to riscv_elf_check_relocs applies to the linker (ld) and changes to riscv_info_to_howto_rela and riscv_elf_rtype_to_howto applies to other tools such like objdump and objcopy. bfd/ChangeLog: * elfnn-riscv.c (riscv_reloc_is_internal_use_only): New to detect internal use only relocation type. (riscv_info_to_howto_rela): Reject invalid relocation types while handling ELF files but linking. (riscv_elf_check_relocs): Reject invalid relocation types while linking. * elfxx-riscv.c (riscv_elf_rtype_to_howto): Also reject types without name meaning unknown relocation type. --- bfd/elfnn-riscv.c | 77 +++++++++++++++++++++++++++++++++++++++++++++-- bfd/elfxx-riscv.c | 2 +- 2 files changed, 76 insertions(+), 3 deletions(-) diff --git a/bfd/elfnn-riscv.c b/bfd/elfnn-riscv.c index 09aa7be225ef..dedfabe131ba 100644 --- a/bfd/elfnn-riscv.c +++ b/bfd/elfnn-riscv.c @@ -262,12 +262,37 @@ riscv_elfNN_set_options (struct bfd_link_info *link_info, riscv_elf_hash_table (link_info)->params = params; } +static bool +riscv_reloc_is_internal_use_only (unsigned int r_type) +{ + switch (r_type) + { + case R_RISCV_RVC_LUI: + case R_RISCV_GPREL_I: + case R_RISCV_GPREL_S: + case R_RISCV_TPREL_I: + case R_RISCV_TPREL_S: + case R_RISCV_DELETE: + return true; + default: + return false; + } +} + static bool riscv_info_to_howto_rela (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst) { - cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, ELFNN_R_TYPE (dst->r_info)); + unsigned int r_type = ELFNN_R_TYPE (dst->r_info); + cache_ptr->howto = riscv_elf_rtype_to_howto (abfd, r_type); + if (cache_ptr->howto && riscv_reloc_is_internal_use_only (r_type)) + { + (*_bfd_error_handler) (_("%pB: unsupported relocation type %#x"), + abfd, r_type); + bfd_set_error (bfd_error_bad_value); + cache_ptr->howto = NULL; + } return cache_ptr->howto != NULL; } @@ -834,8 +859,53 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, h->ref_regular = 1; } + /* Explicitly reject internal use only relocation types. */ + if (riscv_reloc_is_internal_use_only (r_type)) + { + _bfd_error_handler + (_("%pB: internal error: unsupported relocation type %#x"), + abfd, r_type); + return false; + } + switch (r_type) { + case R_RISCV_NONE: + case R_RISCV_TLS_DTPMOD32: + case R_RISCV_TLS_DTPMOD64: + case R_RISCV_TLS_DTPREL32: + case R_RISCV_TLS_DTPREL64: + case R_RISCV_TLS_TPREL32: + case R_RISCV_TLS_TPREL64: + case R_RISCV_PCREL_LO12_I: + case R_RISCV_PCREL_LO12_S: + case R_RISCV_LO12_I: + case R_RISCV_LO12_S: + case R_RISCV_TPREL_LO12_I: + case R_RISCV_TPREL_LO12_S: + case R_RISCV_TPREL_ADD: + case R_RISCV_ADD8: + case R_RISCV_ADD16: + case R_RISCV_ADD32: + case R_RISCV_ADD64: + case R_RISCV_SUB8: + case R_RISCV_SUB16: + case R_RISCV_SUB32: + case R_RISCV_SUB64: + case R_RISCV_ALIGN: + case R_RISCV_RELAX: + case R_RISCV_SUB6: + case R_RISCV_SET6: + case R_RISCV_SET8: + case R_RISCV_SET16: + case R_RISCV_SET32: + case R_RISCV_32_PCREL: + case R_RISCV_IRELATIVE: + case R_RISCV_SET_ULEB128: + case R_RISCV_SUB_ULEB128: + /* Known relocation types without additional checks here. */ + break; + case R_RISCV_TLS_GD_HI20: if (!riscv_elf_record_got_reference (abfd, info, h, r_symndx) || !riscv_elf_record_tls_type (abfd, h, r_symndx, GOT_TLS_GD)) @@ -1064,7 +1134,10 @@ riscv_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, break; default: - break; + _bfd_error_handler + (_("%pB: internal error: unsupported relocation type %#x"), + abfd, r_type); + return false; } } diff --git a/bfd/elfxx-riscv.c b/bfd/elfxx-riscv.c index c070394a3667..ffcdae341b2f 100644 --- a/bfd/elfxx-riscv.c +++ b/bfd/elfxx-riscv.c @@ -975,7 +975,7 @@ riscv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name) reloc_howto_type * riscv_elf_rtype_to_howto (bfd *abfd, unsigned int r_type) { - if (r_type >= ARRAY_SIZE (howto_table)) + if (r_type >= ARRAY_SIZE (howto_table) || !howto_table[r_type].name) { (*_bfd_error_handler) (_("%pB: unsupported relocation type %#x"), abfd, r_type);