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. */
35static struct cache_file *cache;
36static struct cache_file_new *cache_new;
37static 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. */
43static uint32_t *glibc_hwcaps_priorities;
44static uint32_t glibc_hwcaps_priorities_length;
45static uint32_t glibc_hwcaps_priorities_allocated;
46
47/* True if the full malloc was used to allocated the array. */
48static bool glibc_hwcaps_priorities_malloced;
49
50/* Deallocate the glibc_hwcaps_priorities array. */
51static void
52glibc_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. */
65static int
66glibc_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. */
88static void
89glibc_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. */
160static uint32_t
161glibc_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. */
178static inline bool
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. */
187static inline const struct file_entry *
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. */
199static const char *
200search_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 if (best == NULL || flags == GLRO (dl_correct_cache_id))
273 {
274 /* Named/extension hwcaps get slightly different
275 treatment: We keep searching for a better
276 match. */
277 bool named_hwcap = false;
278
279 if (entry_size >= sizeof (struct file_entry_new))
280 {
281 /* The entry is large enough to include
282 HWCAP data. Check it. */
283 struct file_entry_new *libnew
284 = (struct file_entry_new *) lib;
285
286#ifdef SHARED
287 named_hwcap = dl_cache_hwcap_extension (libnew);
288 if (named_hwcap
289 && !dl_cache_hwcap_isa_level_compatible (libnew))
290 continue;
291#endif
292
293 /* The entries with named/extension hwcaps
294 have been exhausted. Return the best
295 match encountered so far if there is
296 one. */
297 if (!named_hwcap && best != NULL)
298 break;
299
300 if ((libnew->hwcap & hwcap_exclude) && !named_hwcap)
301 continue;
302 if (GLRO (dl_osversion)
303 && libnew->osversion > GLRO (dl_osversion))
304 continue;
305 if (_DL_PLATFORMS_COUNT
306 && (libnew->hwcap & _DL_HWCAP_PLATFORM) != 0
307 && ((libnew->hwcap & _DL_HWCAP_PLATFORM)
308 != platform))
309 continue;
310
311#ifdef SHARED
312 /* For named hwcaps, determine the priority
313 and see if beats what has been found so
314 far. */
315 if (named_hwcap)
316 {
317 uint32_t entry_priority
318 = glibc_hwcaps_priority (libnew->hwcap);
319 if (entry_priority == 0)
320 /* Not usable at all. Skip. */
321 continue;
322 else if (best == NULL
323 || entry_priority < best_priority)
324 /* This entry is of higher priority
325 than the previous one, or it is the
326 first entry. */
327 best_priority = entry_priority;
328 else
329 /* An entry has already been found,
330 but it is a better match. */
331 continue;
332 }
333#endif /* SHARED */
334 }
335
336 best = string_table + lib->value;
337
338 if (flags == GLRO (dl_correct_cache_id)
339 && !named_hwcap)
340 /* We've found an exact match for the shared
341 object and no general `ELF' release. Stop
342 searching, but not if a named (extension)
343 hwcap is used. In this case, an entry with
344 a higher priority may come up later. */
345 break;
346 }
347 }
348 }
349 while (++middle <= right);
350 break;
351 }
352
353 if (cmpres < 0)
354 left = middle + 1;
355 else
356 right = middle - 1;
357 }
358
359 return best;
360}
361
362int
363_dl_cache_libcmp (const char *p1, const char *p2)
364{
365 while (*p1 != '\0')
366 {
367 if (*p1 >= '0' && *p1 <= '9')
368 {
369 if (*p2 >= '0' && *p2 <= '9')
370 {
371 /* Must compare this numerically. */
372 int val1;
373 int val2;
374
375 val1 = *p1++ - '0';
376 val2 = *p2++ - '0';
377 while (*p1 >= '0' && *p1 <= '9')
378 val1 = val1 * 10 + *p1++ - '0';
379 while (*p2 >= '0' && *p2 <= '9')
380 val2 = val2 * 10 + *p2++ - '0';
381 if (val1 != val2)
382 return val1 - val2;
383 }
384 else
385 return 1;
386 }
387 else if (*p2 >= '0' && *p2 <= '9')
388 return -1;
389 else if (*p1 != *p2)
390 return *p1 - *p2;
391 else
392 {
393 ++p1;
394 ++p2;
395 }
396 }
397 return *p1 - *p2;
398}
399
400
401/* Look up NAME in ld.so.cache and return the file name stored there, or null
402 if none is found. The cache is loaded if it was not already. If loading
403 the cache previously failed there will be no more attempts to load it.
404 The caller is responsible for freeing the returned string. The ld.so.cache
405 may be unmapped at any time by a completing recursive dlopen and
406 this function must take care that it does not return references to
407 any data in the mapping. */
408char *
409_dl_load_cache_lookup (const char *name)
410{
411 /* Print a message if the loading of libs is traced. */
412 if (__glibc_unlikely (GLRO(dl_debug_mask) & DL_DEBUG_LIBS))
413 _dl_debug_printf (" search cache=%s\n", LD_SO_CACHE);
414
415 if (cache == NULL)
416 {
417 /* Read the contents of the file. */
418 void *file = _dl_sysdep_read_whole_file (LD_SO_CACHE, &cachesize,
419 PROT_READ);
420
421 /* We can handle three different cache file formats here:
422 - only the new format
423 - the old libc5/glibc2.0/2.1 format
424 - the old format with the new format in it
425 The following checks if the cache contains any of these formats. */
426 if (file != MAP_FAILED && cachesize > sizeof *cache_new
427 && memcmp (file, CACHEMAGIC_VERSION_NEW,
428 sizeof CACHEMAGIC_VERSION_NEW - 1) == 0
429 /* Check for corruption, avoiding overflow. */
430 && ((cachesize - sizeof *cache_new) / sizeof (struct file_entry_new)
431 >= ((struct cache_file_new *) file)->nlibs))
432 {
433 if (! cache_file_new_matches_endian (file))
434 {
435 __munmap (file, cachesize);
436 file = (void *) -1;
437 }
438 cache_new = file;
439 cache = file;
440 }
441 else if (file != MAP_FAILED && cachesize > sizeof *cache
442 && memcmp (file, CACHEMAGIC, sizeof CACHEMAGIC - 1) == 0
443 /* Check for corruption, avoiding overflow. */
444 && ((cachesize - sizeof *cache) / sizeof (struct file_entry)
445 >= ((struct cache_file *) file)->nlibs))
446 {
447 size_t offset;
448 /* Looks ok. */
449 cache = file;
450
451 /* Check for new version. */
452 offset = ALIGN_CACHE (sizeof (struct cache_file)
453 + cache->nlibs * sizeof (struct file_entry));
454
455 cache_new = (struct cache_file_new *) ((void *) cache + offset);
456 if (cachesize < (offset + sizeof (struct cache_file_new))
457 || memcmp (cache_new->magic, CACHEMAGIC_VERSION_NEW,
458 sizeof CACHEMAGIC_VERSION_NEW - 1) != 0)
459 cache_new = (void *) -1;
460 else
461 {
462 if (! cache_file_new_matches_endian (cache_new))
463 {
464 /* The old-format part of the cache is bogus as well
465 if the endianness does not match. (But it is
466 unclear how the new header can be located if the
467 endianess does not match.) */
468 cache = (void *) -1;
469 cache_new = (void *) -1;
470 __munmap (file, cachesize);
471 }
472 }
473 }
474 else
475 {
476 if (file != MAP_FAILED)
477 __munmap (file, cachesize);
478 cache = (void *) -1;
479 }
480
481 assert (cache != NULL);
482 }
483
484 if (cache == (void *) -1)
485 /* Previously looked for the cache file and didn't find it. */
486 return NULL;
487
488 const char *best;
489 if (cache_new != (void *) -1)
490 {
491 const char *string_table = (const char *) cache_new;
492 best = search_cache (string_table, cachesize,
493 &cache_new->libs[0].entry, cache_new->nlibs,
494 sizeof (cache_new->libs[0]), name);
495 }
496 else
497 {
498 const char *string_table = (const char *) &cache->libs[cache->nlibs];
499 uint32_t string_table_size
500 = (const char *) cache + cachesize - string_table;
501 best = search_cache (string_table, string_table_size,
502 &cache->libs[0], cache->nlibs,
503 sizeof (cache->libs[0]), name);
504 }
505
506 /* Print our result if wanted. */
507 if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_LIBS, 0)
508 && best != NULL)
509 _dl_debug_printf (" trying file=%s\n", best);
510
511 if (best == NULL)
512 return NULL;
513
514 /* The double copy is *required* since malloc may be interposed
515 and call dlopen itself whose completion would unmap the data
516 we are accessing. Therefore we must make the copy of the
517 mapping data without using malloc. */
518 char *temp;
519 temp = alloca (strlen (best) + 1);
520 strcpy (temp, best);
521 return __strdup (temp);
522}
523
524#ifndef MAP_COPY
525/* If the system does not support MAP_COPY we cannot leave the file open
526 all the time since this would create problems when the file is replaced.
527 Therefore we provide this function to close the file and open it again
528 once needed. */
529void
530_dl_unload_cache (void)
531{
532 if (cache != NULL && cache != (struct cache_file *) -1)
533 {
534 __munmap (cache, cachesize);
535 cache = NULL;
536 }
537#ifdef SHARED
538 /* This marks the glibc_hwcaps_priorities array as out-of-date. */
539 glibc_hwcaps_priorities_length = 0;
540#endif
541}
542#endif
543