From patchwork Thu Jan 12 14:44:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 63104 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 A04FC3854394 for ; Thu, 12 Jan 2023 14:44:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A04FC3854394 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1673534681; bh=zcHhS/RPmCh2UY10niE+WW3kDAgOq/0cZkNDjAAXdz8=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=E2vBZhzjCfy4/203LHc5+9ozkbCeDLVn4xbBltp0QOmw0f5dGiA0cDNGKZ7+bXvpO AuMHn/rqUhKOQUDOFgEpNAccs6Wg5RejS3+3DdLdAwU5B/Ax5Tl1C7yp939KaobSUy r+XVxce99Mn81I2gHILmqepicXcZAKSTdFmjznII= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-oo1-xc34.google.com (mail-oo1-xc34.google.com [IPv6:2607:f8b0:4864:20::c34]) by sourceware.org (Postfix) with ESMTPS id 92C633854395 for ; Thu, 12 Jan 2023 14:44:18 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 92C633854395 Received: by mail-oo1-xc34.google.com with SMTP id 187-20020a4a09c4000000b004d8f3cb09f5so4861496ooa.6 for ; Thu, 12 Jan 2023 06:44:18 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=zcHhS/RPmCh2UY10niE+WW3kDAgOq/0cZkNDjAAXdz8=; b=EUJl03WP999arFDb8Z5D1mwTj4YWNNeH5hU0xWcDDBCJR5WebA+Elus1xSZEcq/pH1 xDSqaGX9VQG1Q6XrQuoX4P9ngO8SK9lf8dVb5cKeYUm3zm9oiUFKtjTMA73DSyRzGQos VxbuQlqzWKqFytwV4id0trHZcEtgmbXphhChZ+yb7Thb+WJfd7+bZAznhPvU8p/bMUct KBMqe4WUhfQBbvgFMrEWPF+VuHa6G/kqutlqwaRhx2xeuWRw4Q5RbajYWbLDEej5iljT NG26AKg+RQw9ERiBq0zzcadnLjOsJbTRlRJ0hJkn8/AHXD0NFeeHwbETW+Zv6iKs1mjy JgBw== X-Gm-Message-State: AFqh2kpsB0TCHleD779R+RpAsHIzDRPhOL/BgUWhVfvH23tCevnC0zfW AxS6FHmess7NW7pGDtqczky9RPlpqAnJXpQHQDE= X-Google-Smtp-Source: AMrXdXs1k134p833LDLYVDCazlhbWi9Jle0UCp/N/dR29gN7Wkw8VdBDEmmMaETFI7osVTkrpKvkBQ== X-Received: by 2002:a4a:8c49:0:b0:4f2:73c:a30f with SMTP id v9-20020a4a8c49000000b004f2073ca30fmr5187955ooj.3.1673534657410; Thu, 12 Jan 2023 06:44:17 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c0:a93a:bf1c:dea8:4fd2:4310]) by smtp.gmail.com with ESMTPSA id r75-20020a4a374e000000b004f241603c49sm1843586oor.20.2023.01.12.06.44.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Jan 2023 06:44:16 -0800 (PST) To: libc-alpha@sourceware.org, Florian Weimer Cc: John Paul Adrian Glaubitz Subject: [PATCH v2] elf: Fix GL(dl_phdr) and GL(dl_phnum) for static builds [BZ #29864] Date: Thu, 12 Jan 2023 11:44:12 -0300 Message-Id: <20230112144412.3102134-1-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, URIBL_BLACK 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: , X-Patchwork-Original-From: Adhemerval Zanella via Libc-alpha From: Adhemerval Zanella Netto Reply-To: Adhemerval Zanella Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" The 73fc4e28b9464f0e refactor did not add the GL(dl_phdr) and GL(dl_phnum) for static build, relying on the __ehdr_start symbol, which is always added by the static linker, to get the correct values. This is problematic in some ways: - The segment may see its in-memory size differ from its in-file size (or the binary may have holes). The Linux has fixed is to provide concise values for both AT_PHDR and AT_PHNUM (commit 0da1d5002745c - "fs/binfmt_elf: Fix AT_PHDR for unusual ELF files") - Some archs (alpha for instance) the hidden weak reference is not correctly pulled by the static linker and __ehdr_start address end up being 0, which makes GL(dl_phdr) and GL(dl_phnum) have both invalid values (and triggering a segfault later on libc.so while accessing TLS variables). The safer fix is to just restore the previous behavior to setup GL(dl_phdr) and GL(dl_phnum) for static based on kernel auxv. The __ehdr_start fallback can also be simplified by not assuming weak linkage (as for PIE). The libc-static.c auxv init logic is moved to dl-support.c, since the later is build without SHARED and then GLRO macro is defined to access the variables directly. The _dl_phdr is also assumed to be always non NULL, since an invalid NULL values does not trigger TLS initialization (which is used in various libc systems). Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. * Changes from v1: - Removed auxv_values implicit initialization. - Removed unused header inclusion. Reviewed-by: Florian Weimer --- csu/libc-start.c | 21 --------------------- csu/libc-tls.c | 25 ++++++++++++------------- elf/dl-support.c | 46 ++++++++++++++++++++++++++++++++-------------- 3 files changed, 44 insertions(+), 48 deletions(-) diff --git a/csu/libc-start.c b/csu/libc-start.c index ef26a89bc4..c3bb6d09bc 100644 --- a/csu/libc-start.c +++ b/csu/libc-start.c @@ -262,28 +262,7 @@ LIBC_START_MAIN (int (*main) (int, char **, char ** MAIN_AUXVEC_DECL), } # endif _dl_aux_init (auxvec); - if (GL(dl_phdr) == NULL) # endif - { - /* Starting from binutils-2.23, the linker will define the - magic symbol __ehdr_start to point to our own ELF header - if it is visible in a segment that also includes the phdrs. - So we can set up _dl_phdr and _dl_phnum even without any - information from auxv. */ - - extern const ElfW(Ehdr) __ehdr_start -# if BUILD_PIE_DEFAULT - __attribute__ ((visibility ("hidden"))); -# else - __attribute__ ((weak, visibility ("hidden"))); - if (&__ehdr_start != NULL) -# endif - { - assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr)); - GL(dl_phdr) = (const void *) &__ehdr_start + __ehdr_start.e_phoff; - GL(dl_phnum) = __ehdr_start.e_phnum; - } - } __tunables_init (__environ); diff --git a/csu/libc-tls.c b/csu/libc-tls.c index 8dabf95bcc..cdf6442c02 100644 --- a/csu/libc-tls.c +++ b/csu/libc-tls.c @@ -119,19 +119,18 @@ __libc_setup_tls (void) __tls_pre_init_tp (); /* Look through the TLS segment if there is any. */ - if (_dl_phdr != NULL) - for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) - if (phdr->p_type == PT_TLS) - { - /* Remember the values we need. */ - memsz = phdr->p_memsz; - filesz = phdr->p_filesz; - initimage = (void *) phdr->p_vaddr + main_map->l_addr; - align = phdr->p_align; - if (phdr->p_align > max_align) - max_align = phdr->p_align; - break; - } + for (phdr = _dl_phdr; phdr < &_dl_phdr[_dl_phnum]; ++phdr) + if (phdr->p_type == PT_TLS) + { + /* Remember the values we need. */ + memsz = phdr->p_memsz; + filesz = phdr->p_filesz; + initimage = (void *) phdr->p_vaddr + main_map->l_addr; + align = phdr->p_align; + if (phdr->p_align > max_align) + max_align = phdr->p_align; + break; + } /* Calculate the size of the static TLS surplus, with 0 auditors. */ _dl_tls_static_surplus_init (0); diff --git a/elf/dl-support.c b/elf/dl-support.c index ef0e5591e3..9714f75db0 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -256,6 +256,25 @@ _dl_aux_init (ElfW(auxv_t) *av) for (int i = 0; i < array_length (auxv_values); ++i) auxv_values[i] = 0; _dl_parse_auxv (av, auxv_values); + + _dl_phdr = (void*) auxv_values[AT_PHDR]; + _dl_phnum = auxv_values[AT_PHNUM]; + + if (_dl_phdr == NULL) + { + /* Starting from binutils-2.23, the linker will define the + magic symbol __ehdr_start to point to our own ELF header + if it is visible in a segment that also includes the phdrs. + So we can set up _dl_phdr and _dl_phnum even without any + information from auxv. */ + + extern const ElfW(Ehdr) __ehdr_start attribute_hidden; + assert (__ehdr_start.e_phentsize == sizeof *GL(dl_phdr)); + _dl_phdr = (const void *) &__ehdr_start + __ehdr_start.e_phoff; + _dl_phnum = __ehdr_start.e_phnum; + } + + assert (_dl_phdr != NULL); } #endif @@ -324,20 +343,19 @@ _dl_non_dynamic_init (void) if (_dl_platform != NULL) _dl_platformlen = strlen (_dl_platform); - if (_dl_phdr != NULL) - for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph) - switch (ph->p_type) - { - /* Check if the stack is nonexecutable. */ - case PT_GNU_STACK: - _dl_stack_flags = ph->p_flags; - break; - - case PT_GNU_RELRO: - _dl_main_map.l_relro_addr = ph->p_vaddr; - _dl_main_map.l_relro_size = ph->p_memsz; - break; - } + for (const ElfW(Phdr) *ph = _dl_phdr; ph < &_dl_phdr[_dl_phnum]; ++ph) + switch (ph->p_type) + { + /* Check if the stack is nonexecutable. */ + case PT_GNU_STACK: + _dl_stack_flags = ph->p_flags; + break; + + case PT_GNU_RELRO: + _dl_main_map.l_relro_addr = ph->p_vaddr; + _dl_main_map.l_relro_size = ph->p_memsz; + break; + } call_function_static_weak (_dl_find_object_init);