1 /* 2 * Copyright (c) Christos Zoulas 2003. 3 * All Rights Reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice immediately at the beginning of the file, without modification, 10 * this list of conditions, and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 #include "file.h" 28 29 #ifndef lint 30 FILE_RCSID("@(#)$File: readelf.c,v 1.141 2018/04/12 16:50:52 christos Exp $") 31 #endif 32 33 #ifdef BUILTIN_ELF 34 #include <string.h> 35 #include <ctype.h> 36 #include <stdlib.h> 37 #ifdef HAVE_UNISTD_H 38 #include <unistd.h> 39 #endif 40 41 #include "readelf.h" 42 #include "magic.h" 43 44 #ifdef ELFCORE 45 private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t, 46 off_t, int *, uint16_t *); 47 #endif 48 private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t, 49 off_t, int, int *, uint16_t *); 50 private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, 51 off_t, int, int, int *, uint16_t *); 52 private size_t donote(struct magic_set *, void *, size_t, size_t, int, 53 int, size_t, int *, uint16_t *, int, off_t, int, off_t); 54 55 #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align) 56 57 #define isquote(c) (strchr("'\"`", (c)) != NULL) 58 59 private uint16_t getu16(int, uint16_t); 60 private uint32_t getu32(int, uint32_t); 61 private uint64_t getu64(int, uint64_t); 62 63 #define MAX_PHNUM 128 64 #define MAX_SHNUM 32768 65 #define SIZE_UNKNOWN ((off_t)-1) 66 67 private int 68 toomany(struct magic_set *ms, const char *name, uint16_t num) 69 { 70 if (file_printf(ms, ", too many %s (%u)", name, num 71 ) == -1) 72 return -1; 73 return 0; 74 } 75 76 private uint16_t 77 getu16(int swap, uint16_t value) 78 { 79 union { 80 uint16_t ui; 81 char c[2]; 82 } retval, tmpval; 83 84 if (swap) { 85 tmpval.ui = value; 86 87 retval.c[0] = tmpval.c[1]; 88 retval.c[1] = tmpval.c[0]; 89 90 return retval.ui; 91 } else 92 return value; 93 } 94 95 private uint32_t 96 getu32(int swap, uint32_t value) 97 { 98 union { 99 uint32_t ui; 100 char c[4]; 101 } retval, tmpval; 102 103 if (swap) { 104 tmpval.ui = value; 105 106 retval.c[0] = tmpval.c[3]; 107 retval.c[1] = tmpval.c[2]; 108 retval.c[2] = tmpval.c[1]; 109 retval.c[3] = tmpval.c[0]; 110 111 return retval.ui; 112 } else 113 return value; 114 } 115 116 private uint64_t 117 getu64(int swap, uint64_t value) 118 { 119 union { 120 uint64_t ui; 121 char c[8]; 122 } retval, tmpval; 123 124 if (swap) { 125 tmpval.ui = value; 126 127 retval.c[0] = tmpval.c[7]; 128 retval.c[1] = tmpval.c[6]; 129 retval.c[2] = tmpval.c[5]; 130 retval.c[3] = tmpval.c[4]; 131 retval.c[4] = tmpval.c[3]; 132 retval.c[5] = tmpval.c[2]; 133 retval.c[6] = tmpval.c[1]; 134 retval.c[7] = tmpval.c[0]; 135 136 return retval.ui; 137 } else 138 return value; 139 } 140 141 #define elf_getu16(swap, value) getu16(swap, value) 142 #define elf_getu32(swap, value) getu32(swap, value) 143 #define elf_getu64(swap, value) getu64(swap, value) 144 145 #define xsh_addr (clazz == ELFCLASS32 \ 146 ? (void *)&sh32 \ 147 : (void *)&sh64) 148 #define xsh_sizeof (clazz == ELFCLASS32 \ 149 ? sizeof(sh32) \ 150 : sizeof(sh64)) 151 #define xsh_size (size_t)(clazz == ELFCLASS32 \ 152 ? elf_getu32(swap, sh32.sh_size) \ 153 : elf_getu64(swap, sh64.sh_size)) 154 #define xsh_offset (off_t)(clazz == ELFCLASS32 \ 155 ? elf_getu32(swap, sh32.sh_offset) \ 156 : elf_getu64(swap, sh64.sh_offset)) 157 #define xsh_type (clazz == ELFCLASS32 \ 158 ? elf_getu32(swap, sh32.sh_type) \ 159 : elf_getu32(swap, sh64.sh_type)) 160 #define xsh_name (clazz == ELFCLASS32 \ 161 ? elf_getu32(swap, sh32.sh_name) \ 162 : elf_getu32(swap, sh64.sh_name)) 163 #define xph_addr (clazz == ELFCLASS32 \ 164 ? (void *) &ph32 \ 165 : (void *) &ph64) 166 #define xph_sizeof (clazz == ELFCLASS32 \ 167 ? sizeof(ph32) \ 168 : sizeof(ph64)) 169 #define xph_type (clazz == ELFCLASS32 \ 170 ? elf_getu32(swap, ph32.p_type) \ 171 : elf_getu32(swap, ph64.p_type)) 172 #define xph_offset (off_t)(clazz == ELFCLASS32 \ 173 ? elf_getu32(swap, ph32.p_offset) \ 174 : elf_getu64(swap, ph64.p_offset)) 175 #define xph_align (size_t)((clazz == ELFCLASS32 \ 176 ? (off_t) (ph32.p_align ? \ 177 elf_getu32(swap, ph32.p_align) : 4) \ 178 : (off_t) (ph64.p_align ? \ 179 elf_getu64(swap, ph64.p_align) : 4))) 180 #define xph_vaddr (size_t)((clazz == ELFCLASS32 \ 181 ? (off_t) (ph32.p_vaddr ? \ 182 elf_getu32(swap, ph32.p_vaddr) : 4) \ 183 : (off_t) (ph64.p_vaddr ? \ 184 elf_getu64(swap, ph64.p_vaddr) : 4))) 185 #define xph_filesz (size_t)((clazz == ELFCLASS32 \ 186 ? elf_getu32(swap, ph32.p_filesz) \ 187 : elf_getu64(swap, ph64.p_filesz))) 188 #define xnh_addr (clazz == ELFCLASS32 \ 189 ? (void *)&nh32 \ 190 : (void *)&nh64) 191 #define xph_memsz (size_t)((clazz == ELFCLASS32 \ 192 ? elf_getu32(swap, ph32.p_memsz) \ 193 : elf_getu64(swap, ph64.p_memsz))) 194 #define xnh_sizeof (clazz == ELFCLASS32 \ 195 ? sizeof(nh32) \ 196 : sizeof(nh64)) 197 #define xnh_type (clazz == ELFCLASS32 \ 198 ? elf_getu32(swap, nh32.n_type) \ 199 : elf_getu32(swap, nh64.n_type)) 200 #define xnh_namesz (clazz == ELFCLASS32 \ 201 ? elf_getu32(swap, nh32.n_namesz) \ 202 : elf_getu32(swap, nh64.n_namesz)) 203 #define xnh_descsz (clazz == ELFCLASS32 \ 204 ? elf_getu32(swap, nh32.n_descsz) \ 205 : elf_getu32(swap, nh64.n_descsz)) 206 #define prpsoffsets(i) (clazz == ELFCLASS32 \ 207 ? prpsoffsets32[i] \ 208 : prpsoffsets64[i]) 209 #define xcap_addr (clazz == ELFCLASS32 \ 210 ? (void *)&cap32 \ 211 : (void *)&cap64) 212 #define xcap_sizeof (clazz == ELFCLASS32 \ 213 ? sizeof cap32 \ 214 : sizeof cap64) 215 #define xcap_tag (clazz == ELFCLASS32 \ 216 ? elf_getu32(swap, cap32.c_tag) \ 217 : elf_getu64(swap, cap64.c_tag)) 218 #define xcap_val (clazz == ELFCLASS32 \ 219 ? elf_getu32(swap, cap32.c_un.c_val) \ 220 : elf_getu64(swap, cap64.c_un.c_val)) 221 #define xauxv_addr (clazz == ELFCLASS32 \ 222 ? (void *)&auxv32 \ 223 : (void *)&auxv64) 224 #define xauxv_sizeof (clazz == ELFCLASS32 \ 225 ? sizeof(auxv32) \ 226 : sizeof(auxv64)) 227 #define xauxv_type (clazz == ELFCLASS32 \ 228 ? elf_getu32(swap, auxv32.a_type) \ 229 : elf_getu64(swap, auxv64.a_type)) 230 #define xauxv_val (clazz == ELFCLASS32 \ 231 ? elf_getu32(swap, auxv32.a_v) \ 232 : elf_getu64(swap, auxv64.a_v)) 233 234 #ifdef ELFCORE 235 /* 236 * Try larger offsets first to avoid false matches 237 * from earlier data that happen to look like strings. 238 */ 239 static const size_t prpsoffsets32[] = { 240 #ifdef USE_NT_PSINFO 241 104, /* SunOS 5.x (command line) */ 242 88, /* SunOS 5.x (short name) */ 243 #endif /* USE_NT_PSINFO */ 244 245 100, /* SunOS 5.x (command line) */ 246 84, /* SunOS 5.x (short name) */ 247 248 44, /* Linux (command line) */ 249 28, /* Linux 2.0.36 (short name) */ 250 251 8, /* FreeBSD */ 252 }; 253 254 static const size_t prpsoffsets64[] = { 255 #ifdef USE_NT_PSINFO 256 152, /* SunOS 5.x (command line) */ 257 136, /* SunOS 5.x (short name) */ 258 #endif /* USE_NT_PSINFO */ 259 260 136, /* SunOS 5.x, 64-bit (command line) */ 261 120, /* SunOS 5.x, 64-bit (short name) */ 262 263 56, /* Linux (command line) */ 264 40, /* Linux (tested on core from 2.4.x, short name) */ 265 266 16, /* FreeBSD, 64-bit */ 267 }; 268 269 #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0]) 270 #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0]) 271 272 #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64) 273 274 /* 275 * Look through the program headers of an executable image, searching 276 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or 277 * "FreeBSD"; if one is found, try looking in various places in its 278 * contents for a 16-character string containing only printable 279 * characters - if found, that string should be the name of the program 280 * that dropped core. Note: right after that 16-character string is, 281 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and 282 * Linux, a longer string (80 characters, in 5.x, probably other 283 * SVR4-flavored systems, and Linux) containing the start of the 284 * command line for that program. 285 * 286 * SunOS 5.x core files contain two PT_NOTE sections, with the types 287 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the 288 * same info about the command name and command line, so it probably 289 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided 290 * above (see USE_NT_PSINFO), in case we ever decide to do so. The 291 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent; 292 * the SunOS 5.x file command relies on this (and prefers the latter). 293 * 294 * The signal number probably appears in a section of type NT_PRSTATUS, 295 * but that's also rather OS-dependent, in ways that are harder to 296 * dissect with heuristics, so I'm not bothering with the signal number. 297 * (I suppose the signal number could be of interest in situations where 298 * you don't have the binary of the program that dropped core; if you 299 * *do* have that binary, the debugger will probably tell you what 300 * signal it was.) 301 */ 302 303 #define OS_STYLE_SVR4 0 304 #define OS_STYLE_FREEBSD 1 305 #define OS_STYLE_NETBSD 2 306 307 private const char os_style_names[][8] = { 308 "SVR4", 309 "FreeBSD", 310 "NetBSD", 311 }; 312 313 #define FLAGS_CORE_STYLE 0x0003 314 315 #define FLAGS_DID_CORE 0x0004 316 #define FLAGS_DID_OS_NOTE 0x0008 317 #define FLAGS_DID_BUILD_ID 0x0010 318 #define FLAGS_DID_CORE_STYLE 0x0020 319 #define FLAGS_DID_NETBSD_PAX 0x0040 320 #define FLAGS_DID_NETBSD_MARCH 0x0080 321 #define FLAGS_DID_NETBSD_CMODEL 0x0100 322 #define FLAGS_DID_NETBSD_EMULATION 0x0200 323 #define FLAGS_DID_NETBSD_UNKNOWN 0x0400 324 #define FLAGS_IS_CORE 0x0800 325 #define FLAGS_DID_AUXV 0x1000 326 327 private int 328 dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off, 329 int num, size_t size, off_t fsize, int *flags, uint16_t *notecount) 330 { 331 Elf32_Phdr ph32; 332 Elf64_Phdr ph64; 333 size_t offset, len; 334 unsigned char nbuf[BUFSIZ]; 335 ssize_t bufsize; 336 off_t ph_off = off; 337 int ph_num = num; 338 339 if (size != xph_sizeof) { 340 if (file_printf(ms, ", corrupted program header size") == -1) 341 return -1; 342 return 0; 343 } 344 345 /* 346 * Loop through all the program headers. 347 */ 348 for ( ; num; num--) { 349 if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) { 350 file_badread(ms); 351 return -1; 352 } 353 off += size; 354 355 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) { 356 /* Perhaps warn here */ 357 continue; 358 } 359 360 if (xph_type != PT_NOTE) 361 continue; 362 363 /* 364 * This is a PT_NOTE section; loop through all the notes 365 * in the section. 366 */ 367 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf); 368 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) { 369 file_badread(ms); 370 return -1; 371 } 372 offset = 0; 373 for (;;) { 374 if (offset >= (size_t)bufsize) 375 break; 376 offset = donote(ms, nbuf, offset, (size_t)bufsize, 377 clazz, swap, 4, flags, notecount, fd, ph_off, 378 ph_num, fsize); 379 if (offset == 0) 380 break; 381 382 } 383 } 384 return 0; 385 } 386 #endif 387 388 static void 389 do_note_netbsd_version(struct magic_set *ms, int swap, void *v) 390 { 391 uint32_t desc; 392 (void)memcpy(&desc, v, sizeof(desc)); 393 desc = elf_getu32(swap, desc); 394 395 if (file_printf(ms, ", for NetBSD") == -1) 396 return; 397 /* 398 * The version number used to be stuck as 199905, and was thus 399 * basically content-free. Newer versions of NetBSD have fixed 400 * this and now use the encoding of __NetBSD_Version__: 401 * 402 * MMmmrrpp00 403 * 404 * M = major version 405 * m = minor version 406 * r = release ["",A-Z,Z[A-Z] but numeric] 407 * p = patchlevel 408 */ 409 if (desc > 100000000U) { 410 uint32_t ver_patch = (desc / 100) % 100; 411 uint32_t ver_rel = (desc / 10000) % 100; 412 uint32_t ver_min = (desc / 1000000) % 100; 413 uint32_t ver_maj = desc / 100000000; 414 415 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1) 416 return; 417 if (ver_rel == 0 && ver_patch != 0) { 418 if (file_printf(ms, ".%u", ver_patch) == -1) 419 return; 420 } else if (ver_rel != 0) { 421 while (ver_rel > 26) { 422 if (file_printf(ms, "Z") == -1) 423 return; 424 ver_rel -= 26; 425 } 426 if (file_printf(ms, "%c", 'A' + ver_rel - 1) 427 == -1) 428 return; 429 } 430 } 431 } 432 433 static void 434 do_note_freebsd_version(struct magic_set *ms, int swap, void *v) 435 { 436 uint32_t desc; 437 438 (void)memcpy(&desc, v, sizeof(desc)); 439 desc = elf_getu32(swap, desc); 440 if (file_printf(ms, ", for FreeBSD") == -1) 441 return; 442 443 /* 444 * Contents is __FreeBSD_version, whose relation to OS 445 * versions is defined by a huge table in the Porter's 446 * Handbook. This is the general scheme: 447 * 448 * Releases: 449 * Mmp000 (before 4.10) 450 * Mmi0p0 (before 5.0) 451 * Mmm0p0 452 * 453 * Development branches: 454 * Mmpxxx (before 4.6) 455 * Mmp1xx (before 4.10) 456 * Mmi1xx (before 5.0) 457 * M000xx (pre-M.0) 458 * Mmm1xx 459 * 460 * M = major version 461 * m = minor version 462 * i = minor version increment (491000 -> 4.10) 463 * p = patchlevel 464 * x = revision 465 * 466 * The first release of FreeBSD to use ELF by default 467 * was version 3.0. 468 */ 469 if (desc == 460002) { 470 if (file_printf(ms, " 4.6.2") == -1) 471 return; 472 } else if (desc < 460100) { 473 if (file_printf(ms, " %d.%d", desc / 100000, 474 desc / 10000 % 10) == -1) 475 return; 476 if (desc / 1000 % 10 > 0) 477 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1) 478 return; 479 if ((desc % 1000 > 0) || (desc % 100000 == 0)) 480 if (file_printf(ms, " (%d)", desc) == -1) 481 return; 482 } else if (desc < 500000) { 483 if (file_printf(ms, " %d.%d", desc / 100000, 484 desc / 10000 % 10 + desc / 1000 % 10) == -1) 485 return; 486 if (desc / 100 % 10 > 0) { 487 if (file_printf(ms, " (%d)", desc) == -1) 488 return; 489 } else if (desc / 10 % 10 > 0) { 490 if (file_printf(ms, ".%d", desc / 10 % 10) == -1) 491 return; 492 } 493 } else { 494 if (file_printf(ms, " %d.%d", desc / 100000, 495 desc / 1000 % 100) == -1) 496 return; 497 if ((desc / 100 % 10 > 0) || 498 (desc % 100000 / 100 == 0)) { 499 if (file_printf(ms, " (%d)", desc) == -1) 500 return; 501 } else if (desc / 10 % 10 > 0) { 502 if (file_printf(ms, ".%d", desc / 10 % 10) == -1) 503 return; 504 } 505 } 506 } 507 508 private int 509 /*ARGSUSED*/ 510 do_bid_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, 511 int swap __attribute__((__unused__)), uint32_t namesz, uint32_t descsz, 512 size_t noff, size_t doff, int *flags) 513 { 514 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && 515 type == NT_GNU_BUILD_ID && (descsz >= 4 && descsz <= 20)) { 516 uint8_t desc[20]; 517 const char *btype; 518 uint32_t i; 519 *flags |= FLAGS_DID_BUILD_ID; 520 switch (descsz) { 521 case 8: 522 btype = "xxHash"; 523 break; 524 case 16: 525 btype = "md5/uuid"; 526 break; 527 case 20: 528 btype = "sha1"; 529 break; 530 default: 531 btype = "unknown"; 532 break; 533 } 534 if (file_printf(ms, ", BuildID[%s]=", btype) == -1) 535 return 1; 536 (void)memcpy(desc, &nbuf[doff], descsz); 537 for (i = 0; i < descsz; i++) 538 if (file_printf(ms, "%02x", desc[i]) == -1) 539 return 1; 540 return 1; 541 } 542 return 0; 543 } 544 545 private int 546 do_os_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, 547 int swap, uint32_t namesz, uint32_t descsz, 548 size_t noff, size_t doff, int *flags) 549 { 550 if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 && 551 type == NT_GNU_VERSION && descsz == 2) { 552 *flags |= FLAGS_DID_OS_NOTE; 553 file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]); 554 return 1; 555 } 556 557 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 && 558 type == NT_GNU_VERSION && descsz == 16) { 559 uint32_t desc[4]; 560 (void)memcpy(desc, &nbuf[doff], sizeof(desc)); 561 562 *flags |= FLAGS_DID_OS_NOTE; 563 if (file_printf(ms, ", for GNU/") == -1) 564 return 1; 565 switch (elf_getu32(swap, desc[0])) { 566 case GNU_OS_LINUX: 567 if (file_printf(ms, "Linux") == -1) 568 return 1; 569 break; 570 case GNU_OS_HURD: 571 if (file_printf(ms, "Hurd") == -1) 572 return 1; 573 break; 574 case GNU_OS_SOLARIS: 575 if (file_printf(ms, "Solaris") == -1) 576 return 1; 577 break; 578 case GNU_OS_KFREEBSD: 579 if (file_printf(ms, "kFreeBSD") == -1) 580 return 1; 581 break; 582 case GNU_OS_KNETBSD: 583 if (file_printf(ms, "kNetBSD") == -1) 584 return 1; 585 break; 586 default: 587 if (file_printf(ms, "<unknown>") == -1) 588 return 1; 589 } 590 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]), 591 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1) 592 return 1; 593 return 1; 594 } 595 596 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) { 597 if (type == NT_NETBSD_VERSION && descsz == 4) { 598 *flags |= FLAGS_DID_OS_NOTE; 599 do_note_netbsd_version(ms, swap, &nbuf[doff]); 600 return 1; 601 } 602 } 603 604 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) { 605 if (type == NT_FREEBSD_VERSION && descsz == 4) { 606 *flags |= FLAGS_DID_OS_NOTE; 607 do_note_freebsd_version(ms, swap, &nbuf[doff]); 608 return 1; 609 } 610 } 611 612 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 && 613 type == NT_OPENBSD_VERSION && descsz == 4) { 614 *flags |= FLAGS_DID_OS_NOTE; 615 if (file_printf(ms, ", for OpenBSD") == -1) 616 return 1; 617 /* Content of note is always 0 */ 618 return 1; 619 } 620 621 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 && 622 type == NT_DRAGONFLY_VERSION && descsz == 4) { 623 uint32_t desc; 624 *flags |= FLAGS_DID_OS_NOTE; 625 if (file_printf(ms, ", for DragonFly") == -1) 626 return 1; 627 (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); 628 desc = elf_getu32(swap, desc); 629 if (file_printf(ms, " %d.%d.%d", desc / 100000, 630 desc / 10000 % 10, desc % 10000) == -1) 631 return 1; 632 return 1; 633 } 634 return 0; 635 } 636 637 private int 638 do_pax_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, 639 int swap, uint32_t namesz, uint32_t descsz, 640 size_t noff, size_t doff, int *flags) 641 { 642 if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 && 643 type == NT_NETBSD_PAX && descsz == 4) { 644 static const char *pax[] = { 645 "+mprotect", 646 "-mprotect", 647 "+segvguard", 648 "-segvguard", 649 "+ASLR", 650 "-ASLR", 651 }; 652 uint32_t desc; 653 size_t i; 654 int did = 0; 655 656 *flags |= FLAGS_DID_NETBSD_PAX; 657 (void)memcpy(&desc, &nbuf[doff], sizeof(desc)); 658 desc = elf_getu32(swap, desc); 659 660 if (desc && file_printf(ms, ", PaX: ") == -1) 661 return 1; 662 663 for (i = 0; i < __arraycount(pax); i++) { 664 if (((1 << (int)i) & desc) == 0) 665 continue; 666 if (file_printf(ms, "%s%s", did++ ? "," : "", 667 pax[i]) == -1) 668 return 1; 669 } 670 return 1; 671 } 672 return 0; 673 } 674 675 private int 676 do_core_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, 677 int swap, uint32_t namesz, uint32_t descsz, 678 size_t noff, size_t doff, int *flags, size_t size, int clazz) 679 { 680 #ifdef ELFCORE 681 int os_style = -1; 682 /* 683 * Sigh. The 2.0.36 kernel in Debian 2.1, at 684 * least, doesn't correctly implement name 685 * sections, in core dumps, as specified by 686 * the "Program Linking" section of "UNIX(R) System 687 * V Release 4 Programmer's Guide: ANSI C and 688 * Programming Support Tools", because my copy 689 * clearly says "The first 'namesz' bytes in 'name' 690 * contain a *null-terminated* [emphasis mine] 691 * character representation of the entry's owner 692 * or originator", but the 2.0.36 kernel code 693 * doesn't include the terminating null in the 694 * name.... 695 */ 696 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) || 697 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) { 698 os_style = OS_STYLE_SVR4; 699 } 700 701 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) { 702 os_style = OS_STYLE_FREEBSD; 703 } 704 705 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11) 706 == 0)) { 707 os_style = OS_STYLE_NETBSD; 708 } 709 710 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) { 711 if (file_printf(ms, ", %s-style", os_style_names[os_style]) 712 == -1) 713 return 1; 714 *flags |= FLAGS_DID_CORE_STYLE; 715 *flags |= os_style; 716 } 717 718 switch (os_style) { 719 case OS_STYLE_NETBSD: 720 if (type == NT_NETBSD_CORE_PROCINFO) { 721 char sbuf[512]; 722 struct NetBSD_elfcore_procinfo pi; 723 memset(&pi, 0, sizeof(pi)); 724 memcpy(&pi, nbuf + doff, descsz); 725 726 if (file_printf(ms, ", from '%.31s', pid=%u, uid=%u, " 727 "gid=%u, nlwps=%u, lwp=%u (signal %u/code %u)", 728 file_printable(sbuf, sizeof(sbuf), 729 CAST(char *, pi.cpi_name)), 730 elf_getu32(swap, (uint32_t)pi.cpi_pid), 731 elf_getu32(swap, pi.cpi_euid), 732 elf_getu32(swap, pi.cpi_egid), 733 elf_getu32(swap, pi.cpi_nlwps), 734 elf_getu32(swap, (uint32_t)pi.cpi_siglwp), 735 elf_getu32(swap, pi.cpi_signo), 736 elf_getu32(swap, pi.cpi_sigcode)) == -1) 737 return 1; 738 739 *flags |= FLAGS_DID_CORE; 740 return 1; 741 } 742 break; 743 744 default: 745 if (type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) { 746 size_t i, j; 747 unsigned char c; 748 /* 749 * Extract the program name. We assume 750 * it to be 16 characters (that's what it 751 * is in SunOS 5.x and Linux). 752 * 753 * Unfortunately, it's at a different offset 754 * in various OSes, so try multiple offsets. 755 * If the characters aren't all printable, 756 * reject it. 757 */ 758 for (i = 0; i < NOFFSETS; i++) { 759 unsigned char *cname, *cp; 760 size_t reloffset = prpsoffsets(i); 761 size_t noffset = doff + reloffset; 762 size_t k; 763 for (j = 0; j < 16; j++, noffset++, 764 reloffset++) { 765 /* 766 * Make sure we're not past 767 * the end of the buffer; if 768 * we are, just give up. 769 */ 770 if (noffset >= size) 771 goto tryanother; 772 773 /* 774 * Make sure we're not past 775 * the end of the contents; 776 * if we are, this obviously 777 * isn't the right offset. 778 */ 779 if (reloffset >= descsz) 780 goto tryanother; 781 782 c = nbuf[noffset]; 783 if (c == '\0') { 784 /* 785 * A '\0' at the 786 * beginning is 787 * obviously wrong. 788 * Any other '\0' 789 * means we're done. 790 */ 791 if (j == 0) 792 goto tryanother; 793 else 794 break; 795 } else { 796 /* 797 * A nonprintable 798 * character is also 799 * wrong. 800 */ 801 if (!isprint(c) || isquote(c)) 802 goto tryanother; 803 } 804 } 805 /* 806 * Well, that worked. 807 */ 808 809 /* 810 * Try next offsets, in case this match is 811 * in the middle of a string. 812 */ 813 for (k = i + 1 ; k < NOFFSETS; k++) { 814 size_t no; 815 int adjust = 1; 816 if (prpsoffsets(k) >= prpsoffsets(i)) 817 continue; 818 for (no = doff + prpsoffsets(k); 819 no < doff + prpsoffsets(i); no++) 820 adjust = adjust 821 && isprint(nbuf[no]); 822 if (adjust) 823 i = k; 824 } 825 826 cname = (unsigned char *) 827 &nbuf[doff + prpsoffsets(i)]; 828 for (cp = cname; *cp && isprint(*cp); cp++) 829 continue; 830 /* 831 * Linux apparently appends a space at the end 832 * of the command line: remove it. 833 */ 834 while (cp > cname && isspace(cp[-1])) 835 cp--; 836 if (file_printf(ms, ", from '%.*s'", 837 (int)(cp - cname), cname) == -1) 838 return 1; 839 *flags |= FLAGS_DID_CORE; 840 return 1; 841 842 tryanother: 843 ; 844 } 845 } 846 break; 847 } 848 #endif 849 return 0; 850 } 851 852 private off_t 853 get_offset_from_virtaddr(struct magic_set *ms, int swap, int clazz, int fd, 854 off_t off, int num, off_t fsize, uint64_t virtaddr) 855 { 856 Elf32_Phdr ph32; 857 Elf64_Phdr ph64; 858 859 /* 860 * Loop through all the program headers and find the header with 861 * virtual address in which the "virtaddr" belongs to. 862 */ 863 for ( ; num; num--) { 864 if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) { 865 file_badread(ms); 866 return -1; 867 } 868 off += xph_sizeof; 869 870 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) { 871 /* Perhaps warn here */ 872 continue; 873 } 874 875 if (virtaddr >= xph_vaddr && virtaddr < xph_vaddr + xph_filesz) 876 return xph_offset + (virtaddr - xph_vaddr); 877 } 878 return 0; 879 } 880 881 private size_t 882 get_string_on_virtaddr(struct magic_set *ms, 883 int swap, int clazz, int fd, off_t ph_off, int ph_num, 884 off_t fsize, uint64_t virtaddr, char *buf, ssize_t buflen) 885 { 886 char *bptr; 887 off_t offset; 888 889 if (buflen == 0) 890 return 0; 891 892 offset = get_offset_from_virtaddr(ms, swap, clazz, fd, ph_off, ph_num, 893 fsize, virtaddr); 894 if ((buflen = pread(fd, buf, CAST(size_t, buflen), offset)) <= 0) { 895 file_badread(ms); 896 return 0; 897 } 898 899 buf[buflen - 1] = '\0'; 900 901 /* We expect only printable characters, so return if buffer contains 902 * non-printable character before the '\0' or just '\0'. */ 903 for (bptr = buf; *bptr && isprint((unsigned char)*bptr); bptr++) 904 continue; 905 if (*bptr != '\0') 906 return 0; 907 908 return bptr - buf; 909 } 910 911 912 /*ARGSUSED*/ 913 private int 914 do_auxv_note(struct magic_set *ms, unsigned char *nbuf, uint32_t type, 915 int swap, uint32_t namesz __attribute__((__unused__)), 916 uint32_t descsz __attribute__((__unused__)), 917 size_t noff __attribute__((__unused__)), size_t doff, 918 int *flags, size_t size __attribute__((__unused__)), int clazz, 919 int fd, off_t ph_off, int ph_num, off_t fsize) 920 { 921 #ifdef ELFCORE 922 Aux32Info auxv32; 923 Aux64Info auxv64; 924 size_t elsize = xauxv_sizeof; 925 const char *tag; 926 int is_string; 927 size_t nval; 928 929 if ((*flags & (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) != 930 (FLAGS_IS_CORE|FLAGS_DID_CORE_STYLE)) 931 return 0; 932 933 switch (*flags & FLAGS_CORE_STYLE) { 934 case OS_STYLE_SVR4: 935 if (type != NT_AUXV) 936 return 0; 937 break; 938 #ifdef notyet 939 case OS_STYLE_NETBSD: 940 if (type != NT_NETBSD_CORE_AUXV) 941 return 0; 942 break; 943 case OS_STYLE_FREEBSD: 944 if (type != NT_FREEBSD_PROCSTAT_AUXV) 945 return 0; 946 break; 947 #endif 948 default: 949 return 0; 950 } 951 952 *flags |= FLAGS_DID_AUXV; 953 954 nval = 0; 955 for (size_t off = 0; off + elsize <= descsz; off += elsize) { 956 (void)memcpy(xauxv_addr, &nbuf[doff + off], xauxv_sizeof); 957 /* Limit processing to 50 vector entries to prevent DoS */ 958 if (nval++ >= 50) { 959 file_error(ms, 0, "Too many ELF Auxv elements"); 960 return 1; 961 } 962 963 switch(xauxv_type) { 964 case AT_LINUX_EXECFN: 965 is_string = 1; 966 tag = "execfn"; 967 break; 968 case AT_LINUX_PLATFORM: 969 is_string = 1; 970 tag = "platform"; 971 break; 972 case AT_LINUX_UID: 973 is_string = 0; 974 tag = "real uid"; 975 break; 976 case AT_LINUX_GID: 977 is_string = 0; 978 tag = "real gid"; 979 break; 980 case AT_LINUX_EUID: 981 is_string = 0; 982 tag = "effective uid"; 983 break; 984 case AT_LINUX_EGID: 985 is_string = 0; 986 tag = "effective gid"; 987 break; 988 default: 989 is_string = 0; 990 tag = NULL; 991 break; 992 } 993 994 if (tag == NULL) 995 continue; 996 997 if (is_string) { 998 char buf[256]; 999 ssize_t buflen; 1000 buflen = get_string_on_virtaddr(ms, swap, clazz, fd, 1001 ph_off, ph_num, fsize, xauxv_val, buf, sizeof(buf)); 1002 1003 if (buflen == 0) 1004 continue; 1005 1006 if (file_printf(ms, ", %s: '%s'", tag, buf) == -1) 1007 return 0; 1008 } else { 1009 if (file_printf(ms, ", %s: %d", tag, (int) xauxv_val) 1010 == -1) 1011 return 0; 1012 } 1013 } 1014 return 1; 1015 #else 1016 return 0; 1017 #endif 1018 } 1019 1020 private size_t 1021 donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size, 1022 int clazz, int swap, size_t align, int *flags, uint16_t *notecount, 1023 int fd, off_t ph_off, int ph_num, off_t fsize) 1024 { 1025 Elf32_Nhdr nh32; 1026 Elf64_Nhdr nh64; 1027 size_t noff, doff; 1028 uint32_t namesz, descsz; 1029 unsigned char *nbuf = CAST(unsigned char *, vbuf); 1030 1031 if (*notecount == 0) 1032 return 0; 1033 --*notecount; 1034 1035 if (xnh_sizeof + offset > size) { 1036 /* 1037 * We're out of note headers. 1038 */ 1039 return xnh_sizeof + offset; 1040 } 1041 1042 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof); 1043 offset += xnh_sizeof; 1044 1045 namesz = xnh_namesz; 1046 descsz = xnh_descsz; 1047 1048 if ((namesz == 0) && (descsz == 0)) { 1049 /* 1050 * We're out of note headers. 1051 */ 1052 return (offset >= size) ? offset : size; 1053 } 1054 1055 if (namesz & 0x80000000) { 1056 (void)file_printf(ms, ", bad note name size %#lx", 1057 (unsigned long)namesz); 1058 return 0; 1059 } 1060 1061 if (descsz & 0x80000000) { 1062 (void)file_printf(ms, ", bad note description size %#lx", 1063 (unsigned long)descsz); 1064 return 0; 1065 } 1066 1067 noff = offset; 1068 doff = ELF_ALIGN(offset + namesz); 1069 1070 if (offset + namesz > size) { 1071 /* 1072 * We're past the end of the buffer. 1073 */ 1074 return doff; 1075 } 1076 1077 offset = ELF_ALIGN(doff + descsz); 1078 if (doff + descsz > size) { 1079 /* 1080 * We're past the end of the buffer. 1081 */ 1082 return (offset >= size) ? offset : size; 1083 } 1084 1085 1086 if ((*flags & FLAGS_DID_OS_NOTE) == 0) { 1087 if (do_os_note(ms, nbuf, xnh_type, swap, 1088 namesz, descsz, noff, doff, flags)) 1089 return offset; 1090 } 1091 1092 if ((*flags & FLAGS_DID_BUILD_ID) == 0) { 1093 if (do_bid_note(ms, nbuf, xnh_type, swap, 1094 namesz, descsz, noff, doff, flags)) 1095 return offset; 1096 } 1097 1098 if ((*flags & FLAGS_DID_NETBSD_PAX) == 0) { 1099 if (do_pax_note(ms, nbuf, xnh_type, swap, 1100 namesz, descsz, noff, doff, flags)) 1101 return offset; 1102 } 1103 1104 if ((*flags & FLAGS_DID_CORE) == 0) { 1105 if (do_core_note(ms, nbuf, xnh_type, swap, 1106 namesz, descsz, noff, doff, flags, size, clazz)) 1107 return offset; 1108 } 1109 1110 if ((*flags & FLAGS_DID_AUXV) == 0) { 1111 if (do_auxv_note(ms, nbuf, xnh_type, swap, 1112 namesz, descsz, noff, doff, flags, size, clazz, 1113 fd, ph_off, ph_num, fsize)) 1114 return offset; 1115 } 1116 1117 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) { 1118 if (descsz > 100) 1119 descsz = 100; 1120 switch (xnh_type) { 1121 case NT_NETBSD_VERSION: 1122 return offset; 1123 case NT_NETBSD_MARCH: 1124 if (*flags & FLAGS_DID_NETBSD_MARCH) 1125 return offset; 1126 *flags |= FLAGS_DID_NETBSD_MARCH; 1127 if (file_printf(ms, ", compiled for: %.*s", 1128 (int)descsz, (const char *)&nbuf[doff]) == -1) 1129 return offset; 1130 break; 1131 case NT_NETBSD_CMODEL: 1132 if (*flags & FLAGS_DID_NETBSD_CMODEL) 1133 return offset; 1134 *flags |= FLAGS_DID_NETBSD_CMODEL; 1135 if (file_printf(ms, ", compiler model: %.*s", 1136 (int)descsz, (const char *)&nbuf[doff]) == -1) 1137 return offset; 1138 break; 1139 case NT_NETBSD_EMULATION: 1140 if (*flags & FLAGS_DID_NETBSD_EMULATION) 1141 return offset; 1142 *flags |= FLAGS_DID_NETBSD_EMULATION; 1143 if (file_printf(ms, ", emulation: %.*s", 1144 (int)descsz, (const char *)&nbuf[doff]) == -1) 1145 return offset; 1146 break; 1147 default: 1148 if (*flags & FLAGS_DID_NETBSD_UNKNOWN) 1149 return offset; 1150 *flags |= FLAGS_DID_NETBSD_UNKNOWN; 1151 if (file_printf(ms, ", note=%u", xnh_type) == -1) 1152 return offset; 1153 break; 1154 } 1155 return offset; 1156 } 1157 1158 return offset; 1159 } 1160 1161 /* SunOS 5.x hardware capability descriptions */ 1162 typedef struct cap_desc { 1163 uint64_t cd_mask; 1164 const char *cd_name; 1165 } cap_desc_t; 1166 1167 static const cap_desc_t cap_desc_sparc[] = { 1168 { AV_SPARC_MUL32, "MUL32" }, 1169 { AV_SPARC_DIV32, "DIV32" }, 1170 { AV_SPARC_FSMULD, "FSMULD" }, 1171 { AV_SPARC_V8PLUS, "V8PLUS" }, 1172 { AV_SPARC_POPC, "POPC" }, 1173 { AV_SPARC_VIS, "VIS" }, 1174 { AV_SPARC_VIS2, "VIS2" }, 1175 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" }, 1176 { AV_SPARC_FMAF, "FMAF" }, 1177 { AV_SPARC_FJFMAU, "FJFMAU" }, 1178 { AV_SPARC_IMA, "IMA" }, 1179 { 0, NULL } 1180 }; 1181 1182 static const cap_desc_t cap_desc_386[] = { 1183 { AV_386_FPU, "FPU" }, 1184 { AV_386_TSC, "TSC" }, 1185 { AV_386_CX8, "CX8" }, 1186 { AV_386_SEP, "SEP" }, 1187 { AV_386_AMD_SYSC, "AMD_SYSC" }, 1188 { AV_386_CMOV, "CMOV" }, 1189 { AV_386_MMX, "MMX" }, 1190 { AV_386_AMD_MMX, "AMD_MMX" }, 1191 { AV_386_AMD_3DNow, "AMD_3DNow" }, 1192 { AV_386_AMD_3DNowx, "AMD_3DNowx" }, 1193 { AV_386_FXSR, "FXSR" }, 1194 { AV_386_SSE, "SSE" }, 1195 { AV_386_SSE2, "SSE2" }, 1196 { AV_386_PAUSE, "PAUSE" }, 1197 { AV_386_SSE3, "SSE3" }, 1198 { AV_386_MON, "MON" }, 1199 { AV_386_CX16, "CX16" }, 1200 { AV_386_AHF, "AHF" }, 1201 { AV_386_TSCP, "TSCP" }, 1202 { AV_386_AMD_SSE4A, "AMD_SSE4A" }, 1203 { AV_386_POPCNT, "POPCNT" }, 1204 { AV_386_AMD_LZCNT, "AMD_LZCNT" }, 1205 { AV_386_SSSE3, "SSSE3" }, 1206 { AV_386_SSE4_1, "SSE4.1" }, 1207 { AV_386_SSE4_2, "SSE4.2" }, 1208 { 0, NULL } 1209 }; 1210 1211 private int 1212 doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num, 1213 size_t size, off_t fsize, int mach, int strtab, int *flags, 1214 uint16_t *notecount) 1215 { 1216 Elf32_Shdr sh32; 1217 Elf64_Shdr sh64; 1218 int stripped = 1, has_debug_info = 0; 1219 size_t nbadcap = 0; 1220 void *nbuf; 1221 off_t noff, coff, name_off; 1222 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilities */ 1223 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilities */ 1224 char name[50]; 1225 ssize_t namesize; 1226 1227 if (size != xsh_sizeof) { 1228 if (file_printf(ms, ", corrupted section header size") == -1) 1229 return -1; 1230 return 0; 1231 } 1232 1233 /* Read offset of name section to be able to read section names later */ 1234 if (pread(fd, xsh_addr, xsh_sizeof, CAST(off_t, (off + size * strtab))) 1235 < (ssize_t)xsh_sizeof) { 1236 if (file_printf(ms, ", missing section headers") == -1) 1237 return -1; 1238 return 0; 1239 } 1240 name_off = xsh_offset; 1241 1242 for ( ; num; num--) { 1243 /* Read the name of this section. */ 1244 if ((namesize = pread(fd, name, sizeof(name) - 1, name_off + xsh_name)) == -1) { 1245 file_badread(ms); 1246 return -1; 1247 } 1248 name[namesize] = '\0'; 1249 if (strcmp(name, ".debug_info") == 0) { 1250 has_debug_info = 1; 1251 stripped = 0; 1252 } 1253 1254 if (pread(fd, xsh_addr, xsh_sizeof, off) < (ssize_t)xsh_sizeof) { 1255 file_badread(ms); 1256 return -1; 1257 } 1258 off += size; 1259 1260 /* Things we can determine before we seek */ 1261 switch (xsh_type) { 1262 case SHT_SYMTAB: 1263 #if 0 1264 case SHT_DYNSYM: 1265 #endif 1266 stripped = 0; 1267 break; 1268 default: 1269 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) { 1270 /* Perhaps warn here */ 1271 continue; 1272 } 1273 break; 1274 } 1275 1276 1277 /* Things we can determine when we seek */ 1278 switch (xsh_type) { 1279 case SHT_NOTE: 1280 if ((uintmax_t)(xsh_size + xsh_offset) > 1281 (uintmax_t)fsize) { 1282 if (file_printf(ms, 1283 ", note offset/size %#" INTMAX_T_FORMAT 1284 "x+%#" INTMAX_T_FORMAT "x exceeds" 1285 " file size %#" INTMAX_T_FORMAT "x", 1286 (uintmax_t)xsh_offset, (uintmax_t)xsh_size, 1287 (uintmax_t)fsize) == -1) 1288 return -1; 1289 return 0; 1290 } 1291 if ((nbuf = malloc(xsh_size)) == NULL) { 1292 file_error(ms, errno, "Cannot allocate memory" 1293 " for note"); 1294 return -1; 1295 } 1296 if (pread(fd, nbuf, xsh_size, xsh_offset) < 1297 (ssize_t)xsh_size) { 1298 file_badread(ms); 1299 free(nbuf); 1300 return -1; 1301 } 1302 1303 noff = 0; 1304 for (;;) { 1305 if (noff >= (off_t)xsh_size) 1306 break; 1307 noff = donote(ms, nbuf, (size_t)noff, 1308 xsh_size, clazz, swap, 4, flags, notecount, 1309 fd, 0, 0, 0); 1310 if (noff == 0) 1311 break; 1312 } 1313 free(nbuf); 1314 break; 1315 case SHT_SUNW_cap: 1316 switch (mach) { 1317 case EM_SPARC: 1318 case EM_SPARCV9: 1319 case EM_IA_64: 1320 case EM_386: 1321 case EM_AMD64: 1322 break; 1323 default: 1324 goto skip; 1325 } 1326 1327 if (nbadcap > 5) 1328 break; 1329 if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) { 1330 file_badseek(ms); 1331 return -1; 1332 } 1333 coff = 0; 1334 for (;;) { 1335 Elf32_Cap cap32; 1336 Elf64_Cap cap64; 1337 char cbuf[/*CONSTCOND*/ 1338 MAX(sizeof cap32, sizeof cap64)]; 1339 if ((coff += xcap_sizeof) > (off_t)xsh_size) 1340 break; 1341 if (read(fd, cbuf, (size_t)xcap_sizeof) != 1342 (ssize_t)xcap_sizeof) { 1343 file_badread(ms); 1344 return -1; 1345 } 1346 if (cbuf[0] == 'A') { 1347 #ifdef notyet 1348 char *p = cbuf + 1; 1349 uint32_t len, tag; 1350 memcpy(&len, p, sizeof(len)); 1351 p += 4; 1352 len = getu32(swap, len); 1353 if (memcmp("gnu", p, 3) != 0) { 1354 if (file_printf(ms, 1355 ", unknown capability %.3s", p) 1356 == -1) 1357 return -1; 1358 break; 1359 } 1360 p += strlen(p) + 1; 1361 tag = *p++; 1362 memcpy(&len, p, sizeof(len)); 1363 p += 4; 1364 len = getu32(swap, len); 1365 if (tag != 1) { 1366 if (file_printf(ms, ", unknown gnu" 1367 " capability tag %d", tag) 1368 == -1) 1369 return -1; 1370 break; 1371 } 1372 // gnu attributes 1373 #endif 1374 break; 1375 } 1376 (void)memcpy(xcap_addr, cbuf, xcap_sizeof); 1377 switch (xcap_tag) { 1378 case CA_SUNW_NULL: 1379 break; 1380 case CA_SUNW_HW_1: 1381 cap_hw1 |= xcap_val; 1382 break; 1383 case CA_SUNW_SF_1: 1384 cap_sf1 |= xcap_val; 1385 break; 1386 default: 1387 if (file_printf(ms, 1388 ", with unknown capability " 1389 "%#" INT64_T_FORMAT "x = %#" 1390 INT64_T_FORMAT "x", 1391 (unsigned long long)xcap_tag, 1392 (unsigned long long)xcap_val) == -1) 1393 return -1; 1394 if (nbadcap++ > 2) 1395 coff = xsh_size; 1396 break; 1397 } 1398 } 1399 /*FALLTHROUGH*/ 1400 skip: 1401 default: 1402 break; 1403 } 1404 } 1405 1406 if (has_debug_info) { 1407 if (file_printf(ms, ", with debug_info") == -1) 1408 return -1; 1409 } 1410 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1) 1411 return -1; 1412 if (cap_hw1) { 1413 const cap_desc_t *cdp; 1414 switch (mach) { 1415 case EM_SPARC: 1416 case EM_SPARC32PLUS: 1417 case EM_SPARCV9: 1418 cdp = cap_desc_sparc; 1419 break; 1420 case EM_386: 1421 case EM_IA_64: 1422 case EM_AMD64: 1423 cdp = cap_desc_386; 1424 break; 1425 default: 1426 cdp = NULL; 1427 break; 1428 } 1429 if (file_printf(ms, ", uses") == -1) 1430 return -1; 1431 if (cdp) { 1432 while (cdp->cd_name) { 1433 if (cap_hw1 & cdp->cd_mask) { 1434 if (file_printf(ms, 1435 " %s", cdp->cd_name) == -1) 1436 return -1; 1437 cap_hw1 &= ~cdp->cd_mask; 1438 } 1439 ++cdp; 1440 } 1441 if (cap_hw1) 1442 if (file_printf(ms, 1443 " unknown hardware capability %#" 1444 INT64_T_FORMAT "x", 1445 (unsigned long long)cap_hw1) == -1) 1446 return -1; 1447 } else { 1448 if (file_printf(ms, 1449 " hardware capability %#" INT64_T_FORMAT "x", 1450 (unsigned long long)cap_hw1) == -1) 1451 return -1; 1452 } 1453 } 1454 if (cap_sf1) { 1455 if (cap_sf1 & SF1_SUNW_FPUSED) { 1456 if (file_printf(ms, 1457 (cap_sf1 & SF1_SUNW_FPKNWN) 1458 ? ", uses frame pointer" 1459 : ", not known to use frame pointer") == -1) 1460 return -1; 1461 } 1462 cap_sf1 &= ~SF1_SUNW_MASK; 1463 if (cap_sf1) 1464 if (file_printf(ms, 1465 ", with unknown software capability %#" 1466 INT64_T_FORMAT "x", 1467 (unsigned long long)cap_sf1) == -1) 1468 return -1; 1469 } 1470 return 0; 1471 } 1472 1473 /* 1474 * Look through the program headers of an executable image, searching 1475 * for a PT_INTERP section; if one is found, it's dynamically linked, 1476 * otherwise it's statically linked. 1477 */ 1478 private int 1479 dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off, 1480 int num, size_t size, off_t fsize, int sh_num, int *flags, 1481 uint16_t *notecount) 1482 { 1483 Elf32_Phdr ph32; 1484 Elf64_Phdr ph64; 1485 const char *linking_style = "statically"; 1486 const char *interp = ""; 1487 unsigned char nbuf[BUFSIZ]; 1488 char ibuf[BUFSIZ]; 1489 ssize_t bufsize; 1490 size_t offset, align, len; 1491 1492 if (size != xph_sizeof) { 1493 if (file_printf(ms, ", corrupted program header size") == -1) 1494 return -1; 1495 return 0; 1496 } 1497 1498 for ( ; num; num--) { 1499 if (pread(fd, xph_addr, xph_sizeof, off) < (ssize_t)xph_sizeof) { 1500 file_badread(ms); 1501 return -1; 1502 } 1503 1504 off += size; 1505 bufsize = 0; 1506 align = 4; 1507 1508 /* Things we can determine before we seek */ 1509 switch (xph_type) { 1510 case PT_DYNAMIC: 1511 linking_style = "dynamically"; 1512 break; 1513 case PT_NOTE: 1514 if (sh_num) /* Did this through section headers */ 1515 continue; 1516 if (((align = xph_align) & 0x80000000UL) != 0 || 1517 align < 4) { 1518 if (file_printf(ms, 1519 ", invalid note alignment %#lx", 1520 (unsigned long)align) == -1) 1521 return -1; 1522 align = 4; 1523 } 1524 /*FALLTHROUGH*/ 1525 case PT_INTERP: 1526 len = xph_filesz < sizeof(nbuf) ? xph_filesz 1527 : sizeof(nbuf); 1528 bufsize = pread(fd, nbuf, len, xph_offset); 1529 if (bufsize == -1) { 1530 file_badread(ms); 1531 return -1; 1532 } 1533 break; 1534 default: 1535 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) { 1536 /* Maybe warn here? */ 1537 continue; 1538 } 1539 break; 1540 } 1541 1542 /* Things we can determine when we seek */ 1543 switch (xph_type) { 1544 case PT_INTERP: 1545 if (bufsize && nbuf[0]) { 1546 nbuf[bufsize - 1] = '\0'; 1547 interp = (const char *)nbuf; 1548 } else 1549 interp = "*empty*"; 1550 break; 1551 case PT_NOTE: 1552 /* 1553 * This is a PT_NOTE section; loop through all the notes 1554 * in the section. 1555 */ 1556 offset = 0; 1557 for (;;) { 1558 if (offset >= (size_t)bufsize) 1559 break; 1560 offset = donote(ms, nbuf, offset, 1561 (size_t)bufsize, clazz, swap, align, 1562 flags, notecount, fd, 0, 0, 0); 1563 if (offset == 0) 1564 break; 1565 } 1566 break; 1567 default: 1568 break; 1569 } 1570 } 1571 if (file_printf(ms, ", %s linked", linking_style) 1572 == -1) 1573 return -1; 1574 if (interp[0]) 1575 if (file_printf(ms, ", interpreter %s", 1576 file_printable(ibuf, sizeof(ibuf), interp)) == -1) 1577 return -1; 1578 return 0; 1579 } 1580 1581 1582 protected int 1583 file_tryelf(struct magic_set *ms, const struct buffer *b) 1584 { 1585 int fd = b->fd; 1586 const unsigned char *buf = b->fbuf; 1587 size_t nbytes = b->flen; 1588 union { 1589 int32_t l; 1590 char c[sizeof (int32_t)]; 1591 } u; 1592 int clazz; 1593 int swap; 1594 struct stat st; 1595 off_t fsize; 1596 int flags = 0; 1597 Elf32_Ehdr elf32hdr; 1598 Elf64_Ehdr elf64hdr; 1599 uint16_t type, phnum, shnum, notecount; 1600 1601 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE|MAGIC_EXTENSION)) 1602 return 0; 1603 /* 1604 * ELF executables have multiple section headers in arbitrary 1605 * file locations and thus file(1) cannot determine it from easily. 1606 * Instead we traverse thru all section headers until a symbol table 1607 * one is found or else the binary is stripped. 1608 * Return immediately if it's not ELF (so we avoid pipe2file unless needed). 1609 */ 1610 if (buf[EI_MAG0] != ELFMAG0 1611 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1) 1612 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3) 1613 return 0; 1614 1615 /* 1616 * If we cannot seek, it must be a pipe, socket or fifo. 1617 */ 1618 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE)) 1619 fd = file_pipe2file(ms, fd, buf, nbytes); 1620 1621 if (fstat(fd, &st) == -1) { 1622 file_badread(ms); 1623 return -1; 1624 } 1625 if (S_ISREG(st.st_mode) || st.st_size != 0) 1626 fsize = st.st_size; 1627 else 1628 fsize = SIZE_UNKNOWN; 1629 1630 clazz = buf[EI_CLASS]; 1631 1632 switch (clazz) { 1633 case ELFCLASS32: 1634 #undef elf_getu 1635 #define elf_getu(a, b) elf_getu32(a, b) 1636 #undef elfhdr 1637 #define elfhdr elf32hdr 1638 #include "elfclass.h" 1639 case ELFCLASS64: 1640 #undef elf_getu 1641 #define elf_getu(a, b) elf_getu64(a, b) 1642 #undef elfhdr 1643 #define elfhdr elf64hdr 1644 #include "elfclass.h" 1645 default: 1646 if (file_printf(ms, ", unknown class %d", clazz) == -1) 1647 return -1; 1648 break; 1649 } 1650 return 0; 1651 } 1652 #endif 1653