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