From patchwork Thu Jun 23 21:49:32 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Palmer Dabbelt X-Patchwork-Id: 55351 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 AA2E5384D1AC for ; Thu, 23 Jun 2022 21:51:03 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pj1-x102f.google.com (mail-pj1-x102f.google.com [IPv6:2607:f8b0:4864:20::102f]) by sourceware.org (Postfix) with ESMTPS id 7612C3850438 for ; Thu, 23 Jun 2022 21:50:12 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7612C3850438 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pj1-x102f.google.com with SMTP id a11-20020a17090acb8b00b001eca0041455so4191964pju.1 for ; Thu, 23 Jun 2022 14:50:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; h=subject:date:message-id:mime-version:content-transfer-encoding:cc :from:to; bh=MXX40FISS2JjqtUyDbLpF5zE+5T3f2tP4frrnJQzY6w=; b=HwU9ijOYzpL//n3Xwln6vrHodWdHb0O8c82bZqpJmQJepwvmtPEG159RF89Jvd1Ej7 vgHyU8sURfYAEMmgtkVVE+gkIesDHfhNI+bSF1jZB/So7yeHKKDYZL5jfe9gwDcdZTU4 ZPAsZDFgn+Dc+OQLeTwPNG8bbxNrpw45B4fcm4jWrTnKifuqyEps1WlBUoG4uZ/2/kl+ rh2cc+mT4/Sv7peMEVQ3K+FXFag457gzyPYoGmJEOuuua7dbPRZKEFpxj9NbNoS4ek42 TNUxRUXO0tYLJt6HYWaRk6TzNRtREueld6BjxUanMMvdCQkn6lmyN0XYdpGL4bDqrUbh GFZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:subject:date:message-id:mime-version :content-transfer-encoding:cc:from:to; bh=MXX40FISS2JjqtUyDbLpF5zE+5T3f2tP4frrnJQzY6w=; b=kT4YkPm6FB9fQLp+g4um+GoTa8kz3eKMxLUfxX+R3tH7Hjbde4p41irlVbe40D8MQP fTvwH6XFtHnkuJ2V2tgizA8GxP3EqAZq+z5dHGcCoQVgUzXkQK0CDZF6YsnvNrOnTklR X2YgYK2LryPnrzAkwarq7UlWpC0ofymL/c1JnhBSw0j8Dzbtjz+iVeHV9+s4BEX3qnE9 yOAbqTmW6BZLhA3QaHXuoX9uWDP0NgPLDuR4dQo8V9RGbzj2imc/ErLaR0mNqFHXr1mM ebGa8pflERYU/gNndsYMAohuv7YNJIDNlGN0FJ3PGoxKXtxRYVam/prIoWsZURY661KM eP5Q== X-Gm-Message-State: AJIora9rNXIx5O/ZfycnOw6N7jvQZk+gu5Y4LPEuQPKlWrb1/sPAFN+3 nidenqB+8USJTBpXpEyfHnfG0w== X-Google-Smtp-Source: AGRyM1tveVPblOr+MJwy+V3jQOR8Bm8Y7OxgYr+Woo2Qvci0WmrAoLF4OyyVG78Ev1OwPkR9BY8hzg== X-Received: by 2002:a17:902:8e8b:b0:168:a135:d636 with SMTP id bg11-20020a1709028e8b00b00168a135d636mr39320464plb.140.1656021011482; Thu, 23 Jun 2022 14:50:11 -0700 (PDT) Received: from localhost ([12.3.194.138]) by smtp.gmail.com with ESMTPSA id a23-20020a17090a6d9700b001eccb13dfb0sm2472814pjk.4.2022.06.23.14.50.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jun 2022 14:50:10 -0700 (PDT) Subject: [PATCH] arm: Avoid UB in elf_machine_rel() Date: Thu, 23 Jun 2022 14:49:32 -0700 Message-Id: <20220623214932.4752-1-palmer@rivosinc.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 From: Palmer Dabbelt To: libc-alpha@sourceware.org X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" This recently came up during a cleanup to remove misaligned accesses from the RISC-V port. Link: https://sourceware.org/pipermail/libc-alpha/2022-June/139961.html Suggested-by: Adhemerval Zanella Signed-off-by: Palmer Dabbelt Reviewed-by: Fangrui Song --- I haven't build this one, as I'm just doing it locally and don't have everything setup here. --- sysdeps/arm/dl-machine.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h index 68dcb96d9d..6bb3917340 100644 --- a/sysdeps/arm/dl-machine.h +++ b/sysdeps/arm/dl-machine.h @@ -402,10 +402,7 @@ elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[], break; case R_ARM_ABS32: { - struct unaligned - { - Elf32_Addr x; - } __attribute__ ((packed, may_alias)); + ElfW(Addr) tmp; # ifndef RTLD_BOOTSTRAP /* This is defined in rtld.c, but nowhere in the static libc.a; make the reference weak so static programs can @@ -425,7 +422,9 @@ elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[], value -= SYMBOL_ADDRESS (map, refsym, true); # endif /* Support relocations on mis-aligned offsets. */ - ((struct unaligned *) reloc_addr)->x += value; + memcpy(&tmp, reloc_addr, sizeof tmp); + tmp += value; + memcpy (reloc_addr, &tmp, sizeof tmp); break; } case R_ARM_TLS_DESC: