From patchwork Sun Dec 12 22:58:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 48852 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 1419F3858401 for ; Sun, 12 Dec 2021 22:59:17 +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 A2EF43858D39 for ; Sun, 12 Dec 2021 22:59:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A2EF43858D39 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 reform (deer0x12.wildebeest.org [172.31.17.148]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id D9EB730002FC; Sun, 12 Dec 2021 23:59:06 +0100 (CET) Received: by reform (Postfix, from userid 1000) id 70DBA2E83736; Sun, 12 Dec 2021 23:59:05 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [COMMITTED] libdwfl: Don't allocate more than SIZE_MAX in dwfl_segment_report_module. Date: Sun, 12 Dec 2021 23:58:54 +0100 Message-Id: <20211212225854.562257-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-10.0 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 , Evgeny Vereshchagin Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" The code in dwfl_segment_report_module tries to allocate and fill in memory as described in a core file. Normally all memory in filled in through the (phdrs) memory_callback or the read_eagerly callback. If the last callback doesn't work we try to calloc file_trimmed_end bytes and then try to fill in the parts of memory we can from the core file at the correct offsets. file_trimmed_end is a GElf_Off which is an unsigned 64bit type. On 32bit systems this means when cast to a size_t to do an allocation might allocate truncated (much smaller) value. So make sure to not allocate more than SIZE_MAX bytes. It would be nice to have a better way to limit the amount of memory allocated here. A core file might describe really big memory areas for which it doesn't provide any data. In that case we really shouldn't calloc mega- or giga-bytes of zeroed out memory. Reported-by: Evgeny Vereshchagin Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 +++++ libdwfl/dwfl_segment_report_module.c | 3 +++ 2 files changed, 8 insertions(+) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 856bd335..aaea164c 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2021-12-12 Mark Wielaard + + * dwfl_segment_report_module.c (dwfl_segment_report_module): Don't + allocate more than SIZE_MAX. + 2021-12-09 Mark Wielaard * link_map.c (dwfl_link_map_report): Limit dyn_filesz malloc size diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c index f4acfe53..46564ec5 100644 --- a/libdwfl/dwfl_segment_report_module.c +++ b/libdwfl/dwfl_segment_report_module.c @@ -904,6 +904,9 @@ 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; + void *contents = calloc (1, file_trimmed_end); if (unlikely (contents == NULL)) goto out;