From patchwork Sun May 26 17:01:24 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 1939367 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=RIeq6qOv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4VnQ6h3LJsz20Vy for ; Mon, 27 May 2024 03:01:48 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B9BEF3858CDB for ; Sun, 26 May 2024 17:01:46 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id 592F93858D28 for ; Sun, 26 May 2024 17:01:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 592F93858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 592F93858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::629 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716742890; cv=none; b=v4PvE/5sfrHtEHrPUtZAVA5sm1cVYKfO6FrxFXfLJOaw1/EWP308m1v5x91byG7AHGMX6b0SaDwFGb/0wpDBmGMN6xWr2ISjhKgQYWWettgOLGtY0bknX/j0rzSXH07VxXGyPBdAGJ+0dsBX8RjrTl+nqvzFNs7OHsT8gz4NJP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716742890; c=relaxed/simple; bh=q/gUbZ5P+e1wTCT8a67QXDfFyaFRr9SPg7prD5J3IXY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=sReQBRvbXrU+cX1fV+AnfaDodO02IndHBMpxarJhPaRBNWX4VSJ7955aRTFAEnA2n6njyiBZyLGTT3Vh/ENU+/rWhh2i07WMqrzAyyF4KgIKVLtSme45UcHgDYY2CcwSEcqpc74a3zalbkppvSFvz6KQ+bp8BrdP+3+VKu4x/yg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1f44b5b9de6so17664555ad.3 for ; Sun, 26 May 2024 10:01:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716742887; x=1717347687; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=bwsfaZ/DkOMg5pxuSecuEKsqu3M8h9uX5XEqjHVcSJk=; b=RIeq6qOv+lNmmbWRPTALJp/8vxJrSYSUgYeRV86BvL7RS5YtQdxOIigiDM36NR74io m7ykdpnqR524Qz70D2bSw0CFrXy4K3adlDrsZ6zsgIh/qJPRc1NpkQZeAVMTVyRUtcEa J/UB717HdgHtpD1NPEKsCkR5Jk6c3vCWipnlXDfn5gbUmCBTNdmdke1KpoTadvuQsa+3 Yr3Tmc9NBMQHwtsbUsVpDk2FwnLNULIWMkqtxYcNqGzL4RSL0A/8eXfdHIcFvPIMT3dG U6A1pOEliVF42Z5zvlZ9cVfKCHqbhwLm3X210N3G1q6GnSM3VInfTiq3XRji2cSbYiDr RwzA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716742887; x=1717347687; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=bwsfaZ/DkOMg5pxuSecuEKsqu3M8h9uX5XEqjHVcSJk=; b=WA4uyxvvqpV/Xlf3d9vOwrvSLMNvI20yp9987Yc0DH/HIWU8/Bh87AFaXAPqtsbBQV or/A6vQQIbnAZlC1LEzP9ncxuvcI1mq6a409e0xukgeI5MZf00bK2sJxXUFGC8t8xSVy fYNhXo9hEuD+L+h9XbjrfZKdjKTJDfrzcuFVYidmPC0pkm7suW3aCunFaaLNOOSoWuMX sB18dVEikViIje2aHNmXmYYqNMnibGafsaicYYCd5xMc4gMTESnvsN1J0xhy++sWaRWK 8rIhFCPC/UuReMQDXfT6MLE8ulZfiDGxUiHXOgohqhfOJYj+Spl+p08oEanBsHg8hGtv bOCg== X-Gm-Message-State: AOJu0YyM7HArqcau07I8IBqMvhUejT73ciz3cl/Naw47LVp9uEmwH5Pa 9Dq56e+DcqoVsk8wRMFJEBMUTwlKge44Hj9r5Dtb+JD0BgBj3Jm2nSk/1sED X-Google-Smtp-Source: AGHT+IGWg7qfu7ZlEy8EXvYc/VE0p6PWLC6kmgQsKMngwwmavgOPO+/00CMPGal+MxoY4hmeHnvOhQ== X-Received: by 2002:a17:902:c403:b0:1e4:4ade:f504 with SMTP id d9443c01a7336-1f449029c88mr76101695ad.46.1716742886818; Sun, 26 May 2024 10:01:26 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.129.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f44c9ab7bfsm45202195ad.233.2024.05.26.10.01.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 10:01:26 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 2D510740601 for ; Sun, 26 May 2024 10:01:25 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v4] elf: Handle static PIE loaded at the specific address [BZ #31799] Date: Sun, 26 May 2024 10:01:24 -0700 Message-ID: <20240526170124.2061051-1-hjl.tools@gmail.com> X-Mailer: git-send-email 2.45.1 MIME-Version: 1.0 X-Spam-Status: No, score=-3021.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org When a static PIE is loaded at the specific address, its PT_DYNAMIC segment entries contain the relocated values for the load address and we must keep the l_addr field as 0 to avoid relocating them again if the e_type field in the ELF header isn't ET_DYN. This fixes BZ #31799. Signed-off-by: H.J. Lu --- configure | 68 ++++++++++++++++++++++++++++++++++++ configure.ac | 31 ++++++++++++++++ elf/Makefile | 20 +++++++++++ elf/dl-reloc-static-pie.c | 11 ++++-- elf/tst-pie-address-static.c | 19 ++++++++++ elf/tst-pie-address.c | 28 +++++++++++++++ 6 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 elf/tst-pie-address-static.c create mode 100644 elf/tst-pie-address.c diff --git a/configure b/configure index 49b093043c..b7a83f1970 100755 --- a/configure +++ b/configure @@ -7820,6 +7820,74 @@ printf "%s\n" "$libc_cv_cc_pie_default" >&6; } config_vars="$config_vars cc-pie-default = $libc_cv_cc_pie_default" +# Get PDE load address. +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking PDE load address" >&5 +printf %s "checking PDE load address... " >&6; } +if test ${libc_cv_pde_load_address+y} +then : + printf %s "(cached) " >&6 +else $as_nop + cat > conftest.S <&5 2>&5; then + # Get the load address of the first PT_LOAD segment. + libc_cv_pde_load_address=$(LC_ALL=C $READELF -Wl conftest \ + | grep LOAD \ + | $AWK '{ print $3; exit 0; }') +else + as_fn_error $? "${CC-cc} can not create PDE" "$LINENO" 5 +fi +rm -f conftest* +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_cv_pde_load_address" >&5 +printf "%s\n" "$libc_cv_pde_load_address" >&6; } +config_vars="$config_vars +pde-load-address = $libc_cv_pde_load_address" + +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking for linker that supports -Ttext-segment=$libc_cv_pde_load_address" >&5 +printf %s "checking for linker that supports -Ttext-segment=$libc_cv_pde_load_address... " >&6; } +libc_linker_feature=no +cat > conftest.c <&5 + (eval $ac_try) 2>&5 + ac_status=$? + printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } +then + if ${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS $no_ssp -Wl,-Ttext-segment=$libc_cv_pde_load_address -nostdlib \ + -nostartfiles -fPIC -shared -o conftest.so conftest.c 2>&1 \ + | grep "warning: -Ttext-segment=$libc_cv_pde_load_address ignored" > /dev/null 2>&1; then + true + else + libc_linker_feature=yes + fi +fi +rm -f conftest* +if test $libc_linker_feature = yes; then + libc_cv_load_address=-Wl,-Ttext-segment +else + libc_cv_load_address= +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_linker_feature" >&5 +printf "%s\n" "$libc_linker_feature" >&6; } +config_vars="$config_vars +load-address-ldflag = $libc_cv_load_address" + { printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking if we can build programs as PIE" >&5 printf %s "checking if we can build programs as PIE... " >&6; } cat confdefs.h - <<_ACEOF >conftest.$ac_ext diff --git a/configure.ac b/configure.ac index e48957f318..4ffd339be1 100644 --- a/configure.ac +++ b/configure.ac @@ -1721,6 +1721,37 @@ fi rm -f conftest.*]) LIBC_CONFIG_VAR([cc-pie-default], [$libc_cv_cc_pie_default]) +# Get PDE load address. +AC_CACHE_CHECK([PDE load address], + libc_cv_pde_load_address, [dnl +cat > conftest.S <&AS_MESSAGE_LOG_FD 2>&AS_MESSAGE_LOG_FD; then + # Get the load address of the first PT_LOAD segment. + libc_cv_pde_load_address=$(LC_ALL=C $READELF -Wl conftest \ + | grep LOAD \ + | $AWK '{ print $3; exit 0; }') +else + AC_MSG_ERROR([${CC-cc} can not create PDE]) +fi +rm -f conftest*]) +LIBC_CONFIG_VAR([pde-load-address], [$libc_cv_pde_load_address]) + +LIBC_LINKER_FEATURE([-Ttext-segment=$libc_cv_pde_load_address], + [-Wl,-Ttext-segment=$libc_cv_pde_load_address], + [libc_cv_load_address=-Wl,-Ttext-segment], + [libc_cv_load_address=]) +LIBC_CONFIG_VAR([load-address-ldflag], [$libc_cv_load_address]) + AC_MSG_CHECKING(if we can build programs as PIE) AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#ifdef PIE_UNSUPPORTED # error PIE is not supported diff --git a/elf/Makefile b/elf/Makefile index 57b3a19d36..4e2d123b8e 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1046,6 +1046,25 @@ tests-pie += \ tst-pie1 \ tst-pie2 \ # tests-pie +ifneq (,$(load-address-ldflag)) +tests += \ + tst-pie-address \ + # tests +tests-pie += \ + tst-pie-address \ + # tests-pie +LDFLAGS-tst-pie-address += $(load-address-ldflag)=$(pde-load-address) +ifeq (yes,$(enable-static-pie)) +tests += \ + tst-pie-address-static \ + # tests +tests-static += \ + tst-pie-address-static \ + # tests-static +LDFLAGS-tst-pie-address-static += \ + $(load-address-ldflag)=$(pde-load-address) +endif +endif ifeq (yes,$(have-protected-data)) tests += vismain tests-pie += vismain @@ -1896,6 +1915,7 @@ $(objpfx)tst-array5-static-cmp.out: tst-array5-static.exp \ CFLAGS-tst-pie1.c += $(pie-ccflag) CFLAGS-tst-pie2.c += $(pie-ccflag) +CFLAGS-tst-pie-address.c += $(pie-ccflag) $(objpfx)tst-piemod1.so: $(libsupport) $(objpfx)tst-pie1: $(objpfx)tst-piemod1.so diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index 10c23d0bf0..57ed4c1a9d 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -37,11 +37,16 @@ _dl_relocate_static_pie (void) { struct link_map *main_map = _dl_get_dl_main_map (); - /* Figure out the run-time load address of static PIE. */ - main_map->l_addr = elf_machine_load_address (); + /* Figure out the run-time load address of static PIE. Must keep the + l_addr field as 0 if static PIE is marked ET_EXEC, which is loaded + at the specific address, since its PT_DYNAMIC segment entries contain + the relocated values for the load address. */ + ElfW(Ehdr) *ehdr = (ElfW(Ehdr) *) elf_machine_load_address (); + if (ehdr->e_type == ET_DYN) + main_map->l_addr = (ElfW(Addr)) ehdr; /* Read our own dynamic section and fill in the info array. */ - main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ()); + main_map->l_ld = ((void *) ehdr + elf_machine_dynamic ()); const ElfW(Phdr) *ph, *phdr = GL(dl_phdr); size_t phnum = GL(dl_phnum); diff --git a/elf/tst-pie-address-static.c b/elf/tst-pie-address-static.c new file mode 100644 index 0000000000..25d27fb962 --- /dev/null +++ b/elf/tst-pie-address-static.c @@ -0,0 +1,19 @@ +/* Test static PIE loaded at the specific address. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include "tst-pie-address.c" diff --git a/elf/tst-pie-address.c b/elf/tst-pie-address.c new file mode 100644 index 0000000000..1f01783631 --- /dev/null +++ b/elf/tst-pie-address.c @@ -0,0 +1,28 @@ +/* Test PIE loaded at the specific address. + Copyright (C) 2024 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include + +static int +do_test (void) +{ + printf ("Hello\n"); + return 0; +} + +#include