public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
* [COMMITTED] readelf: Fix use-after-free ebl pointer issue
@ 2023-03-03 13:26 Mark Wielaard
  0 siblings, 0 replies; only message in thread
From: Mark Wielaard @ 2023-03-03 13:26 UTC (permalink / raw)
  To: elfutils-devel; +Cc: Mark Wielaard

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-03-03 13:26 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-03 13:26 [COMMITTED] readelf: Fix use-after-free ebl pointer issue Mark Wielaard

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).