From patchwork Fri Dec 17 16:56:37 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 49057 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 652183858001 for ; Fri, 17 Dec 2021 16:58:26 +0000 (GMT) X-Original-To: elfutils-devel@sourceware.org Delivered-To: elfutils-devel@sourceware.org Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 8A4EA385843F for ; Fri, 17 Dec 2021 16:56:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8A4EA385843F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from tarox.wildebeest.org (83-87-18-245.cable.dynamic.v4.ziggo.nl [83.87.18.245]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 9889E302FBA5; Fri, 17 Dec 2021 17:56:39 +0100 (CET) Received: by tarox.wildebeest.org (Postfix, from userid 1000) id D74ED425A458; Fri, 17 Dec 2021 17:56:38 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libdwfl: Make dwfl_segment_report_module aware of maximum Elf size Date: Fri, 17 Dec 2021 17:56:37 +0100 Message-Id: <20211217165637.20709-1-mark@klomp.org> X-Mailer: git-send-email 2.18.4 X-Spam-Status: No, score=-10.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: elfutils-devel@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Elfutils-devel mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , Cc: Mark Wielaard Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" 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 f849b816..21f3b6a4 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-08 Mark Wielaard * 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);