From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-qk1-x734.google.com (mail-qk1-x734.google.com [IPv6:2607:f8b0:4864:20::734]) by sourceware.org (Postfix) with ESMTPS id DAD5B3851408 for ; Fri, 26 Aug 2022 18:30:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org DAD5B3851408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qk1-x734.google.com with SMTP id f14so1796689qkm.0 for ; Fri, 26 Aug 2022 11:30:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc; bh=sMZJm/zFIVvcklbks0hVdEaig8aKcj0ekYAuAj4qyHY=; b=EwP+tcNWXzuhkSvmTRIXSvteALBX4l9PSY0Ku7ID/LBmi2gfIXFkIBmwd4B0YYGioE LcLLgM4JsfYdrEXX+5ZDQa0C70h1oY0hMULgTiKTHgbwa8m8KUIAjLEmFKI4r50jO/DJ TD+ZMZk3p23B/j23bWD8xZgz1VgGRHIzSKq5uGNZlLdaR7q/t1G4A6MUzwSUU/3r8MWY h37fvwR9AeXYviRfq9369UAOc+rX2kr2pdkb7sdAe77f+MWi22rq49lebkPTT27HTkXW VeWvvK1lglTmfHns07jMB8ksOkdy/dMkaC3w3jHlLBmVjFjVWvFbSD2PJtzpG/E85aES Hc6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc; bh=sMZJm/zFIVvcklbks0hVdEaig8aKcj0ekYAuAj4qyHY=; b=oh5UyOUWKV9kiaktnmcWSXjrC3Ute3TPziKf1krwI6EcqNV1rNmJsK6WHubO+FuYls dbTGSvNp6O8EhQZGrkSbKd45KexOYz79Bb/FUkz2rrTwDUd4e+HGoxJ0BsWAinv9TdSb ALyENVwW9TEF0ihxghD3vuWQ8+YQ2mVrlfRi/SelXlH7RKrbpY/d1feo2eI/v4jGyte/ TGW70Dd/Ppq3mtoDelAFLCyBd9VofoCNWgu5nrQqh0EfiMLOfbEC3jOnOH39SIDuVF7E cZx37vQmdLQoKJ9eSWNkXZsk+s3e9i79rU6mZst04x+wirSYqZeA9eat1ZlKoZtvlhED ZduQ== X-Gm-Message-State: ACgBeo2KnSvTM5qNWDDGjMbp0zR55Y8ah3LGVuRKJYzly5kcxJVG69Cm 0QMVd6coKtayq5dCiSYbMwvGJPoc1WxhHIcR9GU= X-Google-Smtp-Source: AA6agR40m/OmsG6Q1nn86PZet6sdiQZo+wir86vwbk1HPtID7GJRMjnL47bRubO6Pqcvtn+JBh9xz+mbFBWVefLGw/M= X-Received: by 2002:a37:2c87:0:b0:6bc:475:ab83 with SMTP id s129-20020a372c87000000b006bc0475ab83mr729400qkh.670.1661538623040; Fri, 26 Aug 2022 11:30:23 -0700 (PDT) MIME-Version: 1.0 References: <87fshiq3kc.fsf@oldenburg.str.redhat.com> In-Reply-To: <87fshiq3kc.fsf@oldenburg.str.redhat.com> From: "H.J. Lu" Date: Fri, 26 Aug 2022 11:29:46 -0700 Message-ID: Subject: Re: [PATCH v2] elf: Call __libc_early_init for reused namespaces (bug 29528) To: Florian Weimer Cc: GNU C Library Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-3024.0 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,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Aug 26, 2022 at 11:10 AM Florian Weimer wrote: > > 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. > > --- > v2: Fix C&P error in trace message. > elf/Makefile | 7 +++++++ > 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 ++++++++++++++++++++++++++++++ > 5 files changed, 136 insertions(+), 5 deletions(-) > > diff --git a/elf/Makefile b/elf/Makefile > index 3928a08787..008770bfaf 100644 > --- a/elf/Makefile > +++ b/elf/Makefile > @@ -410,6 +410,7 @@ tests += \ > tst-dlmopen4 \ > tst-dlmopen-dlerror \ > tst-dlmopen-gethostbyname \ > + tst-dlmopen-twice \ > tst-dlopenfail \ > tst-dlopenfail-2 \ > tst-dlopenrpath \ > @@ -839,6 +840,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 \ > @@ -2976,3 +2979,7 @@ $(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 a23e65926b..46e8066fd8 100644 > --- a/elf/dl-open.c > +++ b/elf/dl-open.c > @@ -844,11 +844,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_update (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 > > base-commit: 5ecc98241229d494aaad23a4a3fe106fe11e1f40 > LGTM. Thanks. -- H.J.