Annotation of src/usr.bin/file/readelf.c, Revision 1.12
1.12 ! deraadt 1: /* $OpenBSD: readelf.c,v 1.11 2009/10/27 23:59:38 deraadt Exp $ */
1.5 ian 2: /*
1.8 tedu 3: * Copyright (c) Christos Zoulas 2003.
4: * All Rights Reserved.
1.5 ian 5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: * 1. Redistributions of source code must retain the above copyright
10: * notice immediately at the beginning of the file, without modification,
11: * this list of conditions, and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26: * SUCH DAMAGE.
27: */
1.4 ian 28: #include "file.h"
1.1 millert 29:
30: #ifdef BUILTIN_ELF
31: #include <string.h>
32: #include <ctype.h>
33: #include <stdlib.h>
1.4 ian 34: #ifdef HAVE_UNISTD_H
1.1 millert 35: #include <unistd.h>
1.4 ian 36: #endif
1.1 millert 37:
38: #include "readelf.h"
1.10 chl 39: #include "magic.h"
1.4 ian 40:
41: #ifdef ELFCORE
1.9 chl 42: private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
43: off_t, int *);
1.4 ian 44: #endif
1.9 chl 45: private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
46: off_t, int *);
47: private int doshn(struct magic_set *, int, int, int, off_t, int, size_t, int *);
1.8 tedu 48: private size_t donote(struct magic_set *, unsigned char *, size_t, size_t, int,
1.9 chl 49: int, size_t, int *);
1.8 tedu 50:
51: #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
52:
1.9 chl 53: #define isquote(c) (strchr("'\"`", (c)) != NULL)
54:
1.8 tedu 55: private uint16_t getu16(int, uint16_t);
56: private uint32_t getu32(int, uint32_t);
57: private uint64_t getu64(int, uint64_t);
1.4 ian 58:
1.8 tedu 59: private uint16_t
1.4 ian 60: getu16(int swap, uint16_t value)
61: {
62: union {
63: uint16_t ui;
64: char c[2];
65: } retval, tmpval;
66:
67: if (swap) {
68: tmpval.ui = value;
69:
70: retval.c[0] = tmpval.c[1];
71: retval.c[1] = tmpval.c[0];
72:
73: return retval.ui;
74: } else
75: return value;
76: }
77:
1.8 tedu 78: private uint32_t
1.4 ian 79: getu32(int swap, uint32_t value)
80: {
81: union {
82: uint32_t ui;
83: char c[4];
84: } retval, tmpval;
85:
86: if (swap) {
87: tmpval.ui = value;
88:
89: retval.c[0] = tmpval.c[3];
90: retval.c[1] = tmpval.c[2];
91: retval.c[2] = tmpval.c[1];
92: retval.c[3] = tmpval.c[0];
93:
94: return retval.ui;
95: } else
96: return value;
97: }
98:
1.8 tedu 99: private uint64_t
1.4 ian 100: getu64(int swap, uint64_t value)
101: {
102: union {
103: uint64_t ui;
104: char c[8];
105: } retval, tmpval;
106:
107: if (swap) {
108: tmpval.ui = value;
109:
110: retval.c[0] = tmpval.c[7];
111: retval.c[1] = tmpval.c[6];
112: retval.c[2] = tmpval.c[5];
113: retval.c[3] = tmpval.c[4];
114: retval.c[4] = tmpval.c[3];
115: retval.c[5] = tmpval.c[2];
116: retval.c[6] = tmpval.c[1];
117: retval.c[7] = tmpval.c[0];
118:
119: return retval.ui;
120: } else
121: return value;
122: }
123:
1.10 chl 124: #define elf_getu16(swap, value) getu16(swap, value)
125: #define elf_getu32(swap, value) getu32(swap, value)
1.9 chl 126: #ifdef USE_ARRAY_FOR_64BIT_TYPES
127: # define elf_getu64(swap, array) \
1.10 chl 128: ((swap ? ((uint64_t)elf_getu32(swap, array[0])) << 32 : elf_getu32(swap, array[0])) + \
129: (swap ? elf_getu32(swap, array[1]) : ((uint64_t)elf_getu32(swap, array[1]) << 32)))
1.9 chl 130: #else
131: # define elf_getu64(swap, value) getu64(swap, value)
132: #endif
133:
1.10 chl 134: #define xsh_addr (class == ELFCLASS32 \
135: ? (void *) &sh32 \
1.4 ian 136: : (void *) &sh64)
1.10 chl 137: #define xsh_sizeof (class == ELFCLASS32 \
138: ? sizeof sh32 \
1.4 ian 139: : sizeof sh64)
1.10 chl 140: #define xsh_size (class == ELFCLASS32 \
141: ? elf_getu32(swap, sh32.sh_size) \
142: : elf_getu64(swap, sh64.sh_size))
143: #define xsh_offset (class == ELFCLASS32 \
144: ? elf_getu32(swap, sh32.sh_offset) \
145: : elf_getu64(swap, sh64.sh_offset))
146: #define xsh_type (class == ELFCLASS32 \
147: ? elf_getu32(swap, sh32.sh_type) \
148: : elf_getu32(swap, sh64.sh_type))
149: #define xph_addr (class == ELFCLASS32 \
150: ? (void *) &ph32 \
1.4 ian 151: : (void *) &ph64)
1.10 chl 152: #define xph_sizeof (class == ELFCLASS32 \
153: ? sizeof ph32 \
1.4 ian 154: : sizeof ph64)
1.10 chl 155: #define xph_type (class == ELFCLASS32 \
156: ? elf_getu32(swap, ph32.p_type) \
157: : elf_getu32(swap, ph64.p_type))
158: #define xph_offset (off_t)(class == ELFCLASS32 \
159: ? elf_getu32(swap, ph32.p_offset) \
160: : elf_getu64(swap, ph64.p_offset))
161: #define xph_align (size_t)((class == ELFCLASS32 \
162: ? (off_t) (ph32.p_align ? \
163: elf_getu32(swap, ph32.p_align) : 4) \
164: : (off_t) (ph64.p_align ? \
165: elf_getu64(swap, ph64.p_align) : 4)))
166: #define xph_filesz (size_t)((class == ELFCLASS32 \
167: ? elf_getu32(swap, ph32.p_filesz) \
168: : elf_getu64(swap, ph64.p_filesz)))
169: #define xnh_addr (class == ELFCLASS32 \
170: ? (void *) &nh32 \
1.9 chl 171: : (void *) &nh64)
1.10 chl 172: #define xph_memsz (size_t)((class == ELFCLASS32 \
173: ? elf_getu32(swap, ph32.p_memsz) \
174: : elf_getu64(swap, ph64.p_memsz)))
175: #define xnh_sizeof (class == ELFCLASS32 \
176: ? sizeof nh32 \
1.8 tedu 177: : sizeof nh64)
1.10 chl 178: #define xnh_type (class == ELFCLASS32 \
179: ? elf_getu32(swap, nh32.n_type) \
180: : elf_getu32(swap, nh64.n_type))
181: #define xnh_namesz (class == ELFCLASS32 \
182: ? elf_getu32(swap, nh32.n_namesz) \
183: : elf_getu32(swap, nh64.n_namesz))
184: #define xnh_descsz (class == ELFCLASS32 \
185: ? elf_getu32(swap, nh32.n_descsz) \
186: : elf_getu32(swap, nh64.n_descsz))
187: #define prpsoffsets(i) (class == ELFCLASS32 \
188: ? prpsoffsets32[i] \
1.4 ian 189: : prpsoffsets64[i])
1.1 millert 190:
1.4 ian 191: #ifdef ELFCORE
1.10 chl 192: /*
193: * Try larger offsets first to avoid false matches
194: * from earlier data that happen to look like strings.
195: */
196: static const size_t prpsoffsets32[] = {
197: #ifdef USE_NT_PSINFO
198: 104, /* SunOS 5.x (command line) */
199: 88, /* SunOS 5.x (short name) */
200: #endif /* USE_NT_PSINFO */
201:
202: 100, /* SunOS 5.x (command line) */
203: 84, /* SunOS 5.x (short name) */
204:
205: 44, /* Linux (command line) */
206: 28, /* Linux 2.0.36 (short name) */
207:
1.4 ian 208: 8, /* FreeBSD */
209: };
210:
1.10 chl 211: static const size_t prpsoffsets64[] = {
212: #ifdef USE_NT_PSINFO
213: 152, /* SunOS 5.x (command line) */
214: 136, /* SunOS 5.x (short name) */
215: #endif /* USE_NT_PSINFO */
216:
217: 136, /* SunOS 5.x, 64-bit (command line) */
218: 120, /* SunOS 5.x, 64-bit (short name) */
219:
220: 56, /* Linux (command line) */
221: 40, /* Linux (tested on core from 2.4.x, short name) */
222:
1.9 chl 223: 16, /* FreeBSD, 64-bit */
1.1 millert 224: };
225:
1.4 ian 226: #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
227: #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
228:
229: #define NOFFSETS (class == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
1.1 millert 230:
231: /*
232: * Look through the program headers of an executable image, searching
1.4 ian 233: * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
234: * "FreeBSD"; if one is found, try looking in various places in its
235: * contents for a 16-character string containing only printable
236: * characters - if found, that string should be the name of the program
237: * that dropped core. Note: right after that 16-character string is,
238: * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
239: * Linux, a longer string (80 characters, in 5.x, probably other
240: * SVR4-flavored systems, and Linux) containing the start of the
241: * command line for that program.
242: *
1.10 chl 243: * SunOS 5.x core files contain two PT_NOTE sections, with the types
244: * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
245: * same info about the command name and command line, so it probably
246: * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
247: * above (see USE_NT_PSINFO), in case we ever decide to do so. The
248: * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
249: * the SunOS 5.x file command relies on this (and prefers the latter).
250: *
1.4 ian 251: * The signal number probably appears in a section of type NT_PRSTATUS,
252: * but that's also rather OS-dependent, in ways that are harder to
253: * dissect with heuristics, so I'm not bothering with the signal number.
254: * (I suppose the signal number could be of interest in situations where
255: * you don't have the binary of the program that dropped core; if you
256: * *do* have that binary, the debugger will probably tell you what
257: * signal it was.)
1.1 millert 258: */
1.4 ian 259:
260: #define OS_STYLE_SVR4 0
261: #define OS_STYLE_FREEBSD 1
262: #define OS_STYLE_NETBSD 2
263:
1.10 chl 264: private const char os_style_names[][8] = {
1.4 ian 265: "SVR4",
266: "FreeBSD",
267: "NetBSD",
268: };
269:
1.9 chl 270: #define FLAGS_DID_CORE 1
271: #define FLAGS_DID_NOTE 2
1.10 chl 272: #define FLAGS_DID_CORE_STYLE 4
1.9 chl 273:
1.8 tedu 274: private int
275: dophn_core(struct magic_set *ms, int class, int swap, int fd, off_t off,
1.9 chl 276: int num, size_t size, off_t fsize, int *flags)
1.1 millert 277: {
1.4 ian 278: Elf32_Phdr ph32;
279: Elf64_Phdr ph64;
1.8 tedu 280: size_t offset;
281: unsigned char nbuf[BUFSIZ];
282: ssize_t bufsize;
1.9 chl 283: off_t savedoffset;
284: struct stat st;
1.8 tedu 285:
1.9 chl 286: if (fstat(fd, &st) < 0) {
287: file_badread(ms);
288: return -1;
289: }
290:
291: if (size != xph_sizeof) {
1.8 tedu 292: if (file_printf(ms, ", corrupted program header size") == -1)
293: return -1;
294: return 0;
295: }
1.9 chl 296:
1.4 ian 297: /*
298: * Loop through all the program headers.
299: */
1.1 millert 300: for ( ; num; num--) {
1.9 chl 301: if ((savedoffset = lseek(fd, off, SEEK_SET)) == (off_t)-1) {
1.8 tedu 302: file_badseek(ms);
303: return -1;
304: }
1.9 chl 305: if (read(fd, xph_addr, xph_sizeof) == -1) {
1.8 tedu 306: file_badread(ms);
307: return -1;
308: }
1.9 chl 309: if (xph_offset > fsize) {
310: if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
311: file_badseek(ms);
312: return -1;
313: }
314: continue;
315: }
316:
1.1 millert 317: off += size;
1.9 chl 318: if (xph_type != PT_NOTE)
1.1 millert 319: continue;
1.4 ian 320:
321: /*
322: * This is a PT_NOTE section; loop through all the notes
323: * in the section.
324: */
1.9 chl 325: if (lseek(fd, xph_offset, SEEK_SET) == (off_t)-1) {
1.8 tedu 326: file_badseek(ms);
327: return -1;
328: }
1.9 chl 329: bufsize = read(fd, nbuf,
330: ((xph_filesz < sizeof(nbuf)) ? xph_filesz : sizeof(nbuf)));
1.8 tedu 331: if (bufsize == -1) {
332: file_badread(ms);
333: return -1;
334: }
1.1 millert 335: offset = 0;
336: for (;;) {
1.8 tedu 337: if (offset >= (size_t)bufsize)
338: break;
339: offset = donote(ms, nbuf, offset, (size_t)bufsize,
1.9 chl 340: class, swap, 4, flags);
1.8 tedu 341: if (offset == 0)
1.1 millert 342: break;
343:
1.8 tedu 344: }
345: }
346: return 0;
347: }
348: #endif
349:
350: private size_t
351: donote(struct magic_set *ms, unsigned char *nbuf, size_t offset, size_t size,
1.9 chl 352: int class, int swap, size_t align, int *flags)
1.8 tedu 353: {
354: Elf32_Nhdr nh32;
355: Elf64_Nhdr nh64;
356: size_t noff, doff;
357: #ifdef ELFCORE
358: int os_style = -1;
359: #endif
360: uint32_t namesz, descsz;
1.12 ! deraadt 361:
! 362: if (xnh_sizeof + offset > size) {
! 363: /*
! 364: * We're out of note headers.
! 365: */
! 366: return xnh_sizeof + offset;
! 367: }
1.8 tedu 368:
1.9 chl 369: (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
370: offset += xnh_sizeof;
1.8 tedu 371:
1.9 chl 372: namesz = xnh_namesz;
373: descsz = xnh_descsz;
1.8 tedu 374: if ((namesz == 0) && (descsz == 0)) {
375: /*
376: * We're out of note headers.
377: */
1.9 chl 378: return (offset >= size) ? offset : size;
1.8 tedu 379: }
380:
381: if (namesz & 0x80000000) {
382: (void)file_printf(ms, ", bad note name size 0x%lx",
383: (unsigned long)namesz);
384: return offset;
385: }
386:
387: if (descsz & 0x80000000) {
388: (void)file_printf(ms, ", bad note description size 0x%lx",
389: (unsigned long)descsz);
390: return offset;
391: }
392:
393:
394: noff = offset;
395: doff = ELF_ALIGN(offset + namesz);
396:
1.9 chl 397: if (offset + namesz > size) {
1.8 tedu 398: /*
399: * We're past the end of the buffer.
400: */
401: return doff;
402: }
403:
404: offset = ELF_ALIGN(doff + descsz);
1.9 chl 405: if (doff + descsz > size) {
406: /*
407: * We're past the end of the buffer.
408: */
409: return (offset >= size) ? offset : size;
1.8 tedu 410: }
411:
1.9 chl 412: if (*flags & FLAGS_DID_NOTE)
413: goto core;
414:
1.8 tedu 415: if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
1.9 chl 416: xnh_type == NT_GNU_VERSION && descsz == 16) {
1.8 tedu 417: uint32_t desc[4];
418: (void)memcpy(desc, &nbuf[doff], sizeof(desc));
419:
420: if (file_printf(ms, ", for GNU/") == -1)
421: return size;
1.10 chl 422: switch (elf_getu32(swap, desc[0])) {
1.8 tedu 423: case GNU_OS_LINUX:
424: if (file_printf(ms, "Linux") == -1)
425: return size;
426: break;
427: case GNU_OS_HURD:
428: if (file_printf(ms, "Hurd") == -1)
429: return size;
430: break;
431: case GNU_OS_SOLARIS:
432: if (file_printf(ms, "Solaris") == -1)
433: return size;
434: break;
1.10 chl 435: case GNU_OS_KFREEBSD:
436: if (file_printf(ms, "kFreeBSD") == -1)
437: return size;
438: break;
439: case GNU_OS_KNETBSD:
440: if (file_printf(ms, "kNetBSD") == -1)
441: return size;
442: break;
1.8 tedu 443: default:
444: if (file_printf(ms, "<unknown>") == -1)
445: return size;
446: }
1.10 chl 447: if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
448: elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
1.8 tedu 449: return size;
1.9 chl 450: *flags |= FLAGS_DID_NOTE;
1.8 tedu 451: return size;
452: }
453:
454: if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
1.9 chl 455: xnh_type == NT_NETBSD_VERSION && descsz == 4) {
1.8 tedu 456: uint32_t desc;
457: (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10 chl 458: desc = elf_getu32(swap, desc);
1.8 tedu 459:
460: if (file_printf(ms, ", for NetBSD") == -1)
461: return size;
462: /*
463: * The version number used to be stuck as 199905, and was thus
464: * basically content-free. Newer versions of NetBSD have fixed
465: * this and now use the encoding of __NetBSD_Version__:
466: *
467: * MMmmrrpp00
468: *
469: * M = major version
470: * m = minor version
471: * r = release ["",A-Z,Z[A-Z] but numeric]
472: * p = patchlevel
473: */
474: if (desc > 100000000U) {
1.9 chl 475: uint32_t ver_patch = (desc / 100) % 100;
476: uint32_t ver_rel = (desc / 10000) % 100;
477: uint32_t ver_min = (desc / 1000000) % 100;
478: uint32_t ver_maj = desc / 100000000;
1.8 tedu 479:
480: if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
481: return size;
482: if (ver_rel == 0 && ver_patch != 0) {
483: if (file_printf(ms, ".%u", ver_patch) == -1)
484: return size;
485: } else if (ver_rel != 0) {
486: while (ver_rel > 26) {
1.9 chl 487: if (file_printf(ms, "Z") == -1)
488: return size;
1.8 tedu 489: ver_rel -= 26;
490: }
1.9 chl 491: if (file_printf(ms, "%c", 'A' + ver_rel - 1)
492: == -1)
493: return size;
1.1 millert 494: }
1.8 tedu 495: }
1.9 chl 496: *flags |= FLAGS_DID_NOTE;
1.8 tedu 497: return size;
498: }
1.4 ian 499:
1.8 tedu 500: if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
1.9 chl 501: xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
1.8 tedu 502: uint32_t desc;
503: (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10 chl 504: desc = elf_getu32(swap, desc);
1.8 tedu 505: if (file_printf(ms, ", for FreeBSD") == -1)
506: return size;
1.1 millert 507:
1.8 tedu 508: /*
509: * Contents is __FreeBSD_version, whose relation to OS
1.9 chl 510: * versions is defined by a huge table in the Porter's
511: * Handbook. This is the general scheme:
512: *
513: * Releases:
514: * Mmp000 (before 4.10)
515: * Mmi0p0 (before 5.0)
516: * Mmm0p0
517: *
518: * Development branches:
519: * Mmpxxx (before 4.6)
520: * Mmp1xx (before 4.10)
521: * Mmi1xx (before 5.0)
522: * M000xx (pre-M.0)
523: * Mmm1xx
524: *
525: * M = major version
526: * m = minor version
527: * i = minor version increment (491000 -> 4.10)
528: * p = patchlevel
529: * x = revision
530: *
531: * The first release of FreeBSD to use ELF by default
532: * was version 3.0.
1.8 tedu 533: */
1.9 chl 534: if (desc == 460002) {
535: if (file_printf(ms, " 4.6.2") == -1)
536: return size;
537: } else if (desc < 460100) {
1.8 tedu 538: if (file_printf(ms, " %d.%d", desc / 100000,
539: desc / 10000 % 10) == -1)
540: return size;
541: if (desc / 1000 % 10 > 0)
542: if (file_printf(ms, ".%d", desc / 1000 % 10)
543: == -1)
544: return size;
1.9 chl 545: if ((desc % 1000 > 0) || (desc % 100000 == 0))
546: if (file_printf(ms, " (%d)", desc) == -1)
547: return size;
548: } else if (desc < 500000) {
549: if (file_printf(ms, " %d.%d", desc / 100000,
550: desc / 10000 % 10 + desc / 1000 % 10) == -1)
551: return size;
552: if (desc / 100 % 10 > 0) {
553: if (file_printf(ms, " (%d)", desc) == -1)
554: return size;
555: } else if (desc / 10 % 10 > 0) {
556: if (file_printf(ms, ".%d", desc / 10 % 10)
557: == -1)
558: return size;
559: }
1.8 tedu 560: } else {
561: if (file_printf(ms, " %d.%d", desc / 100000,
562: desc / 1000 % 100) == -1)
563: return size;
1.9 chl 564: if ((desc / 100 % 10 > 0) ||
565: (desc % 100000 / 100 == 0)) {
566: if (file_printf(ms, " (%d)", desc) == -1)
1.8 tedu 567: return size;
1.9 chl 568: } else if (desc / 10 % 10 > 0) {
569: if (file_printf(ms, ".%d", desc / 10 % 10)
570: == -1)
1.8 tedu 571: return size;
1.4 ian 572: }
1.8 tedu 573: }
1.9 chl 574: *flags |= FLAGS_DID_NOTE;
1.8 tedu 575: return size;
576: }
577:
578: if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
1.9 chl 579: xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
1.8 tedu 580: if (file_printf(ms, ", for OpenBSD") == -1)
581: return size;
582: /* Content of note is always 0 */
1.9 chl 583: *flags |= FLAGS_DID_NOTE;
1.8 tedu 584: return size;
585: }
586:
1.9 chl 587: if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
588: xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
589: uint32_t desc;
590: if (file_printf(ms, ", for DragonFly") == -1)
591: return size;
592: (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
1.10 chl 593: desc = elf_getu32(swap, desc);
1.9 chl 594: if (file_printf(ms, " %d.%d.%d", desc / 100000,
595: desc / 10000 % 10, desc % 10000) == -1)
596: return size;
597: *flags |= FLAGS_DID_NOTE;
598: return size;
599: }
600:
601: core:
1.8 tedu 602: /*
603: * Sigh. The 2.0.36 kernel in Debian 2.1, at
604: * least, doesn't correctly implement name
605: * sections, in core dumps, as specified by
606: * the "Program Linking" section of "UNIX(R) System
607: * V Release 4 Programmer's Guide: ANSI C and
608: * Programming Support Tools", because my copy
609: * clearly says "The first 'namesz' bytes in 'name'
610: * contain a *null-terminated* [emphasis mine]
611: * character representation of the entry's owner
612: * or originator", but the 2.0.36 kernel code
613: * doesn't include the terminating null in the
614: * name....
615: */
616: if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
617: (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
618: os_style = OS_STYLE_SVR4;
619: }
620:
621: if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
622: os_style = OS_STYLE_FREEBSD;
623: }
624:
625: if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
626: == 0)) {
627: os_style = OS_STYLE_NETBSD;
628: }
1.4 ian 629:
1.8 tedu 630: #ifdef ELFCORE
1.9 chl 631: if ((*flags & FLAGS_DID_CORE) != 0)
632: return size;
633:
1.10 chl 634: if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
1.9 chl 635: if (file_printf(ms, ", %s-style", os_style_names[os_style])
636: == -1)
1.8 tedu 637: return size;
1.10 chl 638: *flags |= FLAGS_DID_CORE_STYLE;
1.9 chl 639: }
1.4 ian 640:
1.9 chl 641: switch (os_style) {
642: case OS_STYLE_NETBSD:
643: if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
644: uint32_t signo;
645: /*
646: * Extract the program name. It is at
647: * offset 0x7c, and is up to 32-bytes,
648: * including the terminating NUL.
649: */
650: if (file_printf(ms, ", from '%.31s'",
651: &nbuf[doff + 0x7c]) == -1)
652: return size;
653:
654: /*
655: * Extract the signal number. It is at
656: * offset 0x08.
657: */
658: (void)memcpy(&signo, &nbuf[doff + 0x08],
659: sizeof(signo));
660: if (file_printf(ms, " (signal %u)",
1.10 chl 661: elf_getu32(swap, signo)) == -1)
1.9 chl 662: return size;
1.10 chl 663: *flags |= FLAGS_DID_CORE;
1.8 tedu 664: return size;
1.9 chl 665: }
666: break;
1.8 tedu 667:
1.9 chl 668: default:
669: if (xnh_type == NT_PRPSINFO) {
670: size_t i, j;
671: unsigned char c;
672: /*
673: * Extract the program name. We assume
674: * it to be 16 characters (that's what it
675: * is in SunOS 5.x and Linux).
676: *
677: * Unfortunately, it's at a different offset
678: * in various OSes, so try multiple offsets.
679: * If the characters aren't all printable,
680: * reject it.
681: */
682: for (i = 0; i < NOFFSETS; i++) {
1.10 chl 683: unsigned char *cname, *cp;
1.9 chl 684: size_t reloffset = prpsoffsets(i);
685: size_t noffset = doff + reloffset;
686: for (j = 0; j < 16; j++, noffset++,
687: reloffset++) {
1.8 tedu 688: /*
1.9 chl 689: * Make sure we're not past
690: * the end of the buffer; if
691: * we are, just give up.
1.8 tedu 692: */
1.9 chl 693: if (noffset >= size)
1.8 tedu 694: goto tryanother;
1.9 chl 695:
1.1 millert 696: /*
1.9 chl 697: * Make sure we're not past
698: * the end of the contents;
699: * if we are, this obviously
700: * isn't the right offset.
1.1 millert 701: */
1.9 chl 702: if (reloffset >= descsz)
1.8 tedu 703: goto tryanother;
1.9 chl 704:
705: c = nbuf[noffset];
706: if (c == '\0') {
707: /*
708: * A '\0' at the
709: * beginning is
710: * obviously wrong.
711: * Any other '\0'
712: * means we're done.
713: */
714: if (j == 0)
715: goto tryanother;
716: else
717: break;
718: } else {
719: /*
720: * A nonprintable
721: * character is also
722: * wrong.
723: */
724: if (!isprint(c) || isquote(c))
725: goto tryanother;
726: }
1.8 tedu 727: }
1.9 chl 728: /*
729: * Well, that worked.
730: */
1.10 chl 731: cname = (unsigned char *)
732: &nbuf[doff + prpsoffsets(i)];
733: for (cp = cname; *cp && isprint(*cp); cp++)
734: continue;
735: /*
736: * Linux apparently appends a space at the end
737: * of the command line: remove it.
738: */
739: while (cp > cname && isspace(cp[-1]))
740: cp--;
741: if (file_printf(ms, ", from '%.*s'",
742: (int)(cp - cname), cname) == -1)
1.9 chl 743: return size;
1.10 chl 744: *flags |= FLAGS_DID_CORE;
1.8 tedu 745: return size;
746:
1.9 chl 747: tryanother:
748: ;
749: }
1.8 tedu 750: }
1.9 chl 751: break;
1.8 tedu 752: }
753: #endif
754: return offset;
755: }
756:
757: private int
758: doshn(struct magic_set *ms, int class, int swap, int fd, off_t off, int num,
1.9 chl 759: size_t size, int *flags)
1.8 tedu 760: {
761: Elf32_Shdr sh32;
762: Elf64_Shdr sh64;
1.9 chl 763: int stripped = 1;
764: void *nbuf;
765: off_t noff;
1.8 tedu 766:
1.9 chl 767: if (size != xsh_sizeof) {
1.8 tedu 768: if (file_printf(ms, ", corrupted section header size") == -1)
769: return -1;
770: return 0;
771: }
772:
773: if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
774: file_badseek(ms);
775: return -1;
776: }
777:
778: for ( ; num; num--) {
1.9 chl 779: if (read(fd, xsh_addr, xsh_sizeof) == -1) {
1.8 tedu 780: file_badread(ms);
781: return -1;
782: }
1.9 chl 783: switch (xsh_type) {
784: case SHT_SYMTAB:
785: #if 0
786: case SHT_DYNSYM:
787: #endif
788: stripped = 0;
789: break;
790: case SHT_NOTE:
791: if ((off = lseek(fd, (off_t)0, SEEK_CUR)) ==
792: (off_t)-1) {
793: file_badread(ms);
794: return -1;
795: }
796: if ((nbuf = malloc((size_t)xsh_size)) == NULL) {
797: file_error(ms, errno, "Cannot allocate memory"
798: " for note");
799: return -1;
800: }
801: if ((noff = lseek(fd, (off_t)xsh_offset, SEEK_SET)) ==
802: (off_t)-1) {
803: file_badread(ms);
804: free(nbuf);
805: return -1;
806: }
807: if (read(fd, nbuf, (size_t)xsh_size) !=
808: (ssize_t)xsh_size) {
809: free(nbuf);
810: file_badread(ms);
811: return -1;
812: }
813:
814: noff = 0;
815: for (;;) {
816: if (noff >= (size_t)xsh_size)
817: break;
818: noff = donote(ms, nbuf, (size_t)noff,
819: (size_t)xsh_size, class, swap, 4,
820: flags);
821: if (noff == 0)
822: break;
823: }
824: if ((lseek(fd, off, SEEK_SET)) == (off_t)-1) {
825: free(nbuf);
826: file_badread(ms);
1.8 tedu 827: return -1;
1.9 chl 828: }
829: free(nbuf);
830: break;
1.8 tedu 831: }
832: }
1.9 chl 833: if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
1.8 tedu 834: return -1;
835: return 0;
836: }
837:
838: /*
839: * Look through the program headers of an executable image, searching
840: * for a PT_INTERP section; if one is found, it's dynamically linked,
841: * otherwise it's statically linked.
842: */
843: private int
844: dophn_exec(struct magic_set *ms, int class, int swap, int fd, off_t off,
1.9 chl 845: int num, size_t size, off_t fsize, int *flags)
1.8 tedu 846: {
847: Elf32_Phdr ph32;
848: Elf64_Phdr ph64;
849: const char *linking_style = "statically";
850: const char *shared_libraries = "";
851: unsigned char nbuf[BUFSIZ];
852: int bufsize;
853: size_t offset, align;
1.9 chl 854: off_t savedoffset = (off_t)-1;
855: struct stat st;
1.8 tedu 856:
1.9 chl 857: if (fstat(fd, &st) < 0) {
858: file_badread(ms);
859: return -1;
860: }
861:
862: if (size != xph_sizeof) {
1.8 tedu 863: if (file_printf(ms, ", corrupted program header size") == -1)
1.10 chl 864: return -1;
1.8 tedu 865: return 0;
866: }
1.9 chl 867:
1.8 tedu 868: if (lseek(fd, off, SEEK_SET) == (off_t)-1) {
869: file_badseek(ms);
870: return -1;
871: }
872:
873: for ( ; num; num--) {
1.9 chl 874: if (read(fd, xph_addr, xph_sizeof) == -1) {
1.8 tedu 875: file_badread(ms);
876: return -1;
877: }
1.9 chl 878: if (xph_offset > st.st_size && savedoffset != (off_t)-1) {
879: if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
880: file_badseek(ms);
881: return -1;
882: }
883: continue;
884: }
885:
1.8 tedu 886: if ((savedoffset = lseek(fd, (off_t)0, SEEK_CUR)) == (off_t)-1) {
887: file_badseek(ms);
888: return -1;
889: }
890:
1.9 chl 891: if (xph_offset > fsize) {
892: if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
893: file_badseek(ms);
894: return -1;
895: }
896: continue;
897: }
898:
899: switch (xph_type) {
1.8 tedu 900: case PT_DYNAMIC:
901: linking_style = "dynamically";
902: break;
903: case PT_INTERP:
904: shared_libraries = " (uses shared libs)";
905: break;
906: case PT_NOTE:
1.9 chl 907: if ((align = xph_align) & 0x80000000) {
1.8 tedu 908: if (file_printf(ms,
909: ", invalid note alignment 0x%lx",
910: (unsigned long)align) == -1)
911: return -1;
912: align = 4;
913: }
914: /*
915: * This is a PT_NOTE section; loop through all the notes
916: * in the section.
917: */
1.9 chl 918: if (lseek(fd, xph_offset, SEEK_SET)
1.8 tedu 919: == (off_t)-1) {
920: file_badseek(ms);
921: return -1;
922: }
1.9 chl 923: bufsize = read(fd, nbuf, ((xph_filesz < sizeof(nbuf)) ?
924: xph_filesz : sizeof(nbuf)));
1.8 tedu 925: if (bufsize == -1) {
926: file_badread(ms);
927: return -1;
928: }
929: offset = 0;
930: for (;;) {
931: if (offset >= (size_t)bufsize)
932: break;
933: offset = donote(ms, nbuf, offset,
1.9 chl 934: (size_t)bufsize, class, swap, align,
935: flags);
1.8 tedu 936: if (offset == 0)
1.4 ian 937: break;
1.1 millert 938: }
1.9 chl 939: if (lseek(fd, savedoffset, SEEK_SET) == (off_t)-1) {
1.8 tedu 940: file_badseek(ms);
941: return -1;
942: }
943: break;
1.10 chl 944: default:
945: break;
1.1 millert 946: }
947: }
1.8 tedu 948: if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
949: == -1)
950: return -1;
951: return 0;
1.1 millert 952: }
953:
1.8 tedu 954:
955: protected int
956: file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
957: size_t nbytes)
1.1 millert 958: {
959: union {
1.3 itojun 960: int32_t l;
961: char c[sizeof (int32_t)];
1.1 millert 962: } u;
1.4 ian 963: int class;
964: int swap;
1.9 chl 965: struct stat st;
966: off_t fsize;
967: int flags = 0;
1.10 chl 968: Elf32_Ehdr elf32hdr;
969: Elf64_Ehdr elf64hdr;
970: uint16_t type;
971:
972: if (ms->flags & MAGIC_MIME)
973: return 0;
974: /*
975: * ELF executables have multiple section headers in arbitrary
976: * file locations and thus file(1) cannot determine it from easily.
977: * Instead we traverse thru all section headers until a symbol table
978: * one is found or else the binary is stripped.
979: * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
980: */
981: if (buf[EI_MAG0] != ELFMAG0
982: || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
983: || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
984: return 0;
1.4 ian 985:
986: /*
1.8 tedu 987: * If we cannot seek, it must be a pipe, socket or fifo.
1.4 ian 988: */
989: if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
1.8 tedu 990: fd = file_pipe2file(ms, fd, buf, nbytes);
1.1 millert 991:
1.9 chl 992: if (fstat(fd, &st) == -1) {
993: file_badread(ms);
994: return -1;
995: }
996: fsize = st.st_size;
997:
998: class = buf[EI_CLASS];
1.4 ian 999:
1.10 chl 1000: switch (class) {
1001: case ELFCLASS32:
1002: #undef elf_getu
1003: #define elf_getu(a, b) elf_getu32(a, b)
1004: #undef elfhdr
1005: #define elfhdr elf32hdr
1006: #include "elfclass.h"
1007: case ELFCLASS64:
1008: #undef elf_getu
1009: #define elf_getu(a, b) elf_getu64(a, b)
1010: #undef elfhdr
1011: #define elfhdr elf64hdr
1012: #include "elfclass.h"
1013: default:
1014: if (file_printf(ms, ", unknown class %d", class) == -1)
1015: return -1;
1016: break;
1.1 millert 1017: }
1.8 tedu 1018: return 0;
1.1 millert 1019: }
1020: #endif