lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * A small little ldd implementation for uClibc |
| 4 | * |
| 5 | * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org> |
| 6 | * |
| 7 | * Several functions in this file (specifically, elf_find_section_type(), |
| 8 | * elf_find_phdr_type(), and elf_find_dynamic(), were stolen from elflib.c from |
| 9 | * elfvector (http://www.BitWagon.com/elfvector.html) by John F. Reiser |
| 10 | * <jreiser@BitWagon.com>, which is copyright 2000 BitWagon Software LLC |
| 11 | * (GPL2). |
| 12 | * |
| 13 | * Licensed under GPLv2 or later |
| 14 | */ |
| 15 | |
| 16 | #include "porting.h" |
| 17 | |
| 18 | #if defined(__alpha__) |
| 19 | #define MATCH_MACHINE(x) (x == EM_ALPHA) |
| 20 | #define ELFCLASSM ELFCLASS64 |
| 21 | #endif |
| 22 | |
| 23 | #if defined(__arm__) || defined(__thumb__) |
| 24 | #define MATCH_MACHINE(x) (x == EM_ARM) |
| 25 | #define ELFCLASSM ELFCLASS32 |
| 26 | #endif |
| 27 | |
| 28 | #if defined(__avr32__) |
| 29 | #define MATCH_MACHINE(x) (x == EM_AVR32) |
| 30 | #define ELFCLASSM ELFCLASS32 |
| 31 | #endif |
| 32 | |
| 33 | #if defined(__s390__) |
| 34 | #define MATCH_MACHINE(x) (x == EM_S390) |
| 35 | #define ELFCLASSM ELFCLASS32 |
| 36 | #endif |
| 37 | |
| 38 | #if defined(__hppa__) |
| 39 | #define MATCH_MACHINE(x) (x == EM_PARISC) |
| 40 | #if defined(__LP64__) |
| 41 | #define ELFCLASSM ELFCLASS64 |
| 42 | #else |
| 43 | #define ELFCLASSM ELFCLASS32 |
| 44 | #endif |
| 45 | #endif |
| 46 | |
| 47 | #if defined(__i386__) |
| 48 | #ifndef EM_486 |
| 49 | #define MATCH_MACHINE(x) (x == EM_386) |
| 50 | #else |
| 51 | #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486) |
| 52 | #endif |
| 53 | #define ELFCLASSM ELFCLASS32 |
| 54 | #endif |
| 55 | |
| 56 | #if defined(__ia64__) |
| 57 | #define MATCH_MACHINE(x) (x == EM_IA_64) |
| 58 | #define ELFCLASSM ELFCLASS64 |
| 59 | #endif |
| 60 | |
| 61 | #if defined(__mc68000__) |
| 62 | #define MATCH_MACHINE(x) (x == EM_68K) |
| 63 | #define ELFCLASSM ELFCLASS32 |
| 64 | #endif |
| 65 | |
| 66 | #if defined(__mips__) |
| 67 | #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE) |
| 68 | #define ELFCLASSM ELFCLASS32 |
| 69 | #endif |
| 70 | |
| 71 | #if defined(__powerpc64__) |
| 72 | #define MATCH_MACHINE(x) (x == EM_PPC64) |
| 73 | #define ELFCLASSM ELFCLASS64 |
| 74 | #elif defined(__powerpc__) |
| 75 | #define MATCH_MACHINE(x) (x == EM_PPC) |
| 76 | #define ELFCLASSM ELFCLASS32 |
| 77 | #endif |
| 78 | |
| 79 | #if defined(__sh__) |
| 80 | #define MATCH_MACHINE(x) (x == EM_SH) |
| 81 | #define ELFCLASSM ELFCLASS32 |
| 82 | #endif |
| 83 | |
| 84 | #if defined(__v850e__) |
| 85 | #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850) |
| 86 | #define ELFCLASSM ELFCLASS32 |
| 87 | #endif |
| 88 | |
| 89 | #if defined(__sparc__) |
| 90 | #define MATCH_MACHINE(x) ((x) == EM_SPARC || (x) == EM_SPARC32PLUS) |
| 91 | #define ELFCLASSM ELFCLASS32 |
| 92 | #endif |
| 93 | |
| 94 | #if defined(__cris__) |
| 95 | #define MATCH_MACHINE(x) (x == EM_CRIS) |
| 96 | #define ELFCLASSM ELFCLASS32 |
| 97 | #endif |
| 98 | |
| 99 | #if defined(__x86_64__) |
| 100 | #define MATCH_MACHINE(x) (x == EM_X86_64) |
| 101 | #define ELFCLASSM ELFCLASS64 |
| 102 | #endif |
| 103 | |
| 104 | #if defined(__microblaze__) |
| 105 | #define MATCH_MACHINE(x) (x == EM_MICROBLAZE_OLD) |
| 106 | #define ELFCLASSM ELFCLASS32 |
| 107 | #endif |
| 108 | |
| 109 | #ifndef MATCH_MACHINE |
| 110 | # ifdef __linux__ |
| 111 | # include <asm/elf.h> |
| 112 | # endif |
| 113 | # ifdef ELF_ARCH |
| 114 | # define MATCH_MACHINE(x) (x == ELF_ARCH) |
| 115 | # endif |
| 116 | # ifdef ELF_CLASS |
| 117 | # define ELFCLASSM ELF_CLASS |
| 118 | # endif |
| 119 | #endif |
| 120 | #ifndef MATCH_MACHINE |
| 121 | # warning "You really should add a MATCH_MACHINE() macro for your architecture" |
| 122 | #endif |
| 123 | |
| 124 | #if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE |
| 125 | #define ELFDATAM ELFDATA2LSB |
| 126 | #elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG |
| 127 | #define ELFDATAM ELFDATA2MSB |
| 128 | #endif |
| 129 | |
| 130 | #define ARRAY_SIZE(v) (sizeof(v) / sizeof(*v)) |
| 131 | #define TRUSTED_LDSO UCLIBC_RUNTIME_PREFIX "lib/" UCLIBC_LDSO |
| 132 | |
| 133 | struct library { |
| 134 | char *name; |
| 135 | int resolved; |
| 136 | char *path; |
| 137 | struct library *next; |
| 138 | }; |
| 139 | static struct library *lib_list = NULL; |
| 140 | static char not_found[] = "not found"; |
| 141 | static char *interp_name = NULL; |
| 142 | static char *interp_dir = NULL; |
| 143 | static int byteswap; |
| 144 | static int interpreter_already_found = 0; |
| 145 | |
| 146 | static __inline__ uint32_t byteswap32_to_host(uint32_t value) |
| 147 | { |
| 148 | if (byteswap == 1) { |
| 149 | return (bswap_32(value)); |
| 150 | } else { |
| 151 | return (value); |
| 152 | } |
| 153 | } |
| 154 | static __inline__ uint64_t byteswap64_to_host(uint64_t value) |
| 155 | { |
| 156 | if (byteswap == 1) { |
| 157 | return (bswap_64(value)); |
| 158 | } else { |
| 159 | return (value); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #if ELFCLASSM == ELFCLASS32 |
| 164 | # define byteswap_to_host(x) byteswap32_to_host(x) |
| 165 | #else |
| 166 | # define byteswap_to_host(x) byteswap64_to_host(x) |
| 167 | #endif |
| 168 | |
| 169 | static ElfW(Shdr) *elf_find_section_type(uint32_t key, ElfW(Ehdr) *ehdr) |
| 170 | { |
| 171 | int j; |
| 172 | ElfW(Shdr) *shdr; |
| 173 | shdr = (ElfW(Shdr) *) (ehdr->e_shoff + (char *)ehdr); |
| 174 | for (j = ehdr->e_shnum; --j >= 0; ++shdr) { |
| 175 | if (key == byteswap32_to_host(shdr->sh_type)) { |
| 176 | return shdr; |
| 177 | } |
| 178 | } |
| 179 | return NULL; |
| 180 | } |
| 181 | |
| 182 | static ElfW(Phdr) *elf_find_phdr_type(uint32_t type, ElfW(Ehdr) *ehdr) |
| 183 | { |
| 184 | int j; |
| 185 | ElfW(Phdr) *phdr = (ElfW(Phdr) *) (ehdr->e_phoff + (char *)ehdr); |
| 186 | for (j = ehdr->e_phnum; --j >= 0; ++phdr) { |
| 187 | if (type == byteswap32_to_host(phdr->p_type)) { |
| 188 | return phdr; |
| 189 | } |
| 190 | } |
| 191 | return NULL; |
| 192 | } |
| 193 | |
| 194 | /* Returns value if return_val==1, ptr otherwise */ |
| 195 | static void *elf_find_dynamic(int64_t const key, ElfW(Dyn) *dynp, |
| 196 | ElfW(Ehdr) *ehdr, int return_val) |
| 197 | { |
| 198 | ElfW(Phdr) *pt_text = elf_find_phdr_type(PT_LOAD, ehdr); |
| 199 | unsigned tx_reloc = byteswap_to_host(pt_text->p_vaddr) - byteswap_to_host(pt_text->p_offset); |
| 200 | for (; DT_NULL != byteswap_to_host(dynp->d_tag); ++dynp) { |
| 201 | if (key == byteswap_to_host(dynp->d_tag)) { |
| 202 | if (return_val == 1) |
| 203 | return (void *)byteswap_to_host(dynp->d_un.d_val); |
| 204 | else |
| 205 | return (void *)(byteswap_to_host(dynp->d_un.d_val) - tx_reloc + (char *)ehdr); |
| 206 | } |
| 207 | } |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | static char *elf_find_rpath(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic) |
| 212 | { |
| 213 | ElfW(Dyn) *dyns; |
| 214 | |
| 215 | for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) { |
| 216 | if (DT_RPATH == byteswap_to_host(dyns->d_tag)) { |
| 217 | char *strtab; |
| 218 | strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); |
| 219 | return ((char *)strtab + byteswap_to_host(dyns->d_un.d_val)); |
| 220 | } |
| 221 | } |
| 222 | return NULL; |
| 223 | } |
| 224 | |
| 225 | static int check_elf_header(ElfW(Ehdr) *const ehdr) |
| 226 | { |
| 227 | if (!ehdr || *(uint32_t*)ehdr != ELFMAG_U32 |
| 228 | || ehdr->e_ident[EI_CLASS] != ELFCLASSM |
| 229 | || ehdr->e_ident[EI_VERSION] != EV_CURRENT |
| 230 | ) { |
| 231 | return 1; |
| 232 | } |
| 233 | |
| 234 | /* Check if the target endianness matches the host's endianness */ |
| 235 | byteswap = 0; |
| 236 | if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE) { |
| 237 | if (ehdr->e_ident[5] == ELFDATA2MSB) |
| 238 | byteswap = 1; |
| 239 | } else if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG) { |
| 240 | if (ehdr->e_ident[5] == ELFDATA2LSB) |
| 241 | byteswap = 1; |
| 242 | } |
| 243 | |
| 244 | /* Be very lazy, and only byteswap the stuff we use */ |
| 245 | if (byteswap) { |
| 246 | ehdr->e_type = bswap_16(ehdr->e_type); |
| 247 | ehdr->e_phoff = byteswap_to_host(ehdr->e_phoff); |
| 248 | ehdr->e_shoff = byteswap_to_host(ehdr->e_shoff); |
| 249 | ehdr->e_phnum = bswap_16(ehdr->e_phnum); |
| 250 | ehdr->e_shnum = bswap_16(ehdr->e_shnum); |
| 251 | } |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | #ifdef __LDSO_CACHE_SUPPORT__ |
| 257 | static caddr_t cache_addr = NULL; |
| 258 | static size_t cache_size = 0; |
| 259 | |
| 260 | static int map_cache(void) |
| 261 | { |
| 262 | int fd; |
| 263 | struct stat st; |
| 264 | header_t *header; |
| 265 | libentry_t *libent; |
| 266 | int i, strtabsize; |
| 267 | |
| 268 | if (cache_addr == (caddr_t) - 1) |
| 269 | return -1; |
| 270 | else if (cache_addr != NULL) |
| 271 | return 0; |
| 272 | |
| 273 | if (stat(LDSO_CACHE, &st) || (fd = open(LDSO_CACHE, O_RDONLY)) < 0) { |
| 274 | fprintf(stderr, "ldd: can't open cache '%s'\n", LDSO_CACHE); |
| 275 | cache_addr = (caddr_t) - 1; /* so we won't try again */ |
| 276 | return -1; |
| 277 | } |
| 278 | |
| 279 | cache_size = st.st_size; |
| 280 | cache_addr = mmap(0, cache_size, PROT_READ, MAP_SHARED, fd, 0); |
| 281 | close(fd); |
| 282 | if (cache_addr == MAP_FAILED) { |
| 283 | fprintf(stderr, "ldd: can't map cache '%s'\n", LDSO_CACHE); |
| 284 | return -1; |
| 285 | } |
| 286 | |
| 287 | header = (header_t *) cache_addr; |
| 288 | |
| 289 | if (cache_size < sizeof(header_t) |
| 290 | || memcmp(header->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN) |
| 291 | || memcmp(header->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN) |
| 292 | || cache_size < (sizeof(header_t) + header->nlibs * sizeof(libentry_t)) |
| 293 | || cache_addr[cache_size - 1] != '\0') |
| 294 | { |
| 295 | fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE); |
| 296 | goto fail; |
| 297 | } |
| 298 | |
| 299 | strtabsize = cache_size - sizeof(header_t) - header->nlibs * sizeof(libentry_t); |
| 300 | libent = (libentry_t *) & header[1]; |
| 301 | |
| 302 | for (i = 0; i < header->nlibs; i++) { |
| 303 | if (libent[i].sooffset >= strtabsize || libent[i].liboffset >= strtabsize) { |
| 304 | fprintf(stderr, "ldd: cache '%s' is corrupt\n", LDSO_CACHE); |
| 305 | goto fail; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | |
| 311 | fail: |
| 312 | munmap(cache_addr, cache_size); |
| 313 | cache_addr = (caddr_t) - 1; |
| 314 | return -1; |
| 315 | } |
| 316 | |
| 317 | static int unmap_cache(void) |
| 318 | { |
| 319 | if (cache_addr == NULL || cache_addr == (caddr_t) - 1) |
| 320 | return -1; |
| 321 | |
| 322 | #if 1 |
| 323 | munmap(cache_addr, cache_size); |
| 324 | cache_addr = NULL; |
| 325 | #endif |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | #else |
| 330 | static __inline__ void map_cache(void) |
| 331 | { |
| 332 | } |
| 333 | static __inline__ void unmap_cache(void) |
| 334 | { |
| 335 | } |
| 336 | #endif |
| 337 | |
| 338 | /* This function's behavior must exactly match that |
| 339 | * in uClibc/ldso/ldso/dl-elf.c */ |
| 340 | static void search_for_named_library(char *name, char *result, |
| 341 | const char *path_list) |
| 342 | { |
| 343 | int i, count = 1; |
| 344 | char *path, *path_n; |
| 345 | struct stat filestat; |
| 346 | |
| 347 | /* We need a writable copy of this string */ |
| 348 | path = strdup(path_list); |
| 349 | if (!path) { |
| 350 | fprintf(stderr, "%s: Out of memory!\n", __func__); |
| 351 | exit(EXIT_FAILURE); |
| 352 | } |
| 353 | /* Eliminate all double //s */ |
| 354 | path_n = path; |
| 355 | while ((path_n = strstr(path_n, "//"))) { |
| 356 | i = strlen(path_n); |
| 357 | memmove(path_n, path_n + 1, i - 1); |
| 358 | *(path_n + i - 1) = '\0'; |
| 359 | } |
| 360 | |
| 361 | /* Replace colons with zeros in path_list and count them */ |
| 362 | for (i = strlen(path); i > 0; i--) { |
| 363 | if (path[i] == ':') { |
| 364 | path[i] = 0; |
| 365 | count++; |
| 366 | } |
| 367 | } |
| 368 | path_n = path; |
| 369 | for (i = 0; i < count; i++) { |
| 370 | strcpy(result, path_n); |
| 371 | strcat(result, "/"); |
| 372 | strcat(result, name); |
| 373 | if (stat(result, &filestat) == 0 && filestat.st_mode & S_IRUSR) { |
| 374 | free(path); |
| 375 | return; |
| 376 | } |
| 377 | path_n += (strlen(path_n) + 1); |
| 378 | } |
| 379 | free(path); |
| 380 | *result = '\0'; |
| 381 | } |
| 382 | |
| 383 | static void locate_library_file(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, |
| 384 | int is_suid, struct library *lib) |
| 385 | { |
| 386 | char *buf; |
| 387 | char *path; |
| 388 | struct stat filestat; |
| 389 | |
| 390 | /* If this is a fully resolved name, our job is easy */ |
| 391 | if (stat(lib->name, &filestat) == 0) { |
| 392 | lib->path = strdup(lib->name); |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | /* We need some elbow room here. Make some room... */ |
| 397 | buf = malloc(1024); |
| 398 | if (!buf) { |
| 399 | fprintf(stderr, "%s: Out of memory!\n", __func__); |
| 400 | exit(EXIT_FAILURE); |
| 401 | } |
| 402 | |
| 403 | /* This function must match the behavior of _dl_load_shared_library |
| 404 | * in readelflib1.c or things won't work out as expected... */ |
| 405 | |
| 406 | /* The ABI specifies that RPATH is searched first, so do that now. */ |
| 407 | path = elf_find_rpath(ehdr, dynamic); |
| 408 | if (path) { |
| 409 | search_for_named_library(lib->name, buf, path); |
| 410 | if (*buf != '\0') { |
| 411 | lib->path = buf; |
| 412 | return; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | /* Next check LD_{ELF_}LIBRARY_PATH if specified and allowed. |
| 417 | * Since this app doesn't actually run an executable I will skip |
| 418 | * the suid check, and just use LD_{ELF_}LIBRARY_PATH if set */ |
| 419 | if (is_suid == 1) |
| 420 | path = NULL; |
| 421 | else |
| 422 | path = getenv("LD_LIBRARY_PATH"); |
| 423 | if (path) { |
| 424 | search_for_named_library(lib->name, buf, path); |
| 425 | if (*buf != '\0') { |
| 426 | lib->path = buf; |
| 427 | return; |
| 428 | } |
| 429 | } |
| 430 | #ifdef __LDSO_CACHE_SUPPORT__ |
| 431 | if (cache_addr != NULL && cache_addr != (caddr_t) - 1) { |
| 432 | int i; |
| 433 | header_t *header = (header_t *) cache_addr; |
| 434 | libentry_t *libent = (libentry_t *) & header[1]; |
| 435 | char *strs = (char *)&libent[header->nlibs]; |
| 436 | |
| 437 | for (i = 0; i < header->nlibs; i++) { |
| 438 | if ((libent[i].flags == LIB_ELF || |
| 439 | libent[i].flags == LIB_ELF_LIBC0 || |
| 440 | libent[i].flags == LIB_ELF_LIBC5) && |
| 441 | strcmp(lib->name, strs + libent[i].sooffset) == 0) |
| 442 | { |
| 443 | lib->path = strdup(strs + libent[i].liboffset); |
| 444 | return; |
| 445 | } |
| 446 | } |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | /* Next look for libraries wherever the shared library |
| 451 | * loader was installed -- this is usually where we |
| 452 | * should find things... */ |
| 453 | if (interp_dir) { |
| 454 | search_for_named_library(lib->name, buf, interp_dir); |
| 455 | if (*buf != '\0') { |
| 456 | lib->path = buf; |
| 457 | return; |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | /* Lastly, search the standard list of paths for the library. |
| 462 | This list must exactly match the list in uClibc/ldso/ldso/dl-elf.c */ |
| 463 | path = UCLIBC_RUNTIME_PREFIX "lib:" UCLIBC_RUNTIME_PREFIX "usr/lib" |
| 464 | #ifndef __LDSO_CACHE_SUPPORT__ |
| 465 | ":" UCLIBC_RUNTIME_PREFIX "usr/X11R6/lib" |
| 466 | #endif |
| 467 | ; |
| 468 | search_for_named_library(lib->name, buf, path); |
| 469 | if (*buf != '\0') { |
| 470 | lib->path = buf; |
| 471 | } else { |
| 472 | free(buf); |
| 473 | lib->path = not_found; |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | static int add_library(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid, char *s) |
| 478 | { |
| 479 | char *tmp, *tmp1, *tmp2; |
| 480 | struct library *cur, *newlib = lib_list; |
| 481 | |
| 482 | if (!s || !strlen(s)) |
| 483 | return 1; |
| 484 | |
| 485 | tmp = s; |
| 486 | while (*tmp) { |
| 487 | if (*tmp == '/') |
| 488 | s = tmp + 1; |
| 489 | tmp++; |
| 490 | } |
| 491 | |
| 492 | /* We add ldso elsewhere */ |
| 493 | if (interpreter_already_found && (tmp = strrchr(interp_name, '/')) != NULL) { |
| 494 | int len = strlen(interp_dir); |
| 495 | if (strcmp(s, interp_name + 1 + len) == 0) |
| 496 | return 1; |
| 497 | } |
| 498 | |
| 499 | for (cur = lib_list; cur; cur = cur->next) { |
| 500 | /* Check if this library is already in the list */ |
| 501 | tmp1 = tmp2 = cur->name; |
| 502 | while (*tmp1) { |
| 503 | if (*tmp1 == '/') |
| 504 | tmp2 = tmp1 + 1; |
| 505 | tmp1++; |
| 506 | } |
| 507 | if (strcmp(tmp2, s) == 0) { |
| 508 | /*printf("find_elf_interpreter is skipping '%s' (already in list)\n", cur->name); */ |
| 509 | return 0; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* Ok, this lib needs to be added to the list */ |
| 514 | newlib = malloc(sizeof(struct library)); |
| 515 | if (!newlib) |
| 516 | return 1; |
| 517 | newlib->name = malloc(strlen(s) + 1); |
| 518 | strcpy(newlib->name, s); |
| 519 | newlib->resolved = 0; |
| 520 | newlib->path = NULL; |
| 521 | newlib->next = NULL; |
| 522 | |
| 523 | /* Now try and locate where this library might be living... */ |
| 524 | locate_library_file(ehdr, dynamic, is_setuid, newlib); |
| 525 | |
| 526 | /*printf("add_library is adding '%s' to '%s'\n", newlib->name, newlib->path); */ |
| 527 | if (!lib_list) { |
| 528 | lib_list = newlib; |
| 529 | } else { |
| 530 | for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */ |
| 531 | cur->next = newlib; |
| 532 | } |
| 533 | return 0; |
| 534 | } |
| 535 | |
| 536 | static void find_needed_libraries(ElfW(Ehdr) *ehdr, ElfW(Dyn) *dynamic, int is_setuid) |
| 537 | { |
| 538 | ElfW(Dyn) *dyns; |
| 539 | |
| 540 | for (dyns = dynamic; byteswap_to_host(dyns->d_tag) != DT_NULL; ++dyns) { |
| 541 | if (DT_NEEDED == byteswap_to_host(dyns->d_tag)) { |
| 542 | char *strtab; |
| 543 | strtab = (char *)elf_find_dynamic(DT_STRTAB, dynamic, ehdr, 0); |
| 544 | add_library(ehdr, dynamic, is_setuid, (char *)strtab + byteswap_to_host(dyns->d_un.d_val)); |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | #ifdef __LDSO_LDD_SUPPORT__ |
| 550 | static struct library *find_elf_interpreter(ElfW(Ehdr) *ehdr) |
| 551 | { |
| 552 | ElfW(Phdr) *phdr; |
| 553 | |
| 554 | if (interpreter_already_found == 1) |
| 555 | return NULL; |
| 556 | phdr = elf_find_phdr_type(PT_INTERP, ehdr); |
| 557 | if (phdr) { |
| 558 | struct library *cur, *newlib = NULL; |
| 559 | char *s = (char *)ehdr + byteswap_to_host(phdr->p_offset); |
| 560 | |
| 561 | char *tmp, *tmp1; |
| 562 | interp_name = strdup(s); |
| 563 | interp_dir = strdup(s); |
| 564 | tmp = strrchr(interp_dir, '/'); |
| 565 | if (tmp) |
| 566 | *tmp = '\0'; |
| 567 | else { |
| 568 | free(interp_dir); |
| 569 | interp_dir = interp_name; |
| 570 | } |
| 571 | tmp1 = tmp = s; |
| 572 | while (*tmp) { |
| 573 | if (*tmp == '/') |
| 574 | tmp1 = tmp + 1; |
| 575 | tmp++; |
| 576 | } |
| 577 | for (cur = lib_list; cur; cur = cur->next) { |
| 578 | /* Check if this library is already in the list */ |
| 579 | if (strcmp(cur->name, tmp1) == 0) { |
| 580 | /*printf("find_elf_interpreter is replacing '%s' (already in list)\n", cur->name); */ |
| 581 | newlib = cur; |
| 582 | free(newlib->name); |
| 583 | if (newlib->path != not_found) { |
| 584 | free(newlib->path); |
| 585 | } |
| 586 | newlib->name = NULL; |
| 587 | newlib->path = NULL; |
| 588 | break; |
| 589 | } |
| 590 | } |
| 591 | if (newlib == NULL) |
| 592 | newlib = malloc(sizeof(struct library)); |
| 593 | if (!newlib) |
| 594 | return NULL; |
| 595 | newlib->name = malloc(strlen(s) + 1); |
| 596 | strcpy(newlib->name, s); |
| 597 | newlib->path = strdup(newlib->name); |
| 598 | newlib->resolved = 1; |
| 599 | newlib->next = NULL; |
| 600 | |
| 601 | #if 0 |
| 602 | /*printf("find_elf_interpreter is adding '%s' to '%s'\n", newlib->name, newlib->path); */ |
| 603 | if (!lib_list) { |
| 604 | lib_list = newlib; |
| 605 | } else { |
| 606 | for (cur = lib_list; cur->next; cur = cur->next) ; /* nothing */ |
| 607 | cur->next = newlib; |
| 608 | } |
| 609 | #endif |
| 610 | interpreter_already_found = 1; |
| 611 | return newlib; |
| 612 | } |
| 613 | return NULL; |
| 614 | } |
| 615 | #endif /* __LDSO_LDD_SUPPORT__ */ |
| 616 | |
| 617 | /* map the .so, and locate interesting pieces */ |
| 618 | /* |
| 619 | #warning "There may be two warnings here about vfork() clobbering, ignore them" |
| 620 | */ |
| 621 | static int find_dependencies(char *filename) |
| 622 | { |
| 623 | int is_suid = 0; |
| 624 | FILE *thefile; |
| 625 | struct stat statbuf; |
| 626 | ElfW(Ehdr) *ehdr = NULL; |
| 627 | ElfW(Shdr) *dynsec = NULL; |
| 628 | ElfW(Dyn) *dynamic = NULL; |
| 629 | #ifdef __LDSO_LDD_SUPPORT__ |
| 630 | struct library *interp; |
| 631 | #endif |
| 632 | |
| 633 | if (filename == not_found) |
| 634 | return 0; |
| 635 | |
| 636 | if (!filename) { |
| 637 | fprintf(stderr, "No filename specified.\n"); |
| 638 | return -1; |
| 639 | } |
| 640 | if (!(thefile = fopen(filename, "r"))) { |
| 641 | perror(filename); |
| 642 | return -1; |
| 643 | } |
| 644 | if (fstat(fileno(thefile), &statbuf) < 0) { |
| 645 | perror(filename); |
| 646 | fclose(thefile); |
| 647 | return -1; |
| 648 | } |
| 649 | |
| 650 | if ((size_t) statbuf.st_size < sizeof(ElfW(Ehdr))) |
| 651 | goto foo; |
| 652 | |
| 653 | if (!S_ISREG(statbuf.st_mode)) |
| 654 | goto foo; |
| 655 | |
| 656 | /* mmap the file to make reading stuff from it effortless */ |
| 657 | ehdr = mmap(0, statbuf.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fileno(thefile), 0); |
| 658 | if (ehdr == MAP_FAILED) { |
| 659 | fclose(thefile); |
| 660 | fprintf(stderr, "mmap(%s) failed: %s\n", filename, strerror(errno)); |
| 661 | return -1; |
| 662 | } |
| 663 | |
| 664 | foo: |
| 665 | fclose(thefile); |
| 666 | |
| 667 | /* Check if this looks like a legit ELF file */ |
| 668 | if (check_elf_header(ehdr)) { |
| 669 | fprintf(stderr, "%s: not an ELF file.\n", filename); |
| 670 | return -1; |
| 671 | } |
| 672 | /* Check if this is the right kind of ELF file */ |
| 673 | if (ehdr->e_type != ET_EXEC && ehdr->e_type != ET_DYN) { |
| 674 | fprintf(stderr, "%s: not a dynamic executable\n", filename); |
| 675 | return -1; |
| 676 | } |
| 677 | if (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) { |
| 678 | if (statbuf.st_mode & S_ISUID) |
| 679 | is_suid = 1; |
| 680 | if ((statbuf.st_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) |
| 681 | is_suid = 1; |
| 682 | /* FIXME */ |
| 683 | if (is_suid) |
| 684 | fprintf(stderr, "%s: is setuid\n", filename); |
| 685 | } |
| 686 | |
| 687 | interpreter_already_found = 0; |
| 688 | #ifdef __LDSO_LDD_SUPPORT__ |
| 689 | interp = find_elf_interpreter(ehdr); |
| 690 | |
| 691 | if (interp |
| 692 | && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN) |
| 693 | && ehdr->e_ident[EI_CLASS] == ELFCLASSM |
| 694 | && ehdr->e_ident[EI_DATA] == ELFDATAM |
| 695 | && ehdr->e_ident[EI_VERSION] == EV_CURRENT |
| 696 | && MATCH_MACHINE(ehdr->e_machine)) |
| 697 | { |
| 698 | struct stat st; |
| 699 | if (stat(interp->path, &st) == 0 && S_ISREG(st.st_mode)) { |
| 700 | pid_t pid; |
| 701 | int status; |
| 702 | static const char *const environment[] = { |
| 703 | "PATH=/usr/bin:/bin:/usr/sbin:/sbin", |
| 704 | "SHELL=/bin/sh", |
| 705 | "LD_TRACE_LOADED_OBJECTS=1", |
| 706 | NULL |
| 707 | }; |
| 708 | # ifdef __LDSO_STANDALONE_SUPPORT__ |
| 709 | char * lib_path = getenv("LD_LIBRARY_PATH"); |
| 710 | /* The 'extended' environment inclusing the LD_LIBRARY_PATH */ |
| 711 | static char *ext_environment[ARRAY_SIZE(environment) + 1]; |
| 712 | char **envp = (char **) environment; |
| 713 | |
| 714 | if (lib_path) { |
| 715 | /* |
| 716 | * If the LD_LIBRARY_PATH is set, it needs to include it |
| 717 | * into the environment for the new process to be spawned |
| 718 | */ |
| 719 | char ** eenvp = (char **) ext_environment; |
| 720 | |
| 721 | /* Copy the N-1 environment's entries */ |
| 722 | while (*envp) |
| 723 | *eenvp++=*envp++; |
| 724 | |
| 725 | /* Make room for LD_LIBRARY_PATH */ |
| 726 | *eenvp = (char *) malloc(sizeof("LD_LIBRARY_PATH=") |
| 727 | + strlen(lib_path)); |
| 728 | strcpy(*eenvp, "LD_LIBRARY_PATH="); |
| 729 | strcat(*eenvp, lib_path); |
| 730 | lib_path = *eenvp; |
| 731 | /* ext_environment[size] is already NULL */ |
| 732 | |
| 733 | /* Use the extended environment */ |
| 734 | envp = ext_environment; |
| 735 | } |
| 736 | if ((pid = vfork()) == 0) { |
| 737 | /* |
| 738 | * Force to use the standard dynamic linker in stand-alone mode. |
| 739 | * It will fails at runtime if support is not actually available |
| 740 | */ |
| 741 | execle(TRUSTED_LDSO, TRUSTED_LDSO, filename, NULL, envp); |
| 742 | _exit(0xdead); |
| 743 | } |
| 744 | # else |
| 745 | if ((pid = vfork()) == 0) { |
| 746 | /* Cool, it looks like we should be able to actually |
| 747 | * run this puppy. Do so now... */ |
| 748 | execle(filename, filename, NULL, environment); |
| 749 | _exit(0xdead); |
| 750 | } |
| 751 | # endif |
| 752 | /* Wait till it returns */ |
| 753 | waitpid(pid, &status, 0); |
| 754 | |
| 755 | # ifdef __LDSO_STANDALONE_SUPPORT__ |
| 756 | /* Do not leak */ |
| 757 | free(lib_path); |
| 758 | # endif |
| 759 | |
| 760 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
| 761 | return 1; |
| 762 | } |
| 763 | |
| 764 | /* If the exec failed, we fall through to trying to find |
| 765 | * all the needed libraries ourselves by rummaging about |
| 766 | * in the ELF headers... */ |
| 767 | } |
| 768 | } |
| 769 | #endif |
| 770 | |
| 771 | dynsec = elf_find_section_type(SHT_DYNAMIC, ehdr); |
| 772 | if (dynsec) { |
| 773 | dynamic = (ElfW(Dyn) *) (byteswap_to_host(dynsec->sh_offset) + (char *)ehdr); |
| 774 | find_needed_libraries(ehdr, dynamic, is_suid); |
| 775 | } |
| 776 | |
| 777 | return 0; |
| 778 | } |
| 779 | |
| 780 | int main(int argc, char **argv) |
| 781 | { |
| 782 | int multi = 0; |
| 783 | int got_em_all = 1; |
| 784 | char *filename = NULL; |
| 785 | struct library *cur; |
| 786 | |
| 787 | if (argc < 2) { |
| 788 | fprintf(stderr, "ldd: missing file arguments\n" |
| 789 | "Try `ldd --help' for more information.\n"); |
| 790 | exit(EXIT_FAILURE); |
| 791 | } |
| 792 | if (argc > 2) |
| 793 | multi++; |
| 794 | |
| 795 | while (--argc > 0) { |
| 796 | ++argv; |
| 797 | |
| 798 | if (strcmp(*argv, "--") == 0) { |
| 799 | /* Ignore "--" */ |
| 800 | continue; |
| 801 | } |
| 802 | |
| 803 | if (strcmp(*argv, "--help") == 0 || strcmp(*argv, "-h") == 0) { |
| 804 | fprintf(stderr, "Usage: ldd [OPTION]... FILE...\n" |
| 805 | "\t--help\t\tprint this help and exit\n"); |
| 806 | exit(EXIT_SUCCESS); |
| 807 | } |
| 808 | |
| 809 | filename = *argv; |
| 810 | if (!filename) { |
| 811 | fprintf(stderr, "No filename specified.\n"); |
| 812 | exit(EXIT_FAILURE); |
| 813 | } |
| 814 | |
| 815 | if (multi) { |
| 816 | printf("%s:\n", *argv); |
| 817 | } |
| 818 | |
| 819 | map_cache(); |
| 820 | |
| 821 | if (find_dependencies(filename) != 0) |
| 822 | continue; |
| 823 | |
| 824 | while (got_em_all) { |
| 825 | got_em_all = 0; |
| 826 | /* Keep walking the list till everybody is resolved */ |
| 827 | for (cur = lib_list; cur; cur = cur->next) { |
| 828 | if (cur->resolved == 0 && cur->path) { |
| 829 | got_em_all = 1; |
| 830 | printf("checking sub-depends for '%s'\n", cur->path); |
| 831 | find_dependencies(cur->path); |
| 832 | cur->resolved = 1; |
| 833 | } |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | unmap_cache(); |
| 838 | |
| 839 | /* Print the list */ |
| 840 | got_em_all = 0; |
| 841 | for (cur = lib_list; cur; cur = cur->next) { |
| 842 | got_em_all = 1; |
| 843 | printf("\t%s => %s (0x00000000)\n", cur->name, cur->path); |
| 844 | } |
| 845 | if (interp_name && interpreter_already_found == 1) |
| 846 | printf("\t%s => %s (0x00000000)\n", interp_name, interp_name); |
| 847 | else |
| 848 | printf("\tnot a dynamic executable\n"); |
| 849 | |
| 850 | for (cur = lib_list; cur; cur = cur->next) { |
| 851 | free(cur->name); |
| 852 | cur->name = NULL; |
| 853 | if (cur->path && cur->path != not_found) { |
| 854 | free(cur->path); |
| 855 | cur->path = NULL; |
| 856 | } |
| 857 | } |
| 858 | lib_list = NULL; |
| 859 | } |
| 860 | |
| 861 | return 0; |
| 862 | } |