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: [PATCH] libdwfl: Make dwfl_segment_report_module aware of maximum Elf size
Date: Fri, 17 Dec 2021 17:56:37 +0100	[thread overview]
Message-ID: <20211217165637.20709-1-mark@klomp.org> (raw)

At the end of dwfl_segment_report_module we might try to read in
the whole contents described by a core file. To do this we first
allocate a zeroed block of memory that is as big as possible. The
core file however may describe much more loaded data than is actually
available in the Elf image. So pass the maximum size so we can
limit the amount of memory we reserve.

Signed-off-by: Mark Wielaard <mark@klomp.org>
---
 libdwfl/ChangeLog                    | 8 ++++++++
 libdwfl/core-file.c                  | 1 +
 libdwfl/dwfl_segment_report_module.c | 5 +++--
 libdwfl/libdwflP.h                   | 1 +
 4 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog
index f849b816..21f3b6a4 100644
--- a/libdwfl/ChangeLog
+++ b/libdwfl/ChangeLog
@@ -1,3 +1,11 @@
+2021-12-17  Mark Wielaard  <mark@klomp.org>
+
+	* libdwflP.h (dwfl_segment_report_module): Add maxread argument.
+	* core-file.c (dwfl_core_file_report): Pass elf->maximum_size to
+	dwfl_segment_report_module.
+	* dwfl_segment_report_module.c (dwfl_segment_report_module): Add
+	maxread argument. Check file_trimmed_end against maxread.
+
 2021-12-08  Mark Wielaard  <mark@klomp.org>
 
 	* dwfl_segment_report_module.c (dwfl_segment_report_module): Add
diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c
index 4e4c9b3c..b04d1d18 100644
--- a/libdwfl/core-file.c
+++ b/libdwfl/core-file.c
@@ -559,6 +559,7 @@ dwfl_core_file_report (Dwfl *dwfl, Elf *elf, const char *executable)
       int seg = dwfl_segment_report_module (dwfl, ndx, NULL,
 					    &dwfl_elf_phdr_memory_callback, elf,
 					    core_file_read_eagerly, elf,
+					    elf->maximum_size,
 					    note_file, note_file_size,
 					    &r_debug_info);
       if (unlikely (seg < 0))
diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c
index f323929e..73704efe 100644
--- a/libdwfl/dwfl_segment_report_module.c
+++ b/libdwfl/dwfl_segment_report_module.c
@@ -294,6 +294,7 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
 			    void *memory_callback_arg,
 			    Dwfl_Module_Callback *read_eagerly,
 			    void *read_eagerly_arg,
+			    size_t maxread,
 			    const void *note_file, size_t note_file_size,
 			    const struct r_debug_info *r_debug_info)
 {
@@ -908,8 +909,8 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
       /* The caller wants to read the whole file in right now, but hasn't
 	 done it for us.  Fill in a local image of the virtual file.  */
 
-      if (file_trimmed_end > SIZE_MAX)
-	goto out;
+      if (file_trimmed_end > maxread)
+	file_trimmed_end = maxread;
 
       void *contents = calloc (1, file_trimmed_end);
       if (unlikely (contents == NULL))
diff --git a/libdwfl/libdwflP.h b/libdwfl/libdwflP.h
index 4344e356..7503a627 100644
--- a/libdwfl/libdwflP.h
+++ b/libdwfl/libdwflP.h
@@ -698,6 +698,7 @@ extern int dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name,
 				       void *memory_callback_arg,
 				       Dwfl_Module_Callback *read_eagerly,
 				       void *read_eagerly_arg,
+				       size_t maxread,
 				       const void *note_file,
 				       size_t note_file_size,
 				       const struct r_debug_info *r_debug_info);
-- 
2.18.4


                 reply	other threads:[~2021-12-17 16:56 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=20211217165637.20709-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).