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.133.124]) by sourceware.org (Postfix) with ESMTPS id 2F4B73858D20 for ; Tue, 30 May 2023 13:41:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2F4B73858D20 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1685454068; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=TZa0ff3fwwc6zPHAh+8VaKy1jSviaSVkKmUBybr5ZfQ=; b=Zz7uRh832fkPSctQh3QVIrIIunfOWpIVz1zfLkAo8xNQ5S7DVuZl4OqFRlgdj4yDnBkbot VhVYn5BhyrB8lKa5qkpfRn1aINvPOLixWUL0UYf6F2/DsxAc52XJ/GG5z03QjVyVEceT5E gL32Mrk7RgsdmnD6Dm2YyGKxDKh0aAc= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-tEhsBtgAP2GeknUgY8wPLQ-1; Tue, 30 May 2023 09:41:07 -0400 X-MC-Unique: tEhsBtgAP2GeknUgY8wPLQ-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 435D53C0F366; Tue, 30 May 2023 13:41:07 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AC69740C6EC4; Tue, 30 May 2023 13:41:06 +0000 (UTC) From: Florian Weimer To: Szabolcs Nagy Cc: libc-alpha@sourceware.org Subject: Re: [PATCH v3] elf: Make more functions available for binding during dlclose (bug 30425) References: <875y8aw3ut.fsf@oldenburg.str.redhat.com> Date: Tue, 30 May 2023 15:41:04 +0200 In-Reply-To: (Szabolcs Nagy's message of "Tue, 30 May 2023 14:33:41 +0100") Message-ID: <87zg5mrl73.fsf@oldenburg.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.2 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain X-Spam-Status: No, score=-10.9 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,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: * Szabolcs Nagy: > The 05/30/2023 11:44, Florian Weimer via Libc-alpha wrote: >> diff --git a/elf/dl-lookup.c b/elf/dl-lookup.c >> index 05f36a2507..a8f48fed12 100644 >> --- a/elf/dl-lookup.c >> +++ b/elf/dl-lookup.c >> @@ -366,8 +366,25 @@ do_lookup_x (const char *undef_name, unsigned int new_hash, >> if ((type_class & ELF_RTYPE_CLASS_COPY) && map->l_type == lt_executable) >> continue; >> >> - /* Do not look into objects which are going to be removed. */ >> - if (map->l_removed) >> + /* Do not look into objects which are going to be removed, >> + except when the referencing object itself is being removed. >> + >> + The second part covers the situation when an object lazily >> + binds to another object while running its destructor, but the >> + destructor of the other object has already run, so that >> + dlclose has set l_removed. It may not always be obvious how >> + to avoid such a scenario to programmers creating DSOs, >> + particularly if C++ vague linkage is involved and triggers >> + symbol interposition. >> + >> + Accepting these to-be-removed objects makes the lazy and >> + BIND_NOW cases more similar. (With BIND_NOW, the symbol is >> + resolved early, before the destructor call, so the issue does >> + not arise.). Behavior matches the constructor scenario: the >> + implementation allows binding to symbols of objects whose >> + constructors have not run. In fact, not doing this would be >> + mostly incompatible with symbol interposition. */ >> + if (map->l_removed && !(undef_map != NULL && undef_map->l_removed)) >> continue; > > btw is there a valid use-case that goes wrong if the check is > dropped completely? (keep binding to map when map->l_removed) I think something like this is needed for useful diagnostics in multi-threaded programs, where another thread mind bind lazily to an object that is under removal. Usually, we'd record a relocation dependency to prevent removal, but we can't do that once dlclose has started for real. So without the l_removed check, we proceed to bind the symbol, and crash during a later call. With the check and a non-weak symbol, we terminate the process with an error message naming the symbol at least. But that suggests we should set l_removed even earlier, before invoking ELF destructors. Thanks, Florian