From patchwork Sat Dec 18 01:04:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 49081 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 C5198385801C for ; Sat, 18 Dec 2021 01:04:23 +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 A2D943858407 for ; Sat, 18 Dec 2021 01:04:16 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A2D943858407 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 D06BB302FBA5; Sat, 18 Dec 2021 02:04:09 +0100 (CET) Received: by reform (Postfix, from userid 1000) id 110382E8374A; Sat, 18 Dec 2021 02:04:09 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libdwfl: Make sure note data is properly aligned. Date: Sat, 18 Dec 2021 02:04:06 +0100 Message-Id: <20211218010406.876500-1-mark@klomp.org> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, 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" In dwfl_segment_report_module the note data might not be properly aligned. Check that it is before accessing the data directly. Otherwise convert data so it is properly aligned. Also fix NOTE_ALIGN4 and NOTE_ALIGN8 to work correctly with long types. Signed-off-by: Mark Wielaard --- libdwfl/ChangeLog | 5 +++++ libdwfl/dwfl_segment_report_module.c | 5 ++++- libelf/ChangeLog | 5 +++++ libelf/libelfP.h | 4 ++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/libdwfl/ChangeLog b/libdwfl/ChangeLog index 8760b1ef..f18a0c45 100644 --- a/libdwfl/ChangeLog +++ b/libdwfl/ChangeLog @@ -1,3 +1,8 @@ +2021-12-16 Mark Wielaard + + * dwfl_segment_report_module.c (dwfl_segment_report_module): Check + note data is properly aligned. + 2021-12-16 Mark Wielaard * link_map.c (dwfl_link_map_report): Make sure phnum is non-zero. diff --git a/libdwfl/dwfl_segment_report_module.c b/libdwfl/dwfl_segment_report_module.c index f323929e..2263e3cc 100644 --- a/libdwfl/dwfl_segment_report_module.c +++ b/libdwfl/dwfl_segment_report_module.c @@ -517,7 +517,10 @@ dwfl_segment_report_module (Dwfl *dwfl, int ndx, const char *name, assert (sizeof (Elf32_Nhdr) == sizeof (Elf64_Nhdr)); void *notes; - if (ei_data == MY_ELFDATA) + if (ei_data == MY_ELFDATA + && (uintptr_t) data == (align == 8 + ? NOTE_ALIGN8 ((uintptr_t) data) + : NOTE_ALIGN4 ((uintptr_t) data))) notes = data; else { diff --git a/libelf/ChangeLog b/libelf/ChangeLog index 96059eff..617d97a5 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2021-12-16 Mark Wielaard + + * libelfP.h (NOTE_ALIGN4): And with negative unsigned long. + (NOTE_ALIGN8): Likewise. + 2021-12-15 Mark Wielaard * elf_begin.c (get_shnum): Use offsetof to get field of unaligned diff --git a/libelf/libelfP.h b/libelf/libelfP.h index fc1aebec..2c6995bb 100644 --- a/libelf/libelfP.h +++ b/libelf/libelfP.h @@ -603,10 +603,10 @@ extern void __libelf_reset_rawdata (Elf_Scn *scn, void *buf, size_t size, /* Align offset to 4 bytes as needed for note name and descriptor data. This is almost always used, except for GNU Property notes, which use 8 byte padding... */ -#define NOTE_ALIGN4(n) (((n) + 3) & -4U) +#define NOTE_ALIGN4(n) (((n) + 3) & -4UL) /* Special note padding rule for GNU Property notes. */ -#define NOTE_ALIGN8(n) (((n) + 7) & -8U) +#define NOTE_ALIGN8(n) (((n) + 7) & -8UL) /* Convenience macro. */ #define INVALID_NDX(ndx, type, data) \