From 3831c0b2c2334b129a9d04f12dc7549db412d3bd Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Fri, 17 Dec 2021 17:43:19 +0100 Subject: [PATCH] libdwfl: Make dwfl_segment_report_module aware of maximum Elf size 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 --- 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 f18a0c457..6a3e041bd 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,11 @@ +2021-12-17 Mark Wielaard + + * 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-16 Mark Wielaard * dwfl_segment_report_module.c (dwfl_segment_report_module): Check diff --git a/libdwfl/core-file.c b/libdwfl/core-file.c index 4e4c9b3cf..b04d1d18b 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 2263e3cc1..3e87d2070 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) { @@ -911,8 +912,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 4344e356b..7503a6273 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.47.3