From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pj1-x1036.google.com (mail-pj1-x1036.google.com [IPv6:2607:f8b0:4864:20::1036]) by sourceware.org (Postfix) with ESMTPS id F06843858298 for ; Mon, 21 Aug 2023 10:43:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F06843858298 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pj1-x1036.google.com with SMTP id 98e67ed59e1d1-26b67b38b61so1806242a91.0 for ; Mon, 21 Aug 2023 03:43:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692614579; x=1693219379; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date:message-id:reply-to; bh=dl1SQrVhhYOJhg+nleeUbN7ArM6NU3SCVkfsiX59lPQ=; b=MNfGP6sbmeJ+auR3LKorbw4c6aF9A7xFFOjR8YbCXLeQK0PIV+O6It3jZSfZtjxEQs YlwloExiIwsVyOMfuy+keW0MAh1Yaw+c63ZcvA2uATtpu0YEsVBc5xo4h2jxue0QAy7G EmJBq4AyYD8h0wI35D41TVHw8zg7tHu96n707r9MmNKs8cosHUm7eTU/9iGVdbrNRX99 VU/132Cs+MwkMNH0eQ4zuLYTH5/icTJ5wj2e4rZ8TQCRXrNFT0Kl4haBfKugm3oyUNub 3WqAbSMFH9EQcvzjPktrsyU/Q7UWafnYfDuSjdYVEhHgBPzkmltJCVEdIxgfzaPlrzeK b3+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692614579; x=1693219379; 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=dl1SQrVhhYOJhg+nleeUbN7ArM6NU3SCVkfsiX59lPQ=; b=gDBohswUP3J4kNHUZAZcBuBhKTSmKvWYJA2bRfrwOwXTKGzB0z6ACuIE5pjb1p8GHP BX8jIxOxQA9EE6zMfeV754cKIljENNIfjX+QcD1odpXwq6lPcxgDF/kgZjv9doeMmlwU ly0VA5cNEgbueas1msSTxFyzKt24LN8QYdIUBoQZk5IPoy1s//bPC0T8hGnA9Lpx+++D gKlueUIkpUoL8B1/lYA0QCvkxNGMGFCrVGzrpjeN78i8iAZwlU0eOUHbicMoUOqPGDIJ /JPTtBnflzuKLXDwaRBhqUk/TJCauzRClXun++JjGCk54eNb5mlRsSXk4NSaN52fiNM8 4OMQ== X-Gm-Message-State: AOJu0YwMJhLQvM2jr5jAX3J2wYylYyqy6C7naOKPA8BAZohPwLQsUWXb bTNAlECzATcqr9UT83vhjWImzh816qU= X-Google-Smtp-Source: AGHT+IGbHZ0N/xp9JBvfk+HcmmacffBjBaQgTiP762naMVskvmmhnKQ3CTgvVcbQEqe8Aj8SZ08Slw== X-Received: by 2002:a17:90a:4963:b0:269:86ce:c3b5 with SMTP id c90-20020a17090a496300b0026986cec3b5mr3344519pjh.44.1692614579303; Mon, 21 Aug 2023 03:42:59 -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 w11-20020a17090aea0b00b002639c4f81cesm7718784pjy.3.2023.08.21.03.42.58 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 21 Aug 2023 03:42:58 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id BABFD11423E6; Mon, 21 Aug 2023 20:12:55 +0930 (ACST) Date: Mon, 21 Aug 2023 20:12:55 +0930 From: Alan Modra To: binutils@sourceware.org Subject: bfd_close_all_done bug and bfd_last_cache Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3034.2 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 List-Id: bfd_close ought to always call iovec->bclose so that cache_bclose is called. If not, bfd_last_cache will be left pointing at freed memory. This bug was found by oss-fuzz with the trigger being an old bug in the ia64-vms support. Given a file of the "wrong" size, elf64_vms_close_and_cleanup attempted to extend it, leading to an error since the file was opened read-only by nm. nm bad_file bad_file then hit the use-after-free when opening the second file. commit 8219cab3f8 fixed multiple bugs of this type in bfd_close and bfd_close_all_done, but didn't go quite far enough. * elf64-ia64-vms.c (elf64_vms_close_and_cleanup): Don't attempt to extend read-only files. * opncls.c (bfd_close_all_done): Always call _close_and_cleanup. An old bug in the ia64-vms support can be used to tickle another bug in bfd_close_all_done. If _close_and_cleanup returns an error, diff --git a/bfd/elf64-ia64-vms.c b/bfd/elf64-ia64-vms.c index f8e9922fc4a..ab1f18b8d81 100644 --- a/bfd/elf64-ia64-vms.c +++ b/bfd/elf64-ia64-vms.c @@ -4713,7 +4713,8 @@ static bool elf64_vms_close_and_cleanup (bfd *abfd) { bool ret = true; - if (bfd_get_format (abfd) == bfd_object) + if (bfd_get_format (abfd) == bfd_object + && bfd_write_p (abfd)) { long isize; diff --git a/bfd/opncls.c b/bfd/opncls.c index 56a90046b1a..741d20e81da 100644 --- a/bfd/opncls.c +++ b/bfd/opncls.c @@ -920,13 +920,11 @@ bfd_close_all_done (bfd *abfd) { bool ret = BFD_SEND (abfd, _close_and_cleanup, (abfd)); - if (ret && abfd->iovec != NULL) - { - ret = abfd->iovec->bclose (abfd) == 0; + if (abfd->iovec != NULL) + ret &= abfd->iovec->bclose (abfd) == 0; - if (ret) - _maybe_make_executable (abfd); - } + if (ret) + _maybe_make_executable (abfd); _bfd_delete_bfd (abfd); free (_bfd_error_buf); -- Alan Modra Australia Development Lab, IBM