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