From patchwork Wed Dec 15 22:05:44 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Wielaard X-Patchwork-Id: 48991 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 C05D53858413 for ; Wed, 15 Dec 2021 22:06:48 +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 16D80385841C for ; Wed, 15 Dec 2021 22:05:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 16D80385841C 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 82D73302FBA5; Wed, 15 Dec 2021 23:05:48 +0100 (CET) Received: by reform (Postfix, from userid 1000) id DE0632E83743; Wed, 15 Dec 2021 23:05:47 +0100 (CET) From: Mark Wielaard To: elfutils-devel@sourceware.org Subject: [PATCH] libelf: Use offsetof to get field of unaligned Date: Wed, 15 Dec 2021 23:05:44 +0100 Message-Id: <20211215220544.625735-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" gcc undefined sanitizer flags: elf_begin.c:230:18: runtime error: member access within misaligned address 0xf796400a for type 'struct Elf64_Shdr', which requires 4 byte alignment struct. This seems a wrong warning since we aren't accessing the field member of the struct, but are taking the address of it. But we can do the same by adding the field offsetof to the base address. Which doesn't trigger a runtime error. Signed-off-by: Mark Wielaard --- libelf/ChangeLog | 5 +++++ libelf/elf_begin.c | 15 +++++++++------ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/libelf/ChangeLog b/libelf/ChangeLog index 041da9b1..96059eff 100644 --- a/libelf/ChangeLog +++ b/libelf/ChangeLog @@ -1,3 +1,8 @@ +2021-12-15 Mark Wielaard + + * elf_begin.c (get_shnum): Use offsetof to get field of unaligned + struct. + 2021-09-06 Dmitry V. Levin * common.h (allocate_elf): Remove cast of calloc return value. diff --git a/libelf/elf_begin.c b/libelf/elf_begin.c index 93d1e12f..bd3399de 100644 --- a/libelf/elf_begin.c +++ b/libelf/elf_begin.c @@ -1,5 +1,6 @@ /* Create descriptor for processing file. Copyright (C) 1998-2010, 2012, 2014, 2015, 2016 Red Hat, Inc. + Copyright (C) 2021 Mark J. Wielaard This file is part of elfutils. Written by Ulrich Drepper , 1998. @@ -170,9 +171,10 @@ get_shnum (void *map_address, unsigned char *e_ident, int fildes, if (likely (map_address != NULL)) /* gcc will optimize the memcpy to a simple memory access while taking care of alignment issues. */ - memcpy (&size, &((Elf32_Shdr *) ((char *) map_address - + ehdr.e32->e_shoff - + offset))->sh_size, + memcpy (&size, ((char *) map_address + + ehdr.e32->e_shoff + + offset + + offsetof (Elf32_Shdr, sh_size)), sizeof (Elf32_Word)); else if (unlikely ((r = pread_retry (fildes, &size, @@ -227,9 +229,10 @@ get_shnum (void *map_address, unsigned char *e_ident, int fildes, if (likely (map_address != NULL)) /* gcc will optimize the memcpy to a simple memory access while taking care of alignment issues. */ - memcpy (&size, &((Elf64_Shdr *) ((char *) map_address - + ehdr.e64->e_shoff - + offset))->sh_size, + memcpy (&size, ((char *) map_address + + ehdr.e64->e_shoff + + offset + + offsetof (Elf64_Shdr, sh_size)), sizeof (Elf64_Xword)); else if (unlikely ((r = pread_retry (fildes, &size,