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