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