From patchwork Fri Dec 24 00:49: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: 49228 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 A38F7389202F for ; Fri, 24 Dec 2021 00:50:06 +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 AA2123858D35 for ; Fri, 24 Dec 2021 00:50:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AA2123858D35 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 02532302FBA8; Fri, 24 Dec 2021 01:49:57 +0100 (CET) Received: by reform (Postfix, from userid 1000) id B60AD2E8373F; Fri, 24 Dec 2021 01:49:56 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libdwfl: Call xlatetom on aligned buffers in dwfl_link_map_report Date: Fri, 24 Dec 2021 01:49:54 +0100 Message-Id: <20211224004954.1302383-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 Errors-To: elfutils-devel-bounces+patchwork=sourceware.org@sourceware.org Sender: "Elfutils-devel" Make sure that when calling xlatetom for Phdrs and Dyns in dwfl_link_map_report the input buffer is correctly aligned by calling memcpy and setting in.d_buf to out.d_buf. https://sourceware.org/bugzilla/show_bug.cgi?id=28720 Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 +++++ libdwfl/link_map.c | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 49a35e41..73d8613c 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2021-12-23 Mark Wielaard + + * link_map.c (dwfl_link_map_report): Call memcpy and set in.d_buf to + out.d_buf before calling xlatetom for unaligned buffers. + 2021-12-23 Mark Wielaard * core-file.c (dwfl_elf_phdr_memory_callback): Check start < diff --git a/libdwfl/link_map.c b/libdwfl/link_map.c index c4f79f11..f57c5585 100644 --- a/libdwfl/link_map.c +++ b/libdwfl/link_map.c @@ -922,11 +922,20 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size, return false; } } + bool is32 = (elfclass == ELFCLASS32); + size_t phdr_align = (is32 + ? __alignof__ (Elf32_Phdr) + : __alignof__ (Elf64_Phdr)); + if (!in_from_exec + && ((uintptr_t) in.d_buf & (phdr_align - 1)) != 0) + { + memcpy (out.d_buf, in.d_buf, in.d_size); + in.d_buf = out.d_buf; + } if (likely ((elfclass == ELFCLASS32 ? elf32_xlatetom : elf64_xlatetom) (&out, &in, elfdata) != NULL)) { - bool is32 = (elfclass == ELFCLASS32); for (size_t i = 0; i < phnum; ++i) { GElf_Word type = (is32 @@ -1044,6 +1053,14 @@ dwfl_link_map_report (Dwfl *dwfl, const void *auxv, size_t auxv_size, }; if (in.d_size > out.d_size) in.d_size = out.d_size; + size_t dyn_align = (elfclass == ELFCLASS32 + ? __alignof__ (Elf32_Dyn) + : __alignof__ (Elf64_Dyn)); + if (((uintptr_t) in.d_buf & (dyn_align - 1)) != 0) + { + memcpy (out.d_buf, in.d_buf, in.d_size); + in.d_buf = out.d_buf; + } if (likely ((elfclass == ELFCLASS32 ? elf32_xlatetom : elf64_xlatetom) (&out, &in, elfdata) != NULL))