From patchwork Fri Oct 25 22:18:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "H.J. Lu" X-Patchwork-Id: 99641 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 E0C2C3858CDB for ; Fri, 25 Oct 2024 22:19:29 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-yw1-x112b.google.com (mail-yw1-x112b.google.com [IPv6:2607:f8b0:4864:20::112b]) by sourceware.org (Postfix) with ESMTPS id E6CFA3858D21 for ; Fri, 25 Oct 2024 22:18:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6CFA3858D21 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 E6CFA3858D21 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::112b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729894741; cv=none; b=Xrx9/g+9fIG/2CEfgP3d/55Etv7opoqi5SaevPZP5ZPGyOtt5LzfzNadnV31xs9h5Kroy1ckebIm211RqqdzumoGFUoUawJWk6t2uxlsKzXsxQtCOEvUQhJKdcFfgDMaH0VPbtymCTsLzgNv9SuEvW3Lt3AkuGai3mq94Pr062M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729894741; c=relaxed/simple; bh=FtJ5FTcQ3aXhh000oa9YTUpdiqpCqcFQaPjjW52Wdeg=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=tiHEW/kAPjh0fS7wIGQZ4nBsDnIVvUDFRnjB8gp19FC91/Pb7QYzYnkEg6NEapd0cXWzuNW6bYff+AychkH9spmdr0nUFyyA3kTFWK90etXXrKBEsdKjnzJ2dtsIgV88jlBvc1HHRJlqk5mrCfCdplQy1esu1w1fw9gJ6EQw4HE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-yw1-x112b.google.com with SMTP id 00721157ae682-6e3cdbc25a0so30295717b3.2 for ; Fri, 25 Oct 2024 15:18:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729894737; x=1730499537; darn=sourceware.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bAagfIU28PhzW/q25Tcu1tGjX7eUC5JJSlgIaB5ct00=; b=d5UKhEEjC9qqPdzoleOD0iMe7LwtboqQmIXnfYETWtJ9o4a7H6KTWJkg1jLTV6PsUj kEZrpyILW3pPeM7KUZ4TW8eMUiV2C8Zt6eUv3hCBMUIUwueuE5uVlmKVJqwoC/sovF1S zux516Mfujsa6/5iPwTi7wAiKqX2oj5SSyl6Y60UZ5ev9MJWLoQ04zs9Kmbv5Cy8HXF+ u6oxMGz/aoU3juRgnzeL2+ZheIkBtXSD3q2WlW9gTEOr2eiKW03aYnDVqc1pbzAWvqlh zLoG+5deeKEwGDY9L5h6NFFYWIwIG/tvc+Gri0CZbj18noCk6ohgrnbB4bqkKi97KPBW kd9w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729894737; x=1730499537; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bAagfIU28PhzW/q25Tcu1tGjX7eUC5JJSlgIaB5ct00=; b=l4wORPt4IosghmWggkt+2idahncwWY9LMORRzd5A1/Jws8OJ5e444k3IkBK7NNDskk rfJGi3db5YAlx7emIH1S9Vlm6tjnZJLYP+xCD/e3rXmfuKURZMW72k05+DFR4vPl7DpY +U4YJPfRWtGFQD7uD3V+OcfNQpq9URGd1QaxyyLuaUv1rgvGkyi38OS/8OOl5Rmf9Diq /veEjQvWqJhFyNR7zvUvDWoZUdBZQ7mzjhVjFPCBgs1js1A7nWZ4bKljat+PVdM7tBge +Tcrdt/fEg1UE0WyYPdM4tRI3f95aHMqcsHl0QhGCrCMHZ1mK1J7T4BIRx1pQ+/3YZoK 7wXg== X-Gm-Message-State: AOJu0YwySad2sgfD6w14U4ceoAoa7yIbiAZL7K1UofgRqdABvZXMgI16 b+pEVGsQRYtayHwaFtjrjr5GdNc0H6aJZ7Xk1vKnsEe8O6k619WPHR8vG7QfQJtCVxkY+WoVyAr B5etl5KVMcidvaRw1tX6zXW+oThYx9fK5II37q6oG X-Google-Smtp-Source: AGHT+IEQsRlipw9TQkkrk5zAthW/nqOy5KUyCPWOPi8skg+mgkeSD2H4VaiBC2PmTONMV1XtMh30lKTu5rfUWd1XTGc= X-Received: by 2002:a05:690c:f10:b0:6e5:e6e8:d6b7 with SMTP id 00721157ae682-6e9d88b8a27mr12974407b3.1.1729894737143; Fri, 25 Oct 2024 15:18:57 -0700 (PDT) MIME-Version: 1.0 References: <20240527143803.3550703-1-hjl.tools@gmail.com> <20240527143803.3550703-2-hjl.tools@gmail.com> In-Reply-To: From: "H.J. Lu" Date: Sat, 26 Oct 2024 06:18:19 +0800 Message-ID: Subject: [PATCH v6 1/1] elf: Handle static PIE loaded at the non-zero address [BZ #31799] To: Noah Goldstein Cc: GNU C Library , "Carlos O'Donell" , Florian Weimer X-Spam-Status: No, score=-3017.6 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 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~patchwork=sourceware.org@sourceware.org On Wed, Oct 23, 2024 at 10:30 PM Noah Goldstein wrote: ... > >>>> >> +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]) > >>>> >> + > > IIUC, all of the new conf code is for setting up tests. > Can you make that more clear in either comment or var names? Done. > >>>> >> 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..6286f19f1f 100644 > >>>> >> --- a/elf/dl-reloc-static-pie.c > >>>> >> +++ b/elf/dl-reloc-static-pie.c > >>>> >> @@ -37,21 +37,32 @@ _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 (); > >>>> >> - > >>>> >> - /* Read our own dynamic section and fill in the info array. */ > >>>> >> - main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ()); > >>>> >> - > >>>> >> + ElfW(Addr) file_p_vaddr = 0; > >>>> >> const ElfW(Phdr) *ph, *phdr = GL(dl_phdr); > >>>> >> size_t phnum = GL(dl_phnum); > >>>> >> for (ph = phdr; ph < &phdr[phnum]; ++ph) > >>>> >> - if (ph->p_type == PT_DYNAMIC) > >>>> >> + switch (ph->p_type) > >>>> >> { > >>>> >> + case PT_LOAD: > >>>> >> + /* Get p_vaddr of the PT_LOAD segement which covers the file > >>>> >> + start. */ > >>>> >> + if (ph->p_offset == 0) > >>>> >> + file_p_vaddr = ph->p_vaddr; > >>>> >> + break; > >>>> >> + case PT_DYNAMIC: > >>>> >> main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0; > >>>> >> break; > >>>> >> + default: > >>>> >> + break; > >>>> >> } > >>>> >> > >>>> >> + /* Figure out the run-time load address of static PIE. */ > >>>> >> + ElfW(Addr) l_addr = elf_machine_load_address (); > >>>> >> + main_map->l_addr = l_addr - file_p_vaddr; > > Can you explain this? I though vaddr field was the true virtual-address > but you seem to be treating it as an offset here. The l_addr field contains the difference between the address in the ELF file and the address in memory. file_p_vaddr is used as the address in static PIE. I updated comments in the v6 patch, Here is the v6 patch. OK to install? Thanks. From c309d6edd62b64d5d732ef9b662e6074a79c2eff Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Sun, 26 May 2024 04:55:32 -0700 Subject: [PATCH v6] elf: Handle static PIE loaded at the non-zero address [BZ #31799] When a static PIE is loaded at the non-zero address, its PT_DYNAMIC segment entries contain the relocated values for the load address in static PIE. Since static PIE usually doesn't have PT_PHDR segment, use p_vaddr of the PT_LOAD segment which covers the file start as the load address in static PIE and adjust the entries of PT_DYNAMIC segment with the load address in static PIE by properly setting the l_addr field for static PIE. This fixes BZ #31799. Signed-off-by: H.J. Lu --- configure | 72 ++++++++++++++++++++++++++++++++++++ configure.ac | 34 +++++++++++++++++ elf/Makefile | 20 ++++++++++ elf/dl-reloc-static-pie.c | 30 +++++++++++---- elf/tst-pie-address-static.c | 19 ++++++++++ elf/tst-pie-address.c | 28 ++++++++++++++ 6 files changed, 196 insertions(+), 7 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 9c0c0dce03..a84bb53fc8 100755 --- a/configure +++ b/configure @@ -8107,6 +8107,78 @@ printf "%s\n" "$libc_cv_cc_pie_default" >&6; } config_vars="$config_vars cc-pie-default = $libc_cv_cc_pie_default" +# Get Position Dependent Executable (PDE) load address to be used to +# load static Position Independent Executable (PIE) at a known working +# non-zero 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 case e in #( + e) 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 \ + | $AWK '/LOAD/ { print $3; exit 0; }') +else + as_fn_error $? "${CC-cc} can not create PDE" "$LINENO" 5 +fi +rm -f conftest* ;; +esac +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" + +# Get the linker command-line option to load executable at a non-zero +# 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_ldflag=-Wl,-Ttext-segment +else + libc_cv_load_address_ldflag= +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_ldflag" + { 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 d5a00461ff..8dc7e80ca3 100644 --- a/configure.ac +++ b/configure.ac @@ -1763,6 +1763,40 @@ fi rm -f conftest.*]) LIBC_CONFIG_VAR([cc-pie-default], [$libc_cv_cc_pie_default]) +# Get Position Dependent Executable (PDE) load address to be used to +# load static Position Independent Executable (PIE) at a known working +# non-zero 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 \ + | $AWK '/LOAD/ { 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]) + +# Get the linker command-line option to load executable at a non-zero +# address. +LIBC_LINKER_FEATURE([-Ttext-segment=$libc_cv_pde_load_address], + [-Wl,-Ttext-segment=$libc_cv_pde_load_address], + [libc_cv_load_address_ldflag=-Wl,-Ttext-segment], + [libc_cv_load_address_ldflag=]) +LIBC_CONFIG_VAR([load-address-ldflag], [$libc_cv_load_address_ldflag]) + 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 9cfe738919..4eaca7af67 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1091,6 +1091,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 @@ -1937,6 +1956,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..66fabf5543 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -37,21 +37,37 @@ _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 (); - - /* Read our own dynamic section and fill in the info array. */ - main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ()); - + /* NB: elf_machine_load_address () returns the run-time load address + of static PIE. The l_addr field contains the difference between the + address in the ELF file and the address in memory. If a static PIE + has a non-zero load address, we need to subtract it when computing + the l_addr field. Since static PIE usually doesn't have PT_PHDR + segment, use p_vaddr of the PT_LOAD segment which covers the file + start as the load address in static PIE. */ + ElfW(Addr) file_p_vaddr = 0; const ElfW(Phdr) *ph, *phdr = GL(dl_phdr); size_t phnum = GL(dl_phnum); for (ph = phdr; ph < &phdr[phnum]; ++ph) - if (ph->p_type == PT_DYNAMIC) + switch (ph->p_type) { + case PT_LOAD: + if (ph->p_offset == 0) + file_p_vaddr = ph->p_vaddr; + break; + case PT_DYNAMIC: main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0; break; + default: + break; } + /* Figure out the run-time load address of static PIE. */ + ElfW(Addr) l_addr = elf_machine_load_address (); + main_map->l_addr = l_addr - file_p_vaddr; + + /* Read our own dynamic section and fill in the info array. */ + main_map->l_ld = ((void *) l_addr + elf_machine_dynamic ()); + elf_get_dynamic_info (main_map, false, true); # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC 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 -- 2.47.0