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