From: Mark Wielaard <mark@klomp.org>
To: elfutils-devel@sourceware.org
Cc: Mark Wielaard <mark@klomp.org>
Subject: [PATCH] libasm: Fix use-after-free issue with circular single linked list cleanup
Date: Fri, 17 Feb 2023 15:00:27 +0100 [thread overview]
Message-ID: <20230217140027.125332-1-mark@klomp.org> (raw)
Pointed out by gcc 12 with -Wuse-after-free=3
In function ‘free_section’
asm_end.c:552:17: error: pointer ‘data’ used after ‘free’ [-Werror=use-after-free]
552 | while (oldp != scnp->content);
| ~~~~~^~~~~~~~~~~~~~~~
asm_end.c:550:9: note: call to ‘free’ here
550 | free (oldp);
| ^~~~~~~~~~~
Fix by freeing scnp->content last.
Signed-off-by: Mark Wielaard <mark@klomp.org>
---
libasm/ChangeLog | 4 ++++
libasm/asm_end.c | 18 ++++++++++--------
2 files changed, 14 insertions(+), 8 deletions(-)
diff --git a/libasm/ChangeLog b/libasm/ChangeLog
index a12d14b3..f23d5914 100644
--- a/libasm/ChangeLog
+++ b/libasm/ChangeLog
@@ -1,3 +1,7 @@
+2023-02-17 Mark Wielaard <mark@klomp.org>
+
+ * asm_end.c (free_section): free scnp->content last.
+
2022-12-20 Mark Wielaard <mark@klomp.org>
* disasm_begin.c: Include libeblP.h.
diff --git a/libasm/asm_end.c b/libasm/asm_end.c
index c06d2366..29165ac4 100644
--- a/libasm/asm_end.c
+++ b/libasm/asm_end.c
@@ -541,16 +541,18 @@ free_section (AsmScn_t *scnp)
if (scnp->subnext != NULL)
free_section (scnp->subnext);
+ /* This is a circular single linked list. */
struct AsmData *data = scnp->content;
if (data != NULL)
- do
- {
- oldp = data;
- data = data->next;
- free (oldp);
- }
- while (oldp != scnp->content);
-
+ {
+ while (data != scnp->content)
+ {
+ oldp = data;
+ data = data->next;
+ free (oldp);
+ }
+ free (scnp->content);
+ }
free (scnp);
}
--
2.39.1
next reply other threads:[~2023-02-17 14:00 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-17 14:00 Mark Wielaard [this message]
2023-02-21 12:22 ` Mark Wielaard
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=20230217140027.125332-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).