From patchwork Wed Aug 23 01:37:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 136616 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp179473vqm; Tue, 22 Aug 2023 18:37:36 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGsxu/NyrZR9EFYT05dPGYiNYtOvm4dWnQmCeBS0pisthRhGuTRP79til4VK5Jj+RNWSYAY X-Received: by 2002:aa7:c603:0:b0:523:3fff:5ce2 with SMTP id h3-20020aa7c603000000b005233fff5ce2mr8415966edq.41.1692754655997; Tue, 22 Aug 2023 18:37:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692754655; cv=none; d=google.com; s=arc-20160816; b=H5oYjppQFehN44qOg02msu1MZk1aPRixjVexWgE057NLJzq46twiEVFQchg59s/eMj xNmRfnRqThMLXG1eylpz/d9Dc7QG3uGtQMHbKIXlwiERp9cx4kyOnk8fsEv+ub0yRRCB wd0dNdhQedNmemHs8y8+T1iiTgLn8HSqityCcgekx6DmthGZd80aWRlVu+O6UZ8xiQrt UNWMJlJO6Z6sQ9BDwF7fX+kYEYPvY3q23CHIi8b+1LVWcHUo6+ucbXYTINeLa7TPCLxx ixD9z27JQnWrmhxyYSsbaP9O+pjCOa+AVhWOdT1cav1oglfhzXXoYg1Vx5nsUrSvKOQ/ ICvQ== 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=dwN9/wixHX86dTu00NsySbk3dyrpDqEbk1J/mPdAXUw=; fh=HNIqzTA9Dp00ovCAyCdXfuEvWDT00OlFxFRPLybXRis=; b=SdJyrFwSedIBi1GQ1TN4yDoR8ZV13bOp6iM52kVZUTGS7tqrgSpIGdBL5XcrKdaRGC 4yj+66Y7Y3ZOdwoyluB51qNRzjLC03Gy2QYZIcG8hB0Bh7G5zd95ROzcRV8G5PqOuGxB jRqwqTzLFBcA29g3xWQbYwjlihXmnUZtF+IcB3BgWc+ysQPHowWu9VClPKOIO7sVLX0f h0toYuzXjUevo3HOmdYoDDMX+1UnWltfcv3ogWim+I/WKQ+nGG0+Et0YSWclBGKbGb8T JeYvpaPsYWvRfUDBpSkjBGtDp1wMNOFttbRYP6POv4snb208Irvk3kEqgN1cvUgLd7KU 3NQw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b="Ule5PR/q"; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id r9-20020aa7da09000000b0052724ba9ddbsi7721696eds.246.2023.08.22.18.37.35 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 18:37:35 -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="Ule5PR/q"; 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 D5CD4385B524 for ; Wed, 23 Aug 2023 01:37:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D5CD4385B524 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1692754654; bh=dwN9/wixHX86dTu00NsySbk3dyrpDqEbk1J/mPdAXUw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Ule5PR/qgaCAf7SDY7n1upfIaql4VVjFAlP19q8qu9JbeJh3XtgnzygvahyglbwRz mQrtVVmlYhI09ws9xNAW8VSD2ps+HiUVs79tiepYoos0S62/wsQ5VOHk2mm5caJr4O yQL9SxJTcFMOmNf32S88MxMPHr5gtC4LjxuwCId8= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pf1-x42b.google.com (mail-pf1-x42b.google.com [IPv6:2607:f8b0:4864:20::42b]) by sourceware.org (Postfix) with ESMTPS id 06A833858C53 for ; Wed, 23 Aug 2023 01:37:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06A833858C53 Received: by mail-pf1-x42b.google.com with SMTP id d2e1a72fcca58-68a40d85593so2101360b3a.2 for ; Tue, 22 Aug 2023 18:37:24 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692754643; x=1693359443; 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=dwN9/wixHX86dTu00NsySbk3dyrpDqEbk1J/mPdAXUw=; b=GmBHg7W+wfbpTXeIfnzr1ZyBPHn6BnL9mmr0uGD+nE6vugqGZhsZVav+mmP5vk8p0N 508pDiv7a6VeqB5SfnO97Yrdepw9IAAaEBOEQ8OUOyNZQWY/9HM+kdAcp6mxLhuclEHu tzdUw84MNvQJ9UblXIAD3xbfZiGeDM9YtIy0q67jtMfZfLX/6oDoyci+fOUm5NIKT3dH vB0Yura61i+Xm+FBXAd4sEH8pB0kyY3K5vUgALTqBy3ZYBcXIDU0hfXWLLRg6g8+yD/x bFYLUZJIHqi8kxIa3Q+dGxr/tF6FJSx7/zSr4ifJybM9eV1avRyn/sCLGzpjgNlsXijA lrUA== X-Gm-Message-State: AOJu0YzGzX4V58nPM5Tcg1DhNNqmSxD0W2yymUUtrXiBuyQ4ZgUHz5Yw FYyFcdfYbqZW4TN7kcA+CfjSjUvW0QTlBA== X-Received: by 2002:a05:6a00:2496:b0:68b:a137:373e with SMTP id c22-20020a056a00249600b0068ba137373emr2631198pfv.32.1692754643281; Tue, 22 Aug 2023 18:37:23 -0700 (PDT) Received: from squeak.grove.modra.org ([2406:3400:51d:8cc0:ead0:eed5:cfa5:cc5d]) by smtp.gmail.com with ESMTPSA id a18-20020a62bd12000000b00687dde8ae5dsm8239860pff.154.2023.08.22.18.37.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Aug 2023 18:37:22 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 120C911423E0; Wed, 23 Aug 2023 11:07:20 +0930 (ACST) Date: Wed, 23 Aug 2023 11:07:20 +0930 To: binutils@sourceware.org Subject: bfd_get_symbol_leading_char vs. "" Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3033.8 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: INBOX X-GMAIL-THRID: 1774981906281359990 X-GMAIL-MSGID: 1774981906281359990 Some places matching the first char of a string against bfd_get_symbol_leading_char, which may be zero, didn't check for the string being "". This patch adds the check to stop accesses past the end of the string and potential buffer overruns. The dlltool one was found by oss-fuzz quite a while ago. bfd/ * cofflink.c (_bfd_coff_link_input_bfd): Ensure a zero bfd_get_symbol_leading_char doesn't lead to accessing past the zero string terminator. * linker.c (bfd_wrapped_link_hash_lookup): Likewise. (unwrap_hash_lookup): Likewise. binutils/ * dlltool.c (scan_filtered_symbols): Ensure a zero bfd_get_symbol_leading_char doesn't lead to accessing past the zero string terminator. diff --git a/bfd/cofflink.c b/bfd/cofflink.c index aea5c4c38a5..221f6e8183a 100644 --- a/bfd/cofflink.c +++ b/bfd/cofflink.c @@ -1618,7 +1618,8 @@ _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd) /* Ignore fake names invented by compiler; treat them all as the same name. */ if (*name == '~' || *name == '.' || *name == '$' - || (*name == bfd_get_symbol_leading_char (input_bfd) + || (*name + && *name == bfd_get_symbol_leading_char (input_bfd) && (name[1] == '~' || name[1] == '.' || name[1] == '$'))) name = ""; diff --git a/bfd/linker.c b/bfd/linker.c index 0f4f9a1776c..28fffc3ad63 100644 --- a/bfd/linker.c +++ b/bfd/linker.c @@ -544,7 +544,9 @@ bfd_wrapped_link_hash_lookup (bfd *abfd, char prefix = '\0'; l = string; - if (*l == bfd_get_symbol_leading_char (abfd) || *l == info->wrap_char) + if (*l + && (*l == bfd_get_symbol_leading_char (abfd) + || *l == info->wrap_char)) { prefix = *l; ++l; @@ -621,8 +623,9 @@ unwrap_hash_lookup (struct bfd_link_info *info, { const char *l = h->root.string; - if (*l == bfd_get_symbol_leading_char (input_bfd) - || *l == info->wrap_char) + if (*l + && (*l == bfd_get_symbol_leading_char (input_bfd) + || *l == info->wrap_char)) ++l; if (startswith (l, WRAP)) diff --git a/binutils/dlltool.c b/binutils/dlltool.c index 085d4c2ce41..6d63e11e084 100644 --- a/binutils/dlltool.c +++ b/binutils/dlltool.c @@ -1500,7 +1500,8 @@ scan_filtered_symbols (bfd *abfd, void *minisyms, long symcount, bfd_fatal (bfd_get_filename (abfd)); symbol_name = bfd_asymbol_name (sym); - if (bfd_get_symbol_leading_char (abfd) == symbol_name[0]) + if (*symbol_name + && *symbol_name == bfd_get_symbol_leading_char (abfd)) ++symbol_name; def_exports (xstrdup (symbol_name) , 0, -1, 0, 0,