1 /* Support for reading /etc/ld.so.cache files written by Linux ldconfig.
2    Copyright (C) 1996-2021 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4 
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9 
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14 
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <https://www.gnu.org/licenses/>.  */
18 
19 #include <assert.h>
20 #include <unistd.h>
21 #include <ldsodefs.h>
22 #include <sys/mman.h>
23 #include <dl-cache.h>
24 #include <dl-procinfo.h>
25 #include <stdint.h>
26 #include <_itoa.h>
27 #include <dl-hwcaps.h>
28 #include <dl-isa-level.h>
29 
30 #ifndef _DL_PLATFORMS_COUNT
31 # define _DL_PLATFORMS_COUNT 0
32 #endif
33 
34 /* This is the starting address and the size of the mmap()ed file.  */
35 static struct cache_file *cache;
36 static struct cache_file_new *cache_new;
37 static size_t cachesize;
38 
39 #ifdef SHARED
40 /* This is used to cache the priorities of glibc-hwcaps
41    subdirectories.  The elements of _dl_cache_priorities correspond to
42    the strings in the cache_extension_tag_glibc_hwcaps section.  */
43 static uint32_t *glibc_hwcaps_priorities;
44 static uint32_t glibc_hwcaps_priorities_length;
45 static uint32_t glibc_hwcaps_priorities_allocated;
46 
47 /* True if the full malloc was used to allocated the array.  */
48 static bool glibc_hwcaps_priorities_malloced;
49 
50 /* Deallocate the glibc_hwcaps_priorities array.  */
51 static void
glibc_hwcaps_priorities_free(void)52 glibc_hwcaps_priorities_free (void)
53 {
54   /* When the minimal malloc is in use, free does not do anything,
55      so it does not make sense to call it.  */
56   if (glibc_hwcaps_priorities_malloced)
57     free (glibc_hwcaps_priorities);
58   glibc_hwcaps_priorities = NULL;
59   glibc_hwcaps_priorities_allocated = 0;
60 }
61 
62 /* Ordered comparison of a hwcaps string from the cache on the left
63    (identified by its string table index) and a _dl_hwcaps_priorities
64    element on the right.  */
65 static int
glibc_hwcaps_compare(uint32_t left_index,struct dl_hwcaps_priority * right)66 glibc_hwcaps_compare (uint32_t left_index, struct dl_hwcaps_priority *right)
67 {
68   const char *left_name = (const char *) cache + left_index;
69   uint32_t left_name_length = strlen (left_name);
70   uint32_t to_compare;
71   if (left_name_length < right->name_length)
72     to_compare = left_name_length;
73   else
74     to_compare = right->name_length;
75   int cmp = memcmp (left_name, right->name, to_compare);
76   if (cmp != 0)
77     return cmp;
78   if (left_name_length < right->name_length)
79     return -1;
80   else if (left_name_length > right->name_length)
81     return 1;
82   else
83     return 0;
84 }
85 
86 /* Initialize the glibc_hwcaps_priorities array and its length,
87    glibc_hwcaps_priorities_length.  */
88 static void
glibc_hwcaps_priorities_init(void)89 glibc_hwcaps_priorities_init (void)
90 {
91   struct cache_extension_all_loaded ext;
92   if (!cache_extension_load (cache_new, cache, cachesize, &ext))
93     return;
94 
95   uint32_t length = (ext.sections[cache_extension_tag_glibc_hwcaps].size
96 		     / sizeof (uint32_t));
97   if (length > glibc_hwcaps_priorities_allocated)
98     {
99       glibc_hwcaps_priorities_free ();
100 
101       uint32_t *new_allocation = malloc (length * sizeof (uint32_t));
102       if (new_allocation == NULL)
103 	/* This effectively disables hwcaps on memory allocation
104 	   errors.  */
105 	return;
106 
107       glibc_hwcaps_priorities = new_allocation;
108       glibc_hwcaps_priorities_allocated = length;
109       glibc_hwcaps_priorities_malloced = __rtld_malloc_is_complete ();
110     }
111 
112   /* Compute the priorities for the subdirectories by merging the
113      array in the cache with the dl_hwcaps_priorities array.  */
114   const uint32_t *left = ext.sections[cache_extension_tag_glibc_hwcaps].base;
115   const uint32_t *left_end = left + length;
116   struct dl_hwcaps_priority *right = _dl_hwcaps_priorities;
117   struct dl_hwcaps_priority *right_end = right + _dl_hwcaps_priorities_length;
118   uint32_t *result = glibc_hwcaps_priorities;
119 
120   while (left < left_end && right < right_end)
121     {
122       if (*left < cachesize)
123 	{
124 	  int cmp = glibc_hwcaps_compare (*left, right);
125 	  if (cmp == 0)
126 	    {
127 	      *result = right->priority;
128 	      ++result;
129 	      ++left;
130 	      ++right;
131 	    }
132 	  else if (cmp < 0)
133 	    {
134 	      *result = 0;
135 	      ++result;
136 	      ++left;
137 	    }
138 	  else
139 	    ++right;
140 	}
141       else
142 	{
143 	  *result = 0;
144 	  ++result;
145 	}
146     }
147   while (left < left_end)
148     {
149       *result = 0;
150       ++result;
151       ++left;
152     }
153 
154   glibc_hwcaps_priorities_length = length;
155 }
156 
157 /* Return the priority of the cache_extension_tag_glibc_hwcaps section
158    entry at INDEX.  Zero means do not use.  Otherwise, lower values
159    indicate greater preference.  */
160 static uint32_t
glibc_hwcaps_priority(uint32_t index)161 glibc_hwcaps_priority (uint32_t index)
162 {
163   /* This does not need to repeated initialization attempts because
164      this function is only called if there is glibc-hwcaps data in the
165      cache, so the first call initializes the glibc_hwcaps_priorities
166      array.  */
167   if (glibc_hwcaps_priorities_length == 0)
168     glibc_hwcaps_priorities_init ();
169 
170   if (index < glibc_hwcaps_priorities_length)
171     return glibc_hwcaps_priorities[index];
172   else
173     return 0;
174 }
175 #endif /* SHARED */
176 
177 /* True if PTR is a valid string table index.  */
178 static inline bool
_dl_cache_verify_ptr(uint32_t ptr,size_t string_table_size)179 _dl_cache_verify_ptr (uint32_t ptr, size_t string_table_size)
180 {
181   return ptr < string_table_size;
182 }
183 
184 /* Compute the address of the element INDEX of the array at LIBS.
185    Conceptually, this is &LIBS[INDEX], but use ENTRY_SIZE for the size
186    of *LIBS.  */
187 static inline const struct file_entry *
_dl_cache_file_entry(const struct file_entry * libs,size_t entry_size,size_t index)188 _dl_cache_file_entry (const struct file_entry *libs, size_t entry_size,
189 		      size_t index)
190 {
191   return (const void *) libs + index * entry_size;
192 }
193 
194 /* We use binary search since the table is sorted in the cache file.
195    The first matching entry in the table is returned.  It is important
196    to use the same algorithm as used while generating the cache file.
197    STRING_TABLE_SIZE indicates the maximum offset in STRING_TABLE at
198    which data is mapped; it is not exact.  */
199 static const char *
search_cache(const char * string_table,uint32_t string_table_size,struct file_entry * libs,uint32_t nlibs,uint32_t entry_size,const char * name)200 search_cache (const char *string_table, uint32_t string_table_size,
201 	      struct file_entry *libs, uint32_t nlibs, uint32_t entry_size,
202 	      const char *name)
203 {
204   /* Used by the HWCAP check in the struct file_entry_new case.  */
205   uint64_t platform = _dl_string_platform (GLRO (dl_platform));
206   if (platform != (uint64_t) -1)
207     platform = 1ULL << platform;
208   uint64_t hwcap_mask = GET_HWCAP_MASK ();
209 #define _DL_HWCAP_TLS_MASK (1LL << 63)
210   uint64_t hwcap_exclude = ~((GLRO (dl_hwcap) & hwcap_mask)
211 			     | _DL_HWCAP_PLATFORM | _DL_HWCAP_TLS_MASK);
212 
213   int left = 0;
214   int right = nlibs - 1;
215   const char *best = NULL;
216 #ifdef SHARED
217   uint32_t best_priority = 0;
218 #endif
219 
220   while (left <= right)
221     {
222       int middle = (left + right) / 2;
223       uint32_t key = _dl_cache_file_entry (libs, entry_size, middle)->key;
224 
225       /* Make sure string table indices are not bogus before using
226 	 them.  */
227       if (!_dl_cache_verify_ptr (key, string_table_size))
228 	return NULL;
229 
230       /* Actually compare the entry with the key.  */
231       int cmpres = _dl_cache_libcmp (name, string_table + key);
232       if (__glibc_unlikely (cmpres == 0))
233 	{
234 	  /* Found it.  LEFT now marks the last entry for which we
235 	     know the name is correct.  */
236 	  left = middle;
237 
238 	  /* There might be entries with this name before the one we
239 	     found.  So we have to find the beginning.  */
240 	  while (middle > 0)
241 	    {
242 	      key = _dl_cache_file_entry (libs, entry_size, middle - 1)->key;
243 	      /* Make sure string table indices are not bogus before
244 		 using them.  */
245 	      if (!_dl_cache_verify_ptr (key, string_table_size)
246 		  /* Actually compare the entry.  */
247 		  || _dl_cache_libcmp (name, string_table + key) != 0)
248 		break;
249 	      --middle;
250 	    }
251 
252 	  do
253 	    {
254 	      int flags;
255 	      const struct file_entry *lib
256 		= _dl_cache_file_entry (libs, entry_size, middle);
257 
258 	      /* Only perform the name test if necessary.  */
259 	      if (middle > left
260 		  /* We haven't seen this string so far.  Test whether the
261 		     index is ok and whether the name matches.  Otherwise
262 		     we are done.  */
263 		  && (! _dl_cache_verify_ptr (lib->key, string_table_size)
264 		      || (_dl_cache_libcmp (name, string_table + lib->key)
265 			  != 0)))
266 		break;
267 
268 	      flags = lib->flags;
269 	      if (_dl_cache_check_flags (flags)
270 		  && _dl_cache_verify_ptr (lib->value, string_table_size))
271 		{
272 		  /* Named/extension hwcaps get slightly different
273 		     treatment: We keep searching for a better
274 		     match.  */
275 		  bool named_hwcap = false;
276 
277 		  if (entry_size >= sizeof (struct file_entry_new))
278 		    {
279 		      /* The entry is large enough to include
280 			 HWCAP data.  Check it.  */
281 		      struct file_entry_new *libnew
282 			= (struct file_entry_new *) lib;
283 
284 #ifdef SHARED
285 		      named_hwcap = dl_cache_hwcap_extension (libnew);
286 		      if (named_hwcap
287 			  && !dl_cache_hwcap_isa_level_compatible (libnew))
288 			continue;
289 #endif
290 
291 		      /* The entries with named/extension hwcaps have
292 			 been exhausted (they are listed before all
293 			 other entries).  Return the best match
294 			 encountered so far if there is one.  */
295 		      if (!named_hwcap && best != NULL)
296 			break;
297 
298 		      if ((libnew->hwcap & hwcap_exclude) && !named_hwcap)
299 			continue;
300 		      if (GLRO (dl_osversion)
301 			  && libnew->osversion > GLRO (dl_osversion))
302 			continue;
303 		      if (_DL_PLATFORMS_COUNT
304 			  && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0
305 			  && ((libnew->hwcap & _DL_HWCAP_PLATFORM)
306 			      != platform))
307 			continue;
308 
309 #ifdef SHARED
310 		      /* For named hwcaps, determine the priority and
311 			 see if beats what has been found so far.  */
312 		      if (named_hwcap)
313 			{
314 			  uint32_t entry_priority
315 			    = glibc_hwcaps_priority (libnew->hwcap);
316 			  if (entry_priority == 0)
317 			    /* Not usable at all.  Skip.  */
318 			    continue;
319 			  else if (best == NULL
320 				   || entry_priority < best_priority)
321 			    /* This entry is of higher priority
322 			       than the previous one, or it is the
323 			       first entry.  */
324 			    best_priority = entry_priority;
325 			  else
326 			    /* An entry has already been found,
327 			       but it is a better match.  */
328 			    continue;
329 			}
330 #endif /* SHARED */
331 		    }
332 
333 		  best = string_table + lib->value;
334 
335 		  if (!named_hwcap && flags == _DL_CACHE_DEFAULT_ID)
336 		    /* With named hwcaps, we need to keep searching to
337 		       see if we find a better match.  A better match
338 		       is also possible if the flags of the current
339 		       entry do not match the expected cache flags.
340 		       But if the flags match, no better entry will be
341 		       found.  */
342 		    break;
343 		}
344 	    }
345 	  while (++middle <= right);
346 	  break;
347 	}
348 
349       if (cmpres < 0)
350 	left = middle + 1;
351       else
352 	right = middle - 1;
353     }
354 
355   return best;
356 }
357 
358 int
_dl_cache_libcmp(const char * p1,const char * p2)359 _dl_cache_libcmp (const char *p1, const char *p2)
360 {
361   while (*p1 != '\0')
362     {
363       if (*p1 >= '0' && *p1 <= '9')
364         {
365           if (*p2 >= '0' && *p2 <= '9')
366             {
367 	      /* Must compare this numerically.  */
368 	      int val1;
369 	      int val2;
370 
371 	      val1 = *p1++ - '0';
372 	      val2 = *p2++ - '0';
373 	      while (*p1 >= '0' && *p1 <= '9')
374 	        val1 = val1 * 10 + *p1++ - '0';
375 	      while (*p2 >= '0' && *p2 <= '9')
376 	        val2 = val2 * 10 + *p2++ - '0';
377 	      if (val1 != val2)
378 		return val1 - val2;
379 	    }
380 	  else
381             return 1;
382         }
383       else if (*p2 >= '0' && *p2 <= '9')
384         return -1;
385       else if (*p1 != *p2)
386         return *p1 - *p2;
387       else
388 	{
389 	  ++p1;
390 	  ++p2;
391 	}
392     }
393   return *p1 - *p2;
394 }
395 
396 
397 /* Look up NAME in ld.so.cache and return the file name stored there, or null
398    if none is found.  The cache is loaded if it was not already.  If loading
399    the cache previously failed there will be no more attempts to load it.
400    The caller is responsible for freeing the returned string.  The ld.so.cache
401    may be unmapped at any time by a completing recursive dlopen and
402    this function must take care that it does not return references to
403    any data in the mapping.  */
404 char *
_dl_load_cache_lookup(const char * name)405 _dl_load_cache_lookup (const char *name)
406 {
407   /* Print a message if the loading of libs is traced.  */
408   if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
409     _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE);
410 
411   if (cache == NULL)
412     {
413       /* Read the contents of the file.  */
414       void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
415 					       PROT_READ);
416 
417       /* We can handle three different cache file formats here:
418 	 - only the new format
419 	 - the old libc5/glibc2.0/2.1 format
420 	 - the old format with the new format in it
421 	 The following checks if the cache contains any of these formats.  */
422       if (file != MAP_FAILED && cachesize > sizeof *cache_new
423 	  && memcmp (file, CACHEMAGIC_VERSION_NEW,
424 		     sizeof CACHEMAGIC_VERSION_NEW - 1) == 0
425 	  /* Check for corruption, avoiding overflow.  */
426 	  && ((cachesize - sizeof *cache_new) / sizeof (struct file_entry_new)
427 	      >= ((struct cache_file_new *) file)->nlibs))
428 	{
429 	  if (! cache_file_new_matches_endian (file))
430 	    {
431 	      __munmap (file, cachesize);
432 	      file = (void *) -1;
433 	    }
434 	  cache_new = file;
435 	  cache = file;
436 	}
437       else if (file != MAP_FAILED && cachesize > sizeof *cache
438 	       && memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1) == 0
439 	       /* Check for corruption, avoiding overflow.  */
440 	       && ((cachesize - sizeof *cache) / sizeof (struct file_entry)
441 		   >= ((struct cache_file *) file)->nlibs))
442 	{
443 	  size_t offset;
444 	  /* Looks ok.  */
445 	  cache = file;
446 
447 	  /* Check for new version.  */
448 	  offset = ALIGN_CACHE (sizeof (struct cache_file)
449 				+ cache->nlibs * sizeof (struct file_entry));
450 
451 	  cache_new = (struct cache_file_new *) ((void *) cache + offset);
452 	  if (cachesize < (offset + sizeof (struct cache_file_new))
453 	      || memcmp (cache_new->magic, CACHEMAGIC_VERSION_NEW,
454 			 sizeof CACHEMAGIC_VERSION_NEW - 1) != 0)
455 	      cache_new = (void *) -1;
456 	  else
457 	    {
458 	      if (! cache_file_new_matches_endian (cache_new))
459 		{
460 		  /* The old-format part of the cache is bogus as well
461 		     if the endianness does not match.  (But it is
462 		     unclear how the new header can be located if the
463 		     endianess does not match.)  */
464 		  cache = (void *) -1;
465 		  cache_new = (void *) -1;
466 		  __munmap (file, cachesize);
467 		}
468 	    }
469 	}
470       else
471 	{
472 	  if (file != MAP_FAILED)
473 	    __munmap (file, cachesize);
474 	  cache = (void *) -1;
475 	}
476 
477       assert (cache != NULL);
478     }
479 
480   if (cache == (void *) -1)
481     /* Previously looked for the cache file and didn't find it.  */
482     return NULL;
483 
484   const char *best;
485   if (cache_new != (void *) -1)
486     {
487       const char *string_table = (const char *) cache_new;
488       best = search_cache (string_table, cachesize,
489 			   &cache_new->libs[0].entry, cache_new->nlibs,
490 			   sizeof (cache_new->libs[0]), name);
491     }
492   else
493     {
494       const char *string_table = (const char *) &cache->libs[cache->nlibs];
495       uint32_t string_table_size
496 	= (const char *) cache + cachesize - string_table;
497       best = search_cache (string_table, string_table_size,
498 			   &cache->libs[0], cache->nlibs,
499 			   sizeof (cache->libs[0]), name);
500     }
501 
502   /* Print our result if wanted.  */
503   if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
504       && best != NULL)
505     _dl_debug_printf ("  trying file=%s\n", best);
506 
507   if (best == NULL)
508     return NULL;
509 
510   /* The double copy is *required* since malloc may be interposed
511      and call dlopen itself whose completion would unmap the data
512      we are accessing. Therefore we must make the copy of the
513      mapping data without using malloc.  */
514   char *temp;
515   temp = alloca (strlen (best) + 1);
516   strcpy (temp, best);
517   return __strdup (temp);
518 }
519 
520 #ifndef MAP_COPY
521 /* If the system does not support MAP_COPY we cannot leave the file open
522    all the time since this would create problems when the file is replaced.
523    Therefore we provide this function to close the file and open it again
524    once needed.  */
525 void
_dl_unload_cache(void)526 _dl_unload_cache (void)
527 {
528   if (cache != NULL && cache != (struct cache_file *) -1)
529     {
530       __munmap (cache, cachesize);
531       cache = NULL;
532     }
533 #ifdef SHARED
534   /* This marks the glibc_hwcaps_priorities array as out-of-date.  */
535   glibc_hwcaps_priorities_length = 0;
536 #endif
537 }
538 #endif
539