From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2178) id 20BE03835DCA; Tue, 30 Aug 2022 14:30:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 20BE03835DCA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1661869829; bh=+TYAQlY6uRDLxiKlUHD1kWxfCb0X8EjZePwQybOWQdc=; h=From:To:Subject:Date:From; b=Gg4iYr1uO1OsHZpJgmBdqh7Os3YrIvqGfEF5NEF111yLx4anT68wlXBMHVUrufkCd BkYbNAIsJzVrYLmzbNO8WGa6xlSJ63bdq1mx2OIJPTndvPvfgsYdMDmXZyR+lC2Acl X1hMJ7AzysCE+2JwGA0yos3bcxFD7ekP4L9u22Rw= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Florian Weimer To: glibc-cvs@sourceware.org Subject: [glibc/release/2.34/master] elf: Call __libc_early_init for reused namespaces (bug 29528) X-Act-Checkin: glibc X-Git-Author: Florian Weimer X-Git-Refname: refs/heads/release/2.34/master X-Git-Oldrev: 68507377f249d165f1f35502d96e9365edb07d9a X-Git-Newrev: 536ddc5c02f1ee82483319863a893ccb381beece Message-Id: <20220830143029.20BE03835DCA@sourceware.org> Date: Tue, 30 Aug 2022 14:30:29 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=536ddc5c02f1ee82483319863a893ccb381beece commit 536ddc5c02f1ee82483319863a893ccb381beece Author: Florian Weimer Date: Fri Aug 26 21:15:43 2022 +0200 elf: Call __libc_early_init for reused namespaces (bug 29528) libc_map is never reset to NULL, neither during dlclose nor on a dlopen call which reuses the namespace structure. As a result, if a namespace is reused, its libc is not initialized properly. The most visible result is a crash in the functions. To prevent similar bugs on namespace reuse from surfacing, unconditionally initialize the chosen namespace to zero using memset. (cherry picked from commit d0e357ff45a75553dee3b17ed7d303bfa544f6fe) Diff: --- NEWS | 1 + elf/Makefile | 25 ++++++++++++++++++++++ elf/dl-open.c | 13 +++++++----- elf/tst-dlmopen-twice-mod1.c | 37 ++++++++++++++++++++++++++++++++ elf/tst-dlmopen-twice-mod2.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ elf/tst-dlmopen-twice.c | 34 ++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 70d739b8ec..ef7671b2f6 100644 --- a/NEWS +++ b/NEWS @@ -115,6 +115,7 @@ The following bugs are resolved with this release: [29214] nptl: pthread_setcanceltype fails to set type [29446] _dlopen now ignores dl_caller argument in static mode [29490] alpha: New __brk_call implementation is broken + [29528] elf: Call __libc_early_init for reused namespaces Version 2.34 diff --git a/elf/Makefile b/elf/Makefile index 344b80f76a..576e2c8a8a 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -386,6 +386,7 @@ tests += \ tst-dlmopen3 \ tst-dlmopen-dlerror \ tst-dlmopen-gethostbyname \ + tst-dlmopen-twice \ tst-dlopenfail \ tst-dlopenfail-2 \ tst-dlopenrpath \ @@ -723,6 +724,8 @@ modules-names = \ tst-dlmopen1mod \ tst-dlmopen-dlerror-mod \ tst-dlmopen-gethostbyname-mod \ + tst-dlmopen-twice-mod1 \ + tst-dlmopen-twice-mod2 \ tst-dlopenfaillinkmod \ tst-dlopenfailmod1 \ tst-dlopenfailmod2 \ @@ -2610,3 +2613,25 @@ $(objpfx)tst-tls-allocation-failure-static-patched.out: \ grep -q '^Fatal glibc error: Cannot allocate TLS block$$' $@ \ && grep -q '^status: 127$$' $@; \ $(evaluate-test) + +$(objpfx)tst-audit-tlsdesc: $(objpfx)tst-audit-tlsdesc-mod1.so \ + $(objpfx)tst-audit-tlsdesc-mod2.so \ + $(shared-thread-library) +ifeq (yes,$(have-mtls-dialect-gnu2)) +# The test is valid for all TLS types, but we want to exercise GNU2 +# TLS if possible. +CFLAGS-tst-audit-tlsdesc-mod1.c += -mtls-dialect=gnu2 +CFLAGS-tst-audit-tlsdesc-mod2.c += -mtls-dialect=gnu2 +endif +$(objpfx)tst-audit-tlsdesc-dlopen: $(shared-thread-library) +$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-audit-tlsdesc-mod1.so \ + $(objpfx)tst-audit-tlsdesc-mod2.so +$(objpfx)tst-audit-tlsdesc-mod1.so: $(objpfx)tst-audit-tlsdesc-mod2.so +$(objpfx)tst-audit-tlsdesc.out: $(objpfx)tst-auditmod-tlsdesc.so +tst-audit-tlsdesc-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so +$(objpfx)tst-audit-tlsdesc-dlopen.out: $(objpfx)tst-auditmod-tlsdesc.so +tst-audit-tlsdesc-dlopen-ENV = LD_AUDIT=$(objpfx)tst-auditmod-tlsdesc.so + +$(objpfx)tst-dlmopen-twice.out: \ + $(objpfx)tst-dlmopen-twice-mod1.so \ + $(objpfx)tst-dlmopen-twice-mod2.so diff --git a/elf/dl-open.c b/elf/dl-open.c index bc68726328..1ab3c7b5ac 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -839,11 +839,14 @@ _dl_open (const char *file, int mode, const void *caller_dlopen, Lmid_t nsid, _dl_signal_error (EINVAL, file, NULL, N_("\ no more namespaces available for dlmopen()")); } - else if (nsid == GL(dl_nns)) - { - __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock); - ++GL(dl_nns); - } + + if (nsid == GL(dl_nns)) + ++GL(dl_nns); + + /* Initialize the new namespace. Most members are + zero-initialized, only the lock needs special treatment. */ + memset (&GL(dl_ns)[nsid], 0, sizeof (GL(dl_ns)[nsid])); + __rtld_lock_initialize (GL(dl_ns)[nsid]._ns_unique_sym_table.lock); _dl_debug_initialize (0, nsid)->r_state = RT_CONSISTENT; } diff --git a/elf/tst-dlmopen-twice-mod1.c b/elf/tst-dlmopen-twice-mod1.c new file mode 100644 index 0000000000..0eaf04948c --- /dev/null +++ b/elf/tst-dlmopen-twice-mod1.c @@ -0,0 +1,37 @@ +/* Initialization of libc after dlmopen/dlclose/dlmopen (bug 29528). Module 1. + Copyright (C) 2022 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 void __attribute__ ((constructor)) +init (void) +{ + puts ("info: tst-dlmopen-twice-mod1.so loaded"); + fflush (stdout); +} + +static void __attribute__ ((destructor)) +fini (void) +{ + puts ("info: tst-dlmopen-twice-mod1.so about to be unloaded"); + fflush (stdout); +} + +/* Large allocation. The second module does not have this, so it + should load libc at a different address. */ +char large_allocate[16 * 1024 * 1024]; diff --git a/elf/tst-dlmopen-twice-mod2.c b/elf/tst-dlmopen-twice-mod2.c new file mode 100644 index 0000000000..40c6c01f96 --- /dev/null +++ b/elf/tst-dlmopen-twice-mod2.c @@ -0,0 +1,50 @@ +/* Initialization of libc after dlmopen/dlclose/dlmopen (bug 29528). Module 2. + Copyright (C) 2022 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 __attribute__ ((constructor)) +init (void) +{ + puts ("info: tst-dlmopen-twice-mod2.so loaded"); + fflush (stdout); +} + +static void __attribute__ ((destructor)) +fini (void) +{ + puts ("info: tst-dlmopen-twice-mod2.so about to be unloaded"); + fflush (stdout); +} + +int +run_check (void) +{ + puts ("info: about to call isalpha"); + fflush (stdout); + + volatile char ch = 'a'; + if (!isalpha (ch)) + { + puts ("error: isalpha ('a') is not true"); + fflush (stdout); + return 1; + } + return 0; +} diff --git a/elf/tst-dlmopen-twice.c b/elf/tst-dlmopen-twice.c new file mode 100644 index 0000000000..449f3c8fa9 --- /dev/null +++ b/elf/tst-dlmopen-twice.c @@ -0,0 +1,34 @@ +/* Initialization of libc after dlmopen/dlclose/dlmopen (bug 29528). Main. + Copyright (C) 2022 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 int +do_test (void) +{ + void *handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-twice-mod1.so", RTLD_NOW); + xdlclose (handle); + handle = xdlmopen (LM_ID_NEWLM, "tst-dlmopen-twice-mod2.so", RTLD_NOW); + int (*run_check) (void) = xdlsym (handle, "run_check"); + TEST_COMPARE (run_check (), 0); + xdlclose (handle); + return 0; +} + +#include