From patchwork Thu Aug 8 11:40:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 1970515 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=EDVmdqsB; 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 4WflYN1J5cz1yfC for ; Thu, 8 Aug 2024 21:43:36 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3A28A3858424 for ; Thu, 8 Aug 2024 11:43:33 +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 062E13858C56 for ; Thu, 8 Aug 2024 11:43:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 062E13858C56 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 062E13858C56 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=1723117394; cv=none; b=biZuJTUTns20mSXE9jv1dCBLonC0Dacz6IaWBmqQQg3obGdg82z5cUtrI2o3TYvQ4QLtPLdbSy59Wuk5Rtq39Sa69mGV5YdzeJ0kTwRLnSolJBgLUB3X2G5m+iIYNJSq4H/X2vk6chf1qalOZfWno520Q7l6NlC6JAfZBX+Ap5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723117394; c=relaxed/simple; bh=8ji9maPoaK5YIZjaarLHmD6ts5ZuLs/CHoLnVIippWw=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=weINWfmqIcQYydMIkKvwOISCrxUq1854WGpWQ1VLQSXBaihaUUNfPspObFDiOWVQBcprrJrkCxA7ZVEJENLsGlJ3rXv8n+J3FrThU5KKxXjgCgtS4/rVW87yow94heQI8UoYU2KdVd/SYnCgudC5yJNEs3oEy5P8oVsjEXgn/Ek= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1723117391; 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=4l3tn28KLLCb9PwjEpuzOSKiqjHDnNE8m38tzmWPHyY=; b=EDVmdqsBL2wViY+0DBnAJzVg06iTMcNbx5k8oBkQxHcqhTuC68n8p5KEyllMDAOq/S90yc gvw8US15kdlu+1Eh68lVVMcsPSHz3VMGGpGFBDy7F5I+/nEpg/FH7FXk2DL5e0VHjYRqyY OJCLlw2xODuszMUi86Xz7n2HD+vX76k= 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-556-l8hod5hRNgeeRN2QOKiFRA-1; Thu, 08 Aug 2024 07:43:09 -0400 X-MC-Unique: l8hod5hRNgeeRN2QOKiFRA-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 95E6C197730E for ; Thu, 8 Aug 2024 11:40:05 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.45.224.76]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 9C15F3000197 for ; Thu, 8 Aug 2024 11:40:04 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v2 1/3] elf: Run constructors on cyclic recursive dlopen (bug 31986) In-Reply-To: Message-ID: <3f27d400f91fbf02eabd188f002c3003dd6f994d.1723116962.git.fweimer@redhat.com> References: X-From-Line: 3f27d400f91fbf02eabd188f002c3003dd6f994d Mon Sep 17 00:00:00 2001 Date: Thu, 08 Aug 2024 13:40:01 +0200 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-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP 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.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 is conceptually similar to the reported bug, but does not depend on auditing. The fix is simple: just complete execution of the constructors. This exposed the fact that the link map for statically linked executables does not have l_init_called set, even though constructors have run. Reviewed-by: Adhemerval Zanella --- elf/Makefile | 6 ++++ elf/dl-open.c | 8 +++++ elf/dl-support.c | 1 + elf/tst-dlopen-recurse.c | 34 +++++++++++++++++++ elf/tst-dlopen-recursemod1.c | 50 +++++++++++++++++++++++++++ elf/tst-dlopen-recursemod2.c | 66 ++++++++++++++++++++++++++++++++++++ 6 files changed, 165 insertions(+) create mode 100644 elf/tst-dlopen-recurse.c create mode 100644 elf/tst-dlopen-recursemod1.c create mode 100644 elf/tst-dlopen-recursemod2.c diff --git a/elf/Makefile b/elf/Makefile index 0792b57678..cc3685550d 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -414,6 +414,7 @@ tests += \ tst-dlmopen1 \ tst-dlmopen3 \ tst-dlmopen4 \ + tst-dlopen-recurse \ tst-dlopen-self \ tst-dlopen-tlsmodid \ tst-dlopen-tlsreinit1 \ @@ -864,6 +865,8 @@ modules-names += \ tst-dlmopen-twice-mod1 \ tst-dlmopen-twice-mod2 \ tst-dlmopen1mod \ + tst-dlopen-recursemod1 \ + tst-dlopen-recursemod2 \ tst-dlopen-tlsreinitmod1 \ tst-dlopen-tlsreinitmod2 \ tst-dlopen-tlsreinitmod3 \ @@ -3155,3 +3158,6 @@ $(objpfx)tst-dlopen-tlsreinit3.out: $(objpfx)tst-auditmod1.so tst-dlopen-tlsreinit3-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so $(objpfx)tst-dlopen-tlsreinit4.out: $(objpfx)tst-auditmod1.so tst-dlopen-tlsreinit4-ENV = LD_AUDIT=$(objpfx)tst-auditmod1.so + +$(objpfx)tst-dlopen-recurse.out: $(objpfx)tst-dlopen-recursemod1.so +$(objpfx)tst-dlopen-recursemod1.so: $(objpfx)tst-dlopen-recursemod2.so diff --git a/elf/dl-open.c b/elf/dl-open.c index 8b4704c09d..2c20aa1df9 100644 --- a/elf/dl-open.c +++ b/elf/dl-open.c @@ -590,6 +590,14 @@ dl_open_worker_begin (void *a) = _dl_debug_update (args->nsid)->r_state; assert (r_state == RT_CONSISTENT); + /* Do not return without calling the (supposedly new) map's + constructor. This case occurs if a dependency of a directly + opened map has a constructor that calls dlopen again on the + initially opened map. The new map is initialized last, so + checking only it is enough. */ + if (!new->l_init_called) + _dl_catch_exception (NULL, call_dl_init, args); + return; } diff --git a/elf/dl-support.c b/elf/dl-support.c index 451932dd03..94e8197c63 100644 --- a/elf/dl-support.c +++ b/elf/dl-support.c @@ -99,6 +99,7 @@ static struct link_map _dl_main_map = .l_used = 1, .l_tls_offset = NO_TLS_OFFSET, .l_serial = 1, + .l_init_called = 1, }; /* Namespace information. */ diff --git a/elf/tst-dlopen-recurse.c b/elf/tst-dlopen-recurse.c new file mode 100644 index 0000000000..c7fb379d37 --- /dev/null +++ b/elf/tst-dlopen-recurse.c @@ -0,0 +1,34 @@ +/* Test that recursive dlopen runs constructors before return (bug 31986). + 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 +#include + +static int +do_test (void) +{ + void *handle = xdlopen ("tst-dlopen-recursemod1.so", RTLD_NOW); + int *status = dlsym (handle, "recursemod1_status"); + printf ("info: recursemod1_status == %d (from main)\n", *status); + TEST_COMPARE (*status, 2); + xdlclose (handle); + return 0; +} + +#include diff --git a/elf/tst-dlopen-recursemod1.c b/elf/tst-dlopen-recursemod1.c new file mode 100644 index 0000000000..5e0cc0eb8c --- /dev/null +++ b/elf/tst-dlopen-recursemod1.c @@ -0,0 +1,50 @@ +/* Directly opened test module that gets recursively opened again. + 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 +#include + +int recursemod1_status; + +/* Force linking against st-dlopen-recursemod2.so. Also allows + checking for relocation. */ +extern int recursemod2_status; +int *force_recursemod2_reference = &recursemod2_status; + +static void __attribute__ ((constructor)) +init (void) +{ + ++recursemod1_status; + printf ("info: tst-dlopen-recursemod1.so constructor called (status %d)\n", + recursemod1_status); +} + +static void __attribute__ ((destructor)) +fini (void) +{ + /* The recursemod1_status variable was incremented in the + tst-dlopen-recursemod2.so constructor. */ + printf ("info: tst-dlopen-recursemod1.so destructor called (status %d)\n", + recursemod1_status); + if (recursemod1_status != 2) + { + puts ("error: recursemod1_status == 2 expected"); + exit (1); + } +} diff --git a/elf/tst-dlopen-recursemod2.c b/elf/tst-dlopen-recursemod2.c new file mode 100644 index 0000000000..edd2f2526b --- /dev/null +++ b/elf/tst-dlopen-recursemod2.c @@ -0,0 +1,66 @@ +/* Indirectly opened module that recursively opens the directly opened module. + 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 +#include + +int recursemod2_status; + +static void __attribute__ ((constructor)) +init (void) +{ + ++recursemod2_status; + printf ("info: tst-dlopen-recursemod2.so constructor called (status %d)\n", + recursemod2_status); + void *handle = dlopen ("tst-dlopen-recursemod1.so", RTLD_NOW); + if (handle == NULL) + { + printf ("error: dlopen: %s\n", dlerror ()); + exit (1); + } + int *status = dlsym (handle, "recursemod1_status"); + if (status == NULL) + { + printf ("error: dlsym: %s\n", dlerror ()); + exit (1); + } + printf ("info: recursemod1_status == %d\n", *status); + if (*status != 1) + { + puts ("error: recursemod1_status == 1 expected"); + exit (1); + } + ++*status; + printf ("info: recursemod1_status == %d\n", *status); + + int **mod2_status = dlsym (handle, "force_recursemod2_reference"); + if (mod2_status == NULL || *mod2_status != &recursemod2_status) + { + puts ("error: invalid recursemod2_status address in" + " tst-dlopen-recursemod1.so"); + exit (1); + } +} + +static void __attribute__ ((destructor)) +fini (void) +{ + printf ("info: tst-dlopen-recursemod2.so destructor called (status %d)\n", + recursemod2_status); +}