From patchwork Thu Dec 7 10:31:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 81634 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A2726385DC1C for ; Thu, 7 Dec 2023 10:31:57 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id D8499385DC28 for ; Thu, 7 Dec 2023 10:31:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D8499385DC28 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 D8499385DC28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945099; cv=none; b=Cqz7fNULxiZ9FoZdP/vvm5ZLQoUL6Ox5KETPh8ZGa6CLZN2T2M/Z4k0U3sYdpibVI6zcToETL/16x7FQESYLYCV5N29GCm1Khm/h9DL6KMuYOPFgvwkM8WN7Za7AZC+N2ZgDOWthslKbzrPMFR6amtuQ4AG/g10mIMgZdbjM+LQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701945099; c=relaxed/simple; bh=BEMsta3HgSzBV8MajDPgjuQkvAWrCc6/5+NVEIykd8Y=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=lqmSwYA0Om0l2TbWNluLS/UF57SJkHqKKOi1Rbw7MopVQWaB7wUxY10X8pXaChx0B7wTmWFBKAYZ6ko4G0q4q4QM48WzXAlO7L6AXoWTMWpvIfs7QrlHGxYdjrawJARsbXaLfQtlDeUOZtt2qfohgaAjx9WMvKrQmrseDMax0Cs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701945097; 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=jwk6fEummaOkXlfsN/bwaXFkOjRLlQyZEnQ85QP1Ruo=; b=RPIr0E367Ap2ffmyMlV4RFN3b2umJmFO2yud5kuxEwwPDiTWBjdfbClTcJvFyEY0NMpbfs H93PpnbOycwkfTTpsAEPPmJfxlsvtdqQvivueN9dfknCYV8GrwIsg6I/KoG+QYYvHUiH95 DGP94HUMJCWqjekYdOiXFZZZGox71PE= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-595-5HnpjtsjO6WvI0m0B00Peg-1; Thu, 07 Dec 2023 05:31:36 -0500 X-MC-Unique: 5HnpjtsjO6WvI0m0B00Peg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 mimecast-mx02.redhat.com (Postfix) with ESMTPS id E9C0E3C100B9 for ; Thu, 7 Dec 2023 10:31:35 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.131]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4E9BC1C060AF for ; Thu, 7 Dec 2023 10:31:35 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH v3 07/32] elf: Disambiguate some failures in _dl_load_cache_lookup In-Reply-To: Message-ID: <37bdcf0c7d559672bb913bb55cf681d60a6c30d8.1701944612.git.fweimer@redhat.com> References: X-From-Line: 37bdcf0c7d559672bb913bb55cf681d60a6c30d8 Mon Sep 17 00:00:00 2001 Date: Thu, 07 Dec 2023 11:31:33 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 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, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, 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 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+patchwork=sourceware.org@sourceware.org Failure to allocate a copy of the string is now distinct from a cache lookup failure. Some infrastructure failures in _dl_sysdep_read_whole_file are still treated as cache lookup failures, though. --- elf/dl-cache.c | 22 ++++++++++++++++------ elf/dl-load.c | 5 ++++- sysdeps/generic/ldsodefs.h | 10 +++++++--- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/elf/dl-cache.c b/elf/dl-cache.c index 804bf23222..a3eb960dac 100644 --- a/elf/dl-cache.c +++ b/elf/dl-cache.c @@ -398,8 +398,8 @@ _dl_cache_libcmp (const char *p1, const char *p2) may be unmapped at any time by a completing recursive dlopen and this function must take care that it does not return references to any data in the mapping. */ -char * -_dl_load_cache_lookup (const char *name) +bool +_dl_load_cache_lookup (const char *name, char **realname) { /* Print a message if the loading of libs is traced. */ if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS)) @@ -475,8 +475,11 @@ _dl_load_cache_lookup (const char *name) } if (cache == (void *) -1) - /* Previously looked for the cache file and didn't find it. */ - return NULL; + { + /* Previously looked for the cache file and didn't find it. */ + *realname = NULL; + return true; + } const char *best; if (cache_new != (void *) -1) @@ -502,7 +505,10 @@ _dl_load_cache_lookup (const char *name) _dl_debug_printf (" trying file=%s\n", best); if (best == NULL) - return NULL; + { + *realname = NULL; + return true; + } /* The double copy is *required* since malloc may be interposed and call dlopen itself whose completion would unmap the data @@ -512,7 +518,11 @@ _dl_load_cache_lookup (const char *name) size_t best_len = strlen (best) + 1; temp = alloca (best_len); memcpy (temp, best, best_len); - return __strdup (temp); + char *copy = __strdup (temp); + if (copy == NULL) + return false; + *realname = copy; + return true; } #ifndef MAP_COPY diff --git a/elf/dl-load.c b/elf/dl-load.c index 65f910f0e5..2084366663 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -2082,7 +2082,10 @@ _dl_map_object (struct link_map *loader, const char *name, { /* Check the list of libraries in the file /etc/ld.so.cache, for compatibility with Linux's ldconfig program. */ - char *cached = _dl_load_cache_lookup (name); + char *cached; + if (!_dl_load_cache_lookup (name, &cached)) + _dl_signal_error (ENOMEM, NULL, NULL, + N_("cannot allocate library name")); if (cached != NULL) { diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h index 9b50ddd09f..80f078b65f 100644 --- a/sysdeps/generic/ldsodefs.h +++ b/sysdeps/generic/ldsodefs.h @@ -1111,9 +1111,13 @@ const struct r_strlenpair *_dl_important_hwcaps (const char *prepend, size_t *max_capstrlen) attribute_hidden; -/* Look up NAME in ld.so.cache and return the file name stored there, - or null if none is found. Caller must free returned string. */ -extern char *_dl_load_cache_lookup (const char *name) attribute_hidden; +/* Look up NAME in ld.so.cache. Return false on memory allocation + failure and do not change *REALNAME. If lookup fails, return true + and write a null pointer to *REALNAME. If lookup suceeds, write a + copy of the full name to *REALNAME (which has to be freed by the + caller). */ +bool _dl_load_cache_lookup (const char *name, char **realname) + attribute_hidden __nonnull ((1, 2)) __attribute__ ((warn_unused_result)); /* If the system does not support MAP_COPY we cannot leave the file open all the time since this would create problems when the file is replaced.