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