From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 874CD385801B for ; Tue, 12 Apr 2022 18:42:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 874CD385801B Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-483-rpng3TcBP7C5WfyrKhe7hg-1; Tue, 12 Apr 2022 14:42:18 -0400 X-MC-Unique: rpng3TcBP7C5WfyrKhe7hg-1 Received: by mail-qk1-f198.google.com with SMTP id bs8-20020a05620a470800b0067e383077adso10211820qkb.10 for ; Tue, 12 Apr 2022 11:42:17 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Jgieil/KLgScaYHf5yAbB9GYilBNgi4tZULSmvNmeQk=; b=rPUnVexva0fCWmposasYG1E0E85v2zYZj58DCbA/l581Q4SqnCZpMFyGYUBakPyyFX uj8txqJ/xroHPCdIm6p9xBfwqAI9YRFchctXwPydiJUO7B+YtYqy2PO4/vvXrEHDTGRL JLPrfZw3em0yhrKKnvf+XNjZ1ZLOO9Iz5F/wMlbwgrkiZ1cVZK7glqOjvl9h1c5Ule/I dtXwOuEnklzaRnETc6uNF0KT/uyvfUiuZ4CrjyUwMgh3+V04DgWdOdHMbaDjXVjyMOJP KEkg1zJkqE1ETtvbY+1ixX8OSjaIEr12AYhFcxzE78Tb6pKvRtvZ7finwyQwVIYSZcrR tWbA== X-Gm-Message-State: AOAM5310yt8NxQeTRcbO5ldDuhma29jdjQrMRafa5yDe7PyJCfNk107f qul3cFbQ3cppam//HHedwJLCiJKQ5VehxtHIG51o/+ODF1j7veRF+2RtxYPll8i0aB4qdQhVLLy joBRRj4FLbzD6wT6xnepWkXGuL0/NYs7tvcVaOJVmLnYPEWYlRYZn1B9Bjv/VI3KH1C8CVOM= X-Received: by 2002:a37:897:0:b0:69c:6a8:cd4e with SMTP id 145-20020a370897000000b0069c06a8cd4emr4144352qki.456.1649788937026; Tue, 12 Apr 2022 11:42:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmR0civ+Ytc4tsFcPBsg/2vqhSeoT7/Ad8HjxEdgLE4Men42mtf3N1WsAYfV0GCiBx1TYxVw== X-Received: by 2002:a37:897:0:b0:69c:6a8:cd4e with SMTP id 145-20020a370897000000b0069c06a8cd4emr4144330qki.456.1649788936621; Tue, 12 Apr 2022 11:42:16 -0700 (PDT) Received: from athas.redhat.com (135-23-175-80.cpe.pppoe.ca. [135.23.175.80]) by smtp.gmail.com with ESMTPSA id j19-20020a05622a039300b002ecc2ebfd87sm10953840qtx.32.2022.04.12.11.42.15 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Apr 2022 11:42:15 -0700 (PDT) From: Carlos O'Donell To: libc-stable@sourceware.org Cc: Adhemerval Zanella , Fangrui Song Subject: [PATCH 03/27] elf: Fix elf_get_dynamic_info definition Date: Tue, 12 Apr 2022 14:41:41 -0400 Message-Id: <20220412184205.3343677-4-carlos@redhat.com> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220412184205.3343677-1-carlos@redhat.com> References: <20220412184205.3343677-1-carlos@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: application/octet-stream; x-default=true X-Spam-Status: No, score=-5.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-stable@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-stable mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 12 Apr 2022 18:42:20 -0000 From: Adhemerval Zanella Before to 490e6c62aa31a8a ('elf: Avoid nested functions in the loader [BZ #27220]'), elf_get_dynamic_info() was defined twice on rtld.c: on the first dynamic-link.h include and later within _dl_start(). The former definition did not define DONT_USE_BOOTSTRAP_MAP and it is used on setup_vdso() (since it is a global definition), while the former does define DONT_USE_BOOTSTRAP_MAP and it is used on loader self-relocation. With the commit change, the function is now included and defined once instead of defined as a nested function. So rtld.c defines without defining RTLD_BOOTSTRAP and it brokes at least powerpc32. This patch fixes by moving the get-dynamic-info.h include out of dynamic-link.h, which then the caller can corirectly set the expected semantic by defining STATIC_PIE_BOOTSTRAP, RTLD_BOOTSTRAP, and/or RESOLVE_MAP. It also required to enable some asserts only for the loader bootstrap to avoid issues when called from setup_vdso(). As a side note, this is another issues with nested functions: it is not clear from pre-processed output (-E -dD) how the function will be build and its semantic (since nested function will be local and extra C defines may change it). I checked on x86_64-linux-gnu (w/o --enable-static-pie), i686-linux-gnu, powerpc64-linux-gnu, powerpc-linux-gnu-power4, aarch64-linux-gnu, arm-linux-gnu, sparc64-linux-gnu, and s390x-linux-gnu. Reviewed-by: Fangrui Song (cherry picked from commit 4af6982e4c9fc465ffb7a54b794aaaa134241f05) Resolved conflicts: elf/rtld.c --- elf/dl-conflict.c | 1 + elf/dl-load.c | 3 ++- elf/dl-reloc-static-pie.c | 3 ++- elf/dl-runtime.c | 1 + elf/dynamic-link.h | 1 - elf/get-dynamic-info.h | 25 ++++++++++++++----------- elf/rtld.c | 5 +++-- elf/setup-vdso.h | 2 +- sysdeps/arm/dl-machine.h | 1 + sysdeps/i386/dl-machine.h | 1 + sysdeps/x86_64/dl-machine.h | 1 + 11 files changed, 27 insertions(+), 17 deletions(-) diff --git a/elf/dl-conflict.c b/elf/dl-conflict.c index 5c8e51d19a..d54356dee3 100644 --- a/elf/dl-conflict.c +++ b/elf/dl-conflict.c @@ -17,6 +17,7 @@ License along with the GNU C Library; see the file COPYING.LIB. If not, see . */ +#include #include #include #include diff --git a/elf/dl-load.c b/elf/dl-load.c index 0976977fbd..eea06629a9 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -58,6 +58,7 @@ struct filebuf }; #include "dynamic-link.h" +#include "get-dynamic-info.h" #include #include #include @@ -1295,7 +1296,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (l->l_ld != 0) l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l); + elf_get_dynamic_info (l, false); /* Make sure we are not dlopen'ing an object that has the DF_1_NOOPEN flag set, or a PIE object. */ diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index a52ba8aeb8..f323b4dd0d 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -28,6 +28,7 @@ #define STATIC_PIE_BOOTSTRAP #define RESOLVE_MAP(map, scope, sym, version, flags) map #include "dynamic-link.h" +#include "get-dynamic-info.h" /* Relocate static executable with PIE. */ @@ -51,7 +52,7 @@ _dl_relocate_static_pie (void) break; } - elf_get_dynamic_info (main_map); + elf_get_dynamic_info (main_map, false); # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info); diff --git a/elf/dl-runtime.c b/elf/dl-runtime.c index 9d0d941000..61c260ddb8 100644 --- a/elf/dl-runtime.c +++ b/elf/dl-runtime.c @@ -19,6 +19,7 @@ #define IN_DL_RUNTIME 1 /* This can be tested in dl-machine.h. */ #include +#include #include #include #include diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h index 7cc3021164..21cdfc88bb 100644 --- a/elf/dynamic-link.h +++ b/elf/dynamic-link.h @@ -93,7 +93,6 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], #include -#include "get-dynamic-info.h" #ifdef RESOLVE_MAP diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h index 15c316b38c..d169099fbc 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -25,7 +25,7 @@ #include static inline void __attribute__ ((unused, always_inline)) -elf_get_dynamic_info (struct link_map *l) +elf_get_dynamic_info (struct link_map *l, bool check) { #if __ELF_NATIVE_CLASS == 32 typedef Elf32_Word d_tag_utype; @@ -112,16 +112,19 @@ elf_get_dynamic_info (struct link_map *l) assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel))); #endif #ifdef RTLD_BOOTSTRAP - /* Only the bind now flags are allowed. */ - assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL - || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0); - /* Flags must not be set for ld.so. */ - assert (info[DT_FLAGS] == NULL - || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0); -#endif -#if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP - assert (info[DT_RUNPATH] == NULL); - assert (info[DT_RPATH] == NULL); + if (check) + { + /* Only the bind now flags are allowed. */ + assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL + || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0); + /* Flags must not be set for ld.so. */ + assert (info[DT_FLAGS] == NULL + || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0); +# ifdef STATIC_PIE_BOOTSTRAP + assert (info[DT_RUNPATH] == NULL); + assert (info[DT_RPATH] == NULL); +# endif + } #else if (info[DT_FLAGS] != NULL) { diff --git a/elf/rtld.c b/elf/rtld.c index e5653d8e3c..23207112a1 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -513,6 +513,7 @@ _dl_start_final (void *arg, struct dl_start_final_info *info) is trivial: always the map of ld.so itself. */ #define RTLD_BOOTSTRAP #define RESOLVE_MAP(map, scope, sym, version, flags) map +#include "get-dynamic-info.h" #include "dynamic-link.h" static ElfW(Addr) __attribute_used__ @@ -548,7 +549,7 @@ _dl_start (void *arg) /* Read our own dynamic section and fill in the info array. */ bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; - elf_get_dynamic_info (&bootstrap_map); + elf_get_dynamic_info (&bootstrap_map, true); #if NO_TLS_OFFSET != 0 bootstrap_map.l_tls_offset = NO_TLS_OFFSET; @@ -1616,7 +1617,7 @@ dl_main (const ElfW(Phdr) *phdr, if (! rtld_is_main) { /* Extract the contents of the dynamic section for easy access. */ - elf_get_dynamic_info (main_map); + elf_get_dynamic_info (main_map, false); /* If the main map is libc.so, update the base namespace to refer to this map. If libc.so is loaded later, this happens diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h index f44748bc98..6fdffafcca 100644 --- a/elf/setup-vdso.h +++ b/elf/setup-vdso.h @@ -64,7 +64,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)), l->l_map_end += l->l_addr; l->l_text_end += l->l_addr; l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l); + elf_get_dynamic_info (l, false); _dl_setup_hash (l); l->l_relocated = 1; diff --git a/sysdeps/arm/dl-machine.h b/sysdeps/arm/dl-machine.h index 7e6761bbe8..86f866ca7c 100644 --- a/sysdeps/arm/dl-machine.h +++ b/sysdeps/arm/dl-machine.h @@ -21,6 +21,7 @@ #define ELF_MACHINE_NAME "ARM" +#include #include #include #include diff --git a/sysdeps/i386/dl-machine.h b/sysdeps/i386/dl-machine.h index 78ce890c0f..fa902612ca 100644 --- a/sysdeps/i386/dl-machine.h +++ b/sysdeps/i386/dl-machine.h @@ -21,6 +21,7 @@ #define ELF_MACHINE_NAME "i386" +#include #include #include #include diff --git a/sysdeps/x86_64/dl-machine.h b/sysdeps/x86_64/dl-machine.h index d3fcbb37bf..90c77cfea1 100644 --- a/sysdeps/x86_64/dl-machine.h +++ b/sysdeps/x86_64/dl-machine.h @@ -22,6 +22,7 @@ #define ELF_MACHINE_NAME "x86_64" +#include #include #include #include -- 2.35.1