From patchwork Thu Oct 31 16:09:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 2004737 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=BQ+N91ym; 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 4XfTVl6wsTz1xwF for ; Fri, 1 Nov 2024 03:10:39 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2AD873857354 for ; Thu, 31 Oct 2024 16:10:38 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 8D81A3857BBF for ; Thu, 31 Oct 2024 16:09:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8D81A3857BBF Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8D81A3857BBF Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390975; cv=none; b=Oju2oKsy1gj0iRclc16/ZDMbRw9kEYB1H9trJS6IKqreDP1ycjPNcnwZzg9dTYgxasOpSfZCFvnWm65VUlH86YM2UKwOZti21V0HFpGqE+zm4Hf24xwVRNa317plI7nm6t3ZWdviMZcSyh4AHTszhJ2+WLUqP7gy7q49dV2izp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390975; c=relaxed/simple; bh=YmjS+DOwXn77zu4eUUv+gVanoUZOpggYbQAA5q9GEJU=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=vEjlzYc0CMEqquN4bHGTvhPSLYoNZcKrJ5+xwDUYarciywmz2+kqBp99IHKiaQVkB9jQPSRyhKQazHqbgPoUIjw4cRSpH6Dm84JlGHuY7DWP6MtL9OdSyALvLQW4G2Pnur8nyyhHvcQwtu7EEPpyh2YIOo6tVoZaPja4QYyS3c0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730390972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=p5F/qdBGa7iwJK4Bzr1RpzsVa4e6QTptdegAGRMrb34=; b=BQ+N91ym9D809xDc52eb3od0itnfNdBoOIsWVlyR96xGPQsj8WlUyCAD5htYEBOZdELoeD Ma+DDSHH/XELpW0RxUSjKZoRba0GWw7jZXxOBHWymDEXqNc0XjTjfsdGD6fVkCHYXr0s1J JZCEokvHljNhY13miqmIuZ6m6sObvMI= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-564-cd-y2nrDPOyrgYMet0fdEw-1; Thu, 31 Oct 2024 12:09:21 -0400 X-MC-Unique: cd-y2nrDPOyrgYMet0fdEw-1 Received: from mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.15]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id EDE901954B1A for ; Thu, 31 Oct 2024 16:09:20 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.225.77]) by mx-prod-int-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 2C1C61956086 for ; Thu, 31 Oct 2024 16:09:19 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 1/4] elf: rtld_multiple_ref is always true In-Reply-To: Message-ID: <761b0a8ec6b348c0c8524990d956639867b22183.1730374087.git.fweimer@redhat.com> References: X-From-Line: 761b0a8ec6b348c0c8524990d956639867b22183 Mon Sep 17 00:00:00 2001 Date: Thu, 31 Oct 2024 17:09:17 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.15 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 a long time, libc.so.6 has dependend on ld.so, which means that there is a reference to ld.so in all processes, and rtld_multiple_ref is always true. In fact, if rtld_multiple_ref were false, some of the ld.so setup code would not run. Reviewed-by: DJ Delorie --- elf/rtld.c | 104 +++++++++++++++++++++++++---------------------------- 1 file changed, 48 insertions(+), 56 deletions(-) diff --git a/elf/rtld.c b/elf/rtld.c index cb6199aa61..dcd0f4cdc6 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1963,43 +1963,37 @@ dl_main (const ElfW(Phdr) *phdr, if (main_map->l_searchlist.r_list[i] == &GL(dl_rtld_map)) break; - bool rtld_multiple_ref = false; - if (__glibc_likely (i < main_map->l_searchlist.r_nlist)) - { - /* Some DT_NEEDED entry referred to the interpreter object itself, so - put it back in the list of visible objects. We insert it into the - chain in symbol search order because gdb uses the chain's order as - its symbol search order. */ - rtld_multiple_ref = true; + /* Insert the link map for the dynamic loader into the chain in + symbol search order because gdb uses the chain's order as its + symbol search order. */ - GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1]; - if (__glibc_likely (state.mode == rtld_mode_normal)) - { - GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist - ? main_map->l_searchlist.r_list[i + 1] - : NULL); + GL(dl_rtld_map).l_prev = main_map->l_searchlist.r_list[i - 1]; + if (__glibc_likely (state.mode == rtld_mode_normal)) + { + GL(dl_rtld_map).l_next = (i + 1 < main_map->l_searchlist.r_nlist + ? main_map->l_searchlist.r_list[i + 1] + : NULL); #ifdef NEED_DL_SYSINFO_DSO - if (GLRO(dl_sysinfo_map) != NULL - && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map) - && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map)) - GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map); + if (GLRO(dl_sysinfo_map) != NULL + && GL(dl_rtld_map).l_prev->l_next == GLRO(dl_sysinfo_map) + && GL(dl_rtld_map).l_next != GLRO(dl_sysinfo_map)) + GL(dl_rtld_map).l_prev = GLRO(dl_sysinfo_map); #endif - } - else - /* In trace mode there might be an invisible object (which we - could not find) after the previous one in the search list. - In this case it doesn't matter much where we put the - interpreter object, so we just initialize the list pointer so - that the assertion below holds. */ - GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next; - - assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next); - GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map); - if (GL(dl_rtld_map).l_next != NULL) - { - assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev); - GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map); - } + } + else + /* In trace mode there might be an invisible object (which we + could not find) after the previous one in the search list. + In this case it doesn't matter much where we put the + interpreter object, so we just initialize the list pointer so + that the assertion below holds. */ + GL(dl_rtld_map).l_next = GL(dl_rtld_map).l_prev->l_next; + + assert (GL(dl_rtld_map).l_prev->l_next == GL(dl_rtld_map).l_next); + GL(dl_rtld_map).l_prev->l_next = &GL(dl_rtld_map); + if (GL(dl_rtld_map).l_next != NULL) + { + assert (GL(dl_rtld_map).l_next->l_prev == GL(dl_rtld_map).l_prev); + GL(dl_rtld_map).l_next->l_prev = &GL(dl_rtld_map); } /* Now let us see whether all libraries are available in the @@ -2327,35 +2321,33 @@ dl_main (const ElfW(Phdr) *phdr, /* Make sure no new search directories have been added. */ assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs)); - if (rtld_multiple_ref) - { - /* There was an explicit ref to the dynamic linker as a shared lib. - Re-relocate ourselves with user-controlled symbol definitions. + /* Re-relocate ourselves with user-controlled symbol definitions. - We must do this after TLS initialization in case after this - re-relocation, we might call a user-supplied function - (e.g. calloc from _dl_relocate_object) that uses TLS data. */ + We must do this after TLS initialization in case after this + re-relocation, we might call a user-supplied function + (e.g. calloc from _dl_relocate_object) that uses TLS data. */ - /* Set up the object lookup structures. */ - _dl_find_object_init (); + /* Set up the object lookup structures. */ + _dl_find_object_init (); - /* The malloc implementation has been relocated, so resolving - its symbols (and potentially calling IFUNC resolvers) is safe - at this point. */ - __rtld_malloc_init_real (main_map); + /* The malloc implementation has been relocated, so resolving + its symbols (and potentially calling IFUNC resolvers) is safe + at this point. */ + __rtld_malloc_init_real (main_map); - /* Likewise for the locking implementation. */ - __rtld_mutex_init (); + /* Likewise for the locking implementation. */ + __rtld_mutex_init (); - RTLD_TIMING_VAR (start); - rtld_timer_start (&start); + { + RTLD_TIMING_VAR (start); + rtld_timer_start (&start); - /* Mark the link map as not yet relocated again. */ - GL(dl_rtld_map).l_relocated = 0; - _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0); + /* Mark the link map as not yet relocated again. */ + GL(dl_rtld_map).l_relocated = 0; + _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0); - rtld_timer_accum (&relocate_time, start); - } + rtld_timer_accum (&relocate_time, start); + } /* Relocation is complete. Perform early libc initialization. This is the initial libc, even if audit modules have been loaded with From patchwork Thu Oct 31 16:09:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 2004736 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=gILUkuqU; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4XfTV92VGhz1xwc for ; Fri, 1 Nov 2024 03:10:09 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8DC5E385773F for ; Thu, 31 Oct 2024 16:10:07 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id E7D7A3857739 for ; Thu, 31 Oct 2024 16:09:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7D7A3857739 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E7D7A3857739 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390977; cv=none; b=qmZLMvwXYrI4JYiK4IccdKRrjoV2phDMwY2ZXApDhLCr+UMOqrLiP58BFl7upRfn2KpSrX/3oQ+mFvjtgQGzTVnHNbbCxSpsYp0+QIeELz4aqCEqZfi1JVX6auOn09W15pqNpo6YD7JoW5MK1qR7o7Q+2Grs+m3FZSbITczD0MU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390977; c=relaxed/simple; bh=2zjQfS5UmUjaUBI4u2cOS4vORjeUm9r2uQrhIJLLbP4=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=iwnjD2yCOVZy0A1xjtBSs7MTiCBaGQ4BBTeNULIXqgMpQx6v4JogKj/iKeUXSadR4eHLqaLHBAiEkBJkTmrvuHnWPI5N4z4eUtAGOXQronoGvtfb97h5vLKdsuE6Am6rHEKgOUrJKx41kfNeeqIkASDOiQ0mORJE/YUCFbv8m5E= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730390972; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=6OeaYN7o2HPpOke7lWIh08sP/KM/0cNpwl3/0uv40hM=; b=gILUkuqUN4spzAjv8sustq9IbSBWmFA2XPgmAwpHng7tJ3daaGSVVaC1RGXVdzQLYupT/I DwdGyq1OS+sRMzn7YyoKg79rByTY4WGw/hUrnDDLNImmnMquDnAjrDFrIOXnQMV/tRRgma Q6V4xpdxSf/uA5FFloMcA2Defexqmgw= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-22-yJp6eJxGOCay5oykwuzJAQ-1; Thu, 31 Oct 2024 12:09:31 -0400 X-MC-Unique: yJp6eJxGOCay5oykwuzJAQ-1 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 79FBE1954B16 for ; Thu, 31 Oct 2024 16:09:30 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.225.77]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 90E11300018D for ; Thu, 31 Oct 2024 16:09:29 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 2/4] elf: Do not define consider_profiling, consider_symbind as macros In-Reply-To: Message-ID: <8a43a5aaaa30585a9dd8f73377ab6fa755ddd1c5.1730374087.git.fweimer@redhat.com> References: X-From-Line: 8a43a5aaaa30585a9dd8f73377ab6fa755ddd1c5 Mon Sep 17 00:00:00 2001 Date: Thu, 31 Oct 2024 17:09:26 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 This avoids surprises when refactoring the code if these identifiers are re-used later in the file. --- elf/dl-reloc.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index 4bf7aec88b..b2c1627ceb 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -220,8 +220,8 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], int lazy = reloc_mode & RTLD_LAZY; int skip_ifunc = reloc_mode & __RTLD_NOIFUNC; -#ifdef SHARED bool consider_symbind = false; +#ifdef SHARED /* If we are auditing, install the same handlers we need for profiling. */ if ((reloc_mode & __RTLD_AUDIT) == 0) { @@ -240,9 +240,7 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], } #elif defined PROF /* Never use dynamic linker profiling for gprof profiling code. */ -# define consider_profiling 0 -#else -# define consider_symbind 0 + consider_profiling = 0; #endif /* If DT_BIND_NOW is set relocate all references in this object. We @@ -300,7 +298,6 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], ELF_DYNAMIC_RELOCATE (l, scope, lazy, consider_profiling, skip_ifunc); -#ifndef PROF if ((consider_profiling || consider_symbind) && l->l_info[DT_PLTRELSZ] != NULL) { @@ -321,7 +318,6 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], _dl_fatal_printf (errstring, RTLD_PROGNAME, l->l_name); } } -#endif } /* Mark the object so we know this work has been done. */ From patchwork Thu Oct 31 16:09:34 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 2004738 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=dwmJfour; 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 4XfTW23c0Mz1xwF for ; Fri, 1 Nov 2024 03:10:54 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B357B3857340 for ; Thu, 31 Oct 2024 16:10:52 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id CDF85385783C for ; Thu, 31 Oct 2024 16:09:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CDF85385783C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org CDF85385783C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390986; cv=none; b=ksTIEUajDmmMeeupH9MJ9ghx+ekBvNrB1Nb1XheDEzWHy81+9zDvdCw6u3gDGdWPap8Z3igAKYWaL4z9DyQMvGvrbCInM7Mu0cV4eoC6en+i5ahwio7E5lx0AngiPNWVMc+mq8wmdXwXoApOUX3ARQaGLA97unslDu5y1Og7vp0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390986; c=relaxed/simple; bh=wOdDurG4DzXfJ7NMFcQlKqBjGZRqn7F6MP+39wWWGGw=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=SC99hJ4aWhd6/MBSeq9K0TUYo6JSYYCw+PnBhhcgliTjBB2pNZvulaZGK11LvprKq3lZTUoquARhhkCsGNq2jy9kIAQjKDbx7riP7qD+uU1IqA2/rpjWGtYQ20NuTbDQFh31lNCHxpA8efWNCAvqXilUTwy2mGBGabd7u8R/+7s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730390980; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YVruZPHL5/wcB2yTOGIShOD5kEBDWJZZxEv4+FpSlm4=; b=dwmJfourriBpzOOTTr7odITvsK3LkXYimjLJKBVBPEt1qJeJSLIYFVtfC9BA8NboGXre0D EuaNlrK+67xGcZ54Qpd2eFL9DSp8ReypEhr4II3HNWG0ZWYWxGqYOX0K7Zkwz8Xq473KoG d/GcNPtEorirH5/AmRHaoKRw9fMQk2M= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-44-E8GlkXrNOmislkYPuKrgww-1; Thu, 31 Oct 2024 12:09:38 -0400 X-MC-Unique: E8GlkXrNOmislkYPuKrgww-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 122C2197701C for ; Thu, 31 Oct 2024 16:09:38 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.225.77]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 32A451956052 for ; Thu, 31 Oct 2024 16:09:36 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 3/4] elf: Introduce _dl_relocate_object_no_relro In-Reply-To: Message-ID: References: X-From-Line: bef6d7625d8f6aa7bad6d34fa816535821ed2865 Mon Sep 17 00:00:00 2001 Date: Thu, 31 Oct 2024 17:09:34 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 And make _dl_protect_relro apply RELRO conditionally. Reviewed-by: DJ Delorie --- elf/dl-reloc.c | 24 ++++++++++++++---------- sysdeps/generic/ldsodefs.h | 7 +++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c index b2c1627ceb..76d14830dd 100644 --- a/elf/dl-reloc.c +++ b/elf/dl-reloc.c @@ -202,12 +202,9 @@ resolve_map (lookup_t l, struct r_scope_elem *scope[], const ElfW(Sym) **ref, #include "dynamic-link.h" void -_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], - int reloc_mode, int consider_profiling) +_dl_relocate_object_no_relro (struct link_map *l, struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) { - if (l->l_relocated) - return; - struct textrels { caddr_t start; @@ -338,17 +335,24 @@ _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], textrels = textrels->next; } - - /* In case we can protect the data now that the relocations are - done, do it. */ - if (l->l_relro_size != 0) - _dl_protect_relro (l); } +void +_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) +{ + if (l->l_relocated) + return; + _dl_relocate_object_no_relro (l, scope, reloc_mode, consider_profiling); + _dl_protect_relro (l); +} void _dl_protect_relro (struct link_map *l) { + if (l->l_relro_size == 0) + return; + ElfW(Addr) start = ALIGN_DOWN((l->l_addr + l->l_relro_addr), GLRO(dl_pagesize)); diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 259ce2e7d6..91447a5e77 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1014,6 +1014,13 @@ extern void _dl_relocate_object (struct link_map *map, int reloc_mode, int consider_profiling) attribute_hidden; +/* Perform relocation, but do not apply RELRO. Does not check + L->relocated. Otherwise the same as _dl_relocate_object. */ +void _dl_relocate_object_no_relro (struct link_map *map, + struct r_scope_elem *scope[], + int reloc_mode, int consider_profiling) + attribute_hidden; + /* Protect PT_GNU_RELRO area. */ extern void _dl_protect_relro (struct link_map *map) attribute_hidden; From patchwork Thu Oct 31 16:09:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 2004739 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=CcOb5azY; 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 4XfTWr4dXKz1xwF for ; Fri, 1 Nov 2024 03:11:36 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D307B3857719 for ; Thu, 31 Oct 2024 16:11:34 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTP id 042763857733 for ; Thu, 31 Oct 2024 16:09:51 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 042763857733 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 042763857733 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390994; cv=none; b=spDUMOodYwQeYYx2seMHfZK6GrSfzVskvLZ2ht9iRuCdX5KeA0gPFqW/D9FvEoiMbkvEqMoliUklS0ky7ITRkWnMXj7EoyOSv6Lp0WsE0x81ipCDTlM/e2F9swccGtclnO2ivuPYnahnHg7L8neJoyeL7A9TtoslE0IcSD9HXr0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1730390994; c=relaxed/simple; bh=FfD8DhGrGN9lqDij4TVaXX1evE2cO6QRTrS9ZtNz3fw=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=ZIu5kfxJEUgVqdnMIS0lGqtiuk2e8PB4V2Xwx3GUrDp631xr8P1pxJ4xVJbtw9h0QHGELVWwiTfkm0+n3V5oa1SRCa8MkHav81RYx25QSzpUmVwhY1tVShyBv170xe0BFfzLO9rdVTLrfHtw+hzcYcyZC942w+AjY5UgZJmAxE0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1730390991; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=hwkX7rzzP3K30nHQt8i8zU6+VRwk1T6wWJpF2fAh3Mo=; b=CcOb5azYA/M/6Ldxl76Jh2mtBaoNK02ioYM0NCkl15/buVS459PH2SlyAMfXBCZE35RhpG G3IqO9Aajdnh5ETagg6tDMgKgcbZV0Pb74KvryL970NMwXK1JPDqhTENOhRTbdbuTIaTDZ cgUp/f4J+XTEYw+tkG7+fpH83BD7FTg= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-388-ts-h6JjBPpO0WN09GaHT9A-1; Thu, 31 Oct 2024 12:09:50 -0400 X-MC-Unique: ts-h6JjBPpO0WN09GaHT9A-1 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 0830D1955F43 for ; Thu, 31 Oct 2024 16:09:49 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.225.77]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 3715B1955F40 for ; Thu, 31 Oct 2024 16:09:47 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 4/4] elf: Switch to main malloc after final ld.so self-relocation In-Reply-To: Message-ID: References: X-From-Line: b60c89eb447e15203946a76dcd48d89d9d645bc4 Mon Sep 17 00:00:00 2001 Date: Thu, 31 Oct 2024 17:09:45 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com 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 Before commit ee1ada1bdb8074de6e1bdc956ab19aef7b6a7872 ("elf: Rework exception handling in the dynamic loader [BZ #25486]"), the previous order called the main calloc to allocate a shadow GOT/PLT array for auditing support. This happened before libc.so.6 ELF constructors were run, so a user malloc could run without libc.so.6 having been initialized fully. One observable effect was that environ was NULL at this point. It does not seem to be possible at present to trigger such an allocation, but it seems more robust to delay switching to main malloc after ld.so self-relocation is complete. The elf/tst-rtld-no-malloc-audit test case fails with a 2.34-era glibc that does not have this fix. --- elf/Makefile | 9 ++++ elf/dl-support.c | 3 +- elf/rtld.c | 25 +++++------ elf/tst-rtld-no-malloc-audit.c | 1 + elf/tst-rtld-no-malloc-preload.c | 1 + elf/tst-rtld-no-malloc.c | 75 ++++++++++++++++++++++++++++++++ 6 files changed, 98 insertions(+), 16 deletions(-) create mode 100644 elf/tst-rtld-no-malloc-audit.c create mode 100644 elf/tst-rtld-no-malloc-preload.c create mode 100644 elf/tst-rtld-no-malloc.c diff --git a/elf/Makefile b/elf/Makefile index fda796f6d5..3a1cb72955 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -453,6 +453,9 @@ tests += \ tst-recursive-tls \ tst-relsort1 \ tst-ro-dynamic \ + tst-rtld-no-malloc \ + tst-rtld-no-malloc-audit \ + tst-rtld-no-malloc-preload \ tst-rtld-run-static \ tst-single_threaded \ tst-single_threaded-pthread \ @@ -3160,3 +3163,9 @@ tst-dlopen-tlsreinit4-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so tst-dlopen-auditdup-ENV = LD_AUDIT=$(objpfx)tst-dlopen-auditdup-auditmod.so $(objpfx)tst-dlopen-auditdup.out: \ $(objpfx)tst-dlopen-auditdupmod.so $(objpfx)tst-dlopen-auditdup-auditmod.so + +# Reuse an audit module which provides ample debug logging. +tst-rtld-no-malloc-audit-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so + +# Any shared object should do. +tst-rtld-no-malloc-preload-ENV = LD_PRELOAD=$(objpfx)tst-auditmod1.so diff --git a/elf/dl-support.c b/elf/dl-support.c index 451932dd03..ee590edf93 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -338,8 +338,7 @@ _dl_non_dynamic_init (void) call_function_static_weak (_dl_find_object_init); /* Setup relro on the binary itself. */ - if (_dl_main_map.l_relro_size != 0) - _dl_protect_relro (&_dl_main_map); + _dl_protect_relro (&_dl_main_map); } #ifdef DL_SYSINFO_IMPLEMENTATION diff --git a/elf/rtld.c b/elf/rtld.c index dcd0f4cdc6..b8cc3f605f 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -2321,30 +2321,27 @@ dl_main (const ElfW(Phdr) *phdr, /* Make sure no new search directories have been added. */ assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs)); - /* Re-relocate ourselves with user-controlled symbol definitions. - - We must do this after TLS initialization in case after this - re-relocation, we might call a user-supplied function - (e.g. calloc from _dl_relocate_object) that uses TLS data. */ - /* Set up the object lookup structures. */ _dl_find_object_init (); - /* The malloc implementation has been relocated, so resolving - its symbols (and potentially calling IFUNC resolvers) is safe - at this point. */ - __rtld_malloc_init_real (main_map); - /* Likewise for the locking implementation. */ __rtld_mutex_init (); + /* Re-relocate ourselves with user-controlled symbol definitions. */ + { RTLD_TIMING_VAR (start); rtld_timer_start (&start); - /* Mark the link map as not yet relocated again. */ - GL(dl_rtld_map).l_relocated = 0; - _dl_relocate_object (&GL(dl_rtld_map), main_map->l_scope, 0, 0); + _dl_relocate_object_no_relro (&GL(dl_rtld_map), main_map->l_scope, 0, 0); + + /* The malloc implementation has been relocated, so resolving + its symbols (and potentially calling IFUNC resolvers) is safe + at this point. */ + __rtld_malloc_init_real (main_map); + + if (GL(dl_rtld_map).l_relro_size != 0) + _dl_protect_relro (&GL(dl_rtld_map)); rtld_timer_accum (&relocate_time, start); } diff --git a/elf/tst-rtld-no-malloc-audit.c b/elf/tst-rtld-no-malloc-audit.c new file mode 100644 index 0000000000..a028377ad1 --- /dev/null +++ b/elf/tst-rtld-no-malloc-audit.c @@ -0,0 +1 @@ +#include "tst-rtld-no-malloc.c" diff --git a/elf/tst-rtld-no-malloc-preload.c b/elf/tst-rtld-no-malloc-preload.c new file mode 100644 index 0000000000..a028377ad1 --- /dev/null +++ b/elf/tst-rtld-no-malloc-preload.c @@ -0,0 +1 @@ +#include "tst-rtld-no-malloc.c" diff --git a/elf/tst-rtld-no-malloc.c b/elf/tst-rtld-no-malloc.c new file mode 100644 index 0000000000..a78f1ba727 --- /dev/null +++ b/elf/tst-rtld-no-malloc.c @@ -0,0 +1,75 @@ +/* Test that program loading does not call malloc. + 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 +#include + +static void +print (const char *s) +{ + const char *end = s + strlen (s); + while (s < end) + { + ssize_t ret = write (STDOUT_FILENO, s, end - s); + if (ret <= 0) + _exit (2); + s += ret; + } +} + +static void __attribute__ ((noreturn)) +unexpected_call (const char *function) +{ + print ("error: unexpected call to "); + print (function); + print ("\n"); + _exit (1); +} + +/* These are the malloc functions implement in elf/dl-minimal.c. */ + +void +free (void *ignored) +{ + unexpected_call ("free"); +} + +void * +calloc (size_t ignored1, size_t ignored2) +{ + unexpected_call ("calloc"); +} + +void * +malloc (size_t ignored) +{ + unexpected_call ("malloc"); +} + +void * +realloc (void *ignored1, size_t ignored2) +{ + unexpected_call ("realloc"); +} + +int +main (void) +{ + /* Do not use the test wrapper, to avoid spurious malloc calls from it. */ +}