From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 19816 invoked by alias); 3 Mar 2020 11:14:26 -0000 Mailing-List: contact binutils-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: binutils-owner@sourceware.org Received: (qmail 19747 invoked by uid 89); 3 Mar 2020 11:14:26 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,RCVD_IN_DNSWL_NONE,SPF_PASS autolearn=ham version=3.3.1 spammy=2527 X-HELO: mail-pg1-f173.google.com Received: from mail-pg1-f173.google.com (HELO mail-pg1-f173.google.com) (209.85.215.173) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 03 Mar 2020 11:14:24 +0000 Received: by mail-pg1-f173.google.com with SMTP id m5so1405113pgg.0 for ; Tue, 03 Mar 2020 03:14:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=8Y8pe0VbuaPkLgP1MUimpccTaKPq1lF0fBhXblw+SuU=; b=UkWwC744rf9DBTQG90ncmZKp5WCYonT29uoirzIboVQyXLF8ERj3sM9ZYQfYTg+ebB i/HdmHiGY40GBQqLcllxyPY21z8MdYVXKWlouz4PBS/tPqZBAY4sL7HJFF/bY+fyhhGu ZuHDear0mjpvAgnp28jPtTgQ27jtu6IlUUq7cwwHrKg5V8uCvfHY4oyfTo1i8ZsiBcoG rhK56JAwtxHfjoRPg0KU9zKpQ0ov+Z+GK6wLJhyoAMyJi1PrYvJ6D4i5T0AZb9IWq9Kw 844xhB7NEdgKkmr4GQsEJ0qeuv2OBsKbhF/+tQMqraj98zTjtmriez1X7RUdKzt+Q3I2 oX5g== Return-Path: Received: from bubble.grove.modra.org ([2406:3400:51d:8cc0:c198:aaf6:f1b9:1be7]) by smtp.gmail.com with ESMTPSA id 6sm8581869pfx.32.2020.03.03.03.14.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 Mar 2020 03:14:21 -0800 (PST) Received: by bubble.grove.modra.org (Postfix, from userid 1000) id B126889FF9; Tue, 3 Mar 2020 21:44:17 +1030 (ACDT) Date: Tue, 03 Mar 2020 11:14:00 -0000 From: Alan Modra To: binutils@sourceware.org Subject: bfd_check_format_matches preserving matches vs. cleanups Message-ID: <20200303111417.GN5384@bubble.grove.modra.org> References: <20200302085904.GH5384@bubble.grove.modra.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200302085904.GH5384@bubble.grove.modra.org> User-Agent: Mutt/1.9.4 (2018-02-28) X-IsSubscribed: yes X-SW-Source: 2020-03/txt/msg00040.txt It didn't take long for oss-fuzz to find double frees due to a bug in the cleanup logic. It's seen when reading in any alpha-vms object file except when alpha_vms_vec is the default. But alpha_vms_vec is of course the default when building for --target=alpha-dec-vms (and naturally what I used to test the cleanup support since that is the only target with a cleanup that does anything currently). Anyway, the bug is that if bfd_check_format_matches is to preserve a match the cleanup for that match can't be run. Quite obviously that would destroy part of the match state. * format.c (struct bfd_preserve): Add cleanup field. (bfd_preserve_save): Add cleanup param and save. (bfd_preserve_restore): Return cleanup. (bfd_preserve_finish): Call the cleanup for the discarded match. (bfd_check_format_matches): Pass cleanup to bfd_preserve_save, and clear when preserving a match. Restore cleanup too when restoring that match. diff --git a/bfd/format.c b/bfd/format.c index b181742f3b..61f26c6042 100644 --- a/bfd/format.c +++ b/bfd/format.c @@ -106,6 +106,7 @@ struct bfd_preserve unsigned int section_id; struct bfd_hash_table section_htab; const struct bfd_build_id *build_id; + bfd_cleanup cleanup; }; /* When testing an object for compatibility with a particular target @@ -118,7 +119,8 @@ struct bfd_preserve the subset. */ static bfd_boolean -bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve) +bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve, + bfd_cleanup cleanup) { preserve->tdata = abfd->tdata.any; preserve->arch_info = abfd->arch_info; @@ -130,6 +132,7 @@ bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve) preserve->section_htab = abfd->section_htab; preserve->marker = bfd_alloc (abfd, 1); preserve->build_id = abfd->build_id; + preserve->cleanup = cleanup; if (preserve->marker == NULL) return FALSE; @@ -153,7 +156,7 @@ bfd_reinit (bfd *abfd, unsigned int section_id, bfd_cleanup cleanup) /* Restores bfd state saved by bfd_preserve_save. */ -static void +static bfd_cleanup bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) { bfd_hash_table_free (&abfd->section_htab); @@ -172,6 +175,7 @@ bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) its arg, as well as its arg. */ bfd_release (abfd, preserve->marker); preserve->marker = NULL; + return preserve->cleanup; } /* Called when the bfd state saved by bfd_preserve_save is no longer @@ -180,6 +184,15 @@ bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve) static void bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve) { + if (preserve->cleanup) + { + /* Run the cleanup, assuming that all it will need is the + tdata at the time the cleanup was returned. */ + void *tdata = abfd->tdata.any; + abfd->tdata.any = preserve->tdata; + preserve->cleanup (abfd); + abfd->tdata.any = tdata; + } /* It would be nice to be able to free more memory here, eg. old tdata, but that's not possible since these blocks are sitting inside bfd_alloc'd memory. The section hash is on a separate @@ -252,7 +265,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) save_targ = abfd->xvec; preserve_match.marker = NULL; - if (!bfd_preserve_save (abfd, &preserve)) + if (!bfd_preserve_save (abfd, &preserve, NULL)) goto err_ret; /* If the target type was explicitly specified, just check that target. */ @@ -381,8 +394,9 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) if (preserve_match.marker == NULL) { match_targ = abfd->xvec; - if (!bfd_preserve_save (abfd, &preserve_match)) + if (!bfd_preserve_save (abfd, &preserve_match, cleanup)) goto err_ret; + cleanup = NULL; } } } @@ -455,7 +469,7 @@ bfd_check_format_matches (bfd *abfd, bfd_format format, char ***matching) whole bfd and restoring it would be even worse; the first thing you notice is that the cached bfd file position gets out of sync. */ if (preserve_match.marker != NULL) - bfd_preserve_restore (abfd, &preserve_match); + cleanup = bfd_preserve_restore (abfd, &preserve_match); if (match_count == 1) { -- Alan Modra Australia Development Lab, IBM