libdwfl: Make dwfl_segment_report_module aware of maximum Elf size

Message ID 20211217165637.20709-1-mark@klomp.org
State Committed
Headers
Series libdwfl: Make dwfl_segment_report_module aware of maximum Elf size |

Commit Message

Mark Wielaard Dec. 17, 2021, 4:56 p.m. UTC
  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(-)
  

Patch

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