mgelf.c (14689B)
1 /* see LICENSE file for copyright and license details */ 2 #ifndef _POSIX_C_SOURCE 3 #define _POSIX_C_SOURCE 200809L 4 #endif 5 6 #include "mgint.h" 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <string.h> 11 12 /* elf details file(1) reads from the headers rather than with magic tests */ 13 14 #define NBUFSIZE 2024 15 #define NOTES_MAX 256 16 #define PHNUM_MAX 2048 17 #define SHNUM_MAX 32768 18 #define SHSIZE_MAX (128 * 1024 * 1024) 19 20 #define ET_REL 1 21 #define ET_EXEC 2 22 #define ET_DYN 3 23 24 #define PT_DYNAMIC 2 25 #define PT_INTERP 3 26 #define PT_NOTE 4 27 28 #define SHT_SYMTAB 2 29 #define SHT_NOTE 7 30 31 #define DT_NEEDED 1 32 #define DT_FLAGS_1 0x6ffffffb 33 #define DF_1_PIE 0x08000000 34 35 #define NT_GNU_VERSION 1 36 #define NT_GNU_BUILD_ID 3 37 #define NT_GO_BUILD_ID 4 38 39 #define GNU_OS_LINUX 0 40 #define GNU_OS_HURD 1 41 #define GNU_OS_SOLARIS 2 42 #define GNU_OS_KFREEBSD 3 43 #define GNU_OS_KNETBSD 4 44 45 enum ElfFlag { EF_DID_OS_NOTE = 1 << 0, EF_DID_BUILD_ID = 1 << 1 }; 46 47 struct Elf { 48 struct MagicRun *r; 49 struct MagicOut *o; 50 int is64; 51 int be; 52 long long fsize; /* -1 when unknown */ 53 int flags; 54 unsigned notes; /* notes still allowed */ 55 }; 56 57 static uint32_t 58 get32(const struct Elf *e, const unsigned char *p) 59 { 60 if (e->be) 61 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; 62 return (uint32_t)p[3] << 24 | (uint32_t)p[2] << 16 | (uint32_t)p[1] << 8 | p[0]; 63 } 64 65 static uint16_t 66 get16(const struct Elf *e, const unsigned char *p) 67 { 68 return e->be ? (uint16_t)(p[0] << 8 | p[1]) : (uint16_t)(p[1] << 8 | p[0]); 69 } 70 71 static uint64_t 72 get64(const struct Elf *e, const unsigned char *p) 73 { 74 if (e->be) 75 return (uint64_t)get32(e, p) << 32 | get32(e, p + 4); 76 return (uint64_t)get32(e, p + 4) << 32 | get32(e, p); 77 } 78 79 /* an address sized field: 4 bytes in elf32, 8 in elf64 */ 80 static uint64_t 81 getaddr(const struct Elf *e, const unsigned char *p32, const unsigned char *p64) 82 { 83 return e->is64 ? get64(e, p64) : get32(e, p32); 84 } 85 86 static void 87 eprint(struct Elf *e, const char *fmt, long long v) 88 { 89 if (!e->r->mime) 90 mg_printf(e->o, fmt, v); 91 } 92 93 /* a buffer may hold only the head of the file, so what lies past it is unknown, not missing */ 94 static void 95 eunread(struct Elf *e, const char *fmt, long long v) 96 { 97 if (e->r->fd >= 0) 98 eprint(e, fmt, v); 99 } 100 101 static void 102 eputs(struct Elf *e, const char *s) 103 { 104 if (!e->r->mime) 105 mg_puts(e->o, s); 106 } 107 108 static int 109 nameis(const unsigned char *n, uint32_t namesz, const char *v) 110 { 111 return namesz == strlen(v) + 1 && memcmp(n, v, namesz) == 0; 112 } 113 114 static int 115 os_note( 116 struct Elf *e, 117 const unsigned char *nb, 118 uint32_t type, 119 uint32_t namesz, 120 uint32_t descsz, 121 size_t noff, 122 size_t doff 123 ) 124 { 125 static const char *gnu_os[] = {"Linux", "Hurd", "Solaris", "kFreeBSD", "kNetBSD"}; 126 const unsigned char *name = nb + noff, *d = nb + doff; 127 char buf[160]; 128 uint32_t os, v; 129 130 if (nameis(name, namesz, "SuSE") && type == NT_GNU_VERSION && descsz == 2) { 131 e->flags |= EF_DID_OS_NOTE; 132 snprintf(buf, sizeof(buf), ", for SuSE %d.%d", d[0], d[1]); 133 eputs(e, buf); 134 return 1; 135 } 136 if (nameis(name, namesz, "GNU") && type == NT_GNU_VERSION && descsz == 16) { 137 e->flags |= EF_DID_OS_NOTE; 138 os = get32(e, d); 139 snprintf( 140 buf, 141 sizeof(buf), 142 ", for GNU/%s %d.%d.%d", 143 os <= GNU_OS_KNETBSD ? gnu_os[os] : "<unknown>", 144 (int)get32(e, d + 4), 145 (int)get32(e, d + 8), 146 (int)get32(e, d + 12) 147 ); 148 eputs(e, buf); 149 return 1; 150 } 151 if (nameis(name, namesz, "OpenBSD") && type == 1 && descsz == 4) { 152 e->flags |= EF_DID_OS_NOTE; 153 eputs(e, ", for OpenBSD"); 154 return 1; 155 } 156 if (nameis(name, namesz, "DragonFly") && type == 1 && descsz == 4) { 157 e->flags |= EF_DID_OS_NOTE; 158 v = get32(e, d); 159 snprintf( 160 buf, 161 sizeof(buf), 162 ", for DragonFly %d.%d.%d", 163 (int)(v / 100000), 164 (int)(v / 10000 % 10), 165 (int)(v % 10000) 166 ); 167 eputs(e, buf); 168 return 1; 169 } 170 if (nameis(name, namesz, "Android") && type == 1 && descsz >= 4) { 171 e->flags |= EF_DID_OS_NOTE; 172 snprintf(buf, sizeof(buf), ", for Android %d", (int)get32(e, d)); 173 eputs(e, buf); 174 if (descsz >= 4 + 64 + 64) { 175 snprintf( 176 buf, 177 sizeof(buf), 178 ", built by NDK %.64s (%.64s)", 179 (const char *)d + 4, 180 (const char *)d + 4 + 64 181 ); 182 eputs(e, buf); 183 } 184 } 185 return 0; 186 } 187 188 static int 189 bid_note( 190 struct Elf *e, 191 const unsigned char *nb, 192 uint32_t type, 193 uint32_t namesz, 194 uint32_t descsz, 195 size_t noff, 196 size_t doff 197 ) 198 { 199 char buf[300], hex[4]; 200 uint32_t i; 201 202 if (nameis(nb + noff, namesz, "GNU") && type == NT_GNU_BUILD_ID && descsz >= 4 && descsz <= 20) { 203 e->flags |= EF_DID_BUILD_ID; 204 snprintf( 205 buf, 206 sizeof(buf), 207 ", BuildID[%s]=", 208 descsz == 8 ? "xxHash" 209 : descsz == 16 ? "md5/uuid" 210 : descsz == 20 ? "sha1" 211 : "unknown" 212 ); 213 eputs(e, buf); 214 for (i = 0; i < descsz; i++) { 215 snprintf(hex, sizeof(hex), "%02x", nb[doff + i]); 216 eputs(e, hex); 217 } 218 return 1; 219 } 220 if (namesz == 4 && memcmp(nb + noff, "Go", 3) == 0 && type == NT_GO_BUILD_ID && descsz < 128) { 221 memcpy(buf, nb + doff, descsz); 222 buf[descsz] = '\0'; 223 eputs(e, ", Go BuildID="); 224 eputs(e, buf); 225 return 1; 226 } 227 return 0; 228 } 229 230 static size_t 231 elf_align(size_t a, size_t align) 232 { 233 return (a + align - 1) / align * align; 234 } 235 236 /* one note at offset, returns where the next starts or 0 to stop */ 237 static size_t 238 donote(struct Elf *e, const unsigned char *nb, size_t offset, size_t size, size_t align) 239 { 240 uint32_t namesz, descsz, type; 241 size_t noff, doff; 242 243 if (!e->notes) 244 return 0; 245 e->notes--; 246 if (12 + offset > size) 247 return 12 + offset; 248 namesz = get32(e, nb + offset); 249 descsz = get32(e, nb + offset + 4); 250 type = get32(e, nb + offset + 8); 251 offset += 12; 252 if (!namesz && !descsz) 253 return offset >= size ? offset : size; 254 if (namesz & 0x80000000) { 255 eprint(e, ", bad note name size %#llx", namesz); 256 return 0; 257 } 258 if (descsz & 0x80000000) { 259 eprint(e, ", bad note description size %#llx", descsz); 260 return 0; 261 } 262 noff = offset; 263 doff = elf_align(offset + namesz, align); 264 if (offset + namesz > size) 265 return doff; 266 offset = elf_align(doff + descsz, align); 267 if (doff + descsz > size) 268 return offset >= size ? offset : size; 269 if (!(e->flags & EF_DID_OS_NOTE) && os_note(e, nb, type, namesz, descsz, noff, doff)) 270 return offset; 271 if (!(e->flags & EF_DID_BUILD_ID) && bid_note(e, nb, type, namesz, descsz, noff, doff)) 272 return offset; 273 return offset; 274 } 275 276 static void 277 notes(struct Elf *e, const unsigned char *nb, size_t size, size_t align) 278 { 279 size_t off = 0; 280 281 while (off < size) { 282 if (!(off = donote(e, nb, off, size, align))) 283 break; 284 } 285 } 286 287 /* program headers: linking, interpreter, and notes when there are no sections */ 288 static int 289 dophn(struct Elf *e, long long off, unsigned num, size_t size, unsigned shnum) 290 { 291 unsigned char ph[56], nb[NBUFSIZE], dyn[16]; 292 char interp[NBUFSIZE]; 293 long long got, poff; 294 uint64_t tag, val; 295 size_t psize = e->is64 ? 56 : 32, dsize = e->is64 ? 16 : 8, align, len, i; 296 uint32_t type; 297 int dynamic = 0, pie = 0; 298 unsigned need = 0; 299 300 if (!num) { 301 eputs(e, ", no program header"); 302 return 0; 303 } 304 if (size != psize) { 305 eputs(e, ", corrupted program header size"); 306 return 0; 307 } 308 interp[0] = '\0'; 309 for (; num; num--, off += (long long)size) { 310 if (mg_pread(e->r, ph, psize, off) < (long long)psize) { 311 eunread(e, ", can't read elf program headers at %lld", off); 312 return 0; 313 } 314 type = get32(e, ph); 315 poff = (long long)getaddr(e, ph + 4, ph + 8); 316 len = (size_t)getaddr(e, ph + 16, ph + 32); 317 align = 4; 318 switch (type) { 319 case PT_NOTE: 320 /* the section headers say the same */ 321 if (shnum) 322 continue; 323 align = (size_t)getaddr(e, ph + 28, ph + 48); 324 if ((align & 0x80000000UL) || align < 4) { 325 eprint(e, ", invalid note alignment %#llx", (long long)align); 326 align = 4; 327 } 328 break; 329 case PT_DYNAMIC: 330 case PT_INTERP: 331 break; 332 default: 333 continue; 334 } 335 if (len > sizeof(nb)) 336 len = sizeof(nb); 337 if ((got = mg_pread(e->r, nb, len, poff)) < 0) { 338 eunread(e, ", can't read section at %lld", poff); 339 return 0; 340 } 341 switch (type) { 342 case PT_DYNAMIC: 343 dynamic = 1; 344 /* DF_1_PIE decides between pie executable and shared object */ 345 e->r->xmode = 0; 346 for (i = 0; i + dsize <= (size_t)got; i += dsize) { 347 memcpy(dyn, nb + i, dsize); 348 tag = getaddr(e, dyn, dyn); 349 val = getaddr(e, dyn + 4, dyn + 8); 350 if (tag == DT_FLAGS_1) { 351 pie = (val & DF_1_PIE) != 0; 352 e->r->xmode = pie; 353 } else if (tag == DT_NEEDED) { 354 need++; 355 } 356 } 357 break; 358 case PT_INTERP: 359 need++; 360 if (e->r->mime) 361 continue; 362 if (got && nb[0]) { 363 nb[got - 1] = '\0'; 364 memcpy(interp, nb, (size_t)got); 365 } else { 366 strcpy(interp, "*empty*"); 367 } 368 break; 369 case PT_NOTE: 370 if (e->r->mime) 371 return 0; 372 notes(e, nb, (size_t)got, align); 373 break; 374 default: 375 break; 376 } 377 } 378 if (e->r->mime) 379 return 0; 380 mg_puts( 381 e->o, 382 dynamic ? (pie && !need ? ", static-pie linked" : ", dynamically linked") 383 : ", statically linked" 384 ); 385 if (interp[0]) { 386 mg_printable((char *)nb, sizeof(nb), interp, sizeof(interp)); 387 mg_puts(e->o, ", interpreter "); 388 mg_puts(e->o, (char *)nb); 389 } 390 return 0; 391 } 392 393 /* section headers: symbol table, debug info and notes */ 394 static int 395 doshn(struct Elf *e, long long off, unsigned num, size_t size, unsigned strtab) 396 { 397 unsigned char sh[64], *nb; 398 char name[50]; 399 long long name_off, offs, n, soff; 400 size_t ssize = e->is64 ? 64 : 40, len; 401 uint32_t type; 402 int stripped = 1, debug = 0; 403 404 if (e->r->mime) 405 return 0; 406 if (!num) { 407 eputs(e, ", no section header"); 408 return 0; 409 } 410 if (size != ssize) { 411 eputs(e, ", corrupted section header size"); 412 return 0; 413 } 414 offs = off + (long long)(size * strtab); 415 if (mg_pread(e->r, sh, ssize, offs) < (long long)ssize) { 416 eunread(e, ", missing section headers at %lld", offs); 417 return 0; 418 } 419 name_off = (long long)getaddr(e, sh + 16, sh + 24); 420 if (e->fsize >= 0 && e->fsize < name_off) { 421 eprint(e, ", too large section header offset %lld", name_off); 422 return 0; 423 } 424 for (; num; num--) { 425 /* file(1) reads the name from the header before this one, the first time strtab */ 426 offs = name_off + get32(e, sh); 427 if ((n = mg_pread(e->r, name, sizeof(name) - 1, offs)) < 0) { 428 eunread(e, ", can't read name of elf section at %lld", offs); 429 return 0; 430 } 431 name[n] = '\0'; 432 if (strcmp(name, ".debug_info") == 0) { 433 debug = 1; 434 stripped = 0; 435 } 436 if (mg_pread(e->r, sh, ssize, off) < (long long)ssize) { 437 eunread(e, ", can't read elf section at %lld", off); 438 return 0; 439 } 440 off += (long long)size; 441 type = get32(e, sh + 4); 442 soff = (long long)getaddr(e, sh + 16, sh + 24); 443 len = (size_t)getaddr(e, sh + 20, sh + 32); 444 if (type == SHT_SYMTAB) { 445 stripped = 0; 446 continue; 447 } 448 if (e->fsize >= 0 && soff > e->fsize) 449 continue; 450 if (type != SHT_NOTE) 451 continue; 452 if ((unsigned long long)len + (unsigned long long)soff > (unsigned long long)e->fsize) { 453 if (!e->r->mime) { 454 mg_printf( 455 e->o, 456 ", note offset/size %#llx+%#llx exceeds file size %#llx", 457 (unsigned long long)soff, 458 (unsigned long long)len, 459 (unsigned long long)e->fsize 460 ); 461 } 462 return 0; 463 } 464 if (len > SHSIZE_MAX || !(nb = malloc(len ? len : 1))) 465 return -1; 466 if (mg_pread(e->r, nb, len, soff) < (long long)len) { 467 free(nb); 468 eunread(e, ", can't read elf note at %lld", soff); 469 return 0; 470 } 471 notes(e, nb, len, 4); 472 free(nb); 473 } 474 if (debug) 475 eputs(e, ", with debug_info"); 476 eputs(e, stripped ? ", stripped" : ", not stripped"); 477 return 0; 478 } 479 480 /* 481 * the elf details file(1) appends to the magic description: linking, 482 * interpreter, notes and stripping. also decides r->xmode for the pie 483 * test of the elf magic. 0 when the head is not elf 484 */ 485 int 486 mg_elf(struct MagicRun *r, struct MagicOut *o) 487 { 488 struct Elf e; 489 unsigned char h[64]; 490 unsigned type, phnum, shnum; 491 long long phoff, shoff; 492 493 if (r->head.n <= 5) 494 return 0; 495 if (r->head.p[0] != 0x7f || (r->head.p[1] != 'E' && r->head.p[1] != 'O') || r->head.p[2] != 'L' 496 || r->head.p[3] != 'F') 497 return 0; 498 memset(&e, 0, sizeof(e)); 499 e.r = r; 500 e.o = o; 501 e.fsize = r->fsize; 502 e.notes = NOTES_MAX; 503 memset(h, 0, sizeof(h)); 504 memcpy(h, r->head.p, r->head.n < sizeof(h) ? r->head.n : sizeof(h)); 505 switch (h[4]) { 506 case 1: 507 e.is64 = 0; 508 break; 509 case 2: 510 e.is64 = 1; 511 break; 512 default: 513 eprint(&e, ", unknown class %lld", h[4]); 514 return 1; 515 } 516 /* file(1) wants more than the elf header itself */ 517 if (r->head.n <= (e.is64 ? 64u : 52u)) 518 return 0; 519 e.be = h[5] != 1; 520 type = get16(&e, h + 16); 521 phoff = (long long)getaddr(&e, h + 28, h + 32); 522 shoff = (long long)getaddr(&e, h + 32, h + 40); 523 phnum = get16(&e, h + (e.is64 ? 56 : 44)); 524 shnum = get16(&e, h + (e.is64 ? 60 : 48)); 525 switch (type) { 526 case ET_EXEC: 527 case ET_DYN: 528 if (phnum > PHNUM_MAX) { 529 eprint(&e, ", too many program (%lld)", phnum); 530 return 1; 531 } 532 if (shnum > SHNUM_MAX) { 533 eprint(&e, ", too many section (%lld)", shnum); 534 return 1; 535 } 536 if (dophn(&e, phoff, phnum, get16(&e, h + (e.is64 ? 54 : 42)), shnum) < 0) 537 return -1; 538 /* fallthrough */ 539 case ET_REL: 540 if (shnum > SHNUM_MAX) { 541 eprint(&e, ", too many section headers (%lld)", shnum); 542 return 1; 543 } 544 if (doshn( 545 &e, shoff, shnum, get16(&e, h + (e.is64 ? 58 : 46)), get16(&e, h + (e.is64 ? 62 : 50)) 546 ) 547 < 0) 548 return -1; 549 break; 550 default: 551 break; 552 } 553 if (!e.notes) 554 eprint(&e, ", too many notes (%lld)", NOTES_MAX); 555 return 1; 556 }