From patchwork Sun May 26 16:11:08 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: 1939363 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=WotWWXUy; 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 4VnP0q4Pf2z1ydW for ; Mon, 27 May 2024 02:11:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3B3443858403 for ; Sun, 26 May 2024 16:11:30 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by sourceware.org (Postfix) with ESMTPS id 408D83858C33 for ; Sun, 26 May 2024 16:11:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 408D83858C33 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 408D83858C33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716739873; cv=none; b=j6MHJS328elu27OIqdD4pCh/P4ZgMiEFu5yM0A/h9vpWh5QUKchCzNk4622kWQhpgv75xZ8yljNGOFg7N/8rBkvczOXZZJf9HzPUiPWZLijpqqv2SFFJ968JdG1WCq8U0njb7WQgj8bsl92imVq0fiSghX/f1L81ncQVCJvcYL8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716739873; c=relaxed/simple; bh=SNfz3P8GCk/eSyVSovYu2sE4hjoj5OIJHWG0akI9eUk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=C4nUCwXGSC0Xuv+EBOcofl5pPCdiKIqRWNKZH0oHf2ghzryHwZHJfPZHvHI5GmbCmE0EOY71Gq1ia/SRIu5uzeLia5bu9brwO/tYIsy3Kv7tsd0NSgV/JmlvOfeOThMBLHCw+EPo8LzKqDtW5LWN3ofTWRxPkzU3sXNoKs+7xow= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52b.google.com with SMTP id 41be03b00d2f7-681953ad4f2so1807888a12.2 for ; Sun, 26 May 2024 09:11:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716739870; x=1717344670; 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=7A745K7/0shXl+rBiM0OIyOg3m5zKiCmSXe7CQQn4Ug=; b=WotWWXUyuirxaz3hfBYAUElGnBgYFIr0jYxK/Riv6YCmQjBCdmdrnCofVkb/uhprrR VQtXULe9CWC7L5nlDCEU3RlDBsq9TdI+t7KtgJupHIpk5aUjRGl1IZC8M3JErR3DcnfY PTTrjD8bpT8r/YvO7dBYlImczb4wM72FKp9x9VSSXkDffvpx7XcLLusCzf797y7m+QcG 4dlQ39oze/IPNe8fvfU9xz3E6B977RGxXhQYHn7tNrWNCSOGwy/+RdpP1vv7PSFEmmhl 8wqzXvk7AjBLqnr+1HDVtKHpx0oIP9Jv1omBxKlLnQtYsD2NpqfNSHcRxykMQiDZ2vBP 7e+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716739870; x=1717344670; 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=7A745K7/0shXl+rBiM0OIyOg3m5zKiCmSXe7CQQn4Ug=; b=eUIOONQFRRCsnruZcmE8ocggcCfrmEkD0McLy7E/WHH594jZ/yF2oNXd0y0s1fD0NN mbBiaVIAgAfkk1p79vZF448P8a3zpM5NL+IJoiYy3efpZ6/35e/0KFsMU0pwtEQdKPmp nngescs+Tr3h+pUim4PaAtj3cKW4ltWwE7wh3fDd3NKeYvrI6CjSBUOwQX5/O7pRzNkH 5PSYN5a9/NDHzHZOMpZlUEd2+PSNvr2FXgjAmbMwHPgta//G/aRPxwwdq+sbJ5qHt6yX /WTbY9u+zJtkSdw0CNt4A2ur902qZn6W27Zzr9bSULTEdQsxfXt6eluwxYdtZJVYh4X1 FlFw== X-Gm-Message-State: AOJu0YwQtz9w2Ni5uIIjSFGEH0zGBroDYWSrrNXgUgWsRJHhT29jbzsL v62w3b6PQqWQGOjIv/NRItgWtaOnGt4H0VuQ8px+l2JzVq13LRGHPvQx2FhY X-Google-Smtp-Source: AGHT+IFaBhKzXjIaXcyJqABAeGB48c541qNc+2R8ccIRau6WKLS1BzKQieXkw4mKTxFDJjVVL1jpvw== X-Received: by 2002:a17:903:2451:b0:1f3:dfe:3373 with SMTP id d9443c01a7336-1f4486d1f83mr86759535ad.7.1716739869755; Sun, 26 May 2024 09:11:09 -0700 (PDT) Received: from gnu-cfl-3.localdomain ([172.59.129.178]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-1f44c75dd32sm45150395ad.12.2024.05.26.09.11.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 09:11:09 -0700 (PDT) Received: from gnu-cfl-3.. (localhost [IPv6:::1]) by gnu-cfl-3.localdomain (Postfix) with ESMTP id 3E15E740366 for ; Sun, 26 May 2024 09:11:08 -0700 (PDT) From: "H.J. Lu" To: libc-alpha@sourceware.org Subject: [PATCH v3] elf: Handle static PIE loaded at the specific address [BZ #31799] Date: Sun, 26 May 2024 09:11:08 -0700 Message-ID: <20240526161108.2017118-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 For static PIE, the load address is the address of the ELF header. When a static PIE is loaded at the specific address with -Ttext-segment=ORG, its dynamic section entries contain the relocated values for the load address and we must keep the l_addr field as 0 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 | 16 +++++++++ elf/dl-reloc-static-pie.c | 13 ++++--- elf/tst-pie-address-static.c | 19 ++++++++++ elf/tst-pie-address.c | 28 +++++++++++++++ 6 files changed, 171 insertions(+), 4 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..e3b9fb3fe9 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1046,6 +1046,22 @@ tests-pie += \ tst-pie1 \ tst-pie2 \ # tests-pie +ifneq (,$(load-address-ldflag)) +tests += \ + tst-pie-address \ + # tests +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 diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index 10c23d0bf0..ac35c26f53 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -23,7 +23,6 @@ #include #include -#include #include #define RESOLVE_MAP(map, scope, sym, version, flags) map @@ -37,11 +36,17 @@ _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 + l_addr as 0 if static PIE is marked ET_EXEC and loaded at the + specific address since its dynamic section entries contain the + relocated values for the load address. */ + extern const ElfW(Ehdr) __ehdr_start attribute_hidden; + ElfW(Addr) l_addr = (ElfW(Addr)) &__ehdr_start; + if (__ehdr_start.e_type == ET_DYN) + main_map->l_addr = l_addr; /* 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 *) l_addr + 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