sod.c revision 1.27
1/*	$OpenBSD: sod.c,v 1.27 2013/12/03 01:47:05 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/mman.h>
36#include <machine/exec.h>
37#include <limits.h>
38#include <stdio.h>
39#include <fcntl.h>
40#include <nlist.h>
41#include <link.h>
42#include <limits.h>
43#include <string.h>
44#include <stdlib.h>
45#include <unistd.h>
46
47#include "syscall.h"
48#include "archdep.h"
49#include "path.h"
50#include "util.h"
51#include "sod.h"
52
53int _dl_hinthash(char *cp, int vmajor, int vminor);
54void _dl_maphints(void);
55
56/*
57 * Populate sod struct for dlopen's call to map_object
58 */
59void
60_dl_build_sod(const char *name, struct sod *sodp)
61{
62	unsigned int	tuplet;
63	int		major, minor;
64	char		*realname, *tok, *etok, *cp;
65
66	/* default is an absolute or relative path */
67	sodp->sod_name = (long)_dl_strdup(name);    /* strtok is destructive */
68	sodp->sod_library = 0;
69	sodp->sod_major = sodp->sod_minor = 0;
70
71	/* does it look like /^lib/ ? */
72	if (_dl_strncmp((char *)sodp->sod_name, "lib", 3) != 0)
73		goto backout;
74
75	/* is this a filename? */
76	if (_dl_strchr((char *)sodp->sod_name, '/'))
77		goto backout;
78
79	/* skip over 'lib' */
80	cp = (char *)sodp->sod_name + 3;
81
82	realname = cp;
83
84	/* dot guardian */
85	if ((_dl_strchr(cp, '.') == NULL) || (*(cp+_dl_strlen(cp)-1) == '.'))
86		goto backout;
87
88	cp = _dl_strstr(cp, ".so");
89	if (cp == NULL)
90		goto backout;
91
92	/* default */
93	major = minor = -1;
94
95	/* loop through name - parse skipping name */
96	for (tuplet = 0; (tok = strsep(&cp, ".")) != NULL; tuplet++) {
97		switch (tuplet) {
98		case 0:
99			/* empty tok, we already skipped to "\.so.*" */
100			break;
101		case 1:
102			/* 'so' extension */
103			break;
104		case 2:
105			/* major version extension */
106			major = _dl_strtol(tok, &etok, 10);
107			if (*tok == '\0' || *etok != '\0')
108				goto backout;
109			break;
110		case 3:
111			/* minor version extension */
112			minor = _dl_strtol(tok, &etok, 10);
113			if (*tok == '\0' || *etok != '\0')
114				goto backout;
115			break;
116		/* if we get here, it must be weird */
117		default:
118			goto backout;
119		}
120	}
121	if (realname == NULL)
122		goto backout;
123	cp = (char *)sodp->sod_name;
124	sodp->sod_name = (long)_dl_strdup(realname);
125	_dl_free(cp);
126	sodp->sod_library = 1;
127	sodp->sod_major = major;
128	sodp->sod_minor = minor;
129	return;
130
131backout:
132	_dl_free((char *)sodp->sod_name);
133	sodp->sod_name = (long)_dl_strdup(name);
134}
135
136void
137_dl_set_sod(const char *path, struct sod *sod)
138{
139	char *fname = _dl_strrchr(path, '/');
140
141	if (fname != NULL)
142		_dl_build_sod(++fname, sod);
143	else
144		_dl_build_sod(path, sod);
145}
146
147static struct hints_header	*hheader = NULL;
148static struct hints_bucket	*hbuckets;
149static char			*hstrtab;
150char				**_dl_hint_search_path = NULL;
151
152#define HINTS_VALID (hheader != NULL && hheader != (struct hints_header *)-1)
153
154void
155_dl_maphints(void)
156{
157	struct stat	sb;
158	caddr_t		addr = MAP_FAILED;
159	long		hsize = 0;
160	int		hfd;
161
162	if ((hfd = _dl_open(_PATH_LD_HINTS, O_RDONLY)) < 0)
163		goto bad_hints;
164
165	if (_dl_fstat(hfd, &sb) != 0 || !S_ISREG(sb.st_mode) ||
166	    sb.st_size < sizeof(struct hints_header) || sb.st_size > LONG_MAX)
167		goto bad_hints;
168
169	hsize = (long)sb.st_size;
170	addr = (void *)_dl_mmap(0, hsize, PROT_READ, MAP_PRIVATE, hfd, 0);
171	if (_dl_mmap_error(addr))
172		goto bad_hints;
173
174	hheader = (struct hints_header *)addr;
175	if (HH_BADMAG(*hheader) || hheader->hh_ehints > hsize)
176		goto bad_hints;
177
178	if (hheader->hh_version != LD_HINTS_VERSION_1 &&
179	    hheader->hh_version != LD_HINTS_VERSION_2)
180		goto bad_hints;
181
182	hbuckets = (struct hints_bucket *)(addr + hheader->hh_hashtab);
183	hstrtab = (char *)(addr + hheader->hh_strtab);
184	if (hheader->hh_version >= LD_HINTS_VERSION_2)
185		_dl_hint_search_path = _dl_split_path(hstrtab + hheader->hh_dirlist);
186
187	/* close the file descriptor, leaving the hints mapped */
188	_dl_close(hfd);
189
190	return;
191
192bad_hints:
193	if (!_dl_mmap_error(addr))
194		_dl_munmap(addr, hsize);
195	if (hfd != -1)
196		_dl_close(hfd);
197	hheader = (struct hints_header *)-1;
198}
199
200char *
201_dl_findhint(char *name, int major, int minor, char *preferred_path)
202{
203	struct hints_bucket	*bp;
204
205	/*
206	 * If not mapped, and we have not tried before, try to map the
207	 * hints, if previous attempts failed hheader is -1 and we
208	 * do not wish to retry it.
209	 */
210	if (hheader == NULL)
211		_dl_maphints();
212
213	/* if it failed to map, return failure */
214	if (!(HINTS_VALID))
215		return NULL;
216
217	if (hheader->hh_nbucket == 0)
218		return NULL;
219
220	bp = hbuckets + (_dl_hinthash(name, major, minor) % hheader->hh_nbucket);
221
222	while (1) {
223		/* Sanity check */
224		if (bp->hi_namex >= hheader->hh_strtab_sz) {
225			_dl_printf("Bad name index: %#x\n", bp->hi_namex);
226			_dl_exit(7);
227			break;
228		}
229		if (bp->hi_pathx >= hheader->hh_strtab_sz) {
230			_dl_printf("Bad path index: %#x\n", bp->hi_pathx);
231			_dl_exit(7);
232			break;
233		}
234
235		if (_dl_strcmp(name, hstrtab + bp->hi_namex) == 0) {
236			/* It's `name', check version numbers */
237			if (bp->hi_major == major &&
238			    (bp->hi_ndewey < 2 || bp->hi_minor >= minor)) {
239				if (preferred_path == NULL) {
240					return hstrtab + bp->hi_pathx;
241				} else {
242					char *path = hstrtab + bp->hi_pathx;
243					char *edir = _dl_strrchr(path, '/');
244
245					if ((_dl_strncmp(preferred_path, path,
246					    (edir - path)) == 0) &&
247					    (preferred_path[edir - path] == '\0'))
248						return path;
249				}
250			}
251		}
252
253		if (bp->hi_next == -1)
254			break;
255
256		/* Move on to next in bucket */
257		bp = &hbuckets[bp->hi_next];
258	}
259
260	/* No hints available for name */
261	return NULL;
262}
263
264int
265_dl_hinthash(char *cp, int vmajor, int vminor)
266{
267	int	k = 0;
268
269	while (*cp)
270		k = (((k << 1) + (k >> 14)) ^ (*cp++)) & 0x3fff;
271
272	k = (((k << 1) + (k >> 14)) ^ (vmajor*257)) & 0x3fff;
273	if (hheader->hh_version == LD_HINTS_VERSION_1)
274		k = (((k << 1) + (k >> 14)) ^ (vminor*167)) & 0x3fff;
275
276	return k;
277}
278