xref: /openbsd-src/libexec/ld.so/library_subr.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: library_subr.c,v 1.45 2016/01/24 03:54:34 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  *	try the LD_LIBRARY_PATH specification (if present)
290  *	   search hints for match in LD_LIBRARY_PATH dirs
291  *           this will only match specific library version.
292  *	   search LD_LIBRARY_PATH dirs for match.
293  *           this will find largest minor version in first dir found.
294  *	check DT_RPATH paths, (if present)
295  *	   search hints for match in DT_RPATH dirs
296  *           this will only match specific library version.
297  *	   search DT_RPATH dirs for match.
298  *           this will find largest minor version in first dir found.
299  *	last look in default search directory, either as specified
300  *      by ldconfig or default to '/usr/lib'
301  */
302 
303 
304 elf_object_t *
305 _dl_load_shlib(const char *libname, elf_object_t *parent, int type, int flags)
306 {
307 	int try_any_minor, ignore_hints;
308 	struct sod sod, req_sod;
309 	elf_object_t *object = NULL;
310 	char *hint;
311 
312 	try_any_minor = 0;
313 	ignore_hints = 0;
314 
315 	if (_dl_strchr(libname, '/')) {
316 		char *paths[2];
317 		char *lpath, *lname;
318 		lpath = _dl_strdup(libname);
319 		if (lpath == NULL)
320 			_dl_exit(5);
321 		lname = _dl_strrchr(lpath, '/');
322 		if (lname == NULL) {
323 			_dl_free(lpath);
324 			_dl_errno = DL_NOT_FOUND;
325 			return (object);
326 		}
327 		*lname = '\0';
328 		lname++;
329 		if (*lname  == '\0') {
330 			_dl_free(lpath);
331 			_dl_errno = DL_NOT_FOUND;
332 			return (object);
333 		}
334 
335 		_dl_build_sod(lname, &sod);
336 		req_sod = sod;
337 
338 		paths[0] = lpath;
339 		paths[1] = NULL;
340 fullpathagain:
341 		hint = _dl_find_shlib(&req_sod, paths, ignore_hints);
342 		if (hint != NULL)
343 			goto fullpathdone;
344 
345 		if (try_any_minor == 0) {
346 			try_any_minor = 1;
347 			ignore_hints = 1;
348 			req_sod.sod_minor = -1;
349 			goto fullpathagain;
350 		}
351 		_dl_errno = DL_NOT_FOUND;
352 fullpathdone:
353 		_dl_free(lpath);
354 		goto done;
355 	}
356 
357 	_dl_build_sod(libname, &sod);
358 	req_sod = sod;
359 
360 	object = _dl_find_loaded_shlib(libname, req_sod, flags);
361 	if (object) {
362 		_dl_free((char *)sod.sod_name);
363 		return (object);
364 	}
365 
366 again:
367 	/* No '/' in name. Scan the known places, LD_LIBRARY_PATH first.  */
368 	if (_dl_libpath != NULL) {
369 		hint = _dl_find_shlib(&req_sod, _dl_libpath, ignore_hints);
370 		if (hint != NULL)
371 			goto done;
372 	}
373 
374 	/* Check DT_RPATH.  */
375 	if (parent->rpath != NULL) {
376 		hint = _dl_find_shlib(&req_sod, parent->rpath, ignore_hints);
377 		if (hint != NULL)
378 			goto done;
379 	}
380 
381 	/* Check main program's DT_RPATH, if parent != main program */
382 	if (parent != _dl_objects && _dl_objects->rpath != NULL) {
383 		hint = _dl_find_shlib(&req_sod, _dl_objects->rpath, ignore_hints);
384 		if (hint != NULL)
385 			goto done;
386 	}
387 
388 	/* check 'standard' locations */
389 	hint = _dl_find_shlib(&req_sod, NULL, ignore_hints);
390 	if (hint != NULL)
391 		goto done;
392 
393 	if (try_any_minor == 0) {
394 		try_any_minor = 1;
395 		ignore_hints = 1;
396 		req_sod.sod_minor = -1;
397 		goto again;
398 	}
399 	_dl_errno = DL_NOT_FOUND;
400 done:
401 	if (hint != NULL) {
402 		if (req_sod.sod_minor < sod.sod_minor)
403 			_dl_printf("warning: lib%s.so.%d.%d: "
404 			    "minor version >= %d expected, "
405 			    "using it anyway\n",
406 			    sod.sod_name, sod.sod_major,
407 			    req_sod.sod_minor, sod.sod_minor);
408 		object = _dl_tryload_shlib(hint, type, flags);
409 	}
410 	_dl_free((char *)sod.sod_name);
411 	return(object);
412 }
413 
414 
415 void
416 _dl_link_dlopen(elf_object_t *dep)
417 {
418 	struct dep_node *n;
419 
420 	dep->opencount++;
421 
422 	if (OBJECT_DLREF_CNT(dep) > 1)
423 		return;
424 
425 	n = _dl_malloc(sizeof *n);
426 	if (n == NULL)
427 		_dl_exit(5);
428 
429 	n->data = dep;
430 	TAILQ_INSERT_TAIL(&_dlopened_child_list, n, next_sib);
431 
432 	DL_DEB(("linking %s as dlopen()ed\n", dep->load_name));
433 }
434 
435 static void
436 _dl_child_refcnt_decrement(elf_object_t *object)
437 {
438 	struct dep_node *n;
439 
440 	object->refcount--;
441 	if (OBJECT_REF_CNT(object) == 0)
442 		TAILQ_FOREACH(n, &object->child_list, next_sib)
443 			_dl_child_refcnt_decrement(n->data);
444 }
445 
446 void
447 _dl_notify_unload_shlib(elf_object_t *object)
448 {
449 	struct dep_node *n;
450 
451 	if (OBJECT_REF_CNT(object) == 0)
452 		TAILQ_FOREACH(n, &object->child_list, next_sib)
453 			_dl_child_refcnt_decrement(n->data);
454 
455 	if (OBJECT_DLREF_CNT(object) == 0) {
456 		while ((n = TAILQ_FIRST(&object->grpref_list)) != NULL) {
457 			TAILQ_REMOVE(&object->grpref_list, n, next_sib);
458 			n->data->grprefcount--;
459 			_dl_notify_unload_shlib(n->data);
460 			_dl_free(n);
461 		}
462 	}
463 }
464 
465 void
466 _dl_unload_dlopen(void)
467 {
468 	struct dep_node *node;
469 
470 	TAILQ_FOREACH_REVERSE(node, &_dlopened_child_list, dlochld, next_sib) {
471 		/* dont dlclose the main program */
472 		if (node->data == _dl_objects)
473 			continue;
474 
475 		while (node->data->opencount > 0) {
476 			node->data->opencount--;
477 			_dl_notify_unload_shlib(node->data);
478 			_dl_run_all_dtors();
479 		}
480 	}
481 }
482 
483 void
484 _dl_link_grpref(elf_object_t *load_group, elf_object_t *load_object)
485 {
486 	struct dep_node *n;
487 
488 	n = _dl_malloc(sizeof *n);
489 	if (n == NULL)
490 		_dl_exit(7);
491 	n->data = load_group;
492 	TAILQ_INSERT_TAIL(&load_object->grpref_list, n, next_sib);
493 	load_group->grprefcount++;
494 }
495 
496 void
497 _dl_link_child(elf_object_t *dep, elf_object_t *p)
498 {
499 	struct dep_node *n;
500 
501 	n = _dl_malloc(sizeof *n);
502 	if (n == NULL)
503 		_dl_exit(7);
504 	n->data = dep;
505 	TAILQ_INSERT_TAIL(&p->child_list, n, next_sib);
506 
507 	dep->refcount++;
508 
509 	DL_DEB(("linking dep %s as child of %s\n", dep->load_name,
510 	    p->load_name));
511 }
512 
513 /* Generation number of the current grpsym insertion/caching */
514 static unsigned int _dl_grpsym_gen = 0;
515 
516 void
517 _dl_link_grpsym(elf_object_t *object, int checklist)
518 {
519 	struct dep_node *n;
520 
521 	if (checklist) {
522 		TAILQ_FOREACH(n, &_dl_loading_object->grpsym_list, next_sib)
523 			if (n->data == object)
524 				return; /* found, dont bother adding */
525 	} else {
526 		if (object->grpsym_gen == _dl_grpsym_gen) {
527 			return; /* found, dont bother adding */
528 		}
529 	}
530 	object->grpsym_gen = _dl_grpsym_gen;
531 
532 	n = _dl_malloc(sizeof *n);
533 	if (n == NULL)
534 		_dl_exit(8);
535 	n->data = object;
536 	TAILQ_INSERT_TAIL(&_dl_loading_object->grpsym_list, n, next_sib);
537 }
538 
539 void
540 _dl_cache_grpsym_list_setup(elf_object_t *object)
541 {
542 	_dl_grpsym_gen += 1;
543 
544 	if (_dl_grpsym_gen == 0) {
545 		/*
546 		 * If the count rolls over, reset all counters so
547 		 * we don't get accidental collision.
548 		 */
549 		elf_object_t *walkobj;
550 		for (walkobj = _dl_objects;
551 		    walkobj != NULL;
552 		    walkobj = walkobj->next) {
553 			walkobj->grpsym_gen = 0;
554 		}
555 		_dl_grpsym_gen = 1;
556 	}
557 	_dl_cache_grpsym_list(object);
558 }
559 void
560 _dl_cache_grpsym_list(elf_object_t *object)
561 {
562 	struct dep_node *n;
563 
564 	/*
565 	 * grpsym_list is an ordered list of all child libs of the
566 	 * _dl_loading_object with no dups. The order is equivalent
567 	 * to a breadth-first traversal of the child list without dups.
568 	 */
569 
570 	TAILQ_FOREACH(n, &object->child_list, next_sib)
571 		_dl_link_grpsym(n->data, 0);
572 
573 	TAILQ_FOREACH(n, &object->child_list, next_sib)
574 		_dl_cache_grpsym_list(n->data);
575 }
576