xref: /freebsd/libexec/rtld-elf/libmap.c (revision f05cddf9)
1 /*
2  * $FreeBSD$
3  */
4 
5 #include <sys/types.h>
6 #include <sys/param.h>
7 #include <sys/fcntl.h>
8 #include <sys/mman.h>
9 #include <sys/queue.h>
10 #include <sys/stat.h>
11 #include <dirent.h>
12 #include <errno.h>
13 #include <stdlib.h>
14 #include <string.h>
15 
16 #include "debug.h"
17 #include "rtld.h"
18 #include "libmap.h"
19 
20 #ifndef _PATH_LIBMAP_CONF
21 #define	_PATH_LIBMAP_CONF	"/etc/libmap.conf"
22 #endif
23 
24 #ifdef COMPAT_32BIT
25 #undef _PATH_LIBMAP_CONF
26 #define	_PATH_LIBMAP_CONF	"/etc/libmap32.conf"
27 #endif
28 
29 TAILQ_HEAD(lm_list, lm);
30 struct lm {
31 	char *f;
32 	char *t;
33 	TAILQ_ENTRY(lm)	lm_link;
34 };
35 
36 TAILQ_HEAD(lmp_list, lmp) lmp_head = TAILQ_HEAD_INITIALIZER(lmp_head);
37 struct lmp {
38 	char *p;
39 	enum { T_EXACT=0, T_BASENAME, T_DIRECTORY } type;
40 	struct lm_list lml;
41 	TAILQ_ENTRY(lmp) lmp_link;
42 };
43 
44 static TAILQ_HEAD(lmc_list, lmc) lmc_head = TAILQ_HEAD_INITIALIZER(lmc_head);
45 struct lmc {
46 	char *path;
47 	TAILQ_ENTRY(lmc) next;
48 };
49 
50 static int lm_count;
51 
52 static void lmc_parse(char *, size_t);
53 static void lmc_parse_file(char *);
54 static void lmc_parse_dir(char *);
55 static void lm_add(const char *, const char *, const char *);
56 static void lm_free(struct lm_list *);
57 static char *lml_find(struct lm_list *, const char *);
58 static struct lm_list *lmp_find(const char *);
59 static struct lm_list *lmp_init(char *);
60 static const char *quickbasename(const char *);
61 
62 #define	iseol(c)	(((c) == '#') || ((c) == '\0') || \
63 			 ((c) == '\n') || ((c) == '\r'))
64 
65 /*
66  * Do not use ctype.h macros, which rely on working TLS.  It is
67  * too early to have thread-local variables functional.
68  */
69 #define	rtld_isspace(c)	((c) == ' ' || (c) == '\t')
70 
71 int
72 lm_init(char *libmap_override)
73 {
74 	char *p;
75 
76 	dbg("lm_init(\"%s\")", libmap_override);
77 	TAILQ_INIT(&lmp_head);
78 
79 	lmc_parse_file(_PATH_LIBMAP_CONF);
80 
81 	if (libmap_override) {
82 		/*
83 		 * Do some character replacement to make $LIBMAP look
84 		 * like a text file, then parse it.
85 		 */
86 		libmap_override = xstrdup(libmap_override);
87 		for (p = libmap_override; *p; p++) {
88 			switch (*p) {
89 			case '=':
90 				*p = ' ';
91 				break;
92 			case ',':
93 				*p = '\n';
94 				break;
95 			}
96 		}
97 		lmc_parse(p, strlen(p));
98 		free(p);
99 	}
100 
101 	return (lm_count == 0);
102 }
103 
104 static void
105 lmc_parse_file(char *path)
106 {
107 	struct lmc *p;
108 	struct stat st;
109 	int fd;
110 	char *rpath;
111 	char *lm_map;
112 
113 	rpath = realpath(path, NULL);
114 	if (rpath == NULL)
115 		return;
116 
117 	TAILQ_FOREACH(p, &lmc_head, next) {
118 		if (strcmp(p->path, rpath) == 0) {
119 			free(rpath);
120 			return;
121 		}
122 	}
123 
124 	fd = open(rpath, O_RDONLY | O_CLOEXEC);
125 	if (fd == -1) {
126 		dbg("lm_parse_file: open(\"%s\") failed, %s", rpath,
127 		    rtld_strerror(errno));
128 		free(rpath);
129 		return;
130 	}
131 	if (fstat(fd, &st) == -1) {
132 		close(fd);
133 		dbg("lm_parse_file: fstat(\"%s\") failed, %s", rpath,
134 		    rtld_strerror(errno));
135 		free(rpath);
136 		return;
137 	}
138 	lm_map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
139 	if (lm_map == (const char *)MAP_FAILED) {
140 		close(fd);
141 		dbg("lm_parse_file: mmap(\"%s\") failed, %s", rpath,
142 		    rtld_strerror(errno));
143 		free(rpath);
144 		return;
145 	}
146 	close(fd);
147 	p = xmalloc(sizeof(struct lmc));
148 	p->path = rpath;
149 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
150 	lmc_parse(lm_map, st.st_size);
151 	munmap(lm_map, st.st_size);
152 }
153 
154 static void
155 lmc_parse_dir(char *idir)
156 {
157 	DIR *d;
158 	struct dirent *dp;
159 	struct lmc *p;
160 	char conffile[MAXPATHLEN];
161 	char *ext;
162 	char *rpath;
163 
164 	rpath = realpath(idir, NULL);
165 	if (rpath == NULL)
166 		return;
167 
168 	TAILQ_FOREACH(p, &lmc_head, next) {
169 		if (strcmp(p->path, rpath) == 0) {
170 			free(rpath);
171 			return;
172 		}
173 	}
174 	d = opendir(idir);
175 	if (d == NULL) {
176 		free(rpath);
177 		return;
178 	}
179 
180 	p = xmalloc(sizeof(struct lmc));
181 	p->path = rpath;
182 	TAILQ_INSERT_HEAD(&lmc_head, p, next);
183 
184 	while ((dp = readdir(d)) != NULL) {
185 		if (dp->d_ino == 0)
186 			continue;
187 		if (dp->d_type != DT_REG)
188 			continue;
189 		ext = strrchr(dp->d_name, '.');
190 		if (ext == NULL)
191 			continue;
192 		if (strcmp(ext, ".conf") != 0)
193 			continue;
194 		if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN)
195 			continue; /* too long */
196 		if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN)
197 			continue; /* too long */
198 		if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN)
199 			continue; /* too long */
200 		lmc_parse_file(conffile);
201 	}
202 	closedir(d);
203 }
204 
205 static void
206 lmc_parse(char *lm_p, size_t lm_len)
207 {
208 	char *cp, *f, *t, *c, *p;
209 	char prog[MAXPATHLEN];
210 	/* allow includedir + full length path */
211 	char line[MAXPATHLEN + 13];
212 	size_t cnt;
213 	int i;
214 
215 	cnt = 0;
216 	p = NULL;
217 	while (cnt < lm_len) {
218 		i = 0;
219 		while (lm_p[cnt] != '\n' && cnt < lm_len &&
220 		    i < sizeof(line) - 1) {
221 			line[i] = lm_p[cnt];
222 			cnt++;
223 			i++;
224 		}
225 		line[i] = '\0';
226 		while (lm_p[cnt] != '\n' && cnt < lm_len)
227 			cnt++;
228 		/* skip over nl */
229 		cnt++;
230 
231 		cp = &line[0];
232 		t = f = c = NULL;
233 
234 		/* Skip over leading space */
235 		while (rtld_isspace(*cp)) cp++;
236 
237 		/* Found a comment or EOL */
238 		if (iseol(*cp)) continue;
239 
240 		/* Found a constraint selector */
241 		if (*cp == '[') {
242 			cp++;
243 
244 			/* Skip leading space */
245 			while (rtld_isspace(*cp)) cp++;
246 
247 			/* Found comment, EOL or end of selector */
248 			if  (iseol(*cp) || *cp == ']')
249 				continue;
250 
251 			c = cp++;
252 			/* Skip to end of word */
253 			while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']')
254 				cp++;
255 
256 			/* Skip and zero out trailing space */
257 			while (rtld_isspace(*cp)) *cp++ = '\0';
258 
259 			/* Check if there is a closing brace */
260 			if (*cp != ']') continue;
261 
262 			/* Terminate string if there was no trailing space */
263 			*cp++ = '\0';
264 
265 			/*
266 			 * There should be nothing except whitespace or comment
267 			  from this point to the end of the line.
268 			 */
269 			while(rtld_isspace(*cp)) cp++;
270 			if (!iseol(*cp)) continue;
271 
272 			if (strlcpy(prog, c, sizeof prog) >= sizeof prog)
273 				continue;
274 			p = prog;
275 			continue;
276 		}
277 
278 		/* Parse the 'from' candidate. */
279 		f = cp++;
280 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
281 
282 		/* Skip and zero out the trailing whitespace */
283 		while (rtld_isspace(*cp)) *cp++ = '\0';
284 
285 		/* Found a comment or EOL */
286 		if (iseol(*cp)) continue;
287 
288 		/* Parse 'to' mapping */
289 		t = cp++;
290 		while (!rtld_isspace(*cp) && !iseol(*cp)) cp++;
291 
292 		/* Skip and zero out the trailing whitespace */
293 		while (rtld_isspace(*cp)) *cp++ = '\0';
294 
295 		/* Should be no extra tokens at this point */
296 		if (!iseol(*cp)) continue;
297 
298 		*cp = '\0';
299 		if (strcmp(f, "includedir") == 0)
300 			lmc_parse_dir(t);
301 		else if (strcmp(f, "include") == 0)
302 			lmc_parse_file(t);
303 		else
304 			lm_add(p, f, t);
305 	}
306 }
307 
308 static void
309 lm_free (struct lm_list *lml)
310 {
311 	struct lm *lm;
312 
313 	dbg("%s(%p)", __func__, lml);
314 
315 	while (!TAILQ_EMPTY(lml)) {
316 		lm = TAILQ_FIRST(lml);
317 		TAILQ_REMOVE(lml, lm, lm_link);
318 		free(lm->f);
319 		free(lm->t);
320 		free(lm);
321 	}
322 	return;
323 }
324 
325 void
326 lm_fini (void)
327 {
328 	struct lmp *lmp;
329 	struct lmc *p;
330 
331 	dbg("%s()", __func__);
332 
333 	while (!TAILQ_EMPTY(&lmc_head)) {
334 		p = TAILQ_FIRST(&lmc_head);
335 		TAILQ_REMOVE(&lmc_head, p, next);
336 		free(p->path);
337 		free(p);
338 	}
339 
340 	while (!TAILQ_EMPTY(&lmp_head)) {
341 		lmp = TAILQ_FIRST(&lmp_head);
342 		TAILQ_REMOVE(&lmp_head, lmp, lmp_link);
343 		free(lmp->p);
344 		lm_free(&lmp->lml);
345 		free(lmp);
346 	}
347 	return;
348 }
349 
350 static void
351 lm_add (const char *p, const char *f, const char *t)
352 {
353 	struct lm_list *lml;
354 	struct lm *lm;
355 
356 	if (p == NULL)
357 		p = "$DEFAULT$";
358 
359 	dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t);
360 
361 	if ((lml = lmp_find(p)) == NULL)
362 		lml = lmp_init(xstrdup(p));
363 
364 	lm = xmalloc(sizeof(struct lm));
365 	lm->f = xstrdup(f);
366 	lm->t = xstrdup(t);
367 	TAILQ_INSERT_HEAD(lml, lm, lm_link);
368 	lm_count++;
369 }
370 
371 char *
372 lm_find (const char *p, const char *f)
373 {
374 	struct lm_list *lml;
375 	char *t;
376 
377 	dbg("%s(\"%s\", \"%s\")", __func__, p, f);
378 
379 	if (p != NULL && (lml = lmp_find(p)) != NULL) {
380 		t = lml_find(lml, f);
381 		if (t != NULL) {
382 			/*
383 			 * Add a global mapping if we have
384 			 * a successful constrained match.
385 			 */
386 			lm_add(NULL, f, t);
387 			return (t);
388 		}
389 	}
390 	lml = lmp_find("$DEFAULT$");
391 	if (lml != NULL)
392 		return (lml_find(lml, f));
393 	else
394 		return (NULL);
395 }
396 
397 /* Given a libmap translation list and a library name, return the
398    replacement library, or NULL */
399 #ifdef COMPAT_32BIT
400 char *
401 lm_findn (const char *p, const char *f, const int n)
402 {
403 	char pathbuf[64], *s, *t;
404 
405 	if (n < sizeof(pathbuf) - 1)
406 		s = pathbuf;
407 	else
408 		s = xmalloc(n + 1);
409 	memcpy(s, f, n);
410 	s[n] = '\0';
411 	t = lm_find(p, s);
412 	if (s != pathbuf)
413 		free(s);
414 	return (t);
415 }
416 #endif
417 
418 static char *
419 lml_find (struct lm_list *lmh, const char *f)
420 {
421 	struct lm *lm;
422 
423 	dbg("%s(%p, \"%s\")", __func__, lmh, f);
424 
425 	TAILQ_FOREACH(lm, lmh, lm_link)
426 		if (strcmp(f, lm->f) == 0)
427 			return (lm->t);
428 	return (NULL);
429 }
430 
431 /* Given an executable name, return a pointer to the translation list or
432    NULL if no matches */
433 static struct lm_list *
434 lmp_find (const char *n)
435 {
436 	struct lmp *lmp;
437 
438 	dbg("%s(\"%s\")", __func__, n);
439 
440 	TAILQ_FOREACH(lmp, &lmp_head, lmp_link)
441 		if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) ||
442 		    (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, strlen(lmp->p)) == 0) ||
443 		    (lmp->type == T_BASENAME && strcmp(quickbasename(n), lmp->p) == 0))
444 			return (&lmp->lml);
445 	return (NULL);
446 }
447 
448 static struct lm_list *
449 lmp_init (char *n)
450 {
451 	struct lmp *lmp;
452 
453 	dbg("%s(\"%s\")", __func__, n);
454 
455 	lmp = xmalloc(sizeof(struct lmp));
456 	lmp->p = n;
457 	if (n[strlen(n)-1] == '/')
458 		lmp->type = T_DIRECTORY;
459 	else if (strchr(n,'/') == NULL)
460 		lmp->type = T_BASENAME;
461 	else
462 		lmp->type = T_EXACT;
463 	TAILQ_INIT(&lmp->lml);
464 	TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link);
465 
466 	return (&lmp->lml);
467 }
468 
469 /* libc basename is overkill.  Return a pointer to the character after the
470    last /, or the original string if there are no slashes. */
471 static const char *
472 quickbasename (const char *path)
473 {
474 	const char *p = path;
475 	for (; *path; path++) {
476 		if (*path == '/')
477 			p = path+1;
478 	}
479 	return (p);
480 }
481