public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Florian Weimer <fweimer@redhat.com>
To: libc-alpha@sourceware.org
Subject: [PATCH v2 07/32] elf: Disambiguate some failures in _dl_load_cache_lookup
Date: Fri, 07 Jul 2023 20:48:03 +0200	[thread overview]
Message-ID: <fdb8756d7c233510d77038d84a38b65107d1246c.1688741159.git.fweimer@redhat.com> (raw)
In-Reply-To: <cover.1688741159.git.fweimer@redhat.com>

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.
-- 
2.41.0



  parent reply	other threads:[~2023-07-07 18:48 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-07 18:47 [PATCH v2 00/32] RELRO link maps Florian Weimer
2023-07-07 18:47 ` [PATCH v2 01/32] support: Add <support/memprobe.h> for protection flags probing Florian Weimer
2023-07-07 18:47 ` [PATCH v2 02/32] misc: Enable internal use of memory protection keys Florian Weimer
2023-07-07 18:47 ` [PATCH v2 03/32] elf: Remove _dl_sysdep_open_object hook function Florian Weimer
2023-07-07 18:47 ` [PATCH v2 04/32] elf: Eliminate second loop in find_version in dl-version.c Florian Weimer
2023-07-07 18:47 ` [PATCH v2 05/32] elf: In rtld_setup_main_map, assume ld.so has a DYNAMIC segment Florian Weimer
2023-07-07 18:47 ` [PATCH v2 06/32] elf: Remove version assert in check_match in elf/dl-lookup.c Florian Weimer
2023-07-07 18:48 ` Florian Weimer [this message]
2023-07-07 18:48 ` [PATCH v2 08/32] elf: Eliminate alloca in open_verify Florian Weimer
2023-07-07 18:48 ` [PATCH v2 09/32] Do not export <alloc_buffer.h> functions from libc Florian Weimer
2023-07-07 18:48 ` [PATCH v2 10/32] elf: Make <alloc_buffer.h> usable in ld.so Florian Weimer
2023-07-07 18:48 ` [PATCH v2 11/32] elf: Merge the three implementations of _dl_dst_substitute Florian Weimer
2023-07-07 18:48 ` [PATCH v2 12/32] elf: Move __rtld_malloc_init_stubs call into _dl_start_final Florian Weimer
2023-07-07 18:48 ` [PATCH v2 13/32] elf: Merge __dl_libc_freemem into __rtld_libc_freeres Florian Weimer
2023-07-07 18:48 ` [PATCH v2 14/32] elf: Use struct link_map_private for the internal link map Florian Weimer
2023-07-07 18:48 ` [PATCH v2 15/32] elf: Remove run-time-writable fields from struct link_map_private Florian Weimer
2023-07-07 18:48 ` [PATCH v2 16/32] elf: Move l_tls_offset into read-write part of link map Florian Weimer
2023-07-07 18:48 ` [PATCH v2 17/32] elf: Allocate auditor state after read-write " Florian Weimer
2023-07-07 18:48 ` [PATCH v2 18/32] elf: Move link map fields used by dependency sorting to writable part Florian Weimer
2023-07-07 18:48 ` [PATCH v2 19/32] elf: Split _dl_lookup_map, _dl_map_new_object from _dl_map_object Florian Weimer
2023-07-07 18:48 ` [PATCH v2 20/32] elf: Add l_soname accessor function for DT_SONAME values Florian Weimer
2023-07-07 18:49 ` [PATCH v2 21/32] elf: _dl_rtld_map should not exist in static builds Florian Weimer
2023-07-07 18:49 ` [PATCH v2 22/32] elf: Introduce GLPM accessor for the protected memory area Florian Weimer
2023-07-07 18:49 ` [PATCH v2 23/32] elf: Bootstrap allocation for future protected memory allocator Florian Weimer
2023-07-07 18:49 ` [PATCH v2 24/32] elf: Implement a basic " Florian Weimer
2023-07-07 18:49 ` [PATCH v2 25/32] elf: Move most of the _dl_find_object data to the protected heap Florian Weimer
2023-07-07 18:49 ` [PATCH v2 26/32] elf: Switch to a region-based protected memory allocator Florian Weimer
2023-07-07 18:49 ` [PATCH v2 27/32] elf: Determine the caller link map in _dl_open Florian Weimer
2023-07-07 18:49 ` [PATCH v2 28/32] elf: Add fast path to dlopen for fully-opened maps Florian Weimer
2023-07-07 18:49 ` [PATCH v2 29/32] elf: Use _dl_find_object instead of _dl_find_dso_for_object in dlopen Florian Weimer
2023-07-07 18:50 ` [PATCH v2 30/32] elf: Put critical _dl_find_object pointers into protected memory area Florian Weimer
2023-07-07 19:08 ` [PATCH v2 31/32] elf: Add hash tables to speed up DT_NEEDED, dlopen lookups Florian Weimer
2023-07-07 19:08 ` [PATCH v2 32/32] elf: Use memory protection keys for the protected memory allocator Florian Weimer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fdb8756d7c233510d77038d84a38b65107d1246c.1688741159.git.fweimer@redhat.com \
    --to=fweimer@redhat.com \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).