1 /* $OpenBSD: library_subr.c,v 1.50 2019/10/04 17:42:16 guenther Exp $ */ 2 3 /* 4 * Copyright (c) 2002 Dale Rahn 5 * Copyright (c) 1998 Per Fogelstrom, Opsycon AB 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 * 28 */ 29 30 #define _DYN_LOADER 31 32 #include <sys/types.h> 33 #include <sys/queue.h> 34 #include <limits.h> 35 #include <dirent.h> 36 37 #include "archdep.h" 38 #include "resolve.h" 39 #include "dir.h" 40 #include "sod.h" 41 42 char * _dl_default_path[2] = { "/usr/lib", NULL }; 43 44 45 /* STATIC DATA */ 46 struct dlochld _dlopened_child_list; 47 48 49 /* 50 * _dl_match_file() 51 * 52 * This function determines if a given name matches what is specified 53 * in a struct sod. The major must match exactly, and the minor must 54 * be same or larger. 55 * 56 * sodp is updated with the minor if this matches. 57 */ 58 59 int 60 _dl_match_file(struct sod *sodp, const char *name, int namelen) 61 { 62 int match; 63 struct sod lsod; 64 const char *lname; 65 66 lname = name; 67 if (sodp->sod_library) { 68 if (_dl_strncmp(name, "lib", 3)) 69 return 0; 70 lname += 3; 71 } 72 if (_dl_strncmp(lname, (char *)sodp->sod_name, 73 _dl_strlen((char *)sodp->sod_name))) 74 return 0; 75 76 _dl_build_sod(name, &lsod); 77 78 match = 0; 79 if ((_dl_strcmp((char *)lsod.sod_name, (char *)sodp->sod_name) == 0) && 80 (lsod.sod_library == sodp->sod_library) && 81 ((sodp->sod_major == -1) || (sodp->sod_major == lsod.sod_major)) && 82 ((sodp->sod_minor == -1) || 83 (lsod.sod_minor >= sodp->sod_minor))) { 84 match = 1; 85 86 /* return version matched */ 87 sodp->sod_major = lsod.sod_major; 88 sodp->sod_minor = lsod.sod_minor; 89 } 90 _dl_free((char *)lsod.sod_name); 91 return match; 92 } 93 94 /* 95 * _dl_cmp_sod() 96 * 97 * This function compares sod structs. The major must match exactly, 98 * and the minor must be same or larger. 99 * 100 * sodp is updated with the minor if this matches. 101 */ 102 103 static int 104 _dl_cmp_sod(struct sod *sodp, const struct sod *lsod) 105 { 106 int match; 107 108 match = 1; 109 if ((_dl_strcmp((char *)lsod->sod_name, (char *)sodp->sod_name) == 0) && 110 (lsod->sod_library == sodp->sod_library) && 111 ((sodp->sod_major == -1) || (sodp->sod_major == lsod->sod_major)) && 112 ((sodp->sod_minor == -1) || 113 (lsod->sod_minor >= sodp->sod_minor))) { 114 match = 0; 115 116 /* return version matched */ 117 sodp->sod_major = lsod->sod_major; 118 sodp->sod_minor = lsod->sod_minor; 119 } 120 return match; 121 } 122 123 char _dl_hint_store[PATH_MAX]; 124 125 char * 126 _dl_find_shlib(struct sod *sodp, char **searchpath, int nohints) 127 { 128 char *hint, **pp; 129 struct dirent *dp; 130 int match, len; 131 _dl_DIR *dd; 132 struct sod tsod, bsod; /* transient and best sod */ 133 134 /* if we are to search default directories, and hints 135 * are not to be used, search the standard path from ldconfig 136 * (_dl_hint_search_path) or use the default path 137 */ 138 if (nohints) 139 goto nohints; 140 141 if (searchpath == NULL) { 142 /* search 'standard' locations, find any match in the hints */ 143 hint = _dl_findhint((char *)sodp->sod_name, sodp->sod_major, 144 sodp->sod_minor, NULL); 145 if (hint) 146 return hint; 147 } else { 148 /* search hints requesting matches for only 149 * the searchpath directories, 150 */ 151 for (pp = searchpath; *pp != NULL; pp++) { 152 hint = _dl_findhint((char *)sodp->sod_name, 153 sodp->sod_major, sodp->sod_minor, *pp); 154 if (hint != NULL) 155 return hint; 156 } 157 } 158 159 /* 160 * For each directory in the searchpath, read the directory 161 * entries looking for a match to sod. filename compare is 162 * done by _dl_match_file() 163 */ 164 nohints: 165 if (searchpath == NULL) { 166 if (_dl_hint_search_path != NULL) 167 searchpath = _dl_hint_search_path; 168 else 169 searchpath = _dl_default_path; 170 } 171 _dl_memset(&bsod, 0, sizeof(bsod)); 172 for (pp = searchpath; *pp != NULL; pp++) { 173 if ((dd = _dl_opendir(*pp)) != NULL) { 174 match = 0; 175 while ((dp = _dl_readdir(dd)) != NULL) { 176 tsod = *sodp; 177 if (_dl_match_file(&tsod, dp->d_name, 178 dp->d_namlen)) { 179 /* 180 * When a match is found, tsod is 181 * updated with the major+minor found. 182 * This version is compared with the 183 * largest so far (kept in bsod), 184 * and saved if larger. 185 */ 186 if (!match || 187 tsod.sod_major == -1 || 188 tsod.sod_major > bsod.sod_major || 189 ((tsod.sod_major == 190 bsod.sod_major) && 191 tsod.sod_minor > bsod.sod_minor)) { 192 bsod = tsod; 193 match = 1; 194 len = _dl_strlcpy( 195 _dl_hint_store, *pp, 196 PATH_MAX); 197 if (pp[0][len-1] != '/') { 198 _dl_hint_store[len] = 199 '/'; 200 len++; 201 } 202 _dl_strlcpy( 203 &_dl_hint_store[len], 204 dp->d_name, 205 PATH_MAX-len); 206 if (tsod.sod_major == -1) 207 break; 208 } 209 } 210 } 211 _dl_closedir(dd); 212 if (match) { 213 *sodp = bsod; 214 return (_dl_hint_store); 215 } 216 } 217 } 218 return NULL; 219 } 220 221 static elf_object_t * 222 _dl_lookup_object(const char *req_name, struct sod *req_sod) 223 { 224 elf_object_t *object = _dl_objects; 225 226 while (object) { 227 char *soname; 228 229 if (_dl_cmp_sod(req_sod, &object->sod) == 0) 230 return(object); 231 232 soname = (char *)object->Dyn.info[DT_SONAME]; 233 if (soname != NULL) { 234 if (_dl_strcmp(req_name, soname) == 0) 235 return(object); 236 } 237 238 object = object->next; 239 } 240 241 return(NULL); 242 } 243 244 static elf_object_t * 245 _dl_find_loaded_shlib(const char *req_name, struct sod req_sod, int flags) 246 { 247 elf_object_t *object; 248 249 object = _dl_lookup_object(req_name, &req_sod); 250 251 /* if not found retry with any minor */ 252 if (object == NULL && req_sod.sod_library && req_sod.sod_minor != -1) { 253 short orig_minor = req_sod.sod_minor; 254 req_sod.sod_minor = -1; 255 object = _dl_lookup_object(req_name, &req_sod); 256 257 if (object != NULL && req_sod.sod_minor < orig_minor) 258 _dl_printf("warning: lib%s.so.%d.%d: " 259 "minor version >= %d expected, " 260 "using it anyway\n", 261 req_sod.sod_name, req_sod.sod_major, 262 req_sod.sod_minor, orig_minor); 263 } 264 265 if (object) { /* Already loaded */ 266 object->obj_flags |= flags & DF_1_GLOBAL; 267 if (_dl_loading_object == NULL) 268 _dl_loading_object = object; 269 if (object->load_object != _dl_objects && 270 object->load_object != _dl_loading_object) { 271 _dl_link_grpref(object->load_object, _dl_loading_object); 272 } 273 } 274 275 return (object); 276 } 277 278 /* 279 * Load a shared object. Search order is: 280 * First check loaded objects for a matching shlib, otherwise: 281 * 282 * If the name contains a '/' use only the path preceding the 283 * library name and do not continue on to other methods if not 284 * found. 285 * search hints for match in path preceding library name 286 * this will only match specific library version. 287 * search path preceding library name 288 * this will find largest minor version in path provided 289 * 290 * Otherwise, the name doesn't contain a '/': 291 * search hints for the specific library version, trying in turn 292 * paths from the following: 293 * - the LD_LIBRARY_PATH environment variable (if set) 294 * - the library's own DT_RUNPATH 295 * - if DT_RUNPATH wasn't set, then: 296 * - the library's own DT_RPATH 297 * - the executable's own DT_RPATH 298 * - the default search path set by ldconfig, or /usr/lib if unset 299 * 300 * If the hints doesn't have an exact match, then we search 301 * that exact same list of directories again, looking for a 302 * lib with the correct major version. If we find a match on 303 * the major, then we take the match *in that directory* which 304 * has the largest minor version 305 */ 306 307 elf_object_t * 308 _dl_load_shlib(const char *libname, elf_object_t *parent, int type, int flags) 309 { 310 int try_any_minor, ignore_hints; 311 struct sod sod, req_sod; 312 elf_object_t *object = NULL; 313 char *hint; 314 315 try_any_minor = 0; 316 ignore_hints = 0; 317 318 if (_dl_strchr(libname, '/')) { 319 char *paths[2]; 320 char *lpath, *lname; 321 lpath = _dl_strdup(libname); 322 if (lpath == NULL) 323 _dl_oom(); 324 lname = _dl_strrchr(lpath, '/'); 325 if (lname == NULL) { 326 _dl_free(lpath); 327 _dl_errno = DL_NOT_FOUND; 328 return (object); 329 } 330 *lname = '\0'; 331 lname++; 332 if (*lname == '\0') { 333 _dl_free(lpath); 334 _dl_errno = DL_NOT_FOUND; 335 return (object); 336 } 337 338 _dl_build_sod(lname, &sod); 339 req_sod = sod; 340 341 paths[0] = lpath; 342 paths[1] = NULL; 343 fullpathagain: 344 hint = _dl_find_shlib(&req_sod, paths, ignore_hints); 345 if (hint != NULL) 346 goto fullpathdone; 347 348 if (try_any_minor == 0) { 349 try_any_minor = 1; 350 ignore_hints = 1; 351 req_sod.sod_minor = -1; 352 goto fullpathagain; 353 } 354 _dl_errno = DL_NOT_FOUND; 355 fullpathdone: 356 _dl_free(lpath); 357 goto done; 358 } 359 360 _dl_build_sod(libname, &sod); 361 req_sod = sod; 362 363 object = _dl_find_loaded_shlib(libname, req_sod, flags); 364 if (object) { 365 _dl_free((char *)sod.sod_name); 366 return (object); 367 } 368 369 again: 370 /* No '/' in name. Scan the known places, LD_LIBRARY_PATH first. */ 371 if (_dl_libpath != NULL) { 372 hint = _dl_find_shlib(&req_sod, _dl_libpath, ignore_hints); 373 if (hint != NULL) 374 goto done; 375 } 376 377 /* Check DT_RUNPATH */ 378 if (parent->runpath != NULL) { 379 hint = _dl_find_shlib(&req_sod, parent->runpath, ignore_hints); 380 if (hint != NULL) 381 goto done; 382 } else { 383 /* 384 * If DT_RUNPATH wasn't set then first check DT_RPATH, 385 * followed by the main program's DT_RPATH. 386 */ 387 if (parent->rpath != NULL) { 388 hint = _dl_find_shlib(&req_sod, parent->rpath, 389 ignore_hints); 390 if (hint != NULL) 391 goto done; 392 } 393 if (parent != _dl_objects && _dl_objects->rpath != NULL) { 394 hint = _dl_find_shlib(&req_sod, _dl_objects->rpath, 395 ignore_hints); 396 if (hint != NULL) 397 goto done; 398 } 399 } 400 401 /* check 'standard' locations */ 402 hint = _dl_find_shlib(&req_sod, NULL, ignore_hints); 403 if (hint != NULL) 404 goto done; 405 406 if (try_any_minor == 0) { 407 try_any_minor = 1; 408 ignore_hints = 1; 409 req_sod.sod_minor = -1; 410 goto again; 411 } 412 _dl_errno = DL_NOT_FOUND; 413 done: 414 if (hint != NULL) { 415 if (req_sod.sod_minor < sod.sod_minor) 416 _dl_printf("warning: lib%s.so.%d.%d: " 417 "minor version >= %d expected, " 418 "using it anyway\n", 419 sod.sod_name, sod.sod_major, 420 req_sod.sod_minor, sod.sod_minor); 421 object = _dl_tryload_shlib(hint, type, flags); 422 } 423 _dl_free((char *)sod.sod_name); 424 return(object); 425 } 426 427 428 void 429 _dl_link_dlopen(elf_object_t *dep) 430 { 431 struct dep_node *n; 432 433 dep->opencount++; 434 435 if (OBJECT_DLREF_CNT(dep) > 1) 436 return; 437 438 n = _dl_malloc(sizeof *n); 439 if (n == NULL) 440 _dl_oom(); 441 442 n->data = dep; 443 TAILQ_INSERT_TAIL(&_dlopened_child_list, n, next_sib); 444 445 DL_DEB(("linking %s as dlopen()ed\n", dep->load_name)); 446 } 447 448 static void 449 _dl_child_refcnt_decrement(elf_object_t *object) 450 { 451 struct object_vector vec; 452 int i; 453 454 object->refcount--; 455 if (OBJECT_REF_CNT(object) == 0) 456 for (vec = object->child_vec, i = 0; i < vec.len; i++) 457 _dl_child_refcnt_decrement(vec.vec[i]); 458 } 459 460 void 461 _dl_notify_unload_shlib(elf_object_t *object) 462 { 463 struct object_vector vec; 464 struct dep_node *n; 465 int i; 466 467 if (OBJECT_REF_CNT(object) == 0) 468 for (vec = object->child_vec, i = 0; i < vec.len; i++) 469 _dl_child_refcnt_decrement(vec.vec[i]); 470 471 if (OBJECT_DLREF_CNT(object) == 0) { 472 while ((n = TAILQ_FIRST(&object->grpref_list)) != NULL) { 473 TAILQ_REMOVE(&object->grpref_list, n, next_sib); 474 n->data->grprefcount--; 475 _dl_notify_unload_shlib(n->data); 476 _dl_free(n); 477 } 478 } 479 } 480 481 void 482 _dl_unload_dlopen(void) 483 { 484 struct dep_node *node; 485 486 TAILQ_FOREACH_REVERSE(node, &_dlopened_child_list, dlochld, next_sib) { 487 /* dont dlclose the main program */ 488 if (node->data == _dl_objects) 489 continue; 490 491 while (node->data->opencount > 0) { 492 node->data->opencount--; 493 _dl_notify_unload_shlib(node->data); 494 _dl_run_all_dtors(); 495 } 496 } 497 } 498 499 void 500 _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object) 501 { 502 struct dep_node *n; 503 504 n = _dl_malloc(sizeof *n); 505 if (n == NULL) 506 _dl_oom(); 507 n->data = load_group; 508 TAILQ_INSERT_TAIL(&load_object->grpref_list, n, next_sib); 509 load_group->grprefcount++; 510 } 511 512 void 513 _dl_link_child(elf_object_t *dep, elf_object_t *p) 514 { 515 int i; 516 517 i = p->child_vec.len++; 518 if (i == p->child_vec.alloc) 519 _dl_die("child appeared %d > %d", p->child_vec.len, 520 p->child_vec.alloc); 521 p->child_vec.vec[i] = dep; 522 523 dep->refcount++; 524 525 DL_DEB(("linking dep %s as child of %s\n", dep->load_name, 526 p->load_name)); 527 } 528 529 void 530 object_vec_grow(struct object_vector *vec, int more) 531 { 532 vec->alloc += more; 533 vec->vec = _dl_reallocarray(vec->vec, vec->alloc, sizeof(*vec->vec)); 534 if (vec->vec == NULL) 535 _dl_oom(); 536 } 537 538 /* Generation number of the current grpsym insertion/caching */ 539 static unsigned int _dl_grpsym_gen = 0; 540 541 void 542 _dl_link_grpsym(elf_object_t *object) 543 { 544 struct object_vector *vec; 545 int len; 546 547 if (object->grpsym_gen == _dl_grpsym_gen) 548 return; 549 object->grpsym_gen = _dl_grpsym_gen; 550 551 vec = &_dl_loading_object->grpsym_vec; 552 len = vec->len++; 553 if (len >= vec->alloc) 554 _dl_die("more grpsym than objects?! %d > %d", vec->len, 555 vec->alloc); 556 vec->vec[len] = object; 557 } 558 559 void 560 _dl_cache_grpsym_list_setup(elf_object_t *object) 561 { 562 struct object_vector *vec; 563 int next; 564 565 _dl_grpsym_gen += 1; 566 567 if (_dl_grpsym_gen == 0) { 568 /* 569 * If the count rolls over, reset all counters so 570 * we don't get accidental collision. 571 */ 572 elf_object_t *walkobj; 573 for (walkobj = _dl_objects; 574 walkobj != NULL; 575 walkobj = walkobj->next) { 576 walkobj->grpsym_gen = 0; 577 } 578 _dl_grpsym_gen = 1; 579 } 580 581 /* 582 * grpsym_vec is a vector of all child libs of the 583 * _dl_loading_object with no dups. The order is equivalent 584 * to a breadth-first traversal of the child list without dups. 585 */ 586 587 vec = &object->grpsym_vec; 588 object_vec_grow(vec, object_count); 589 next = 0; 590 591 /* add first object manually */ 592 _dl_link_grpsym(object); 593 594 while (next < vec->len) { 595 struct object_vector child_vec; 596 int i; 597 598 child_vec = vec->vec[next++]->child_vec; 599 for (i = 0; i < child_vec.len; i++) 600 _dl_link_grpsym(child_vec.vec[i]); 601 } 602 } 603