1 /* $NetBSD: readelf.c,v 1.6 2013/03/23 16:15:58 christos Exp $ */ 2 3 /* 4 * Copyright (c) Christos Zoulas 2003. 5 * All Rights Reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice immediately at the beginning of the file, without modification, 12 * this list of conditions, and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 #include "file.h" 30 31 #ifndef lint 32 #if 0 33 FILE_RCSID("@(#)$File: readelf.c,v 1.97 2013/03/06 03:35:30 christos Exp $") 34 #else 35 __RCSID("$NetBSD: readelf.c,v 1.6 2013/03/23 16:15:58 christos Exp $"); 36 #endif 37 #endif 38 39 #ifdef BUILTIN_ELF 40 #include <string.h> 41 #include <ctype.h> 42 #include <stdlib.h> 43 #ifdef HAVE_UNISTD_H 44 #include <unistd.h> 45 #endif 46 47 #include "readelf.h" 48 #include "magic.h" 49 50 #ifdef ELFCORE 51 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t, 52 off_t, int *); 53 #endif 54 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t, 55 off_t, int *, int); 56 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, 57 off_t, int *, int, int); 58 private size_t donote(struct magic_set *, void *, size_t, size_t, int, 59 int, size_t, int *); 60 61 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align) 62 63 #define isquote(c) (strchr("'\"`", (c)) != NULL) 64 65 private uint16_t getu16(int, uint16_t); 66 private uint32_t getu32(int, uint32_t); 67 private uint64_t getu64(int, uint64_t); 68 69 private uint16_t 70 getu16(int swap, uint16_t value) 71 { 72 union { 73 uint16_t ui; 74 char c[2]; 75 } retval, tmpval; 76 77 if (swap) { 78 tmpval.ui = value; 79 80 retval.c[0] = tmpval.c[1]; 81 retval.c[1] = tmpval.c[0]; 82 83 return retval.ui; 84 } else 85 return value; 86 } 87 88 private uint32_t 89 getu32(int swap, uint32_t value) 90 { 91 union { 92 uint32_t ui; 93 char c[4]; 94 } retval, tmpval; 95 96 if (swap) { 97 tmpval.ui = value; 98 99 retval.c[0] = tmpval.c[3]; 100 retval.c[1] = tmpval.c[2]; 101 retval.c[2] = tmpval.c[1]; 102 retval.c[3] = tmpval.c[0]; 103 104 return retval.ui; 105 } else 106 return value; 107 } 108 109 private uint64_t 110 getu64(int swap, uint64_t value) 111 { 112 union { 113 uint64_t ui; 114 char c[8]; 115 } retval, tmpval; 116 117 if (swap) { 118 tmpval.ui = value; 119 120 retval.c[0] = tmpval.c[7]; 121 retval.c[1] = tmpval.c[6]; 122 retval.c[2] = tmpval.c[5]; 123 retval.c[3] = tmpval.c[4]; 124 retval.c[4] = tmpval.c[3]; 125 retval.c[5] = tmpval.c[2]; 126 retval.c[6] = tmpval.c[1]; 127 retval.c[7] = tmpval.c[0]; 128 129 return retval.ui; 130 } else 131 return value; 132 } 133 134 #define elf_getu16(swap, value) getu16(swap, value) 135 #define elf_getu32(swap, value) getu32(swap, value) 136 #define elf_getu64(swap, value) getu64(swap, value) 137 138 #define xsh_addr (clazz == ELFCLASS32 \ 139 ? (void *)&sh32 \ 140 : (void *)&sh64) 141 #define xsh_sizeof (clazz == ELFCLASS32 \ 142 ? sizeof(sh32) \ 143 : sizeof(sh64)) 144 #define xsh_size (size_t)(clazz == ELFCLASS32 \ 145 ? elf_getu32(swap, sh32.sh_size) \ 146 : elf_getu64(swap, sh64.sh_size)) 147 #define xsh_offset (off_t)(clazz == ELFCLASS32 \ 148 ? elf_getu32(swap, sh32.sh_offset) \ 149 : elf_getu64(swap, sh64.sh_offset)) 150 #define xsh_type (clazz == ELFCLASS32 \ 151 ? elf_getu32(swap, sh32.sh_type) \ 152 : elf_getu32(swap, sh64.sh_type)) 153 #define xsh_name (clazz == ELFCLASS32 \ 154 ? elf_getu32(swap, sh32.sh_name) \ 155 : elf_getu32(swap, sh64.sh_name)) 156 #define xph_addr (clazz == ELFCLASS32 \ 157 ? (void *) &ph32 \ 158 : (void *) &ph64) 159 #define xph_sizeof (clazz == ELFCLASS32 \ 160 ? sizeof(ph32) \ 161 : sizeof(ph64)) 162 #define xph_type (clazz == ELFCLASS32 \ 163 ? elf_getu32(swap, ph32.p_type) \ 164 : elf_getu32(swap, ph64.p_type)) 165 #define xph_offset (off_t)(clazz == ELFCLASS32 \ 166 ? elf_getu32(swap, ph32.p_offset) \ 167 : elf_getu64(swap, ph64.p_offset)) 168 #define xph_align (size_t)((clazz == ELFCLASS32 \ 169 ? (off_t) (ph32.p_align ? \ 170 elf_getu32(swap, ph32.p_align) : 4) \ 171 : (off_t) (ph64.p_align ? \ 172 elf_getu64(swap, ph64.p_align) : 4))) 173 #define xph_filesz (size_t)((clazz == ELFCLASS32 \ 174 ? elf_getu32(swap, ph32.p_filesz) \ 175 : elf_getu64(swap, ph64.p_filesz))) 176 #define xnh_addr (clazz == ELFCLASS32 \ 177 ? (void *)&nh32 \ 178 : (void *)&nh64) 179 #define xph_memsz (size_t)((clazz == ELFCLASS32 \ 180 ? elf_getu32(swap, ph32.p_memsz) \ 181 : elf_getu64(swap, ph64.p_memsz))) 182 #define xnh_sizeof (clazz == ELFCLASS32 \ 183 ? sizeof nh32 \ 184 : sizeof nh64) 185 #define xnh_type (clazz == ELFCLASS32 \ 186 ? elf_getu32(swap, nh32.n_type) \ 187 : elf_getu32(swap, nh64.n_type)) 188 #define xnh_namesz (clazz == ELFCLASS32 \ 189 ? elf_getu32(swap, nh32.n_namesz) \ 190 : elf_getu32(swap, nh64.n_namesz)) 191 #define xnh_descsz (clazz == ELFCLASS32 \ 192 ? elf_getu32(swap, nh32.n_descsz) \ 193 : elf_getu32(swap, nh64.n_descsz)) 194 #define prpsoffsets(i) (clazz == ELFCLASS32 \ 195 ? prpsoffsets32[i] \ 196 : prpsoffsets64[i]) 197 #define xcap_addr (clazz == ELFCLASS32 \ 198 ? (void *)&cap32 \ 199 : (void *)&cap64) 200 #define xcap_sizeof (clazz == ELFCLASS32 \ 201 ? sizeof cap32 \ 202 : sizeof cap64) 203 #define xcap_tag (clazz == ELFCLASS32 \ 204 ? elf_getu32(swap, cap32.c_tag) \ 205 : elf_getu64(swap, cap64.c_tag)) 206 #define xcap_val (clazz == ELFCLASS32 \ 207 ? elf_getu32(swap, cap32.c_un.c_val) \ 208 : elf_getu64(swap, cap64.c_un.c_val)) 209 210 #ifdef ELFCORE 211 /* 212 * Try larger offsets first to avoid false matches 213 * from earlier data that happen to look like strings. 214 */ 215 static const size_t prpsoffsets32[] = { 216 #ifdef USE_NT_PSINFO 217 104, /* SunOS 5.x (command line) */ 218 88, /* SunOS 5.x (short name) */ 219 #endif /* USE_NT_PSINFO */ 220 221 100, /* SunOS 5.x (command line) */ 222 84, /* SunOS 5.x (short name) */ 223 224 44, /* Linux (command line) */ 225 28, /* Linux 2.0.36 (short name) */ 226 227 8, /* FreeBSD */ 228 }; 229 230 static const size_t prpsoffsets64[] = { 231 #ifdef USE_NT_PSINFO 232 152, /* SunOS 5.x (command line) */ 233 136, /* SunOS 5.x (short name) */ 234 #endif /* USE_NT_PSINFO */ 235 236 136, /* SunOS 5.x, 64-bit (command line) */ 237 120, /* SunOS 5.x, 64-bit (short name) */ 238 239 56, /* Linux (command line) */ 240 40, /* Linux (tested on core from 2.4.x, short name) */ 241 242 16, /* FreeBSD, 64-bit */ 243 }; 244 245 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0]) 246 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0]) 247 248 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64) 249 250 /* 251 * Look through the program headers of an executable image, searching 252 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or 253 * "FreeBSD"; if one is found, try looking in various places in its 254 * contents for a 16-character string containing only printable 255 * characters - if found, that string should be the name of the program 256 * that dropped core. Note: right after that 16-character string is, 257 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and 258 * Linux, a longer string (80 characters, in 5.x, probably other 259 * SVR4-flavored systems, and Linux) containing the start of the 260 * command line for that program. 261 * 262 * SunOS 5.x core files contain two PT_NOTE sections, with the types 263 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the 264 * same info about the command name and command line, so it probably 265 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided 266 * above (see USE_NT_PSINFO), in case we ever decide to do so. The 267 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent; 268 * the SunOS 5.x file command relies on this (and prefers the latter). 269 * 270 * The signal number probably appears in a section of type NT_PRSTATUS, 271 * but that's also rather OS-dependent, in ways that are harder to 272 * dissect with heuristics, so I'm not bothering with the signal number. 273 * (I suppose the signal number could be of interest in situations where 274 * you don't have the binary of the program that dropped core; if you 275 * *do* have that binary, the debugger will probably tell you what 276 * signal it was.) 277 */ 278 279 #define OS_STYLE_SVR4 0 280 #define OS_STYLE_FREEBSD 1 281 #define OS_STYLE_NETBSD 2 282 283 private const char os_style_names[][8] = { 284 "SVR4", 285 "FreeBSD", 286 "NetBSD", 287 }; 288 289 #define FLAGS_DID_CORE 0x01 290 #define FLAGS_DID_NOTE 0x02 291 #define FLAGS_DID_BUILD_ID 0x04 292 #define FLAGS_DID_CORE_STYLE 0x08 293 #define FLAGS_IS_CORE 0x10 294 295 private int 296 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off, 297 int num, size_t size, off_t fsize, int *flags) 298 { 299 Elf32_Phdr ph32; 300 Elf64_Phdr ph64; 301 size_t offset, len; 302 unsigned char nbuf[BUFSIZ]; 303 ssize_t bufsize; 304 305 if (size != xph_sizeof) { 306 if (file_printf(ms, ", corrupted program header size") == -1) 307 return -1; 308 return 0; 309 } 310 311 /* 312 * Loop through all the program headers. 313 */ 314 for ( ; num; num--) { 315 if (pread(fd, xph_addr, xph_sizeof, off) == -1) { 316 file_badread(ms); 317 return -1; 318 } 319 off += size; 320 321 if (xph_offset > fsize) { 322 /* Perhaps warn here */ 323 continue; 324 } 325 326 if (xph_type != PT_NOTE) 327 continue; 328 329 /* 330 * This is a PT_NOTE section; loop through all the notes 331 * in the section. 332 */ 333 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf); 334 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) { 335 file_badread(ms); 336 return -1; 337 } 338 offset = 0; 339 for (;;) { 340 if (offset >= (size_t)bufsize) 341 break; 342 offset = donote(ms, nbuf, offset, (size_t)bufsize, 343 clazz, swap, 4, flags); 344 if (offset == 0) 345 break; 346 347 } 348 } 349 return 0; 350 } 351 #endif 352 353 private size_t 354 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size, 355 int clazz, int swap, size_t align, int *flags) 356 { 357 Elf32_Nhdr nh32; 358 Elf64_Nhdr nh64; 359 size_t noff, doff; 360 #ifdef ELFCORE 361 int os_style = -1; 362 #endif 363 uint32_t namesz, descsz; 364 unsigned char *nbuf = CAST(unsigned char *, vbuf); 365 366 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof); 367 offset += xnh_sizeof; 368 369 namesz = xnh_namesz; 370 descsz = xnh_descsz; 371 if ((namesz == 0) && (descsz == 0)) { 372 /* 373 * We're out of note headers. 374 */ 375 return (offset >= size) ? offset : size; 376 } 377 378 if (namesz & 0x80000000) { 379 (void)file_printf(ms, ", bad note name size 0x%lx", 380 (unsigned long)namesz); 381 return offset; 382 } 383 384 if (descsz & 0x80000000) { 385 (void)file_printf(ms, ", bad note description size 0x%lx", 386 (unsigned long)descsz); 387 return offset; 388 } 389 390 391 noff = offset; 392 doff = ELF_ALIGN(offset + namesz); 393 394 if (offset + namesz > size) { 395 /* 396 * We're past the end of the buffer. 397 */ 398 return doff; 399 } 400 401 offset = ELF_ALIGN(doff + descsz); 402 if (doff + descsz > size) { 403 /* 404 * We're past the end of the buffer. 405 */ 406 return (offset >= size) ? offset : size; 407 } 408 409 if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) == 410 (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) 411 goto core; 412 413 if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 && 414 xnh_type == NT_GNU_VERSION && descsz == 2) { 415 file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]); 416 } 417 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && 418 xnh_type == NT_GNU_VERSION && descsz == 16) { 419 uint32_t desc[4]; 420 (void)memcpy(desc, &nbuf[doff], sizeof(desc)); 421 422 if (file_printf(ms, ", for GNU/") == -1) 423 return size; 424 switch (elf_getu32(swap, desc[0])) { 425 case GNU_OS_LINUX: 426 if (file_printf(ms, "Linux") == -1) 427 return size; 428 break; 429 case GNU_OS_HURD: 430 if (file_printf(ms, "Hurd") == -1) 431 return size; 432 break; 433 case GNU_OS_SOLARIS: 434 if (file_printf(ms, "Solaris") == -1) 435 return size; 436 break; 437 case GNU_OS_KFREEBSD: 438 if (file_printf(ms, "kFreeBSD") == -1) 439 return size; 440 break; 441 case GNU_OS_KNETBSD: 442 if (file_printf(ms, "kNetBSD") == -1) 443 return size; 444 break; 445 default: 446 if (file_printf(ms, "<unknown>") == -1) 447 return size; 448 } 449 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]), 450 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1) 451 return size; 452 *flags |= FLAGS_DID_NOTE; 453 return size; 454 } 455 456 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && 457 xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) { 458 uint8_t desc[20]; 459 uint32_t i; 460 if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" : 461 "sha1") == -1) 462 return size; 463 (void)memcpy(desc, &nbuf[doff], descsz); 464 for (i = 0; i < descsz; i++) 465 if (file_printf(ms, "%02x", desc[i]) == -1) 466 return size; 467 *flags |= FLAGS_DID_BUILD_ID; 468 } 469 470 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 && 471 xnh_type == NT_NETBSD_VERSION && descsz == 4) { 472 uint32_t desc; 473 (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); 474 desc = elf_getu32(swap, desc); 475 476 if (file_printf(ms, ", for NetBSD") == -1) 477 return size; 478 /* 479 * The version number used to be stuck as 199905, and was thus 480 * basically content-free. Newer versions of NetBSD have fixed 481 * this and now use the encoding of __NetBSD_Version__: 482 * 483 * MMmmrrpp00 484 * 485 * M = major version 486 * m = minor version 487 * r = release ["",A-Z,Z[A-Z] but numeric] 488 * p = patchlevel 489 */ 490 if (desc > 100000000U) { 491 uint32_t ver_patch = (desc / 100) % 100; 492 uint32_t ver_rel = (desc / 10000) % 100; 493 uint32_t ver_min = (desc / 1000000) % 100; 494 uint32_t ver_maj = desc / 100000000; 495 496 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1) 497 return size; 498 if (ver_rel == 0 && ver_patch != 0) { 499 if (file_printf(ms, ".%u", ver_patch) == -1) 500 return size; 501 } else if (ver_rel != 0) { 502 while (ver_rel > 26) { 503 if (file_printf(ms, "Z") == -1) 504 return size; 505 ver_rel -= 26; 506 } 507 if (file_printf(ms, "%c", 'A' + ver_rel - 1) 508 == -1) 509 return size; 510 } 511 } 512 *flags |= FLAGS_DID_NOTE; 513 return size; 514 } 515 516 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 && 517 xnh_type == NT_FREEBSD_VERSION && descsz == 4) { 518 uint32_t desc; 519 (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); 520 desc = elf_getu32(swap, desc); 521 if (file_printf(ms, ", for FreeBSD") == -1) 522 return size; 523 524 /* 525 * Contents is __FreeBSD_version, whose relation to OS 526 * versions is defined by a huge table in the Porter's 527 * Handbook. This is the general scheme: 528 * 529 * Releases: 530 * Mmp000 (before 4.10) 531 * Mmi0p0 (before 5.0) 532 * Mmm0p0 533 * 534 * Development branches: 535 * Mmpxxx (before 4.6) 536 * Mmp1xx (before 4.10) 537 * Mmi1xx (before 5.0) 538 * M000xx (pre-M.0) 539 * Mmm1xx 540 * 541 * M = major version 542 * m = minor version 543 * i = minor version increment (491000 -> 4.10) 544 * p = patchlevel 545 * x = revision 546 * 547 * The first release of FreeBSD to use ELF by default 548 * was version 3.0. 549 */ 550 if (desc == 460002) { 551 if (file_printf(ms, " 4.6.2") == -1) 552 return size; 553 } else if (desc < 460100) { 554 if (file_printf(ms, " %d.%d", desc / 100000, 555 desc / 10000 % 10) == -1) 556 return size; 557 if (desc / 1000 % 10 > 0) 558 if (file_printf(ms, ".%d", desc / 1000 % 10) 559 == -1) 560 return size; 561 if ((desc % 1000 > 0) || (desc % 100000 == 0)) 562 if (file_printf(ms, " (%d)", desc) == -1) 563 return size; 564 } else if (desc < 500000) { 565 if (file_printf(ms, " %d.%d", desc / 100000, 566 desc / 10000 % 10 + desc / 1000 % 10) == -1) 567 return size; 568 if (desc / 100 % 10 > 0) { 569 if (file_printf(ms, " (%d)", desc) == -1) 570 return size; 571 } else if (desc / 10 % 10 > 0) { 572 if (file_printf(ms, ".%d", desc / 10 % 10) 573 == -1) 574 return size; 575 } 576 } else { 577 if (file_printf(ms, " %d.%d", desc / 100000, 578 desc / 1000 % 100) == -1) 579 return size; 580 if ((desc / 100 % 10 > 0) || 581 (desc % 100000 / 100 == 0)) { 582 if (file_printf(ms, " (%d)", desc) == -1) 583 return size; 584 } else if (desc / 10 % 10 > 0) { 585 if (file_printf(ms, ".%d", desc / 10 % 10) 586 == -1) 587 return size; 588 } 589 } 590 *flags |= FLAGS_DID_NOTE; 591 return size; 592 } 593 594 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 && 595 xnh_type == NT_OPENBSD_VERSION && descsz == 4) { 596 if (file_printf(ms, ", for OpenBSD") == -1) 597 return size; 598 /* Content of note is always 0 */ 599 *flags |= FLAGS_DID_NOTE; 600 return size; 601 } 602 603 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 && 604 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) { 605 uint32_t desc; 606 if (file_printf(ms, ", for DragonFly") == -1) 607 return size; 608 (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); 609 desc = elf_getu32(swap, desc); 610 if (file_printf(ms, " %d.%d.%d", desc / 100000, 611 desc / 10000 % 10, desc % 10000) == -1) 612 return size; 613 *flags |= FLAGS_DID_NOTE; 614 return size; 615 } 616 617 core: 618 /* 619 * Sigh. The 2.0.36 kernel in Debian 2.1, at 620 * least, doesn't correctly implement name 621 * sections, in core dumps, as specified by 622 * the "Program Linking" section of "UNIX(R) System 623 * V Release 4 Programmer's Guide: ANSI C and 624 * Programming Support Tools", because my copy 625 * clearly says "The first 'namesz' bytes in 'name' 626 * contain a *null-terminated* [emphasis mine] 627 * character representation of the entry's owner 628 * or originator", but the 2.0.36 kernel code 629 * doesn't include the terminating null in the 630 * name.... 631 */ 632 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) || 633 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) { 634 os_style = OS_STYLE_SVR4; 635 } 636 637 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) { 638 os_style = OS_STYLE_FREEBSD; 639 } 640 641 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11) 642 == 0)) { 643 os_style = OS_STYLE_NETBSD; 644 } 645 646 #ifdef ELFCORE 647 if ((*flags & FLAGS_DID_CORE) != 0) 648 return size; 649 650 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) { 651 if (file_printf(ms, ", %s-style", os_style_names[os_style]) 652 == -1) 653 return size; 654 *flags |= FLAGS_DID_CORE_STYLE; 655 } 656 657 switch (os_style) { 658 case OS_STYLE_NETBSD: 659 if (xnh_type == NT_NETBSD_CORE_PROCINFO) { 660 uint32_t signo; 661 /* 662 * Extract the program name. It is at 663 * offset 0x7c, and is up to 32-bytes, 664 * including the terminating NUL. 665 */ 666 if (file_printf(ms, ", from '%.31s'", 667 &nbuf[doff + 0x7c]) == -1) 668 return size; 669 670 /* 671 * Extract the signal number. It is at 672 * offset 0x08. 673 */ 674 (void)memcpy(&signo, &nbuf[doff + 0x08], 675 sizeof(signo)); 676 if (file_printf(ms, " (signal %u)", 677 elf_getu32(swap, signo)) == -1) 678 return size; 679 *flags |= FLAGS_DID_CORE; 680 return size; 681 } 682 break; 683 684 default: 685 if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) { 686 size_t i, j; 687 unsigned char c; 688 /* 689 * Extract the program name. We assume 690 * it to be 16 characters (that's what it 691 * is in SunOS 5.x and Linux). 692 * 693 * Unfortunately, it's at a different offset 694 * in various OSes, so try multiple offsets. 695 * If the characters aren't all printable, 696 * reject it. 697 */ 698 for (i = 0; i < NOFFSETS; i++) { 699 unsigned char *cname, *cp; 700 size_t reloffset = prpsoffsets(i); 701 size_t noffset = doff + reloffset; 702 size_t k; 703 for (j = 0; j < 16; j++, noffset++, 704 reloffset++) { 705 /* 706 * Make sure we're not past 707 * the end of the buffer; if 708 * we are, just give up. 709 */ 710 if (noffset >= size) 711 goto tryanother; 712 713 /* 714 * Make sure we're not past 715 * the end of the contents; 716 * if we are, this obviously 717 * isn't the right offset. 718 */ 719 if (reloffset >= descsz) 720 goto tryanother; 721 722 c = nbuf[noffset]; 723 if (c == '\0') { 724 /* 725 * A '\0' at the 726 * beginning is 727 * obviously wrong. 728 * Any other '\0' 729 * means we're done. 730 */ 731 if (j == 0) 732 goto tryanother; 733 else 734 break; 735 } else { 736 /* 737 * A nonprintable 738 * character is also 739 * wrong. 740 */ 741 if (!isprint(c) || isquote(c)) 742 goto tryanother; 743 } 744 } 745 /* 746 * Well, that worked. 747 */ 748 749 /* 750 * Try next offsets, in case this match is 751 * in the middle of a string. 752 */ 753 for (k = i + 1 ; k < NOFFSETS ; k++) { 754 size_t no; 755 int adjust = 1; 756 if (prpsoffsets(k) >= prpsoffsets(i)) 757 continue; 758 for (no = doff + prpsoffsets(k); 759 no < doff + prpsoffsets(i); no++) 760 adjust = adjust 761 && isprint(nbuf[no]); 762 if (adjust) 763 i = k; 764 } 765 766 cname = (unsigned char *) 767 &nbuf[doff + prpsoffsets(i)]; 768 for (cp = cname; *cp && isprint(*cp); cp++) 769 continue; 770 /* 771 * Linux apparently appends a space at the end 772 * of the command line: remove it. 773 */ 774 while (cp > cname && isspace(cp[-1])) 775 cp--; 776 if (file_printf(ms, ", from '%.*s'", 777 (int)(cp - cname), cname) == -1) 778 return size; 779 *flags |= FLAGS_DID_CORE; 780 return size; 781 782 tryanother: 783 ; 784 } 785 } 786 break; 787 } 788 #endif 789 return offset; 790 } 791 792 /* SunOS 5.x hardware capability descriptions */ 793 typedef struct cap_desc { 794 uint64_t cd_mask; 795 const char *cd_name; 796 } cap_desc_t; 797 798 static const cap_desc_t cap_desc_sparc[] = { 799 { AV_SPARC_MUL32, "MUL32" }, 800 { AV_SPARC_DIV32, "DIV32" }, 801 { AV_SPARC_FSMULD, "FSMULD" }, 802 { AV_SPARC_V8PLUS, "V8PLUS" }, 803 { AV_SPARC_POPC, "POPC" }, 804 { AV_SPARC_VIS, "VIS" }, 805 { AV_SPARC_VIS2, "VIS2" }, 806 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" }, 807 { AV_SPARC_FMAF, "FMAF" }, 808 { AV_SPARC_FJFMAU, "FJFMAU" }, 809 { AV_SPARC_IMA, "IMA" }, 810 { 0, NULL } 811 }; 812 813 static const cap_desc_t cap_desc_386[] = { 814 { AV_386_FPU, "FPU" }, 815 { AV_386_TSC, "TSC" }, 816 { AV_386_CX8, "CX8" }, 817 { AV_386_SEP, "SEP" }, 818 { AV_386_AMD_SYSC, "AMD_SYSC" }, 819 { AV_386_CMOV, "CMOV" }, 820 { AV_386_MMX, "MMX" }, 821 { AV_386_AMD_MMX, "AMD_MMX" }, 822 { AV_386_AMD_3DNow, "AMD_3DNow" }, 823 { AV_386_AMD_3DNowx, "AMD_3DNowx" }, 824 { AV_386_FXSR, "FXSR" }, 825 { AV_386_SSE, "SSE" }, 826 { AV_386_SSE2, "SSE2" }, 827 { AV_386_PAUSE, "PAUSE" }, 828 { AV_386_SSE3, "SSE3" }, 829 { AV_386_MON, "MON" }, 830 { AV_386_CX16, "CX16" }, 831 { AV_386_AHF, "AHF" }, 832 { AV_386_TSCP, "TSCP" }, 833 { AV_386_AMD_SSE4A, "AMD_SSE4A" }, 834 { AV_386_POPCNT, "POPCNT" }, 835 { AV_386_AMD_LZCNT, "AMD_LZCNT" }, 836 { AV_386_SSSE3, "SSSE3" }, 837 { AV_386_SSE4_1, "SSE4.1" }, 838 { AV_386_SSE4_2, "SSE4.2" }, 839 { 0, NULL } 840 }; 841 842 private int 843 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num, 844 size_t size, off_t fsize, int *flags, int mach, int strtab) 845 { 846 Elf32_Shdr sh32; 847 Elf64_Shdr sh64; 848 int stripped = 1; 849 void *nbuf; 850 off_t noff, coff, name_off; 851 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */ 852 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */ 853 char name[50]; 854 855 if (size != xsh_sizeof) { 856 if (file_printf(ms, ", corrupted section header size") == -1) 857 return -1; 858 return 0; 859 } 860 861 /* Read offset of name section to be able to read section names later */ 862 if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) { 863 file_badread(ms); 864 return -1; 865 } 866 name_off = xsh_offset; 867 868 for ( ; num; num--) { 869 /* Read the name of this section. */ 870 if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) { 871 file_badread(ms); 872 return -1; 873 } 874 name[sizeof(name) - 1] = '\0'; 875 if (strcmp(name, ".debug_info") == 0) 876 stripped = 0; 877 878 if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) { 879 file_badread(ms); 880 return -1; 881 } 882 off += size; 883 884 /* Things we can determine before we seek */ 885 switch (xsh_type) { 886 case SHT_SYMTAB: 887 #if 0 888 case SHT_DYNSYM: 889 #endif 890 stripped = 0; 891 break; 892 default: 893 if (xsh_offset > fsize) { 894 /* Perhaps warn here */ 895 continue; 896 } 897 break; 898 } 899 900 /* Things we can determine when we seek */ 901 switch (xsh_type) { 902 case SHT_NOTE: 903 if ((nbuf = malloc(xsh_size)) == NULL) { 904 file_error(ms, errno, "Cannot allocate memory" 905 " for note"); 906 return -1; 907 } 908 if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) { 909 file_badread(ms); 910 free(nbuf); 911 return -1; 912 } 913 914 noff = 0; 915 for (;;) { 916 if (noff >= (off_t)xsh_size) 917 break; 918 noff = donote(ms, nbuf, (size_t)noff, 919 xsh_size, clazz, swap, 4, flags); 920 if (noff == 0) 921 break; 922 } 923 free(nbuf); 924 break; 925 case SHT_SUNW_cap: 926 switch (mach) { 927 case EM_SPARC: 928 case EM_SPARCV9: 929 case EM_IA_64: 930 case EM_386: 931 case EM_AMD64: 932 break; 933 default: 934 goto skip; 935 } 936 937 if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) { 938 file_badseek(ms); 939 return -1; 940 } 941 coff = 0; 942 for (;;) { 943 Elf32_Cap cap32; 944 Elf64_Cap cap64; 945 char cbuf[/*CONSTCOND*/ 946 MAX(sizeof cap32, sizeof cap64)]; 947 if ((coff += xcap_sizeof) > (off_t)xsh_size) 948 break; 949 if (read(fd, cbuf, (size_t)xcap_sizeof) != 950 (ssize_t)xcap_sizeof) { 951 file_badread(ms); 952 return -1; 953 } 954 (void)memcpy(xcap_addr, cbuf, xcap_sizeof); 955 switch (xcap_tag) { 956 case CA_SUNW_NULL: 957 break; 958 case CA_SUNW_HW_1: 959 cap_hw1 |= xcap_val; 960 break; 961 case CA_SUNW_SF_1: 962 cap_sf1 |= xcap_val; 963 break; 964 default: 965 if (file_printf(ms, 966 ", with unknown capability " 967 "0x%" INT64_T_FORMAT "x = 0x%" 968 INT64_T_FORMAT "x", 969 (unsigned long long)xcap_tag, 970 (unsigned long long)xcap_val) == -1) 971 return -1; 972 break; 973 } 974 } 975 /*FALLTHROUGH*/ 976 skip: 977 default: 978 break; 979 } 980 } 981 982 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1) 983 return -1; 984 if (cap_hw1) { 985 const cap_desc_t *cdp; 986 switch (mach) { 987 case EM_SPARC: 988 case EM_SPARC32PLUS: 989 case EM_SPARCV9: 990 cdp = cap_desc_sparc; 991 break; 992 case EM_386: 993 case EM_IA_64: 994 case EM_AMD64: 995 cdp = cap_desc_386; 996 break; 997 default: 998 cdp = NULL; 999 break; 1000 } 1001 if (file_printf(ms, ", uses") == -1) 1002 return -1; 1003 if (cdp) { 1004 while (cdp->cd_name) { 1005 if (cap_hw1 & cdp->cd_mask) { 1006 if (file_printf(ms, 1007 " %s", cdp->cd_name) == -1) 1008 return -1; 1009 cap_hw1 &= ~cdp->cd_mask; 1010 } 1011 ++cdp; 1012 } 1013 if (cap_hw1) 1014 if (file_printf(ms, 1015 " unknown hardware capability 0x%" 1016 INT64_T_FORMAT "x", 1017 (unsigned long long)cap_hw1) == -1) 1018 return -1; 1019 } else { 1020 if (file_printf(ms, 1021 " hardware capability 0x%" INT64_T_FORMAT "x", 1022 (unsigned long long)cap_hw1) == -1) 1023 return -1; 1024 } 1025 } 1026 if (cap_sf1) { 1027 if (cap_sf1 & SF1_SUNW_FPUSED) { 1028 if (file_printf(ms, 1029 (cap_sf1 & SF1_SUNW_FPKNWN) 1030 ? ", uses frame pointer" 1031 : ", not known to use frame pointer") == -1) 1032 return -1; 1033 } 1034 cap_sf1 &= ~SF1_SUNW_MASK; 1035 if (cap_sf1) 1036 if (file_printf(ms, 1037 ", with unknown software capability 0x%" 1038 INT64_T_FORMAT "x", 1039 (unsigned long long)cap_sf1) == -1) 1040 return -1; 1041 } 1042 return 0; 1043 } 1044 1045 /* 1046 * Look through the program headers of an executable image, searching 1047 * for a PT_INTERP section; if one is found, it's dynamically linked, 1048 * otherwise it's statically linked. 1049 */ 1050 private int 1051 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off, 1052 int num, size_t size, off_t fsize, int *flags, int sh_num) 1053 { 1054 Elf32_Phdr ph32; 1055 Elf64_Phdr ph64; 1056 const char *linking_style = "statically"; 1057 const char *shared_libraries = ""; 1058 unsigned char nbuf[BUFSIZ]; 1059 ssize_t bufsize; 1060 size_t offset, align, len; 1061 1062 if (size != xph_sizeof) { 1063 if (file_printf(ms, ", corrupted program header size") == -1) 1064 return -1; 1065 return 0; 1066 } 1067 1068 for ( ; num; num--) { 1069 if (pread(fd, xph_addr, xph_sizeof, off) == -1) { 1070 file_badread(ms); 1071 return -1; 1072 } 1073 1074 off += size; 1075 1076 /* Things we can determine before we seek */ 1077 switch (xph_type) { 1078 case PT_DYNAMIC: 1079 linking_style = "dynamically"; 1080 break; 1081 case PT_INTERP: 1082 shared_libraries = " (uses shared libs)"; 1083 break; 1084 default: 1085 if (xph_offset > fsize) { 1086 /* Maybe warn here? */ 1087 continue; 1088 } 1089 break; 1090 } 1091 1092 /* Things we can determine when we seek */ 1093 switch (xph_type) { 1094 case PT_NOTE: 1095 if ((align = xph_align) & 0x80000000UL) { 1096 if (file_printf(ms, 1097 ", invalid note alignment 0x%lx", 1098 (unsigned long)align) == -1) 1099 return -1; 1100 align = 4; 1101 } 1102 if (sh_num) 1103 break; 1104 /* 1105 * This is a PT_NOTE section; loop through all the notes 1106 * in the section. 1107 */ 1108 len = xph_filesz < sizeof(nbuf) ? xph_filesz 1109 : sizeof(nbuf); 1110 bufsize = pread(fd, nbuf, len, xph_offset); 1111 if (bufsize == -1) { 1112 file_badread(ms); 1113 return -1; 1114 } 1115 offset = 0; 1116 for (;;) { 1117 if (offset >= (size_t)bufsize) 1118 break; 1119 offset = donote(ms, nbuf, offset, 1120 (size_t)bufsize, clazz, swap, align, 1121 flags); 1122 if (offset == 0) 1123 break; 1124 } 1125 break; 1126 default: 1127 break; 1128 } 1129 } 1130 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries) 1131 == -1) 1132 return -1; 1133 return 0; 1134 } 1135 1136 1137 protected int 1138 file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf, 1139 size_t nbytes) 1140 { 1141 union { 1142 int32_t l; 1143 char c[sizeof (int32_t)]; 1144 } u; 1145 int clazz; 1146 int swap; 1147 struct stat st; 1148 off_t fsize; 1149 int flags = 0; 1150 Elf32_Ehdr elf32hdr; 1151 Elf64_Ehdr elf64hdr; 1152 uint16_t type; 1153 1154 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE)) 1155 return 0; 1156 /* 1157 * ELF executables have multiple section headers in arbitrary 1158 * file locations and thus file(1) cannot determine it from easily. 1159 * Instead we traverse thru all section headers until a symbol table 1160 * one is found or else the binary is stripped. 1161 * Return immediately if it's not ELF (so we avoid pipe2file unless needed). 1162 */ 1163 if (buf[EI_MAG0] != ELFMAG0 1164 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1) 1165 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) 1166 return 0; 1167 1168 /* 1169 * If we cannot seek, it must be a pipe, socket or fifo. 1170 */ 1171 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE)) 1172 fd = file_pipe2file(ms, fd, buf, nbytes); 1173 1174 if (fstat(fd, &st) == -1) { 1175 file_badread(ms); 1176 return -1; 1177 } 1178 fsize = st.st_size; 1179 1180 clazz = buf[EI_CLASS]; 1181 1182 switch (clazz) { 1183 case ELFCLASS32: 1184 #undef elf_getu 1185 #define elf_getu(a, b) elf_getu32(a, b) 1186 #undef elfhdr 1187 #define elfhdr elf32hdr 1188 #include "elfclass.h" 1189 case ELFCLASS64: 1190 #undef elf_getu 1191 #define elf_getu(a, b) elf_getu64(a, b) 1192 #undef elfhdr 1193 #define elfhdr elf64hdr 1194 #include "elfclass.h" 1195 default: 1196 if (file_printf(ms, ", unknown class %d", clazz) == -1) 1197 return -1; 1198 break; 1199 } 1200 return 0; 1201 } 1202 #endif 1203