Deleted Added
sdiff udiff text old ( 275666 ) new ( 275698 )
full compact
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
30FILE_RCSID("@(#)$File: readelf.c,v 1.111 2014/12/09 02:47:45 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
45private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
46 off_t, int *);
47#endif
48private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
49 off_t, int *, int);
50private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
51 off_t, int *, int, int);
52private size_t donote(struct magic_set *, void *, size_t, size_t, int,
53 int, size_t, int *);
54
55#define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
56
57#define isquote(c) (strchr("'\"`", (c)) != NULL)
58
59private uint16_t getu16(int, uint16_t);
60private uint32_t getu32(int, uint32_t);
61private uint64_t getu64(int, uint64_t);
62
63#define MAX_PHNUM 128
64#define MAX_SHNUM 32768
65#define SIZE_UNKNOWN ((off_t)-1)
66
67private int
68toomany(struct magic_set *ms, const char *name, uint16_t num)
69{
70 if (file_printf(ms, ", too many %s header sections (%u)", name, num
71 ) == -1)
72 return -1;
73 return 0;
74}
75
76private uint16_t
77getu16(int swap, uint16_t value)
78{
79 union {
80 uint16_t ui;
81 char c[2];
82 } retval, tmpval;
83
84 if (swap) {
85 tmpval.ui = value;
86
87 retval.c[0] = tmpval.c[1];
88 retval.c[1] = tmpval.c[0];
89
90 return retval.ui;
91 } else
92 return value;
93}
94
95private uint32_t
96getu32(int swap, uint32_t value)
97{
98 union {
99 uint32_t ui;
100 char c[4];
101 } retval, tmpval;
102
103 if (swap) {
104 tmpval.ui = value;
105
106 retval.c[0] = tmpval.c[3];
107 retval.c[1] = tmpval.c[2];
108 retval.c[2] = tmpval.c[1];
109 retval.c[3] = tmpval.c[0];
110
111 return retval.ui;
112 } else
113 return value;
114}
115
116private uint64_t
117getu64(int swap, uint64_t value)
118{
119 union {
120 uint64_t ui;
121 char c[8];
122 } retval, tmpval;
123
124 if (swap) {
125 tmpval.ui = value;
126
127 retval.c[0] = tmpval.c[7];
128 retval.c[1] = tmpval.c[6];
129 retval.c[2] = tmpval.c[5];
130 retval.c[3] = tmpval.c[4];
131 retval.c[4] = tmpval.c[3];
132 retval.c[5] = tmpval.c[2];
133 retval.c[6] = tmpval.c[1];
134 retval.c[7] = tmpval.c[0];
135
136 return retval.ui;
137 } else
138 return value;
139}
140
141#define elf_getu16(swap, value) getu16(swap, value)
142#define elf_getu32(swap, value) getu32(swap, value)
143#define elf_getu64(swap, value) getu64(swap, value)
144
145#define xsh_addr (clazz == ELFCLASS32 \
146 ? (void *)&sh32 \
147 : (void *)&sh64)
148#define xsh_sizeof (clazz == ELFCLASS32 \
149 ? sizeof(sh32) \
150 : sizeof(sh64))
151#define xsh_size (size_t)(clazz == ELFCLASS32 \
152 ? elf_getu32(swap, sh32.sh_size) \
153 : elf_getu64(swap, sh64.sh_size))
154#define xsh_offset (off_t)(clazz == ELFCLASS32 \
155 ? elf_getu32(swap, sh32.sh_offset) \
156 : elf_getu64(swap, sh64.sh_offset))
157#define xsh_type (clazz == ELFCLASS32 \
158 ? elf_getu32(swap, sh32.sh_type) \
159 : elf_getu32(swap, sh64.sh_type))
160#define xsh_name (clazz == ELFCLASS32 \
161 ? elf_getu32(swap, sh32.sh_name) \
162 : elf_getu32(swap, sh64.sh_name))
163#define xph_addr (clazz == ELFCLASS32 \
164 ? (void *) &ph32 \
165 : (void *) &ph64)
166#define xph_sizeof (clazz == ELFCLASS32 \
167 ? sizeof(ph32) \
168 : sizeof(ph64))
169#define xph_type (clazz == ELFCLASS32 \
170 ? elf_getu32(swap, ph32.p_type) \
171 : elf_getu32(swap, ph64.p_type))
172#define xph_offset (off_t)(clazz == ELFCLASS32 \
173 ? elf_getu32(swap, ph32.p_offset) \
174 : elf_getu64(swap, ph64.p_offset))
175#define xph_align (size_t)((clazz == ELFCLASS32 \
176 ? (off_t) (ph32.p_align ? \
177 elf_getu32(swap, ph32.p_align) : 4) \
178 : (off_t) (ph64.p_align ? \
179 elf_getu64(swap, ph64.p_align) : 4)))
180#define xph_filesz (size_t)((clazz == ELFCLASS32 \
181 ? elf_getu32(swap, ph32.p_filesz) \
182 : elf_getu64(swap, ph64.p_filesz)))
183#define xnh_addr (clazz == ELFCLASS32 \
184 ? (void *)&nh32 \
185 : (void *)&nh64)
186#define xph_memsz (size_t)((clazz == ELFCLASS32 \
187 ? elf_getu32(swap, ph32.p_memsz) \
188 : elf_getu64(swap, ph64.p_memsz)))
189#define xnh_sizeof (clazz == ELFCLASS32 \
190 ? sizeof nh32 \
191 : sizeof nh64)
192#define xnh_type (clazz == ELFCLASS32 \
193 ? elf_getu32(swap, nh32.n_type) \
194 : elf_getu32(swap, nh64.n_type))
195#define xnh_namesz (clazz == ELFCLASS32 \
196 ? elf_getu32(swap, nh32.n_namesz) \
197 : elf_getu32(swap, nh64.n_namesz))
198#define xnh_descsz (clazz == ELFCLASS32 \
199 ? elf_getu32(swap, nh32.n_descsz) \
200 : elf_getu32(swap, nh64.n_descsz))
201#define prpsoffsets(i) (clazz == ELFCLASS32 \
202 ? prpsoffsets32[i] \
203 : prpsoffsets64[i])
204#define xcap_addr (clazz == ELFCLASS32 \
205 ? (void *)&cap32 \
206 : (void *)&cap64)
207#define xcap_sizeof (clazz == ELFCLASS32 \
208 ? sizeof cap32 \
209 : sizeof cap64)
210#define xcap_tag (clazz == ELFCLASS32 \
211 ? elf_getu32(swap, cap32.c_tag) \
212 : elf_getu64(swap, cap64.c_tag))
213#define xcap_val (clazz == ELFCLASS32 \
214 ? elf_getu32(swap, cap32.c_un.c_val) \
215 : elf_getu64(swap, cap64.c_un.c_val))
216
217#ifdef ELFCORE
218/*
219 * Try larger offsets first to avoid false matches
220 * from earlier data that happen to look like strings.
221 */
222static const size_t prpsoffsets32[] = {
223#ifdef USE_NT_PSINFO
224 104, /* SunOS 5.x (command line) */
225 88, /* SunOS 5.x (short name) */
226#endif /* USE_NT_PSINFO */
227
228 100, /* SunOS 5.x (command line) */
229 84, /* SunOS 5.x (short name) */
230
231 44, /* Linux (command line) */
232 28, /* Linux 2.0.36 (short name) */
233
234 8, /* FreeBSD */
235};
236
237static const size_t prpsoffsets64[] = {
238#ifdef USE_NT_PSINFO
239 152, /* SunOS 5.x (command line) */
240 136, /* SunOS 5.x (short name) */
241#endif /* USE_NT_PSINFO */
242
243 136, /* SunOS 5.x, 64-bit (command line) */
244 120, /* SunOS 5.x, 64-bit (short name) */
245
246 56, /* Linux (command line) */
247 40, /* Linux (tested on core from 2.4.x, short name) */
248
249 16, /* FreeBSD, 64-bit */
250};
251
252#define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
253#define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
254
255#define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
256
257/*
258 * Look through the program headers of an executable image, searching
259 * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
260 * "FreeBSD"; if one is found, try looking in various places in its
261 * contents for a 16-character string containing only printable
262 * characters - if found, that string should be the name of the program
263 * that dropped core. Note: right after that 16-character string is,
264 * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
265 * Linux, a longer string (80 characters, in 5.x, probably other
266 * SVR4-flavored systems, and Linux) containing the start of the
267 * command line for that program.
268 *
269 * SunOS 5.x core files contain two PT_NOTE sections, with the types
270 * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
271 * same info about the command name and command line, so it probably
272 * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
273 * above (see USE_NT_PSINFO), in case we ever decide to do so. The
274 * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
275 * the SunOS 5.x file command relies on this (and prefers the latter).
276 *
277 * The signal number probably appears in a section of type NT_PRSTATUS,
278 * but that's also rather OS-dependent, in ways that are harder to
279 * dissect with heuristics, so I'm not bothering with the signal number.
280 * (I suppose the signal number could be of interest in situations where
281 * you don't have the binary of the program that dropped core; if you
282 * *do* have that binary, the debugger will probably tell you what
283 * signal it was.)
284 */
285
286#define OS_STYLE_SVR4 0
287#define OS_STYLE_FREEBSD 1
288#define OS_STYLE_NETBSD 2
289
290private const char os_style_names[][8] = {
291 "SVR4",
292 "FreeBSD",
293 "NetBSD",
294};
295
296#define FLAGS_DID_CORE 0x01
297#define FLAGS_DID_NOTE 0x02
298#define FLAGS_DID_BUILD_ID 0x04
299#define FLAGS_DID_CORE_STYLE 0x08
300#define FLAGS_IS_CORE 0x10
301
302private int
303dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
304 int num, size_t size, off_t fsize, int *flags)
305{
306 Elf32_Phdr ph32;
307 Elf64_Phdr ph64;
308 size_t offset, len;
309 unsigned char nbuf[BUFSIZ];
310 ssize_t bufsize;
311
312 if (size != xph_sizeof) {
313 if (file_printf(ms, ", corrupted program header size") == -1)
314 return -1;
315 return 0;
316 }
317
318 /*
319 * Loop through all the program headers.
320 */
321 for ( ; num; num--) {
322 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
323 file_badread(ms);
324 return -1;
325 }
326 off += size;
327
328 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
329 /* Perhaps warn here */
330 continue;
331 }
332
333 if (xph_type != PT_NOTE)
334 continue;
335
336 /*
337 * This is a PT_NOTE section; loop through all the notes
338 * in the section.
339 */
340 len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
341 if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
342 file_badread(ms);
343 return -1;
344 }
345 offset = 0;
346 for (;;) {
347 if (offset >= (size_t)bufsize)
348 break;
349 offset = donote(ms, nbuf, offset, (size_t)bufsize,
350 clazz, swap, 4, flags);
351 if (offset == 0)
352 break;
353
354 }
355 }
356 return 0;
357}
358#endif
359
360static void
361do_note_netbsd_version(struct magic_set *ms, int swap, void *v)
362{
363 uint32_t desc;
364 (void)memcpy(&desc, v, sizeof(desc));
365 desc = elf_getu32(swap, desc);
366
367 if (file_printf(ms, ", for NetBSD") == -1)
368 return;
369 /*
370 * The version number used to be stuck as 199905, and was thus
371 * basically content-free. Newer versions of NetBSD have fixed
372 * this and now use the encoding of __NetBSD_Version__:
373 *
374 * MMmmrrpp00
375 *
376 * M = major version
377 * m = minor version
378 * r = release ["",A-Z,Z[A-Z] but numeric]
379 * p = patchlevel
380 */
381 if (desc > 100000000U) {
382 uint32_t ver_patch = (desc / 100) % 100;
383 uint32_t ver_rel = (desc / 10000) % 100;
384 uint32_t ver_min = (desc / 1000000) % 100;
385 uint32_t ver_maj = desc / 100000000;
386
387 if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
388 return;
389 if (ver_rel == 0 && ver_patch != 0) {
390 if (file_printf(ms, ".%u", ver_patch) == -1)
391 return;
392 } else if (ver_rel != 0) {
393 while (ver_rel > 26) {
394 if (file_printf(ms, "Z") == -1)
395 return;
396 ver_rel -= 26;
397 }
398 if (file_printf(ms, "%c", 'A' + ver_rel - 1)
399 == -1)
400 return;
401 }
402 }
403}
404
405static void
406do_note_freebsd_version(struct magic_set *ms, int swap, void *v)
407{
408 uint32_t desc;
409
410 (void)memcpy(&desc, v, sizeof(desc));
411 desc = elf_getu32(swap, desc);
412 if (file_printf(ms, ", for FreeBSD") == -1)
413 return;
414
415 /*
416 * Contents is __FreeBSD_version, whose relation to OS
417 * versions is defined by a huge table in the Porter's
418 * Handbook. This is the general scheme:
419 *
420 * Releases:
421 * Mmp000 (before 4.10)
422 * Mmi0p0 (before 5.0)
423 * Mmm0p0
424 *
425 * Development branches:
426 * Mmpxxx (before 4.6)
427 * Mmp1xx (before 4.10)
428 * Mmi1xx (before 5.0)
429 * M000xx (pre-M.0)
430 * Mmm1xx
431 *
432 * M = major version
433 * m = minor version
434 * i = minor version increment (491000 -> 4.10)
435 * p = patchlevel
436 * x = revision
437 *
438 * The first release of FreeBSD to use ELF by default
439 * was version 3.0.
440 */
441 if (desc == 460002) {
442 if (file_printf(ms, " 4.6.2") == -1)
443 return;
444 } else if (desc < 460100) {
445 if (file_printf(ms, " %d.%d", desc / 100000,
446 desc / 10000 % 10) == -1)
447 return;
448 if (desc / 1000 % 10 > 0)
449 if (file_printf(ms, ".%d", desc / 1000 % 10) == -1)
450 return;
451 if ((desc % 1000 > 0) || (desc % 100000 == 0))
452 if (file_printf(ms, " (%d)", desc) == -1)
453 return;
454 } else if (desc < 500000) {
455 if (file_printf(ms, " %d.%d", desc / 100000,
456 desc / 10000 % 10 + desc / 1000 % 10) == -1)
457 return;
458 if (desc / 100 % 10 > 0) {
459 if (file_printf(ms, " (%d)", desc) == -1)
460 return;
461 } else if (desc / 10 % 10 > 0) {
462 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
463 return;
464 }
465 } else {
466 if (file_printf(ms, " %d.%d", desc / 100000,
467 desc / 1000 % 100) == -1)
468 return;
469 if ((desc / 100 % 10 > 0) ||
470 (desc % 100000 / 100 == 0)) {
471 if (file_printf(ms, " (%d)", desc) == -1)
472 return;
473 } else if (desc / 10 % 10 > 0) {
474 if (file_printf(ms, ".%d", desc / 10 % 10) == -1)
475 return;
476 }
477 }
478}
479
480private size_t
481donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
482 int clazz, int swap, size_t align, int *flags)
483{
484 Elf32_Nhdr nh32;
485 Elf64_Nhdr nh64;
486 size_t noff, doff;
487#ifdef ELFCORE
488 int os_style = -1;
489#endif
490 uint32_t namesz, descsz;
491 unsigned char *nbuf = CAST(unsigned char *, vbuf);
492
493 if (xnh_sizeof + offset > size) {
494 /*
495 * We're out of note headers.
496 */
497 return xnh_sizeof + offset;
498 }
499
500 (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
501 offset += xnh_sizeof;
502
503 namesz = xnh_namesz;
504 descsz = xnh_descsz;
505 if ((namesz == 0) && (descsz == 0)) {
506 /*
507 * We're out of note headers.
508 */
509 return (offset >= size) ? offset : size;
510 }
511
512 if (namesz & 0x80000000) {
513 (void)file_printf(ms, ", bad note name size 0x%lx",
514 (unsigned long)namesz);
515 return 0;
516 }
517
518 if (descsz & 0x80000000) {
519 (void)file_printf(ms, ", bad note description size 0x%lx",
520 (unsigned long)descsz);
521 return 0;
522 }
523
524
525 noff = offset;
526 doff = ELF_ALIGN(offset + namesz);
527
528 if (offset + namesz > size) {
529 /*
530 * We're past the end of the buffer.
531 */
532 return doff;
533 }
534
535 offset = ELF_ALIGN(doff + descsz);
536 if (doff + descsz > size) {
537 /*
538 * We're past the end of the buffer.
539 */
540 return (offset >= size) ? offset : size;
541 }
542
543 if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
544 (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
545 goto core;
546
547 if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
548 xnh_type == NT_GNU_VERSION && descsz == 2) {
549 file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
550 }
551 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
552 xnh_type == NT_GNU_VERSION && descsz == 16) {
553 uint32_t desc[4];
554 (void)memcpy(desc, &nbuf[doff], sizeof(desc));
555
556 if (file_printf(ms, ", for GNU/") == -1)
557 return size;
558 switch (elf_getu32(swap, desc[0])) {
559 case GNU_OS_LINUX:
560 if (file_printf(ms, "Linux") == -1)
561 return size;
562 break;
563 case GNU_OS_HURD:
564 if (file_printf(ms, "Hurd") == -1)
565 return size;
566 break;
567 case GNU_OS_SOLARIS:
568 if (file_printf(ms, "Solaris") == -1)
569 return size;
570 break;
571 case GNU_OS_KFREEBSD:
572 if (file_printf(ms, "kFreeBSD") == -1)
573 return size;
574 break;
575 case GNU_OS_KNETBSD:
576 if (file_printf(ms, "kNetBSD") == -1)
577 return size;
578 break;
579 default:
580 if (file_printf(ms, "<unknown>") == -1)
581 return size;
582 }
583 if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
584 elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
585 return size;
586 *flags |= FLAGS_DID_NOTE;
587 return size;
588 }
589
590 if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
591 xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
592 uint8_t desc[20];
593 uint32_t i;
594 if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
595 "sha1") == -1)
596 return size;
597 (void)memcpy(desc, &nbuf[doff], descsz);
598 for (i = 0; i < descsz; i++)
599 if (file_printf(ms, "%02x", desc[i]) == -1)
600 return size;
601 *flags |= FLAGS_DID_BUILD_ID;
602 }
603
604 if (namesz == 4 && strcmp((char *)&nbuf[noff], "PaX") == 0 &&
605 xnh_type == NT_NETBSD_PAX && descsz == 4) {
606 static const char *pax[] = {
607 "+mprotect",
608 "-mprotect",
609 "+segvguard",
610 "-segvguard",
611 "+ASLR",
612 "-ASLR",
613 };
614 uint32_t desc;
615 size_t i;
616 int did = 0;
617
618 (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
619 desc = elf_getu32(swap, desc);
620
621 if (desc && file_printf(ms, ", PaX: ") == -1)
622 return size;
623
624 for (i = 0; i < __arraycount(pax); i++) {
625 if (((1 << i) & desc) == 0)
626 continue;
627 if (file_printf(ms, "%s%s", did++ ? "," : "",
628 pax[i]) == -1)
629 return size;
630 }
631 }
632
633 if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0) {
634 switch (xnh_type) {
635 case NT_NETBSD_VERSION:
636 if (descsz == 4) {
637 do_note_netbsd_version(ms, swap, &nbuf[doff]);
638 *flags |= FLAGS_DID_NOTE;
639 return size;
640 }
641 break;
642 case NT_NETBSD_MARCH:
643 if (file_printf(ms, ", compiled for: %.*s", (int)descsz,
644 (const char *)&nbuf[doff]) == -1)
645 return size;
646 break;
647 case NT_NETBSD_CMODEL:
648 if (file_printf(ms, ", compiler model: %.*s",
649 (int)descsz, (const char *)&nbuf[doff]) == -1)
650 return size;
651 break;
652 default:
653 if (file_printf(ms, ", note=%u", xnh_type) == -1)
654 return size;
655 break;
656 }
657 return size;
658 }
659
660 if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0) {
661 if (xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
662 do_note_freebsd_version(ms, swap, &nbuf[doff]);
663 *flags |= FLAGS_DID_NOTE;
664 return size;
665 }
666 }
667
668 if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
669 xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
670 if (file_printf(ms, ", for OpenBSD") == -1)
671 return size;
672 /* Content of note is always 0 */
673 *flags |= FLAGS_DID_NOTE;
674 return size;
675 }
676
677 if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
678 xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
679 uint32_t desc;
680 if (file_printf(ms, ", for DragonFly") == -1)
681 return size;
682 (void)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 size;
687 *flags |= FLAGS_DID_NOTE;
688 return size;
689 }
690
691core:
692 /*
693 * Sigh. The 2.0.36 kernel in Debian 2.1, at
694 * least, doesn't correctly implement name
695 * sections, in core dumps, as specified by
696 * the "Program Linking" section of "UNIX(R) System
697 * V Release 4 Programmer's Guide: ANSI C and
698 * Programming Support Tools", because my copy
699 * clearly says "The first 'namesz' bytes in 'name'
700 * contain a *null-terminated* [emphasis mine]
701 * character representation of the entry's owner
702 * or originator", but the 2.0.36 kernel code
703 * doesn't include the terminating null in the
704 * name....
705 */
706 if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
707 (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
708 os_style = OS_STYLE_SVR4;
709 }
710
711 if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
712 os_style = OS_STYLE_FREEBSD;
713 }
714
715 if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
716 == 0)) {
717 os_style = OS_STYLE_NETBSD;
718 }
719
720#ifdef ELFCORE
721 if ((*flags & FLAGS_DID_CORE) != 0)
722 return size;
723
724 if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
725 if (file_printf(ms, ", %s-style", os_style_names[os_style])
726 == -1)
727 return size;
728 *flags |= FLAGS_DID_CORE_STYLE;
729 }
730
731 switch (os_style) {
732 case OS_STYLE_NETBSD:
733 if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
734 uint32_t signo;
735 /*
736 * Extract the program name. It is at
737 * offset 0x7c, and is up to 32-bytes,
738 * including the terminating NUL.
739 */
740 if (file_printf(ms, ", from '%.31s'",
741 &nbuf[doff + 0x7c]) == -1)
742 return size;
743
744 /*
745 * Extract the signal number. It is at
746 * offset 0x08.
747 */
748 (void)memcpy(&signo, &nbuf[doff + 0x08],
749 sizeof(signo));
750 if (file_printf(ms, " (signal %u)",
751 elf_getu32(swap, signo)) == -1)
752 return size;
753 *flags |= FLAGS_DID_CORE;
754 return size;
755 }
756 break;
757
758 default:
759 if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
760 size_t i, j;
761 unsigned char c;
762 /*
763 * Extract the program name. We assume
764 * it to be 16 characters (that's what it
765 * is in SunOS 5.x and Linux).
766 *
767 * Unfortunately, it's at a different offset
768 * in various OSes, so try multiple offsets.
769 * If the characters aren't all printable,
770 * reject it.
771 */
772 for (i = 0; i < NOFFSETS; i++) {
773 unsigned char *cname, *cp;
774 size_t reloffset = prpsoffsets(i);
775 size_t noffset = doff + reloffset;
776 size_t k;
777 for (j = 0; j < 16; j++, noffset++,
778 reloffset++) {
779 /*
780 * Make sure we're not past
781 * the end of the buffer; if
782 * we are, just give up.
783 */
784 if (noffset >= size)
785 goto tryanother;
786
787 /*
788 * Make sure we're not past
789 * the end of the contents;
790 * if we are, this obviously
791 * isn't the right offset.
792 */
793 if (reloffset >= descsz)
794 goto tryanother;
795
796 c = nbuf[noffset];
797 if (c == '\0') {
798 /*
799 * A '\0' at the
800 * beginning is
801 * obviously wrong.
802 * Any other '\0'
803 * means we're done.
804 */
805 if (j == 0)
806 goto tryanother;
807 else
808 break;
809 } else {
810 /*
811 * A nonprintable
812 * character is also
813 * wrong.
814 */
815 if (!isprint(c) || isquote(c))
816 goto tryanother;
817 }
818 }
819 /*
820 * Well, that worked.
821 */
822
823 /*
824 * Try next offsets, in case this match is
825 * in the middle of a string.
826 */
827 for (k = i + 1 ; k < NOFFSETS ; k++) {
828 size_t no;
829 int adjust = 1;
830 if (prpsoffsets(k) >= prpsoffsets(i))
831 continue;
832 for (no = doff + prpsoffsets(k);
833 no < doff + prpsoffsets(i); no++)
834 adjust = adjust
835 && isprint(nbuf[no]);
836 if (adjust)
837 i = k;
838 }
839
840 cname = (unsigned char *)
841 &nbuf[doff + prpsoffsets(i)];
842 for (cp = cname; *cp && isprint(*cp); cp++)
843 continue;
844 /*
845 * Linux apparently appends a space at the end
846 * of the command line: remove it.
847 */
848 while (cp > cname && isspace(cp[-1]))
849 cp--;
850 if (file_printf(ms, ", from '%.*s'",
851 (int)(cp - cname), cname) == -1)
852 return size;
853 *flags |= FLAGS_DID_CORE;
854 return size;
855
856 tryanother:
857 ;
858 }
859 }
860 break;
861 }
862#endif
863 return offset;
864}
865
866/* SunOS 5.x hardware capability descriptions */
867typedef struct cap_desc {
868 uint64_t cd_mask;
869 const char *cd_name;
870} cap_desc_t;
871
872static const cap_desc_t cap_desc_sparc[] = {
873 { AV_SPARC_MUL32, "MUL32" },
874 { AV_SPARC_DIV32, "DIV32" },
875 { AV_SPARC_FSMULD, "FSMULD" },
876 { AV_SPARC_V8PLUS, "V8PLUS" },
877 { AV_SPARC_POPC, "POPC" },
878 { AV_SPARC_VIS, "VIS" },
879 { AV_SPARC_VIS2, "VIS2" },
880 { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
881 { AV_SPARC_FMAF, "FMAF" },
882 { AV_SPARC_FJFMAU, "FJFMAU" },
883 { AV_SPARC_IMA, "IMA" },
884 { 0, NULL }
885};
886
887static const cap_desc_t cap_desc_386[] = {
888 { AV_386_FPU, "FPU" },
889 { AV_386_TSC, "TSC" },
890 { AV_386_CX8, "CX8" },
891 { AV_386_SEP, "SEP" },
892 { AV_386_AMD_SYSC, "AMD_SYSC" },
893 { AV_386_CMOV, "CMOV" },
894 { AV_386_MMX, "MMX" },
895 { AV_386_AMD_MMX, "AMD_MMX" },
896 { AV_386_AMD_3DNow, "AMD_3DNow" },
897 { AV_386_AMD_3DNowx, "AMD_3DNowx" },
898 { AV_386_FXSR, "FXSR" },
899 { AV_386_SSE, "SSE" },
900 { AV_386_SSE2, "SSE2" },
901 { AV_386_PAUSE, "PAUSE" },
902 { AV_386_SSE3, "SSE3" },
903 { AV_386_MON, "MON" },
904 { AV_386_CX16, "CX16" },
905 { AV_386_AHF, "AHF" },
906 { AV_386_TSCP, "TSCP" },
907 { AV_386_AMD_SSE4A, "AMD_SSE4A" },
908 { AV_386_POPCNT, "POPCNT" },
909 { AV_386_AMD_LZCNT, "AMD_LZCNT" },
910 { AV_386_SSSE3, "SSSE3" },
911 { AV_386_SSE4_1, "SSE4.1" },
912 { AV_386_SSE4_2, "SSE4.2" },
913 { 0, NULL }
914};
915
916private int
917doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
918 size_t size, off_t fsize, int *flags, int mach, int strtab)
919{
920 Elf32_Shdr sh32;
921 Elf64_Shdr sh64;
922 int stripped = 1;
923 size_t nbadcap = 0;
924 void *nbuf;
925 off_t noff, coff, name_off;
926 uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
927 uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
928 char name[50];
929
930 if (size != xsh_sizeof) {
931 if (file_printf(ms, ", corrupted section header size") == -1)
932 return -1;
933 return 0;
934 }
935
936 /* Read offset of name section to be able to read section names later */
937 if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
938 file_badread(ms);
939 return -1;
940 }
941 name_off = xsh_offset;
942
943 for ( ; num; num--) {
944 /* Read the name of this section. */
945 if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
946 file_badread(ms);
947 return -1;
948 }
949 name[sizeof(name) - 1] = '\0';
950 if (strcmp(name, ".debug_info") == 0)
951 stripped = 0;
952
953 if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
954 file_badread(ms);
955 return -1;
956 }
957 off += size;
958
959 /* Things we can determine before we seek */
960 switch (xsh_type) {
961 case SHT_SYMTAB:
962#if 0
963 case SHT_DYNSYM:
964#endif
965 stripped = 0;
966 break;
967 default:
968 if (fsize != SIZE_UNKNOWN && xsh_offset > fsize) {
969 /* Perhaps warn here */
970 continue;
971 }
972 break;
973 }
974
975 /* Things we can determine when we seek */
976 switch (xsh_type) {
977 case SHT_NOTE:
978 if ((nbuf = malloc(xsh_size)) == NULL) {
979 file_error(ms, errno, "Cannot allocate memory"
980 " for note");
981 return -1;
982 }
983 if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
984 file_badread(ms);
985 free(nbuf);
986 return -1;
987 }
988
989 noff = 0;
990 for (;;) {
991 if (noff >= (off_t)xsh_size)
992 break;
993 noff = donote(ms, nbuf, (size_t)noff,
994 xsh_size, clazz, swap, 4, flags);
995 if (noff == 0)
996 break;
997 }
998 free(nbuf);
999 break;
1000 case SHT_SUNW_cap:
1001 switch (mach) {
1002 case EM_SPARC:
1003 case EM_SPARCV9:
1004 case EM_IA_64:
1005 case EM_386:
1006 case EM_AMD64:
1007 break;
1008 default:
1009 goto skip;
1010 }
1011
1012 if (nbadcap > 5)
1013 break;
1014 if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
1015 file_badseek(ms);
1016 return -1;
1017 }
1018 coff = 0;
1019 for (;;) {
1020 Elf32_Cap cap32;
1021 Elf64_Cap cap64;
1022 char cbuf[/*CONSTCOND*/
1023 MAX(sizeof cap32, sizeof cap64)];
1024 if ((coff += xcap_sizeof) > (off_t)xsh_size)
1025 break;
1026 if (read(fd, cbuf, (size_t)xcap_sizeof) !=
1027 (ssize_t)xcap_sizeof) {
1028 file_badread(ms);
1029 return -1;
1030 }
1031 if (cbuf[0] == 'A') {
1032#ifdef notyet
1033 char *p = cbuf + 1;
1034 uint32_t len, tag;
1035 memcpy(&len, p, sizeof(len));
1036 p += 4;
1037 len = getu32(swap, len);
1038 if (memcmp("gnu", p, 3) != 0) {
1039 if (file_printf(ms,
1040 ", unknown capability %.3s", p)
1041 == -1)
1042 return -1;
1043 break;
1044 }
1045 p += strlen(p) + 1;
1046 tag = *p++;
1047 memcpy(&len, p, sizeof(len));
1048 p += 4;
1049 len = getu32(swap, len);
1050 if (tag != 1) {
1051 if (file_printf(ms, ", unknown gnu"
1052 " capability tag %d", tag)
1053 == -1)
1054 return -1;
1055 break;
1056 }
1057 // gnu attributes
1058#endif
1059 break;
1060 }
1061 (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
1062 switch (xcap_tag) {
1063 case CA_SUNW_NULL:
1064 break;
1065 case CA_SUNW_HW_1:
1066 cap_hw1 |= xcap_val;
1067 break;
1068 case CA_SUNW_SF_1:
1069 cap_sf1 |= xcap_val;
1070 break;
1071 default:
1072 if (file_printf(ms,
1073 ", with unknown capability "
1074 "0x%" INT64_T_FORMAT "x = 0x%"
1075 INT64_T_FORMAT "x",
1076 (unsigned long long)xcap_tag,
1077 (unsigned long long)xcap_val) == -1)
1078 return -1;
1079 if (nbadcap++ > 2)
1080 coff = xsh_size;
1081 break;
1082 }
1083 }
1084 /*FALLTHROUGH*/
1085 skip:
1086 default:
1087 break;
1088 }
1089 }
1090
1091 if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1092 return -1;
1093 if (cap_hw1) {
1094 const cap_desc_t *cdp;
1095 switch (mach) {
1096 case EM_SPARC:
1097 case EM_SPARC32PLUS:
1098 case EM_SPARCV9:
1099 cdp = cap_desc_sparc;
1100 break;
1101 case EM_386:
1102 case EM_IA_64:
1103 case EM_AMD64:
1104 cdp = cap_desc_386;
1105 break;
1106 default:
1107 cdp = NULL;
1108 break;
1109 }
1110 if (file_printf(ms, ", uses") == -1)
1111 return -1;
1112 if (cdp) {
1113 while (cdp->cd_name) {
1114 if (cap_hw1 & cdp->cd_mask) {
1115 if (file_printf(ms,
1116 " %s", cdp->cd_name) == -1)
1117 return -1;
1118 cap_hw1 &= ~cdp->cd_mask;
1119 }
1120 ++cdp;
1121 }
1122 if (cap_hw1)
1123 if (file_printf(ms,
1124 " unknown hardware capability 0x%"
1125 INT64_T_FORMAT "x",
1126 (unsigned long long)cap_hw1) == -1)
1127 return -1;
1128 } else {
1129 if (file_printf(ms,
1130 " hardware capability 0x%" INT64_T_FORMAT "x",
1131 (unsigned long long)cap_hw1) == -1)
1132 return -1;
1133 }
1134 }
1135 if (cap_sf1) {
1136 if (cap_sf1 & SF1_SUNW_FPUSED) {
1137 if (file_printf(ms,
1138 (cap_sf1 & SF1_SUNW_FPKNWN)
1139 ? ", uses frame pointer"
1140 : ", not known to use frame pointer") == -1)
1141 return -1;
1142 }
1143 cap_sf1 &= ~SF1_SUNW_MASK;
1144 if (cap_sf1)
1145 if (file_printf(ms,
1146 ", with unknown software capability 0x%"
1147 INT64_T_FORMAT "x",
1148 (unsigned long long)cap_sf1) == -1)
1149 return -1;
1150 }
1151 return 0;
1152}
1153
1154/*
1155 * Look through the program headers of an executable image, searching
1156 * for a PT_INTERP section; if one is found, it's dynamically linked,
1157 * otherwise it's statically linked.
1158 */
1159private int
1160dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
1161 int num, size_t size, off_t fsize, int *flags, int sh_num)
1162{
1163 Elf32_Phdr ph32;
1164 Elf64_Phdr ph64;
1165 const char *linking_style = "statically";
1166 const char *shared_libraries = "";
1167 unsigned char nbuf[BUFSIZ];
1168 ssize_t bufsize;
1169 size_t offset, align, len;
1170
1171 if (size != xph_sizeof) {
1172 if (file_printf(ms, ", corrupted program header size") == -1)
1173 return -1;
1174 return 0;
1175 }
1176
1177 for ( ; num; num--) {
1178 if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
1179 file_badread(ms);
1180 return -1;
1181 }
1182
1183 off += size;
1184
1185 /* Things we can determine before we seek */
1186 switch (xph_type) {
1187 case PT_DYNAMIC:
1188 linking_style = "dynamically";
1189 break;
1190 case PT_INTERP:
1191 shared_libraries = " (uses shared libs)";
1192 break;
1193 default:
1194 if (fsize != SIZE_UNKNOWN && xph_offset > fsize) {
1195 /* Maybe warn here? */
1196 continue;
1197 }
1198 break;
1199 }
1200
1201 /* Things we can determine when we seek */
1202 switch (xph_type) {
1203 case PT_NOTE:
1204 if (((align = xph_align) & 0x80000000UL) != 0 ||
1205 align < 4) {
1206 if (file_printf(ms,
1207 ", invalid note alignment 0x%lx",
1208 (unsigned long)align) == -1)
1209 return -1;
1210 align = 4;
1211 }
1212 if (sh_num)
1213 break;
1214 /*
1215 * This is a PT_NOTE section; loop through all the notes
1216 * in the section.
1217 */
1218 len = xph_filesz < sizeof(nbuf) ? xph_filesz
1219 : sizeof(nbuf);
1220 bufsize = pread(fd, nbuf, len, xph_offset);
1221 if (bufsize == -1) {
1222 file_badread(ms);
1223 return -1;
1224 }
1225 offset = 0;
1226 for (;;) {
1227 if (offset >= (size_t)bufsize)
1228 break;
1229 offset = donote(ms, nbuf, offset,
1230 (size_t)bufsize, clazz, swap, align,
1231 flags);
1232 if (offset == 0)
1233 break;
1234 }
1235 break;
1236 default:
1237 break;
1238 }
1239 }
1240 if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
1241 == -1)
1242 return -1;
1243 return 0;
1244}
1245
1246
1247protected int
1248file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
1249 size_t nbytes)
1250{
1251 union {
1252 int32_t l;
1253 char c[sizeof (int32_t)];
1254 } u;
1255 int clazz;
1256 int swap;
1257 struct stat st;
1258 off_t fsize;
1259 int flags = 0;
1260 Elf32_Ehdr elf32hdr;
1261 Elf64_Ehdr elf64hdr;
1262 uint16_t type, phnum, shnum;
1263
1264 if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
1265 return 0;
1266 /*
1267 * ELF executables have multiple section headers in arbitrary
1268 * file locations and thus file(1) cannot determine it from easily.
1269 * Instead we traverse thru all section headers until a symbol table
1270 * one is found or else the binary is stripped.
1271 * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
1272 */
1273 if (buf[EI_MAG0] != ELFMAG0
1274 || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
1275 || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
1276 return 0;
1277
1278 /*
1279 * If we cannot seek, it must be a pipe, socket or fifo.
1280 */
1281 if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1282 fd = file_pipe2file(ms, fd, buf, nbytes);
1283
1284 if (fstat(fd, &st) == -1) {
1285 file_badread(ms);
1286 return -1;
1287 }
1288 if (S_ISREG(st.st_mode) || st.st_size != 0)
1289 fsize = st.st_size;
1290 else
1291 fsize = SIZE_UNKNOWN;
1292
1293 clazz = buf[EI_CLASS];
1294
1295 switch (clazz) {
1296 case ELFCLASS32:
1297#undef elf_getu
1298#define elf_getu(a, b) elf_getu32(a, b)
1299#undef elfhdr
1300#define elfhdr elf32hdr
1301#include "elfclass.h"
1302 case ELFCLASS64:
1303#undef elf_getu
1304#define elf_getu(a, b) elf_getu64(a, b)
1305#undef elfhdr
1306#define elfhdr elf64hdr
1307#include "elfclass.h"
1308 default:
1309 if (file_printf(ms, ", unknown class %d", clazz) == -1)
1310 return -1;
1311 break;
1312 }
1313 return 0;
1314}
1315#endif