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