public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [COMMITTED] readelf: Fix use-after-free ebl pointer issue
Date: Fri,  3 Mar 2023 14:26:31 +0100	[thread overview]
Message-ID: <20230303132631.219940-1-mark@klomp.org> (raw)

With -flto gcc 13 sees that we use the ebl pointer after closing and
freeing it.

In function ‘process_elf_file’,
    inlined from ‘process_dwflmod’ at readelf.c:818:3:
readelf.c:1070:6: error: pointer ‘ebl_18’ used after ‘free’ [-Werror=use-after-free]
 1070 |   if (pure_ebl != ebl)
      |      ^
In function ‘ebl_closebackend’,
    inlined from ‘process_elf_file’ at readelf.c:1068:3,
    inlined from ‘process_dwflmod’ at readelf.c:818:3:
../libebl/eblclosebackend.c:47:7: note: call to ‘free’ here
   47 |       free (ebl);
      |       ^

Fix by only calling ebl_closebackend after using it in the comparison.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 src/ChangeLog | 5 +++++
 src/readelf.c | 5 +++--
 2 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 699d98ee..c26dafdd 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,3 +1,8 @@
+2023-03-03  Mark Wielaard  <mark@klomp.org>
+
+	* readelf (process_elf_file): ebl_closebackend only after
+	comparing ebl pointer.
+
 2023-02-12  Mark Wielaard  <mark@klomp.org>
 
 	* readelf.c (print_attributes): Add comment about check.
diff --git a/src/readelf.c b/src/readelf.c
index 0f13874f..6950204e 100644
--- a/src/readelf.c
+++ b/src/readelf.c
@@ -1065,13 +1065,14 @@ process_elf_file (Dwfl_Module *dwflmod, int fd)
   if (print_string_sections)
     print_strings (ebl);
 
-  ebl_closebackend (ebl);
-
   if (pure_ebl != ebl)
     {
+      ebl_closebackend (ebl);
       ebl_closebackend (pure_ebl);
       elf_end (pure_elf);
     }
+  else
+    ebl_closebackend (ebl);
 }
 
 
-- 
2.39.2


                 reply	other threads:[~2023-03-03 13:26 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230303132631.219940-1-mark@klomp.org \
    --to=mark@klomp.org \
    --cc=elfutils-devel@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).