public inbox for elfutils@sourceware.org
 help / color / mirror / Atom feed
From: Mark Wielaard <mjw@redhat.com>
To: elfutils-devel@lists.fedorahosted.org
Subject: [PATCH] libdwfl: Fix memory leak in link_map.c dwfl_link_map_report.
Date: Tue, 01 Dec 2015 23:17:22 +0100	[thread overview]
Message-ID: <1449008242-6473-1-git-send-email-mjw@redhat.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2068 bytes --]

The phdrs buffer could come from the core file or through the executable.
dwfl_link_map_report would leak the buffer if it came from the executable.
Track where the buffer came from and free appropriately.

Signed-off-by: Mark Wielaard <mjw@redhat.com>
---
 libdwfl/ChangeLog  | 5 +++++
 libdwfl/link_map.c | 9 +++++++--
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index 9640b4f..30a5205 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,5 +1,10 @@
 2015-12-01  Mark Wielaard  <mjw@redhat.com>
 
+	* link_map.c (dwfl_link_map_report): Track whether in.d_buf comes
+	from exec or memory_callback, free as appropriate.
+
+2015-12-01  Mark Wielaard  <mjw@redhat.com>
+
 	* libdwflP.h (struct Dwfl_User_Core): New.
 	(struct DWfl): Replace executable_for_core with user_core.
 	* argp-std.c (parse_opt): Store core and fd in Dwfl user_core.
diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c
index e73b219..2bc0400 100644
--- a/libdwfl/link_map.c
+++ b/libdwfl/link_map.c
@@ -791,6 +791,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 	  bool in_ok = (*memory_callback) (dwfl, phdr_segndx, &in.d_buf,
 					   &in.d_size, phdr, phnum * phent,
 					   memory_callback_arg);
+	  bool in_from_exec = false;
 	  if (! in_ok
 	      && dwfl->user_core != NULL
 	      && dwfl->user_core->executable_for_core != NULL)
@@ -855,6 +856,7 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		  return false;
 		}
 	      in_ok = true;
+	      in_from_exec = true;
 	    }
 	  if (in_ok)
 	    {
@@ -903,8 +905,11 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size,
 		    }
 		}
 
-	      (*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0,
-				  memory_callback_arg);
+	      if (in_from_exec)
+		free (in.d_buf);
+	      else
+		(*memory_callback) (dwfl, -1, &in.d_buf, &in.d_size, 0, 0,
+				    memory_callback_arg);
 	      free (buf);
 	    }
 	  else
-- 
2.5.0

             reply	other threads:[~2015-12-01 22:17 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-01 22:17 Mark Wielaard [this message]
2016-01-02 20:09 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=1449008242-6473-1-git-send-email-mjw@redhat.com \
    --to=mjw@redhat.com \
    --cc=elfutils-devel@lists.fedorahosted.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).