From: Mark Wielaard Date: Wed, 8 Dec 2021 13:26:54 +0000 (+0100) Subject: libdwfl: Don't read beyond end of file in dwfl_segment_report_module X-Git-Tag: elfutils-0.187~77 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3038ee821b29147f2b036372937747f150f2f755;p=thirdparty%2Felfutils.git libdwfl: Don't read beyond end of file in dwfl_segment_report_module The ELF might not be fully mapped into memory (which probably means the phdrs are bogus). Don't try to read beyond what we have in memory already. Reported-by: Evgeny Vereshchagin Signed-off-by: Mark Wielaard --- diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 57b2c494c..b2a8752a0 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2021-12-08 Mark Wielaard + + * dwfl_segment_report_module.c (dwfl_segment_report_module): Don't + read beyond of (actual) end of (memory) file. + 2021-11-18 Matthias Maennich * linux-kernel-modules.c (dwfl_linux_kernel_report_modules): diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c index ee9cfa2e5..f6a1799ef 100644 --- a/libdwfl/dwfl_segment_report_module.c +++ b/libdwfl/dwfl_segment_report_module.c @@ -924,8 +924,12 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name, GElf_Off offset = is32 ? p32[i].p_offset : p64[i].p_offset; GElf_Xword filesz = is32 ? p32[i].p_filesz : p64[i].p_filesz; + /* Don't try to read beyond the actual end of file. */ + if (offset >= file_trimmed_end) + continue; + void *into = contents + offset; - size_t read_size = filesz; + size_t read_size = MIN (filesz, file_trimmed_end - offset); (*memory_callback) (dwfl, addr_segndx (dwfl, segment, vaddr + bias, false), &into, &read_size, vaddr + bias, read_size,