sod.c revision 1.18
1/*	$OpenBSD: sod.c,v 1.18 2003/07/06 20:03:57 deraadt Exp $	*/
2
3/*
4 * Copyright (c) 1993 Paul Kranenburg
5 * All rights reserved.
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 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by Paul Kranenburg.
18 * 4. The name of the author may not be used to endorse or promote products
19 *    derived from this software without specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <sys/types.h>
35#include <sys/syslimits.h>
36#include <stdio.h>
37#include <fcntl.h>
38#include <nlist.h>
39#include <link.h>
40#include <limits.h>
41#include <machine/exec.h>
42#include <sys/mman.h>
43#include <string.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <syscall.h>
47
48#include "archdep.h"
49#include "util.h"
50#include "sod.h"
51
52int _dl_hinthash(char *cp, int vmajor, int vminor);
53void _dl_maphints(void);
54
55/*
56 * Populate sod struct for dlopen's call to map_object
57 */
58void
59_dl_build_sod(const char *name, struct sod *sodp)
60{
61	unsigned int	tuplet;
62	int		major, minor;
63	char		*realname, *tok, *etok, *cp;
64
65	/* default is an absolute or relative path */
66	sodp->sod_name = (long)_dl_strdup(name);    /* strtok is destructive */
67	sodp->sod_library = 0;
68	sodp->sod_major = sodp->sod_minor = 0;
69
70	/* does it look like /^lib/ ? */
71	if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0)
72		return;
73
74	/* is this a filename? */
75	if (_dl_strchr((char *)sodp->sod_name, '/'))
76		return;
77
78	/* skip over 'lib' */
79	cp = (char *)sodp->sod_name + 3;
80
81	/* dot guardian */
82	if ((_dl_strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.'))
83		return;
84
85	/* default */
86	major = minor = -1;
87
88	realname = NULL;
89	/* loop through name - parse skipping name */
90	for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) {
91		switch (tuplet) {
92		case 0:
93			/* removed 'lib' and extensions from name */
94			realname = tok;
95			break;
96		case 1:
97			/* 'so' extension */
98			if (_dl_strcmp(tok, "so") != 0)
99				goto backout;
100			break;
101		case 2:
102			/* major version extension */
103			major = _dl_strtol(tok, &etok, 10);
104			if (*tok == '\0' || *etok != '\0')
105				goto backout;
106			break;
107		case 3:
108			/* minor version extension */
109			minor = _dl_strtol(tok, &etok, 10);
110			if (*tok == '\0' || *etok != '\0')
111				goto backout;
112			break;
113		/* if we get here, it must be weird */
114		default:
115			goto backout;
116		}
117	}
118	if (realname == NULL)
119		goto backout;
120	cp = (char *)sodp->sod_name;
121	sodp->sod_name = (long)_dl_strdup(realname);
122	_dl_free(cp);
123	sodp->sod_library = 1;
124	sodp->sod_major = major;
125	sodp->sod_minor = minor;
126	return;
127
128backout:
129	_dl_free((char *)sodp->sod_name);
130	sodp->sod_name = (long)_dl_strdup(name);
131}
132
133static struct hints_header	*hheader = NULL;
134static struct hints_bucket	*hbuckets;
135static char			*hstrtab;
136char				*_dl_hint_search_path = NULL;
137
138#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
139
140void
141_dl_maphints(void)
142{
143	struct stat	sb;
144	caddr_t		addr = MAP_FAILED;
145	long		hsize = 0;
146	int		hfd;
147
148	if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY)) < 0)
149		goto bad_hints;
150
151	if (_dl_fstat(hfd, &sb) != 0 || !S_ISREG(sb.st_mode) ||
152	    sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX)
153		goto bad_hints;
154
155	hsize = (long)sb.st_size;
156	addr = (void *)_dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0);
157	if (addr == MAP_FAILED)
158		goto bad_hints;
159
160	hheader = (struct hints_header *)addr;
161	if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize)
162		goto bad_hints;
163
164	if (hheader->hh_version != LD_HINTS_VERSION_1 &&
165	    hheader->hh_version != LD_HINTS_VERSION_2)
166		goto bad_hints;
167
168	hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
169	hstrtab = (char *)(addr + hheader->hh_strtab);
170	if (hheader->hh_version >= LD_HINTS_VERSION_2)
171		_dl_hint_search_path = hstrtab + hheader->hh_dirlist;
172
173	/* close the file descriptor, leaving the hints mapped */
174	_dl_close(hfd);
175
176	return;
177
178bad_hints:
179	if (addr != MAP_FAILED)
180		_dl_munmap(addr, hsize);
181	if (hfd != -1)
182		_dl_close(hfd);
183	hheader = (struct hints_header *)-1;
184}
185
186char *
187_dl_findhint(char *name, int major, int minor, char *prefered_path)
188{
189	struct hints_bucket	*bp;
190
191	/*
192	 * If not mapped, and we have not tried before, try to map the
193	 * hints, if previous attempts failed hheader is -1 and we
194	 * do not wish to retry it.
195	 */
196	if (hheader == NULL)
197		_dl_maphints();
198
199	/* if it failed to map, return failure */
200	if (!(HINTS_VALID))
201		return NULL;
202
203	bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket);
204
205	while (1) {
206		/* Sanity check */
207		if (bp->hi_namex >= hheader->hh_strtab_sz) {
208			_dl_printf("Bad name index: %#x\n", bp->hi_namex);
209			_dl_exit(7);
210			break;
211		}
212		if (bp->hi_pathx >= hheader->hh_strtab_sz) {
213			_dl_printf("Bad path index: %#x\n", bp->hi_pathx);
214			_dl_exit(7);
215			break;
216		}
217
218		if (_dl_strcmp(name, hstrtab + bp->hi_namex) == 0) {
219			/* It's `name', check version numbers */
220			if (bp->hi_major == major &&
221			    (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) {
222				if (prefered_path == NULL ||
223				    _dl_strncmp(prefered_path,
224				    hstrtab + bp->hi_pathx,
225				    _dl_strlen(prefered_path)) == 0)
226					return hstrtab + bp->hi_pathx;
227			}
228		}
229
230		if (bp->hi_next == -1)
231			break;
232
233		/* Move on to next in bucket */
234		bp = &hbuckets[bp->hi_next];
235	}
236
237	/* No hints available for name */
238	return NULL;
239}
240
241int
242_dl_hinthash(char *cp, int vmajor, int vminor)
243{
244	int	k = 0;
245
246	while (*cp)
247		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
248
249	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
250	if (hheader->hh_version == LD_HINTS_VERSION_1)
251		k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
252
253	return k;
254}
255