From patchwork Fri Jul 7 18:48:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 72321 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 2B7C7386EAD1 for ; Fri, 7 Jul 2023 18:48:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2B7C7386EAD1 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1688755712; bh=M9uOHYVuab/JACaCWUooiYYIQNm2lieNWKjT0gKYm+I=; h=To:Subject:In-Reply-To:References:Date:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=gQnsT037/6tx5mkscOUQUO8xZtA/GChDmkILj6YZvuI8AyJ/itDUSyVdeVrhJGsjS XpWhsHPvk5P/jXfNgdnvpk2v8gqyFPo6bJctjHbB9Yv7F4yeK/tbdUnD0HLcMEtwhh PpE0ieOmy7/Fr8Qo56QPIIOSzfEn+vzTzGRbyDAg= 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 ESMTPS id 25123386C4B0 for ; Fri, 7 Jul 2023 18:48:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 25123386C4B0 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-604-bZcP0lLQOOmji-JPK75xVg-1; Fri, 07 Jul 2023 14:48:05 -0400 X-MC-Unique: bZcP0lLQOOmji-JPK75xVg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 879FE805C10 for ; Fri, 7 Jul 2023 18:48:05 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.2.16.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E0366492B01 for ; Fri, 7 Jul 2023 18:48:04 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v2 07/32] elf: Disambiguate some failures in _dl_load_cache_lookup In-Reply-To: Message-ID: References: X-From-Line: fdb8756d7c233510d77038d84a38b65107d1246c Mon Sep 17 00:00:00 2001 Date: Fri, 07 Jul 2023 20:48:03 +0200 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.9 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Florian Weimer via Libc-alpha From: Florian Weimer Reply-To: Florian Weimer Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" 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 9a9cee599d..ff721fe5d2 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -2079,7 +2079,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 8b7e603c87..5941da3ec1 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.