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 char *lm_map; 103 struct stat st; 104 ssize_t retval; 105 int fd; 106 107 TAILQ_FOREACH(p, &lmc_head, next) { 108 if (strcmp(p->path, path) == 0) 109 return; 110 } 111 112 fd = open(path, O_RDONLY | O_CLOEXEC); 113 if (fd == -1) { 114 dbg("lm_parse_file: open(\"%s\") failed, %s", path, 115 rtld_strerror(errno)); 116 return; 117 } 118 if (fstat(fd, &st) == -1) { 119 close(fd); 120 dbg("lm_parse_file: fstat(\"%s\") failed, %s", path, 121 rtld_strerror(errno)); 122 return; 123 } 124 125 TAILQ_FOREACH(p, &lmc_head, next) { 126 if (p->dev == st.st_dev && p->ino == st.st_ino) { 127 close(fd); 128 return; 129 } 130 } 131 132 lm_map = xmalloc(st.st_size); 133 retval = read(fd, lm_map, st.st_size); 134 if (retval != st.st_size) { 135 close(fd); 136 free(lm_map); 137 dbg("lm_parse_file: read(\"%s\") failed, %s", path, 138 rtld_strerror(errno)); 139 return; 140 } 141 close(fd); 142 p = xmalloc(sizeof(struct lmc)); 143 p->path = xstrdup(path); 144 p->dev = st.st_dev; 145 p->ino = st.st_ino; 146 TAILQ_INSERT_HEAD(&lmc_head, p, next); 147 lmc_parse(lm_map, st.st_size); 148 free(lm_map); 149 } 150 151 static void 152 lmc_parse_dir(char *idir) 153 { 154 DIR *d; 155 struct dirent *dp; 156 struct lmc *p; 157 char conffile[MAXPATHLEN]; 158 char *ext; 159 160 TAILQ_FOREACH(p, &lmc_head, next) { 161 if (strcmp(p->path, idir) == 0) 162 return; 163 } 164 d = opendir(idir); 165 if (d == NULL) 166 return; 167 168 p = xmalloc(sizeof(struct lmc)); 169 p->path = xstrdup(idir); 170 p->dev = NODEV; 171 p->ino = 0; 172 TAILQ_INSERT_HEAD(&lmc_head, p, next); 173 174 while ((dp = readdir(d)) != NULL) { 175 if (dp->d_ino == 0) 176 continue; 177 if (dp->d_type != DT_REG) 178 continue; 179 ext = strrchr(dp->d_name, '.'); 180 if (ext == NULL) 181 continue; 182 if (strcmp(ext, ".conf") != 0) 183 continue; 184 if (strlcpy(conffile, idir, MAXPATHLEN) >= MAXPATHLEN) 185 continue; /* too long */ 186 if (strlcat(conffile, "/", MAXPATHLEN) >= MAXPATHLEN) 187 continue; /* too long */ 188 if (strlcat(conffile, dp->d_name, MAXPATHLEN) >= MAXPATHLEN) 189 continue; /* too long */ 190 lmc_parse_file(conffile); 191 } 192 closedir(d); 193 } 194 195 static void 196 lmc_parse(char *lm_p, size_t lm_len) 197 { 198 char *cp, *f, *t, *c, *p; 199 char prog[MAXPATHLEN]; 200 /* allow includedir + full length path */ 201 char line[MAXPATHLEN + 13]; 202 size_t cnt; 203 int i; 204 205 cnt = 0; 206 p = NULL; 207 while (cnt < lm_len) { 208 i = 0; 209 while (cnt < lm_len && lm_p[cnt] != '\n' && 210 i < sizeof(line) - 1) { 211 line[i] = lm_p[cnt]; 212 cnt++; 213 i++; 214 } 215 line[i] = '\0'; 216 while (cnt < lm_len && lm_p[cnt] != '\n') 217 cnt++; 218 /* skip over nl */ 219 cnt++; 220 221 cp = &line[0]; 222 t = f = c = NULL; 223 224 /* Skip over leading space */ 225 while (rtld_isspace(*cp)) 226 cp++; 227 228 /* Found a comment or EOL */ 229 if (iseol(*cp)) 230 continue; 231 232 /* Found a constraint selector */ 233 if (*cp == '[') { 234 cp++; 235 236 /* Skip leading space */ 237 while (rtld_isspace(*cp)) 238 cp++; 239 240 /* Found comment, EOL or end of selector */ 241 if (iseol(*cp) || *cp == ']') 242 continue; 243 244 c = cp++; 245 /* Skip to end of word */ 246 while (!rtld_isspace(*cp) && !iseol(*cp) && *cp != ']') 247 cp++; 248 249 /* Skip and zero out trailing space */ 250 while (rtld_isspace(*cp)) 251 *cp++ = '\0'; 252 253 /* Check if there is a closing brace */ 254 if (*cp != ']') 255 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)) 265 cp++; 266 if (!iseol(*cp)) 267 continue; 268 269 if (strlcpy(prog, c, sizeof prog) >= sizeof prog) 270 continue; 271 p = prog; 272 continue; 273 } 274 275 /* Parse the 'from' candidate. */ 276 f = cp++; 277 while (!rtld_isspace(*cp) && !iseol(*cp)) 278 cp++; 279 280 /* Skip and zero out the trailing whitespace */ 281 while (rtld_isspace(*cp)) 282 *cp++ = '\0'; 283 284 /* Found a comment or EOL */ 285 if (iseol(*cp)) 286 continue; 287 288 /* Parse 'to' mapping */ 289 t = cp++; 290 while (!rtld_isspace(*cp) && !iseol(*cp)) 291 cp++; 292 293 /* Skip and zero out the trailing whitespace */ 294 while (rtld_isspace(*cp)) 295 *cp++ = '\0'; 296 297 /* Should be no extra tokens at this point */ 298 if (!iseol(*cp)) 299 continue; 300 301 *cp = '\0'; 302 if (strcmp(f, "includedir") == 0) 303 lmc_parse_dir(t); 304 else if (strcmp(f, "include") == 0) 305 lmc_parse_file(t); 306 else 307 lm_add(p, f, t); 308 } 309 } 310 311 static void 312 lm_free(struct lm_list *lml) 313 { 314 struct lm *lm; 315 316 dbg("%s(%p)", __func__, lml); 317 318 while (!TAILQ_EMPTY(lml)) { 319 lm = TAILQ_FIRST(lml); 320 TAILQ_REMOVE(lml, lm, lm_link); 321 free(lm->f); 322 free(lm->t); 323 free(lm); 324 } 325 } 326 327 void 328 lm_fini(void) 329 { 330 struct lmp *lmp; 331 struct lmc *p; 332 333 dbg("%s()", __func__); 334 335 while (!TAILQ_EMPTY(&lmc_head)) { 336 p = TAILQ_FIRST(&lmc_head); 337 TAILQ_REMOVE(&lmc_head, p, next); 338 free(p->path); 339 free(p); 340 } 341 342 while (!TAILQ_EMPTY(&lmp_head)) { 343 lmp = TAILQ_FIRST(&lmp_head); 344 TAILQ_REMOVE(&lmp_head, lmp, lmp_link); 345 free(lmp->p); 346 lm_free(&lmp->lml); 347 free(lmp); 348 } 349 } 350 351 static void 352 lm_add(const char *p, const char *f, const char *t) 353 { 354 struct lm_list *lml; 355 struct lm *lm; 356 357 if (p == NULL) 358 p = "$DEFAULT$"; 359 360 dbg("%s(\"%s\", \"%s\", \"%s\")", __func__, p, f, t); 361 362 if ((lml = lmp_find(p)) == NULL) 363 lml = lmp_init(xstrdup(p)); 364 365 lm = xmalloc(sizeof(struct lm)); 366 lm->f = xstrdup(f); 367 lm->t = xstrdup(t); 368 TAILQ_INSERT_HEAD(lml, lm, lm_link); 369 lm_count++; 370 } 371 372 char * 373 lm_find(const char *p, const char *f) 374 { 375 struct lm_list *lml; 376 char *t; 377 378 dbg("%s(\"%s\", \"%s\")", __func__, p, f); 379 380 if (p != NULL && (lml = lmp_find(p)) != NULL) { 381 t = lml_find(lml, f); 382 if (t != NULL) { 383 /* 384 * Add a global mapping if we have 385 * a successful constrained match. 386 */ 387 lm_add(NULL, f, t); 388 return (t); 389 } 390 } 391 lml = lmp_find("$DEFAULT$"); 392 if (lml != NULL) 393 return (lml_find(lml, f)); 394 return (NULL); 395 } 396 397 /* 398 * Given a libmap translation list and a library name, return the 399 * replacement library, or NULL. 400 */ 401 char * 402 lm_findn(const char *p, const char *f, const int n) 403 { 404 char pathbuf[64], *s, *t; 405 406 if (n < sizeof(pathbuf) - 1) 407 s = pathbuf; 408 else 409 s = xmalloc(n + 1); 410 memcpy(s, f, n); 411 s[n] = '\0'; 412 t = lm_find(p, s); 413 if (s != pathbuf) 414 free(s); 415 return (t); 416 } 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 } 429 return (NULL); 430 } 431 432 /* 433 * Given an executable name, return a pointer to the translation list or 434 * NULL if no matches. 435 */ 436 static struct lm_list * 437 lmp_find(const char *n) 438 { 439 struct lmp *lmp; 440 441 dbg("%s(\"%s\")", __func__, n); 442 443 TAILQ_FOREACH(lmp, &lmp_head, lmp_link) { 444 if ((lmp->type == T_EXACT && strcmp(n, lmp->p) == 0) || 445 (lmp->type == T_DIRECTORY && strncmp(n, lmp->p, 446 strlen(lmp->p)) == 0) || 447 (lmp->type == T_BASENAME && strcmp(quickbasename(n), 448 lmp->p) == 0)) 449 return (&lmp->lml); 450 } 451 return (NULL); 452 } 453 454 static struct lm_list * 455 lmp_init(char *n) 456 { 457 struct lmp *lmp; 458 459 dbg("%s(\"%s\")", __func__, n); 460 461 lmp = xmalloc(sizeof(struct lmp)); 462 lmp->p = n; 463 if (n[strlen(n) - 1] == '/') 464 lmp->type = T_DIRECTORY; 465 else if (strchr(n,'/') == NULL) 466 lmp->type = T_BASENAME; 467 else 468 lmp->type = T_EXACT; 469 TAILQ_INIT(&lmp->lml); 470 TAILQ_INSERT_HEAD(&lmp_head, lmp, lmp_link); 471 472 return (&lmp->lml); 473 } 474 475 /* 476 * libc basename is overkill. Return a pointer to the character after 477 * the last /, or the original string if there are no slashes. 478 */ 479 static const char * 480 quickbasename(const char *path) 481 { 482 const char *p; 483 484 for (p = path; *path != '\0'; path++) { 485 if (*path == '/') 486 p = path + 1; 487 } 488 return (p); 489 } 490