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